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 namespace { | |
21 | |
22 GURL AddSuboriginToUrl(const GURL& url, const std::string& suborigin) { | |
23 GURL::Replacements replacements; | |
24 replacements.SetSchemeStr((url.scheme() == kHttpScheme) | |
25 ? kHttpSuboriginScheme | |
26 : kHttpsSuboriginScheme); | |
27 std::string new_host = suborigin + "." + url.host(); | |
28 replacements.SetHostStr(new_host); | |
29 return url.ReplaceComponents(replacements); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
20 Origin::Origin() : unique_(true) { | 34 Origin::Origin() : unique_(true) { |
21 } | 35 } |
22 | 36 |
23 Origin::Origin(const GURL& url) : unique_(true) { | 37 Origin::Origin(const GURL& url) : unique_(true) { |
24 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) | 38 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) |
25 return; | 39 return; |
26 | 40 |
27 if (url.SchemeIsFileSystem()) { | 41 if (url.SchemeIsFileSystem()) { |
28 tuple_ = SchemeHostPort(*url.inner_url()); | 42 tuple_ = SchemeHostPort(*url.inner_url()); |
29 } else if (url.SchemeIsBlob()) { | 43 } else if (url.SchemeIsBlob()) { |
30 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin | 44 // 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 | 45 // 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 | 46 // the "path", which boils down to everything after the scheme. GURL's |
33 // 'GetContent()' gives us exactly that. | 47 // 'GetContent()' gives us exactly that. |
34 tuple_ = SchemeHostPort(GURL(url.GetContent())); | 48 tuple_ = SchemeHostPort(GURL(url.GetContent())); |
49 } else if (url.SchemeIsSuborigin()) { | |
50 GURL::Replacements replacements; | |
51 if (url.scheme() == kHttpSuboriginScheme) { | |
52 replacements.SetSchemeStr(kHttpScheme); | |
53 } else { | |
54 DCHECK(url.scheme() == kHttpsSuboriginScheme); | |
55 replacements.SetSchemeStr(kHttpsScheme); | |
56 } | |
nasko
2016/10/13 22:16:45
Why not keep the same code in AddSuboriginToUrl? T
jww
2016/10/14 01:08:58
Done.
| |
57 | |
58 std::string host = url.host(); | |
59 size_t suborigin_end = host.find("."); | |
60 bool no_dot = suborigin_end == std::string::npos; | |
61 std::string new_host( | |
62 no_dot ? "" | |
63 : host.substr(suborigin_end + 1, | |
64 url.host().length() - suborigin_end - 1)); | |
65 replacements.SetHostStr(new_host); | |
66 | |
67 tuple_ = SchemeHostPort(url.ReplaceComponents(replacements)); | |
68 | |
69 bool invalid_suborigin = no_dot || suborigin_end == 0; | |
70 if (invalid_suborigin || tuple_.IsInvalid()) { | |
71 suborigin_ = ""; | |
nasko
2016/10/13 22:16:45
Why not add this to the initializer list? That way
jww
2016/10/14 01:08:58
Done.
| |
72 unique_ = true; | |
73 return; | |
74 } | |
75 | |
76 suborigin_ = host.substr(0, suborigin_end); | |
35 } else { | 77 } else { |
36 tuple_ = SchemeHostPort(url); | 78 tuple_ = SchemeHostPort(url); |
37 } | 79 } |
38 | 80 |
39 unique_ = tuple_.IsInvalid(); | 81 unique_ = tuple_.IsInvalid(); |
40 } | 82 } |
41 | 83 |
42 Origin::Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port) | 84 Origin::Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port) |
43 : tuple_(scheme, host, port) { | 85 : tuple_(scheme, host, port) { |
44 unique_ = tuple_.IsInvalid(); | 86 unique_ = tuple_.IsInvalid(); |
(...skipping 10 matching lines...) Expand all Loading... | |
55 return Origin(scheme, host, port); | 97 return Origin(scheme, host, port); |
56 } | 98 } |
57 | 99 |
58 std::string Origin::Serialize() const { | 100 std::string Origin::Serialize() const { |
59 if (unique()) | 101 if (unique()) |
60 return "null"; | 102 return "null"; |
61 | 103 |
62 if (scheme() == kFileScheme) | 104 if (scheme() == kFileScheme) |
63 return "file://"; | 105 return "file://"; |
64 | 106 |
107 if (!suborigin_.empty()) { | |
108 GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); | |
109 return SchemeHostPort(url_with_suborigin).Serialize(); | |
110 } | |
111 | |
65 return tuple_.Serialize(); | 112 return tuple_.Serialize(); |
66 } | 113 } |
67 | 114 |
115 std::string Origin::SerializePhysicalOrigin() const { | |
116 if (unique()) | |
117 return "null"; | |
118 | |
119 if (scheme() == kFileScheme) | |
120 return "file://"; | |
121 | |
122 // Note that there is no special case for suborigins because the | |
123 // SchemeHostPort |tuple_| explicitly represents the components of the | |
124 // physical origin, so that alone is what is needed to serialize for the | |
125 // suborigin. | |
126 return tuple_.Serialize(); | |
nasko
2016/10/13 22:16:45
This part of the comment is a bit confusing "so th
jww
2016/10/14 01:08:58
Yes, that's what I meant. I tried to clarify the c
| |
127 } | |
128 | |
68 GURL Origin::GetURL() const { | 129 GURL Origin::GetURL() const { |
69 if (unique()) | 130 if (unique()) |
70 return GURL(); | 131 return GURL(); |
71 | 132 |
72 if (scheme() == kFileScheme) | 133 if (scheme() == kFileScheme) |
73 return GURL("file:///"); | 134 return GURL("file:///"); |
74 | 135 |
75 return tuple_.GetURL(); | 136 GURL tuple_url(tuple_.GetURL()); |
137 | |
138 if (!suborigin_.empty()) | |
139 return AddSuboriginToUrl(tuple_url, suborigin_); | |
140 | |
141 return tuple_url; | |
76 } | 142 } |
77 | 143 |
78 bool Origin::IsSameOriginWith(const Origin& other) const { | 144 bool Origin::IsSameOriginWith(const Origin& other) const { |
145 return IsSamePhysicalOriginWith(other) && suborigin_ == other.suborigin_; | |
146 } | |
147 | |
148 bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { | |
79 if (unique_ || other.unique_) | 149 if (unique_ || other.unique_) |
80 return false; | 150 return false; |
81 | 151 |
82 return tuple_.Equals(other.tuple_); | 152 return tuple_.Equals(other.tuple_); |
83 } | 153 } |
84 | 154 |
85 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { | 155 bool Origin::DomainIs(base::StringPiece lower_ascii_domain) const { |
86 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); | 156 return !unique_ && url::DomainIs(tuple_.host(), lower_ascii_domain); |
87 } | 157 } |
88 | 158 |
89 bool Origin::operator<(const Origin& other) const { | 159 bool Origin::operator<(const Origin& other) const { |
90 return tuple_ < other.tuple_; | 160 return tuple_ < other.tuple_; |
91 } | 161 } |
92 | 162 |
93 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { | 163 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { |
94 return out << origin.Serialize(); | 164 return out << origin.Serialize(); |
95 } | 165 } |
96 | 166 |
97 bool IsSameOriginWith(const GURL& a, const GURL& b) { | 167 bool IsSameOriginWith(const GURL& a, const GURL& b) { |
98 return Origin(a).IsSameOriginWith(Origin(b)); | 168 return Origin(a).IsSameOriginWith(Origin(b)); |
99 } | 169 } |
100 | 170 |
171 bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { | |
172 return Origin(a).IsSamePhysicalOriginWith(Origin(b)); | |
173 } | |
174 | |
101 } // namespace url | 175 } // namespace url |
OLD | NEW |