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
3 // found in the LICENSE file.
5 #include "url/origin.h"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
12 #include "url/url_canon.h"
13 #include "url/url_canon_stdstring.h"
14 #include "url/url_constants.h"
15 #include "url/url_util.h"
19 Origin::Origin() : unique_(true) {
22 Origin::Origin(const GURL
& url
) : unique_(true) {
23 if (!url
.is_valid() || (!url
.IsStandard() && !url
.SchemeIsBlob()))
26 if (url
.SchemeIsFileSystem()) {
27 tuple_
= SchemeHostPort(*url
.inner_url());
28 } else if (url
.SchemeIsBlob()) {
29 // TODO(mkwst): This relies on the fact that GURL pushes the unparseable
30 // bits and pieces of a non-standard scheme into the GURL's path. It seems
31 // fairly fragile, so it might be worth teaching GURL about blobs' data in
32 // the same way it's been taught about filesystems' inner URLs.
33 tuple_
= SchemeHostPort(GURL(url
.path()));
35 tuple_
= SchemeHostPort(url
);
38 unique_
= tuple_
.IsInvalid();
44 std::string
Origin::Serialize() const {
48 if (scheme() == kFileScheme
)
51 return tuple_
.Serialize();
54 bool Origin::IsSameOriginWith(const Origin
& other
) const {
55 if (unique_
|| other
.unique_
)
58 return tuple_
.Equals(other
.tuple_
);
61 bool Origin::operator<(const Origin
& other
) const {
62 return tuple_
< other
.tuple_
;
65 std::ostream
& operator<<(std::ostream
& out
, const url::Origin
& origin
) {
66 return out
<< origin
.Serialize();