Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Brave shields #1

Merged
merged 24 commits into from
Dec 17, 2017
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f214b88
Add Brave shields components and ad-block
bbondy Nov 11, 2017
1fd2c31
Move data reader to dat_file_util
bbondy Nov 15, 2017
6aa38cd
Refactor out common code to BaseBraveShieldsService
bbondy Nov 15, 2017
6b996df
Add TrackingProtection service
bbondy Nov 15, 2017
96fc93b
Add logic to download DAT files
bbondy Nov 16, 2017
220cca0
Add HTTPS Everywhere service (not hooked up yet)
bbondy Nov 17, 2017
7d296b9
Update DEPS for shield repos
bbondy Nov 17, 2017
91f6b99
Add brave-extension DEP
bbondy Nov 17, 2017
5179994
Add ad-block and TP extension contentSetting API
bbondy Nov 20, 2017
7649b2f
Conditionally use ad-block and TP based on content setting
bbondy Nov 19, 2017
5eec8e6
Add braveShields onBlocked extension event
bbondy Nov 20, 2017
c5d88ea
Update background process store extension
bbondy Nov 23, 2017
bd2f133
Use tab URL origin host instead of initiator origin host
bbondy Nov 24, 2017
575f632
Add brave_shields exension manifest
bbondy Nov 24, 2017
6a727e3
Add BraveNetworkDelegate and tie in HTTPS Everywhere
bbondy Dec 1, 2017
d63ad66
Refactor extension dispatch and settings to util file
bbondy Dec 2, 2017
6b90c6a
Fixes for HTTPSE caching
bbondy Dec 3, 2017
75c958b
Remove unneeded string comparison for lookups
bbondy Dec 4, 2017
5fd8b0d
Fix threading issues
bbondy Dec 6, 2017
28c5090
Fix some lint problems
bbondy Dec 6, 2017
6e18540
Reduce the amount of patch file changes
bbondy Dec 13, 2017
4a0a9f3
Don't use GetActiveProfile
bbondy Dec 13, 2017
16d5d5a
Refactor brave network delegate
bbondy Dec 15, 2017
a4e93ee
Handle extensions in antimuon
bbondy Dec 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions DEPS
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use_relative_paths = True

