Skip to content

Commit

Permalink
Merge pull request #2908 from brave/shields_no_url_request_2
Browse files Browse the repository at this point in the history
Untie more network callbacks from net::URLRequest
  • Loading branch information
iefremov authored Jul 22, 2019
2 parents 1e4d016 + 0350716 commit a1559f4
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 120 deletions.
4 changes: 2 additions & 2 deletions browser/net/brave_network_delegate_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void BraveNetworkDelegateBase::RunNextCallback(
brave::ResponseCallback next_callback =
base::Bind(&BraveNetworkDelegateBase::RunNextCallback,
base::Unretained(this), request, ctx);
rv = callback.Run(request, ctx->headers, next_callback, ctx);
rv = callback.Run(ctx->headers, next_callback, ctx);
if (rv == net::ERR_IO_PENDING) {
return;
}
Expand All @@ -351,7 +351,7 @@ void BraveNetworkDelegateBase::RunNextCallback(
brave::ResponseCallback next_callback =
base::Bind(&BraveNetworkDelegateBase::RunNextCallback,
base::Unretained(this), request, ctx);
rv = callback.Run(request, ctx->original_response_headers,
rv = callback.Run(ctx->original_response_headers,
ctx->override_response_headers,
ctx->allowed_unsafe_redirect_url, next_callback, ctx);
if (rv == net::ERR_IO_PENDING) {
Expand Down
1 change: 0 additions & 1 deletion browser/net/brave_referrals_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace brave {

int OnBeforeStartTransaction_ReferralsWork(
net::URLRequest* request,
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
Expand Down
1 change: 0 additions & 1 deletion browser/net/brave_referrals_network_delegate_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class URLRequest;
namespace brave {

int OnBeforeStartTransaction_ReferralsWork(
net::URLRequest* request,
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TEST_F(BraveReferralsNetworkDelegateHelperTest,
brave_request_info);
brave_request_info->referral_headers_list = referral_headers_list;
int ret = brave::OnBeforeStartTransaction_ReferralsWork(
request.get(), &headers, callback, brave_request_info);
&headers, callback, brave_request_info);

std::string partner_header;
headers.GetHeader("X-Brave-Partner", &partner_header);
Expand Down Expand Up @@ -125,7 +125,7 @@ TEST_F(BraveReferralsNetworkDelegateHelperTest,
new brave::BraveRequestInfo());
brave_request_info->referral_headers_list = referral_headers_list;
int ret = brave::OnBeforeStartTransaction_ReferralsWork(
request.get(), &headers, callback, brave_request_info);
&headers, callback, brave_request_info);

EXPECT_FALSE(headers.HasHeader("X-Brave-Partner"));

Expand Down
10 changes: 5 additions & 5 deletions browser/net/brave_site_hacks_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void CheckForCookieOverride(const GURL& url, const URLPattern& pattern,
}

bool IsBlockTwitterSiteHack(std::shared_ptr<BraveRequestInfo> ctx,
net::HttpRequestHeaders* headers) {
net::HttpRequestHeaders* headers) {
URLPattern redirectURLPattern(URLPattern::SCHEME_ALL, kTwitterRedirectURL);
URLPattern referrerPattern(URLPattern::SCHEME_ALL, kTwitterReferrer);
if (redirectURLPattern.MatchesURL(ctx->request_url)) {
Expand All @@ -85,10 +85,10 @@ bool IsBlockTwitterSiteHack(std::shared_ptr<BraveRequestInfo> ctx,
return false;
}

int OnBeforeStartTransaction_SiteHacksWork(net::URLRequest* request,
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
int OnBeforeStartTransaction_SiteHacksWork(
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
CheckForCookieOverride(ctx->request_url,
URLPattern(URLPattern::SCHEME_ALL, kForbesPattern), headers,
kForbesExtraCookies);
Expand Down
13 changes: 8 additions & 5 deletions browser/net/brave_site_hacks_network_delegate_helper.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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_SITE_HACKS_NETWORK_DELEGATE_H_
#define BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_H_
#ifndef BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_HELPER_H_
#define BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_HELPER_H_

#include <memory>

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

Expand All @@ -17,11 +20,11 @@ int OnBeforeURLRequest_SiteHacksWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);

int OnBeforeStartTransaction_SiteHacksWork(net::URLRequest* request,
int OnBeforeStartTransaction_SiteHacksWork(
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx);

} // namespace brave

#endif // BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_H_
#endif // BRAVE_BROWSER_NET_BRAVE_SITE_HACKS_NETWORK_DELEGATE_HELPER_H_
16 changes: 8 additions & 8 deletions browser/net/brave_site_hacks_network_delegate_helper_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, ForbesWithCookieHeader) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
std::string cookies;
headers.GetHeader(kCookieHeader, &cookies);
Expand All @@ -68,7 +68,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, ForbesWithoutCookieHeader) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
std::string cookies;
headers.GetHeader(kCookieHeader, &cookies);
Expand All @@ -90,7 +90,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, NotForbesNoCookieChange) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
std::string cookies;
headers.GetHeader(kCookieHeader, &cookies);
Expand All @@ -111,7 +111,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, NoScriptTwitterMobileRedirect) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
EXPECT_EQ(ret, net::ERR_ABORTED);
}
Expand All @@ -130,7 +130,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest,
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
EXPECT_EQ(ret, net::OK);
}
Expand All @@ -148,7 +148,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, TwitterNoCancelWithReferer) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
EXPECT_EQ(ret, net::OK);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, UAWhitelistedTest) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
std::string user_agent;
headers.GetHeader(kUserAgentHeader, &user_agent);
Expand Down Expand Up @@ -219,7 +219,7 @@ TEST_F(BraveSiteHacksNetworkDelegateHelperTest, NOTUAWhitelistedTest) {
brave::BraveRequestInfo::FillCTXFromRequest(request.get(),
brave_request_info);
brave::ResponseCallback callback;
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(request.get(),
int ret = brave::OnBeforeStartTransaction_SiteHacksWork(
&headers, callback, brave_request_info);
std::string user_agent;
headers.GetHeader(kUserAgentHeader, &user_agent);
Expand Down
68 changes: 65 additions & 3 deletions browser/net/url_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,81 @@
#include <memory>
#include <string>

#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "brave/common/url_constants.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/browser/resource_request_info.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"

#if BUILDFLAG(ENABLE_BRAVE_WEBTORRENT)
#include "extensions/browser/info_map.h"
#endif

namespace brave {

BraveRequestInfo::BraveRequestInfo() {
namespace {

bool IsWebTorrentDisabled(content::ResourceContext* resource_context) {
#if BUILDFLAG(ENABLE_BRAVE_WEBTORRENT)
DCHECK(resource_context);

const ProfileIOData* io_data =
ProfileIOData::FromResourceContext(resource_context);
if (!io_data) {
return false;
}

const extensions::InfoMap* infoMap = io_data->GetExtensionInfoMap();
if (!infoMap) {
return false;
}

return !infoMap->extensions().Contains(brave_webtorrent_extension_id) ||
infoMap->disabled_extensions().Contains(brave_webtorrent_extension_id);
#else
return true;
#endif // BUILDFLAG(ENABLE_BRAVE_WEBTORRENT)
}

BraveRequestInfo::~BraveRequestInfo() {
std::string GetUploadDataFromURLRequest(const net::URLRequest* request) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request->has_upload())
return {};

const net::UploadDataStream* stream = request->get_upload();
if (!stream->GetElementReaders())
return {};

const auto* element_readers = stream->GetElementReaders();
if (element_readers->empty())
return {};

std::string upload_data;
for (const auto& element_reader : *element_readers) {
const net::UploadBytesElementReader* reader =
element_reader->AsBytesReader();
if (!reader) {
return {};
}
upload_data.append(reader->bytes(), reader->length());
}
return upload_data;
}

} // namespace

BraveRequestInfo::BraveRequestInfo() = default;

BraveRequestInfo::~BraveRequestInfo() = default;

void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request,
std::shared_ptr<brave::BraveRequestInfo> ctx) {
ctx->request_identifier = request->identifier();
Expand All @@ -40,7 +98,11 @@ void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request,
auto* request_info = content::ResourceRequestInfo::ForRequest(request);
if (request_info) {
ctx->resource_type = request_info->GetResourceType();
if (auto* context = request_info->GetContext()) {
ctx->is_webtorrent_disabled = IsWebTorrentDisabled(context);
}
}

brave_shields::GetRenderFrameInfo(request,
&ctx->render_frame_id,
&ctx->render_process_id,
Expand Down Expand Up @@ -77,7 +139,7 @@ void BraveRequestInfo::FillCTXFromRequest(const net::URLRequest* request,
request, ctx->tab_origin, ctx->tab_origin, CONTENT_SETTINGS_TYPE_PLUGINS,
brave_shields::kReferrers);

ctx->request = request;
ctx->upload_data = GetUploadDataFromURLRequest(request);
}

} // namespace brave
10 changes: 5 additions & 5 deletions browser/net/url_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct BraveRequestInfo {
bool allow_3p_cookies = false;
bool allow_referrers = false;
bool allow_google_auth = true;
bool is_webtorrent_disabled = false;
int render_process_id = 0;
int render_frame_id = 0;
int frame_tree_node_id = 0;
Expand All @@ -86,6 +87,8 @@ struct BraveRequestInfo {
static_cast<content::ResourceType>(-1);
content::ResourceType resource_type = kInvalidResourceType;

std::string upload_data;

static void FillCTXFromRequest(const net::URLRequest* request,
std::shared_ptr<brave::BraveRequestInfo> ctx);

Expand All @@ -100,9 +103,6 @@ struct BraveRequestInfo {
std::shared_ptr<brave::BraveRequestInfo> ctx);
friend class ::BraveNetworkDelegateBase;

// Don't use this directly after any dispatch
// request is deprecated, do not use it.
const net::URLRequest* request;
GURL* new_url = nullptr;

DISALLOW_COPY_AND_ASSIGN(BraveRequestInfo);
Expand All @@ -113,12 +113,12 @@ using OnBeforeURLRequestCallback =
base::Callback<int(const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx)>;
using OnBeforeStartTransactionCallback =
base::Callback<int(net::URLRequest* request,
base::Callback<int(
net::HttpRequestHeaders* headers,
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx)>;
using OnHeadersReceivedCallback =
base::Callback<int(net::URLRequest* request,
base::Callback<int(
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url,
Expand Down
36 changes: 2 additions & 34 deletions components/brave_rewards/browser/net/network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,13 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/websocket_handshake_request_info.h"
#include "content/public/browser/web_contents.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/url_request/url_request.h"
#include "url/gurl.h"

namespace brave_rewards {

namespace {

bool GetPostData(const net::URLRequest* request, std::string* post_data) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!request->has_upload())
return false;

const net::UploadDataStream* stream = request->get_upload();
if (!stream->GetElementReaders())
return false;

const auto* element_readers = stream->GetElementReaders();

if (element_readers->empty())
return false;

post_data->clear();
for (const auto& element_reader : *element_readers) {
const net::UploadBytesElementReader* reader =
element_reader->AsBytesReader();
if (!reader) {
post_data->clear();
return false;
}
post_data->append(reader->bytes(), reader->length());
}
return true;
}

content::WebContents* GetWebContents(
int render_process_id,
int render_frame_id,
Expand Down Expand Up @@ -109,11 +78,10 @@ int OnBeforeURLRequest(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

if (IsMediaLink(ctx->request_url, ctx->tab_origin, ctx->referrer)) {
std::string post_data;
if (GetPostData(ctx->request, &post_data)) {
if (!ctx->upload_data.empty()) {
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(&DispatchOnUI,
post_data,
ctx->upload_data,
ctx->request_url,
ctx->tab_url,
ctx->referrer.spec(),
Expand Down
Loading

0 comments on commit a1559f4

Please sign in to comment.