Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass ShieldsSettings to renderer via mojo. #25505

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -676,29 +676,23 @@ 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) {
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;
}
return BraveFarblingLevel::BALANCED;
const brave_shields::mojom::FarblingLevel farbling_level =
brave_shields::GetFarblingLevel(
HostContentSettingsMapFactory::GetForProfile(browser_context), url);

PrefService* pref_service = user_prefs::UserPrefs::Get(browser_context);

goodov marked this conversation as resolved.
Show resolved Hide resolved
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
101 changes: 41 additions & 60 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 @@ -46,9 +49,7 @@ BraveShieldsWebContentsObserver* g_receiver_impl_for_testing = nullptr;

} // namespace

BraveShieldsWebContentsObserver::~BraveShieldsWebContentsObserver() {
brave_shields_remotes_.clear();
}
BraveShieldsWebContentsObserver::~BraveShieldsWebContentsObserver() = default;

BraveShieldsWebContentsObserver::BraveShieldsWebContentsObserver(
WebContents* web_contents)
Expand All @@ -57,36 +58,6 @@ BraveShieldsWebContentsObserver::BraveShieldsWebContentsObserver(
*web_contents),
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));
}
}
}
}

void BraveShieldsWebContentsObserver::RenderFrameDeleted(RenderFrameHost* rfh) {
brave_shields_remotes_.erase(rfh);
}

void BraveShieldsWebContentsObserver::RenderFrameHostChanged(
RenderFrameHost* old_host,
RenderFrameHost* new_host) {
if (old_host) {
RenderFrameDeleted(old_host);
}
if (new_host) {
RenderFrameCreated(new_host);
}
}

bool BraveShieldsWebContentsObserver::IsBlockedSubresource(
const std::string& subresource) {
return blocked_url_paths_.find(subresource) != blocked_url_paths_.end();
Expand Down Expand Up @@ -261,11 +232,15 @@ void BraveShieldsWebContentsObserver::RegisterProfilePrefs(
}

void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) {
// when the main frame navigate away
NavigationHandle* navigation_handle) {
// Ignore same document navigations.
if (navigation_handle->IsSameDocument()) {
return;
}

// When the main frame navigates away.
content::ReloadType reload_type = navigation_handle->GetReloadType();
if (navigation_handle->IsInMainFrame() &&
!navigation_handle->IsSameDocument()) {
if (navigation_handle->IsInMainFrame()) {
if (reload_type == content::ReloadType::NONE) {
// For new loads, we reset the counters for both blocked scripts and URLs.
allowed_scripts_.clear();
Expand All @@ -278,17 +253,7 @@ 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));
}
}
});
SendShieldsSettings(navigation_handle);
}