deps = {
"vendor/ad-block": "https://github.com/brave/ad-block.git@b74f508be52931604199195905d3113bf450c851",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@f4e56becfc197b76cbb726b6813afe1c9dd55b88",
"vendor/hashset-cpp": "https://github.com/bbondy/hashset-cpp.git@728fd67bc269765f5a566fb1d2fd9b04b632e68a",
"vendor/bloom-filter-cpp": "https://github.com/bbondy/bloom-filter-cpp.git@b5509def04d1ecf60fdad62457a3bd09c457df90",
"vendor/ad-block": "https://github.com/brave/ad-block.git@9b1ff3275a2f4ce76ad3aaa749e4a01f32a9dabf",
"vendor/tracking-protection": "https://github.com/brave/tracking-protection.git@0931529eba33109c6b3946a83295577fea540045",
"vendor/hashset-cpp": "https://github.com/bbondy/hashset-cpp.git@f427324d667d7188a9e0975cca7f3a8c06226b4d",
"vendor/bloom-filter-cpp": "https://github.com/bbondy/bloom-filter-cpp.git@6faa14ececa33badad149c40f94ff9867159681c",
"vendor/brave-extension": "https://github.com/brave/brave-extension.git@4e7c8782e6d8c0f07b626d8f36c86093bcc7b687",
"vendor/requests": "https://github.com/kennethreitz/requests@e4d59bedfd3c7f4f254f4f5d036587bcd8152458",
"vendor/boto": "https://github.com/boto/boto@f7574aa6cc2c819430c1f05e9a1a1a666ef8169b",
"vendor/python-patch": "https://github.com/svn2github/python-patch@a336a458016ced89aba90dfc3f4c8222ae3b1403",
Expand Down
6 changes: 6 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ source_set("browser") {

public_deps = [
"//chrome/browser",
"//chrome/common",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't these deps for net?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on header files actually only for components/brave_shields/browser/, it's already there too so this was redundant. Removed.

]

deps = [
"//brave/components/brave_shields/browser:brave_shields",
"net",
]
}
13 changes: 13 additions & 0 deletions browser/net/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import("//build/config/features.gni")

source_set("net") {
sources = [
"brave_network_delegate.cc",
"brave_network_delegate.h",
]
public_deps = [
"//content/public/common",
"//third_party/boringssl",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error for boringssl comes up when you're missing another dep. It looks like you should have //content/public/browser here for browser thread and resource_request_info and that will probably take care of the boringssl issue. It also doesn't look like you should need //content/public/common.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added //chrome/browser since that was used too for ProfileIOData

"//net",
]
}
164 changes: 164 additions & 0 deletions browser/net/brave_network_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* 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/. */

#include "brave/browser/net/brave_network_delegate.h"

#include "brave/browser/brave_browser_process_impl.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/https_everywhere_service.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_request_info.h"
#include "net/url_request/url_request.h"


struct OnBeforeURLRequestContext {
OnBeforeURLRequestContext() {}
~OnBeforeURLRequestContext() {}
GURL request_url;
std::string new_url_spec;
uint64_t request_identifier = 0;

DISALLOW_COPY_AND_ASSIGN(OnBeforeURLRequestContext);
};

class PendingRequests {
public:
void Insert(const uint64_t& request_identifier) {
pending_requests_.insert(request_identifier);
}
void Destroy(const uint64_t& request_identifier) {
pending_requests_.erase(request_identifier);
}
bool IsPendingAndAlive(const uint64_t& request_identifier) {
bool isPending = pending_requests_.find(request_identifier) !=
pending_requests_.end();
return isPending;
}
private:
std::set<uint64_t> pending_requests_;
// No need synchronization, should be executed in the same
// thread content::BrowserThread::IO
};

BraveNetworkDelegate::BraveNetworkDelegate(
extensions::EventRouterForwarder* event_router,
BooleanPrefMember* enable_referrers) :
ChromeNetworkDelegate(event_router, enable_referrers) {
pending_requests_.reset(new PendingRequests());
}

BraveNetworkDelegate::~BraveNetworkDelegate() {
}

int BraveNetworkDelegate::OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) {
GURL tab_origin = request->site_for_cookies().GetOrigin();
bool allow_https_everywhere = brave_shields::IsAllowContentSettingFromIO(
request, tab_origin, CONTENT_SETTINGS_TYPE_BRAVEHTTPSEVERYWHERE);
if (!allow_https_everywhere) {
return ChromeNetworkDelegate::OnBeforeURLRequest(request,
callback, new_url);
}

std::shared_ptr<OnBeforeURLRequestContext> ctx(
new OnBeforeURLRequestContext());
if (request) {
ctx->request_identifier = request->identifier();
}
return OnBeforeURLRequest_HttpsePreFileWork(request, callback, new_url, ctx);
}

int BraveNetworkDelegate::OnBeforeURLRequest_HttpsePreFileWork(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

bool is_valid_url = true;
if (request) {
is_valid_url = request->url().is_valid();
std::string scheme = request->url().scheme();
if (scheme.length()) {
std::transform(scheme.begin(), scheme.end(), scheme.begin(), ::tolower);
if ("http" != scheme && "https" != scheme) {
is_valid_url = false;
}
}
}

if (is_valid_url) {
if (!g_browser_process->https_everywhere_service()->
GetHTTPSURLFromCacheOnly(&request->url(), request->identifier(),
ctx->new_url_spec)) {
ctx->request_url = request->url();
content::BrowserThread::PostTaskAndReply(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using base::CreateSequencedTaskRunnerWithTraits because the BrowserThread methods are removed in 63

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

content::BrowserThread::FILE, FROM_HERE,
base::Bind(&BraveNetworkDelegate::OnBeforeURLRequest_HttpseFileWork,
base::Unretained(this), base::Unretained(request), ctx),
base::Bind(base::IgnoreResult(
&BraveNetworkDelegate::OnBeforeURLRequest_HttpsePostFileWork),
base::Unretained(this), base::Unretained(request),
callback, new_url, ctx)
);
pending_requests_->Insert(request->identifier());
return net::ERR_IO_PENDING;
} else {
if (!ctx->new_url_spec.empty()) {
*new_url = GURL(ctx->new_url_spec);
brave_shields::DispatchBlockedEventFromIO(request, "httpsEverywhere");
}
}
}

return ChromeNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see atom_extensions_network_delegate for chaining these together in a wrapped callback

}

void BraveNetworkDelegate::OnBeforeURLRequest_HttpseFileWork(
net::URLRequest* request, std::shared_ptr<OnBeforeURLRequestContext> ctx) {
base::ThreadRestrictions::AssertIOAllowed();
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
DCHECK(ctx->request_identifier != 0);
g_browser_process->https_everywhere_service()->
GetHTTPSURL(&ctx->request_url, ctx->request_identifier, ctx->new_url_spec);
}

