Skip to content

Commit

Permalink
Add JS blocking details
Browse files Browse the repository at this point in the history
Fix #38
  • Loading branch information
bbondy committed Dec 28, 2017
1 parent d6d2b1c commit 1a5b750
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 21 deletions.
3 changes: 2 additions & 1 deletion BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ group("child_dependencies") {
public_deps = [
"//brave/renderer",
"//brave/utility",
"common"
]
}

group("browser_dependencies") {
public_deps = [
"//brave/browser",
"//brave/common/extensions/api"
"common",
]
}

Expand Down
4 changes: 3 additions & 1 deletion browser/net/brave_httpse_network_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "brave/browser/net/url_context.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "brave/components/brave_shields/browser/shield_types.h"
#include "content/public/browser/browser_thread.h"
#include "net/url_request/url_request.h"

Expand Down Expand Up @@ -38,7 +39,8 @@ void OnBeforeURLRequest_HttpsePostFileWork(
if (!ctx->new_url_spec.empty() &&
ctx->new_url_spec != request->url().spec()) {
*new_url = GURL(ctx->new_url_spec);
brave_shields::DispatchBlockedEventFromIO(request, "httpsEverywhere");
brave_shields::DispatchBlockedEventFromIO(request,
brave_shields::kHttpsEverywhere);
}

next_callback.Run();
Expand Down
9 changes: 9 additions & 0 deletions common/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source_set("common") {
sources = [
"common_message_generator.cc",
"common_message_generator.h",
]
deps = [
"extensions/api"
]
}
35 changes: 35 additions & 0 deletions common/common_message_generator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* 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 http://mozilla.org/MPL/2.0/. */

// Get basic type definitions.
#define IPC_MESSAGE_IMPL

#include "brave/common/common_message_generator.h"

// Generate constructors.
#include "ipc/struct_constructor_macros.h"
#include "brave/common/common_message_generator.h"

// Generate destructors.
#include "ipc/struct_destructor_macros.h"
#include "brave/common/common_message_generator.h"

// Generate param traits write methods.
#include "ipc/param_traits_write_macros.h"
namespace IPC {
#include "brave/common/common_message_generator.h"
} // namespace IPC

// Generate param traits read methods.
#include "ipc/param_traits_read_macros.h"
namespace IPC {
#include "brave/common/common_message_generator.h"
} // namespace IPC

// Generate param traits log methods.
#include "ipc/param_traits_log_macros.h"
namespace IPC {
#include "brave/common/common_message_generator.h"
} // namespace IPC

2 changes: 2 additions & 0 deletions common/common_message_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Multiply-included file, no traditional include guard.
#include "brave/common/render_messages.h"
3 changes: 2 additions & 1 deletion common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"name": "details",
"properties": {
"tabId": {"type": "integer", "description": "The ID of the tab in which the action occurs."},
"blockType": {"type": "string", "description": "\"adBlock\" or \"trackingProtection\"."}
"blockType": {"type": "string", "description": "\"adBlock\" or \"trackingProtection\"."},
"subresource": {"type": "string", "description": "The URL of the subresource in question."}
}
}
]
Expand Down
18 changes: 18 additions & 0 deletions common/render_messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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 http://mozilla.org/MPL/2.0/. */

// Multiply-included file, no traditional include guard.

#include "base/strings/string16.h"
#include "ipc/ipc_message_macros.h"

// The message starter should be declared in ipc/ipc_message_start.h. Since
// we don't want to patch Chromium, we just pretend to be Content Shell.

#define IPC_MESSAGE_START ShellMsgStart