void BraveShieldsWebContentsObserver::BlockAllowedScripts(
Expand Down Expand Up @@ -317,25 +282,41 @@ void BraveShieldsWebContentsObserver::SetReceiverImplForTesting(
g_receiver_impl_for_testing = impl;
}

void BraveShieldsWebContentsObserver::SendShieldsSettings(
NavigationHandle* navigation_handle) {
DCHECK(navigation_handle);
RenderFrameHost* rfh = navigation_handle->GetRenderFrameHost();

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

const brave_shields::mojom::FarblingLevel farbling_level =
brave_shields::GetFarblingLevel(
HostContentSettingsMapFactory::GetForProfile(
rfh->GetBrowserContext()),
primary_url);

PrefService* pref_service =
user_prefs::UserPrefs::Get(rfh->GetBrowserContext());

mojo::AssociatedRemote<brave_shields::mojom::BraveShields> agent;
rfh->GetRemoteAssociatedInterfaces()->GetInterface(&agent);
agent->SetShieldsSettings(brave_shields::mojom::ShieldsSettings::New(
farbling_level, allowed_scripts_,
brave_shields::IsReduceLanguageEnabledForProfile(pref_service)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsReduceLanguageEnabledForProfile seems like it belongs in BraveRendererUpdater ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data passed by BraveRendererUpdater is not accessible by blink internals.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not directly, but it could be exposed through our own class just like we have done in other places

}

void BraveShieldsWebContentsObserver::BindReceiver(
mojo::PendingAssociatedReceiver<brave_shields::mojom::BraveShieldsHost>
receiver,
content::RenderFrameHost* rfh) {
receivers_.Bind(rfh, std::move(receiver));
}

mojo::AssociatedRemote<brave_shields::mojom::BraveShields>&
BraveShieldsWebContentsObserver::GetBraveShieldsRemote(
content::RenderFrameHost* rfh) {
if (!brave_shields_remotes_.contains(rfh)) {
rfh->GetRemoteAssociatedInterfaces()->GetInterface(
&brave_shields_remotes_[rfh]);
}

DCHECK(brave_shields_remotes_[rfh].is_bound());
return brave_shields_remotes_[rfh];
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BraveShieldsWebContentsObserver);

} // namespace brave_shields
21 changes: 6 additions & 15 deletions browser/brave_shields/brave_shields_web_contents_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class BraveShieldsWebContentsObserver

protected:
// content::WebContentsObserver overrides.
void RenderFrameCreated(content::RenderFrameHost* host) override;
void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
void RenderFrameHostChanged(content::RenderFrameHost* old_host,
content::RenderFrameHost* new_host) override;
void ReadyToCommitNavigation(
content::NavigationHandle* navigation_handle) override;

Expand All @@ -92,17 +88,16 @@ 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.
// 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);

// Return an already bound remote for the brave_shields::mojom::BraveShields
// mojo interface. It is an error to call this method with an invalid |rfh|.
mojo::AssociatedRemote<brave_shields::mojom::BraveShields>&
GetBraveShieldsRemote(content::RenderFrameHost* rfh);
// Sends the current shields settings to the renderer process bound to the
// given |navigation_handle|.
void SendShieldsSettings(content::NavigationHandle* navigation_handle);

std::vector<std::string> allowed_scripts_;
// We keep a set of the current page's blocked URLs in case the page
Expand All @@ -112,10 +107,6 @@ class BraveShieldsWebContentsObserver
content::RenderFrameHostReceiverSet<brave_shields::mojom::BraveShieldsHost>
receivers_;

// Map of remote endpoints for the brave_shields::mojom::BraveShields mojo
// interface, to prevent binding a new remote each time it's used.
BraveShieldsRemotesMap brave_shields_remotes_;

WEB_CONTENTS_USER_DATA_KEY_DECL();
};

Expand Down
77 changes: 64 additions & 13 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,53 @@
* 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 "brave/components/content_settings/renderer/brave_content_settings_agent_impl.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(
#define WorkerContentSettingsClient WorkerContentSettingsClient_ChromiumImpl

#include "src/chrome/renderer/worker_content_settings_client.cc"

#undef WorkerContentSettingsClient

WorkerContentSettingsClient_BraveImpl::WorkerContentSettingsClient_BraveImpl(
content::RenderFrame* render_frame)
: WorkerContentSettingsClient_ChromiumImpl(render_frame) {
content_settings::ContentSettingsAgentImpl* agent =
content_settings::ContentSettingsAgentImpl::Get(render_frame);

if (const auto& shields_settings =
static_cast<content_settings::BraveContentSettingsAgentImpl*>(agent)
->shields_settings()) {
shields_settings_ = shields_settings->Clone();
}
}

WorkerContentSettingsClient_BraveImpl::
~WorkerContentSettingsClient_BraveImpl() = default;

WorkerContentSettingsClient_BraveImpl::WorkerContentSettingsClient_BraveImpl(
const WorkerContentSettingsClient_BraveImpl& other)
: WorkerContentSettingsClient_ChromiumImpl(other) {
if (other.shields_settings_) {
shields_settings_ = other.shields_settings_->Clone();
}
}

std::unique_ptr<blink::WebContentSettingsClient>
WorkerContentSettingsClient_BraveImpl::Clone() {
return base::WrapUnique(new WorkerContentSettingsClient_BraveImpl(*this));
}

brave_shields::mojom::ShieldsSettingsPtr
WorkerContentSettingsClient_BraveImpl::GetBraveShieldsSettings(
ContentSettingsType webcompat_settings_type) {
ContentSetting setting = CONTENT_SETTING_DEFAULT;
if (content_setting_rules_) {
Expand Down Expand Up @@ -40,22 +78,37 @@ 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 {
farbling_level = brave_shields::mojom::FarblingLevel::BALANCED;
}

if (shields_settings_) {
auto shields_settings = shields_settings_.Clone();
shields_settings->farbling_level = farbling_level;
return shields_settings;
} else {
return BraveFarblingLevel::BALANCED;
DCHECK(!HasContentSettingsRules());
return brave_shields::mojom::ShieldsSettings::New(
farbling_level, std::vector<std::string>(), false);
}
}

blink::WebSecurityOrigin
WorkerContentSettingsClient::GetEphemeralStorageOriginSync() {
if (!base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage))
WorkerContentSettingsClient_BraveImpl::GetEphemeralStorageOriginSync() {
if (!base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage)) {
return {};
}

if (is_unique_origin_)
if (is_unique_origin_) {
return {};
}

// If first party ephemeral storage is enabled, we should always ask the
// browser if a worker should use ephemeral storage or not.
Expand All @@ -73,16 +126,14 @@ WorkerContentSettingsClient::GetEphemeralStorageOriginSync() {
content_settings_manager_->AllowEphemeralStorageAccess(
frame_token_, document_origin_, site_for_cookies_, top_frame_origin_,
&optional_ephemeral_storage_origin);
// Don't cache the value intentionally as other WorkerContentSettingsClient
// methods do.
// Don't cache the value intentionally as other
// WorkerContentSettingsClient_BraveImpl methods do.
return blink::WebSecurityOrigin(
optional_ephemeral_storage_origin
? blink::WebSecurityOrigin(*optional_ephemeral_storage_origin)
: blink::WebSecurityOrigin());
}

bool WorkerContentSettingsClient::HasContentSettingsRules() const {
bool WorkerContentSettingsClient_BraveImpl::HasContentSettingsRules() const {
return content_setting_rules_.get();
}

#include "src/chrome/renderer/worker_content_settings_client.cc"
Loading
Loading