Skip to content

Commit

Permalink
Merge pull request #8081 from brave/adoptedstylesheets
Browse files Browse the repository at this point in the history
don't override website's document.adoptedStyleSheets
  • Loading branch information
SergeyZhukovsky authored Feb 27, 2021
2 parents 671ab15 + 5b9668c commit c83eb4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
6 changes: 3 additions & 3 deletions chromium_src/chrome/common/chrome_isolated_world_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_
#define BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_
#ifndef BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_H_
#define BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_H_

#define ISOLATED_WORLD_ID_CHROME_INTERNAL \
ISOLATED_WORLD_ID_CHROME_INTERNAL, ISOLATED_WORLD_ID_BRAVE_INTERNAL
Expand All @@ -13,4 +13,4 @@

#undef ISOLATED_WORLD_ID_CHROME_INTERNAL

#endif // BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_
#endif // BRAVE_CHROMIUM_SRC_CHROME_COMMON_CHROME_ISOLATED_WORLD_IDS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ const char kHideSelectorsInjectScript[] =
if (!document.adoptedStyleSheets.includes(
window.content_cosmetic.cosmeticStyleSheet)) {
document.adoptedStyleSheets =
[window.content_cosmetic.cosmeticStyleSheet];
[window.content_cosmetic.cosmeticStyleSheet,
...document.adoptedStyleSheets];
};
})();)";

Expand All @@ -112,7 +113,8 @@ const char kForceHideSelectorsInjectScript[] =
if (!document.adoptedStyleSheets.includes(
window.content_cosmetic.cosmeticStyleSheet)) {
document.adoptedStyleSheets =
[window.content_cosmetic.cosmeticStyleSheet];
[window.content_cosmetic.cosmeticStyleSheet,
...document.adoptedStyleSheets];
};
})();)";

Expand Down Expand Up @@ -144,7 +146,8 @@ const char kStyleSelectorsInjectScript[] =
if (!document.adoptedStyleSheets.includes(
window.content_cosmetic.cosmeticStyleSheet)){
document.adoptedStyleSheets =
[window.content_cosmetic.cosmeticStyleSheet];
[window.content_cosmetic.cosmeticStyleSheet,
...document.adoptedStyleSheets];
};
})();)";

Expand Down Expand Up @@ -270,10 +273,11 @@ bool CosmeticFiltersJSHandler::EnsureConnected() {
}

void CosmeticFiltersJSHandler::ProcessURL(const GURL& url) {
if (!EnsureConnected())
url_ = url;
// Trivially, don't make exceptions for malformed URLs.
if (!EnsureConnected() || url_.is_empty() || !url_.is_valid())
return;

url_ = url;
cosmetic_filters_resources_->ShouldDoCosmeticFiltering(
url_.spec(),
base::BindOnce(&CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering,
Expand Down Expand Up @@ -329,10 +333,6 @@ void CosmeticFiltersJSHandler::OnUrlCosmeticResources(base::Value result) {

void CosmeticFiltersJSHandler::CSSRulesRoutine(
base::DictionaryValue* resources_dict) {
// Trivially, don't make exceptions for malformed URLs.
if (url_.is_empty() || !url_.is_valid())
return;

// Otherwise, if its a vetted engine AND we're not in aggressive
// mode, also don't do cosmetic filtering.
if (!enabled_1st_party_cf_ && IsVettedSearchEngine(url_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace cosmetic_filters {

namespace {

const char kSecurityOrigin[] =
"chrome-extension://mnojpmjdmbbfmejpflffifhffcmidifd";
const char kSecurityOrigin[] = "chrome://cosmetic_filters";

void EnsureIsolatedWorldInitialized(int world_id) {
static base::Optional<int> last_used_world_id;
Expand Down Expand Up @@ -69,15 +68,12 @@ void CosmeticFiltersJsRenderFrameObserver::DidCreateScriptContext(
}

void CosmeticFiltersJsRenderFrameObserver::DidCreateNewDocument() {
// There could be empty and "about:blank" URLs, empty URLs are duplicated
// with DidCreateDocumentElement, so we just skip them, "about:blank"
// should fallback to the main frame rules
if (url_.is_empty())
return;
if (url_.spec() == "about:blank") {
// There could be empty, invalid and "about:blank" URLs,
// they should fallback to the main frame rules
if (url_.is_empty() || !url_.is_valid() || url_.spec() == "about:blank")
url_ = url::Origin(render_frame()->GetWebFrame()->GetSecurityOrigin())
.GetURL();
}

if (!native_javascript_handle_) {
native_javascript_handle_.reset(
new CosmeticFiltersJSHandler(render_frame(), isolated_world_id_));
Expand Down

0 comments on commit c83eb4a

Please sign in to comment.