-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11374bf
commit 5d3edde
Showing
37 changed files
with
470 additions
and
367 deletions.
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.