REVIEW: create AtomNetworkDelegate on the IO thread

This commit is contained in:
deepak1556 2017-12-15 23:02:33 +05:30 committed by Cheng Zhao
parent b2cef31bc0
commit ebb0e46380
10 changed files with 53 additions and 34 deletions

View file

@ -439,6 +439,17 @@ void DownloadIdCallback(content::DownloadManager* download_manager,
std::vector<content::DownloadItem::ReceivedSlice>());
}
void SetDevToolsNetworkEmulationClientIdInIO(
brightray::URLRequestContextGetter* context_getter,
const std::string& client_id) {
if (!context_getter)
return;
auto network_delegate =
static_cast<AtomNetworkDelegate*>(context_getter->network_delegate());
if (network_delegate)
network_delegate->SetDevToolsNetworkEmulationClientId(client_id);
}
} // namespace
Session::Session(v8::Isolate* isolate, AtomBrowserContext* browser_context)
@ -548,16 +559,24 @@ void Session::EnableNetworkEmulation(const mate::Dictionary& options) {
browser_context_->network_controller_handle()->SetNetworkState(
devtools_network_emulation_client_id_, std::move(conditions));
browser_context_->network_delegate()->SetDevToolsNetworkEmulationClientId(
devtools_network_emulation_client_id_);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(
&SetDevToolsNetworkEmulationClientIdInIO,
base::RetainedRef(browser_context_->url_request_context_getter()),
devtools_network_emulation_client_id_));
}
void Session::DisableNetworkEmulation() {
std::unique_ptr<brightray::DevToolsNetworkConditions> conditions;
browser_context_->network_controller_handle()->SetNetworkState(
devtools_network_emulation_client_id_, std::move(conditions));
browser_context_->network_delegate()->SetDevToolsNetworkEmulationClientId(
std::string());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(
&SetDevToolsNetworkEmulationClientIdInIO,
base::RetainedRef(browser_context_->url_request_context_getter()),
std::string()));
}
void Session::SetCertVerifyProc(v8::Local<v8::Value> val,

View file

@ -74,10 +74,16 @@ void WebRequest::SetListener(Method method, Event type, mate::Arguments* args) {
return;
}
auto delegate = browser_context_->network_delegate();
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(method, base::Unretained(delegate), type,
patterns, listener));
auto url_request_context_getter =
browser_context_->url_request_context_getter();
if (!url_request_context_getter)
return;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(method,
base::Unretained(static_cast<AtomNetworkDelegate*>(
url_request_context_getter->network_delegate())),
type, patterns, listener));
}
// static

View file