int BraveNetworkDelegate::OnBeforeURLRequest_HttpsePostFileWork(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx) {

DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

if (PendedRequestIsDestroyedOrCancelled(ctx.get(), request)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OnURLRequestDestroyed is probably a better way to handle this

return net::OK;
}

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");
}

int rv =
ChromeNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
if (rv != net::ERR_IO_PENDING) {
callback.Run(rv);
}
return rv;
}

bool BraveNetworkDelegate::PendedRequestIsDestroyedOrCancelled(
OnBeforeURLRequestContext* ctx, net::URLRequest* request) {

if (!pending_requests_->IsPendingAndAlive(ctx->request_identifier)
|| request->status().status() == net::URLRequestStatus::CANCELED) {
return true;
}

return false;
}
63 changes: 63 additions & 0 deletions browser/net/brave_network_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* 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_BROWSER_NET_BRAVE_NETWORK_DELEGATE_H_
#define BRAVE_BROWSER_NET_BRAVE_NETWORK_DELEGATE_H_

#include "chrome/browser/net/chrome_network_delegate.h"

template<class T> class PrefMember;

typedef PrefMember<bool> BooleanPrefMember;
struct OnBeforeURLRequestContext;
class PendingRequests;

namespace extensions {
class EventRouterForwarder;
}

namespace net {
class URLRequest;
}

// BraveNetworkDelegate is the central point from within the Brave code to
// add hooks into the network stack.
class BraveNetworkDelegate : public ChromeNetworkDelegate {
public:
// |enable_referrers| (and all of the other optional PrefMembers) should be
// initialized on the UI thread (see below) beforehand. This object's owner is
// responsible for cleaning them up at shutdown.
BraveNetworkDelegate(extensions::EventRouterForwarder* event_router,
BooleanPrefMember* enable_referrers);
~BraveNetworkDelegate() override;
// NetworkDelegate implementation.
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;

protected:
int OnBeforeURLRequest_HttpsePreFileWork(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx);
void OnBeforeURLRequest_HttpseFileWork(
net::URLRequest* request,
std::shared_ptr<OnBeforeURLRequestContext> ctx);
int OnBeforeURLRequest_HttpsePostFileWork(
net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url,
std::shared_ptr<OnBeforeURLRequestContext> ctx);
bool PendedRequestIsDestroyedOrCancelled(
OnBeforeURLRequestContext* ctx,
net::URLRequest* request);
// (TODO)find a better way to handle last first party
// This is a hack from Android
GURL last_first_party_url_;
std::auto_ptr<PendingRequests> pending_requests_;
DISALLOW_COPY_AND_ASSIGN(BraveNetworkDelegate);
};

#endif // BRAVE_BROWSER_NET_BRAVE_NETWORK_DELEGATE_H_
27 changes: 27 additions & 0 deletions common/extensions/api/brave_shields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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/.

[
{
"namespace": "braveShields",
"description": "Use the <code>chrome.braveShields</code> API to receive notifications about the status of brave shields.",
"events": [
{
"name": "onBlocked",
"type": "function",
"description": "Fired when an ad or tracker is blocked.",
"parameters": [
{
"type": "object",
"name": "details",
"properties": {
"tabId": {"type": "integer", "description": "The ID of the tab in which the action occurs."},
"blockType": {"type": "string", "description": "\"adBlock\" or \"trackingProtection\"."}
}
}
]
}
]
}
]
31 changes: 31 additions & 0 deletions components/brave_shields/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
source_set("brave_shields") {
sources = [
"ad_block_service.cc",
"ad_block_service.h",
"base_brave_shields_service.cc",
"base_brave_shields_service.h",
"brave_shields_resource_throttle.cc",
"brave_shields_resource_throttle.h",
"brave_shields_util.cc",
"brave_shields_util.h",
"brave_resource_dispatcher_host_delegate.cc",
"brave_resource_dispatcher_host_delegate.h",
"dat_file_util.cc",
"dat_file_util.h",
"dat_file_web_request.cc",
"dat_file_web_request.h",
"https_everywhere_recently_used_cache.h",
"https_everywhere_service.cc",
"https_everywhere_service.h",
"tracking_protection_service.cc",
"tracking_protection_service.h",
]
deps = [
"//brave/vendor/ad-block/antimuon:ad-block",
"//brave/vendor/tracking-protection/antimuon:tracking-protection",
]
public_deps = [
"//chrome/common",
"//third_party/leveldatabase",
]
}