protocol_handler.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_
  5. #define COMPONENTS_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/time/time.h"
  9. #include "base/values.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "third_party/blink/public/common/security/protocol_handler_security_level.h"
  12. #include "url/gurl.h"
  13. namespace custom_handlers {
  14. // A single tuple of (protocol, url, last_modified) that indicates how URLs
  15. // of the given protocol should be rewritten to be handled.
  16. // The |last_modified| field is used to correctly perform deletion
  17. // of protocol handlers based on time ranges.
  18. class ProtocolHandler {
  19. public:
  20. static ProtocolHandler CreateProtocolHandler(
  21. const std::string& protocol,
  22. const GURL& url,
  23. blink::ProtocolHandlerSecurityLevel security_level =
  24. blink::ProtocolHandlerSecurityLevel::kStrict);
  25. ProtocolHandler(const std::string& protocol,
  26. const GURL& url,
  27. base::Time last_modified,
  28. blink::ProtocolHandlerSecurityLevel security_level);
  29. static ProtocolHandler CreateWebAppProtocolHandler(
  30. const std::string& protocol,
  31. const GURL& url,
  32. const std::string& app_id);
  33. ProtocolHandler(const std::string& protocol,
  34. const GURL& url,
  35. const std::string& app_id,
  36. base::Time last_modified,
  37. blink::ProtocolHandlerSecurityLevel security_level);
  38. ProtocolHandler(const ProtocolHandler& other);
  39. ~ProtocolHandler();
  40. // Creates a ProtocolHandler with fields from the dictionary. Returns an
  41. // empty ProtocolHandler if the input is invalid.
  42. static ProtocolHandler CreateProtocolHandler(const base::Value::Dict& value);
  43. // Returns true if the dictionary value has all the necessary fields to
  44. // define a ProtocolHandler.
  45. static bool IsValidDict(const base::Value::Dict& value);
  46. // Return true if the protocol handler meets security constraints.
  47. // Verify custom handler URLs security and syntax as well as the schemes
  48. // safelist as described in steps 1, 2, 6 and 7 (except same origin).
  49. // https://html.spec.whatwg.org/multipage/system-state.html#custom-handlers.
  50. bool IsValid() const;
  51. // Returns true if this handler's url has the same origin as the given one.
  52. bool IsSameOrigin(const ProtocolHandler& handler) const;
  53. // Canonical empty ProtocolHandler.
  54. static const ProtocolHandler& EmptyProtocolHandler();
  55. // Interpolates the given URL into the URL template of this handler.
  56. GURL TranslateUrl(const GURL& url) const;
  57. // Returns true if the handlers are considered equivalent when determining
  58. // if both handlers can be registered, or if a handler has previously been
  59. // ignored.
  60. bool IsEquivalent(const ProtocolHandler& other) const;
  61. // Encodes this protocol handler as a DictionaryValue.
  62. base::Value::Dict Encode() const;
  63. // Returns a friendly name for |protocol| if one is available, otherwise
  64. // this function returns |protocol|.
  65. static std::u16string GetProtocolDisplayName(const std::string& protocol);
  66. // Returns a friendly name for |this.protocol_| if one is available, otherwise
  67. // this function returns |this.protocol_|.
  68. std::u16string GetProtocolDisplayName() const;
  69. const std::string& protocol() const { return protocol_; }
  70. const GURL& url() const { return url_; }
  71. const absl::optional<std::string>& web_app_id() const { return web_app_id_; }
  72. const base::Time& last_modified() const { return last_modified_; }
  73. bool IsEmpty() const { return protocol_.empty(); }
  74. #if !defined(NDEBUG)
  75. // Returns a string representation suitable for use in debugging.
  76. std::string ToString() const;
  77. #endif
  78. bool operator==(const ProtocolHandler& other) const;
  79. bool operator<(const ProtocolHandler& other) const;
  80. private:
  81. ProtocolHandler();
  82. std::string protocol_;
  83. GURL url_;
  84. absl::optional<std::string> web_app_id_;
  85. base::Time last_modified_;
  86. blink::ProtocolHandlerSecurityLevel security_level_;
  87. };
  88. } // namespace custom_handlers
  89. #endif // COMPONENTS_CUSTOM_HANDLERS_PROTOCOL_HANDLER_H_