-
Notifications
You must be signed in to change notification settings - Fork 871
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
Content picker native inject #25411
Merged
Merged
Content picker native inject #25411
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
fdb12b8
Implement cf_work.addSiteCosmeticFilter()
atuchin-m 64285ed
Drop extension-based menu
atuchin-m 60dbdd2
move files
atuchin-m 90d928b
Fix compilation
atuchin-m 49f52eb
adblock menu items, drop braveShields API
atuchin-m 012e6ea
Fix after rebase
atuchin-m 47c4872
Add TODOs
atuchin-m dcbc69c
Rename files
atuchin-m 97447a1
Add a test
atuchin-m a4471de
Fix console.error
atuchin-m 674eec8
Move the menu
atuchin-m a709fd0
update grd
atuchin-m 0eac315
Fix TODO & presubmit
atuchin-m a713343
Fix and add tests
atuchin-m 0909dcb
fix the last TODO
atuchin-m 6b4e896
minor fixes
atuchin-m 6213856
fix compilation
atuchin-m 3d1da35
Fix grd strings
atuchin-m b7e59b5
Remove cosmeticFilterEvents_test.ts
atuchin-m 5789362
Disable the tests for android
atuchin-m 4642c6f
Fix strings
atuchin-m 425c9a1
Fix menu position
atuchin-m 303a6fc
disable render_view_context_menu_test_util.h for Android
atuchin-m 9be474e
Rework the menu, add a tab helper.
atuchin-m c7c0f9c
Add SanitizeSelector()
atuchin-m a12ca31
Fix android compilation
atuchin-m 00663c9
Move the menu, fix review issues
atuchin-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
browser/cosmetic_filters/cosmetic_filters_tab_helper.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "brave/browser/cosmetic_filters/cosmetic_filters_tab_helper.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/strings/string_util.h" | ||
#include "brave/browser/brave_browser_process.h" | ||
#include "brave/components/brave_shields/content/browser/ad_block_service.h" | ||
#include "content/public/browser/web_contents.h" | ||
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" | ||
|
||
#if !BUILDFLAG(IS_ANDROID) | ||
#include "brave/browser/ui/brave_pages.h" | ||
#include "chrome/browser/ui/browser_finder.h" | ||
#endif // !BUILDFLAG(IS_ANDROID) | ||
|
||
namespace cosmetic_filters { | ||
|
||
namespace { | ||
std::string SanitizeSelector(const std::string selector) { | ||
if (!base::IsStringUTF8(selector)) { | ||
return {}; | ||
} | ||
|
||
// The rules are parsed by adblock-rust via lines() method. | ||
// The method checks a newline byte (the 0xA byte) or CRLF (0xD, 0xA bytes). | ||
// https://doc.rust-lang.org/stable/std/io/trait.BufRead.html#method.lines | ||
if (base::Contains(selector, '\n')) { | ||
return {}; | ||
} | ||
|
||
return selector; | ||
} | ||
} // namespace | ||
|
||
// static | ||
void CosmeticFiltersTabHelper::LaunchContentPicker( | ||
content::WebContents* web_contents) { | ||
CosmeticFiltersTabHelper::CreateForWebContents(web_contents); | ||
if (auto* main_rfh = web_contents->GetPrimaryMainFrame()) { | ||
mojo::AssociatedRemote<mojom::CosmeticFiltersAgent> cosmetic_filter_agent; | ||
main_rfh->GetRemoteAssociatedInterfaces()->GetInterface( | ||
&cosmetic_filter_agent); | ||
cosmetic_filter_agent->LaunchContentPicker(); | ||
} | ||
} | ||
|
||
// static | ||
void CosmeticFiltersTabHelper::BindCosmeticFiltersHandler( | ||
content::RenderFrameHost* rfh, | ||
mojo::PendingAssociatedReceiver<mojom::CosmeticFiltersHandler> receiver) { | ||
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh); | ||
if (!web_contents) { | ||
return; | ||
} | ||
CosmeticFiltersTabHelper::CreateForWebContents(web_contents); | ||
if (auto* tab_helper = | ||
CosmeticFiltersTabHelper::FromWebContents(web_contents)) { | ||
tab_helper->receivers_.Bind(rfh, std::move(receiver)); | ||
} | ||
} | ||
|
||
void CosmeticFiltersTabHelper::AddSiteCosmeticFilter( | ||
const std::string& filter) { | ||
// `filter` doesn't have a host, because we don't trust a renderer process. | ||
// Instead, we calculate and add the host explicitly here. | ||
const auto* sender_rfh = receivers_.GetCurrentTargetFrame(); | ||
CHECK(sender_rfh); | ||
const std::string sanitized_filter = SanitizeSelector(filter); | ||
if (!sanitized_filter.empty()) { | ||
const auto host = sender_rfh->GetLastCommittedOrigin().host(); | ||
g_brave_browser_process->ad_block_service()->AddUserCosmeticFilter( | ||
host + "##" + sanitized_filter); | ||
} | ||
} | ||
|
||
void CosmeticFiltersTabHelper::ManageCustomFilters() { | ||
#if !BUILDFLAG(IS_ANDROID) | ||
Browser* browser = chrome::FindLastActive(); | ||
if (browser) { | ||
brave::ShowBraveAdblock(browser); | ||
} | ||
#else // !BUILDFLAG(IS_ANDROID) | ||
NOTIMPLEMENTED(); | ||
#endif // !BUILDFLAG(IS_ANDROID) | ||
} | ||
|
||
CosmeticFiltersTabHelper::CosmeticFiltersTabHelper( | ||
content::WebContents* web_contents) | ||
: content::WebContentsUserData<CosmeticFiltersTabHelper>(*web_contents), | ||
receivers_(web_contents, this) {} | ||
|
||
CosmeticFiltersTabHelper::~CosmeticFiltersTabHelper() = default; | ||
|
||
WEB_CONTENTS_USER_DATA_KEY_IMPL(CosmeticFiltersTabHelper); | ||
} // namespace cosmetic_filters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_BROWSER_COSMETIC_FILTERS_COSMETIC_FILTERS_TAB_HELPER_H_ | ||
#define BRAVE_BROWSER_COSMETIC_FILTERS_COSMETIC_FILTERS_TAB_HELPER_H_ | ||
|
||
#include <string> | ||
|
||
#include "brave/components/cosmetic_filters/common/cosmetic_filters.mojom.h" | ||
#include "content/public/browser/render_frame_host_receiver_set.h" | ||
#include "content/public/browser/web_contents_user_data.h" | ||
|
||
namespace cosmetic_filters { | ||
// A tab helper to communicate with instances of CosmeticFiltersJSHandler. | ||
// Currently it's created on demand and used for Content Picker feature. | ||
class CosmeticFiltersTabHelper | ||
: public content::WebContentsUserData<CosmeticFiltersTabHelper>, | ||
public mojom::CosmeticFiltersHandler { | ||
public: | ||
CosmeticFiltersTabHelper(const CosmeticFiltersTabHelper&) = delete; | ||
CosmeticFiltersTabHelper& operator=(const CosmeticFiltersTabHelper&) = delete; | ||
~CosmeticFiltersTabHelper() override; | ||
|
||
static void LaunchContentPicker(content::WebContents* web_contents); | ||
|
||
static void BindCosmeticFiltersHandler( | ||
content::RenderFrameHost* rfh, | ||
mojo::PendingAssociatedReceiver<mojom::CosmeticFiltersHandler> receiver); | ||
|
||
private: | ||
void AddSiteCosmeticFilter(const std::string& filter) override; | ||
void ManageCustomFilters() override; | ||
|
||
friend class content::WebContentsUserData<CosmeticFiltersTabHelper>; | ||
|
||
explicit CosmeticFiltersTabHelper(content::WebContents* web_contents); | ||
|
||
content::RenderFrameHostReceiverSet<mojom::CosmeticFiltersHandler> receivers_; | ||
|
||
WEB_CONTENTS_USER_DATA_KEY_DECL(); | ||
}; | ||
} // namespace cosmetic_filters | ||
#endif // BRAVE_BROWSER_COSMETIC_FILTERS_COSMETIC_FILTERS_TAB_HELPER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
brave_browser_cosmetic_filters_sources = [ | ||
"//brave/browser/cosmetic_filters/cosmetic_filters_tab_helper.cc", | ||
"//brave/browser/cosmetic_filters/cosmetic_filters_tab_helper.h", | ||
] | ||
|
||
brave_browser_cosmetic_filters_deps = [ | ||
"//base", | ||
"//brave/browser:browser_process", | ||
"//brave/components/cosmetic_filters/common:mojom", | ||
"//chrome/browser/ui", | ||
"//third_party/blink/public/common", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@fmarier could you take a look?
To address old and new sec issues here I've:
\n
and invalid UTF-8 symbols infilter
.origin
to add the host part to a new rule.