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 base::StringPiece suborigin) { | 111 base::StringPiece suborigin) { |
112 return Origin(scheme, host, port, suborigin, | 112 return Origin(scheme, host, port, suborigin, |
113 SchemeHostPort::CHECK_CANONICALIZATION); | 113 SchemeHostPort::CHECK_CANONICALIZATION); |
114 } | 114 } |
115 | 115 |
116 Origin Origin::CreateFromNormalizedTupleWithSuborigin( | 116 Origin Origin::CreateFromNormalizedTupleWithSuborigin( |
117 std::string scheme, | 117 std::string scheme, |
118 std::string host, | 118 std::string host, |
119 uint16_t port, | 119 uint16_t port, |
120 std::string suborigin) { | 120 std::string suborigin) { |
121 return Origin(std::move(scheme), std::move(host), port, std::move(suborigin), | 121 return Origin(std::move(scheme), std::move(host), port, std::move(suborigin), |
122 SchemeHostPort::ALREADY_CANONICALIZED); | 122 SchemeHostPort::ALREADY_CANONICALIZED); |
123 } | 123 } |
124 | 124 |
125 std::string Origin::Serialize() const { | 125 std::string Origin::Serialize() const { |
126 if (unique()) | 126 if (opaque()) |
127 return "null"; | 127 return "null"; |
128 | 128 |
129 if (scheme() == kFileScheme) | 129 if (scheme() == kFileScheme) |
130 return "file://"; | 130 return "file://"; |
131 | 131 |
132 if (!suborigin_.empty()) { | 132 if (!suborigin_.empty()) { |
133 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); | 133 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); |
134 return SchemeHostPort(url_with_suborigin).Serialize(); | 134 return SchemeHostPort(url_with_suborigin).Serialize(); |
135 } | 135 } |
136 | 136 |
137 return tuple_.Serialize(); | 137 return tuple_.Serialize(); |
138 } | 138 } |
139 | 139 |
140 Origin Origin::GetPhysicalOrigin() const { | 140 Origin Origin::GetPhysicalOrigin() const { |
141 if (suborigin_.empty()) | 141 if (suborigin_.empty()) |
142 return *this; | 142 return *this; |
143 | 143 |
144 return Origin(tuple_.GetURL()); | 144 return Origin(tuple_.GetURL()); |
145 } | 145 } |
146 | 146 |
147 GURL Origin::GetURL() const { | 147 GURL Origin::GetURL() const { |
148 if (unique()) | 148 if (opaque()) |
149 return GURL(); | 149 return GURL(); |
150 | 150 |
151 if (scheme() == kFileScheme) | 151 if (scheme() == kFileScheme) |
152 return GURL("file:///"); | 152 return GURL("file:///"); |
153 | 153 |
154 GURL tuple_url(tuple_.GetURL()); | 154 GURL tuple_url(tuple_.GetURL()); |
155 | 155 |
156 if (!suborigin_.empty()) | 156 if (!suborigin_.empty()) |
157 return AddSuboriginToUrl(tuple_url, suborigin_); | 157 return AddSuboriginToUrl(tuple_url, suborigin_); |
158 | 158 |
159 return tuple_url; | 159 return tuple_url; |
160 } | 160 } |
161 | 161 |
162 bool Origin::IsSameOriginWith(const Origin& other) const { | 162 bool Origin::IsSameOriginWith(const Origin& other) const { |
163 if (unique_ || other.unique_) | 163 if (opaque_ || other.opaque_) |
164 return false; | 164 return false; |
165 | 165 |
166 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_; | 166 return tuple_.Equals(other.tuple_) && suborigin_ == other.suborigin_; |
167 } | 167 } |
168 | 168 |
169 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { | 169 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { |
170 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin()); | 170 return GetPhysicalOrigin().IsSameOriginWith(other.GetPhysicalOrigin()); |
171 } | 171 } |
172 | 172 |
173 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { | 173 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { |
174 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); | 174 return !opaque_ && url::DomainIs(tuple_.host(), lower_ascii_domain); |
175 } | 175 } |
176 | 176 |
177 bool Origin::operator<(const Origin& other) const { | 177 bool Origin::operator<(const Origin& other) const { |
178 return tuple_ < other.tuple_ || | 178 return tuple_ < other.tuple_ || |
179 (tuple_.Equals(other.tuple_) && suborigin_ < other.suborigin_); | 179 (tuple_.Equals(other.tuple_) && suborigin_ < other.suborigin_); |
180 } | 180 } |
181 | 181 |
182 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { | 182 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { |
183 return out << origin.Serialize(); | 183 return out << origin.Serialize(); |
184 } | 184 } |
185 | 185 |
186 bool IsSameOriginWith(const GURL& a, const GURL& b) { | 186 bool IsSameOriginWith(const GURL& a, const GURL& b) { |
187 return Origin(a).IsSameOriginWith(Origin(b)); | 187 return Origin(a).IsSameOriginWith(Origin(b)); |
188 } | 188 } |
189 | 189 |
190 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { | 190 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { |
191 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); | 191 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); |
192 } | 192 } |
193 | 193 |
194 } // namespace url | 194 } // namespace url |
OLD | NEW |