-
Notifications
You must be signed in to change notification settings - Fork 909
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
Conversation
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; | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
4def0be
to
de5dc15
Compare
@antonok-edm I force repushed with requested changes. Re review it please when have a chance. |
de5dc15
to
69313f3
Compare
corrected and force pushed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
a422b3a
to
3bf423c
Compare
std::string to_inject; | ||
resources_dict->GetString("injected_script", &to_inject); | ||
if (to_inject.length() > 1) { | ||
content::RenderFrameHost::AllowInjectingJavaScript(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
ff34a63
to
96cfe63
Compare
common/brave_isolated_worlds.h
Outdated
@@ -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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Profile::FromBrowserContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
96cfe63
to
80b4456
Compare
Resolves brave/brave-browser#11599
Submitter Checklist:
npm run lint
)git rebase master
(if needed).git rebase -i
to squash commits (if needed).Test Plan:
Reviewer Checklist:
After-merge Checklist:
changes has landed on.