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

cosmetic filters for Android and desktop #6735

Merged
merged 45 commits into from
Jan 22, 2021

Conversation

SergeyZhukovsky
Copy link
Member

@SergeyZhukovsky SergeyZhukovsky commented Sep 30, 2020

Resolves brave/brave-browser#13070

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.

@@ -24,6 +24,109 @@


namespace {
const char k_observing_script[] =
Copy link
Collaborator

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

Copy link
Member Author

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

@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch 2 times, most recently from 105f858 to 90ed5df Compare November 4, 2020 19:03
@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch 2 times, most recently from fc5ba5f to 0282789 Compare November 10, 2020 18:17
@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch from f92c1d7 to 327258c Compare November 24, 2020 15:18
@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch from 2f0b9c0 to 660f733 Compare December 4, 2020 17:54
@SergeyZhukovsky SergeyZhukovsky self-assigned this Dec 7, 2020
@SergeyZhukovsky SergeyZhukovsky added this to the 1.20.x - Nightly milestone Dec 7, 2020
@SergeyZhukovsky SergeyZhukovsky marked this pull request as ready for review December 7, 2020 15:31
@SergeyZhukovsky SergeyZhukovsky requested a review from a team as a code owner December 7, 2020 15:31
@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch from b5da8b8 to 7561c49 Compare December 7, 2020 16:27
@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch 2 times, most recently from 81c8a58 to f5794c4 Compare December 9, 2020 16:08
@iefremov
Copy link
Contributor

@SergeyZhukovsky is it ready for review?

@SergeyZhukovsky
Copy link
Member Author

@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.

renderer/brave_content_renderer_client.cc Outdated Show resolved Hide resolved
browser/extensions/api/brave_shields_api.cc Outdated Show resolved Hide resolved
browser/brave_cosmetic_resources_tab_helper.cc Outdated Show resolved Hide resolved
browser/brave_cosmetic_resources_tab_helper.cc Outdated Show resolved Hide resolved
browser/brave_cosmetic_resources_tab_helper.cc Outdated Show resolved Hide resolved
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Member

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.

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!

const base::android::JavaRef<jobject>& jrender_frame_host_android);
#endif

- ~RenderFrameHost() override {}
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 understand why this patch is here or the one for the constructor. You don't have to initialize a unique_ptr to null

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 is a build error otherwise that class with complex members like added std::unique_ptr should have constructor and destructor implementations in cc file

Copy link
Member Author

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->
Copy link
Collaborator

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?

Copy link
Member Author

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

@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch from 7bf3915 to 2f41ca1 Compare December 16, 2020 15:40
@SergeyZhukovsky SergeyZhukovsky changed the title cosmetic filters render frame observer cosmetic filters for Android and desktop Dec 16, 2020
content::RenderFrame* render_frame,
const int32_t isolated_world_id)
: RenderFrameObserver(render_frame),
worker_isolated_world_id_(isolated_world_id) {}
Copy link
Collaborator

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

Copy link
Member Author

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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing v8 header

Copy link
Member Author

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;
Copy link
Collaborator

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

Copy link
Member Author

Choose a reason for hiding this comment

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

added

@SergeyZhukovsky SergeyZhukovsky force-pushed the cometic_filters_css_rules branch from 19ff267 to e4df7f8 Compare January 21, 2021 20:08
base::BindOnce(HiddenClassIdSelectorsOnTaskRunner, ad_block_service_,
classes, ids, exceptions),
base::BindOnce(&CosmeticFiltersResources::HiddenClassIdSelectorsOnUI,
base::Unretained(this), std::move(callback)));
Copy link
Contributor

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(
Copy link
Contributor

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(
Copy link
Contributor

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.
Copy link
Contributor

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());
Copy link
Contributor

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);
Copy link
Contributor

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;
Copy link
Contributor

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

Copy link
Member Author

@SergeyZhukovsky SergeyZhukovsky Jan 22, 2021

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,
Copy link
Contributor

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)));
Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Contributor

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?

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'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.

Copy link
Contributor

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())
Copy link
Contributor

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(
Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Contributor

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

Comment on lines +847 to +848
R"(function waitCSSSelector() {
if (checkSelector('#ad-banner', 'display', 'none')) {
Copy link
Collaborator

@antonok-edm antonok-edm Jan 27, 2022

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"
Copy link
Collaborator

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 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement cosmetic filters
9 participants