Skip to content

Commit

Permalink
Pass ShieldsSettings to renderer via mojo.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Sep 11, 2024
1 parent 33ed755 commit 45189f3
Show file tree
Hide file tree
Showing 38 changed files with 310 additions and 183 deletions.
36 changes: 22 additions & 14 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,29 +670,37 @@ BraveContentBrowserClient::GetEphemeralStorageToken(
bool BraveContentBrowserClient::AllowWorkerFingerprinting(
const GURL& url,
content::BrowserContext* browser_context) {
return WorkerGetBraveFarblingLevel(url, browser_context) !=
BraveFarblingLevel::MAXIMUM;
return WorkerGetBraveShieldSettings(url, browser_context)->farbling_level !=
brave_shields::mojom::FarblingLevel::MAXIMUM;
}

uint8_t BraveContentBrowserClient::WorkerGetBraveFarblingLevel(
brave_shields::mojom::ShieldsSettingsPtr
BraveContentBrowserClient::WorkerGetBraveShieldSettings(
const GURL& url,
content::BrowserContext* browser_context) {
brave_shields::mojom::FarblingLevel farbling_level =
brave_shields::mojom::FarblingLevel::BALANCED;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(browser_context);
const bool shields_up =
brave_shields::GetBraveShieldsEnabled(host_content_settings_map, url);
if (!shields_up) {
return BraveFarblingLevel::OFF;
}
auto fingerprinting_type = brave_shields::GetFingerprintingControlType(
host_content_settings_map, url);
if (fingerprinting_type == ControlType::BLOCK) {
return BraveFarblingLevel::MAXIMUM;
}
if (fingerprinting_type == ControlType::ALLOW) {
return BraveFarblingLevel::OFF;
if (shields_up) {
auto fingerprinting_type = brave_shields::GetFingerprintingControlType(
host_content_settings_map, url);
if (fingerprinting_type == ControlType::BLOCK) {
farbling_level = brave_shields::mojom::FarblingLevel::MAXIMUM;
}
if (fingerprinting_type == ControlType::ALLOW) {
farbling_level = brave_shields::mojom::FarblingLevel::OFF;
}
} else {
farbling_level = brave_shields::mojom::FarblingLevel::OFF;
}
return BraveFarblingLevel::BALANCED;
PrefService* pref_service = user_prefs::UserPrefs::Get(browser_context);

return brave_shields::mojom::ShieldsSettings::New(
farbling_level, std::vector<std::string>(),
brave_shields::IsReduceLanguageEnabledForProfile(pref_service));
}

content::ContentBrowserClient::AllowWebBluetoothResult
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {
const GURL& url,
content::BrowserContext* browser_context) override;

uint8_t WorkerGetBraveFarblingLevel(
brave_shields::mojom::ShieldsSettingsPtr WorkerGetBraveShieldSettings(
const GURL& url,
content::BrowserContext* browser_context) override;

Expand Down
69 changes: 47 additions & 22 deletions browser/brave_shields/brave_shields_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#include "brave/components/brave_shields/content/browser/brave_shields_util.h"
#include "brave/components/brave_shields/core/common/brave_shield_constants.h"
#include "brave/components/brave_shields/core/common/pref_names.h"
#include "brave/components/brave_shields/core/common/shields_settings.mojom.h"
#include "brave/components/constants/pref_names.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/renderer_configuration.mojom.h"
#include "components/prefs/pref_registry_simple.h"
Expand All @@ -35,6 +37,7 @@
#include "brave/browser/brave_shields/brave_shields_tab_helper.h"
#endif

using content::NavigationHandle;
using content::RenderFrameHost;
using content::WebContents;

Expand All @@ -58,18 +61,49 @@ BraveShieldsWebContentsObserver::BraveShieldsWebContentsObserver(
receivers_(web_contents, this) {}

void BraveShieldsWebContentsObserver::RenderFrameCreated(RenderFrameHost* rfh) {
if (rfh && allowed_scripts_.size()) {
GetBraveShieldsRemote(rfh)->SetAllowScriptsFromOriginsOnce(
allowed_scripts_);
}
if (rfh) {
if (content::BrowserContext* context = rfh->GetBrowserContext()) {
if (PrefService* pref_service = user_prefs::UserPrefs::Get(context)) {
GetBraveShieldsRemote(rfh)->SetReduceLanguageEnabled(
brave_shields::IsReduceLanguageEnabledForProfile(pref_service));
}
SendShieldsSettingsToFrame(rfh, nullptr);
}

void BraveShieldsWebContentsObserver::SendShieldsSettingsToFrame(
RenderFrameHost* rfh,
NavigationHandle* navigation_handle) {
DCHECK(rfh);
DCHECK(!navigation_handle || rfh == navigation_handle->GetRenderFrameHost());

const GURL& primary_url =
navigation_handle
? (navigation_handle->GetParentFrameOrOuterDocument()
? navigation_handle->GetParentFrameOrOuterDocument()
->GetOutermostMainFrame()
->GetLastCommittedURL()
: navigation_handle->GetURL())
: rfh->GetLastCommittedURL();

brave_shields::mojom::FarblingLevel farbling_level =
brave_shields::mojom::FarblingLevel::BALANCED;
HostContentSettingsMap* host_content_settings_map =
HostContentSettingsMapFactory::GetForProfile(rfh->GetBrowserContext());
const bool shields_up = brave_shields::GetBraveShieldsEnabled(
host_content_settings_map, primary_url);
if (shields_up) {
auto fingerprinting_type = brave_shields::GetFingerprintingControlType(
host_content_settings_map, primary_url);
if (fingerprinting_type == ControlType::BLOCK) {
farbling_level = brave_shields::mojom::FarblingLevel::MAXIMUM;
}
if (fingerprinting_type == ControlType::ALLOW) {
farbling_level = brave_shields::mojom::FarblingLevel::OFF;
}
} else {
farbling_level = brave_shields::mojom::FarblingLevel::OFF;
}
PrefService* pref_service =
user_prefs::UserPrefs::Get(rfh->GetBrowserContext());

GetBraveShieldsRemote(rfh)->SetShieldsSettings(
brave_shields::mojom::ShieldsSettings::New(
farbling_level, allowed_scripts_,
brave_shields::IsReduceLanguageEnabledForProfile(pref_service)));
}

void BraveShieldsWebContentsObserver::RenderFrameDeleted(RenderFrameHost* rfh) {
Expand Down Expand Up @@ -261,7 +295,7 @@ void BraveShieldsWebContentsObserver::RegisterProfilePrefs(
}

void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
NavigationHandle* navigation_handle) {
// when the main frame navigate away
content::ReloadType reload_type = navigation_handle->GetReloadType();
if (navigation_handle->IsInMainFrame() &&
Expand All @@ -278,17 +312,8 @@ void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
}
}

navigation_handle->GetWebContents()->ForEachRenderFrameHost(
[this](content::RenderFrameHost* rfh) {
GetBraveShieldsRemote(rfh)->SetAllowScriptsFromOriginsOnce(
allowed_scripts_);
if (content::BrowserContext* context = rfh->GetBrowserContext()) {
if (PrefService* pref_service = user_prefs::UserPrefs::Get(context)) {
GetBraveShieldsRemote(rfh)->SetReduceLanguageEnabled(
brave_shields::IsReduceLanguageEnabledForProfile(pref_service));
}
}
});
RenderFrameHost* rfh = navigation_handle->GetRenderFrameHost();
SendShieldsSettingsToFrame(rfh, navigation_handle);
}

void BraveShieldsWebContentsObserver::BlockAllowedScripts(
Expand Down
9 changes: 6 additions & 3 deletions browser/brave_shields/brave_shields_web_contents_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ class BraveShieldsWebContentsObserver
// other than this own class, for testing purposes only.
static void SetReceiverImplForTesting(BraveShieldsWebContentsObserver* impl);

// Only used from the BindBraveShieldsHost() static method, useful to bind the
// mojo receiver of brave_shields::mojom::BraveShieldsHost to a different
// implementor when needed, for testing purposes.
void SendShieldsSettingsToFrame(content::RenderFrameHost* rfh,
content::NavigationHandle* navigation_handle);

// Only used from the BindBraveShieldsHost() static method, useful to bind
// the mojo receiver of brave_shields::mojom::BraveShieldsHost to a
// different implementor when needed, for testing purposes.
void BindReceiver(mojo::PendingAssociatedReceiver<
brave_shields::mojom::BraveShieldsHost> receiver,
content::RenderFrameHost* rfh);
Expand Down
17 changes: 12 additions & 5 deletions chromium_src/chrome/renderer/worker_content_settings_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "chrome/renderer/worker_content_settings_client.h"

#include <optional>

#include "brave/components/brave_shields/core/common/brave_shield_utils.h"
#include "chrome/renderer/worker_content_settings_client.h"
#include "components/content_settings/renderer/content_settings_agent_impl.h"
#include "net/base/features.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"

BraveFarblingLevel WorkerContentSettingsClient::GetBraveFarblingLevel(
brave_shields::mojom::ShieldsSettingsPtr
WorkerContentSettingsClient::GetBraveShieldsSettings(
ContentSettingsType webcompat_settings_type) {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
if (content_setting_rules_) {
Expand Down Expand Up @@ -40,13 +42,18 @@ BraveFarblingLevel WorkerContentSettingsClient::GetBraveFarblingLevel(
}
}
}

brave_shields::mojom::FarblingLevel farbling_level =
brave_shields::mojom::FarblingLevel::BALANCED;
if (setting == CONTENT_SETTING_BLOCK) {
return BraveFarblingLevel::MAXIMUM;
farbling_level = brave_shields::mojom::FarblingLevel::MAXIMUM;
} else if (setting == CONTENT_SETTING_ALLOW) {
return BraveFarblingLevel::OFF;
farbling_level = brave_shields::mojom::FarblingLevel::OFF;
} else {
return BraveFarblingLevel::BALANCED;
farbling_level = brave_shields::mojom::FarblingLevel::BALANCED;
}
return brave_shields::mojom::ShieldsSettings::New(
farbling_level, std::vector<std::string>(), false);
}

blink::WebSecurityOrigin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define BRAVE_CHROMIUM_SRC_CHROME_RENDERER_WORKER_CONTENT_SETTINGS_CLIENT_H_

#define BRAVE_WORKER_CONTENT_SETTINGS_CLIENT_H \
BraveFarblingLevel GetBraveFarblingLevel( \
brave_shields::mojom::ShieldsSettingsPtr GetBraveShieldsSettings( \
ContentSettingsType webcompat_settings_type) override; \
blink::WebSecurityOrigin GetEphemeralStorageOriginSync() override; \
bool HasContentSettingsRules() const override;
Expand Down
1 change: 1 addition & 0 deletions chromium_src/content/browser/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+brave/components/brave_shields/core/common",
"+content/public/browser",
"+content/public/common",
"+services/network/public/mojom/cookie_manager.mojom.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

namespace content {

void ServiceWorkerContentSettingsProxyImpl::GetBraveFarblingLevel(
GetBraveFarblingLevelCallback callback) {
void ServiceWorkerContentSettingsProxyImpl::GetBraveShieldsSettings(
GetBraveShieldsSettingsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// May be shutting down.
if (!context_wrapper_->browser_context()) {
std::move(callback).Run(1 /* OFF */);
std::move(callback).Run(brave_shields::mojom::ShieldsSettings::New());
return;
}
if (origin_.opaque()) {
std::move(callback).Run(1 /* OFF */);
std::move(callback).Run(brave_shields::mojom::ShieldsSettings::New());
return;
}
std::move(callback).Run(
GetContentClient()->browser()->WorkerGetBraveFarblingLevel(
GetContentClient()->browser()->WorkerGetBraveShieldSettings(
origin_.GetURL(), context_wrapper_->browser_context()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// affects this class.
#include "third_party/blink/public/mojom/worker/worker_content_settings_proxy.mojom.h"

#define RequestFileSystemAccessSync \
GetBraveFarblingLevel(GetBraveFarblingLevelCallback callback) override; \
#define RequestFileSystemAccessSync \
GetBraveShieldsSettings(GetBraveShieldsSettingsCallback callback) override; \
void RequestFileSystemAccessSync

#include "src/content/browser/service_worker/service_worker_content_settings_proxy_impl.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

namespace content {

void SharedWorkerContentSettingsProxyImpl::GetBraveFarblingLevel(
GetBraveFarblingLevelCallback callback) {
void SharedWorkerContentSettingsProxyImpl::GetBraveShieldsSettings(
GetBraveShieldsSettingsCallback callback) {
if (!origin_.opaque()) {
owner_->GetBraveFarblingLevel(origin_.GetURL(), std::move(callback));
owner_->GetBraveShieldsSettings(origin_.GetURL(), std::move(callback));
} else {
std::move(callback).Run(1 /* OFF */);
std::move(callback).Run(brave_shields::mojom::ShieldsSettings::New());
}
}

} // namespace content
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// affects this class.
#include "third_party/blink/public/mojom/worker/worker_content_settings_proxy.mojom.h"

#define RequestFileSystemAccessSync \
GetBraveFarblingLevel(GetBraveFarblingLevelCallback callback) override; \
#define RequestFileSystemAccessSync \
GetBraveShieldsSettings(GetBraveShieldsSettingsCallback callback) override; \
void RequestFileSystemAccessSync

#include "src/content/browser/worker_host/shared_worker_content_settings_proxy_impl.h" // IWYU pragma: export
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

namespace content {

void SharedWorkerHost::GetBraveFarblingLevel(
void SharedWorkerHost::GetBraveShieldsSettings(
const GURL& url,
base::OnceCallback<void(uint8_t)> callback) {
base::OnceCallback<void(brave_shields::mojom::ShieldsSettingsPtr)>
callback) {
std::move(callback).Run(
GetContentClient()->browser()->WorkerGetBraveFarblingLevel(
GetContentClient()->browser()->WorkerGetBraveShieldSettings(
url, GetProcessHost()->GetBrowserContext()));
}

Expand Down
14 changes: 9 additions & 5 deletions chromium_src/content/browser/worker_host/shared_worker_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_BROWSER_WORKER_HOST_SHARED_WORKER_HOST_H_
#define BRAVE_CHROMIUM_SRC_CONTENT_BROWSER_WORKER_HOST_SHARED_WORKER_HOST_H_

#define CreateNetworkFactoryParamsForSubresources \
UnusedFunction(); \
void GetBraveFarblingLevel(const GURL& url, \
base::OnceCallback<void(uint8_t)> callback); \
network::mojom::URLLoaderFactoryParamsPtr \
#include "brave/components/brave_shields/core/common/shields_settings.mojom.h"

#define CreateNetworkFactoryParamsForSubresources \
UnusedFunction(); \
void GetBraveShieldsSettings( \
const GURL& url, \
base::OnceCallback<void(brave_shields::mojom::ShieldsSettingsPtr)> \
callback); \
network::mojom::URLLoaderFactoryParamsPtr \
CreateNetworkFactoryParamsForSubresources

#include "src/content/browser/worker_host/shared_worker_host.h" // IWYU pragma: export
Expand Down
1 change: 1 addition & 0 deletions chromium_src/content/public/browser/DEPS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include_rules = [
"+brave/components/brave_shields/core/common",
"+components/services/storage/public/mojom/blob_storage_context.mojom.h",
"+content/browser",
"+content/common",
Expand Down
5 changes: 3 additions & 2 deletions chromium_src/content/public/browser/content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ ContentBrowserClient::GetEphemeralStorageToken(
return std::nullopt;
}

uint8_t ContentBrowserClient::WorkerGetBraveFarblingLevel(
brave_shields::mojom::ShieldsSettingsPtr
ContentBrowserClient::WorkerGetBraveShieldSettings(
const GURL& url,
BrowserContext* browser_context) {
return 1 /* OFF */;
return brave_shields::mojom::ShieldsSettingsPtr();
}

GURL ContentBrowserClient::SanitizeURL(content::RenderFrameHost*,
Expand Down
6 changes: 4 additions & 2 deletions chromium_src/content/public/browser/content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_
#define BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_

#include "brave/components/brave_shields/core/common/shields_settings.mojom.h"
#include "third_party/blink/public/mojom/loader/referrer.mojom.h"

// Brave-specific: allows the embedder to modify the referrer string
Expand All @@ -25,8 +26,9 @@
RenderFrameHost* render_frame_host, const url::Origin& origin); \
virtual bool AllowWorkerFingerprinting(const GURL& url, \
BrowserContext* browser_context); \
virtual uint8_t WorkerGetBraveFarblingLevel( \
const GURL& url, BrowserContext* browser_context); \
virtual brave_shields::mojom::ShieldsSettingsPtr \
WorkerGetBraveShieldSettings(const GURL& url, \
BrowserContext* browser_context); \
virtual GURL SanitizeURL(content::RenderFrameHost*, const GURL&); \
virtual void SetBrowserStartupIsCompleteForTesting

Expand Down
1 change: 1 addition & 0 deletions chromium_src/third_party/blink/public/DEPS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include_rules = [
"+brave/components/brave_shields/core/common",
"+brave/third_party/blink/renderer",
]
Loading

0 comments on commit 45189f3

Please sign in to comment.