Skip to content

Commit

Permalink
Add ephemeral storage enabler to debounce throttler.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Oct 1, 2021
1 parent 470a1c9 commit d111307
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 4 deletions.
2 changes: 2 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/brave_shields/brave_shields_web_contents_observer.h"
#include "brave/browser/debounce/debounce_service_factory.h"
#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
#include "brave/browser/ethereum_remote_client/buildflags/buildflags.h"
#include "brave/browser/net/brave_proxying_url_loader_factory.h"
#include "brave/browser/net/brave_proxying_web_socket.h"
Expand Down Expand Up @@ -747,6 +748,7 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(
brave_shields::DomainBlockNavigationThrottle::MaybeCreateThrottleFor(
handle, g_brave_browser_process->ad_block_service(),
g_brave_browser_process->ad_block_custom_filters_service(),
EphemeralStorageServiceFactory::GetForContext(context),
HostContentSettingsMapFactory::GetForProfile(
Profile::FromBrowserContext(context)),
g_browser_process->GetApplicationLocale()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "net/base/features.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"

using content::RenderFrameHost;
using content::WebContents;
Expand Down
55 changes: 55 additions & 0 deletions browser/ephemeral_storage/ephemeral_storage_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* 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 "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"

#include <memory>

#include "base/feature_list.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "net/base/features.h"

// static
EphemeralStorageServiceFactory* EphemeralStorageServiceFactory::GetInstance() {
return base::Singleton<EphemeralStorageServiceFactory>::get();
}

// static
ephemeral_storage::EphemeralStorageService*
EphemeralStorageServiceFactory::GetForContext(
content::BrowserContext* context) {
return static_cast<ephemeral_storage::EphemeralStorageService*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}

EphemeralStorageServiceFactory::EphemeralStorageServiceFactory()
: BrowserContextKeyedServiceFactory(
"EphemeralStorageService",
BrowserContextDependencyManager::GetInstance()) {}

EphemeralStorageServiceFactory::~EphemeralStorageServiceFactory() {}

KeyedService* EphemeralStorageServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
if (!base::FeatureList::IsEnabled(
net::features::kBraveFirstPartyEphemeralStorage)) {
return nullptr;
}
return new ephemeral_storage::EphemeralStorageService(
context, HostContentSettingsMapFactory::GetForProfile(context));
}

content::BrowserContext* EphemeralStorageServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}

bool EphemeralStorageServiceFactory::ServiceIsCreatedWithBrowserContext()
const {
return false;
}
36 changes: 36 additions & 0 deletions browser/ephemeral_storage/ephemeral_storage_service_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* 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_BROWSER_EPHEMERAL_STORAGE_EPHEMERAL_STORAGE_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_EPHEMERAL_STORAGE_EPHEMERAL_STORAGE_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"

namespace ephemeral_storage {
class EphemeralStorageService;
}

class EphemeralStorageServiceFactory
: public BrowserContextKeyedServiceFactory {
public:
static ephemeral_storage::EphemeralStorageService* GetForContext(
content::BrowserContext* context);
static EphemeralStorageServiceFactory* GetInstance();

private:
friend struct base::DefaultSingletonTraits<EphemeralStorageServiceFactory>;

EphemeralStorageServiceFactory();
~EphemeralStorageServiceFactory() override;

KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
bool ServiceIsCreatedWithBrowserContext() const override;
};

#endif // BRAVE_BROWSER_EPHEMERAL_STORAGE_EPHEMERAL_STORAGE_SERVICE_FACTORY_H_
3 changes: 3 additions & 0 deletions browser/ephemeral_storage/sources.gni
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
brave_browser_ephemeral_storage_sources = [
"//brave/browser/ephemeral_storage/ephemeral_storage_service_factory.cc",
"//brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h",
"//brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.cc",
"//brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h",
]

