Skip to content

Commit

Permalink
Implement proper in-memory ephemeral localStorage support.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Jul 9, 2021
1 parent c0af31d commit e5774fb
Show file tree
Hide file tree
Showing 23 changed files with 432 additions and 68 deletions.
16 changes: 0 additions & 16 deletions browser/ephemeral_storage/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
source_set("ephemeral_storage") {
sources = [
"ephemeral_storage_tab_helper.cc",
"ephemeral_storage_tab_helper.h",
]

deps = [
"//base",
"//chrome/browser/ui",
"//content/public/browser",
"//net",
"//third_party/blink/public/common",
]
}

if (!is_android) {
source_set("ephemeral_storage_tests") {
testonly = true
Expand All @@ -24,7 +9,6 @@ if (!is_android) {
]
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
deps = [
":ephemeral_storage",
"//base",
"//brave/components/brave_shields/browser:browser",
"//brave/components/brave_shields/common:common",
Expand Down
26 changes: 25 additions & 1 deletion browser/ephemeral_storage/ephemeral_storage_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
#include "brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h"

#include <map>
#include <memory>
#include <set>

#include "base/feature_list.h"
#include "base/hash/md5.h"
#include "base/ranges/ranges.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/storage_partition.h"
Expand Down Expand Up @@ -47,6 +51,23 @@ std::string StringToSessionStorageId(const std::string& string,
return hash;
}

class EphemeralStorageOriginsSourceImpl
: public content::TLDEphemeralLifetime::EphemeralStorageOriginsSource {
public:
EphemeralStorageOriginsSourceImpl(
scoped_refptr<content_settings::CookieSettings> cookie_settings)
: cookie_settings_(std::move(cookie_settings)) {}

base::flat_map<url::Origin, url::Origin> TakeEphemeralStorageOrigins(
const std::string& ephemeral_storage_domain) override {
return cookie_settings_->TakeEphemeralStorageOrigins(
ephemeral_storage_domain);
}

private:
scoped_refptr<content_settings::CookieSettings> cookie_settings_;
};

} // namespace

// EphemeralStorageTabHelper helps to manage the lifetime of ephemeral storage.
Expand Down Expand Up @@ -176,7 +197,10 @@ void EphemeralStorageTabHelper::CreateEphemeralStorageAreasForDomainAndURL(
: absl::nullopt);

tld_ephemeral_lifetime_ = content::TLDEphemeralLifetime::GetOrCreate(
browser_context, partition, new_domain);
browser_context, partition, new_domain,
std::make_unique<EphemeralStorageOriginsSourceImpl>(
CookieSettingsFactory::GetForProfile(
Profile::FromBrowserContext(browser_context))));
}

// static
Expand Down
14 changes: 14 additions & 0 deletions browser/ephemeral_storage/sources.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
brave_browser_ephemeral_storage_sources = [
"//brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.cc",
"//brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h",
]

brave_browser_ephemeral_storage_deps = [
"//base",
"//chrome/browser/profiles",
"//chrome/browser/ui",
"//components/content_settings/core/browser",
"//content/public/browser",
"//net",
"//third_party/blink/public/common",
]
1 change: 0 additions & 1 deletion browser/permissions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ if (!is_android) {

deps = [
"//brave/browser",
"//brave/browser/ephemeral_storage",
"//chrome/browser",
"//chrome/browser/ui",
"//chrome/test:test_support",
Expand Down
4 changes: 3 additions & 1 deletion browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import("//brave/browser/component_updater/sources.gni")
import("//brave/browser/crypto_dot_com/sources.gni")
import("//brave/browser/decentralized_dns/sources.gni")
import("//brave/browser/download/sources.gni")
import("//brave/browser/ephemeral_storage/sources.gni")
import("//brave/browser/ethereum_remote_client/buildflags/buildflags.gni")
import("//brave/browser/gemini/sources.gni")
import("//brave/browser/greaselion/sources.gni")
Expand Down Expand Up @@ -92,7 +93,6 @@ brave_chrome_browser_deps = [
"//brave/browser/brave_ads/notifications",
"//brave/browser/component_updater",
"//brave/browser/content_settings",
"//brave/browser/ephemeral_storage",
"//brave/browser/gcm_driver",
"//brave/browser/net",
"//brave/browser/ntp_background_images",
Expand Down Expand Up @@ -338,6 +338,7 @@ brave_chrome_browser_sources += brave_browser_component_updater_sources
brave_chrome_browser_sources += brave_browser_crypto_dot_com_sources
brave_chrome_browser_sources += brave_browser_decentralized_dns_sources
brave_chrome_browser_sources += brave_browser_download_sources
brave_chrome_browser_sources += brave_browser_ephemeral_storage_sources
brave_chrome_browser_sources += brave_browser_gemini_sources
brave_chrome_browser_sources += brave_browser_greaselion_sources
brave_chrome_browser_sources += brave_browser_importer_sources
Expand All @@ -358,6 +359,7 @@ brave_chrome_browser_deps += brave_browser_component_updater_deps
brave_chrome_browser_deps += brave_browser_crypto_dot_com_deps
brave_chrome_browser_deps += brave_browser_decentralized_dns_deps
brave_chrome_browser_deps += brave_browser_download_deps
brave_chrome_browser_deps += brave_browser_ephemeral_storage_deps
brave_chrome_browser_deps += brave_browser_gemini_deps
brave_chrome_browser_deps += brave_browser_greaselion_deps
brave_chrome_browser_deps += brave_browser_importer_deps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ void ContentSettingsManagerImpl::AllowEphemeralStorageAccess(
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));
AllowEphemeralStorageAccessCallback callback) {
url::Origin storage_origin;
const bool should_use = cookie_settings_->ShouldUseEphemeralStorage(
origin, site_for_cookies, top_frame_origin, storage_origin);
std::move(callback).Run(should_use
? absl::make_optional<url::Origin>(storage_origin)
: absl::nullopt);
}

} // namespace content_settings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#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 "base/containers/flat_map.h"
#include "components/content_settings/common/content_settings_manager.mojom.h"

