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

inject scriplets on websites on Android as a part of cosmetic filters #6581

Merged
merged 1 commit into from
Sep 10, 2020

Conversation

SergeyZhukovsky
Copy link
Member

Resolves brave/brave-browser#11599

Submitter Checklist:

Test Plan:

Reviewer Checklist:

  • New files have MPL-2.0 license header.
  • Request a security/privacy review as needed.
  • Adequate test coverage exists to prevent regressions
  • Verify test plan is specified in PR before merging to source

After-merge Checklist:

  • The associated issue milestone is set to the smallest version that the
    changes has landed on.
  • All relevant documentation has been updated.

@SergeyZhukovsky SergeyZhukovsky added CI/skip-linux CI/skip-macos-x64 Do not run CI builds for macOS x64 CI/skip-ios Do not run CI builds for iOS labels Sep 4, 2020
@SergeyZhukovsky SergeyZhukovsky added this to the 1.15.x - Nightly milestone Sep 4, 2020
@SergeyZhukovsky SergeyZhukovsky self-assigned this Sep 4, 2020
browser/android/brave_cosmetic_resources_tab_helper.cc Outdated Show resolved Hide resolved
Comment on lines 40 to 65
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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently moved the regional and custom filters services into the main adblock service; the majority of this function ought to be moved into AdBlockService::UrlCosmeticResources so we can share the same logic on desktop. I can handle this in a follow-up if you'd prefer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it would be good if you can handle, as I'm not certain of what should be done exactly. Should it be just a one call to something instead of regional and custom calls?

Copy link
Collaborator

@antonok-edm antonok-edm Sep 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct - don't worry about it, I'll assign an issue for myself

edit: brave/brave-browser#11600

browser/android/brave_cosmetic_resources_tab_helper.cc Outdated Show resolved Hide resolved
@SergeyZhukovsky
Copy link
Member Author

@antonok-edm I force repushed with requested changes. Re review it please when have a chance.

@SergeyZhukovsky SergeyZhukovsky force-pushed the android_cosmetic_filters_js branch from de5dc15 to 69313f3 Compare September 4, 2020 19:20
@SergeyZhukovsky
Copy link
Member Author

corrected and force pushed

Copy link
Collaborator

