-
Notifications
You must be signed in to change notification settings - Fork 820
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
Changes from 20 commits
f214b88
1fd2c31
6aa38cd
6b996df
96fc93b
220cca0
7d296b9
91f6b99
5179994
7649b2f
5eec8e6
c5d88ea
bd2f133
575f632
6a727e3
d63ad66
6b90c6a
75c958b
5fd8b0d
28c5090
6e18540
4a0a9f3
16d5d5a
a4e93ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also added //chrome/browser since that was used too for ProfileIOData |
||
"//net", | ||
] | ||
} |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; | ||
} |
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_ |
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\"."} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
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", | ||
] | ||
} |
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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.