brave_browser_ephemeral_storage_deps = [
"//base",
"//brave/components/ephemeral_storage",
"//chrome/browser/profiles",
"//chrome/browser/ui",
"//components/content_settings/core/browser",
Expand Down
25 changes: 25 additions & 0 deletions components/brave_shields/browser/domain_block_controller_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/domain_block_tab_storage.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
#include "components/prefs/pref_service.h"
#include "components/security_interstitials/content/settings_page_helper.h"
#include "components/security_interstitials/core/metrics_helper.h"
Expand All @@ -32,6 +33,7 @@ DomainBlockControllerClient::DomainBlockControllerClient(
content::WebContents* web_contents,
const GURL& request_url,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
PrefService* prefs,
const std::string& locale)
: security_interstitials::SecurityInterstitialControllerClient(
Expand All @@ -43,8 +45,11 @@ DomainBlockControllerClient::DomainBlockControllerClient(
nullptr /* settings_page_helper */),
request_url_(request_url),
ad_block_custom_filters_service_(ad_block_custom_filters_service),
ephemeral_storage_service_(ephemeral_storage_service),
dont_warn_again_(false) {}

DomainBlockControllerClient::~DomainBlockControllerClient() = default;

void DomainBlockControllerClient::GoBack() {
SecurityInterstitialControllerClient::GoBackAfterNavigationCommitted();
}
Expand All @@ -59,9 +64,29 @@ void DomainBlockControllerClient::Proceed() {
ad_block_custom_filters_service_->UpdateCustomFilters(
"@@||" + request_url_.host() + "^\n" + custom_filters);
}
if (ephemeral_storage_service_) {
ephemeral_storage_service_->CanEnable1PESForUrl(
request_url_,
base::BindOnce(&DomainBlockControllerClient::OnCanEnable1PESForUrl,
weak_ptr_factory_.GetWeakPtr()));
} else {
ReloadPage();
}
}

void DomainBlockControllerClient::ReloadPage() {
web_contents_->GetController().Reload(content::ReloadType::NORMAL, false);
}

void DomainBlockControllerClient::OnCanEnable1PESForUrl(bool can_enable_1pes) {
LOG(ERROR) << "OnCanEnable1PESForUrl can_enable_1pes " << can_enable_1pes;
if (can_enable_1pes) {
LOG(ERROR) << can_enable_1pes;
ephemeral_storage_service_->Enable1PESForUrl(request_url_, true);
}
ReloadPage();
}

void DomainBlockControllerClient::SetDontWarnAgain(bool value) {
dont_warn_again_ = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>
#include <string>

#include "base/memory/weak_ptr.h"
#include "components/security_interstitials/content/security_interstitial_controller_client.h"
#include "url/gurl.h"

Expand All @@ -20,6 +21,10 @@ namespace security_interstitials {
class MetricsHelper;
} // namespace security_interstitials

namespace ephemeral_storage {
class EphemeralStorageService;
} // namespace ephemeral_storage

namespace brave_shields {

class AdBlockCustomFiltersService;
Expand All @@ -34,9 +39,10 @@ class DomainBlockControllerClient
content::WebContents* web_contents,
const GURL& request_url,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
PrefService* prefs,
const std::string& locale);
~DomainBlockControllerClient() override = default;
~DomainBlockControllerClient() override;

DomainBlockControllerClient(const DomainBlockControllerClient&) = delete;
DomainBlockControllerClient& operator=(const DomainBlockControllerClient&) =
Expand All @@ -49,9 +55,15 @@ class DomainBlockControllerClient
void Proceed() override;

private:
void ReloadPage();
void OnCanEnable1PESForUrl(bool can_enable_1pes);

const GURL request_url_;
AdBlockCustomFiltersService* ad_block_custom_filters_service_;
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service_;
bool dont_warn_again_;

base::WeakPtrFactory<DomainBlockControllerClient> weak_ptr_factory_{this};
};

} // namespace brave_shields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ DomainBlockNavigationThrottle::MaybeCreateThrottleFor(
content::NavigationHandle* navigation_handle,
AdBlockService* ad_block_service,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
HostContentSettingsMap* content_settings,
const std::string& locale) {
if (!ad_block_service || !ad_block_custom_filters_service)
Expand All @@ -70,18 +71,20 @@ DomainBlockNavigationThrottle::MaybeCreateThrottleFor(
return nullptr;
return std::make_unique<DomainBlockNavigationThrottle>(
navigation_handle, ad_block_service, ad_block_custom_filters_service,
content_settings, locale);
ephemeral_storage_service, content_settings, locale);
}

DomainBlockNavigationThrottle::DomainBlockNavigationThrottle(
content::NavigationHandle* navigation_handle,
AdBlockService* ad_block_service,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
HostContentSettingsMap* content_settings,
const std::string& locale)
: content::NavigationThrottle(navigation_handle),
ad_block_service_(ad_block_service),
ad_block_custom_filters_service_(ad_block_custom_filters_service),
ephemeral_storage_service_(ephemeral_storage_service),
content_settings_(content_settings),
locale_(locale) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Expand Down Expand Up @@ -170,8 +173,8 @@ void DomainBlockNavigationThrottle::ShowInterstitial() {
// The controller client implements the actual logic to "go back" or "proceed"
// from the interstitial.
auto controller_client = std::make_unique<DomainBlockControllerClient>(
web_contents, request_url, ad_block_custom_filters_service_, pref_service,
locale_);
web_contents, request_url, ad_block_custom_filters_service_,
ephemeral_storage_service_, pref_service, locale_);

// This handles populating the HTML template of the interstitial page with
// localized strings and other information we only know at runtime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class NavigationHandle;
class WebContents;
} // namespace content

namespace ephemeral_storage {
class EphemeralStorageService;
} // namespace ephemeral_storage

namespace brave_shields {

class AdBlockService;
Expand All @@ -32,6 +36,7 @@ class DomainBlockNavigationThrottle : public content::NavigationThrottle {
content::NavigationHandle* navigation_handle,
AdBlockService* ad_block_service,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
HostContentSettingsMap* content_settings,
const std::string& locale);
~DomainBlockNavigationThrottle() override;
Expand All @@ -44,6 +49,7 @@ class DomainBlockNavigationThrottle : public content::NavigationThrottle {
content::NavigationHandle* navigation_handle,
AdBlockService* ad_block_service,
AdBlockCustomFiltersService* ad_block_custom_filters_service,
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service,
HostContentSettingsMap* content_settings,
const std::string& locale);

Expand All @@ -61,6 +67,8 @@ class DomainBlockNavigationThrottle : public content::NavigationThrottle {

AdBlockService* ad_block_service_ = nullptr;
AdBlockCustomFiltersService* ad_block_custom_filters_service_ = nullptr;
ephemeral_storage::EphemeralStorageService* ephemeral_storage_service_ =
nullptr;
HostContentSettingsMap* content_settings_ = nullptr;
std::string locale_;
base::WeakPtrFactory<DomainBlockNavigationThrottle> weak_ptr_factory_{this};
Expand Down
24 changes: 24 additions & 0 deletions components/ephemeral_storage/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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/. */

static_library("ephemeral_storage") {
sources = [
"ephemeral_storage_service.cc",
"ephemeral_storage_service.h",
]

deps = [
"//base",
"//components/keyed_service/core",
"//components/prefs",
"//components/security_interstitials/content:security_interstitial_page",
"//components/security_interstitials/core",
"//components/user_prefs",
"//content/public/browser",
"//net",
"//ui/base",
"//url",
]
}
Loading

0 comments on commit d111307

Please sign in to comment.