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" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
13 #include "url/url_canon.h" | 13 #include "url/url_canon.h" |
14 #include "url/url_canon_stdstring.h" | 14 #include "url/url_canon_stdstring.h" |
15 #include "url/url_constants.h" | 15 #include "url/url_constants.h" |
16 #include "url/url_util.h" | 16 #include "url/url_util.h" |
17 | 17 |
18 namespace url { | 18 namespace url { |
19 | 19 |
20 Origin::Origin() : unique_(true) { | 20 namespace { |
21 | |
22 GURL AddSuboriginToUrl(const GURL& url, const std::string& suborigin) { | |
23 GURL::Replacements replacements; | |
24 if (url.scheme() == kHttpScheme) { | |
25 replacements.SetSchemeStr(kHttpSuboriginScheme); | |
26 } else { | |
27 DCHECK(url.scheme() == kHttpsScheme); | |
28 replacements.SetSchemeStr(kHttpsSuboriginScheme); | |
29 } | |
30 std::string new_host = suborigin + "." + url.host(); | |
31 replacements.SetHostStr(new_host); | |
32 return url.ReplaceComponents(replacements); | |
21 } | 33 } |
22 | 34 |
23 Origin::Origin(const GURL& url) : unique_(true) { | 35 } // namespace |
36 | |
37 Origin::Origin() : unique_(true), suborigin_(std::string()) {} | |
38 | |
39 Origin::Origin(const GURL& url) : unique_(true), suborigin_(std::string()) { | |
24 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) | 40 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) |
25 return; | 41 return; |
26 | 42 |
27 if (url.SchemeIsFileSystem()) { | 43 if (url.SchemeIsFileSystem()) { |
28 tuple_ = SchemeHostPort(*url.inner_url()); | 44 tuple_ = SchemeHostPort(*url.inner_url()); |
29 } else if (url.SchemeIsBlob()) { | 45 } else if (url.SchemeIsBlob()) { |
30 // 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 |
31 // 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 |
32 // 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 |
33 // 'GetContent()' gives us exactly that. | 49 // 'GetContent()' gives us exactly that. |
34 tuple_ = SchemeHostPort(GURL(url.GetContent())); | 50 tuple_ = SchemeHostPort(GURL(url.GetContent())); |
51 } else if (url.SchemeIsSuborigin()) { | |
52 GURL::Replacements replacements; | |
53 if (url.scheme() == kHttpSuboriginScheme) { | |
54 replacements.SetSchemeStr(kHttpScheme); | |
55 } else { | |
56 DCHECK(url.scheme() == kHttpsSuboriginScheme); | |
57 replacements.SetSchemeStr(kHttpsScheme); | |
58 } | |
59 | |
60 std::string host = url.host(); | |
61 size_t suborigin_end = host.find("."); | |
62 bool no_dot = suborigin_end == std::string::npos; | |
63 std::string new_host( | |
64 no_dot ? "" | |
65 : host.substr(suborigin_end + 1, | |
66 url.host().length() - suborigin_end - 1)); | |
67 replacements.SetHostStr(new_host); | |
68 | |
69 tuple_ = SchemeHostPort(url.ReplaceComponents(replacements)); | |
70 | |
71 bool invalid_suborigin = no_dot || suborigin_end == 0; | |
72 if (invalid_suborigin || tuple_.IsInvalid()) | |
73 return; | |
74 | |
75 suborigin_ = host.substr(0, suborigin_end); | |
35 } else { | 76 } else { |
36 tuple_ = SchemeHostPort(url); | 77 tuple_ = SchemeHostPort(url); |
37 } | 78 } |
38 | 79 |
39 unique_ = tuple_.IsInvalid(); | 80 unique_ = tuple_.IsInvalid(); |
40 } | 81 } |
41 | 82 |
42 Origin::Origin(base::StringPiece scheme, | 83 Origin::Origin(base::StringPiece scheme, |
43 base::StringPiece host, | 84 base::StringPiece host, |
44 uint16_t port, | 85 uint16_t port, |
(...skipping 19 matching lines...) Expand all Loading... | |
64 return Origin(scheme, host, port, SchemeHostPort::ALREADY_CANONICALIZED); | 105 return Origin(scheme, host, port, SchemeHostPort::ALREADY_CANONICALIZED); |
65 } | 106 } |
66 | 107 |
67 std::string Origin::Serialize() const { | 108 std::string Origin::Serialize() const { |
68 if (unique()) | 109 if (unique()) |
69 return "null"; | 110 return "null"; |
70 | 111 |
71 if (scheme() == kFileScheme) | 112 if (scheme() == kFileScheme) |
72 return "file://"; | 113 return "file://"; |
73 | 114 |
115 if (!suborigin_.empty()) { | |
116 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); | |
117 return SchemeHostPort(url_with_suborigin).Serialize(); | |
118 } | |
119 | |
74 return tuple_.Serialize(); | 120 return tuple_.Serialize(); |
75 } | 121 } |
76 | 122 |
123 std::string Origin::SerializePhysicalOrigin() const { | |
124 if (unique()) | |
125 return "null"; | |
126 | |
127 if (scheme() == kFileScheme) | |
128 return "file://"; | |
129 | |
130 // Note that there is no special case for suborigins because the | |
131 // SchemeHostPort |tuple_| explicitly represents the components of the | |
132 // physical origin. Thus the tuple is all all that is needed to serialize the | |
nasko
2016/10/14 14:12:41
nit: "all all".
| |
133 // physical origin | |
134 return tuple_.Serialize(); | |
135 } | |
136 | |
77 GURL Origin::GetURL() const { | 137 GURL Origin::GetURL() const { |
78 if (unique()) | 138 if (unique()) |
79 return GURL(); | 139 return GURL(); |
80 | 140 |
81 if (scheme() == kFileScheme) | 141 if (scheme() == kFileScheme) |
82 return GURL("file:///"); | 142 return GURL("file:///"); |
83 | 143 |
84 return tuple_.GetURL(); | 144 GURL tuple_url(tuple_.GetURL()); |
145 | |
146 if (!suborigin_.empty()) | |
147 return AddSuboriginToUrl(tuple_url, suborigin_); | |
148 | |
149 return tuple_url; | |
85 } | 150 } |
86 | 151 |
87 bool Origin::IsSameOriginWith(const Origin& other) const { | 152 bool Origin::IsSameOriginWith(const Origin& other) const { |
153 return IsSamePhysicalOriginWith(other) && suborigin_ == other.suborigin_; | |
154 } | |
155 | |
156 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { | |
88 if (unique_ || other.unique_) | 157 if (unique_ || other.unique_) |
89 return false; | 158 return false; |
90 | 159 |
91 return tuple_.Equals(other.tuple_); | 160 return tuple_.Equals(other.tuple_); |
92 } | 161 } |
93 | 162 |
94 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { | 163 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { |
95 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); | 164 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); |
96 } | 165 } |
97 | 166 |
98 bool Origin::operator<(const Origin& other) const { | 167 bool Origin::operator<(const Origin& other) const { |
99 return tuple_ < other.tuple_; | 168 return tuple_ < other.tuple_; |
100 } | 169 } |
101 | 170 |
102 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { | 171 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { |
103 return out << origin.Serialize(); | 172 return out << origin.Serialize(); |
104 } | 173 } |
105 | 174 |
106 bool IsSameOriginWith(const GURL& a, const GURL& b) { | 175 bool IsSameOriginWith(const GURL& a, const GURL& b) { |
107 return Origin(a).IsSameOriginWith(Origin(b)); | 176 return Origin(a).IsSameOriginWith(Origin(b)); |
108 } | 177 } |
109 | 178 |
179 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { | |
180 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); | |
181 } | |
182 | |
110 } // namespace url | 183 } // namespace url |
OLD | NEW |