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" |
8 | 9 |
9 namespace url { | 10 namespace url { |
10 | 11 |
11 Origin::Origin() : string_("null") {} | 12 Origin::Origin() { |
13 Init(GURL()); | |
14 } | |
12 | 15 |
13 Origin::Origin(const std::string& origin) : string_(origin) { | 16 Origin::Origin(const GURL& url) { |
14 DCHECK(origin == "null" || MatchPattern(origin, "?*://?*")); | 17 Init(url); |
15 DCHECK_GT(origin.size(), 0u); | 18 } |
16 DCHECK(origin == "file://" || origin[origin.size() - 1] != '/'); | 19 |
20 Origin::Origin(const std::string& origin) { | |
21 Init(GURL(origin)); | |
22 } | |
23 | |
24 void Origin::Init(const GURL& url) { | |
25 // Start with a unique origin, parse from there: | |
26 scheme_ = ""; | |
27 host_ = ""; | |
Ryan Sleevi
2015/05/22 02:50:03
scheme_.clear()
host_.clear()
| |
28 port_ = 0; | |
29 unique_ = true; | |
30 | |
31 GURL origin(url.GetOrigin()); | |
32 if (!origin.is_valid() || !origin.IsStandard()) | |
33 return; | |
34 | |
35 // Ok. We have an origin. | |
Ryan Sleevi
2015/05/22 02:50:03
No pronouns in comments or the boogey-sleevi will
| |
36 unique_ = false; | |
37 if (origin.SchemeIsFile()) { | |
38 scheme_ = "file"; | |
39 return; | |
40 } | |
Ryan Sleevi
2015/05/22 02:50:03
API wise, I don't think this is right. You're enco
| |
41 | |
42 unique_ = false; | |
43 scheme_ = origin.scheme(); | |
44 host_ = origin.HostNoBrackets(); | |
45 port_ = origin.EffectiveIntPort(); | |
46 is_default_port_ = origin.EffectiveIntPort() != origin.IntPort(); | |
Ryan Sleevi
2015/05/22 02:50:03
BUG: I don't think this does what you think it doe
| |
47 } | |
48 | |
49 std::string Origin::serialize() const { | |
50 if (unique_) | |
51 return "null"; | |
52 | |
53 if (scheme_ == "file") | |
54 return "file://"; | |
55 | |
56 return scheme_ + "://" + host_ + | |
57 (is_default_port_ ? "" : ":" + base::IntToString(port_)); | |
58 } | |
Ryan Sleevi
2015/05/22 02:50:03
url code uses the URL constants - https://code.goo
| |
59 | |
60 bool Origin::SchemeIs(const std::string& scheme) const { | |
61 return scheme_ == scheme; | |
62 } | |
63 | |
64 bool Origin::operator==(const Origin& other) const { | |
65 return !unique_ && !other.unique_ && scheme_ == other.scheme_ && | |
66 host_ == other.host_ && port_ == other.port_; | |
67 } | |
68 | |
69 bool Origin::operator!=(const Origin& other) const { | |
70 return unique_ || other.unique_ || scheme_ != other.scheme_ || | |
71 host_ != other.host_ || port_ != other.port_; | |
72 } | |
73 | |
74 std::ostream& operator<<(std::ostream& out, const url::Origin& url) { | |
75 return out << url.serialize(); | |
17 } | 76 } |
18 | 77 |
19 } // namespace url | 78 } // namespace url |
OLD | NEW |