Skip to content

Commit

Permalink
Removed unnecessary AllowJavascript. base::StartsWith replaced with s…
Browse files Browse the repository at this point in the history
…td::string::starts_with. Added test.
  • Loading branch information
boocmp committed Oct 31, 2024
1 parent 5a96f14 commit 215f8f5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
29 changes: 29 additions & 0 deletions browser/brave_shields/ad_block_custom_resources_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,32 @@ IN_PROC_BROWSER_TEST_F(AdblockCustomResourcesTest, ExecCustomScriptlet) {

EXPECT_EQ("custom-script", EvalJs(web_contents(), "window.test"));
}

IN_PROC_BROWSER_TEST_F(AdblockCustomResourcesTest, NameConflicts) {
constexpr const char kBraveFix[] = "window.test = 'default-script'";
constexpr const char kBraveFixResource[] = R"json(
[{
"name": "brave-fix.js",
"kind": { "mime": "application/javascript" },
"content": "$1"
}]
)json";

UpdateAdBlockResources(base::ReplaceStringPlaceholders(
kBraveFixResource, {base::Base64Encode(kBraveFix)}, nullptr));

NavigateToURL(GURL("brave://settings/shields/filters"));

constexpr const char kContent[] = "window.test = 'custom-script'";

ASSERT_TRUE(ClickAddCustomScriptlet(web_contents()));
SaveCustomScriptlet("brave-fix", kContent);

UpdateAdBlockInstanceWithRules("a.com##+js(brave-fix)");

GURL tab_url =
embedded_test_server()->GetURL("a.com", "/cosmetic_filtering.html");
NavigateToURL(tab_url);

EXPECT_EQ("default-script", EvalJs(web_contents(), "window.test"));
}
1 change: 0 additions & 1 deletion browser/ui/webui/settings/brave_adblock_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ void BraveAdBlockHandler::UpdateCustomScriptlet(const base::Value::List& args) {
void BraveAdBlockHandler::RemoveCustomScriptlet(const base::Value::List& args) {
CHECK(base::FeatureList::IsEnabled(
brave_shields::features::kCosmeticFilteringCustomScriptlets));
AllowJavascript();
CHECK(args.size() == 2u && args[0].is_string() && args[1].is_string());
AllowJavascript();
auto error_code = g_brave_browser_process->ad_block_service()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool IsValidResource(const base::Value& resource) {

if (*mime == kAppJs) {
// Resource is a scriptlet:
if (!base::StartsWith(*name, "brave-") || !base::EndsWith(*name, ".js")) {
if (!name->starts_with("brave-") || !name->ends_with(".js")) {
return false;
}
} else {
Expand Down

0 comments on commit 215f8f5

Please sign in to comment.