From 3ded109c2e90cce94e7ccab60da246cb5450cd62 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Tue, 2 Oct 2018 11:08:10 -0700 Subject: [PATCH] stop using x-devtools-emulate-network-conditions-client-id header https://chromium-review.googlesource.com/c/chromium/src/+/1086949 --- atom/browser/api/atom_api_session.cc | 63 ++++++++--------------- atom/browser/api/atom_api_session.h | 4 +- atom/browser/net/atom_network_delegate.cc | 11 ---- atom/browser/net/atom_network_delegate.h | 5 -- 4 files changed, 23 insertions(+), 60 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 57aa15a0c781..4f679f36bc4d 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -27,7 +27,6 @@ #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/net_converter.h" #include "atom/common/native_mate_converters/value_converter.h" -#include "atom/common/node_includes.h" #include "base/files/file_path.h" #include "base/guid.h" #include "base/strings/string_number_conversions.h" @@ -56,10 +55,10 @@ #include "net/url_request/static_http_user_agent_settings.h" #include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context_getter.h" -#include "services/network/throttling/network_conditions.h" -#include "services/network/throttling/throttling_controller.h" #include "ui/base/l10n/l10n_util.h" +#include "atom/common/node_includes.h" + using atom::api::Cookies; using content::BrowserThread; using content::StoragePartition; @@ -353,18 +352,6 @@ void DownloadIdCallback(content::DownloadManager* download_manager, false, std::vector()); } -void SetDevToolsNetworkEmulationClientIdInIO( - net::URLRequestContextGetter* url_request_context_getter, - const std::string& client_id) { - if (!url_request_context_getter) - return; - net::URLRequestContext* context = - url_request_context_getter->GetURLRequestContext(); - AtomNetworkDelegate* network_delegate = - static_cast(context->network_delegate()); - network_delegate->SetDevToolsNetworkEmulationClientId(client_id); -} - void DestroyGlobalHandle(v8::Isolate* isolate, const v8::Global& global_handle) { v8::Locker locker(isolate); @@ -385,7 +372,7 @@ void DestroyGlobalHandle(v8::Isolate* isolate, } // namespace Session::Session(v8::Isolate* isolate, AtomBrowserContext* browser_context) - : devtools_network_emulation_client_id_(base::GenerateGUID()), + : network_emulation_token_(base::UnguessableToken::Create()), browser_context_(browser_context) { // Observe DownloadManager to get download notifications. content::BrowserContext::GetDownloadManager(browser_context) @@ -503,37 +490,29 @@ void Session::SetDownloadPath(const base::FilePath& path) { } void Session::EnableNetworkEmulation(const mate::Dictionary& options) { - std::unique_ptr conditions; - bool offline = false; - double latency = 0.0, download_throughput = 0.0, upload_throughput = 0.0; - if (options.Get("offline", &offline) && offline) { - conditions.reset(new network::NetworkConditions(offline)); - } else { - options.Get("latency", &latency); - options.Get("downloadThroughput", &download_throughput); - options.Get("uploadThroughput", &upload_throughput); - conditions.reset(new network::NetworkConditions( - false, latency, download_throughput, upload_throughput)); + auto conditions = network::mojom::NetworkConditions::New(); + + options.Get("offline", &conditions->offline); + options.Get("downloadThroughput", &conditions->download_throughput); + options.Get("uploadThroughput", &conditions->upload_throughput); + double latency = 0.0; + if (options.Get("latency", &latency) && latency) { + conditions->latency = base::TimeDelta::FromMillisecondsD(latency); } - network::ThrottlingController::SetConditions( - devtools_network_emulation_client_id_, std::move(conditions)); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::BindOnce(&SetDevToolsNetworkEmulationClientIdInIO, - base::RetainedRef(browser_context_->GetRequestContext()), - devtools_network_emulation_client_id_)); + auto* network_context = content::BrowserContext::GetDefaultStoragePartition( + browser_context_.get()) + ->GetNetworkContext(); + network_context->SetNetworkConditions(network_emulation_token_, + std::move(conditions)); } void Session::DisableNetworkEmulation() { - auto conditions = std::make_unique(); - network::ThrottlingController::SetConditions( - devtools_network_emulation_client_id_, std::move(conditions)); - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::BindOnce(&SetDevToolsNetworkEmulationClientIdInIO, - base::RetainedRef(browser_context_->GetRequestContext()), - std::string())); + auto* network_context = content::BrowserContext::GetDefaultStoragePartition( + browser_context_.get()) + ->GetNetworkContext(); + network_context->SetNetworkConditions( + network_emulation_token_, network::mojom::NetworkConditions::New()); } void Session::SetCertVerifyProc(v8::Local val, diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 09be19595a36..3a6216a03181 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -107,8 +107,8 @@ class Session : public mate::TrackableObject, v8::Global web_request_; v8::Global net_log_; - // The X-DevTools-Emulate-Network-Conditions-Client-Id. - std::string devtools_network_emulation_client_id_; + // The client id to enable the network throttler. + base::UnguessableToken network_emulation_token_; scoped_refptr browser_context_; diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 2bd337ed9f5a..c1a36466e653 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -20,10 +20,8 @@ #include "net/base/load_flags.h" #include "net/base/net_errors.h" #include "net/url_request/url_request.h" -#include "services/network/throttling/throttling_network_transaction.h" using content::BrowserThread; -using network::ThrottlingNetworkTransaction; namespace atom { @@ -269,11 +267,6 @@ void AtomNetworkDelegate::SetResponseListenerInIO(ResponseEvent type, response_listeners_[type] = {std::move(patterns), std::move(callback)}; } -void AtomNetworkDelegate::SetDevToolsNetworkEmulationClientId( - const std::string& client_id) { - client_id_ = client_id; -} - int AtomNetworkDelegate::OnBeforeURLRequest( net::URLRequest* request, const net::CompletionCallback& callback, @@ -297,10 +290,6 @@ int AtomNetworkDelegate::OnBeforeStartTransaction( net::URLRequest* request, const net::CompletionCallback& callback, net::HttpRequestHeaders* headers) { - if (!client_id_.empty()) - headers->SetHeader(network::ThrottlingNetworkTransaction:: - kDevToolsEmulateNetworkConditionsClientId, - client_id_); if (!base::ContainsKey(response_listeners_, kOnBeforeSendHeaders)) return net::OK; diff --git a/atom/browser/net/atom_network_delegate.h b/atom/browser/net/atom_network_delegate.h index b585ead77e26..332632ddf2f5 100644 --- a/atom/browser/net/atom_network_delegate.h +++ b/atom/browser/net/atom_network_delegate.h @@ -79,8 +79,6 @@ class AtomNetworkDelegate : public net::NetworkDelegate { URLPatterns patterns, ResponseListener callback); - void SetDevToolsNetworkEmulationClientId(const std::string& client_id); - protected: // net::NetworkDelegate: int OnBeforeURLRequest(net::URLRequest* request, @@ -172,9 +170,6 @@ class AtomNetworkDelegate : public net::NetworkDelegate { std::map callbacks_; std::vector ignore_connections_limit_domains_; - // Client id for devtools network emulation. - std::string client_id_; - DISALLOW_COPY_AND_ASSIGN(AtomNetworkDelegate); };