Commit 08057113 authored by Dylan Cutler's avatar Dylan Cutler Committed by Commit Bot

Add CanonicalCookie::FromStorage() static factory method.

This method is meant for cases where we want to copy the properties of
an existing cookie already in the cookie store to another
instance of CanonicalCookie.

Bug: 1102874
Change-Id: I1ab8f732538e4e33bc8a3a350ee547962bf36850
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2452969
Commit-Queue: Dylan Cutler <dylancutler@google.com>
Reviewed-by: default avatarMaksim Orlovich <morlovich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814539}
parent 64175d1f
...@@ -504,6 +504,27 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::CreateSanitizedCookie( ...@@ -504,6 +504,27 @@ std::unique_ptr<CanonicalCookie> CanonicalCookie::CreateSanitizedCookie(
return cc; return cc;
} }
// static
std::unique_ptr<CanonicalCookie> CanonicalCookie::FromStorage(
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
const base::Time& creation,
const base::Time& expiration,
const base::Time& last_access,
bool secure,
bool httponly,
CookieSameSite same_site,
CookiePriority priority,
CookieSourceScheme source_scheme) {
std::unique_ptr<CanonicalCookie> cc(std::make_unique<CanonicalCookie>(
name, value, domain, path, creation, expiration, last_access, secure,
httponly, same_site, priority, source_scheme));
DCHECK(cc->IsCanonical());
return cc;
}
std::string CanonicalCookie::DomainWithoutDot() const { std::string CanonicalCookie::DomainWithoutDot() const {
return cookie_util::CookieDomainAsHost(domain_); return cookie_util::CookieDomainAsHost(domain_);
} }
......
...@@ -106,6 +106,25 @@ class NET_EXPORT CanonicalCookie { ...@@ -106,6 +106,25 @@ class NET_EXPORT CanonicalCookie {
CookieSameSite same_site, CookieSameSite same_site,
CookiePriority priority); CookiePriority priority);
// FromStorage is a factory method which is meant for creating a new
// CanonicalCookie using properties of a previously existing cookie
// that was already ingested into the cookie store.
// This should NOT be used to create a new CanonicalCookie that was not
// already in the store.
static std::unique_ptr<CanonicalCookie> FromStorage(
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
const base::Time& creation,
const base::Time& expiration,
const base::Time& last_access,
bool secure,
bool httponly,
CookieSameSite same_site,
CookiePriority priority,
CookieSourceScheme source_scheme);
const std::string& Name() const { return name_; } const std::string& Name() const { return name_; }
const std::string& Value() const { return value_; } const std::string& Value() const { return value_; }
// We represent the cookie's host-only-flag as the absence of a leading dot in // We represent the cookie's host-only-flag as the absence of a leading dot in
......
...@@ -2098,6 +2098,33 @@ TEST(CanonicalCookieTest, CreateSanitizedCookie_Logic) { ...@@ -2098,6 +2098,33 @@ TEST(CanonicalCookieTest, CreateSanitizedCookie_Logic) {
#endif #endif
} }
TEST(CanonicalCookieTest, FromStorage) {
base::Time two_hours_ago = base::Time::Now() - base::TimeDelta::FromHours(2);
base::Time one_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1);
base::Time one_hour_from_now =
base::Time::Now() + base::TimeDelta::FromHours(1);
std::unique_ptr<CanonicalCookie> cc = CanonicalCookie::FromStorage(
"A", "B", "www.foo.com", "/bar", two_hours_ago, one_hour_from_now,
one_hour_ago, false /*secure*/, false /*httponly*/,
CookieSameSite::NO_RESTRICTION, COOKIE_PRIORITY_DEFAULT,
CookieSourceScheme::kSecure);
EXPECT_TRUE(cc);
EXPECT_EQ("A", cc->Name());
EXPECT_EQ("B", cc->Value());
EXPECT_EQ("www.foo.com", cc->Domain());
EXPECT_EQ("/bar", cc->Path());
EXPECT_EQ(two_hours_ago, cc->CreationDate());
EXPECT_EQ(one_hour_ago, cc->LastAccessDate());
EXPECT_EQ(one_hour_from_now, cc->ExpiryDate());
EXPECT_FALSE(cc->IsSecure());
EXPECT_FALSE(cc->IsHttpOnly());
EXPECT_EQ(CookieSameSite::NO_RESTRICTION, cc->SameSite());
EXPECT_EQ(COOKIE_PRIORITY_MEDIUM, cc->Priority());
EXPECT_EQ(CookieSourceScheme::kSecure, cc->SourceScheme());
EXPECT_FALSE(cc->IsDomainCookie());
}
TEST(CanonicalCookieTest, IsSetPermittedInContext) { TEST(CanonicalCookieTest, IsSetPermittedInContext) {
GURL url("http://www.example.com/test"); GURL url("http://www.example.com/test");
base::Time current_time = base::Time::Now(); base::Time current_time = base::Time::Now();
......
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