Skip to content

Commit

Permalink
Merge pull request #8071 from brave/yt_ads_problem
Browse files Browse the repository at this point in the history
fixes ads on yt and gets rid of csp error
  • Loading branch information
SergeyZhukovsky committed Feb 25, 2021
2 parents ae6d17f + 02fb8d0 commit 7e16bd9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
16 changes: 16 additions & 0 deletions chromium_src/chrome/common/chrome_isolated_world_ids.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_
#define BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_

#define ISOLATED_WORLD_ID_CHROME_INTERNAL \
ISOLATED_WORLD_ID_CHROME_INTERNAL, ISOLATED_WORLD_ID_BRAVE_INTERNAL

#include "../../../../chrome/common/chrome_isolated_world_ids.h"

#undef ISOLATED_WORLD_ID_CHROME_INTERNAL

#endif // BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ 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"});

const char kScriptletInitScript[] =
R"((function() {
let text = `%s`;
let script;
try {
script = document.createElement('script');
const textNode = document.createTextNode(text);
script.appendChild(textNode);;
(document.head || document.documentElement).appendChild(script);
} catch (ex) {
/* Unused catch */
}
if (script) {
if (script.parentNode) {
script.parentNode.removeChild(script);
}
script.textContent = '';
}
})();)";

const char kPreInitScript[] =
R"((function() {
if (window.content_cosmetic == undefined) {
Expand Down Expand Up @@ -281,9 +301,11 @@ void CosmeticFiltersJSHandler::OnUrlCosmeticResources(base::Value result) {

std::string scriptlet_script;
resources_dict->GetString("injected_script", &scriptlet_script);
// Execute scriptlets on all frames
if (!scriptlet_script.empty()) {
web_frame->ExecuteScript(blink::WebString::FromUTF8(scriptlet_script));
scriptlet_script =
base::StringPrintf(kScriptletInitScript, scriptlet_script.c_str());
web_frame->ExecuteScriptInIsolatedWorld(
isolated_world_id_, blink::WebString::FromUTF8(scriptlet_script));
}
if (!render_frame_->IsMainFrame())
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,43 @@

#include "base/bind.h"
#include "content/public/renderer/render_frame.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/platform/web_url.h"
#include "third_party/blink/public/web/web_local_frame.h"

namespace cosmetic_filters {

namespace {

const char kSecurityOrigin[] =
"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd";

void EnsureIsolatedWorldInitialized(int world_id) {
static base::Optional<int> last_used_world_id;
if (last_used_world_id) {
// Early return since the isolated world info. is already initialized.
DCHECK_EQ(*last_used_world_id, world_id)
<< "EnsureIsolatedWorldInitialized should always be called with the "
"same |world_id|";
return;
}

last_used_world_id = world_id;

// Set an empty CSP so that the main world's CSP is not used in the isolated
// world.
constexpr char kContentSecurityPolicy[] = "";

blink::WebIsolatedWorldInfo info;
info.security_origin =
blink::WebSecurityOrigin::Create(GURL(kSecurityOrigin));
info.content_security_policy =
blink::WebString::FromUTF8(kContentSecurityPolicy);
blink::SetIsolatedWorldInfo(world_id, info);
}

} // namespace

CosmeticFiltersJsRenderFrameObserver::CosmeticFiltersJsRenderFrameObserver(
content::RenderFrame* render_frame,
const int32_t isolated_world_id)
Expand Down Expand Up @@ -48,6 +81,7 @@ void CosmeticFiltersJsRenderFrameObserver::DidCreateNewDocument() {
if (!native_javascript_handle_) {
native_javascript_handle_.reset(
new CosmeticFiltersJSHandler(render_frame(), isolated_world_id_));
EnsureIsolatedWorldInitialized(isolated_world_id_);
}
native_javascript_handle_->ProcessURL(url_);
}
Expand Down
2 changes: 1 addition & 1 deletion renderer/brave_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ void BraveContentRendererClient::RenderFrameCreated(
ChromeContentRendererClient::RenderFrameCreated(render_frame);

new cosmetic_filters::CosmeticFiltersJsRenderFrameObserver(
render_frame, ISOLATED_WORLD_ID_CHROME_INTERNAL);
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL);
}

0 comments on commit 7e16bd9

Please sign in to comment.