#define OnContentBlocked \
Expand All @@ -14,7 +15,7 @@
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; \
AllowEphemeralStorageAccessCallback callback) override; \
void OnContentBlocked

#include "../../../../../components/content_settings/browser/content_settings_manager_impl.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface ContentSettingsManager {
StorageType storage_type,
url.mojom.Origin origin,
url.mojom.Url site_for_cookies,
url.mojom.Origin top_frame_origin) => (bool ephemeral_storage_allowed);
url.mojom.Origin top_frame_origin) => (url.mojom.Origin? storage_origin);
};
1 change: 1 addition & 0 deletions chromium_src/components/content_settings/core/browser/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ include_rules = [
"+../../../../../../components/content_settings/core/browser",
"+components/content_settings/core/browser",
"+components/content_settings/core/common",
"+components/keyed_service/core",
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,74 @@
* 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/core/browser/cookie_settings.h"

#include "net/base/url_util.h"

#define BRAVE_COOKIE_SETTINGS_GET_COOKIES_SETTINGS_INTERNAL \
if (setting == CONTENT_SETTING_SESSION_ONLY && !block_third && \
ShouldBlockThirdPartyCookies() && \
!first_party_url.SchemeIs(extension_scheme_)) { \
block_third = true; \
}

#define ShutdownOnUIThread ShutdownOnUIThread_ChromiumImpl

#include "../../../../../../components/content_settings/core/browser/cookie_settings.cc"

#undef ShutdownOnUIThread

namespace content_settings {

void CookieSettings::ShutdownOnUIThread() {
ShutdownOnUIThread_ChromiumImpl();
ephemeral_storage_origins_.clear();
}

bool CookieSettings::ShouldUseEphemeralStorage(
const url::Origin& origin,
const GURL& site_for_cookies,
const absl::optional<url::Origin>& top_frame_origin,
url::Origin& storage_origin) {
const bool should_use = CookieSettingsBase::ShouldUseEphemeralStorage(
origin.GetURL(), site_for_cookies, top_frame_origin);
if (!should_use) {
return false;
}
DCHECK(top_frame_origin);
const std::string ephemeral_storage_domain =
net::URLToEphemeralStorageDomain(top_frame_origin->GetURL());

auto ephemeral_storage_origins_it =
ephemeral_storage_origins_.find(ephemeral_storage_domain);
if (ephemeral_storage_origins_it != ephemeral_storage_origins_.end()) {
const auto& storage_origins = ephemeral_storage_origins_it->second;
auto storage_origin_it = storage_origins.find(origin);
if (storage_origin_it != storage_origins.end()) {
storage_origin = storage_origin_it->second;
return true;
}
}

url::Origin opaque_origin = origin.DeriveNewOpaqueOrigin();
ephemeral_storage_origins_[ephemeral_storage_domain][origin] = opaque_origin;
storage_origin = std::move(opaque_origin);
return true;
}

base::flat_map<url::Origin, url::Origin>
CookieSettings::TakeEphemeralStorageOrigins(
const std::string& ephemeral_storage_domain) {
base::flat_map<url::Origin, url::Origin> result;
auto ephemeral_storage_origins_it =
ephemeral_storage_origins_.find(ephemeral_storage_domain);
if (ephemeral_storage_origins_it != ephemeral_storage_origins_.end()) {
result = std::move(ephemeral_storage_origins_it->second);
ephemeral_storage_origins_.erase(ephemeral_storage_origins_it);
}
return result;
}

} // namespace content_settings

#undef BRAVE_COOKIE_SETTINGS_GET_COOKIES_SETTINGS_INTERNAL
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* 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_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_COOKIE_SETTINGS_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_COOKIE_SETTINGS_H_

#include "base/containers/flat_map.h"
#include "components/content_settings/core/browser/content_settings_provider.h"
#include "components/keyed_service/core/refcounted_keyed_service.h"
#include "url/origin.h"

#define ShutdownOnUIThread \
ShutdownOnUIThread_ChromiumImpl(); \
bool ShouldUseEphemeralStorage( \
const url::Origin& origin, const GURL& site_for_cookies, \
const absl::optional<url::Origin>& top_frame_origin, \
url::Origin& storage_origin); \
base::flat_map<url::Origin, url::Origin> TakeEphemeralStorageOrigins( \
const std::string& ephemeral_storage_domain); \
\
private: \
base::flat_map<std::string, base::flat_map<url::Origin, url::Origin>> \
ephemeral_storage_origins_; \
\
public: \
void ShutdownOnUIThread

#include "../../../../../../components/content_settings/core/browser/cookie_settings.h"

#undef ShutdownOnUIThread

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_COOKIE_SETTINGS_H_
1 change: 1 addition & 0 deletions chromium_src/components/content_settings/core/common/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+../../../../../../components/content_settings/core/common",
"+components/content_settings/common",
"+components/content_settings/core/common",
]
4 changes: 4 additions & 0 deletions chromium_src/components/services/storage/dom_storage/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_rules = [
"+../../../../../../components/services/storage",
"+components/services/storage",
]
Loading

0 comments on commit e5774fb

Please sign in to comment.