| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef URL_ORIGIN_H_ | 5 #ifndef URL_ORIGIN_H_ |
| 6 #define URL_ORIGIN_H_ | 6 #define URL_ORIGIN_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 // Creates an Origin from |url|, as described at | 83 // Creates an Origin from |url|, as described at |
| 84 // https://url.spec.whatwg.org/#origin, with the following additions: | 84 // https://url.spec.whatwg.org/#origin, with the following additions: |
| 85 // | 85 // |
| 86 // 1. If |url| is invalid or non-standard, a unique Origin is constructed. | 86 // 1. If |url| is invalid or non-standard, a unique Origin is constructed. |
| 87 // 2. 'filesystem' URLs behave as 'blob' URLs (that is, the origin is parsed | 87 // 2. 'filesystem' URLs behave as 'blob' URLs (that is, the origin is parsed |
| 88 // out of everything in the URL which follows the scheme). | 88 // out of everything in the URL which follows the scheme). |
| 89 // 3. 'file' URLs all parse as ("file", "", 0). | 89 // 3. 'file' URLs all parse as ("file", "", 0). |
| 90 explicit Origin(const GURL& url); | 90 explicit Origin(const GURL& url); |
| 91 | 91 |
| 92 // A convenience: Origin(GURL(url_string)). |
| 93 explicit Origin(const std::string& url_string); |
| 94 |
| 92 // Creates an Origin from a |scheme|, |host|, and |port|. All the parameters | 95 // Creates an Origin from a |scheme|, |host|, and |port|. All the parameters |
| 93 // must be valid and canonicalized. In particular, note that this cannot be | 96 // must be valid and canonicalized. In particular, note that this cannot be |
| 94 // used to create unique origins; 'url::Origin()' is the right way to do that. | 97 // used to create unique origins; 'url::Origin()' is the right way to do that. |
| 95 // | 98 // |
| 96 // This constructor should be used in order to pass 'Origin' objects back and | 99 // This constructor should be used in order to pass 'Origin' objects back and |
| 97 // forth over IPC (as transitioning through GURL would risk potentially | 100 // forth over IPC (as transitioning through GURL would risk potentially |
| 98 // dangerous recanonicalization); other potential callers should prefer the | 101 // dangerous recanonicalization); other potential callers should prefer the |
| 99 // 'GURL'-based constructor. | 102 // 'GURL'-based constructor. |
| 100 static Origin UnsafelyCreateOriginWithoutNormalization( | 103 static Origin UnsafelyCreateOriginWithoutNormalization( |
| 101 base::StringPiece scheme, | 104 base::StringPiece scheme, |
| 102 base::StringPiece host, | 105 base::StringPiece host, |
| 103 uint16_t port); | 106 uint16_t port); |
| 104 | 107 |
| 105 ~Origin(); | 108 ~Origin(); |
| 106 | 109 |
| 107 // For unique origins, these return ("", "", 0). | 110 // For unique origins, these return ("", "", 0). |
| 108 const std::string& scheme() const { return tuple_.scheme(); } | 111 const std::string& scheme() const { return tuple_.scheme(); } |
| 109 const std::string& host() const { return tuple_.host(); } | 112 const std::string& host() const { return tuple_.host(); } |
| 110 uint16_t port() const { return tuple_.port(); } | 113 uint16_t port() const { return tuple_.port(); } |
| 111 | 114 |
| 112 bool unique() const { return unique_; } | 115 bool unique() const { return unique_; } |
| 113 | 116 |
| 117 // An Origin is empty if it was constructed from an empty GURL or empty |
| 118 // string. Empty Origins are equal to each other, but not to unique Origins. |
| 119 bool empty() const { return empty_; } |
| 120 |
| 114 // An ASCII serialization of the Origin as per Section 6.2 of RFC 6454, with | 121 // An ASCII serialization of the Origin as per Section 6.2 of RFC 6454, with |
| 115 // the addition that all Origins with a 'file' scheme serialize to "file://". | 122 // the addition that all Origins with a 'file' scheme serialize to "file://". |
| 116 std::string Serialize() const; | 123 std::string Serialize() const; |
| 117 | 124 |
| 118 // Two Origins are "same-origin" if their schemes, hosts, and ports are exact | 125 // Two Origins are "same-origin" if their schemes, hosts, and ports are exact |
| 119 // matches; and neither is unique. | 126 // matches; and neither is unique. |
| 120 bool IsSameOriginWith(const Origin& other) const; | 127 bool IsSameOriginWith(const Origin& other) const; |
| 121 bool operator==(const Origin& other) const { | 128 bool operator==(const Origin& other) const { |
| 122 return IsSameOriginWith(other); | 129 return IsSameOriginWith(other); |
| 123 } | 130 } |
| 124 | 131 |
| 125 // Allows Origin to be used as a key in STL (for example, a std::set or | 132 // Allows Origin to be used as a key in STL (for example, a std::set or |
| 126 // std::map). | 133 // std::map). |
| 127 bool operator<(const Origin& other) const; | 134 bool operator<(const Origin& other) const; |
| 128 | 135 |
| 129 private: | 136 private: |
| 130 Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port); | 137 Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port); |
| 131 | 138 |
| 132 SchemeHostPort tuple_; | 139 SchemeHostPort tuple_; |
| 133 bool unique_; | 140 bool unique_; |
| 141 bool empty_; |
| 134 }; | 142 }; |
| 135 | 143 |
| 136 URL_EXPORT std::ostream& operator<<(std::ostream& out, const Origin& origin); | 144 URL_EXPORT std::ostream& operator<<(std::ostream& out, const Origin& origin); |
| 137 | 145 |
| 138 URL_EXPORT bool IsSameOriginWith(const GURL& a, const GURL& b); | 146 URL_EXPORT bool IsSameOriginWith(const GURL& a, const GURL& b); |
| 139 | 147 |
| 140 } // namespace url | 148 } // namespace url |
| 141 | 149 |
| 142 #endif // URL_ORIGIN_H_ | 150 #endif // URL_ORIGIN_H_ |
| OLD | NEW |