Commit 07e9848d authored by Shuran Huang's avatar Shuran Huang Committed by Commit Bot

Add a SiteForCookies constructor that takes a SchemefulSite.

There will be a follow up CL to replace the current use cases of
FromOrigin().

Bug: 1145294
Change-Id: I606d75901f8c4012df285d14627533bbf5dfc20e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2557224
Commit-Queue: Shuran Huang <shuuran@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarLily Chen <chlily@chromium.org>
Cr-Commit-Position: refs/heads/master@{#830733}
parent 3cedaa1b
...@@ -28,6 +28,8 @@ struct StructTraits; ...@@ -28,6 +28,8 @@ struct StructTraits;
namespace net { namespace net {
class SiteForCookies;
// Class which represents a scheme and etld+1 for an origin, as specified by // Class which represents a scheme and etld+1 for an origin, as specified by
// https://html.spec.whatwg.org/multipage/origin.html#obtain-a-site. // https://html.spec.whatwg.org/multipage/origin.html#obtain-a-site.
// //
...@@ -91,6 +93,9 @@ class NET_EXPORT SchemefulSite { ...@@ -91,6 +93,9 @@ class NET_EXPORT SchemefulSite {
friend struct mojo::StructTraits<network::mojom::SchemefulSiteDataView, friend struct mojo::StructTraits<network::mojom::SchemefulSiteDataView,
SchemefulSite>; SchemefulSite>;
// Create SiteForCookies from SchemefulSite needs to access internal origin.
friend class SiteForCookies;
// Needed to serialize opaque and non-transient NetworkIsolationKeys, which // Needed to serialize opaque and non-transient NetworkIsolationKeys, which
// use opaque origins. // use opaque origins.
friend class NetworkIsolationKey; friend class NetworkIsolationKey;
......
...@@ -139,6 +139,11 @@ bool SiteForCookies::IsNull() const { ...@@ -139,6 +139,11 @@ bool SiteForCookies::IsNull() const {
return scheme_.empty(); return scheme_.empty();
} }
SiteForCookies::SiteForCookies(const net::SchemefulSite& schemeful_site)
: scheme_(schemeful_site.site_as_origin_.scheme()),
registrable_domain_(schemeful_site.site_as_origin_.host()),
schemefully_same_(!scheme_.empty()) {}
SiteForCookies::SiteForCookies(const std::string& scheme, SiteForCookies::SiteForCookies(const std::string& scheme,
const std::string& host) const std::string& host)
: scheme_(scheme), : scheme_(scheme),
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
#include "net/base/schemeful_site.h"
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
...@@ -38,6 +39,8 @@ class NET_EXPORT SiteForCookies { ...@@ -38,6 +39,8 @@ class NET_EXPORT SiteForCookies {
SiteForCookies(const SiteForCookies& other); SiteForCookies(const SiteForCookies& other);
SiteForCookies(SiteForCookies&& other); SiteForCookies(SiteForCookies&& other);
explicit SiteForCookies(const SchemefulSite& schemeful_site);
~SiteForCookies(); ~SiteForCookies();
SiteForCookies& operator=(const SiteForCookies& other); SiteForCookies& operator=(const SiteForCookies& other);
......
...@@ -403,5 +403,27 @@ TEST(SiteForCookiesTest, SameSchemeOpaque) { ...@@ -403,5 +403,27 @@ TEST(SiteForCookiesTest, SameSchemeOpaque) {
} }
} }
TEST_F(SchemefulSiteForCookiesTest, SchemefulSite) {
const char* kTestCases[] = {"opaque.com",
"http://a.com",
"https://sub1.example.com:42/something",
"https://a.com",
"ws://a.com",
"wss://a.com",
"file://a.com",
"file://folder1/folder2/file.txt",
"file:///file.txt"};
for (std::string url : kTestCases) {
url::Origin origin = url::Origin::Create(GURL(url));
SiteForCookies from_origin = SiteForCookies::FromOrigin(origin);
SchemefulSite schemeful_site = SchemefulSite(origin);
SiteForCookies from_schemeful_site = SiteForCookies(schemeful_site);
EXPECT_TRUE(from_origin.IsEquivalent(from_schemeful_site));
EXPECT_TRUE(from_schemeful_site.IsEquivalent(from_origin));
}
}
} // namespace } // namespace
} // namespace net } // namespace net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment