| 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 Origin::Origin() : unique_(true) { | 20 Origin::Origin() : unique_(true), empty_(false) {} |
| 21 } | |
| 22 | 21 |
| 23 Origin::Origin(const GURL& url) : unique_(true) { | 22 Origin::Origin(const GURL& url) |
| 24 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) | 23 : unique_(!url.is_empty()), empty_(url.is_empty()) { |
| 24 if (!url.is_valid() || (!url.IsStandard() && !url.SchemeIsBlob())) { |
| 25 return; | 25 return; |
| 26 } |
| 26 | 27 |
| 27 if (url.SchemeIsFileSystem()) { | 28 if (url.SchemeIsFileSystem()) { |
| 28 tuple_ = SchemeHostPort(*url.inner_url()); | 29 tuple_ = SchemeHostPort(*url.inner_url()); |
| 29 } else if (url.SchemeIsBlob()) { | 30 } else if (url.SchemeIsBlob()) { |
| 30 // If we're dealing with a 'blob:' URL, https://url.spec.whatwg.org/#origin | 31 // 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 | 32 // 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 | 33 // the "path", which boils down to everything after the scheme. GURL's |
| 33 // 'GetContent()' gives us exactly that. | 34 // 'GetContent()' gives us exactly that. |
| 34 tuple_ = SchemeHostPort(GURL(url.GetContent())); | 35 tuple_ = SchemeHostPort(GURL(url.GetContent())); |
| 35 } else { | 36 } else { |
| 36 tuple_ = SchemeHostPort(url); | 37 tuple_ = SchemeHostPort(url); |
| 37 } | 38 } |
| 38 | 39 |
| 39 unique_ = tuple_.IsInvalid(); | 40 unique_ = tuple_.IsInvalid(); |
| 40 } | 41 } |
| 41 | 42 |
| 43 Origin::Origin(const std::string& url_string) : Origin(GURL(url_string)) {} |
| 44 |
| 42 Origin::Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port) | 45 Origin::Origin(base::StringPiece scheme, base::StringPiece host, uint16_t port) |
| 43 : tuple_(scheme, host, port) { | 46 : tuple_(scheme, host, port) { |
| 44 unique_ = tuple_.IsInvalid(); | 47 unique_ = tuple_.IsInvalid(); |
| 48 empty_ = scheme.empty() && host.empty() && port == 0; |
| 45 } | 49 } |
| 46 | 50 |
| 47 Origin::~Origin() { | 51 Origin::~Origin() { |
| 48 } | 52 } |
| 49 | 53 |
| 50 // static | 54 // static |
| 51 Origin Origin::UnsafelyCreateOriginWithoutNormalization( | 55 Origin Origin::UnsafelyCreateOriginWithoutNormalization( |
| 52 base::StringPiece scheme, | 56 base::StringPiece scheme, |
| 53 base::StringPiece host, | 57 base::StringPiece host, |
| 54 uint16_t port) { | 58 uint16_t port) { |
| 55 return Origin(scheme, host, port); | 59 return Origin(scheme, host, port); |
| 56 } | 60 } |
| 57 | 61 |
| 58 std::string Origin::Serialize() const { | 62 std::string Origin::Serialize() const { |
| 63 if (empty()) |
| 64 return ""; |
| 65 |
| 59 if (unique()) | 66 if (unique()) |
| 60 return "null"; | 67 return "null"; |
| 61 | 68 |
| 62 if (scheme() == kFileScheme) | 69 if (scheme() == kFileScheme) |
| 63 return "file://"; | 70 return "file://"; |
| 64 | 71 |
| 65 return tuple_.Serialize(); | 72 return tuple_.Serialize(); |
| 66 } | 73 } |
| 67 | 74 |
| 68 bool Origin::IsSameOriginWith(const Origin& other) const { | 75 bool Origin::IsSameOriginWith(const Origin& other) const { |
| 76 if (empty_ && other.empty_) |
| 77 return true; |
| 78 |
| 69 if (unique_ || other.unique_) | 79 if (unique_ || other.unique_) |
| 70 return false; | 80 return false; |
| 71 | 81 |
| 72 return tuple_.Equals(other.tuple_); | 82 return tuple_.Equals(other.tuple_); |
| 73 } | 83 } |
| 74 | 84 |
| 75 bool Origin::operator<(const Origin& other) const { | 85 bool Origin::operator<(const Origin& other) const { |
| 76 return tuple_ < other.tuple_; | 86 return tuple_ < other.tuple_; |
| 77 } | 87 } |
| 78 | 88 |
| 79 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { | 89 std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { |
| 80 return out << origin.Serialize(); | 90 return out << origin.Serialize(); |
| 81 } | 91 } |
| 82 | 92 |
| 83 bool IsSameOriginWith(const GURL& a, const GURL& b) { | 93 bool IsSameOriginWith(const GURL& a, const GURL& b) { |
| 84 return Origin(a).IsSameOriginWith(Origin(b)); | 94 return Origin(a).IsSameOriginWith(Origin(b)); |
| 85 } | 95 } |
| 86 | 96 |
| 87 } // namespace url | 97 } // namespace url |
| OLD | NEW |