diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 3b385d81ad2b..25f0cb42e320 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -17,9 +17,9 @@ #include "brave/browser/renderer_host/brave_navigation_ui_data.h" #include "brave/browser/tor/tor_profile_service_factory.h" #include "brave/common/brave_cookie_blocking.h" -#include "brave/common/webui_url_constants.h" -#include "brave/common/tor/tor_launcher.mojom.h" #include "brave/common/tor/switches.h" +#include "brave/common/tor/tor_launcher.mojom.h" +#include "brave/common/webui_url_constants.h" #include "brave/components/brave_ads/browser/buildflags/buildflags.h" #include "brave/components/brave_rewards/browser/buildflags/buildflags.h" #include "brave/components/brave_shields/browser/brave_shields_util.h" @@ -50,11 +50,11 @@ #include "services/service_manager/public/cpp/manifest_builder.h" #include "ui/base/l10n/l10n_util.h" +using brave_shields::BraveShieldsWebContentsObserver; using content::BrowserThread; using content::ContentBrowserClient; using content::RenderFrameHost; using content::WebContents; -using brave_shields::BraveShieldsWebContentsObserver; #if BUILDFLAG(BRAVE_ADS_ENABLED) #include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h" @@ -92,15 +92,14 @@ bool HandleURLOverrideRewrite(GURL* url, } bool HandleURLReverseOverrideRewrite(GURL* url, - content::BrowserContext* browser_context) { + content::BrowserContext* browser_context) { if (HandleURLOverrideRewrite(url, browser_context)) return true; return false; } -bool HandleURLRewrite(GURL* url, - content::BrowserContext* browser_context) { +bool HandleURLRewrite(GURL* url, content::BrowserContext* browser_context) { if (HandleURLOverrideRewrite(url, browser_context)) return true; @@ -111,13 +110,12 @@ bool HandleURLRewrite(GURL* url, BraveContentBrowserClient::BraveContentBrowserClient( ChromeFeatureListCreator* chrome_feature_list_creator) - : ChromeContentBrowserClient(chrome_feature_list_creator) { -} + : ChromeContentBrowserClient(chrome_feature_list_creator) {} BraveContentBrowserClient::~BraveContentBrowserClient() {} content::BrowserMainParts* BraveContentBrowserClient::CreateBrowserMainParts( - const content::MainFunctionParams& parameters) { + const content::MainFunctionParams& parameters) { ChromeBrowserMainParts* main_parts = static_cast( ChromeContentBrowserClient::CreateBrowserMainParts(parameters)); main_parts->AddParts(new BraveBrowserMainExtraParts()); @@ -130,8 +128,7 @@ void BraveContentBrowserClient::BrowserURLHandlerCreated( content::BrowserURLHandler::null_handler()); handler->AddHandlerPair(&webtorrent::HandleTorrentURLRewrite, &webtorrent::HandleTorrentURLReverseRewrite); - handler->AddHandlerPair(&HandleURLRewrite, - &HandleURLReverseOverrideRewrite); + handler->AddHandlerPair(&HandleURLRewrite, &HandleURLReverseOverrideRewrite); ChromeContentBrowserClient::BrowserURLHandlerCreated(handler); } @@ -143,9 +140,9 @@ bool BraveContentBrowserClient::AllowAccessCookie( int render_frame_id) { GURL tab_origin = BraveShieldsWebContentsObserver::GetTabURLFromRenderFrameInfo( - render_process_id, render_frame_id, -1).GetOrigin(); + render_process_id, render_frame_id, -1) + .GetOrigin(); ProfileIOData* io_data = ProfileIOData::FromResourceContext(context); - bool allow_brave_shields = brave_shields::IsAllowContentSettingWithIOData( io_data, tab_origin, tab_origin, CONTENT_SETTINGS_TYPE_PLUGINS, @@ -159,21 +156,22 @@ bool BraveContentBrowserClient::AllowAccessCookie( brave_shields::kCookies); content_settings::BraveCookieSettings* cookie_settings = (content_settings::BraveCookieSettings*)io_data->GetCookieSettings(); - bool allow = !ShouldBlockCookie(allow_brave_shields, - allow_1p_cookies, - allow_3p_cookies, - first_party, - url, - cookie_settings->GetAllowGoogleAuth()) && - g_brave_browser_process->tracking_protection_service()->ShouldStoreState(cookie_settings, - io_data->GetHostContentSettingsMap(), render_process_id, - render_frame_id, url, first_party, tab_origin); + bool allow = + !ShouldBlockCookie(allow_brave_shields, allow_1p_cookies, + allow_3p_cookies, first_party, url, + cookie_settings->GetAllowGoogleAuth()) && + g_brave_browser_process->tracking_protection_service()->ShouldStoreState( + cookie_settings, io_data->GetHostContentSettingsMap(), + render_process_id, render_frame_id, url, first_party, tab_origin); return allow; } -bool BraveContentBrowserClient::AllowGetCookie(const GURL& url, - const GURL& first_party, const net::CookieList& cookie_list, - content::ResourceContext* context, int render_process_id, +bool BraveContentBrowserClient::AllowGetCookie( + const GURL& url, + const GURL& first_party, + const net::CookieList& cookie_list, + content::ResourceContext* context, + int render_process_id, int render_frame_id) { DCHECK_CURRENTLY_ON(BrowserThread::IO); bool allow = AllowAccessCookie(url, first_party, context, render_process_id, @@ -184,9 +182,12 @@ bool BraveContentBrowserClient::AllowGetCookie(const GURL& url, return allow; } -bool BraveContentBrowserClient::AllowSetCookie(const GURL& url, - const GURL& first_party, const net::CanonicalCookie& cookie, - content::ResourceContext* context, int render_process_id, +bool BraveContentBrowserClient::AllowSetCookie( + const GURL& url, + const GURL& first_party, + const net::CanonicalCookie& cookie, + content::ResourceContext* context, + int render_process_id, int render_frame_id) { DCHECK_CURRENTLY_ON(BrowserThread::IO); bool allow = AllowAccessCookie(url, first_party, context, render_process_id, @@ -205,55 +206,47 @@ BraveContentBrowserClient::AllowWebBluetooth( } bool BraveContentBrowserClient::HandleExternalProtocol( - const GURL& url, - content::ResourceRequestInfo::WebContentsGetter web_contents_getter, - int child_id, - content::NavigationUIData* navigation_data, - bool is_main_frame, - ui::PageTransition page_transition, - bool has_user_gesture, - const std::string& method, - const net::HttpRequestHeaders& headers) { + const GURL& url, + content::ResourceRequestInfo::WebContentsGetter web_contents_getter, + int child_id, + content::NavigationUIData* navigation_data, + bool is_main_frame, + ui::PageTransition page_transition, + bool has_user_gesture, + const std::string& method, + const net::HttpRequestHeaders& headers) { if (webtorrent::HandleMagnetProtocol(url, web_contents_getter, - page_transition, has_user_gesture)) { + page_transition, has_user_gesture)) { return true; } return ChromeContentBrowserClient::HandleExternalProtocol( - url, - web_contents_getter, - child_id, - navigation_data, - is_main_frame, - page_transition, - has_user_gesture, - method, - headers); + url, web_contents_getter, child_id, navigation_data, is_main_frame, + page_transition, has_user_gesture, method, headers); } void BraveContentBrowserClient::RegisterOutOfProcessServices( - OutOfProcessServiceMap* services) { + OutOfProcessServiceMap* services) { ChromeContentBrowserClient::RegisterOutOfProcessServices(services); (*services)[tor::mojom::kTorLauncherServiceName] = base::BindRepeating( - l10n_util::GetStringUTF16, IDS_UTILITY_PROCESS_TOR_LAUNCHER_NAME); + l10n_util::GetStringUTF16, IDS_UTILITY_PROCESS_TOR_LAUNCHER_NAME); #if BUILDFLAG(BRAVE_ADS_ENABLED) - (*services)[bat_ads::mojom::kServiceName] = base::BindRepeating( - l10n_util::GetStringUTF16, IDS_SERVICE_BAT_ADS); + (*services)[bat_ads::mojom::kServiceName] = + base::BindRepeating(l10n_util::GetStringUTF16, IDS_SERVICE_BAT_ADS); #endif #if BUILDFLAG(BRAVE_REWARDS_ENABLED) (*services)[bat_ledger::mojom::kServiceName] = base::BindRepeating( - l10n_util::GetStringUTF16, IDS_UTILITY_PROCESS_LEDGER_NAME); + l10n_util::GetStringUTF16, IDS_UTILITY_PROCESS_LEDGER_NAME); #endif } std::unique_ptr BraveContentBrowserClient::GetNavigationUIData( - content::NavigationHandle* navigation_handle) { + content::NavigationHandle* navigation_handle) { std::unique_ptr navigation_ui_data = - std::make_unique(navigation_handle); - Profile* profile = - Profile::FromBrowserContext(navigation_handle->GetWebContents() - ->GetBrowserContext()); + std::make_unique(navigation_handle); + Profile* profile = Profile::FromBrowserContext( + navigation_handle->GetWebContents()->GetBrowserContext()); TorProfileServiceFactory::SetTorNavigationUIData(profile, navigation_ui_data.get()); return std::move(navigation_ui_data); @@ -274,11 +267,11 @@ void BraveContentBrowserClient::AdjustUtilityServiceProcessCommandLine( const service_manager::Identity& identity, base::CommandLine* command_line) { ChromeContentBrowserClient::AdjustUtilityServiceProcessCommandLine( - identity, command_line); + identity, command_line); if (identity.name() == tor::mojom::kTorLauncherServiceName) { base::FilePath path = - g_brave_browser_process->tor_client_updater()->GetExecutablePath(); + g_brave_browser_process->tor_client_updater()->GetExecutablePath(); DCHECK(!path.empty()); command_line->AppendSwitchPath(tor::switches::kTorExecutablePath, path.BaseName()); @@ -296,28 +289,15 @@ void BraveContentBrowserClient::MaybeHideReferrer( } Profile* profile = Profile::FromBrowserContext(browser_context); - const bool allow_referrers = - brave_shields::IsAllowContentSettingsForProfile( - profile, - document_url, - document_url, - CONTENT_SETTINGS_TYPE_PLUGINS, - brave_shields::kReferrers); - const bool shields_up = - brave_shields::IsAllowContentSettingsForProfile( - profile, - document_url, - GURL(), - CONTENT_SETTINGS_TYPE_PLUGINS, - brave_shields::kBraveShields); - brave_shields::ShouldSetReferrer(allow_referrers, - shields_up, - referrer->url, - document_url, - request_url, - request_url.GetOrigin(), - referrer->policy, - referrer); + const bool allow_referrers = brave_shields::IsAllowContentSettingsForProfile( + profile, document_url, document_url, CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kReferrers); + const bool shields_up = brave_shields::IsAllowContentSettingsForProfile( + profile, document_url, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kBraveShields); + brave_shields::ShouldSetReferrer( + allow_referrers, shields_up, referrer->url, document_url, request_url, + request_url.GetOrigin(), referrer->policy, referrer); } GURL BraveContentBrowserClient::GetEffectiveURL( diff --git a/browser/brave_tab_helpers.cc b/browser/brave_tab_helpers.cc index 3980e1df3717..66fc46cedf5d 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -37,8 +37,7 @@ void AttachTabHelpers(content::WebContents* web_contents) { BraveDrmTabHelper::CreateForWebContents(web_contents); #if BUILDFLAG(BRAVE_STP_ENABLED) if (TrackingProtectionHelper::IsSmartTrackingProtectionEnabled()) { - brave_shields::TrackingProtectionHelper::CreateForWebContents( - web_contents); + brave_shields::TrackingProtectionHelper::CreateForWebContents(web_contents); } #endif #endif diff --git a/browser/content_settings/brave_cookie_settings_factory.cc b/browser/content_settings/brave_cookie_settings_factory.cc index e41a4c9e2090..6cb9979be9ad 100644 --- a/browser/content_settings/brave_cookie_settings_factory.cc +++ b/browser/content_settings/brave_cookie_settings_factory.cc @@ -12,5 +12,5 @@ scoped_refptr BraveCookieSettingsFactory::GetForProfile(Profile* profile) { return static_cast( - CookieSettingsFactory::GetForProfile(profile).get()); -} \ No newline at end of file + CookieSettingsFactory::GetForProfile(profile).get()); +} diff --git a/browser/content_settings/brave_cookie_settings_factory.h b/browser/content_settings/brave_cookie_settings_factory.h index 2bdda3a22fc1..8cdef86252cf 100644 --- a/browser/content_settings/brave_cookie_settings_factory.h +++ b/browser/content_settings/brave_cookie_settings_factory.h @@ -3,8 +3,8 @@ * 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_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_COOKIE_SETTINGS_FACTORY_H_ -#define BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_COOKIE_SETTINGS_FACTORY_H_ +#ifndef BRAVE_BROWSER_CONTENT_SETTINGS_BRAVE_COOKIE_SETTINGS_FACTORY_H_ +#define BRAVE_BROWSER_CONTENT_SETTINGS_BRAVE_COOKIE_SETTINGS_FACTORY_H_ #include @@ -16,8 +16,7 @@ class BraveCookieSettings; class Profile; -class BraveCookieSettingsFactory - : public CookieSettingsFactory { +class BraveCookieSettingsFactory : public CookieSettingsFactory { public: static scoped_refptr GetForProfile( Profile* profile); @@ -31,4 +30,4 @@ class BraveCookieSettingsFactory DISALLOW_COPY_AND_ASSIGN(BraveCookieSettingsFactory); }; -#endif // BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_COOKIE_SETTINGS_FACTORY_H_ \ No newline at end of file +#endif // BRAVE_BROWSER_CONTENT_SETTINGS_BRAVE_COOKIE_SETTINGS_FACTORY_H_ diff --git a/browser/renderer_host/BUILD.gn b/browser/renderer_host/BUILD.gn index dc16f62df398..37231a49c3c5 100644 --- a/browser/renderer_host/BUILD.gn +++ b/browser/renderer_host/BUILD.gn @@ -1,15 +1,15 @@ import("//brave/components/brave_shields/browser/buildflags/buildflags.gni") source_set("renderer_host") { - public_deps = [ - "//brave/components/brave_shields/browser/buildflags", - ] - sources = [ "brave_navigation_ui_data.cc", "brave_navigation_ui_data.h", ] + public_deps = [ + "//brave/components/brave_shields/browser/buildflags", + ] + if (brave_stp_enabled) { sources += [ "brave_render_message_filter.cc", diff --git a/browser/renderer_host/brave_render_message_filter.cc b/browser/renderer_host/brave_render_message_filter.cc index c6457b02cf33..9f04942ca7f5 100644 --- a/browser/renderer_host/brave_render_message_filter.cc +++ b/browser/renderer_host/brave_render_message_filter.cc @@ -1,4 +1,7 @@ -// Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2019 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/renderer_host/brave_render_message_filter.h" @@ -16,11 +19,10 @@ using brave_shields::BraveShieldsWebContentsObserver; using content_settings::CookieSettings; BraveRenderMessageFilter::BraveRenderMessageFilter(int render_process_id, - Profile* profile) - : ChromeRenderMessageFilter(render_process_id, profile), - host_content_settings_map_(HostContentSettingsMapFactory::GetForProfile( - profile)) { -} + Profile* profile) + : ChromeRenderMessageFilter(render_process_id, profile), + host_content_settings_map_( + HostContentSettingsMapFactory::GetForProfile(profile)) {} BraveRenderMessageFilter::~BraveRenderMessageFilter() {} @@ -44,9 +46,11 @@ void BraveRenderMessageFilter::ShouldStoreState(int render_frame_id, BraveShieldsWebContentsObserver::GetTabURLFromRenderFrameInfo( render_process_id_, render_frame_id, -1) .GetOrigin(); - *allowed = g_brave_browser_process->tracking_protection_service()->ShouldStoreState( - cookie_settings_.get(), host_content_settings_map_, render_process_id_, render_frame_id, - origin_url, top_origin_url, tab_origin); + *allowed = + g_brave_browser_process->tracking_protection_service()->ShouldStoreState( + cookie_settings_.get(), host_content_settings_map_, + render_process_id_, render_frame_id, origin_url, top_origin_url, + tab_origin); } void BraveRenderMessageFilter::OnAllowDatabase(int render_frame_id, @@ -55,10 +59,8 @@ void BraveRenderMessageFilter::OnAllowDatabase(int render_frame_id, bool* allowed) { ShouldStoreState(render_frame_id, origin_url, top_origin_url, allowed); if (*allowed) { - ChromeRenderMessageFilter::OnAllowDatabase(render_frame_id, - origin_url, - top_origin_url, - allowed); + ChromeRenderMessageFilter::OnAllowDatabase(render_frame_id, origin_url, + top_origin_url, allowed); } } @@ -69,11 +71,8 @@ void BraveRenderMessageFilter::OnAllowDOMStorage(int render_frame_id, bool* allowed) { ShouldStoreState(render_frame_id, origin_url, top_origin_url, allowed); if (*allowed) { - ChromeRenderMessageFilter::OnAllowDOMStorage(render_frame_id, - origin_url, - top_origin_url, - local, - allowed); + ChromeRenderMessageFilter::OnAllowDOMStorage( + render_frame_id, origin_url, top_origin_url, local, allowed); } } @@ -83,9 +82,7 @@ void BraveRenderMessageFilter::OnAllowIndexedDB(int render_frame_id, bool* allowed) { ShouldStoreState(render_frame_id, origin_url, top_origin_url, allowed); if (*allowed) { - ChromeRenderMessageFilter::OnAllowIndexedDB(render_frame_id, - origin_url, - top_origin_url, - allowed); + ChromeRenderMessageFilter::OnAllowIndexedDB(render_frame_id, origin_url, + top_origin_url, allowed); } } diff --git a/browser/renderer_host/brave_render_message_filter.h b/browser/renderer_host/brave_render_message_filter.h index ba38926bdfba..f1f34f2d6993 100644 --- a/browser/renderer_host/brave_render_message_filter.h +++ b/browser/renderer_host/brave_render_message_filter.h @@ -1,4 +1,7 @@ -// Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2019 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_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_ #define BRAVE_BROWSER_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_ diff --git a/chromium_src/chrome/browser/chrome_content_browser_client.cc b/chromium_src/chrome/browser/chrome_content_browser_client.cc index 867d0618e16d..8cc6efca11be 100644 --- a/chromium_src/chrome/browser/chrome_content_browser_client.cc +++ b/chromium_src/chrome/browser/chrome_content_browser_client.cc @@ -3,8 +3,8 @@ * 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 "build/build_config.h" // For OS_MACOSX #include "brave/components/brave_shields/browser/buildflags/buildflags.h" // For STP +#include "build/build_config.h" // For OS_MACOSX #include "chrome/browser/search/search.h" #include "content/public/browser/browser_url_handler.h" @@ -22,18 +22,17 @@ #if defined(OS_LINUX) #include "brave/browser/ui/views/brave_browser_main_extra_parts_views_linux.h" -#define ChromeBrowserMainExtraPartsViewsLinux - BraveBrowserMainExtraPartsViewsLinux +#define ChromeBrowserMainExtraPartsViewsLinux BraveBrowserMainExtraPartsViewsLinux // NOLINT #endif #define HandleNewTabURLRewrite HandleNewTabURLRewrite_ChromiumImpl #define HandleNewTabURLReverseRewrite HandleNewTabURLReverseRewrite_ChromiumImpl namespace search { - bool HandleNewTabURLRewrite(GURL* url, content::BrowserContext* bc) { + bool HandleNewTabURLRewrite(GURL * url, content::BrowserContext * bc) { return false; } - bool HandleNewTabURLReverseRewrite(GURL* url, content::BrowserContext* bc) { + bool HandleNewTabURLReverseRewrite(GURL * url, content::BrowserContext * bc) { return false; } } // namespace search diff --git a/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.cc b/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.cc index 6544917e86a8..2836cf4f5e5d 100644 --- a/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.cc +++ b/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.cc @@ -7,6 +7,5 @@ #include "brave/browser/content_settings/brave_cookie_settings_factory.h" #define CookieSettingsFactory BraveCookieSettingsFactory -#include "../../../../../../chrome/browser/renderer_host/chrome_render_message_filter.cc" +#include "../../../../../../chrome/browser/renderer_host/chrome_render_message_filter.cc" // NOLINT #undef CookieSettingsFactory - diff --git a/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.h b/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.h index feea934d0a86..3f7cc47ad9a9 100644 --- a/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.h +++ b/chromium_src/chrome/browser/renderer_host/chrome_render_message_filter.h @@ -3,9 +3,13 @@ * 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_CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_MESSAGE_FILTER_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_MESSAGE_FILTER_H_ + #include "brave/components/content_settings/core/browser/brave_cookie_settings.h" #define CookieSettings BraveCookieSettings #include "../../../../../../chrome/browser/renderer_host/chrome_render_message_filter.h" #undef CookieSettings +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_MESSAGE_FILTER_H_ diff --git a/common/brave_switches.cc b/common/brave_switches.cc index 7904fd39a61e..0a1d2eec967a 100644 --- a/common/brave_switches.cc +++ b/common/brave_switches.cc @@ -20,14 +20,14 @@ const char kDisableBraveUpdate[] = "disable-brave-update"; // This switch disables the ChromeGoogleURLTrackerClient const char kDisableChromeGoogleURLTrackingClient[] = - "disable-chrome-google-url-tracking-client"; + "disable-chrome-google-url-tracking-client"; // Allows disabling the PDFJS extension. const char kDisablePDFJSExtension[] = "disable-pdfjs-extension"; // Allows disabling the Tor client updater extension. const char kDisableTorClientUpdaterExtension[] = - "disable-tor-client-updater-extension"; + "disable-tor-client-updater-extension"; // Allows disabling the WebTorrent extension. const char kDisableWebTorrentExtension[] = "disable-webtorrent-extension"; diff --git a/components/brave_shields/browser/BUILD.gn b/components/brave_shields/browser/BUILD.gn index 095ec7e12f60..9874562238ca 100644 --- a/components/brave_shields/browser/BUILD.gn +++ b/components/brave_shields/browser/BUILD.gn @@ -63,4 +63,4 @@ source_set("brave_shields") { "//chrome/common", "//third_party/leveldatabase", ] -} +} \ No newline at end of file diff --git a/components/brave_shields/browser/brave_shields_util.cc b/components/brave_shields/browser/brave_shields_util.cc index b87608ad1d54..0c046093ebe2 100644 --- a/components/brave_shields/browser/brave_shields_util.cc +++ b/components/brave_shields/browser/brave_shields_util.cc @@ -19,17 +19,17 @@ #include "components/content_settings/core/browser/host_content_settings_map.h" #include "components/content_settings/core/common/content_settings_types.h" #include "components/content_settings/core/common/content_settings_utils.h" -#include "content/public/common/referrer.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/resource_request_info.h" #include "content/public/browser/websocket_handshake_request_info.h" +#include "content/public/common/referrer.h" #include "extensions/browser/extension_api_frame_id_map.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "url/gurl.h" -using content::ResourceContext; using content::BrowserThread; using content::Referrer; +using content::ResourceContext; using content::ResourceRequestInfo; using net::URLRequest; @@ -38,7 +38,8 @@ namespace brave_shields { namespace { bool GetDefaultFromResourceIdentifier(const std::string& resource_identifier, - const GURL& primary_url, const GURL& secondary_url) { + const GURL& primary_url, + const GURL& secondary_url) { if (resource_identifier == brave_shields::kAds) { return false; } else if (resource_identifier == brave_shields::kTrackers) { @@ -64,12 +65,10 @@ bool IsAllowContentSetting(HostContentSettingsMap* content_settings, const std::string& resource_identifier) { DCHECK(content_settings); content_settings::SettingInfo setting_info; - std::unique_ptr value = - content_settings->GetWebsiteSetting( - primary_url, secondary_url, setting_type, resource_identifier, - &setting_info); - ContentSetting setting = - content_settings::ValueToContentSetting(value.get()); + std::unique_ptr value = content_settings->GetWebsiteSetting( + primary_url, secondary_url, setting_type, resource_identifier, + &setting_info); + ContentSetting setting = content_settings::ValueToContentSetting(value.get()); // TODO(bbondy): Add a static RegisterUserPrefs method for shields and use // prefs instead of simply returning true / false below. @@ -81,9 +80,10 @@ bool IsAllowContentSetting(HostContentSettingsMap* content_settings, } bool IsAllowContentSettingFromIO(const net::URLRequest* request, - const GURL& primary_url, const GURL& secondary_url, - ContentSettingsType setting_type, - const std::string& resource_identifier) { + const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType setting_type, + const std::string& resource_identifier) { DCHECK_CURRENTLY_ON(BrowserThread::IO); content::ResourceRequestInfo* resource_info = @@ -94,8 +94,8 @@ bool IsAllowContentSettingFromIO(const net::URLRequest* request, } ProfileIOData* io_data = ProfileIOData::FromResourceContext(resource_info->GetContext()); - return IsAllowContentSettingWithIOData(io_data, primary_url, - secondary_url, setting_type, resource_identifier); + return IsAllowContentSettingWithIOData(io_data, primary_url, secondary_url, + setting_type, resource_identifier); } bool IsAllowContentSettingsForProfile(Profile* profile, @@ -106,32 +106,28 @@ bool IsAllowContentSettingsForProfile(Profile* profile, DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK(profile); return IsAllowContentSetting( - HostContentSettingsMapFactory::GetForProfile(profile), - primary_url, - secondary_url, - setting_type, - resource_identifier); + HostContentSettingsMapFactory::GetForProfile(profile), primary_url, + secondary_url, setting_type, resource_identifier); } bool IsAllowContentSettingWithIOData(ProfileIOData* io_data, - const GURL& primary_url, const GURL& secondary_url, - ContentSettingsType setting_type, - const std::string& resource_identifier) { + const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType setting_type, + const std::string& resource_identifier) { if (!io_data) { return GetDefaultFromResourceIdentifier(resource_identifier, primary_url, secondary_url); } return IsAllowContentSetting(io_data->GetHostContentSettingsMap(), - primary_url, - secondary_url, - setting_type, + primary_url, secondary_url, setting_type, resource_identifier); } void GetRenderFrameInfo(const URLRequest* request, - int* render_frame_id, - int* render_process_id, - int* frame_tree_node_id) { + int* render_frame_id, + int* render_process_id, + int* frame_tree_node_id) { DCHECK_CURRENTLY_ON(BrowserThread::IO); *render_frame_id = -1; *render_process_id = -1; @@ -145,7 +141,7 @@ void GetRenderFrameInfo(const URLRequest* request, if (!content::ResourceRequestInfo::GetRenderFrameForRequest( request, render_process_id, render_frame_id)) { const content::WebSocketHandshakeRequestInfo* websocket_info = - content::WebSocketHandshakeRequestInfo::ForRequest(request); + content::WebSocketHandshakeRequestInfo::ForRequest(request); if (websocket_info) { *render_frame_id = websocket_info->GetRenderFrameId(); *render_process_id = websocket_info->GetChildId(); @@ -153,23 +149,28 @@ void GetRenderFrameInfo(const URLRequest* request, } } -void DispatchBlockedEventFromIO(const GURL &request_url, int render_frame_id, - int render_process_id, int frame_tree_node_id, - const std::string& block_type) { +void DispatchBlockedEventFromIO(const GURL& request_url, + int render_frame_id, + int render_process_id, + int frame_tree_node_id, + const std::string& block_type) { DCHECK_CURRENTLY_ON(BrowserThread::IO); - base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, + base::PostTaskWithTraits( + FROM_HERE, {BrowserThread::UI}, base::BindOnce(&BraveShieldsWebContentsObserver::DispatchBlockedEvent, - block_type, request_url.spec(), - render_process_id, render_frame_id, frame_tree_node_id)); + block_type, request_url.spec(), render_process_id, + render_frame_id, frame_tree_node_id)); } -bool ShouldSetReferrer(bool allow_referrers, bool shields_up, - const GURL& original_referrer, const GURL& tab_origin, - const GURL& target_url, const GURL& new_referrer_url, - network::mojom::ReferrerPolicy policy, Referrer *output_referrer) { - if (!output_referrer || - allow_referrers || - !shields_up || +bool ShouldSetReferrer(bool allow_referrers, + bool shields_up, + const GURL& original_referrer, + const GURL& tab_origin, + const GURL& target_url, + const GURL& new_referrer_url, + network::mojom::ReferrerPolicy policy, + Referrer* output_referrer) { + if (!output_referrer || allow_referrers || !shields_up || original_referrer.is_empty() || // Same TLD+1 whouldn't set the referrer SameDomainOrHost( @@ -181,8 +182,8 @@ bool ShouldSetReferrer(bool allow_referrers, bool shields_up, tab_origin, target_url.GetOrigin()))) { return false; } - *output_referrer = Referrer::SanitizeForRequest(target_url, - Referrer(new_referrer_url, policy)); + *output_referrer = Referrer::SanitizeForRequest( + target_url, Referrer(new_referrer_url, policy)); return true; } diff --git a/components/brave_shields/browser/brave_shields_util.h b/components/brave_shields/browser/brave_shields_util.h index 344e041bca55..c39f9560c7f9 100644 --- a/components/brave_shields/browser/brave_shields_util.h +++ b/components/brave_shields/browser/brave_shields_util.h @@ -34,9 +34,10 @@ bool IsAllowContentSetting(HostContentSettingsMap* content_settings, const std::string& resource_identifier); bool IsAllowContentSettingWithIOData(ProfileIOData* io_data, - const GURL& primary_url, const GURL& secondary_url, - ContentSettingsType setting_type, - const std::string& resource_identifier); + const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType setting_type, + const std::string& resource_identifier); bool IsAllowContentSettingsForProfile(Profile* profile, const GURL& primary_url, @@ -45,23 +46,30 @@ bool IsAllowContentSettingsForProfile(Profile* profile, const std::string& resource_identifier); bool IsAllowContentSettingFromIO(const net::URLRequest* request, - const GURL& primary_url, const GURL& secondary_url, - ContentSettingsType setting_type, - const std::string& resource_identifier); + const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType setting_type, + const std::string& resource_identifier); -void DispatchBlockedEventFromIO(const GURL &request_url, int render_frame_id, - int render_process_id, int frame_tree_node_id, - const std::string& block_type); +void DispatchBlockedEventFromIO(const GURL& request_url, + int render_frame_id, + int render_process_id, + int frame_tree_node_id, + const std::string& block_type); void GetRenderFrameInfo(const net::URLRequest* request, - int* render_frame_id, - int* render_process_id, - int* frame_tree_node_id); - -bool ShouldSetReferrer(bool allow_referrers, bool shields_up, - const GURL& original_referrer, const GURL& tab_origin, - const GURL& target_url, const GURL& new_referrer_url, - network::mojom::ReferrerPolicy policy, content::Referrer *output_referrer); + int* render_frame_id, + int* render_process_id, + int* frame_tree_node_id); + +bool ShouldSetReferrer(bool allow_referrers, + bool shields_up, + const GURL& original_referrer, + const GURL& tab_origin, + const GURL& target_url, + const GURL& new_referrer_url, + network::mojom::ReferrerPolicy policy, + content::Referrer* output_referrer); } // namespace brave_shields diff --git a/components/brave_shields/browser/tracking_protection_helper.cc b/components/brave_shields/browser/tracking_protection_helper.cc index f4dc1cd30af8..9581ef1e7582 100644 --- a/components/brave_shields/browser/tracking_protection_helper.cc +++ b/components/brave_shields/browser/tracking_protection_helper.cc @@ -59,11 +59,9 @@ bool TrackingProtectionHelper::IsSmartTrackingProtectionEnabled() { } TrackingProtectionHelper::TrackingProtectionHelper(WebContents* web_contents) - : WebContentsObserver(web_contents) { -} + : WebContentsObserver(web_contents) {} -TrackingProtectionHelper::~TrackingProtectionHelper() { -} +TrackingProtectionHelper::~TrackingProtectionHelper() {} void TrackingProtectionHelper::ReadyToCommitNavigation( NavigationHandle* handle) { @@ -91,7 +89,8 @@ void TrackingProtectionHelper::RenderFrameDeleted( } void TrackingProtectionHelper::RenderFrameHostChanged( - RenderFrameHost* old_host, RenderFrameHost* new_host) { + RenderFrameHost* old_host, + RenderFrameHost* new_host) { if (!old_host || old_host->GetParent() || new_host->GetParent()) { return; } diff --git a/components/brave_shields/browser/tracking_protection_helper.h b/components/brave_shields/browser/tracking_protection_helper.h index e6df24fb4a2a..2756a852a633 100644 --- a/components/brave_shields/browser/tracking_protection_helper.h +++ b/components/brave_shields/browser/tracking_protection_helper.h @@ -7,20 +7,21 @@ #define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_TRACKING_PROTECTION_HELPER_H_ #include "base/macros.h" -#include "base/synchronization/lock.h" #include "base/strings/string16.h" +#include "base/synchronization/lock.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" namespace content { class WebContents; class NavigationHandle; -} +} // namespace content namespace brave_shields { -class TrackingProtectionHelper : public content::WebContentsObserver, - public content::WebContentsUserData { +class TrackingProtectionHelper + : public content::WebContentsObserver, + public content::WebContentsUserData { public: explicit TrackingProtectionHelper(content::WebContents*); ~TrackingProtectionHelper() override; @@ -32,9 +33,10 @@ class TrackingProtectionHelper : public content::WebContentsObserver, static bool IsSmartTrackingProtectionEnabled(); WEB_CONTENTS_USER_DATA_KEY_DECL(); - DISALLOW_COPY_AND_ASSIGN(TrackingProtectionHelper); private: friend class content::WebContentsUserData; + + DISALLOW_COPY_AND_ASSIGN(TrackingProtectionHelper); }; } // namespace brave_shields diff --git a/components/brave_shields/browser/tracking_protection_service.cc b/components/brave_shields/browser/tracking_protection_service.cc index 04e364f5b4ca..7006da0986d0 100644 --- a/components/brave_shields/browser/tracking_protection_service.cc +++ b/components/brave_shields/browser/tracking_protection_service.cc @@ -49,8 +49,7 @@ const char kStorageTrackersFile[] = "StorageTrackingProtection.dat"; const int kThirdPartyHostsCacheSize = 20; TrackingProtectionService::TrackingProtectionService() - : tracking_protection_client_(new CTPParser()), - weak_factory_(this) { + : tracking_protection_client_(new CTPParser()), weak_factory_(this) { DETACH_FROM_SEQUENCE(sequence_checker_); } @@ -108,8 +107,7 @@ void TrackingProtectionService::ModifyRenderFrameKey(int old_render_process_id, int new_render_frame_id) { DCHECK_CURRENTLY_ON(BrowserThread::IO); const RenderFrameIdKey old_key(old_render_process_id, old_render_frame_id); - auto iter = - render_frame_key_to_starting_site_url.find(old_key); + auto iter = render_frame_key_to_starting_site_url.find(old_key); if (iter != render_frame_key_to_starting_site_url.end()) { const RenderFrameIdKey new_key(new_render_process_id, new_render_frame_id); render_frame_key_to_starting_site_url.insert( @@ -156,6 +154,7 @@ bool TrackingProtectionService::ShouldStoreState(HostContentSettingsMap* map, : IsAllowContentSetting(map, starting_site, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kBraveShields); + if (!allow_brave_shields) { return true; } @@ -166,13 +165,14 @@ bool TrackingProtectionService::ShouldStoreState(HostContentSettingsMap* map, : IsAllowContentSetting(map, starting_site, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kTrackers); + if (allow_trackers) { return true; } // deny storage if host is found in the tracker list return first_party_storage_trackers_.find(host) == - first_party_storage_trackers_.end(); + first_party_storage_trackers_.end(); } void TrackingProtectionService::ParseStorageTrackersData() { @@ -182,17 +182,17 @@ void TrackingProtectionService::ParseStorageTrackersData() { } std::string trackers(storage_trackers_buffer_.begin(), - storage_trackers_buffer_.end()); - std::vector storage_trackers = base::SplitString( - base::StringPiece(trackers.data(), trackers.size()), ",", - base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); + storage_trackers_buffer_.end()); + std::vector storage_trackers = + base::SplitString(base::StringPiece(trackers.data(), trackers.size()), + ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); if (storage_trackers.empty()) { LOG(ERROR) << "No first party trackers found"; return; } - first_party_storage_trackers_ = base::flat_set( - std::move(storage_trackers)); + first_party_storage_trackers_ = + base::flat_set(std::move(storage_trackers)); } #endif @@ -204,7 +204,8 @@ bool TrackingProtectionService::ShouldStoreState(BraveCookieSettings* settings, const GURL& first_party_url, const GURL& tab_url) const { #if BUILDFLAG(BRAVE_STP_ENABLED) - const bool allow = ShouldStoreState(map, render_process_id, render_frame_id, url, first_party_url); + const bool allow = ShouldStoreState(map, render_process_id, render_frame_id, + url, first_party_url); if (!allow) { return allow; } @@ -216,7 +217,7 @@ bool TrackingProtectionService::ShouldStoreState(BraveCookieSettings* settings, bool TrackingProtectionService::ShouldStartRequest( const GURL& url, content::ResourceType resource_type, - const std::string &tab_host, + const std::string& tab_host, bool* matching_exception_filter, bool* cancel_request_explicitly) { // There are no exceptions in the TP service, but exceptions are @@ -227,22 +228,22 @@ bool TrackingProtectionService::ShouldStartRequest( // Intentionally don't set cancel_request_explicitly DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); std::string host = url.host(); - if (!tracking_protection_client_->matchesTracker( - tab_host.c_str(), host.c_str())) { + if (!tracking_protection_client_->matchesTracker(tab_host.c_str(), + host.c_str())) { return true; } std::vector hosts(GetThirdPartyHosts(tab_host)); for (size_t i = 0; i < hosts.size(); i++) { if (host == hosts[i] || - host.find((std::string)"." + hosts[i]) != std::string::npos) { + host.find((std::string) "." + hosts[i]) != std::string::npos) { return true; } - size_t iPos = host.find((std::string)"." + hosts[i]); + size_t iPos = host.find((std::string) "." + hosts[i]); if (iPos == std::string::npos) { continue; } - if (hosts[i].length() + ((std::string)".").length() + iPos == + if (hosts[i].length() + ((std::string) ".").length() + iPos == host.length()) { return true; } @@ -257,7 +258,7 @@ void TrackingProtectionService::OnDATFileDataReady() { } tracking_protection_client_.reset(new CTPParser()); if (!tracking_protection_client_->deserialize( - reinterpret_cast(&buffer_.front()))) { + reinterpret_cast(&buffer_.front()))) { tracking_protection_client_.reset(); LOG(ERROR) << "Failed to deserialize tracking protection data"; return; @@ -269,13 +270,13 @@ void TrackingProtectionService::OnComponentReady( const base::FilePath& install_dir, const std::string& manifest) { base::FilePath navigation_tracking_protection_path = - install_dir.AppendASCII(kDatFileVersion).AppendASCII( - kNavigationTrackersFile); + install_dir.AppendASCII(kDatFileVersion) + .AppendASCII(kNavigationTrackersFile); GetTaskRunner()->PostTaskAndReply( FROM_HERE, base::Bind(&GetDATFileData, navigation_tracking_protection_path, - &buffer_), + &buffer_), base::Bind(&TrackingProtectionService::OnDATFileDataReady, weak_factory_.GetWeakPtr())); @@ -284,29 +285,29 @@ void TrackingProtectionService::OnComponentReady( return; } base::FilePath storage_tracking_protection_path = - install_dir.AppendASCII(kDatFileVersion).AppendASCII( - kStorageTrackersFile); + install_dir.AppendASCII(kDatFileVersion) + .AppendASCII(kStorageTrackersFile); GetTaskRunner()->PostTaskAndReply( FROM_HERE, base::Bind(&GetDATFileData, storage_tracking_protection_path, - &storage_trackers_buffer_), - base::Bind(&TrackingProtectionService::ParseStorageTrackersData, - weak_factory_.GetWeakPtr())); + &storage_trackers_buffer_), + base::Bind(&TrackingProtectionService::ParseStorageTrackersData, + weak_factory_.GetWeakPtr())); #endif } // Ported from Android: net/blockers/blockers_worker.cc -std::vector -TrackingProtectionService::GetThirdPartyHosts(const std::string& base_host) { +std::vector TrackingProtectionService::GetThirdPartyHosts( + const std::string& base_host) { { - std::lock_guard guard(third_party_hosts_mutex_); + base::AutoLock guard(third_party_hosts_lock_); std::map>::const_iterator iter = - third_party_hosts_cache_.find(base_host); + third_party_hosts_cache_.find(base_host); if (third_party_hosts_cache_.end() != iter) { - if (third_party_base_hosts_.size() != 0 - && third_party_base_hosts_[third_party_hosts_cache_.size() - 1] != - base_host) { + if (third_party_base_hosts_.size() != 0 && + third_party_base_hosts_[third_party_hosts_cache_.size() - 1] != + base_host) { for (size_t i = 0; i < third_party_base_hosts_.size(); i++) { if (third_party_base_hosts_[i] == base_host) { third_party_base_hosts_.erase(third_party_base_hosts_.begin() + i); @@ -319,10 +320,10 @@ TrackingProtectionService::GetThirdPartyHosts(const std::string& base_host) { } } - char* thirdPartyHosts = - tracking_protection_client_->findFirstPartyHosts(base_host.c_str()); + std::string thirdPartyHosts = + tracking_protection_client_->findFirstPartyHosts(base_host.c_str()); std::vector hosts; - if (nullptr != thirdPartyHosts) { + if (!thirdPartyHosts.empty()) { std::string strThirdPartyHosts = thirdPartyHosts; size_t iPos = strThirdPartyHosts.find(","); while (iPos != std::string::npos) { @@ -334,11 +335,10 @@ TrackingProtectionService::GetThirdPartyHosts(const std::string& base_host) { if (0 != strThirdPartyHosts.length()) { hosts.push_back(strThirdPartyHosts); } - delete []thirdPartyHosts; } { - std::lock_guard guard(third_party_hosts_mutex_); + base::AutoLock guard(third_party_hosts_lock_); if (third_party_hosts_cache_.size() == kThirdPartyHostsCacheSize && third_party_base_hosts_.size() == kThirdPartyHostsCacheSize) { third_party_hosts_cache_.erase(third_party_base_hosts_[0]); @@ -364,7 +364,7 @@ TrackingProtectionService::GetTaskRunner() { // is the job of the browser process. std::unique_ptr TrackingProtectionServiceFactory() { std::unique_ptr service = - std::make_unique(); + std::make_unique(); g_brave_browser_process->local_data_files_service()->AddObserver( service.get()); return service; diff --git a/components/brave_shields/browser/tracking_protection_service.h b/components/brave_shields/browser/tracking_protection_service.h index 67a5e7c9bd9c..fde162479211 100644 --- a/components/brave_shields/browser/tracking_protection_service.h +++ b/components/brave_shields/browser/tracking_protection_service.h @@ -20,6 +20,7 @@ #include "base/memory/weak_ptr.h" #include "base/sequence_checker.h" #include "base/sequenced_task_runner.h" +#include "base/synchronization/lock.h" #include "brave/components/brave_shields/browser/base_local_data_files_observer.h" #include "brave/components/brave_shields/browser/buildflags/buildflags.h" // For STP #include "brave/components/brave_shields/browser/dat_file_util.h" @@ -31,7 +32,7 @@ class HostContentSettingsMap; class TrackingProtectionServiceTest; namespace content_settings { - class BraveCookieSettings; +class BraveCookieSettings; } namespace brave_shields { @@ -73,10 +74,12 @@ class TrackingProtectionService : public BaseLocalDataFilesObserver { const GURL& top_origin_url, const GURL& origin_url) const; - void SetStartingSiteForRenderFrame(GURL starting_site, int render_process_id, - int render_frame_id); + void SetStartingSiteForRenderFrame(GURL starting_site, + int render_process_id, + int render_frame_id); GURL GetStartingSiteForRenderFrame(int render_process_id, int render_frame_id) const; + void DeleteRenderFrameKey(int render_process_id, int render_frame_id); void ModifyRenderFrameKey(int old_render_process_id, int old_render_frame_id, @@ -120,7 +123,7 @@ class TrackingProtectionService : public BaseLocalDataFilesObserver { std::unique_ptr tracking_protection_client_; std::vector third_party_base_hosts_; std::map> third_party_hosts_cache_; - std::mutex third_party_hosts_mutex_; + base::Lock third_party_hosts_lock_; SEQUENCE_CHECKER(sequence_checker_); base::WeakPtrFactory weak_factory_; diff --git a/components/brave_shields/browser/tracking_protection_service_browsertest.cc b/components/brave_shields/browser/tracking_protection_service_browsertest.cc index c6db196c925f..d9bee6abff3e 100644 --- a/components/brave_shields/browser/tracking_protection_service_browsertest.cc +++ b/components/brave_shields/browser/tracking_protection_service_browsertest.cc @@ -1,4 +1,7 @@ -// Copyright (c) 2019 The Brave Authors. All rights reserved. +/* Copyright (c) 2019 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 "base/path_service.h" #include "base/strings/stringprintf.h" @@ -10,9 +13,9 @@ #include "brave/components/brave_shields/browser/buildflags/buildflags.h" // For STP #include "brave/components/brave_shields/browser/local_data_files_service.h" #include "brave/components/brave_shields/browser/tracking_protection_service.h" -#include "chrome/browser/ui/browser.h" #include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/net/url_request_mock_util.h" +#include "chrome/browser/ui/browser.h" #include "chrome/test/base/ui_test_utils.h" #include "components/prefs/pref_service.h" #include "content/public/browser/browser_task_traits.h" @@ -73,7 +76,8 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest { void SetUpOnMainThread() override { ExtensionBrowserTest::SetUpOnMainThread(); - base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::IO}, + base::PostTaskWithTraits( + FROM_HERE, {content::BrowserThread::IO}, base::BindOnce(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); host_resolver()->AddRule("*", "127.0.0.1"); } @@ -81,8 +85,8 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest { void PreRunTestOnMainThread() override { ExtensionBrowserTest::PreRunTestOnMainThread(); WaitForTrackingProtectionServiceThread(); - ASSERT_TRUE(g_brave_browser_process->local_data_files_service()-> - IsInitialized()); + ASSERT_TRUE( + g_brave_browser_process->local_data_files_service()->IsInitialized()); } void InitEmbeddedTestServer() { @@ -111,7 +115,7 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest { GetTestDataDir(&test_data_dir); const extensions::Extension* tracking_protection_extension = InstallExtension(test_data_dir.AppendASCII("tracking-protection-data"), - 1); + 1); if (!tracking_protection_extension) return false; @@ -124,10 +128,9 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest { } void WaitForTrackingProtectionServiceThread() { - scoped_refptr io_helper( - new base::ThreadTestHelper( - g_brave_browser_process->tracking_protection_service()-> - GetTaskRunner())); + scoped_refptr io_helper(new base::ThreadTestHelper( + g_brave_browser_process->tracking_protection_service() + ->GetTaskRunner())); ASSERT_TRUE(io_helper->Run()); } }; @@ -135,16 +138,16 @@ class TrackingProtectionServiceTest : public ExtensionBrowserTest { // Load a page that references a tracker from a trusted domain, and // make sure it is not blocked. IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, - TrackerReferencedFromTrustedDomainNotBlocked) { + TrackerReferencedFromTrustedDomainNotBlocked) { ASSERT_TRUE(InstallTrackingProtectionExtension()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), - 0ULL); + 0ULL); GURL url = embedded_test_server()->GetURL("365media.com", kTrackingPage); ui_test_utils::NavigateToURL(browser(), url); - content::WebContents* contents = browser()->tab_strip_model()-> - GetActiveWebContents(); + content::WebContents* contents = + browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(content::WaitForLoadStop(contents)); EXPECT_EQ(url, contents->GetURL()); @@ -152,27 +155,26 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, bool img_loaded; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - base::StringPrintf(kTrackingScript, test_url.spec().c_str()), + contents, base::StringPrintf(kTrackingScript, test_url.spec().c_str()), &img_loaded)); EXPECT_TRUE(img_loaded); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), - 0ULL); + 0ULL); } // Load a page that references a tracker from an untrusted domain, and // make sure it is blocked. IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, - TrackerReferencedFromUntrustedDomainGetsBlocked) { + TrackerReferencedFromUntrustedDomainGetsBlocked) { ASSERT_TRUE(InstallTrackingProtectionExtension()); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), - 0ULL); + 0ULL); GURL url = embedded_test_server()->GetURL("google.com", kTrackingPage); ui_test_utils::NavigateToURL(browser(), url); - content::WebContents* contents = browser()->tab_strip_model()-> - GetActiveWebContents(); + content::WebContents* contents = + browser()->tab_strip_model()->GetActiveWebContents(); ASSERT_TRUE(content::WaitForLoadStop(contents)); EXPECT_EQ(url, contents->GetURL()); @@ -180,13 +182,12 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, bool img_loaded; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - base::StringPrintf(kTrackingScript, test_url.spec().c_str()), + contents, base::StringPrintf(kTrackingScript, test_url.spec().c_str()), &img_loaded)); EXPECT_FALSE(img_loaded); EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kTrackersBlocked), - 1ULL); + 1ULL); } #if BUILDFLAG(BRAVE_STP_ENABLED) @@ -198,21 +199,21 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, StorageTrackingBlocked) { content::DOMMessageQueue message_queue; // tracker.com is in the StorageTrackingProtection list - const GURL tracking_url = embedded_test_server()->GetURL("tracker.com", - kStoragePage); + const GURL tracking_url = + embedded_test_server()->GetURL("tracker.com", kStoragePage); - const GURL url = embedded_test_server()->GetURL("social.com", std::string(kRedirectPage) + - tracking_url.spec()); + const GURL url = embedded_test_server()->GetURL( + "social.com", std::string(kRedirectPage) + tracking_url.spec()); ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url, 2); + content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents(); EXPECT_EQ("tracker.com", contents->GetURL().host()); bool cookie_blocked; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(!IsCookieAvailable())", + contents, "window.domAutomationController.send(!IsCookieAvailable())", &cookie_blocked)); EXPECT_TRUE(cookie_blocked); @@ -234,15 +235,13 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, StorageTrackingBlocked) { bool db_blocked; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(!IsDBAvailable())", + contents, "window.domAutomationController.send(!IsDBAvailable())", &db_blocked)); EXPECT_TRUE(db_blocked); bool indexeddb_blocked; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(!IsIndexedDBAvailable())", + contents, "window.domAutomationController.send(!IsIndexedDBAvailable())", &indexeddb_blocked)); EXPECT_TRUE(indexeddb_blocked); } @@ -255,11 +254,11 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, StorageTrackingAllowed) { content::DOMMessageQueue message_queue; // example.com is not in the StorageTrackingProtection list - const GURL redirect_url = embedded_test_server()->GetURL("example.com", - kStoragePage); + const GURL redirect_url = + embedded_test_server()->GetURL("example.com", kStoragePage); - const GURL url = - embedded_test_server()->GetURL("social.com", kRedirectPage + redirect_url.spec()); + const GURL url = embedded_test_server()->GetURL( + "social.com", kRedirectPage + redirect_url.spec()); ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url, 2); content::WebContents* contents = @@ -268,8 +267,7 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, StorageTrackingAllowed) { bool cookie_allowed; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(IsCookieAvailable())", + contents, "window.domAutomationController.send(IsCookieAvailable())", &cookie_allowed)); EXPECT_TRUE(cookie_allowed); @@ -291,15 +289,13 @@ IN_PROC_BROWSER_TEST_F(TrackingProtectionServiceTest, StorageTrackingAllowed) { bool db_allowed; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(IsDBAvailable())", + contents, "window.domAutomationController.send(IsDBAvailable())", &db_allowed)); EXPECT_TRUE(db_allowed); bool indexeddb_allowed; ASSERT_TRUE(ExecuteScriptAndExtractBool( - contents, - "window.domAutomationController.send(IsIndexedDBAvailable())", + contents, "window.domAutomationController.send(IsIndexedDBAvailable())", &indexeddb_allowed)); } diff --git a/components/content_settings/core/browser/brave_cookie_settings.cc b/components/content_settings/core/browser/brave_cookie_settings.cc index 5bf46837ff80..2827d9722f6c 100644 --- a/components/content_settings/core/browser/brave_cookie_settings.cc +++ b/components/content_settings/core/browser/brave_cookie_settings.cc @@ -6,9 +6,10 @@ #include "brave/components/content_settings/core/browser/brave_cookie_settings.h" #include "base/bind.h" -#include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/common/brave_cookie_blocking.h" #include "brave/common/pref_names.h" +#include "brave/components/brave_shields/common/brave_shield_constants.h" +#include "components/content_settings/core/common/cookie_settings_base.h" #include "components/prefs/pref_service.h" #include "extensions/buildflags/buildflags.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" @@ -23,8 +24,7 @@ BraveCookieSettings::BraveCookieSettings( PrefService* prefs, const char* extension_scheme) : CookieSettings(host_content_settings_map, prefs, extension_scheme), - allow_google_auth_( - prefs->GetBoolean(kGoogleLoginControlType)) { + allow_google_auth_(prefs->GetBoolean(kGoogleLoginControlType)) { pref_change_registrar_.Init(prefs); pref_change_registrar_.Add( kGoogleLoginControlType, @@ -32,9 +32,10 @@ BraveCookieSettings::BraveCookieSettings( base::Unretained(this))); } -BraveCookieSettings::~BraveCookieSettings() { } +BraveCookieSettings::~BraveCookieSettings() {} -void BraveCookieSettings::GetCookieSetting(const GURL& url, +void BraveCookieSettings::GetCookieSetting( + const GURL& url, const GURL& first_party_url, content_settings::SettingSource* source, ContentSetting* cookie_setting) const { @@ -42,7 +43,8 @@ void BraveCookieSettings::GetCookieSetting(const GURL& url, cookie_setting); } -void BraveCookieSettings::GetCookieSetting(const GURL& url, +void BraveCookieSettings::GetCookieSetting( + const GURL& url, const GURL& first_party_url, const GURL& tab_url, content_settings::SettingSource* source, @@ -67,34 +69,35 @@ void BraveCookieSettings::GetCookieSetting(const GURL& url, #endif CookieSettings::GetCookieSetting(url, first_party_url, source, - cookie_setting); + cookie_setting); if (*cookie_setting == CONTENT_SETTING_BLOCK) { return; } - GURL primary_url = (tab_url == GURL("about:blank") || tab_url.is_empty() ? - first_party_url : tab_url); + GURL primary_url = + (tab_url == GURL("about:blank") || tab_url.is_empty() ? first_party_url + : tab_url); ContentSetting brave_shields_setting = host_content_settings_map_->GetContentSetting( - primary_url, GURL(), - CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kBraveShields); + primary_url, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kBraveShields); ContentSetting brave_1p_setting = - host_content_settings_map_->GetContentSetting(primary_url, - GURL("https://firstParty/"), CONTENT_SETTINGS_TYPE_PLUGINS, - brave_shields::kCookies); - ContentSetting brave_3p_setting = host_content_settings_map_->GetContentSetting( - primary_url, GURL(), + primary_url, GURL("https://firstParty/"), CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies); + ContentSetting brave_3p_setting = + host_content_settings_map_->GetContentSetting( + primary_url, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kCookies); bool allow_brave_shields = brave_shields_setting == CONTENT_SETTING_ALLOW || - brave_shields_setting == CONTENT_SETTING_DEFAULT; + brave_shields_setting == CONTENT_SETTING_DEFAULT; bool allow_1p_cookies = brave_1p_setting == CONTENT_SETTING_ALLOW || - brave_1p_setting == CONTENT_SETTING_DEFAULT; + brave_1p_setting == CONTENT_SETTING_DEFAULT; bool allow_3p_cookies = brave_3p_setting == CONTENT_SETTING_ALLOW; - if (ShouldBlockCookie(allow_brave_shields, allow_1p_cookies, - allow_3p_cookies, first_party_url, url, allow_google_auth_)) { + if (ShouldBlockCookie(allow_brave_shields, allow_1p_cookies, allow_3p_cookies, + first_party_url, url, allow_google_auth_)) { *cookie_setting = CONTENT_SETTING_BLOCK; } } @@ -115,8 +118,8 @@ bool BraveCookieSettings::IsCookieAccessAllowed(const GURL& url, void BraveCookieSettings::OnAllowGoogleAuthChanged() { DCHECK(thread_checker_.CalledOnValidThread()); base::AutoLock auto_lock(lock_); - allow_google_auth_ = pref_change_registrar_.prefs()->GetBoolean( - kGoogleLoginControlType); + allow_google_auth_ = + pref_change_registrar_.prefs()->GetBoolean(kGoogleLoginControlType); } } // namespace content_settings diff --git a/components/content_settings/core/browser/brave_cookie_settings.h b/components/content_settings/core/browser/brave_cookie_settings.h index 8edb980b96ac..a29116a87880 100644 --- a/components/content_settings/core/browser/brave_cookie_settings.h +++ b/components/content_settings/core/browser/brave_cookie_settings.h @@ -44,9 +44,7 @@ class BraveCookieSettings : public CookieSettings { const GURL& first_party_url, const GURL& tab_url) const; - bool GetAllowGoogleAuth() const { - return allow_google_auth_; - } + bool GetAllowGoogleAuth() const { return allow_google_auth_; } protected: ~BraveCookieSettings() override;