-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BraveNetworkDelegate and tie in HTTPS Everywhere
- Loading branch information
Showing
10 changed files
with
332 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,5 +24,6 @@ source_set("browser") { | |
|
||
deps = [ | ||
"//brave/components/brave_shields/browser:brave_shields", | ||
"net", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import("//build/config/features.gni") | ||
|
||
source_set("net") { | ||
sources = [ | ||
"brave_network_delegate.cc", | ||
"brave_network_delegate.h", | ||
] | ||
public_deps = [ | ||
"//third_party/boringssl", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* 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/https_everywhere_service.h" | ||
#include "content/public/browser/browser_thread.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) { | ||
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 isValidURL = true; | ||
if (request) { | ||
isValidURL = 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) { | ||
isValidURL = false; | ||
} | ||
} | ||
} | ||
|
||
if (isValidURL) { | ||
ctx->new_url_spec = g_browser_process->https_everywhere_service() | ||
->GetHTTPSURLFromCacheOnly(&request->url(), request->identifier()); | ||
if (ctx->new_url_spec == request->url().spec()) { | ||
ctx->request_url = request->url(); | ||
content::BrowserThread::PostTaskAndReply( | ||
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; | ||
} | ||
} | ||
|
||
return ChromeNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); | ||
} | ||
|
||
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); | ||
ctx->new_url_spec = g_browser_process->https_everywhere_service()-> | ||
GetHTTPSURL(&ctx->request_url, ctx->request_identifier); | ||
} | ||
|
||
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)) { | ||
return net::OK; | ||
} | ||
|
||
if (!ctx->new_url_spec.empty() && | ||
ctx->new_url_spec != request->url().spec()) { | ||
*new_url = GURL(ctx->new_url_spec); | ||
// TODO: dispatch about HTTPS upgrade enabled | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.