@ -71,7 +71,6 @@ AtomBrowserContext::AtomBrowserContext(const std::string& partition,
bool in_memory,
const base::DictionaryValue& options)
: brightray::BrowserContext(partition, in_memory),
network_delegate_(new AtomNetworkDelegate),
cookie_delegate_(new AtomCookieDelegate) {
// Construct user agent string.
Browser* browser = Browser::Get();
@ -104,8 +103,9 @@ void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
user_agent_ = user_agent;
}
net::NetworkDelegate* AtomBrowserContext::CreateNetworkDelegate() {
return network_delegate_;
std::unique_ptr<net::NetworkDelegate>
AtomBrowserContext::CreateNetworkDelegate() {
return base::MakeUnique<AtomNetworkDelegate>();
}
net::CookieMonsterDelegate* AtomBrowserContext::CreateCookieDelegate() {

View file

@ -32,7 +32,7 @@ class AtomBrowserContext : public brightray::BrowserContext {
void SetUserAgent(const std::string& user_agent);
// brightray::URLRequestContextGetter::Delegate:
net::NetworkDelegate* CreateNetworkDelegate() override;
std::unique_ptr<net::NetworkDelegate> CreateNetworkDelegate() override;
net::CookieMonsterDelegate* CreateCookieDelegate() override;
std::string GetUserAgent() override;
std::unique_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory(
@ -52,7 +52,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
void RegisterPrefs(PrefRegistrySimple* pref_registry) override;
AtomBlobReader* GetBlobReader();
AtomNetworkDelegate* network_delegate() const { return network_delegate_; }
AtomCookieDelegate* cookie_delegate() const {
return cookie_delegate_.get();
}
@ -70,8 +69,6 @@ class AtomBrowserContext : public brightray::BrowserContext {
std::string user_agent_;
bool use_cache_;
// Managed by brightray::BrowserContext.
AtomNetworkDelegate* network_delegate_;
scoped_refptr<AtomCookieDelegate> cookie_delegate_;
DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);

View file

@ -247,7 +247,6 @@ void AtomNetworkDelegate::SetResponseListenerInIO(
void AtomNetworkDelegate::SetDevToolsNetworkEmulationClientId(
const std::string& client_id) {
base::AutoLock auto_lock(lock_);
client_id_ = client_id;
}
@ -266,16 +265,10 @@ int AtomNetworkDelegate::OnBeforeStartTransaction(
net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) {
std::string client_id;
{
base::AutoLock auto_lock(lock_);
client_id = client_id_;
}
if (!client_id.empty())
if (!client_id_.empty())
headers->SetHeader(
DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId,
client_id);
client_id_);
if (!base::ContainsKey(response_listeners_, kOnBeforeSendHeaders))
return brightray::NetworkDelegate::OnBeforeStartTransaction(
request, callback, headers);

View file

@ -118,8 +118,6 @@ class AtomNetworkDelegate : public brightray::NetworkDelegate {
std::map<ResponseEvent, ResponseListenerInfo> response_listeners_;
std::map<uint64_t, net::CompletionCallback> callbacks_;
base::Lock lock_;
// Client id for devtools network emulation.
std::string client_id_;

View file

@ -145,8 +145,8 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
return url_request_getter_.get();
}
net::NetworkDelegate* BrowserContext::CreateNetworkDelegate() {
return new NetworkDelegate;
std::unique_ptr<net::NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
return base::MakeUnique<NetworkDelegate>();
}
std::string BrowserContext::GetMediaDeviceIDSalt() {

View file

@ -90,7 +90,7 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
virtual void RegisterPrefs(PrefRegistrySimple* pref_registry) {}
// URLRequestContextGetter::Delegate:
net::NetworkDelegate* CreateNetworkDelegate() override;
std::unique_ptr<net::NetworkDelegate> CreateNetworkDelegate() override;
base::FilePath GetPath() const override;

View file

@ -185,12 +185,11 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
url_request_context_->set_net_log(net_log_);
}
network_delegate_.reset(delegate_->CreateNetworkDelegate());
url_request_context_->set_network_delegate(network_delegate_.get());
storage_.reset(
new net::URLRequestContextStorage(url_request_context_.get()));
storage_->set_network_delegate(delegate_->CreateNetworkDelegate());
auto cookie_path = in_memory_ ?
base::FilePath() : base_path_.Append(FILE_PATH_LITERAL("Cookies"));
auto cookie_config = content::CookieStoreConfig(

View file

@ -15,6 +15,7 @@
#include "net/http/http_cache.h"
#include "net/http/transport_security_state.h"
#include "net/http/url_security_manager.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
namespace base {
@ -44,7 +45,9 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
Delegate() {}
virtual ~Delegate() {}
virtual net::NetworkDelegate* CreateNetworkDelegate() { return nullptr; }
virtual std::unique_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
return nullptr;
}
virtual net::CookieMonsterDelegate* CreateCookieDelegate() {
return nullptr;
}
@ -77,6 +80,11 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
net::HostResolver* host_resolver();
net::URLRequestJobFactory* job_factory() const { return job_factory_; }
net::NetworkDelegate* network_delegate() const {
if (url_request_context_)
return url_request_context_->network_delegate();
return nullptr;
}
private:
Delegate* delegate_;
@ -91,7 +99,6 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
std::unique_ptr<RequireCTDelegate> ct_delegate_;
std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
std::unique_ptr<net::NetworkDelegate> network_delegate_;
std::unique_ptr<net::URLRequestContextStorage> storage_;
std::unique_ptr<net::URLRequestContext> url_request_context_;
std::unique_ptr<net::HostMappingRules> host_mapping_rules_;