Skip to content

Commit

Permalink
Merge pull request #8239 from /issues/14096
Browse files Browse the repository at this point in the history
get cosmetic filtering rules when we get the url
  • Loading branch information
bridiver authored Mar 17, 2021
2 parents 4c53e12 + 0106a25 commit 7c3d5c9
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "brave/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h"

#include <utility>

#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/no_destructor.h"
Expand Down Expand Up @@ -272,7 +274,9 @@ bool CosmeticFiltersJSHandler::EnsureConnected() {
return cosmetic_filters_resources_.is_bound();
}

void CosmeticFiltersJSHandler::ProcessURL(const GURL& url) {
void CosmeticFiltersJSHandler::ProcessURL(const GURL& url,
base::OnceClosure callback) {
resources_dict_.reset();
url_ = url;
// Trivially, don't make exceptions for malformed URLs.
if (!EnsureConnected() || url_.is_empty() || !url_.is_valid())
Expand All @@ -281,10 +285,11 @@ void CosmeticFiltersJSHandler::ProcessURL(const GURL& url) {
cosmetic_filters_resources_->ShouldDoCosmeticFiltering(
url_.spec(),
base::BindOnce(&CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering,
base::Unretained(this)));
base::Unretained(this), std::move(callback)));
}

void CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering(
base::OnceClosure callback,
bool enabled,
bool first_party_enabled) {
if (!enabled || !EnsureConnected())
Expand All @@ -294,17 +299,24 @@ void CosmeticFiltersJSHandler::OnShouldDoCosmeticFiltering(
cosmetic_filters_resources_->UrlCosmeticResources(
url_.spec(),
base::BindOnce(&CosmeticFiltersJSHandler::OnUrlCosmeticResources,
base::Unretained(this)));
base::Unretained(this), std::move(callback)));
}

void CosmeticFiltersJSHandler::OnUrlCosmeticResources(
base::OnceClosure callback,
base::Value result) {
resources_dict_ = base::DictionaryValue::From(
base::Value::ToUniquePtrValue(std::move(result)));
std::move(callback).Run();
}

