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

Additional Shields menu item to show all blocked elements(same as reset) #27910

Conversation

vadimstruts
Copy link
Collaborator

@vadimstruts vadimstruts commented Mar 3, 2025

Resolves brave/brave-browser#43955

Implemented additional menu item to the shields popup dialog. It appears just in case the current page has custom filters related to the element blocker feature (i.e. some blocked elements)
Blocked elements count is not available yet, as needs adblock engine modifications.

Desktop:

2025-03-03.11-11-35.mp4

Android:

2025-03-03.10-15-46.mp4

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@github-actions github-actions bot added the CI/storybook-url Deploy storybook and provide a unique URL for each build label Mar 3, 2025
@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

@vadimstruts vadimstruts marked this pull request as ready for review March 3, 2025 20:17
@vadimstruts vadimstruts requested review from a team and deeppandya as code owners March 3, 2025 20:17
Copy link
Collaborator

@mkarolin mkarolin left a comment

Choose a reason for hiding this comment

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

strings++

Copy link
Contributor

@deeppandya deeppandya left a comment

Choose a reason for hiding this comment

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

a couple of minor changes. the rest LGTM

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.

nit : year should be 2025

android:layout_height="0dp"
android:layout_weight="1"
android:paddingHorizontal="16dp"
android:paddingTop="16dp"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit : we can use paddingVertical here as paddingTop and paddingBottom have the same values.

@vadimstruts vadimstruts force-pushed the 43955-additional-shields-menu-item-to-show-all-blocked-elementssame-as-reset branch from e6296be to e820257 Compare March 11, 2025 10:58
@vadimstruts vadimstruts force-pushed the 43955-additional-shields-menu-item-to-show-all-blocked-elementssame-as-reset branch from e820257 to 027d1ed Compare March 11, 2025 12:20
@vadimstruts vadimstruts force-pushed the 43955-additional-shields-menu-item-to-show-all-blocked-elementssame-as-reset branch from 027d1ed to aa071c1 Compare March 11, 2025 12:37
GetWebContents().GetController().Reload(content::ReloadType::NORMAL, true);
void CosmeticFiltersTabHelper::ManageCustomFilters() {
#if !BUILDFLAG(IS_ANDROID)
Browser* browser = chrome::FindBrowserWithTab(&GetWebContents());
Copy link
Contributor

Choose a reason for hiding this comment

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

reported by reviewdog 🐶
[semgrep] There are several global functions that facilitate dependency inversion. It will not be possible to call them from modularized features (no dependency cycles), and their usage in non-modularized features is considered a red flag

Don't use Browser*. This is functionally a container of hundreds of other pointers. It is impossible to specify dependencies, since Browser* functionally depends on everything. Instead, pass in the relevant pointers, e.g. Profile*, FooFeatureController, etc

References:
- https://chromium.googlesource.com/chromium/src/+/main/docs/chrome_browser_design_principles.md#structure_modularity


Source: https://github.com/brave/security-action/blob/main/assets/semgrep_rules/client/browser-dependency-inversion.yaml


Cc @goodov @cdesouza-chromium @bridiver

Copy link
Member

Choose a reason for hiding this comment

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

cc @bridiver I think the wording can be improved to mention valid use cases when we use Browser* as a "window" object, i.e. we have some functionality bound to the browser window. We shouldn't convert such scenarios to Profile* as it will lose the connection to the exact window.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But we're not supposed to have functionality tied to Browser anymore. This wording is pulled directly from https://source.chromium.org/chromium/chromium/src/+/main:docs/chrome_browser_design_principles.md;l=1?q=design_principles.&ss=chromium

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 in particular it's bad to have a dependency on Browser here in the tab helper. I'm on my phone right now so I can't see what this helper is doing internally, but at the very least we should pass the webcontents and keep the Browser dependency inside the helper. It's possible we should not be using Browser here at all, but I can't say for sure until I get home and see the details

Copy link
Collaborator

@bridiver bridiver Mar 11, 2025

Choose a reason for hiding this comment

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

Also FindBrowserWithTab should have been flagged here. Does it only catch one per line @thypon ?

Copy link
Collaborator

@bridiver bridiver Mar 11, 2025

Choose a reason for hiding this comment

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

this appears to be the only usage of ShowBraveAdblock so we should change it to use

void ShowSingletonTabOverwritingNTP(
    Profile* profile,
    const GURL& url,
    NavigateParams::PathBehavior path_behavior)

instead of the Browser version

Copy link
Member

@goodov goodov Mar 12, 2025

Choose a reason for hiding this comment

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

this appears to be the only usage of ShowBraveAdblock so we should change it to use

void ShowSingletonTabOverwritingNTP(
    Profile* profile,
    const GURL& url,
    NavigateParams::PathBehavior path_behavior)

instead of the Browser version

It doesn't make sense to create a tab using a Profile* instance when you're acting from a specific tab that is bound to a specific Browser* instance.

I think technically we should follow this example here:

// Do not do this:
FooTabFeature {
  // As per (1) above, we shouldn't be passing or storing Browser* anyways.
  // Another common anti-pattern is to dynamically look up Browser* via
  // browser_finder methods. These often result in the wrong Browser*
  Browser* browser_;

  // This will point to the wrong BrowserView if the tab is dragged into a new
  // window. Furthermore, BrowserView* itself is a container of hundreds of
  // other views. The tab-scoped feature typically wants something much more
  // specific.
  BrowserView* browser_view_;

  // Extensions are profile-scoped, and thus any state/logic should be in a
  // ProfileKeyedService. If the user has multiple tabs (possibly across
  // multiple windows) simultaneously interacting with FooTabFeature, then it's
  // likely that the extension will uninstall while it's still in use.
  void InstallExtension();
  void UninstallExtension();
};

// Instead do this:
FooTabFeature {
  // Guaranteed to remain valid for the lifetime of this class. This can be used
  // to dynamically access relevant window state via
  // tab_->GetBrowserWindowInterface()->GetFeatures().GetFooWindowFeature()
  TabInterface* tab_;
};

So ShowSingletonTabOverwritingNTP should be a part of GetBrowserWindowInterface or something like that.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't want to change the function signature atm we can use smth like:
TabInterface::MaybeGetFromContents(contents)->GetBrowserWindowInterface()->GetBrowserForMigrationOnly()
note: don't forget to check pointers on every step.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In context of mentioned approach we could change the function ShowBraveAdblock to look like:

void ShowBraveAdblock(BrowserWindowInterface* browser_window_interface) {
if(!browser_window_interface) {
  return;
}
content::OpenURLParams params(GURL(kBraveUIAdblockURL)), content::Referrer(),
                                WindowOpenDisposition::SINGLETON_TAB,
                                ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false);
browser_window_interface->OpenURL(params, {});
}