// Tells the browser that content in the current page was blocked due to the
// user's content settings.
IPC_MESSAGE_ROUTED1(BraveViewHostMsg_JavaScriptBlocked,
base::string16 /* details on blocked content */)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/shield_types.h"
#include "brave/components/brave_shields/browser/tracking_protection_service.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "components/content_settings/core/common/content_settings_types.h"
Expand Down Expand Up @@ -45,12 +46,14 @@ void BraveShieldsResourceThrottle::WillStartRequest(bool* defer) {
!g_brave_browser_process->tracking_protection_service()->
ShouldStartRequest(request_->url(), resource_type_, tab_origin.host())) {
Cancel();
brave_shields::DispatchBlockedEventFromIO(request_, "adBlock");
brave_shields::DispatchBlockedEventFromIO(request_,
brave_shields::kAdBlock);
}
if (allow_tracking_protection &&
!g_brave_browser_process->ad_block_service()->
ShouldStartRequest(request_->url(), resource_type_, tab_origin.host())) {
Cancel();
brave_shields::DispatchBlockedEventFromIO(request_, "trackingProtection");
brave_shields::DispatchBlockedEventFromIO(request_,
brave_shields::kTrackingProtection);
}
}
3 changes: 2 additions & 1 deletion components/brave_shields/browser/brave_shields_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void DispatchBlockedEventFromIO(URLRequest* request,
&frame_tree_node_id);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::BindOnce(&BraveShieldsWebContentsObserver::DispatchBlockedEvent,
block_type, render_process_id, render_frame_id, frame_tree_node_id));
block_type, request->url().spec(),
render_process_id, render_frame_id, frame_tree_node_id));
}

} // namespace brave_shields
2 changes: 1 addition & 1 deletion components/brave_shields/browser/brave_shields_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool IsAllowContentSettingFromIO(net::URLRequest* request,
const std::string& resource_identifier);

void DispatchBlockedEventFromIO(net::URLRequest* request,
const std::string &block_type);
const std::string& block_type);

} // namespace brave_shields

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,51 @@

#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "brave/common/extensions/api/brave_shields.h"
#include "brave/components/brave_shields/browser/shield_types.h"
#include "brave/common/render_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "ipc/ipc_message_macros.h"


using extensions::Event;
using extensions::EventRouter;
using content::RenderFrameHost;

DEFINE_WEB_CONTENTS_USER_DATA_KEY(
brave_shields::BraveShieldsWebContentsObserver);

namespace {

content::WebContents* GetWebContents(
int render_process_id,
int render_frame_id,
int frame_tree_node_id) {
content::WebContents* web_contents =
content::WebContents::FromFrameTreeNodeId(frame_tree_node_id);
if (!web_contents) {
RenderFrameHost* rfh =
RenderFrameHost::FromID(render_process_id, render_frame_id);
if (!rfh) {
return nullptr;
}
web_contents =
content::WebContents::FromRenderFrameHost(rfh);
}
return web_contents;
}

} // namespace


namespace brave_shields {

BraveShieldsWebContentsObserver::~BraveShieldsWebContentsObserver() {
Expand All @@ -32,38 +60,39 @@ BraveShieldsWebContentsObserver::BraveShieldsWebContentsObserver(
}

void BraveShieldsWebContentsObserver::RenderFrameCreated(
content::RenderFrameHost* host) {
RenderFrameHost* host) {
// Look up the extension API frame ID to force the mapping to be cached.
// This is needed so that cached information is available for tabId.
extensions::ExtensionApiFrameIdMap::Get()->CacheFrameData(host);
}

void BraveShieldsWebContentsObserver::DispatchBlockedEvent(
const std::string& block_type,
const std::string& subresource,
int render_process_id,
int render_frame_id,
int frame_tree_node_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

content::WebContents* web_contents =
content::WebContents::FromFrameTreeNodeId(frame_tree_node_id);
content::WebContents* web_contents = GetWebContents(render_process_id,
render_frame_id, frame_tree_node_id);
DispatchBlockedEventForWebContents(block_type, subresource, web_contents);
}

void BraveShieldsWebContentsObserver::DispatchBlockedEventForWebContents(
const std::string& block_type, const std::string& subresource,
content::WebContents* web_contents) {
if (!web_contents) {
content::RenderFrameHost* rfh =
content::RenderFrameHost::FromID(render_process_id, render_frame_id);
if (!rfh) {
return;
}
web_contents =
content::WebContents::FromRenderFrameHost(rfh);
return;
}

Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
EventRouter* event_router = EventRouter::Get(profile);
if (profile && event_router) {
extensions::api::brave_shields::OnBlocked::Details details;
details.tab_id = extensions::ExtensionTabUtil::GetTabId(web_contents);
details.block_type = block_type;
details.subresource = subresource;
std::unique_ptr<base::ListValue> args(
extensions::api::brave_shields::OnBlocked::Create(details)
.release());
Expand All @@ -75,4 +104,28 @@ void BraveShieldsWebContentsObserver::DispatchBlockedEvent(
}
}

bool BraveShieldsWebContentsObserver::OnMessageReceived(
const IPC::Message& message, RenderFrameHost* render_frame_host) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(BraveShieldsWebContentsObserver,
message, render_frame_host)
IPC_MESSAGE_HANDLER(BraveViewHostMsg_JavaScriptBlocked,
OnJavaScriptBlockedWithDetail)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}

void BraveShieldsWebContentsObserver::OnJavaScriptBlockedWithDetail(
RenderFrameHost* render_frame_host,
const base::string16& details) {
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(render_frame_host);
if (!web_contents) {
return;
}
DispatchBlockedEventForWebContents(brave_shields::kJavaScript,
base::UTF16ToUTF8(details), web_contents);
}

} // namespace brave_shields
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_H_

#include "base/macros.h"
#include "base/strings/string16.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

Expand All @@ -21,12 +22,27 @@ class BraveShieldsWebContentsObserver : public content::WebContentsObserver,
BraveShieldsWebContentsObserver(content::WebContents*);
~BraveShieldsWebContentsObserver() override;

static void DispatchBlockedEvent(const std::string& block_type,
int render_process_id, int render_frame_id, int frame_tree_node_id);

static void DispatchBlockedEventForWebContents(
const std::string& block_type,
const std::string& subresource,
content::WebContents* web_contents);
static void DispatchBlockedEvent(
const std::string& block_type,
const std::string& subresource,
int render_process_id,
int render_frame_id, int frame_tree_node_id);

protected:
// content::WebContentsObserver overrides.
void RenderFrameCreated(content::RenderFrameHost* host) override;

// Invoked if an IPC message is coming from a specific RenderFrameHost.
bool OnMessageReceived(const IPC::Message& message,
content::RenderFrameHost* render_frame_host) override;
void OnJavaScriptBlockedWithDetail(
content::RenderFrameHost* render_frame_host,
const base::string16& details);

DISALLOW_COPY_AND_ASSIGN(BraveShieldsWebContentsObserver);
};