void CosmeticFiltersJSHandler::OnUrlCosmeticResources(base::Value result) {
base::DictionaryValue* resources_dict;
void CosmeticFiltersJSHandler::ApplyRules() {
blink::WebLocalFrame* web_frame = render_frame_->GetWebFrame();
if (!result.GetAsDictionary(&resources_dict) || web_frame->IsProvisional())
if (!resources_dict_ || web_frame->IsProvisional())
return;

std::string scriptlet_script;
base::Value* injected_script = resources_dict->FindPath("injected_script");
base::Value* injected_script = resources_dict_->FindPath("injected_script");
if (injected_script &&
base::JSONWriter::Write(*injected_script, &scriptlet_script)) {
scriptlet_script =
Expand All @@ -319,7 +331,7 @@ void CosmeticFiltersJSHandler::OnUrlCosmeticResources(base::Value result) {

// Working on css rules, we do that on a main frame only
bool generichide = false;
resources_dict->GetBoolean("generichide", &generichide);
resources_dict_->GetBoolean("generichide", &generichide);
std::string cosmetic_filtering_init_script = base::StringPrintf(
kCosmeticFilteringInitScript, enabled_1st_party_cf_ ? "true" : "false",
generichide ? "true" : "false");
Expand All @@ -331,7 +343,7 @@ void CosmeticFiltersJSHandler::OnUrlCosmeticResources(base::Value result) {
web_frame->ExecuteScriptInIsolatedWorld(
isolated_world_id_, blink::WebString::FromUTF8(*g_observing_script));

CSSRulesRoutine(resources_dict);
CSSRulesRoutine(resources_dict_.get());
}

void CosmeticFiltersJSHandler::CSSRulesRoutine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRAVE_COMPONENTS_COSMETIC_FILTERS_RENDERER_COSMETIC_FILTERS_JS_HANDLER_H_
#define BRAVE_COMPONENTS_COSMETIC_FILTERS_RENDERER_COSMETIC_FILTERS_JS_HANDLER_H_

#include <memory>
#include <string>
#include <vector>

Expand All @@ -31,7 +32,8 @@ class CosmeticFiltersJSHandler {
// Adds the "cs_worker" JavaScript object and its functions to the current
// render_frame_.
void AddJavaScriptObjectToFrame(v8::Local<v8::Context> context);
void ProcessURL(const GURL& url);
void ProcessURL(const GURL& url, base::OnceClosure callback);
void ApplyRules();

private:
void BindFunctionsToObject(v8::Isolate* isolate,
Expand All @@ -50,8 +52,10 @@ class CosmeticFiltersJSHandler {
// A function to be called from JS
void HiddenClassIdSelectors(const std::string& input);

void OnShouldDoCosmeticFiltering(bool enabled, bool first_party_enabled);
void OnUrlCosmeticResources(base::Value result);
void OnShouldDoCosmeticFiltering(base::OnceClosure callback,
bool enabled,
bool first_party_enabled);
void OnUrlCosmeticResources(base::OnceClosure callback, base::Value result);
void CSSRulesRoutine(base::DictionaryValue* resources_dict);
void OnHiddenClassIdSelectors(base::Value result);

Expand All @@ -62,6 +66,7 @@ class CosmeticFiltersJSHandler {
bool enabled_1st_party_cf_;
std::vector<std::string> exceptions_;
GURL url_;
std::unique_ptr<base::DictionaryValue> resources_dict_;
};

// static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ CosmeticFiltersJsRenderFrameObserver::CosmeticFiltersJsRenderFrameObserver(
content::RenderFrame* render_frame,
const int32_t isolated_world_id)
: RenderFrameObserver(render_frame),
isolated_world_id_(isolated_world_id) {}
RenderFrameObserverTracker<CosmeticFiltersJsRenderFrameObserver>(
render_frame),
isolated_world_id_(isolated_world_id),
native_javascript_handle_(
new CosmeticFiltersJSHandler(render_frame, isolated_world_id)),
ready_(new base::OneShotEvent()) {}

CosmeticFiltersJsRenderFrameObserver::~CosmeticFiltersJsRenderFrameObserver() {}

Expand All @@ -57,29 +62,49 @@ void CosmeticFiltersJsRenderFrameObserver::DidStartNavigation(
url_ = url;
}

void CosmeticFiltersJsRenderFrameObserver::ReadyToCommitNavigation(
blink::WebDocumentLoader* document_loader) {
ready_.reset(new base::OneShotEvent());

// 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 (!url_.SchemeIsHTTPOrHTTPS())
return;

native_javascript_handle_->ProcessURL(
url_, base::BindOnce(&CosmeticFiltersJsRenderFrameObserver::OnProcessURL,
base::Unretained(this)));
}

void CosmeticFiltersJsRenderFrameObserver::RunScriptsAtDocumentStart() {
ready_->Post(FROM_HERE,
base::BindOnce(&CosmeticFiltersJsRenderFrameObserver::ApplyRules,
base::Unretained(this)));
}

void CosmeticFiltersJsRenderFrameObserver::ApplyRules() {
native_javascript_handle_->ApplyRules();
}

void CosmeticFiltersJsRenderFrameObserver::OnProcessURL() {
ready_->Signal();
}

void CosmeticFiltersJsRenderFrameObserver::DidCreateScriptContext(
v8::Local<v8::Context> context,
int32_t world_id) {
if (!render_frame()->IsMainFrame() || world_id != isolated_world_id_ ||
!native_javascript_handle_)
if (!render_frame()->IsMainFrame() || world_id != isolated_world_id_)
return;

native_javascript_handle_->AddJavaScriptObjectToFrame(context);
}

void CosmeticFiltersJsRenderFrameObserver::DidCreateNewDocument() {
// 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_));
EnsureIsolatedWorldInitialized(isolated_world_id_);
}
native_javascript_handle_->ProcessURL(url_);
EnsureIsolatedWorldInitialized(isolated_world_id_);
}

void CosmeticFiltersJsRenderFrameObserver::OnDestruct() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

#include <memory>

#include "base/one_shot_event.h"
#include "base/optional.h"
#include "brave/components/cosmetic_filters/renderer/cosmetic_filters_js_handler.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "third_party/blink/public/web/web_navigation_type.h"
#include "url/gurl.h"
#include "v8/include/v8.h"
Expand All @@ -21,7 +23,9 @@ namespace cosmetic_filters {
// CosmeticFiltersJsRenderFrame observer waits for a page to be loaded and then
// adds the Javascript worker object.
class CosmeticFiltersJsRenderFrameObserver
: public content::RenderFrameObserver {
: public content::RenderFrameObserver,
public content::RenderFrameObserverTracker<
CosmeticFiltersJsRenderFrameObserver> {
public:
CosmeticFiltersJsRenderFrameObserver(content::RenderFrame* render_frame,
const int32_t isolated_world_id);
Expand All @@ -31,11 +35,19 @@ class CosmeticFiltersJsRenderFrameObserver
void DidStartNavigation(
const GURL& url,
base::Optional<blink::WebNavigationType> navigation_type) override;
void ReadyToCommitNavigation(
blink::WebDocumentLoader* document_loader) override;

void DidCreateScriptContext(v8::Local<v8::Context> context,
int32_t world_id) override;
void DidCreateNewDocument() override;

void RunScriptsAtDocumentStart();

private:
void OnProcessURL();
void ApplyRules();

// RenderFrameObserver implementation.
void OnDestruct() override;

Expand All @@ -46,6 +58,8 @@ class CosmeticFiltersJsRenderFrameObserver
std::unique_ptr<CosmeticFiltersJSHandler> native_javascript_handle_;

GURL url_;

std::unique_ptr<base::OneShotEvent> ready_;
};

} // namespace cosmetic_filters
Expand Down
13 changes: 12 additions & 1 deletion renderer/brave_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BraveContentRendererClient::BraveContentRendererClient()
: ChromeContentRendererClient() {}

void BraveContentRendererClient::
SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() {
SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() {
ChromeContentRendererClient::
SetRuntimeFeaturesDefaultsBeforeBlinkInitialization();

Expand Down Expand Up @@ -43,3 +43,14 @@ void BraveContentRendererClient::RenderFrameCreated(
new cosmetic_filters::CosmeticFiltersJsRenderFrameObserver(
render_frame, ISOLATED_WORLD_ID_BRAVE_INTERNAL);
}

void BraveContentRendererClient::RunScriptsAtDocumentStart(
content::RenderFrame* render_frame) {
auto* observer =
cosmetic_filters::CosmeticFiltersJsRenderFrameObserver::Get(render_frame);
// run this before any extensions
if (observer)
observer->RunScriptsAtDocumentStart();

ChromeContentRendererClient::RunScriptsAtDocumentStart(render_frame);
}
1 change: 1 addition & 0 deletions renderer/brave_content_renderer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BraveContentRendererClient : public ChromeContentRendererClient {
~BraveContentRendererClient() override;
void SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() override;
void RenderFrameCreated(content::RenderFrame* render_frame) override;
void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;

private:
DISALLOW_COPY_AND_ASSIGN(BraveContentRendererClient);
Expand Down

0 comments on commit 7c3d5c9

Please sign in to comment.