Skip to content

Commit

Permalink
inject scriplets on websites on Android as a part of cosmetic filters
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyZhukovsky committed Sep 10, 2020
1 parent 58d6eae commit 80b4456
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 355 deletions.
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ source_set("browser_process") {
]
} else {
sources += [
"android/brave_cosmetic_resources_tab_helper.cc",
"android/brave_cosmetic_resources_tab_helper.h",
"android/brave_feature_list.cc",
"android/brave_relaunch_utils.cc",
"android/brave_shields_content_settings.cc",
Expand Down
123 changes: 123 additions & 0 deletions browser/android/brave_cosmetic_resources_tab_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 3.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/android/brave_cosmetic_resources_tab_helper.h"

#include <memory>
#include <string>
#include <utility>

#include "brave/browser/brave_browser_process_impl.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/ad_block_service_helper.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_isolated_world_ids.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/renderer/render_frame.h"


namespace {
bool ShouldDoCosmeticFiltering(content::WebContents* contents,
const GURL& url) {
Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
auto* map = HostContentSettingsMapFactory::GetForProfile(profile);

return ::brave_shields::ShouldDoCosmeticFiltering(map, url);
}

std::unique_ptr<base::ListValue> GetUrlCosmeticResourcesOnTaskRunner(
const std::string& url) {
auto result_list = std::make_unique<base::ListValue>();

base::Optional<base::Value> resources = g_brave_browser_process->
ad_block_service()->UrlCosmeticResources(url);

if (!resources || !resources->is_dict()) {
return result_list;
}

base::Optional<base::Value> regional_resources = g_brave_browser_process->
ad_block_regional_service_manager()->UrlCosmeticResources(url);

if (regional_resources && regional_resources->is_dict()) {
::brave_shields::MergeResourcesInto(
std::move(*regional_resources), &*resources, /*force_hide=*/false);
}

base::Optional<base::Value> custom_resources = g_brave_browser_process->
ad_block_custom_filters_service()->UrlCosmeticResources(url);

if (custom_resources && custom_resources->is_dict()) {
::brave_shields::MergeResourcesInto(
std::move(*custom_resources), &*resources, /*force_hide=*/true);
}

result_list->Append(std::move(*resources));

return result_list;
}

void GetUrlCosmeticResourcesOnUI(content::RenderFrameHost* render_frame_host,
std::unique_ptr<base::ListValue> resources) {
if (!resources) {
return;
}
for (auto i = resources->GetList().begin();
i < resources->GetList().end(); i++) {
base::DictionaryValue* resources_dict;
if (!i->GetAsDictionary(&resources_dict)) {
continue;
}
std::string to_inject;
resources_dict->GetString("injected_script", &to_inject);
if (to_inject.length() > 1) {
render_frame_host->ExecuteJavaScriptInIsolatedWorld(
base::UTF8ToUTF16(to_inject),
base::NullCallback(), ISOLATED_WORLD_ID_CHROME_INTERNAL);
}
}
}
} // namespace


BraveCosmeticResourcesTabHelper::BraveCosmeticResourcesTabHelper(
content::WebContents* contents)
: WebContentsObserver(contents) {
}

BraveCosmeticResourcesTabHelper::~BraveCosmeticResourcesTabHelper() {
}

void BraveCosmeticResourcesTabHelper::ProcessURL(
content::WebContents* contents,
content::RenderFrameHost* render_frame_host, const GURL& url) {
if (!ShouldDoCosmeticFiltering(contents, url)) {
return;
}
g_brave_browser_process->ad_block_service()->GetTaskRunner()->
PostTaskAndReplyWithResult(FROM_HERE,
base::BindOnce(&GetUrlCosmeticResourcesOnTaskRunner, url.spec()),
base::BindOnce(&GetUrlCosmeticResourcesOnUI, render_frame_host));
}

void BraveCosmeticResourcesTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
ProcessURL(web_contents(), web_contents()->GetMainFrame(),
web_contents()->GetLastCommittedURL());
}

void BraveCosmeticResourcesTabHelper::ResourceLoadComplete(
content::RenderFrameHost* render_frame_host,
const content::GlobalRequestID& request_id,
const blink::mojom::ResourceLoadInfo& resource_load_info) {
ProcessURL(web_contents(), render_frame_host, resource_load_info.final_url);
}

WEB_CONTENTS_USER_DATA_KEY_IMPL(BraveCosmeticResourcesTabHelper)
37 changes: 37 additions & 0 deletions browser/android/brave_cosmetic_resources_tab_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 3.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_BROWSER_ANDROID_BRAVE_COSMETIC_RESOURCES_TAB_HELPER_H_
#define BRAVE_BROWSER_ANDROID_BRAVE_COSMETIC_RESOURCES_TAB_HELPER_H_

#include "base/memory/weak_ptr.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

class BraveCosmeticResourcesTabHelper
: public content::WebContentsObserver,
public content::WebContentsUserData<BraveCosmeticResourcesTabHelper>,
public base::SupportsWeakPtr<BraveCosmeticResourcesTabHelper> {
public:
explicit BraveCosmeticResourcesTabHelper(content::WebContents* contents);
~BraveCosmeticResourcesTabHelper() override;

// content::WebContentsObserver overrides:
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void ResourceLoadComplete(
content::RenderFrameHost* render_frame_host,
const content::GlobalRequestID& request_id,
const blink::mojom::ResourceLoadInfo& resource_load_info) override;

WEB_CONTENTS_USER_DATA_KEY_DECL();

private:
void ProcessURL(content::WebContents* contents,
content::RenderFrameHost* render_frame_host, const GURL& url);
DISALLOW_COPY_AND_ASSIGN(BraveCosmeticResourcesTabHelper);
};

#endif // BRAVE_BROWSER_ANDROID_BRAVE_COSMETIC_RESOURCES_TAB_HELPER_H_
Loading

0 comments on commit 80b4456

Please sign in to comment.