Skip to content

Commit

Permalink
Merge pull request #9095 from /issues/15906-cookieoptions-migration
Browse files Browse the repository at this point in the history
Unify ephemeral storage trigger logic to use net::CookieOptions.
  • Loading branch information
goodov authored Jun 29, 2021
2 parents 06beaa8 + e5225e0 commit aa3746b
Show file tree
Hide file tree
Showing 30 changed files with 947 additions and 171 deletions.
286 changes: 236 additions & 50 deletions browser/ephemeral_storage/ephemeral_storage_browsertest.cc

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions chromium_src/components/content_settings/browser/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_rules = [
"+../../../../../components/content_settings/browser",
"+components/content_settings/browser",
"+components/content_settings/common",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "components/content_settings/browser/content_settings_manager_impl.h"

#include "../../../../../components/content_settings/browser/content_settings_manager_impl.cc"

namespace content_settings {

void ContentSettingsManagerImpl::AllowEphemeralStorageAccess(
int32_t render_frame_id,
StorageType storage_type,
const url::Origin& origin,
const GURL& site_for_cookies,
const url::Origin& top_frame_origin,
base::OnceCallback<void(bool)> callback) {
std::move(callback).Run(cookie_settings_->ShouldUseEphemeralStorage(
origin.GetURL(), site_for_cookies, top_frame_origin));
}

} // namespace content_settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_

#include "components/content_settings/common/content_settings_manager.mojom.h"

#define OnContentBlocked \
NotUsed() {} \
void AllowEphemeralStorageAccess( \
int32_t render_frame_id, StorageType storage_type, \
const url::Origin& origin, const GURL& site_for_cookies, \
const url::Origin& top_frame_origin, \
base::OnceCallback<void(bool)> callback) override; \
void OnContentBlocked

#include "../../../../../components/content_settings/browser/content_settings_manager_impl.h"

#undef OnContentBlocked

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module content_settings.mojom;

import "url/mojom/origin.mojom";
import "url/mojom/url.mojom";

[BraveExtend]
interface ContentSettingsManager {
[Sync]
AllowEphemeralStorageAccess(
int32 render_frame_id,
StorageType storage_type,
url.mojom.Origin origin,
url.mojom.Url site_for_cookies,
url.mojom.Origin top_frame_origin) => (bool ephemeral_storage_allowed);
};
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GURL GetFirstPartyURL(const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) {
return top_frame_origin ? top_frame_origin->GetURL() : site_for_cookies;
}

bool IsFirstPartyAccessAllowed(
const GURL& first_party_url,
const CookieSettingsBase* const cookie_settings) {
Expand All @@ -88,6 +89,15 @@ bool IsFirstPartyAccessAllowed(

} // namespace

ScopedEphemeralStorageAwareness::ScopedEphemeralStorageAwareness(
bool* ephemeral_storage_aware)
: ephemeral_storage_aware_auto_reset_(ephemeral_storage_aware, true) {}
ScopedEphemeralStorageAwareness::~ScopedEphemeralStorageAwareness() = default;
ScopedEphemeralStorageAwareness::ScopedEphemeralStorageAwareness(
ScopedEphemeralStorageAwareness&& rhs) = default;
ScopedEphemeralStorageAwareness& ScopedEphemeralStorageAwareness::operator=(
ScopedEphemeralStorageAwareness&& rhs) = default;

bool CookieSettingsBase::ShouldUseEphemeralStorage(
const GURL& url,
const GURL& site_for_cookies,
Expand All @@ -107,13 +117,18 @@ bool CookieSettingsBase::ShouldUseEphemeralStorage(
return false;

bool allow_3p =
IsCookieAccessAllowed(url, site_for_cookies, top_frame_origin);
IsCookieAccessAllowedImpl(url, site_for_cookies, top_frame_origin);
bool allow_1p = IsFirstPartyAccessAllowed(first_party_url, this);

// only use ephemeral storage for block 3p
return allow_1p && !allow_3p;
}

ScopedEphemeralStorageAwareness
CookieSettingsBase::CreateScopedEphemeralStorageAwareness() const {
return ScopedEphemeralStorageAwareness(&ephemeral_storage_aware_);
}

bool CookieSettingsBase::IsEphemeralCookieAccessAllowed(
const GURL& url,
const GURL& first_party_url) const {
Expand All @@ -124,9 +139,8 @@ bool CookieSettingsBase::IsEphemeralCookieAccessAllowed(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
if (ShouldUseEphemeralStorage(url, site_for_cookies, top_frame_origin))
return true;

auto scoped_ephemeral_storage_awareness =
CreateScopedEphemeralStorageAwareness();
return IsCookieAccessAllowed(url, site_for_cookies, top_frame_origin);
}

Expand All @@ -140,6 +154,18 @@ bool CookieSettingsBase::IsCookieAccessAllowed(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
if (ephemeral_storage_aware_ &&
ShouldUseEphemeralStorage(url, site_for_cookies, top_frame_origin)) {
return true;
}

return IsCookieAccessAllowedImpl(url, site_for_cookies, top_frame_origin);
}

bool CookieSettingsBase::IsCookieAccessAllowedImpl(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
bool allow =
IsChromiumCookieAccessAllowed(url, site_for_cookies, top_frame_origin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_COOKIE_SETTINGS_BASE_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_COOKIE_SETTINGS_BASE_H_

#include "base/auto_reset.h"

namespace content_settings {

// Helper to allow patchless ephemeral storage access in Chromium code.
class ScopedEphemeralStorageAwareness {
public:
explicit ScopedEphemeralStorageAwareness(bool* ephemeral_storage_aware);
ScopedEphemeralStorageAwareness(ScopedEphemeralStorageAwareness&&);
ScopedEphemeralStorageAwareness& operator=(ScopedEphemeralStorageAwareness&&);
~ScopedEphemeralStorageAwareness();

private:
base::AutoReset<bool> ephemeral_storage_aware_auto_reset_;
};

} // namespace content_settings

#define BRAVE_COOKIE_SETTINGS_BASE_H \
bool ShouldUseEphemeralStorage( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
ScopedEphemeralStorageAwareness CreateScopedEphemeralStorageAwareness() \
const; \
bool IsEphemeralCookieAccessAllowed(const GURL& url, \
const GURL& first_party_url) const; \
bool IsEphemeralCookieAccessAllowed( \
Expand All @@ -19,7 +39,14 @@
const GURL& first_party_url) const; \
bool IsChromiumCookieAccessAllowed( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const;
const base::Optional<url::Origin>& top_frame_origin) const; \
\
private: \
bool IsCookieAccessAllowedImpl( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
\
mutable bool ephemeral_storage_aware_ = false;

#include "../../../../../../components/content_settings/core/common/cookie_settings_base.h"

Expand Down
26 changes: 26 additions & 0 deletions chromium_src/net/cookies/cookie_access_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "net/cookies/cookie_access_delegate.h"

#include "base/notreached.h"

#include "../../../../net/cookies/cookie_access_delegate.cc"

namespace net {

bool CookieAccessDelegate::NotUsed() const {
return false;
}

bool CookieAccessDelegate::ShouldUseEphemeralStorage(
const GURL& url,
const net::SiteForCookies& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
NOTREACHED() << "Should be overridden";
return false;
}

} // namespace net
25 changes: 25 additions & 0 deletions chromium_src/net/cookies/cookie_access_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_
#define BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_

#include "base/optional.h"
#include "net/cookies/site_for_cookies.h"
#include "url/gurl.h"
#include "url/origin.h"

#define ShouldTreatUrlAsTrustworthy \
NotUsed() const; \
virtual bool ShouldUseEphemeralStorage( \
const GURL& url, const net::SiteForCookies& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
virtual bool ShouldTreatUrlAsTrustworthy

#include "../../../../net/cookies/cookie_access_delegate.h"

#undef ShouldTreatUrlAsTrustworthy

#endif // BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_
62 changes: 45 additions & 17 deletions chromium_src/net/cookies/cookie_monster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,55 @@ void CookieMonster::SetCookieableSchemes(
ChromiumCookieMonster::SetCookieableSchemes(schemes, std::move(callback));
}

void CookieMonster::GetEphemeralCookieListWithOptionsAsync(
const GURL& url,
const GURL& top_frame_url,
const CookieOptions& options,
GetCookieListCallback callback) {
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(top_frame_url);
ephemeral_monster->GetCookieListWithOptionsAsync(url, options,
std::move(callback));
}

void CookieMonster::SetEphemeralCanonicalCookieAsync(
void CookieMonster::SetCanonicalCookieAsync(
std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const GURL& top_frame_url,
const CookieOptions& options,
SetCookiesCallback callback) {
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(top_frame_url);
ephemeral_monster->SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
if (options.should_use_ephemeral_storage()) {
if (!options.top_frame_origin()) {
// Shouldn't happen, but don't do anything in this case.
NOTREACHED();
MaybeRunCookieCallback(
std::move(callback),
CookieAccessResult(CookieInclusionStatus(
CookieInclusionStatus::EXCLUDE_UNKNOWN_ERROR)));
return;
}
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(
options.top_frame_origin()->GetURL());
ephemeral_monster->SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
return;
}

ChromiumCookieMonster::SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
}

void CookieMonster::GetCookieListWithOptionsAsync(
const GURL& url,
const CookieOptions& options,
GetCookieListCallback callback) {
if (options.should_use_ephemeral_storage()) {
if (!options.top_frame_origin()) {
// Shouldn't happen, but don't do anything in this case.
NOTREACHED();
MaybeRunCookieCallback(std::move(callback), CookieAccessResultList(),
CookieAccessResultList());
return;
}
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(
options.top_frame_origin()->GetURL());
ephemeral_monster->GetCookieListWithOptionsAsync(url, options,
std::move(callback));
return;
}

ChromiumCookieMonster::GetCookieListWithOptionsAsync(url, options,
std::move(callback));
}

} // namespace net
16 changes: 7 additions & 9 deletions chromium_src/net/cookies/cookie_monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ class NET_EXPORT CookieMonster : public ChromiumCookieMonster {
void SetCookieableSchemes(const std::vector<std::string>& schemes,
SetCookieableSchemesCallback callback) override;

void GetEphemeralCookieListWithOptionsAsync(const GURL& url,
const GURL& top_frame_url,
const CookieOptions& options,
GetCookieListCallback callback);
void SetEphemeralCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const GURL& top_frame_url,
const CookieOptions& options,
SetCookiesCallback callback);
void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const CookieOptions& options,
SetCookiesCallback callback) override;
void GetCookieListWithOptionsAsync(const GURL& url,
const CookieOptions& options,
GetCookieListCallback callback) override;

private:
NetLogWithSource net_log_;
Expand Down
47 changes: 47 additions & 0 deletions chromium_src/net/cookies/cookie_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "net/cookies/cookie_options.h"

#include "net/cookies/cookie_access_delegate.h"

#define CookieOptions CookieOptions_ChromiumImpl
#include "../../../../net/cookies/cookie_options.cc"
#undef CookieOptions

namespace net {

CookieOptions::CookieOptions() = default;
CookieOptions::CookieOptions(const CookieOptions&) = default;
CookieOptions::CookieOptions(CookieOptions&&) = default;
CookieOptions::~CookieOptions() = default;
CookieOptions& CookieOptions::operator=(const CookieOptions&) = default;
CookieOptions& CookieOptions::operator=(CookieOptions&&) = default;

CookieOptions::CookieOptions(const CookieOptions_ChromiumImpl& rhs)
: CookieOptions_ChromiumImpl(rhs) {}
CookieOptions::CookieOptions(CookieOptions_ChromiumImpl&& rhs)
: CookieOptions_ChromiumImpl(std::move(rhs)) {}

void FillEphemeralStorageParams(
const GURL& url,
const SiteForCookies& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin,
const CookieAccessDelegate* cookie_access_delegate,
CookieOptions* cookie_options) {
DCHECK(cookie_options);
if (!cookie_access_delegate) {
return;
}
cookie_options->set_should_use_ephemeral_storage(
cookie_access_delegate->ShouldUseEphemeralStorage(url, site_for_cookies,
top_frame_origin));
if (cookie_options->should_use_ephemeral_storage()) {
cookie_options->set_site_for_cookies(site_for_cookies);
cookie_options->set_top_frame_origin(top_frame_origin);
}
}

} // namespace net
Loading

0 comments on commit aa3746b

Please sign in to comment.