diff --git a/components/cosmetic_filters/browser/cosmetic_filters_resources.cc b/components/cosmetic_filters/browser/cosmetic_filters_resources.cc index c877efb8ad57..1026f78ea51a 100644 --- a/components/cosmetic_filters/browser/cosmetic_filters_resources.cc +++ b/components/cosmetic_filters/browser/cosmetic_filters_resources.cc @@ -80,32 +80,35 @@ void CosmeticFiltersResources::HiddenClassIdSelectorsOnUI( } void CosmeticFiltersResources::UrlCosmeticResourcesOnUI( - UrlCosmeticResourcesCallback callback, + base::OnceCallback callback, absl::optional resources) { std::move(callback).Run(resources ? std::move(resources.value()) : base::Value()); } -void CosmeticFiltersResources::ShouldDoCosmeticFiltering( +void CosmeticFiltersResources::UrlCosmeticResources( const std::string& url, - ShouldDoCosmeticFilteringCallback callback) { + UrlCosmeticResourcesCallback callback) { bool enabled = brave_shields::ShouldDoCosmeticFiltering(settings_map_, GURL(url)); + + if (!enabled) { + std::move(callback).Run(enabled, false, base::Value()); + return; + } + bool first_party_enabled = brave_shields::IsFirstPartyCosmeticFilteringEnabled(settings_map_, GURL(url)); - std::move(callback).Run(enabled, first_party_enabled); -} -void CosmeticFiltersResources::UrlCosmeticResources( - const std::string& url, - UrlCosmeticResourcesCallback callback) { ad_block_service_->GetTaskRunner()->PostTaskAndReplyWithResult( FROM_HERE, base::BindOnce(&brave_shields::AdBlockService::UrlCosmeticResources, base::Unretained(ad_block_service_), url), - base::BindOnce(&CosmeticFiltersResources::UrlCosmeticResourcesOnUI, - weak_factory_.GetWeakPtr(), std::move(callback))); + base::BindOnce( + &CosmeticFiltersResources::UrlCosmeticResourcesOnUI, + weak_factory_.GetWeakPtr(), + base::BindOnce(std::move(callback), enabled, first_party_enabled))); } } // namespace cosmetic_filters diff --git a/components/cosmetic_filters/browser/cosmetic_filters_resources.h b/components/cosmetic_filters/browser/cosmetic_filters_resources.h index 1e6783eee64a..9dd51522d313 100644 --- a/components/cosmetic_filters/browser/cosmetic_filters_resources.h +++ b/components/cosmetic_filters/browser/cosmetic_filters_resources.h @@ -10,6 +10,7 @@ #include #include +#include "base/callback.h" #include "base/memory/weak_ptr.h" #include "base/values.h" #include "brave/components/cosmetic_filters/common/cosmetic_filters.mojom.h" @@ -35,20 +36,17 @@ class CosmeticFiltersResources final brave_shields::AdBlockService* ad_block_service); ~CosmeticFiltersResources() override; - // Sends back to renderer a response: do we need to apply cosmetic filters - // for the url. - void ShouldDoCosmeticFiltering( - const std::string& url, - ShouldDoCosmeticFilteringCallback callback) override; - // Sends back to renderer a response about rules that has to be applied // for the specified selectors. void HiddenClassIdSelectors(const std::string& input, const std::vector& exceptions, HiddenClassIdSelectorsCallback callback) override; - // Sends back to renderer a response what rules and scripts has to be - // applied for the specified url. + // If cosmetic filtering is enabled, sends the renderer a response including + // whether or not to apply cosmetic filtering to first party elements along + // with an initial set of rules and scripts to apply for the given URL. + // If cosmetic filtering is disabled, `enabled` will be set to false and the + // other return values should be ignored. void UrlCosmeticResources(const std::string& url, UrlCosmeticResourcesCallback callback) override; @@ -56,7 +54,7 @@ class CosmeticFiltersResources final void HiddenClassIdSelectorsOnUI(HiddenClassIdSelectorsCallback callback, absl::optional resources); - void UrlCosmeticResourcesOnUI(UrlCosmeticResourcesCallback callback, + void UrlCosmeticResourcesOnUI(base::OnceCallback callback, absl::optional resources); HostContentSettingsMap* settings_map_; // Not owned diff --git a/components/cosmetic_filters/common/cosmetic_filters.mojom b/components/cosmetic_filters/common/cosmetic_filters.mojom index 7827c00a1de8..73a6f51ad0df 100644 --- a/components/cosmetic_filters/common/cosmetic_filters.mojom +++ b/components/cosmetic_filters/common/cosmetic_filters.mojom @@ -3,10 +3,11 @@ module cosmetic_filters.mojom; import "mojo/public/mojom/base/values.mojom"; interface CosmeticFiltersResources { - ShouldDoCosmeticFiltering(string url) => (bool enabled, - bool first_party_enabled); - UrlCosmeticResources(string url) => (mojo_base.mojom.Value result); // Receives an input string which is JSON object. HiddenClassIdSelectors(string input, array exceptions) => ( mojo_base.mojom.Value result); + + UrlCosmeticResources(string url) => (bool enabled, + bool first_party_enabled, + mojo_base.mojom.Value result); }; diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc index 5d49b55a0e36..9f906defaeda 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc @@ -292,20 +292,6 @@ void CosmeticFiltersJSHandler::ProcessURL(const GURL& url, if (!EnsureConnected() || url_.is_empty() || !url_.is_valid()) return; - cosmetic_filters_resources_->ShouldDoCosmeticFiltering( - url_.spec(), - base::BindOnce(&CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering, - base::Unretained(this), std::move(callback))); -} - -void CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering( - base::OnceClosure callback, - bool enabled, - bool first_party_enabled) { - if (!enabled || !EnsureConnected()) - return; - - enabled_1st_party_cf_ = first_party_enabled; cosmetic_filters_resources_->UrlCosmeticResources( url_.spec(), base::BindOnce(&CosmeticFiltersJSHandler::OnUrlCosmeticResources, @@ -314,7 +300,12 @@ void CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering( void CosmeticFiltersJSHandler::OnUrlCosmeticResources( base::OnceClosure callback, + bool enabled, + bool first_party_enabled, base::Value result) { + if (!enabled || !EnsureConnected()) + return; + enabled_1st_party_cf_ = first_party_enabled; resources_dict_ = base::DictionaryValue::From( base::Value::ToUniquePtrValue(std::move(result))); std::move(callback).Run(); diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h index 3441ae33ea01..5c2de21037fe 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h @@ -54,10 +54,10 @@ class CosmeticFiltersJSHandler { // A function to be called from JS void HiddenClassIdSelectors(const std::string& input); - void OnShouldDoCosmeticFiltering(base::OnceClosure callback, - bool enabled, - bool first_party_enabled); - void OnUrlCosmeticResources(base::OnceClosure callback, base::Value result); + void OnUrlCosmeticResources(base::OnceClosure callback, + bool enabled, + bool first_party_enabled, + base::Value result); void CSSRulesRoutine(base::DictionaryValue* resources_dict); void OnHiddenClassIdSelectors(base::Value result); diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.cc b/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.cc index 84894b52467f..4a5b733a5723 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.cc +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.cc @@ -84,9 +84,14 @@ void CosmeticFiltersJsRenderFrameObserver::ReadyToCommitNavigation( } void CosmeticFiltersJsRenderFrameObserver::RunScriptsAtDocumentStart() { - ready_->Post(FROM_HERE, - base::BindOnce(&CosmeticFiltersJsRenderFrameObserver::ApplyRules, - weak_factory_.GetWeakPtr())); + if (ready_->is_signaled()) { + ApplyRules(); + } else { + ready_->Post( + FROM_HERE, + base::BindOnce(&CosmeticFiltersJsRenderFrameObserver::ApplyRules, + weak_factory_.GetWeakPtr())); + } } void CosmeticFiltersJsRenderFrameObserver::ApplyRules() {