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/gurl.h" |
| 10 #include "url/url_canon.h" |
| 11 #include "url/url_constants.h" |
| 12 #include "url/url_util.h" |
8 | 13 |
9 namespace url { | 14 namespace url { |
10 | 15 |
11 Origin::Origin() : string_("null") {} | 16 Origin::Origin() { |
| 17 Init(GURL()); |
| 18 } |
12 | 19 |
13 Origin::Origin(const std::string& origin) : string_(origin) { | 20 Origin::Origin(const GURL& url) { |
14 DCHECK(origin == "null" || MatchPattern(origin, "?*://?*")); | 21 Init(url); |
15 DCHECK_GT(origin.size(), 0u); | 22 } |
16 DCHECK(origin == "file://" || origin[origin.size() - 1] != '/'); | 23 |
| 24 Origin::Origin(const std::string& scheme, |
| 25 const std::string& host, |
| 26 uint16 port) { |
| 27 // Special-case unique origins (because GURL normalizes '://:0' into a |
| 28 // valid URL, which is unexpected: https://crbug.com/493123). Otherwise, pass |
| 29 // the data through GURL for normalization: |
| 30 if (!scheme.size() && !host.size() && !port) { |
| 31 Init(GURL()); |
| 32 } else { |
| 33 Init(GURL(scheme + kStandardSchemeSeparator + host + |
| 34 (port ? ":" + base::IntToString(port) : ""))); |
| 35 } |
| 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 DCHECK(!url.SchemeIsFileSystem() || url.inner_url()); |
| 45 |
| 46 // Start with a unique origin, parse from there: |
| 47 scheme_.clear(); |
| 48 host_.clear(); |
| 49 port_ = 0; |
| 50 unique_ = true; |
| 51 serialization_requires_port_ = false; |
| 52 valid_ = false; |
| 53 |
| 54 url::Replacements<char> replacements; |
| 55 replacements.ClearUsername(); |
| 56 replacements.ClearPassword(); |
| 57 replacements.ClearPath(); |
| 58 replacements.ClearQuery(); |
| 59 replacements.ClearRef(); |
| 60 |
| 61 GURL origin = url.SchemeIsFileSystem() |
| 62 ? url.inner_url()->ReplaceComponents(replacements) |
| 63 : url.ReplaceComponents(replacements); |
| 64 |
| 65 if (!origin.is_valid()) |
| 66 return; |
| 67 |
| 68 valid_ = true; |
| 69 |
| 70 if (!origin.IsStandard()) |
| 71 return; |
| 72 |
| 73 unique_ = false; |
| 74 scheme_ = origin.scheme(); |
| 75 |
| 76 if (origin.SchemeIs(kFileScheme)) |
| 77 return; |
| 78 |
| 79 host_ = origin.host(); |
| 80 port_ = origin.EffectiveIntPort(); |
| 81 serialization_requires_port_ = origin.IntPort() != PORT_UNSPECIFIED; |
| 82 } |
| 83 |
| 84 std::string Origin::serialize() const { |
| 85 if (unique_) |
| 86 return kUniqueOriginSerialization; |
| 87 |
| 88 if (host_.empty()) |
| 89 return scheme_ + kStandardSchemeSeparator; |
| 90 |
| 91 return scheme_ + kStandardSchemeSeparator + host_ + |
| 92 (serialization_requires_port_ ? ":" + base::IntToString(port_) : ""); |
| 93 } |
| 94 |
| 95 bool Origin::IsSameOriginWith(const Origin& other) const { |
| 96 return !unique_ && !other.unique_ && scheme_ == other.scheme_ && |
| 97 host_ == other.host_ && port_ == other.port_; |
| 98 } |
| 99 |
| 100 std::ostream& operator<<(std::ostream& out, const url::Origin& url) { |
| 101 return out << url.serialize(); |
17 } | 102 } |
18 | 103 |
19 } // namespace url | 104 } // namespace url |
OLD | NEW |