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

Fix content_cosmetic.bundle.js multiple injections #10039

Merged
merged 3 commits into from
Sep 14, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@

namespace {

static base::NoDestructor<std::string> g_observing_script("");

static base::NoDestructor<std::vector<std::string>> 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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -231,6 +230,7 @@ void CosmeticFiltersJSHandler::AddJavaScriptObjectToFrame(
v8::Context::Scope context_scope(context);

CreateWorkerObject(isolate, context);
bundle_injected_ = false;
}

void CosmeticFiltersJSHandler::CreateWorkerObject(
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<std::string> 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);
atuchin-m marked this conversation as resolved.
Show resolved Hide resolved
}

} // namespace cosmetic_filters
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Context> context);

// A function to be called from JS
Expand All @@ -70,6 +73,10 @@ class CosmeticFiltersJSHandler {
std::vector<std::string> exceptions_;
GURL url_;
std::unique_ptr<base::DictionaryValue> resources_dict_;

// True if the content_cosmetic.bundle.js has injected in the current frame.
bool bundle_injected_ = false;

base::WeakPtrFactory<CosmeticFiltersJSHandler> weak_ptr_factory_{this};
};

Expand Down
16 changes: 11 additions & 5 deletions components/cosmetic_filters/resources/data/content_cosmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions components/definitions/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare global {
alreadyKnownFirstPartySubtrees: WeakSet
_hasDelayOcurred: boolean
_startCheckingId: number | undefined
tryScheduleQueuePump: (() => void)
}
}
}