From 05776159b0e063aaeb5184472dffbacba199f3b7 Mon Sep 17 00:00:00 2001 From: Pranjal Date: Wed, 26 Sep 2018 18:19:31 -0700 Subject: [PATCH] 1. Moved logic for `ShouldStoreState` to `tracking_protection_service.cc`. 2. Invoking super OnAllow functions from BraveRenderMessageFilter. 3. Using chromium src redefine for BraveRenderMessageFilter --- .../brave_render_message_filter.cc | 65 +++++++------------ .../brave_render_message_filter.h | 7 +- .../browser/chrome_content_browser_client.cc | 5 ++ .../browser/tracking_protection_service.cc | 25 +++++-- .../browser/tracking_protection_service.h | 4 +- ...ser-chrome_content_browser_client.cc.patch | 21 ------ 6 files changed, 54 insertions(+), 73 deletions(-) delete mode 100644 patches/chrome-browser-chrome_content_browser_client.cc.patch diff --git a/browser/renderer_host/brave_render_message_filter.cc b/browser/renderer_host/brave_render_message_filter.cc index 9f72677bf15e..55e0c0ed10d8 100644 --- a/browser/renderer_host/brave_render_message_filter.cc +++ b/browser/renderer_host/brave_render_message_filter.cc @@ -6,13 +6,9 @@ #include "brave/browser/brave_browser_process_impl.h" #include "brave/components/brave_shields/browser/tracking_protection_service.h" -#include "brave/components/brave_shields/browser/brave_shields_util.h" -#include "brave/components/brave_shields/common/brave_shield_constants.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" -#include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/render_messages.h" -#include "components/content_settings/core/browser/cookie_settings.h" #include "content/public/browser/browser_thread.h" using content::BrowserThread; @@ -20,8 +16,7 @@ using content::BrowserThread; BraveRenderMessageFilter::BraveRenderMessageFilter(int render_process_id, Profile* profile) : ChromeRenderMessageFilter(render_process_id, profile), - host_content_settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), - weak_factory_(this) { + host_content_settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { } BraveRenderMessageFilter::~BraveRenderMessageFilter() {} @@ -39,36 +34,20 @@ bool BraveRenderMessageFilter::OnMessageReceived(const IPC::Message& message) { return ChromeRenderMessageFilter::OnMessageReceived(message); } -bool BraveRenderMessageFilter::ShouldStoreState(const GURL& origin_url, - const GURL& top_origin_url) { - CHECK(g_brave_browser_process->tracking_protection_service()->IsInitialized()); - - bool allow_brave_shields = brave_shields::IsAllowContentSetting( - host_content_settings_map_, top_origin_url, top_origin_url, CONTENT_SETTINGS_TYPE_PLUGINS, - brave_shields::kBraveShields); - - bool allow_trackers = brave_shields::IsAllowContentSetting( - host_content_settings_map_, top_origin_url, top_origin_url, CONTENT_SETTINGS_TYPE_PLUGINS, - brave_shields::kTrackers); - - return !(allow_brave_shields && !allow_trackers && - !g_brave_browser_process->tracking_protection_service()->ShouldStoreState(origin_url)) && - cookie_settings_->IsCookieAccessAllowed(origin_url, top_origin_url);; -} - void BraveRenderMessageFilter::OnAllowDatabase(int render_frame_id, const GURL& origin_url, const GURL& top_origin_url, const base::string16& name, const base::string16& display_name, bool* allowed) { - *allowed = ShouldStoreState(origin_url, top_origin_url); - - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - base::Bind(&TabSpecificContentSettings::WebDatabaseAccessed, - render_process_id_, render_frame_id, origin_url, name, - display_name, !*allowed)); + CHECK(g_brave_browser_process->tracking_protection_service()->IsInitialized()); + *allowed = g_brave_browser_process->tracking_protection_service()->ShouldStoreState(host_content_settings_map_, + origin_url, top_origin_url); + + if (*allowed) { + ChromeRenderMessageFilter::OnAllowDatabase(render_frame_id, origin_url, top_origin_url, + name, display_name, allowed); + } } void BraveRenderMessageFilter::OnAllowDOMStorage(int render_frame_id, @@ -76,13 +55,15 @@ void BraveRenderMessageFilter::OnAllowDOMStorage(int render_frame_id, const GURL& top_origin_url, bool local, bool* allowed) { - *allowed = ShouldStoreState(origin_url, top_origin_url); + CHECK(g_brave_browser_process->tracking_protection_service()->IsInitialized()); + *allowed = g_brave_browser_process->tracking_protection_service()->ShouldStoreState(host_content_settings_map_, + origin_url, top_origin_url); + + if (*allowed) { + ChromeRenderMessageFilter::OnAllowDOMStorage(render_frame_id, origin_url, top_origin_url, + local, allowed); + } - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - base::Bind(&TabSpecificContentSettings::DOMStorageAccessed, - render_process_id_, render_frame_id, origin_url, local, - !*allowed)); } void BraveRenderMessageFilter::OnAllowIndexedDB(int render_frame_id, @@ -90,11 +71,11 @@ void BraveRenderMessageFilter::OnAllowIndexedDB(int render_frame_id, const GURL& top_origin_url, const base::string16& name, bool* allowed) { - *allowed = ShouldStoreState(origin_url, top_origin_url); + CHECK(g_brave_browser_process->tracking_protection_service()->IsInitialized()); + *allowed = g_brave_browser_process->tracking_protection_service()->ShouldStoreState(host_content_settings_map_, + origin_url, top_origin_url); - BrowserThread::PostTask( - BrowserThread::UI, FROM_HERE, - base::Bind(&TabSpecificContentSettings::IndexedDBAccessed, - render_process_id_, render_frame_id, origin_url, name, - !*allowed)); + if (*allowed) { + ChromeRenderMessageFilter::OnAllowIndexedDB(render_frame_id, origin_url, top_origin_url, name, allowed); + } } \ No newline at end of file diff --git a/browser/renderer_host/brave_render_message_filter.h b/browser/renderer_host/brave_render_message_filter.h index c121515936c1..f2247202df62 100644 --- a/browser/renderer_host/brave_render_message_filter.h +++ b/browser/renderer_host/brave_render_message_filter.h @@ -6,11 +6,13 @@ #define BRAVE_BROWSER_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_ #include "chrome/browser/renderer_host/chrome_render_message_filter.h" -#include "components/content_settings/core/browser/host_content_settings_map.h" #include "content/public/browser/browser_message_filter.h" +class HostContentSettingsMap; + class BraveRenderMessageFilter : public ChromeRenderMessageFilter { public: + using ChromeRenderMessageFilter::ChromeRenderMessageFilter; BraveRenderMessageFilter(int render_process_id, Profile* profile); bool OnMessageReceived(const IPC::Message& message) override; @@ -38,10 +40,7 @@ class BraveRenderMessageFilter : public ChromeRenderMessageFilter { const base::string16& name, bool* allowed); - bool ShouldStoreState(const GURL& origin_url, const GURL& top_origin_url); - HostContentSettingsMap *host_content_settings_map_; - base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(BraveRenderMessageFilter); }; diff --git a/chromium_src/chrome/browser/chrome_content_browser_client.cc b/chromium_src/chrome/browser/chrome_content_browser_client.cc index 5f0806579749..08b34e214c8f 100644 --- a/chromium_src/chrome/browser/chrome_content_browser_client.cc +++ b/chromium_src/chrome/browser/chrome_content_browser_client.cc @@ -4,6 +4,11 @@ #include "build/build_config.h" // For OS_MACOSX +#include "brave/browser/renderer_host/brave_render_message_filter.h" +#include "../../../../chrome/browser/renderer_host/chrome_render_message_filter.h" +#undef ChromeRenderMessageFilter +#define ChromeRenderMessageFilter BraveRenderMessageFilter + #if defined(OS_MACOSX) #include "brave/browser/brave_browser_main_parts_mac.h" #undef ChromeBrowserMainPartsMac diff --git a/components/brave_shields/browser/tracking_protection_service.cc b/components/brave_shields/browser/tracking_protection_service.cc index 4b25fe607717..beaf9fe407aa 100644 --- a/components/brave_shields/browser/tracking_protection_service.cc +++ b/components/brave_shields/browser/tracking_protection_service.cc @@ -16,8 +16,11 @@ #include "base/memory/ptr_util.h" #include "base/strings/utf_string_conversions.h" #include "base/threading/thread_restrictions.h" +#include "brave/components/brave_shields/browser/brave_shields_util.h" #include "brave/components/brave_shields/browser/dat_file_util.h" +#include "brave/components/brave_shields/common/brave_shield_constants.h" #include "brave/vendor/tracking-protection/TPParser.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" #define NAVIGATION_TRACKERS_FILE "TrackingProtection.dat" #define STORAGE_TRACKERS_FILE "StorageTrackingProtection.dat" @@ -90,15 +93,27 @@ bool TrackingProtectionService::ShouldStartRequest(const GURL& url, return false; } -bool TrackingProtectionService::ShouldStoreState(const GURL& url) { +bool TrackingProtectionService::ShouldStoreState(HostContentSettingsMap* map, const GURL& top_origin_url, + const GURL& origin_url) { + if (!first_party_storage_trackers_initailized_) { LOG(ERROR) << "First party storage trackers not initialized"; return true; - } + } + std::string host = origin_url.host(); - std::string host = url.host(); - return !(std::find(first_party_storage_trackers_.begin(), first_party_storage_trackers_.end(), host) - != first_party_storage_trackers_.end()); + bool allow_brave_shields = IsAllowContentSetting( + map, top_origin_url, origin_url, CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kBraveShields); + + bool allow_trackers = IsAllowContentSetting( + map, top_origin_url, origin_url, CONTENT_SETTINGS_TYPE_PLUGINS, + brave_shields::kTrackers); + + bool denyStorage = std::find(first_party_storage_trackers_.begin(), first_party_storage_trackers_.end(), host) + != first_party_storage_trackers_.end(); + + return !(allow_brave_shields && !allow_trackers && denyStorage); } void TrackingProtectionService::ParseStorageTrackersData() { diff --git a/components/brave_shields/browser/tracking_protection_service.h b/components/brave_shields/browser/tracking_protection_service.h index 41fb42e71c5e..958220c72866 100644 --- a/components/brave_shields/browser/tracking_protection_service.h +++ b/components/brave_shields/browser/tracking_protection_service.h @@ -20,6 +20,7 @@ #include "content/public/common/resource_type.h" class CTPParser; +class HostContentSettingsMap; class TrackingProtectionServiceTest; namespace brave_shields { @@ -45,7 +46,8 @@ class TrackingProtectionService : public BaseBraveShieldsService { bool ShouldStartRequest(const GURL& spec, content::ResourceType resource_type, const std::string& tab_host) override; - bool ShouldStoreState(const GURL& url); + bool ShouldStoreState(HostContentSettingsMap* map, + const GURL& top_origin_url, const GURL& origin_url); protected: bool Init() override; diff --git a/patches/chrome-browser-chrome_content_browser_client.cc.patch b/patches/chrome-browser-chrome_content_browser_client.cc.patch deleted file mode 100644 index 7f179d7f327b..000000000000 --- a/patches/chrome-browser-chrome_content_browser_client.cc.patch +++ /dev/null @@ -1,21 +0,0 @@ -diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc -index a85e9d9c17ef8bfcc89f96376675932e656dab03..3bee18f4f26d4a8ac7eaa2cff6c79395aabbcba6 100644 ---- a/chrome/browser/chrome_content_browser_client.cc -+++ b/chrome/browser/chrome_content_browser_client.cc -@@ -29,6 +29,7 @@ - #include "base/strings/utf_string_conversions.h" - #include "base/sys_info.h" - #include "base/threading/thread_task_runner_handle.h" -+#include "brave/browser/renderer_host/brave_render_message_filter.h" - #include "build/build_config.h" - #include "chrome/browser/after_startup_task_utils.h" - #include "chrome/browser/browser_about_handler.h" -@@ -1262,7 +1263,7 @@ void ChromeContentBrowserClient::RenderProcessWillLaunch( - service_manager::mojom::ServiceRequest* service_request) { - int id = host->GetID(); - Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); -- host->AddFilter(new ChromeRenderMessageFilter(id, profile)); -+ host->AddFilter(new BraveRenderMessageFilter(id, profile)); - #if BUILDFLAG(ENABLE_EXTENSIONS) - host->AddFilter(new cast::CastTransportHostFilter(profile)); - #endif