Skip to content

Commit

Permalink
Fix cosmetic_filters_js_handler.cc after revert
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Apr 11, 2022
1 parent 8c3856a commit ad53d05
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
15 changes: 5 additions & 10 deletions browser/brave_shields/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1904,12 +1904,7 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringHide1pContent) {
}

// Test cosmetic filtering on elements added dynamically
<<<<<<< HEAD
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
=======
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, DISABLED_CosmeticFilteringDynamic) {
>>>>>>> parent of bc8e4c25e6 (Merge pull request #12038 from brave/cosmetic-filtering-fixes)
UpdateAdBlockInstanceWithRules("##.blockme");

GURL tab_url =
Expand Down Expand Up @@ -1963,7 +1958,8 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamicCustom) {
browser()->tab_strip_model()->GetActiveWebContents();

auto result_first = EvalJs(contents,
R"(async function waitCSSSelector() {
R"(addElementsDynamically();
async function waitCSSSelector() {
if (await checkSelector('.blockme', 'display', 'none')) {
window.domAutomationController.send(true);
} else {
Expand Down Expand Up @@ -2158,11 +2154,12 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringIframeScriptlet) {
ASSERT_EQ(true, EvalJs(contents, "show_ad"));
}

<<<<<<< HEAD
// Test cosmetic filtering on an element that already has an `!important`
// marker on its `display` style.
// Temporarily disabled by https://github.com/brave/brave-core/pull/12950 due
// to performance impact of newer injection method.
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest,
CosmeticFilteringOverridesImportant) {
DISABLED_CosmeticFilteringOverridesImportant) {
ASSERT_TRUE(InstallDefaultAdBlockExtension());
UpdateAdBlockInstanceWithRules("###inline-block-important");

Expand All @@ -2187,8 +2184,6 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest,
EXPECT_EQ(base::Value(true), result_first.value);
}

=======
>>>>>>> parent of bc8e4c25e6 (Merge pull request #12038 from brave/cosmetic-filtering-fixes)
class DefaultCookieListFlagEnabledTest : public AdBlockServiceTest {
public:
DefaultCookieListFlagEnabledTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ void CosmeticFiltersJSHandler::OnHiddenClassIdSelectors(base::Value result) {
return;
}

blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();

DCHECK(result.is_dict());

base::Value* hide_selectors = result.FindListKey("hide_selectors");
Expand All @@ -489,20 +491,27 @@ void CosmeticFiltersJSHandler::OnHiddenClassIdSelectors(base::Value result) {
DCHECK(force_hide_selectors);

if (force_hide_selectors->GetList().size() != 0) {
std::string stylesheet = "";
for (auto& selector : force_hide_selectors->GetList()) {
DCHECK(selector.is_string());
stylesheet += selector.GetString() + "{display:none !important}";
std::string json_selectors;
if (!base::JSONWriter::Write(force_hide_selectors->GetList(),
&json_selectors) ||
json_selectors.empty()) {
json_selectors = "[]";
}
InjectStylesheet(stylesheet, 0);
// Building a script for stylesheet modifications
std::string new_selectors_script = base::StringPrintf(
kForceHideSelectorsInjectScript, json_selectors.c_str());
web_frame->ExecuteScriptInIsolatedWorld(
isolated_world_id_,
blink::WebScriptSource(
blink::WebString::FromUTF8(new_selectors_script)),
blink::BackForwardCacheAware::kAllow);
}

// If its a vetted engine AND we're not in aggressive
// mode, don't check elements from the default engine (in hide_selectors).
if (!enabled_1st_party_cf_ && IsVettedSearchEngine(url_))
return;

blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
std::string json_selectors;
if (!base::JSONWriter::Write(*hide_selectors, &json_selectors) ||
json_selectors.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,7 @@ class CosmeticFiltersJSHandler {
void OnHiddenClassIdSelectors(base::Value result);
bool OnIsFirstParty(const std::string& url_string);

<<<<<<< HEAD
void InjectStylesheet(const std::string& stylesheet, int id);
void UninjectStylesheet(int id);

bool generichide_ = false;
std::map<int, std::unique_ptr<blink::WebString>> inserted_stylesheet_ids;

=======
>>>>>>> parent of bc8e4c25e6 (Merge pull request #12038 from brave/cosmetic-filtering-fixes)
raw_ptr<content::RenderFrame> render_frame_ = nullptr;
mojo::Remote<cosmetic_filters::mojom::CosmeticFiltersResources>
cosmetic_filters_resources_;
Expand Down
7 changes: 4 additions & 3 deletions test/data/cosmetic_filtering.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
<body>
<div id="ad-banner"><img src="https://example.com/logo.png" alt=""></div>
<div class="ad-banner">
<div class="ad" style="background: url(example.com)"><img src="https://example.com/logo.png" alt=""></div>
<div class="ad" style="background: url(https://example.com)"><img src="https://example.com/logo.png" alt=""></div>
</div>
<div class="ad" style="background: url(example.com)"><img src="https://example.com/logo.png" alt=""></div>
<div class="ad" style="background: url(example.com)"><img src="https://example.com/logo.png" alt=""></div>
<div class="ad" style="background: url(https://example.com)"><img src="https://example.com/logo.png" alt=""></div>
<div class="ad" style="background: url(https://example.com)"><img src="https://example.com/logo.png" alt=""></div>

<!-- Assume that the document host is test.lion.appspot.com (see AdBlockServiceTest.*) -->
<div id="relative-url-div" class="fpsponsored">
Expand All @@ -77,5 +77,6 @@
A text of 30 chars and 5 words is needed here to consider element significant.
<img src="http://chrome.appspot.com/sponsored/640x820.png">
</div>
<div id="inline-block-important" style="display: block !important"></div>
</body>
</html>

0 comments on commit ad53d05

Please sign in to comment.