diff --git a/brave/browser/BUILD.gn b/brave/browser/BUILD.gn index 87ce63aa12..ead7f6fd62 100644 --- a/brave/browser/BUILD.gn +++ b/brave/browser/BUILD.gn @@ -35,6 +35,8 @@ source_set("browser") { "password_manager/brave_password_manager_client.cc", "renderer_preferences_helper.h", "renderer_preferences_helper.cc", + "renderer_host/brave_render_message_filter.h", + "renderer_host/brave_render_message_filter.cc", ] public_deps = [ diff --git a/brave/browser/brave_content_browser_client.cc b/brave/browser/brave_content_browser_client.cc index 2b7d3e5034..447fe4c74f 100644 --- a/brave/browser/brave_content_browser_client.cc +++ b/brave/browser/brave_content_browser_client.cc @@ -19,6 +19,7 @@ #include "base/strings/utf_string_conversions.h" #include "brave/browser/notifications/platform_notification_service_impl.h" #include "brave/browser/password_manager/brave_password_manager_client.h" +#include "brave/browser/renderer_host/brave_render_message_filter.h" #include "brave/grit/brave_resources.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/cache_stats_recorder.h" @@ -449,6 +450,7 @@ void BraveContentBrowserClient::RenderProcessWillLaunch( int id = host->GetID(); Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext()); + host->AddFilter(new BraveRenderMessageFilter(id, profile)); host->AddFilter(new printing::PrintingMessageFilter(id, profile)); host->AddFilter(new TtsMessageFilter(host->GetBrowserContext())); diff --git a/brave/browser/renderer_host/brave_render_message_filter.cc b/brave/browser/renderer_host/brave_render_message_filter.cc new file mode 100644 index 0000000000..a02fa30116 --- /dev/null +++ b/brave/browser/renderer_host/brave_render_message_filter.cc @@ -0,0 +1,88 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "brave/browser/renderer_host/brave_render_message_filter.h" + +#include + +#include + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/logging.h" +#include "base/macros.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/common/render_messages.h" +#include "components/content_settings/core/browser/host_content_settings_map.h" +#include "components/content_settings/core/common/content_settings.h" +#include "components/content_settings/core/common/content_settings_types.h" + +using content::BrowserThread; + +namespace { + +const uint32_t kRenderFilteredMessageClasses[] = { + ChromeMsgStart, +}; + +} // namespace + +BraveRenderMessageFilter::BraveRenderMessageFilter(int render_process_id, + Profile* profile) + : BrowserMessageFilter(kRenderFilteredMessageClasses, + arraysize(kRenderFilteredMessageClasses)), + render_process_id_(render_process_id), + profile_(profile) { + host_content_settings_map_ = + HostContentSettingsMapFactory::GetForProfile(profile); +} + +BraveRenderMessageFilter::~BraveRenderMessageFilter() { +} + +bool BraveRenderMessageFilter::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(BraveRenderMessageFilter, message) + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowDatabase, OnAllowDatabase) + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowDOMStorage, OnAllowDOMStorage) + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_AllowIndexedDB, OnAllowIndexedDB) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + + return handled; +} + +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) { + ContentSetting setting = host_content_settings_map_->GetContentSetting( + origin_url, top_origin_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); + *allowed = setting == ContentSetting::CONTENT_SETTING_ALLOW; +} + +void BraveRenderMessageFilter::OnAllowDOMStorage(int render_frame_id, + const GURL& origin_url, + const GURL& top_origin_url, + bool local, + bool* allowed) { + ContentSetting setting = host_content_settings_map_->GetContentSetting( + origin_url, top_origin_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); + *allowed = setting == ContentSetting::CONTENT_SETTING_ALLOW; +} + +void BraveRenderMessageFilter::OnAllowIndexedDB(int render_frame_id, + const GURL& origin_url, + const GURL& top_origin_url, + const base::string16& name, + bool* allowed) { + ContentSetting setting = host_content_settings_map_->GetContentSetting( + origin_url, top_origin_url, CONTENT_SETTINGS_TYPE_COOKIES, ""); + *allowed = setting == ContentSetting::CONTENT_SETTING_ALLOW; +} + diff --git a/brave/browser/renderer_host/brave_render_message_filter.h b/brave/browser/renderer_host/brave_render_message_filter.h new file mode 100644 index 0000000000..3b6000c482 --- /dev/null +++ b/brave/browser/renderer_host/brave_render_message_filter.h @@ -0,0 +1,69 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BRAVE_BROWSER_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_ +#define BRAVE_BROWSER_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_ + +#include +#include + +#include "base/callback.h" +#include "base/macros.h" +#include "base/sequenced_task_runner_helpers.h" +#include "content/public/browser/browser_message_filter.h" +#include "content/public/browser/browser_thread.h" +#include "extensions/buildflags/buildflags.h" +#include "ppapi/buildflags/buildflags.h" +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" + +class GURL; +class Profile; + +namespace content_settings { +class CookieSettings; +} + +// This class filters out incoming Chrome-specific IPC messages for the renderer +// process on the IPC thread. +class BraveRenderMessageFilter : public content::BrowserMessageFilter { + public: + BraveRenderMessageFilter(int render_process_id, Profile* profile); + + bool OnMessageReceived(const IPC::Message& message) override; + + private: + friend class content::BrowserThread; + friend class base::DeleteHelper; + + ~BraveRenderMessageFilter() override; + + void 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); + void OnAllowDOMStorage(int render_frame_id, + const GURL& origin_url, + const GURL& top_origin_url, + bool local, + bool* allowed); + void OnAllowIndexedDB(int render_frame_id, + const GURL& origin_url, + const GURL& top_origin_url, + const base::string16& name, + bool* allowed); + + const int render_process_id_; + + // The Profile associated with our renderer process. This should only be + // accessed on the UI thread! + Profile* profile_; + + HostContentSettingsMap* host_content_settings_map_; + + DISALLOW_COPY_AND_ASSIGN(BraveRenderMessageFilter); +}; + +#endif // BRAVE_BROWSER_RENDERER_HOST_BRAVE_RENDER_MESSAGE_FILTER_H_