Chromium Code Reviews| Index: url/origin.cc |
| diff --git a/url/origin.cc b/url/origin.cc |
| index f31d38ababa08e57e89ec9a219f7a2676e9e80a3..5a2dbd8fdb1fbd0a301edf91e7e9bcb18751f9ad 100644 |
| --- a/url/origin.cc |
| +++ b/url/origin.cc |
| @@ -17,6 +17,20 @@ |
| namespace url { |
| +namespace { |
| + |
| +GURL AddSuboriginToUrl(const GURL& url, const std::string& suborigin) { |
| + GURL::Replacements replacements; |
| + replacements.SetSchemeStr((url.scheme() == kHttpScheme) |
| + ? kHttpSuboriginScheme |
| + : kHttpsSuboriginScheme); |
| + std::string new_host = suborigin + "." + url.host(); |
| + replacements.SetHostStr(new_host); |
| + return url.ReplaceComponents(replacements); |
| +} |
| + |
| +} // namespace |
| + |
| Origin::Origin() : unique_(true) { |
| } |
| @@ -32,6 +46,34 @@ Origin::Origin(const GURL& url) : unique_(true) { |
| // the "path", which boils down to everything after the scheme. GURL's |
| // 'GetContent()' gives us exactly that. |
| tuple_ = SchemeHostPort(GURL(url.GetContent())); |
| + } else if (url.SchemeIsSuborigin()) { |
| + GURL::Replacements replacements; |
| + if (url.scheme() == kHttpSuboriginScheme) { |
| + replacements.SetSchemeStr(kHttpScheme); |
| + } else { |
| + DCHECK(url.scheme() == kHttpsSuboriginScheme); |
| + replacements.SetSchemeStr(kHttpsScheme); |
| + } |
|
nasko
2016/10/13 22:16:45
Why not keep the same code in AddSuboriginToUrl? T
jww
2016/10/14 01:08:58
Done.
|
| + |
| + std::string host = url.host(); |
| + size_t suborigin_end = host.find("."); |
| + bool no_dot = suborigin_end == std::string::npos; |
| + std::string new_host( |
| + no_dot ? "" |
| + : host.substr(suborigin_end + 1, |
| + url.host().length() - suborigin_end - 1)); |
| + replacements.SetHostStr(new_host); |
| + |
| + tuple_ = SchemeHostPort(url.ReplaceComponents(replacements)); |
| + |
| + bool invalid_suborigin = no_dot || suborigin_end == 0; |
| + if (invalid_suborigin || tuple_.IsInvalid()) { |
| + 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.
|
| + unique_ = true; |
| + return; |
| + } |
| + |
| + suborigin_ = host.substr(0, suborigin_end); |
| } else { |
| tuple_ = SchemeHostPort(url); |
| } |
| @@ -62,6 +104,25 @@ std::string Origin::Serialize() const { |
| if (scheme() == kFileScheme) |
| return "file://"; |
| + if (!suborigin_.empty()) { |
| + GURL url_with_suborigin = AddSuboriginToUrl(tuple_.GetURL(), suborigin_); |
| + return SchemeHostPort(url_with_suborigin).Serialize(); |
| + } |
| + |
| + return tuple_.Serialize(); |
| +} |
| + |
| +std::string Origin::SerializePhysicalOrigin() const { |
| + if (unique()) |
| + return "null"; |
| + |
| + if (scheme() == kFileScheme) |
| + return "file://"; |
| + |
| + // Note that there is no special case for suborigins because the |
| + // SchemeHostPort |tuple_| explicitly represents the components of the |
| + // physical origin, so that alone is what is needed to serialize for the |
| + // suborigin. |
| 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
|
| } |
| @@ -72,10 +133,19 @@ GURL Origin::GetURL() const { |
| if (scheme() == kFileScheme) |
| return GURL("file:///"); |
| - return tuple_.GetURL(); |
| + GURL tuple_url(tuple_.GetURL()); |
| + |
| + if (!suborigin_.empty()) |
| + return AddSuboriginToUrl(tuple_url, suborigin_); |
| + |
| + return tuple_url; |
| } |
| bool Origin::IsSameOriginWith(const Origin& other) const { |
| + return IsSamePhysicalOriginWith(other) && suborigin_ == other.suborigin_; |
| +} |
| + |
| +bool Origin::IsSamePhysicalOriginWith(const Origin& other) const { |
| if (unique_ || other.unique_) |
| return false; |
| @@ -98,4 +168,8 @@ bool IsSameOriginWith(const GURL& a, const GURL& b) { |
| return Origin(a).IsSameOriginWith(Origin(b)); |
| } |
| +bool IsSamePhysicalOriginWith(const GURL& a, const GURL& b) { |
| + return Origin(a).IsSamePhysicalOriginWith(Origin(b)); |
| +} |
| + |
| } // namespace url |