diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc index 6034c4db18cb..836c7a0447c8 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.cc @@ -27,12 +27,14 @@ namespace { -static base::NoDestructor g_observing_script(""); - static base::NoDestructor> g_vetted_search_engines( {"duckduckgo", "qwant", "bing", "startpage", "google", "yandex", "ecosia", "brave"}); +// Entry point to content_cosmetic.ts script. +const char kObservingScriptletEntryPoint[] = + "window.content_cosmetic.tryScheduleQueuePump()"; + const char kScriptletInitScript[] = R"((function() { let text = %s; @@ -193,9 +195,6 @@ CosmeticFiltersJSHandler::CosmeticFiltersJSHandler( : render_frame_(render_frame), isolated_world_id_(isolated_world_id), enabled_1st_party_cf_(false) { - if (g_observing_script->empty()) { - *g_observing_script = LoadDataResource(kCosmeticFiltersGenerated[0].id); - } EnsureConnected(); } @@ -231,6 +230,7 @@ void CosmeticFiltersJSHandler::AddJavaScriptObjectToFrame( v8::Context::Scope context_scope(context); CreateWorkerObject(isolate, context); + bundle_injected_ = false; } void CosmeticFiltersJSHandler::CreateWorkerObject( @@ -366,9 +366,7 @@ void CosmeticFiltersJSHandler::ApplyRules() { web_frame->ExecuteScriptInIsolatedWorld( isolated_world_id_, blink::WebString::FromUTF8(pre_init_script), blink::BackForwardCacheAware::kAllow); - web_frame->ExecuteScriptInIsolatedWorld( - isolated_world_id_, blink::WebString::FromUTF8(*g_observing_script), - blink::BackForwardCacheAware::kAllow); + ExecuteObservingBundleEntryPoint(); CSSRulesRoutine(resources_dict_.get()); } @@ -443,11 +441,8 @@ void CosmeticFiltersJSHandler::CSSRulesRoutine( } } - if (!enabled_1st_party_cf_) { - web_frame->ExecuteScriptInIsolatedWorld( - isolated_world_id_, blink::WebString::FromUTF8(*g_observing_script), - blink::BackForwardCacheAware::kAllow); - } + if (!enabled_1st_party_cf_) + ExecuteObservingBundleEntryPoint(); } void CosmeticFiltersJSHandler::OnHiddenClassIdSelectors(base::Value result) { @@ -478,11 +473,31 @@ void CosmeticFiltersJSHandler::OnHiddenClassIdSelectors(base::Value result) { blink::BackForwardCacheAware::kAllow); } - if (!enabled_1st_party_cf_) { + if (!enabled_1st_party_cf_) + ExecuteObservingBundleEntryPoint(); +} + +void CosmeticFiltersJSHandler::ExecuteObservingBundleEntryPoint() { + blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame(); + DCHECK(web_frame); + + if (!bundle_injected_) { + static base::NoDestructor s_observing_script( + LoadDataResource(kCosmeticFiltersGenerated[0].id)); + bundle_injected_ = true; + web_frame->ExecuteScriptInIsolatedWorld( - isolated_world_id_, blink::WebString::FromUTF8(*g_observing_script), + isolated_world_id_, blink::WebString::FromUTF8(*s_observing_script), blink::BackForwardCacheAware::kAllow); + + // kObservingScriptletEntryPoint was called by `s_observing_script`. + return; } + + web_frame->ExecuteScriptInIsolatedWorld( + isolated_world_id_, + blink::WebString::FromUTF8(kObservingScriptletEntryPoint), + blink::BackForwardCacheAware::kAllow); } } // namespace cosmetic_filters diff --git a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h index 5f949334ba4c..1dd8a4633ca4 100644 --- a/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h +++ b/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h @@ -49,6 +49,9 @@ class CosmeticFiltersJSHandler { bool EnsureConnected(); void OnRemoteDisconnect(); + // Injects content_cosmetic bundle (if needed) and calls the entry point. + void ExecuteObservingBundleEntryPoint(); + void CreateWorkerObject(v8::Isolate* isolate, v8::Local context); // A function to be called from JS @@ -70,6 +73,10 @@ class CosmeticFiltersJSHandler { std::vector exceptions_; GURL url_; std::unique_ptr resources_dict_; + + // True if the content_cosmetic.bundle.js has injected in the current frame. + bool bundle_injected_ = false; + base::WeakPtrFactory weak_ptr_factory_{this}; }; diff --git a/components/cosmetic_filters/resources/data/content_cosmetic.ts b/components/cosmetic_filters/resources/data/content_cosmetic.ts index 24c5af616991..34e7ebf4c294 100644 --- a/components/cosmetic_filters/resources/data/content_cosmetic.ts +++ b/components/cosmetic_filters/resources/data/content_cosmetic.ts @@ -534,9 +534,15 @@ const scheduleQueuePump = (hide1pContent: boolean, genericHide: boolean) => { }, { timeout: maxTimeMSBeforeStart }) } -if (!CC.observingHasStarted) { - CC.observingHasStarted = true - scheduleQueuePump(CC.hide1pContent, CC.generichide) -} else { - scheduleQueuePump(false, false) +const tryScheduleQueuePump = () => { + if (!CC.observingHasStarted) { + CC.observingHasStarted = true + scheduleQueuePump(CC.hide1pContent, CC.generichide) + } else { + scheduleQueuePump(false, false) + } } + +CC.tryScheduleQueuePump = CC.tryScheduleQueuePump || tryScheduleQueuePump + +tryScheduleQueuePump() diff --git a/components/definitions/global.d.ts b/components/definitions/global.d.ts index f04ca20e3a0b..3a9e23cbfe12 100644 --- a/components/definitions/global.d.ts +++ b/components/definitions/global.d.ts @@ -40,6 +40,7 @@ declare global { alreadyKnownFirstPartySubtrees: WeakSet _hasDelayOcurred: boolean _startCheckingId: number | undefined + tryScheduleQueuePump: (() => void) } } }