| 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 #include "url/origin.h" | 5 #include "url/origin.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 DCHECK(url.scheme() == kHttpsScheme); | 27 DCHECK(url.scheme() == kHttpsScheme); |
| 28 replacements.SetSchemeStr(kHttpsSuboriginScheme); | 28 replacements.SetSchemeStr(kHttpsSuboriginScheme); |
| 29 } | 29 } |
| 30 std::string new_host = suborigin + "." + url.host(); | 30 std::string new_host = suborigin + "." + url.host(); |
| 31 replacements.SetHostStr(new_host); | 31 replacements.SetHostStr(new_host); |
| 32 return url.ReplaceComponents(replacements); | 32 return url.ReplaceComponents(replacements); |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 Origin::Origin() : unique_(true), suborigin_(std::string()) {} | 37 Origin::Origin() : opaque_(true), suborigin_(std::string()) {} |
| 38 | 38 |
| 39 Origin::Origin(const GURL& url) : unique_(true), suborigin_(std::string()) { | 39 Origin::Origin(const GURL& url) : opaque_(true), suborigin_(std::string()) { |
| 40 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) | 40 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) |
| 41 return; | 41 return; |
| 42 | 42 |
| 43 if (url.SchemeIsFileSystem()) { | 43 if (url.SchemeIsFileSystem()) { |
| 44 tuple_ = SchemeHostPort(*url.inner_url()); | 44 tuple_ = SchemeHostPort(*url.inner_url()); |
| 45 } else if (url.SchemeIsBlob()) { | 45 } else if (url.SchemeIsBlob()) { |
| 46 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin | 46 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin |
| 47 // defines the origin as the origin of the URL which results from parsing | 47 // defines the origin as the origin of the URL which results from parsing |
| 48 // the "path", which boils down to everything after the scheme. GURL's | 48 // the "path", which boils down to everything after the scheme. GURL's |
| 49 // 'GetContent()' gives us exactly that. | 49 // 'GetContent()' gives us exactly that. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 70 | 70 |
| 71 bool invalid_suborigin = no_dot || suborigin_end == 0; | 71 bool invalid_suborigin = no_dot || suborigin_end == 0; |
| 72 if (invalid_suborigin || tuple_.IsInvalid()) | 72 if (invalid_suborigin || tuple_.IsInvalid()) |
| 73 return; | 73 return; |
| 74 | 74 |
| 75 suborigin_ = host.substr(0, suborigin_end); | 75 suborigin_ = host.substr(0, suborigin_end); |
| 76 } else { | 76 } else { |
| 77 tuple_ = SchemeHostPort(url); | 77 tuple_ = SchemeHostPort(url); |
| 78 } | 78 } |
| 79 | 79 |
| 80 unique_ = tuple_.IsInvalid(); | 80 opaque_ = tuple_.IsInvalid(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 Origin::Origin(base::StringPiece scheme, | 83 Origin::Origin(base::StringPiece scheme, |
| 84 base::StringPiece host, | 84 base::StringPiece host, |
| 85 uint16_t port, | 85 uint16_t port, |
| 86 base::StringPiece suborigin, | 86 base::StringPiece suborigin, |
| 87 SchemeHostPort::ConstructPolicy policy) | 87 SchemeHostPort::ConstructPolicy policy) |
| 88 : tuple_(scheme.as_string(), host.as_string(), port, policy) { | 88 : tuple_(scheme.as_string(), host.as_string(), port, policy) { |
| 89 unique_ = tuple_.IsInvalid(); | 89 opaque_ = tuple_.IsInvalid(); |
| 90 suborigin_ = suborigin.as_string(); | 90 suborigin_ = suborigin.as_string(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 Origin::Origin(std::string scheme, | 93 Origin::Origin(std::string scheme, |
| 94 std::string host, | 94 std::string host, |
| 95 uint16_t port, | 95 uint16_t port, |
| 96 std::string suborigin, | 96 std::string suborigin, |
| 97 SchemeHostPort::ConstructPolicy policy) | 97 SchemeHostPort::ConstructPolicy policy) |
| 98 : tuple_(std::move(scheme), std::move(host), port, policy) { | 98 : tuple_(std::move(scheme), std::move(host), port, policy) { |
| 99 unique_ = tuple_.IsInvalid(); | 99 opaque_ = tuple_.IsInvalid(); |
| 100 suborigin_ = std::move(suborigin); | 100 suborigin_ = std::move(suborigin); |
| 101 } | 101 } |
| 102 | 102 |
| 103 Origin::~Origin() { | 103 Origin::~Origin() { |
| 104 } | 104 } |
| 105 | 105 |
| 106 // static | 106 // static |
| 107 Origin Origin::UnsafelyCreateOriginWithoutNormalization( | 107 Origin Origin::UnsafelyCreateOriginWithoutNormalization( |
| 108 base::StringPiece scheme, | 108 base::StringPiece scheme, |
| 109 base::StringPiece host, | 109 base::StringPiece host, |
| 110 uint16_t port) { | 110 uint16_t port) { |
| 111 return Origin(scheme, host, port, "", SchemeHostPort::CHECK_CANONICALIZATION); | 111 return Origin(scheme, host, port, "", SchemeHostPort::CHECK_CANONICALIZATION); |
| 112 } | 112 } |
| 113 | 113 |
| 114 Origin Origin::CreateFromNormalizedTupleWithSuborigin( | 114 Origin Origin::CreateFromNormalizedTupleWithSuborigin( |
| 115 std::string scheme, | 115 std::string scheme, |
| 116 std::string host, | 116 std::string host, |
| 117 uint16_t port, | 117 uint16_t port, |
| 118 std::string suborigin) { | 118 std::string suborigin) { |
| 119 return Origin(std::move(scheme), std::move(host), port, std::move(suborigin), | 119 return Origin(std::move(scheme), std::move(host), port, std::move(suborigin), |
| 120 SchemeHostPort::ALREADY_CANONICALIZED); | 120 SchemeHostPort::ALREADY_CANONICALIZED); |
| 121 } | 121 } |
| 122 | 122 |
| 123 std::string Origin::Serialize() const { | 123 std::string Origin::Serialize() const { |
| 124 if (unique()) | 124 if (opaque()) |
| 125 return "null"; | 125 return "null"; |
| 126 | 126 |
| 127 if (scheme() == kFileScheme) | 127 if (scheme() == kFileScheme) |
| 128 return "file://"; | 128 return "file://"; |
| 129 | 129 |
| 130 if (!suborigin_.empty()) { | 130 if (!suborigin_.empty()) { |
| 131 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); | 131 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); |
| 132 return SchemeHostPort(url_with_suborigin).Serialize(); | 132 return SchemeHostPort(url_with_suborigin).Serialize(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 return tuple_.Serialize(); | 135 return tuple_.Serialize(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 Origin Origin::GetPhysicalOrigin() const { | 138 Origin Origin::GetPhysicalOrigin() const { |
| 139 if (suborigin_.empty()) | 139 if (suborigin_.empty()) |
| 140 return *this; | 140 return *this; |
| 141 | 141 |
| 142 return Origin(tuple_.GetURL()); | 142 return Origin(tuple_.GetURL()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 GURL Origin::GetURL() const { | 145 GURL Origin::GetURL() const { |
| 146 if (unique()) | 146 if (opaque()) |
| 147 return GURL(); | 147 return GURL(); |
| 148 | 148 |
| 149 if (scheme() == kFileScheme) | 149 if (scheme() == kFileScheme) |
| 150 return GURL("file:///"); | 150 return GURL("file:///"); |
| 151 | 151 |
| 152 GURL tuple_url(tuple_.GetURL()); | 152 GURL tuple_url(tuple_.GetURL()); |
| 153 | 153 |
| 154 if (!suborigin_.empty()) | 154 if (!suborigin_.empty()) |
| 155 return AddSuboriginToUrl(tuple_url, suborigin_); | 155 return AddSuboriginToUrl(tuple_url, suborigin_); |
| 156 | 156 |
| 157 return tuple_url; | 157 return tuple_url; |
| 158 } | 158 } |
| 159 | 159 |
| 160 bool Origin::IsSameOriginWith(const Origin& other) const { | 160 bool Origin::IsSameOriginWith(const Origin& other) const { |
| 161 if (unique_ || other.unique_) | 161 if (opaque_ || other.opaque_) |
| 162 return false; | 162 return false; |
| 163 | 163 |
| 164 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_; | 164 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_; |
| 165 } | 165 } |
| 166 | 166 |
| 167 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { | 167 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { |
| 168 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin()); | 168 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin()); |
| 169 } | 169 } |
| 170 | 170 |
| 171 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { | 171 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { |
| 172 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); | 172 return !opaque_ && url::DomainIs(tuple_.host(), lower_ascii_domain); |
| 173 } | 173 } |
| 174 | 174 |
| 175 bool Origin::operator<(const Origin& other) const { | 175 bool Origin::operator<(const Origin& other) const { |
| 176 return tuple_ < other.tuple_; | 176 return tuple_ < other.tuple_; |
| 177 } | 177 } |
| 178 | 178 |
| 179 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { | 179 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { |
| 180 return out << origin.Serialize(); | 180 return out << origin.Serialize(); |
| 181 } | 181 } |
| 182 | 182 |
| 183 bool IsSameOriginWith(const GURL& a, const GURL& b) { | 183 bool IsSameOriginWith(const GURL& a, const GURL& b) { |
| 184 return Origin(a).IsSameOriginWith(Origin(b)); | 184 return Origin(a).IsSameOriginWith(Origin(b)); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { | 187 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { |
| 188 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); | 188 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); |
| 189 } | 189 } |
| 190 | 190 |
| 191 } // namespace url | 191 } // namespace url |
| OLD | NEW |