It does the same like the current one, doesn't require Browser dependency, and doesn't use the "GetBrowserForMigrationOnly"
WDYT?
@goodov, @bridiver, @boocmp

Copy link
Member

Choose a reason for hiding this comment

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

This looks like a separate task to refactor these functions all at once.

Signed-off-by: Vadym Struts <[email protected]>
@vadimstruts vadimstruts requested a review from a team as a code owner March 11, 2025 18:40
Signed-off-by: Vadym Struts <[email protected]>
@@ -24,8 +24,8 @@ void ShowBraveRewards(Browser* browser) {
ShowSingletonTabOverwritingNTP(browser, GURL(kRewardsPageURL));
}

void ShowBraveAdblock(Browser* browser) {
ShowSingletonTabOverwritingNTP(browser, GURL(kBraveUIAdblockURL));
void ShowBraveAdblock(Profile* profile) {
Copy link
Member

Choose a reason for hiding this comment

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

@bridiver all these functions use Browser* to display the requested page in the exact browser window, NOT in the last active window. Passing Profile* here will switch those function to look for the last active browser window and load the requested page there instead of the explicit browser window the caller asks for.

Signed-off-by: Vadym Struts <[email protected]>
@vadimstruts vadimstruts merged commit b3e162d into master Mar 13, 2025
18 checks passed
@vadimstruts vadimstruts deleted the 43955-additional-shields-menu-item-to-show-all-blocked-elementssame-as-reset branch March 13, 2025 14:55
@github-actions github-actions bot added this to the 1.78.x - Nightly milestone Mar 13, 2025
@brave-builds
Copy link
Collaborator

Released in v1.78.41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Additional Shields menu item to show all blocked elements(same as reset)
9 participants