Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/strings/string_number_conversions.h" | |
| 7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "url/url_constants.h" | |
| 8 | 10 |
| 9 namespace url { | 11 namespace url { |
| 10 | 12 |
| 11 Origin::Origin() : string_("null") {} | 13 Origin::Origin() { |
| 14 Init(GURL()); | |
| 15 } | |
| 12 | 16 |
| 13 Origin::Origin(const std::string& origin) : string_(origin) { | 17 Origin::Origin(const GURL& url) { |
| 14 DCHECK(origin == "null" || MatchPattern(origin, "?*://?*")); | 18 Init(url); |
| 15 DCHECK_GT(origin.size(), 0u); | 19 } |
| 16 DCHECK(origin == "file://" || origin[origin.size() - 1] != '/'); | 20 |
| 21 Origin::Origin(const std::string& scheme, | |
| 22 const std::string& host, | |
| 23 unsigned short port) { | |
| 24 // Special-case 'file://': | |
|
Ryan Sleevi
2015/05/22 20:43:35
I don't think this should be necessary - that is,
Mike West
2015/05/28 07:24:29
Dropped the special-case.
| |
| 25 if (scheme == kFileScheme) { | |
|
Ryan Sleevi
2015/05/22 20:43:35
BUG: The scheme comparisons should operate case-in
Mike West
2015/05/28 07:24:29
Switched to using `url::LowerCaseEqualsASCII` to m
| |
| 26 scheme_ = kFileScheme; | |
| 27 host_.clear(); | |
| 28 port_ = 0; | |
| 29 unique_ = false; | |
| 30 return; | |
| 31 } | |
| 32 | |
| 33 // Otherwise, pass the data through GURL for normalization: | |
| 34 Init(GURL(scheme + kStandardSchemeSeparator + host + ":" + | |
| 35 base::IntToString(port))); | |
| 36 } | |
| 37 | |
| 38 Origin::Origin(const std::string& origin) { | |
| 39 // Pass the string through GURL for normalization: | |
| 40 Init(GURL(origin)); | |
| 41 } | |
| 42 | |
| 43 void Origin::Init(const GURL& url) { | |
| 44 // Start with a unique origin, parse from there: | |
| 45 scheme_.clear(); | |
| 46 host_.clear(); | |
| 47 port_ = 0; | |
| 48 unique_ = true; | |
| 49 serialization_requires_port_ = false; | |
| 50 | |
| 51 // TODO(mkwst): Bring the GURL::GetOrigin replacement logic in here. | |
| 52 GURL origin(url.GetOrigin()); | |
| 53 if (!origin.is_valid()) | |
| 54 return; | |
| 55 | |
| 56 unique_ = false; | |
| 57 scheme_ = origin.scheme(); | |
| 58 | |
| 59 if (!origin.IsStandard() || SchemeIs(kFileScheme)) | |
| 60 return; | |
| 61 | |
| 62 host_ = origin.host(); | |
| 63 port_ = origin.EffectiveIntPort(); | |
| 64 serialization_requires_port_ = origin.IntPort() != PORT_UNSPECIFIED; | |
| 65 } | |
| 66 | |
| 67 std::string Origin::serialize() const { | |
| 68 if (unique_) | |
| 69 return kUniqueOriginSerialization; | |
| 70 | |
| 71 if (host_.empty()) | |
| 72 return scheme_ + kStandardSchemeSeparator; | |
| 73 | |
| 74 return scheme_ + kStandardSchemeSeparator + host_ + | |
| 75 (serialization_requires_port_ ? ":" + base::IntToString(port_) : ""); | |
| 76 } | |
| 77 | |
| 78 bool Origin::SchemeIs(const std::string& scheme) const { | |
| 79 return scheme_ == scheme; | |
|
Ryan Sleevi
2015/05/22 20:43:35
BUG: Case-sensitivity
| |
| 80 } | |
| 81 | |
| 82 bool Origin::SchemeIsCryptographic() const { | |
| 83 return SchemeIs(kHttpsScheme) || SchemeIs(kWssScheme); | |
|
Ryan Sleevi
2015/05/22 20:43:35
We should probably be breaking this out from both
Mike West
2015/05/28 07:24:29
Internal to what?
Note that content/public/common
Ryan Sleevi
2015/05/28 07:32:53
Oh, to GURL. Same with the scheme comparisons - a
| |
| 84 } | |
| 85 | |
| 86 bool Origin::IsSameOriginWith(const Origin& other) const { | |
| 87 return !unique_ && !other.unique_ && scheme_ == other.scheme_ && | |
| 88 host_ == other.host_ && port_ == other.port_; | |
| 89 } | |
| 90 | |
| 91 std::ostream& operator<<(std::ostream& out, const url::Origin& url) { | |
| 92 return out << url.serialize(); | |
| 17 } | 93 } |
| 18 | 94 |
| 19 } // namespace url | 95 } // namespace url |
| OLD | NEW |