-
Notifications
You must be signed in to change notification settings - Fork 900
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
cosmetic filters for Android and desktop #6735
Conversation
@@ -24,6 +24,109 @@ | |||
|
|||
|
|||
namespace { | |||
const char k_observing_script[] = |
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 we'll probably want a transpile_web_ui
target for just content_cosmetic.ts
and put the result in a grd file to isolate it from everything else in brave_extension
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.
it's done via transpile now
105f858
to
90ed5df
Compare
fc5ba5f
to
0282789
Compare
f92c1d7
to
327258c
Compare
2f0b9c0
to
660f733
Compare
b5da8b8
to
7561c49
Compare
components/brave_extension/extension/brave_extension/background/api/cosmeticFilterAPI.ts
Outdated
Show resolved
Hide resolved
81c8a58
to
f5794c4
Compare
@SergeyZhukovsky is it ready for review? |
@iefremov yes, basically. 3 tests are failing, I'm replacing them as they use old functionality that is not there anymore. Other than that I'm happy to hear a feedback. |
@@ -0,0 +1,51 @@ | |||
/* This Source Code Form is subject to the terms of the Mozilla Public |
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.
can we squash this commit with the commit that removes the file? Otherwise we'll lose file history.
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!
const base::android::JavaRef<jobject>& jrender_frame_host_android); | ||
#endif | ||
|
||
- ~RenderFrameHost() override {} |
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 understand why this patch is here or the one for the constructor. You don't have to initialize a unique_ptr to null
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 is a build error otherwise that class with complex members like added std::unique_ptr
should have constructor and destructor implementations in cc file
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.
all patches are removed
@@ -66,31 +66,31 @@ BraveShieldsUrlCosmeticResourcesFunction::Run() { | |||
|
|||
std::unique_ptr<base::ListValue> BraveShieldsUrlCosmeticResourcesFunction:: | |||
GetUrlCosmeticResourcesOnTaskRunner(const std::string& url) { | |||
base::Optional<base::Value> resources = g_brave_browser_process-> | |||
ad_block_service()->UrlCosmeticResources(url); | |||
// base::Optional<base::Value> resources = g_brave_browser_process-> |
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.
why are these all commented out?
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.
as @antonok-edm pointed out above, we don't need that anymore as we want to get rid of the extension
7bf3915
to
2f41ca1
Compare
content::RenderFrame* render_frame, | ||
const int32_t isolated_world_id) | ||
: RenderFrameObserver(render_frame), | ||
worker_isolated_world_id_(isolated_world_id) {} |
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.
same as above worker_
doesn't really make sense here
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.
corrected
} | ||
|
||
void CosmeticFiltersJsRenderFrameObserver::DidCreateScriptContext( | ||
v8::Local<v8::Context> context, |
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.
missing v8 header
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.
it is in the corresponding header file
// RenderFrameObserver implementation. | ||
void DidStartNavigation( | ||
const GURL& url, | ||
base::Optional<blink::WebNavigationType> navigation_type) override; |
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.
missing base/optional and blink headers
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.
added
19ff267
to
e4df7f8
Compare
base::BindOnce(HiddenClassIdSelectorsOnTaskRunner, ad_block_service_, | ||
classes, ids, exceptions), | ||
base::BindOnce(&CosmeticFiltersResources::HiddenClassIdSelectorsOnUI, | ||
base::Unretained(this), std::move(callback))); |
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.
you need weak pointer here and below
|
||
namespace { | ||
|
||
base::Optional<base::Value> HiddenClassIdSelectorsOnTaskRunner( |
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.
basically you could post brave_shields::AdBlockService::HiddenClassIdSelectors
without writing these free functions
public: | ||
CosmeticFiltersResources(const CosmeticFiltersResources&) = delete; | ||
CosmeticFiltersResources& operator=(const CosmeticFiltersResources&) = delete; | ||
explicit CosmeticFiltersResources( |
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.
nit: not explicit
@@ -0,0 +1,68 @@ | |||
/* Copyright (c) 2020 The Brave Authors. All rights reserved. |
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.
2021 :)
void CosmeticFiltersResources::UrlCosmeticResourcesOnUI( | ||
UrlCosmeticResourcesCallback callback, | ||
base::Optional<base::Value> resources) { | ||
std::move(callback).Run(resources ? resources->Clone() : base::Value()); |
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.
Clone()
is expensive, why not just move?
const std::string& input, | ||
const std::vector<std::string>& exceptions, | ||
HiddenClassIdSelectorsCallback callback) { | ||
base::Optional<base::Value> input_value = base::JSONReader::Read(input); |
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'd rather do all this stuff in the function that works on task runner, not a blocker though
void CosmeticFiltersJsRenderFrameObserver::DidStartNavigation( | ||
const GURL& url, | ||
base::Optional<blink::WebNavigationType> navigation_type) { | ||
url_ = url; |
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.
not sure why do we need this method, can we just extract render_frame()->GetWebFrame()->GetDocument().Url()
or something in DidCreateNewDocument()
?
Basically wondering whether DidStartNavigation()
is called for redirects, otherwise we can cache a wrong url
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.
it is called for redirects. This is basically done as DOM distiller does at the RenderFrameObserver, they get URL from this method as well
void DistillerJsRenderFrameObserver::DidStartNavigation(
const GURL& url,
base::Optional<blink::WebNavigationType> navigation_type) {
is_distiller_page_ = url_utils::IsDistilledPage(url);
}
|
||
class CosmeticFiltersJSHandler { | ||
public: | ||
explicit CosmeticFiltersJSHandler(content::RenderFrame* render_frame, |
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.
nit - explicit is need only for constructors that have a single argument
cosmetic_filters_resources_->ShouldDoCosmeticFiltering( | ||
url_.spec(), | ||
base::BindOnce(&CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering, | ||
base::Unretained(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.
we need weak pointers, this object can die long before the callback is invoked, we are in a different process
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.
that object owns the mojo receiver, the receiver will be dead if the object is dead and nothing will be invoked. Not needed WeakPtr in that case
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.
CosmeticFilterResources
posts to TaskRunner, so the Receiver and its owner could die before adblocker replies back?
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 not following how adblocker is involved here. cosmetic_filters_resources_ is a member of CosmeticFiltersJSHandler and a mojo receiver, how the receiver can pass result back to it's owner when owner is dead. Receiver will also be dead as it's owner is.
CosmeticFiltersJSHandler owns the mojo remote if the mojo remote is destroyed then the callback will never happen.
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.
Ah, sorry, you are right. I've reread the documentation and apparently we are just fine
cosmetic_filters_resources_.BindNewPipeAndPassReceiver()); | ||
} | ||
|
||
if (cosmetic_filters_resources_.is_bound()) |
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.
return cosmetic_filters_resources_.is_bound();
base::Unretained(this))); | ||
} | ||
|
||
void CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering( |
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.
do we need this hop (aka OnShouldDoCosmeticFiltering
)? I. e. can we just check !enabled
on the browser side and immediately do UrlCosmeticResources
, then callback to OnUrlCosmeticResources
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've discussed that with @bridiver and came to the conclusion to use it like that. The problem with the solution you propose is that we need one more boolean here enabled_1st_party_cf_filtering_
and we need to store it to use in CosmeticFiltersJSHandler
calls. We could potentially return it with OnUrlCosmeticResources
as well, but we've decided to use it like it is right now.
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.
okay, nothing critical, but I think it worth doing at some point
R"(function waitCSSSelector() { | ||
if (checkSelector('#ad-banner', 'display', 'none')) { |
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.
In most of these tests, checkSelector
returns a Promise
(which evaluates to true
in an if
statement)... so these tests are not actually doing anything 😦
It should be like this:
(async function waitCSSSelector() {
if (await checkSelector('#ad-banner', 'display', 'none')) {
// ...
(Filed brave/brave-browser#20720)
@@ -902,12 +938,29 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) { | |||
content::WebContents* contents = | |||
browser()->tab_strip_model()->GetActiveWebContents(); | |||
|
|||
ASSERT_EQ(true, EvalJs(contents, | |||
"addElementsDynamically();\n" |
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.
^ this addElementsDynamically()
was also important 😅
Resolves brave/brave-browser#13070
Submitter Checklist:
npm run lint
,npm run gn_check
)git rebase master
(if needed).git rebase -i
to squash commits (if needed).Test Plan:
Reviewer Checklist:
After-merge Checklist:
changes has landed on.