@antonok-edm antonok-edm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@SergeyZhukovsky SergeyZhukovsky force-pushed the android_cosmetic_filters_js branch 2 times, most recently from a422b3a to 3bf423c Compare September 4, 2020 21:57
std::string to_inject;
resources_dict->GetString("injected_script", &to_inject);
if (to_inject.length() > 1) {
content::RenderFrameHost::AllowInjectingJavaScript();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering we can call this api freely because upstream doesn't use like us.
I know we've used this api so far. Just curious this is safe. @bridiver ?

  // Globally allows for injecting JavaScript into the main world. This feature
  // is present only to support Android WebView, WebLayer, Fuchsia web.Contexts,
  // and CastOS content shell. It must not be used in other configurations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyZhukovsky since these scripts modify the DOM (which is shared across isolated worlds), can we use ExecuteJavaScriptInIsolatedWorld instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remade it to use ExecuteJavaScriptInIsolatedWorld. @simonhong @bridiver could you please check that?

Copy link
Contributor

@AlexeyBarabash AlexeyBarabash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@SergeyZhukovsky SergeyZhukovsky force-pushed the android_cosmetic_filters_js branch 2 times, most recently from ff34a63 to 96cfe63 Compare September 9, 2020 19:54
@@ -11,6 +11,9 @@
enum BraveIsolatedWorldIDs {
// Isolated world ID for Greaselion (Google Translate reserves END + 1)
ISOLATED_WORLD_ID_GREASELION = content::ISOLATED_WORLD_ID_CONTENT_END + 2,
#if defined(OS_ANDROID)
ISOLATED_WORLD_ID_COSMETIC_SCRIPTS = content::ISOLATED_WORLD_ID_MAX - 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our id managing is broken now. We are using same id with chrome's.
It would be good to fix this also here? :)

enum ChromeIsolatedWorldIDs {
  // Isolated world ID for Chrome Translate.
  ISOLATED_WORLD_ID_TRANSLATE = content::ISOLATED_WORLD_ID_CONTENT_END + 1,

  // Isolated world ID for internal Chrome features.
  ISOLATED_WORLD_ID_CHROME_INTERNAL,

#if defined(OS_MAC)
  // Isolated world ID for AppleScript.
  ISOLATED_WORLD_ID_APPLESCRIPT,
#endif  // defined(OS_MAC)

  // Numbers for isolated worlds for extensions are set in
  // extensions/renderer/script_injection.cc, and are are greater than or equal
  // to this number.
  ISOLATED_WORLD_ID_EXTENSIONS
};

https://source.chromium.org/chromium/chromium/src/+/master:chrome/common/chrome_isolated_world_ids.h

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonhong what do you mean what id do you propose to take? I took the max id - 1, because the latest in the enum you posted ISOLATED_WORLD_ID_EXTENSIONS is actually a first id in the list of unknown size for extensions.

Copy link
Member Author

@SergeyZhukovsky SergeyZhukovsky Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We took it wrong for ISOLATED_WORLD_ID_GREASELION, because it's actually a ISOLATED_WORLD_ID_CHROME_INTERNAL but we don't use it at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think patching to upstream file is one method like below.

enum ChromeIsolatedWorldIDs {
  // Isolated world ID for Chrome Translate.
  ISOLATED_WORLD_ID_TRANSLATE = content::ISOLATED_WORLD_ID_CONTENT_END + 1,

  // Isolated world ID for internal Chrome features.
  ISOLATED_WORLD_ID_CHROME_INTERNAL,

#if defined(OS_MAC)
  // Isolated world ID for AppleScript.
  ISOLATED_WORLD_ID_APPLESCRIPT,
#endif  // defined(OS_MAC)
  BRAVE_ISOLATED_IDS,  // <====== Brave
  // Numbers for isolated worlds for extensions are set in
  // extensions/renderer/script_injection.cc, and are are greater than or equal
  // to this number.
  ISOLATED_WORLD_ID_EXTENSIONS
};

and in chromium_src, BRAVE_ISOLATED_IDS is defined with ours. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that, no problem, but I don't know it's one extra patch, do we need that on this point? We can always add a patch, and every patch is making rebase longer, but in the other hand it's a small file. @bridiver wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are no extensions support on Android

Copy link
Member

@simonhong simonhong Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyZhukovsky Ah!, I forgot about it. Then I think we could use ISOLATED_WORLD_ID_MAX.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll pass this decision to @bridiver .
I think using ISOLATED_WORLD_ID_MAX for android seems fine now.
However, if we want to add ids on desktop in the future, this is not extensible and we should revisit this discussion again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think any of this is necessary including the existing greaselion id. Use ISOLATED_WORLD_ID_CHROME_INTERNAL

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made it to use ISOLATED_WORLD_ID_CHROME_INTERNAL

@@ -231,6 +231,8 @@ source_set("browser_process") {
]
} else {
sources += [
"android/brave_cosmetic_resources_tab_helper.cc",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a lot of deps in brave_cosmetic_resources_tab_helper and I don't see anything added here, but we need to separate these out or it's going to be impossible to manage. Can you please create brave/browser/android/BUILD.gn? We need to stop creating giant targets like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a follow up brave/brave-browser#11653 to move everything to a separate BUILD.gn file brave/brave-browser#11653

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

std::unique_ptr<base::ListValue> GetUrlCosmeticResourcesOnTaskRunner(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @antonok-edm - we need to consolidate this logic

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one step ahead of you 😄
#6581 (comment)

namespace {
bool ShouldDoCosmeticFiltering(content::WebContents* contents,
const GURL& url) {
Profile* profile = static_cast<Profile*>(contents->GetBrowserContext());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Profile::FromBrowserContext

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@SergeyZhukovsky SergeyZhukovsky force-pushed the android_cosmetic_filters_js branch from 96cfe63 to 80b4456 Compare September 10, 2020 15:50
@SergeyZhukovsky SergeyZhukovsky merged commit b6469ee into master Sep 10, 2020
@SergeyZhukovsky SergeyZhukovsky deleted the android_cosmetic_filters_js branch September 10, 2020 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/skip-ios Do not run CI builds for iOS CI/skip-macos-x64 Do not run CI builds for macOS x64
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement cosmetic filters on Android (scriplets injection)
5 participants