Expand Down
18 changes: 18 additions & 0 deletions components/brave_shields/browser/shield_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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 http://mozilla.org/MPL/2.0/. */


#ifndef BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_SHIELD_TYPES_H_
#define BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_SHIELD_TYPES_H_

namespace brave_shields {

const char kJavaScript[] = "javascript";
const char kAdBlock[] = "adBlock";
const char kTrackingProtection[] = "trackingProtection";
const char kHttpsEverywhere[] = "httpsEverywhere";

} // brave_shields

#endif // BRAVE_COMPONENTS_BRAVE_SHIELDS_BROWSER_SHIELD_TYPES_H_
21 changes: 21 additions & 0 deletions patches/master_patch.patch
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,27 @@ index d7734d8883c3e438ece3d509b7bb331931831178..6e6cdda19c271de7e5429db02b3e17b2
},
}
# Note: Update the test expectations in GetBaseMessageIdForMode.GoogleStringIds
diff --git a/chrome/renderer/chrome_content_renderer_client.cc b/chrome/renderer/chrome_content_renderer_client.cc
index 0459db65fee93a4f9c32dc83e7528b6bb8c8f653..d1ba50143543fa1acd3aff9b261ed9f45cacb804 100644
--- a/chrome/renderer/chrome_content_renderer_client.cc
+++ b/chrome/renderer/chrome_content_renderer_client.cc
@@ -20,6 +20,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
+#include "brave/renderer/brave_content_settings_observer.h"
#include "build/build_config.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_content_client.h"
@@ -543,7 +544,7 @@ void ChromeContentRendererClient::RenderFrameCreated(
ext_dispatcher =
ChromeExtensionsRendererClient::GetInstance()->extension_dispatcher();
#endif
- ContentSettingsObserver* content_settings = new ContentSettingsObserver(
+ ContentSettingsObserver* content_settings = new BraveContentSettingsObserver(
render_frame, ext_dispatcher, should_whitelist_for_content_settings,
registry);
if (chrome_observer_.get()) {
diff --git a/chrome/renderer/chrome_content_renderer_client.h b/chrome/renderer/chrome_content_renderer_client.h
index a5d593955f2c75bd50f773c7b615a5ee1a06bc3b..ec1cfa7203625f0bc6168ed88f1bba0aac0101aa 100644
--- a/chrome/renderer/chrome_content_renderer_client.h
Expand Down
2 changes: 2 additions & 0 deletions renderer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ source_set("renderer") {
sources = [
"brave_content_renderer_client.cc",
"brave_content_renderer_client.h",
"brave_content_settings_observer.cc",
"brave_content_settings_observer.h",
]

public_deps = [
Expand Down
Loading

0 comments on commit 1a5b750

Please sign in to comment.