Skip to content

Commit

Permalink
Uplift of #5860 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases committed Jun 16, 2020
1 parent b4adbcb commit 890d01e
Showing 1 changed file with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/strings/utf_string_conversions.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "content/browser/web_contents/web_contents_impl.h"
Expand All @@ -33,16 +34,72 @@ const char k_youtube_background_playback_script[] =
" }"
"}());";

const char k_youtube_ads_block_script[] =
"(function() {"
" const prunePaths = ['playerResponse.adPlacements',"
" 'playerResponse.playerAds', 'adPlacements', 'playerAds'];"
" const findOwner = function(root, path) {"
" let owner = root;"
" let chain = path;"
" for (;;) {"
" if ( owner instanceof Object === false ) { return; }"
" const pos = chain.indexOf('.');"
" if ( pos === -1 ) {"
" return owner.hasOwnProperty(chain)? [ owner, chain ]:"
" undefined;"
" }"
" const prop = chain.slice(0, pos);"
" if ( owner.hasOwnProperty(prop) === false ) { return; }"
" owner = owner[prop];"
" chain = chain.slice(pos + 1);"
" }"
" };"
" JSON.parse = new Proxy(JSON.parse, {"
" apply: function() {"
" const r = Reflect.apply(...arguments);"
" for ( const path of prunePaths ) {"
" const details = findOwner(r, path);"
" if ( details !== undefined ) {"
" delete details[0][details[1]];"
" }"
" }"
" return r;"
" },"
" });"
"})();";

bool IsYouTubeDomain(content::WebContents* contents) {
if (net::registry_controlled_domains::SameDomainOrHost(
contents->GetLastCommittedURL(), GURL("https://www.youtube.com"),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
return true;
}

return false;
}

bool IsBackgroundVideoPlaybackEnabled(content::WebContents* contents) {
PrefService* prefs =
static_cast<Profile*>(contents->GetBrowserContext())->GetPrefs();
if (!prefs->GetBoolean(kBackgroundVideoPlaybackEnabled))
return false;

if (net::registry_controlled_domains::SameDomainOrHost(
contents->GetLastCommittedURL(), GURL("https://www.youtube.com"),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
content::RenderFrameHost::AllowInjectingJavaScript();

return true;
}

bool IsAdBlockEnabled(content::WebContents* contents) {
Profile* profile = static_cast<Profile*>(contents->GetBrowserContext());

brave_shields::ControlType control_type =
brave_shields::GetAdControlType(profile,
contents->GetLastCommittedURL());
if (brave_shields::GetBraveShieldsEnabled(profile,
contents->GetLastCommittedURL()) &&
control_type != brave_shields::ALLOW) {
content::RenderFrameHost::AllowInjectingJavaScript();

return true;
}

Expand All @@ -61,11 +118,20 @@ BackgroundVideoPlaybackTabHelper::~BackgroundVideoPlaybackTabHelper() {

void BackgroundVideoPlaybackTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Filter only YT domains here
if (!IsYouTubeDomain(web_contents())) {
return;
}
if (IsBackgroundVideoPlaybackEnabled(web_contents())) {
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(k_youtube_background_playback_script),
base::NullCallback());
}
if (IsAdBlockEnabled(web_contents())) {
web_contents()->GetMainFrame()->ExecuteJavaScript(
base::UTF8ToUTF16(k_youtube_ads_block_script),
base::NullCallback());
}
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BackgroundVideoPlaybackTabHelper)

0 comments on commit 890d01e

Please sign in to comment.