From 007ea500d0094ba42f73d24b2ba04b1bacc522fe Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Tue, 19 Dec 2017 23:47:48 +0530 Subject: [PATCH] REVIEW: Re-use devtools network throttling from content layer https://chromium-review.googlesource.com/c/chromium/src/+/664356 --- atom/browser/api/atom_api_session.cc | 21 +- atom/browser/net/atom_network_delegate.cc | 10 +- atom/browser/net/url_request_fetch_job.cc | 6 +- brightray/browser/browser_context.cc | 1 - brightray/browser/browser_context.h | 7 - .../browser/devtools_manager_delegate.cc | 8 +- brightray/browser/devtools_manager_delegate.h | 4 - .../net/devtools_network_conditions.cc | 35 -- .../browser/net/devtools_network_conditions.h | 39 --- .../net/devtools_network_controller.cc | 78 ----- .../browser/net/devtools_network_controller.h | 44 --- .../net/devtools_network_controller_handle.cc | 60 ---- .../net/devtools_network_controller_handle.h | 45 --- .../net/devtools_network_interceptor.cc | 293 ----------------- .../net/devtools_network_interceptor.h | 107 ------- .../net/devtools_network_protocol_handler.cc | 173 ---------- .../net/devtools_network_protocol_handler.h | 48 --- .../net/devtools_network_transaction.cc | 302 ------------------ .../net/devtools_network_transaction.h | 113 ------- .../devtools_network_transaction_factory.cc | 53 --- .../devtools_network_transaction_factory.h | 39 --- .../devtools_network_upload_data_stream.cc | 96 ------ .../net/devtools_network_upload_data_stream.h | 50 --- .../browser/url_request_context_getter.cc | 23 +- .../browser/url_request_context_getter.h | 3 - brightray/filenames.gypi | 16 - 26 files changed, 23 insertions(+), 1651 deletions(-) delete mode 100644 brightray/browser/net/devtools_network_conditions.cc delete mode 100644 brightray/browser/net/devtools_network_conditions.h delete mode 100644 brightray/browser/net/devtools_network_controller.cc delete mode 100644 brightray/browser/net/devtools_network_controller.h delete mode 100644 brightray/browser/net/devtools_network_controller_handle.cc delete mode 100644 brightray/browser/net/devtools_network_controller_handle.h delete mode 100644 brightray/browser/net/devtools_network_interceptor.cc delete mode 100644 brightray/browser/net/devtools_network_interceptor.h delete mode 100644 brightray/browser/net/devtools_network_protocol_handler.cc delete mode 100644 brightray/browser/net/devtools_network_protocol_handler.h delete mode 100644 brightray/browser/net/devtools_network_transaction.cc delete mode 100644 brightray/browser/net/devtools_network_transaction.h delete mode 100644 brightray/browser/net/devtools_network_transaction_factory.cc delete mode 100644 brightray/browser/net/devtools_network_transaction_factory.h delete mode 100644 brightray/browser/net/devtools_network_upload_data_stream.cc delete mode 100644 brightray/browser/net/devtools_network_upload_data_stream.h diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index b7b6b2abca6..f55ecc44403 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -31,11 +31,11 @@ #include "base/strings/string_util.h" #include "base/threading/thread_task_runner_handle.h" #include "brightray/browser/media/media_device_id_salt.h" -#include "brightray/browser/net/devtools_network_conditions.h" -#include "brightray/browser/net/devtools_network_controller_handle.h" #include "chrome/browser/browser_process.h" #include "chrome/common/pref_names.h" #include "components/prefs/pref_service.h" +#include "content/common/devtools/devtools_network_conditions.h" +#include "content/common/devtools/devtools_network_controller.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_manager_delegate.h" #include "content/public/browser/storage_partition.h" @@ -543,23 +543,20 @@ void Session::SetDownloadPath(const base::FilePath& path) { } void Session::EnableNetworkEmulation(const mate::Dictionary& options) { - std::unique_ptr conditions; + 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 brightray::DevToolsNetworkConditions(offline)); + conditions.reset(new content::DevToolsNetworkConditions(offline)); } else { options.Get("latency", &latency); options.Get("downloadThroughput", &download_throughput); options.Get("uploadThroughput", &upload_throughput); - conditions.reset( - new brightray::DevToolsNetworkConditions(false, - latency, - download_throughput, - upload_throughput)); + conditions.reset(new content::DevToolsNetworkConditions( + false, latency, download_throughput, upload_throughput)); } - browser_context_->network_controller_handle()->SetNetworkState( + content::DevToolsNetworkController::SetNetworkState( devtools_network_emulation_client_id_, std::move(conditions)); BrowserThread::PostTask( BrowserThread::IO, FROM_HERE, @@ -570,8 +567,8 @@ void Session::EnableNetworkEmulation(const mate::Dictionary& options) { } void Session::DisableNetworkEmulation() { - std::unique_ptr conditions; - browser_context_->network_controller_handle()->SetNetworkState( + auto conditions = base::MakeUnique(); + content::DevToolsNetworkController::SetNetworkState( devtools_network_emulation_client_id_, std::move(conditions)); BrowserThread::PostTask( BrowserThread::IO, FROM_HERE, diff --git a/atom/browser/net/atom_network_delegate.cc b/atom/browser/net/atom_network_delegate.cc index 680459a14c8..fe4424af5fa 100644 --- a/atom/browser/net/atom_network_delegate.cc +++ b/atom/browser/net/atom_network_delegate.cc @@ -10,12 +10,12 @@ #include "atom/common/native_mate_converters/net_converter.h" #include "base/stl_util.h" #include "base/strings/string_util.h" -#include "brightray/browser/net/devtools_network_transaction.h" +#include "content/common/devtools/devtools_network_transaction.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/render_frame_host.h" #include "net/url_request/url_request.h" -using brightray::DevToolsNetworkTransaction; +using content::DevToolsNetworkTransaction; using content::BrowserThread; namespace atom { @@ -266,9 +266,9 @@ int AtomNetworkDelegate::OnBeforeStartTransaction( const net::CompletionCallback& callback, net::HttpRequestHeaders* headers) { if (!client_id_.empty()) - headers->SetHeader( - DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId, - client_id_); + headers->SetHeader(content::DevToolsNetworkTransaction:: + kDevToolsEmulateNetworkConditionsClientId, + client_id_); if (!base::ContainsKey(response_listeners_, kOnBeforeSendHeaders)) return brightray::NetworkDelegate::OnBeforeStartTransaction( request, callback, headers); diff --git a/atom/browser/net/url_request_fetch_job.cc b/atom/browser/net/url_request_fetch_job.cc index 2a941711f68..bc3016d1d4a 100644 --- a/atom/browser/net/url_request_fetch_job.cc +++ b/atom/browser/net/url_request_fetch_job.cc @@ -96,9 +96,9 @@ void URLRequestFetchJob::BeforeStartInUI( if (val->IsNull()) { // We have to create the URLRequestContextGetter on UI thread. url_request_context_getter_ = new brightray::URLRequestContextGetter( - this, nullptr, nullptr, base::FilePath(), true, - BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), - nullptr, content::URLRequestInterceptorScopedVector()); + this, nullptr, base::FilePath(), true, + BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), nullptr, + content::URLRequestInterceptorScopedVector()); } else { mate::Handle session; if (mate::ConvertFromV8(isolate, val, &session) && !session.IsEmpty()) { diff --git a/brightray/browser/browser_context.cc b/brightray/browser/browser_context.cc index 83a3833845d..abf8f698f55 100644 --- a/brightray/browser/browser_context.cc +++ b/brightray/browser/browser_context.cc @@ -132,7 +132,6 @@ net::URLRequestContextGetter* BrowserContext::CreateRequestContext( DCHECK(!url_request_getter_.get()); url_request_getter_ = new URLRequestContextGetter( this, - network_controller_handle(), static_cast(BrowserClient::Get()->GetNetLog()), GetPath(), in_memory_, diff --git a/brightray/browser/browser_context.h b/brightray/browser/browser_context.h index 449e8e8f286..25386099dd9 100644 --- a/brightray/browser/browser_context.h +++ b/brightray/browser/browser_context.h @@ -11,7 +11,6 @@ #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "brightray/browser/media/media_device_id_salt.h" -#include "brightray/browser/net/devtools_network_controller_handle.h" #include "brightray/browser/permission_manager.h" #include "brightray/browser/url_request_context_getter.h" #include "content/public/browser/browser_context.h" @@ -76,10 +75,6 @@ class BrowserContext : public base::RefCounted, return url_request_getter_.get(); } - DevToolsNetworkControllerHandle* network_controller_handle() { - return &network_controller_handle_; - } - void InitPrefs(); PrefService* prefs() { return prefs_.get(); } @@ -126,8 +121,6 @@ class BrowserContext : public base::RefCounted, base::FilePath path_; bool in_memory_; - DevToolsNetworkControllerHandle network_controller_handle_; - std::unique_ptr resource_context_; scoped_refptr url_request_getter_; scoped_refptr storage_policy_; diff --git a/brightray/browser/devtools_manager_delegate.cc b/brightray/browser/devtools_manager_delegate.cc index 0e70af7751b..079931fdb36 100644 --- a/brightray/browser/devtools_manager_delegate.cc +++ b/brightray/browser/devtools_manager_delegate.cc @@ -12,7 +12,6 @@ #include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/strings/utf_string_conversions.h" -#include "brightray/browser/net/devtools_network_protocol_handler.h" #include "brightray/common/content_client.h" #include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_frontend_host.h" @@ -93,9 +92,7 @@ void DevToolsManagerDelegate::StartHttpHandler() { base::FilePath()); } -DevToolsManagerDelegate::DevToolsManagerDelegate() - : handler_(new DevToolsNetworkProtocolHandler) { -} +DevToolsManagerDelegate::DevToolsManagerDelegate() {} DevToolsManagerDelegate::~DevToolsManagerDelegate() { } @@ -107,9 +104,6 @@ bool DevToolsManagerDelegate::HandleCommand( content::DevToolsAgentHost* agent_host, int session_id, base::DictionaryValue* command) { - if (handler_->HandleCommand(agent_host, command)) { - return true; - } return false; } diff --git a/brightray/browser/devtools_manager_delegate.h b/brightray/browser/devtools_manager_delegate.h index e8189161c14..ac779fe1627 100644 --- a/brightray/browser/devtools_manager_delegate.h +++ b/brightray/browser/devtools_manager_delegate.h @@ -13,8 +13,6 @@ namespace brightray { -class DevToolsNetworkProtocolHandler; - class DevToolsManagerDelegate : public content::DevToolsManagerDelegate { public: static void StartHttpHandler(); @@ -34,8 +32,6 @@ class DevToolsManagerDelegate : public content::DevToolsManagerDelegate { std::string GetFrontendResource(const std::string& path) override; private: - std::unique_ptr handler_; - DISALLOW_COPY_AND_ASSIGN(DevToolsManagerDelegate); }; diff --git a/brightray/browser/net/devtools_network_conditions.cc b/brightray/browser/net/devtools_network_conditions.cc deleted file mode 100644 index 7405ed94260..00000000000 --- a/brightray/browser/net/devtools_network_conditions.cc +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "brightray/browser/net/devtools_network_conditions.h" - -namespace brightray { - -DevToolsNetworkConditions::DevToolsNetworkConditions(bool offline) - : offline_(offline), - latency_(0), - download_throughput_(0), - upload_throughput_(0) { -} - -DevToolsNetworkConditions::DevToolsNetworkConditions( - bool offline, - double latency, - double download_throughput, - double upload_throughput) - : offline_(offline), - latency_(latency), - download_throughput_(download_throughput), - upload_throughput_(upload_throughput) { -} - -DevToolsNetworkConditions::~DevToolsNetworkConditions() { -} - -bool DevToolsNetworkConditions::IsThrottling() const { - return !offline_ && ((latency_ != 0.0) || (download_throughput_ != 0.0) || - (upload_throughput_ != 0.0)); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_conditions.h b/brightray/browser/net/devtools_network_conditions.h deleted file mode 100644 index f9885ba4680..00000000000 --- a/brightray/browser/net/devtools_network_conditions.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONDITIONS_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONDITIONS_H_ - -#include "base/macros.h" - -namespace brightray { - -class DevToolsNetworkConditions { - public: - explicit DevToolsNetworkConditions(bool offline); - DevToolsNetworkConditions(bool offline, - double latency, - double download_throughput, - double upload_throughput); - ~DevToolsNetworkConditions(); - - bool IsThrottling() const; - - bool offline() const { return offline_; } - double latency() const { return latency_; } - double download_throughput() const { return download_throughput_; } - double upload_throughput() const { return upload_throughput_; } - - private: - const bool offline_; - const double latency_; - const double download_throughput_; - const double upload_throughput_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkConditions); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONDITIONS_H_ diff --git a/brightray/browser/net/devtools_network_controller.cc b/brightray/browser/net/devtools_network_controller.cc deleted file mode 100644 index bd37e61d947..00000000000 --- a/brightray/browser/net/devtools_network_controller.cc +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#include "brightray/browser/net/devtools_network_controller.h" - -#include "base/bind.h" -#include "brightray/browser/net/devtools_network_conditions.h" -#include "brightray/browser/net/devtools_network_interceptor.h" -#include "brightray/browser/net/devtools_network_transaction.h" -#include "content/public/browser/browser_thread.h" - -using content::BrowserThread; - -namespace brightray { - -DevToolsNetworkController::DevToolsNetworkController() - : appcache_interceptor_(new DevToolsNetworkInterceptor) { -} - -DevToolsNetworkController::~DevToolsNetworkController() { -} - -void DevToolsNetworkController::SetNetworkState( - const std::string& client_id, - std::unique_ptr conditions) { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - - auto it = interceptors_.find(client_id); - if (it == interceptors_.end()) { - if (!conditions) - return; - std::unique_ptr new_interceptor( - new DevToolsNetworkInterceptor); - new_interceptor->UpdateConditions(std::move(conditions)); - interceptors_[client_id] = std::move(new_interceptor); - } else { - if (!conditions) { - std::unique_ptr online_conditions( - new DevToolsNetworkConditions(false)); - it->second->UpdateConditions(std::move(online_conditions)); - interceptors_.erase(client_id); - } else { - it->second->UpdateConditions(std::move(conditions)); - } - } - - bool has_offline_interceptors = false; - for (const auto& interceptor : interceptors_) { - if (interceptor.second->IsOffline()) { - has_offline_interceptors = true; - break; - } - } - - bool is_appcache_offline = appcache_interceptor_->IsOffline(); - if (is_appcache_offline != has_offline_interceptors) { - std::unique_ptr appcache_conditions( - new DevToolsNetworkConditions(has_offline_interceptors)); - appcache_interceptor_->UpdateConditions(std::move(appcache_conditions)); - } -} - -DevToolsNetworkInterceptor* -DevToolsNetworkController::GetInterceptor(const std::string& client_id) { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - - if (interceptors_.empty() || client_id.empty()) - return nullptr; - - auto it = interceptors_.find(client_id); - if (it == interceptors_.end()) - return nullptr; - - return it->second.get(); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_controller.h b/brightray/browser/net/devtools_network_controller.h deleted file mode 100644 index 1ba5bf30869..00000000000 --- a/brightray/browser/net/devtools_network_controller.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_H_ - -#include -#include -#include - -#include "base/macros.h" -#include "base/threading/thread_checker.h" - -namespace brightray { - -class DevToolsNetworkConditions; -class DevToolsNetworkInterceptor; -class DevToolsNetworkTransaction; - -class DevToolsNetworkController { - public: - DevToolsNetworkController(); - virtual ~DevToolsNetworkController(); - - void SetNetworkState(const std::string& client_id, - std::unique_ptr conditions); - - DevToolsNetworkInterceptor* GetInterceptor(const std::string& client_id); - - private: - using InterceptorMap = - std::unordered_map>; - - std::unique_ptr appcache_interceptor_; - InterceptorMap interceptors_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkController); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_H_ diff --git a/brightray/browser/net/devtools_network_controller_handle.cc b/brightray/browser/net/devtools_network_controller_handle.cc deleted file mode 100644 index 8d167a1e515..00000000000 --- a/brightray/browser/net/devtools_network_controller_handle.cc +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#include "brightray/browser/net/devtools_network_controller_handle.h" - -#include "base/bind.h" -#include "brightray/browser/net/devtools_network_conditions.h" -#include "brightray/browser/net/devtools_network_controller.h" -#include "content/public/browser/browser_thread.h" - -using content::BrowserThread; - -namespace brightray { - -DevToolsNetworkControllerHandle::DevToolsNetworkControllerHandle() { - DCHECK_CURRENTLY_ON(BrowserThread::UI); -} - -DevToolsNetworkControllerHandle::~DevToolsNetworkControllerHandle() { - BrowserThread::DeleteSoon(BrowserThread::IO, - FROM_HERE, - controller_.release()); -} - -void DevToolsNetworkControllerHandle::SetNetworkState( - const std::string& client_id, - std::unique_ptr conditions) { - DCHECK_CURRENTLY_ON(BrowserThread::UI); - - BrowserThread::PostTask( - BrowserThread::IO, FROM_HERE, - base::Bind(&DevToolsNetworkControllerHandle::SetNetworkStateOnIO, - base::Unretained(this), client_id, base::Passed(&conditions))); -} - -DevToolsNetworkController* DevToolsNetworkControllerHandle::GetController() { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - - LazyInitialize(); - return controller_.get(); -} - -void DevToolsNetworkControllerHandle::LazyInitialize() { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - - if (!controller_) - controller_.reset(new DevToolsNetworkController); -} - -void DevToolsNetworkControllerHandle::SetNetworkStateOnIO( - const std::string& client_id, - std::unique_ptr conditions) { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - - LazyInitialize(); - controller_->SetNetworkState(client_id, std::move(conditions)); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_controller_handle.h b/brightray/browser/net/devtools_network_controller_handle.h deleted file mode 100644 index cbb89ca25bc..00000000000 --- a/brightray/browser/net/devtools_network_controller_handle.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_ - -#include -#include - -#include "base/macros.h" - -namespace brightray { - -class DevToolsNetworkConditions; -class DevToolsNetworkController; - -// A handle to manage an IO-thread DevToolsNetworkController on the IO thread -// while allowing SetNetworkState to be called from the UI thread. -class DevToolsNetworkControllerHandle { - public: - DevToolsNetworkControllerHandle(); - ~DevToolsNetworkControllerHandle(); - - // Called on the UI thread. - void SetNetworkState(const std::string& client_id, - std::unique_ptr conditions); - - // Called on the IO thread. - DevToolsNetworkController* GetController(); - - private: - void LazyInitialize(); - void SetNetworkStateOnIO( - const std::string& client_id, - std::unique_ptr conditions); - - std::unique_ptr controller_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkControllerHandle); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_CONTROLLER_HANDLE_H_ diff --git a/brightray/browser/net/devtools_network_interceptor.cc b/brightray/browser/net/devtools_network_interceptor.cc deleted file mode 100644 index 8fec4c45db4..00000000000 --- a/brightray/browser/net/devtools_network_interceptor.cc +++ /dev/null @@ -1,293 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "brightray/browser/net/devtools_network_interceptor.h" - -#include -#include - -#include "base/time/time.h" -#include "brightray/browser/net/devtools_network_conditions.h" -#include "net/base/net_errors.h" - -namespace brightray { - -namespace { - -int64_t kPacketSize = 1500; - -base::TimeDelta CalculateTickLength(double throughput) { - if (!throughput) - return base::TimeDelta(); - - int64_t us_tick_length = (1000000L * kPacketSize) / throughput; - if (us_tick_length == 0) - us_tick_length = 1; - return base::TimeDelta::FromMicroseconds(us_tick_length); -} - -} // namespace - -DevToolsNetworkInterceptor::ThrottleRecord::ThrottleRecord() { -} - -DevToolsNetworkInterceptor::ThrottleRecord::ThrottleRecord( - const ThrottleRecord& other) = default; - -DevToolsNetworkInterceptor::ThrottleRecord::~ThrottleRecord() { -} - -DevToolsNetworkInterceptor::DevToolsNetworkInterceptor() - : conditions_(new DevToolsNetworkConditions(false)), - download_last_tick_(0), - upload_last_tick_(0), - weak_ptr_factory_(this) { -} - -DevToolsNetworkInterceptor::~DevToolsNetworkInterceptor() { -} - -base::WeakPtr -DevToolsNetworkInterceptor::GetWeakPtr() { - return weak_ptr_factory_.GetWeakPtr(); -} - -void DevToolsNetworkInterceptor::FinishRecords( - ThrottleRecords* records, bool offline) { - ThrottleRecords temp; - temp.swap(*records); - for (const ThrottleRecord& record : temp) { - bool failed = offline && !record.is_upload; - record.callback.Run( - failed ? net::ERR_INTERNET_DISCONNECTED : record.result, - record.bytes); - } -} - -void DevToolsNetworkInterceptor::UpdateConditions( - std::unique_ptr conditions) { - DCHECK(conditions); - base::TimeTicks now = base::TimeTicks::Now(); - if (conditions_->IsThrottling()) - UpdateThrottled(now); - - conditions_ = std::move(conditions); - - bool offline = conditions_->offline(); - if (offline || !conditions_->IsThrottling()) { - timer_.Stop(); - FinishRecords(&download_, offline); - FinishRecords(&upload_, offline); - FinishRecords(&suspended_, offline); - return; - } - - // Throttling. - DCHECK(conditions_->download_throughput() != 0 || - conditions_->upload_throughput() != 0); - offset_ = now; - - download_last_tick_ = 0; - download_tick_length_ = CalculateTickLength( - conditions_->download_throughput()); - - upload_last_tick_ = 0; - upload_tick_length_ = CalculateTickLength(conditions_->upload_throughput()); - - latency_length_ = base::TimeDelta(); - double latency = conditions_->latency(); - if (latency > 0) - latency_length_ = base::TimeDelta::FromMillisecondsD(latency); - ArmTimer(now); -} - -uint64_t DevToolsNetworkInterceptor::UpdateThrottledRecords( - base::TimeTicks now, - ThrottleRecords* records, - uint64_t last_tick, - base::TimeDelta tick_length) { - if (tick_length.is_zero()) { - DCHECK(records->empty()); - return last_tick; - } - - int64_t new_tick = (now - offset_) / tick_length; - int64_t ticks = new_tick - last_tick; - - int64_t length = records->size(); - if (!length) - return new_tick; - - int64_t shift = ticks % length; - for (int64_t i = 0; i < length; ++i) { - (*records)[i].bytes -= - (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0); - } - std::rotate(records->begin(), records->begin() + shift, records->end()); - return new_tick; -} - -void DevToolsNetworkInterceptor::UpdateThrottled(base::TimeTicks now) { - download_last_tick_ = UpdateThrottledRecords( - now, &download_, download_last_tick_, download_tick_length_); - upload_last_tick_ = UpdateThrottledRecords( - now, &upload_, upload_last_tick_, upload_tick_length_); - UpdateSuspended(now); -} - -void DevToolsNetworkInterceptor::UpdateSuspended(base::TimeTicks now) { - int64_t activation_baseline = - (now - latency_length_ - base::TimeTicks()).InMicroseconds(); - ThrottleRecords suspended; - for (const ThrottleRecord& record : suspended_) { - if (record.send_end <= activation_baseline) { - if (record.is_upload) - upload_.push_back(record); - else - download_.push_back(record); - } else { - suspended.push_back(record); - } - } - suspended_.swap(suspended); -} - -void DevToolsNetworkInterceptor::CollectFinished( - ThrottleRecords* records, ThrottleRecords* finished) { - ThrottleRecords active; - for (const ThrottleRecord& record : *records) { - if (record.bytes < 0) - finished->push_back(record); - else - active.push_back(record); - } - records->swap(active); -} - -void DevToolsNetworkInterceptor::OnTimer() { - base::TimeTicks now = base::TimeTicks::Now(); - UpdateThrottled(now); - - ThrottleRecords finished; - CollectFinished(&download_, &finished); - CollectFinished(&upload_, &finished); - for (const ThrottleRecord& record : finished) - record.callback.Run(record.result, record.bytes); - - ArmTimer(now); -} - -base::TimeTicks DevToolsNetworkInterceptor::CalculateDesiredTime( - const ThrottleRecords& records, - uint64_t last_tick, - base::TimeDelta tick_length) { - int64_t min_ticks_left = 0x10000L; - size_t count = records.size(); - for (size_t i = 0; i < count; ++i) { - int64_t packets_left = (records[i].bytes + kPacketSize - 1) / kPacketSize; - int64_t ticks_left = (i + 1) + count * (packets_left - 1); - if (i == 0 || ticks_left < min_ticks_left) - min_ticks_left = ticks_left; - } - return offset_ + tick_length * (last_tick + min_ticks_left); -} - -void DevToolsNetworkInterceptor::ArmTimer(base::TimeTicks now) { - size_t suspend_count = suspended_.size(); - if (download_.empty() && upload_.empty() && !suspend_count) { - timer_.Stop(); - return; - } - - base::TimeTicks desired_time = CalculateDesiredTime( - download_, download_last_tick_, download_tick_length_); - if (desired_time == offset_) { - FinishRecords(&download_, false); - } - - base::TimeTicks upload_time = CalculateDesiredTime( - upload_, upload_last_tick_, upload_tick_length_); - if (upload_time != offset_ && upload_time < desired_time) - desired_time = upload_time; - - int64_t min_baseline = std::numeric_limits::max(); - for (size_t i = 0; i < suspend_count; ++i) { - if (suspended_[i].send_end < min_baseline) - min_baseline = suspended_[i].send_end; - } - if (suspend_count) { - base::TimeTicks activation_time = base::TimeTicks() + - base::TimeDelta::FromMicroseconds(min_baseline) + latency_length_; - if (activation_time < desired_time) - desired_time = activation_time; - } - - timer_.Start( - FROM_HERE, (desired_time - now).magnitude(), - base::Bind(&DevToolsNetworkInterceptor::OnTimer, base::Unretained(this))); -} - -int DevToolsNetworkInterceptor::StartThrottle( - int result, - int64_t bytes, - base::TimeTicks send_end, - bool start, - bool is_upload, - const ThrottleCallback& callback) { - if (result < 0) - return result; - - if (conditions_->offline()) - return is_upload ? result : net::ERR_INTERNET_DISCONNECTED; - - if ((is_upload && !conditions_->upload_throughput()) || - (!is_upload && !conditions_->download_throughput())) { - return result; - } - - ThrottleRecord record; - record.result = result; - record.bytes = bytes; - record.callback = callback; - record.is_upload = is_upload; - - base::TimeTicks now = base::TimeTicks::Now(); - UpdateThrottled(now); - if (start && latency_length_ != base::TimeDelta()) { - record.send_end = (send_end - base::TimeTicks()).InMicroseconds(); - suspended_.push_back(record); - UpdateSuspended(now); - } else { - if (is_upload) - upload_.push_back(record); - else - download_.push_back(record); - } - ArmTimer(now); - - return net::ERR_IO_PENDING; -} - -void DevToolsNetworkInterceptor::StopThrottle( - const ThrottleCallback& callback) { - RemoveRecord(&download_, callback); - RemoveRecord(&upload_, callback); - RemoveRecord(&suspended_, callback); -} - -void DevToolsNetworkInterceptor::RemoveRecord( - ThrottleRecords* records, const ThrottleCallback& callback) { - records->erase( - std::remove_if(records->begin(), records->end(), - [&callback](const ThrottleRecord& record){ - return record.callback.Equals(callback); - }), - records->end()); -} - -bool DevToolsNetworkInterceptor::IsOffline() { - return conditions_->offline(); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_interceptor.h b/brightray/browser/net/devtools_network_interceptor.h deleted file mode 100644 index 08f5f672164..00000000000 --- a/brightray/browser/net/devtools_network_interceptor.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_INTERCEPTOR_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_INTERCEPTOR_H_ - -#include -#include -#include - -#include "base/macros.h" -#include "base/timer/timer.h" - -namespace base { -class TimeDelta; -class TimeTicks; -} - -namespace brightray { - -class DevToolsNetworkConditions; -class DevToolsNetworkTransaction; - -class DevToolsNetworkInterceptor { - public: - using ThrottleCallback = base::Callback; - - DevToolsNetworkInterceptor(); - virtual ~DevToolsNetworkInterceptor(); - - base::WeakPtr GetWeakPtr(); - - // Applies network emulation configuration. - void UpdateConditions(std::unique_ptr conditions); - - // Throttles with |is_upload == true| always succeed, even in offline mode. - int StartThrottle(int result, - int64_t bytes, - base::TimeTicks send_end, - bool start, - bool is_upload, - const ThrottleCallback& callback); - void StopThrottle(const ThrottleCallback& callback); - - bool IsOffline(); - - private: - struct ThrottleRecord { - public: - ThrottleRecord(); - ThrottleRecord(const ThrottleRecord& other); - ~ThrottleRecord(); - - int result; - int64_t bytes; - int64_t send_end; - bool is_upload; - ThrottleCallback callback; - }; - - using ThrottleRecords = std::vector; - - void FinishRecords(ThrottleRecords* records, bool offline); - - uint64_t UpdateThrottledRecords(base::TimeTicks now, - ThrottleRecords* records, - uint64_t last_tick, - base::TimeDelta tick_length); - void UpdateThrottled(base::TimeTicks now); - void UpdateSuspended(base::TimeTicks now); - - void CollectFinished(ThrottleRecords* records, ThrottleRecords* finished); - void OnTimer(); - - base::TimeTicks CalculateDesiredTime(const ThrottleRecords& records, - uint64_t last_tick, - base::TimeDelta tick_length); - void ArmTimer(base::TimeTicks now); - - void RemoveRecord(ThrottleRecords* records, const ThrottleCallback& callback); - - std::unique_ptr conditions_; - - // Throttables suspended for a "latency" period. - ThrottleRecords suspended_; - - // Throttables waiting for certain amount of transfer to be "accounted". - ThrottleRecords download_; - ThrottleRecords upload_; - - base::OneShotTimer timer_; - base::TimeTicks offset_; - base::TimeDelta download_tick_length_; - base::TimeDelta upload_tick_length_; - base::TimeDelta latency_length_; - uint64_t download_last_tick_; - uint64_t upload_last_tick_; - - base::WeakPtrFactory weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkInterceptor); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_INTERCEPTOR_H_ diff --git a/brightray/browser/net/devtools_network_protocol_handler.cc b/brightray/browser/net/devtools_network_protocol_handler.cc deleted file mode 100644 index caf738435fd..00000000000 --- a/brightray/browser/net/devtools_network_protocol_handler.cc +++ /dev/null @@ -1,173 +0,0 @@ -// Copyright (c) 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#include "brightray/browser/net/devtools_network_protocol_handler.h" - -#include -#include - -#include "base/memory/ptr_util.h" -#include "base/strings/stringprintf.h" -#include "brightray/browser/browser_context.h" -#include "brightray/browser/net/devtools_network_conditions.h" -#include "brightray/browser/net/devtools_network_controller.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/devtools_agent_host.h" - -namespace brightray { - -namespace { - -namespace params { - -const char kDownloadThroughput[] = "downloadThroughput"; -const char kLatency[] = "latency"; -const char kOffline[] = "offline"; -const char kUploadThroughput[] = "uploadThroughput"; -const char kResult[] = "result"; -const char kErrorCode[] = "code"; -const char kErrorMessage[] = "message"; - -} // namespace params - -const char kEmulateNetworkConditions[] = "Network.emulateNetworkConditions"; -const char kCanEmulateNetworkConditions[] = - "Network.canEmulateNetworkConditions"; -const char kId[] = "id"; -const char kMethod[] = "method"; -const char kParams[] = "params"; -const char kError[] = "error"; -// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object -const int kErrorInvalidParams = -32602; - -bool ParseCommand(const base::DictionaryValue* command, - int* id, - std::string* method, - const base::DictionaryValue** params) { - if (!command) - return false; - - if (!command->GetInteger(kId, id) || *id < 0) - return false; - - if (!command->GetString(kMethod, method)) - return false; - - if (!command->GetDictionary(kParams, params)) - *params = nullptr; - - return true; -} - -std::unique_ptr -CreateSuccessResponse(int id, std::unique_ptr result) { - auto response = base::MakeUnique(); - response->SetInteger(kId, id); - response->Set(params::kResult, std::move(result)); - return response; -} - -std::unique_ptr -CreateFailureResponse(int id, const std::string& param) { - auto response = base::MakeUnique(); - auto error_object = base::MakeUnique(); - error_object->SetInteger(params::kErrorCode, kErrorInvalidParams); - error_object->SetString(params::kErrorMessage, - base::StringPrintf("Missing or Invalid '%s' parameter", param.c_str())); - response->Set(kError, std::move(error_object)); - return response; -} - -} // namespace - -DevToolsNetworkProtocolHandler::DevToolsNetworkProtocolHandler() { -} - -DevToolsNetworkProtocolHandler::~DevToolsNetworkProtocolHandler() { -} - -base::DictionaryValue* DevToolsNetworkProtocolHandler::HandleCommand( - content::DevToolsAgentHost* agent_host, - base::DictionaryValue* command) { - int id = 0; - std::string method; - const base::DictionaryValue* params = nullptr; - - if (!ParseCommand(command, &id, &method, ¶ms)) - return nullptr; - - if (method == kEmulateNetworkConditions) - return EmulateNetworkConditions(agent_host, id, params).release(); - - if (method == kCanEmulateNetworkConditions) - return CanEmulateNetworkConditions(agent_host, id, params).release(); - - return nullptr; -} - -void DevToolsNetworkProtocolHandler::DevToolsAgentStateChanged( - content::DevToolsAgentHost* agent_host, - bool attached) { - std::unique_ptr conditions; - if (attached) - conditions.reset(new DevToolsNetworkConditions(false)); - UpdateNetworkState(agent_host, std::move(conditions)); -} - -std::unique_ptr -DevToolsNetworkProtocolHandler::CanEmulateNetworkConditions( - content::DevToolsAgentHost* agent_host, - int id, - const base::DictionaryValue* params) { - std::unique_ptr result(new base::DictionaryValue); - result->SetBoolean(params::kResult, true); - return CreateSuccessResponse(id, std::move(result)); -} - -std::unique_ptr -DevToolsNetworkProtocolHandler::EmulateNetworkConditions( - content::DevToolsAgentHost* agent_host, - int id, - const base::DictionaryValue* params) { - bool offline = false; - if (!params || !params->GetBoolean(params::kOffline, &offline)) - return CreateFailureResponse(id, params::kOffline); - - double latency = 0.0; - if (!params->GetDouble(params::kLatency, &latency)) - return CreateFailureResponse(id, params::kLatency); - if (latency < 0.0) - latency = 0.0; - - double download_throughput = 0.0; - if (!params->GetDouble(params::kDownloadThroughput, &download_throughput)) - return CreateFailureResponse(id, params::kDownloadThroughput); - if (download_throughput < 0.0) - download_throughput = 0.0; - - double upload_throughput = 0.0; - if (!params->GetDouble(params::kUploadThroughput, &upload_throughput)) - return CreateFailureResponse(id, params::kUploadThroughput); - if (upload_throughput < 0.0) - upload_throughput = 0.0; - - std::unique_ptr conditions( - new DevToolsNetworkConditions(offline, - latency, - download_throughput, - upload_throughput)); - UpdateNetworkState(agent_host, std::move(conditions)); - return std::unique_ptr(); -} - -void DevToolsNetworkProtocolHandler::UpdateNetworkState( - content::DevToolsAgentHost* agent_host, - std::unique_ptr conditions) { - auto browser_context = - static_cast(agent_host->GetBrowserContext()); - browser_context->network_controller_handle()->SetNetworkState( - agent_host->GetId(), std::move(conditions)); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_protocol_handler.h b/brightray/browser/net/devtools_network_protocol_handler.h deleted file mode 100644 index 8efa3468fe1..00000000000 --- a/brightray/browser/net/devtools_network_protocol_handler.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_ - -#include "base/macros.h" -#include "base/values.h" - -namespace content { -class DevToolsAgentHost; -} - -namespace brightray { - -class DevToolsNetworkConditions; - -class DevToolsNetworkProtocolHandler { - public: - DevToolsNetworkProtocolHandler(); - ~DevToolsNetworkProtocolHandler(); - - base::DictionaryValue* HandleCommand( - content::DevToolsAgentHost* agent_host, - base::DictionaryValue* command); - void DevToolsAgentStateChanged(content::DevToolsAgentHost* agent_host, - bool attached); - - private: - std::unique_ptr CanEmulateNetworkConditions( - content::DevToolsAgentHost* agent_host, - int command_id, - const base::DictionaryValue* params); - std::unique_ptr EmulateNetworkConditions( - content::DevToolsAgentHost* agent_host, - int command_id, - const base::DictionaryValue* params); - void UpdateNetworkState( - content::DevToolsAgentHost* agent_host, - std::unique_ptr conditions); - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkProtocolHandler); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_PROTOCOL_HANDLER_H_ diff --git a/brightray/browser/net/devtools_network_transaction.cc b/brightray/browser/net/devtools_network_transaction.cc deleted file mode 100644 index 1c18ad17120..00000000000 --- a/brightray/browser/net/devtools_network_transaction.cc +++ /dev/null @@ -1,302 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#include "brightray/browser/net/devtools_network_transaction.h" - -#include -#include - -#include "brightray/browser/net/devtools_network_controller.h" -#include "brightray/browser/net/devtools_network_upload_data_stream.h" -#include "net/base/load_timing_info.h" -#include "net/base/net_errors.h" -#include "net/base/upload_progress.h" -#include "net/http/http_network_transaction.h" -#include "net/http/http_request_info.h" -#include "net/socket/connection_attempts.h" - -namespace brightray { - -// static -const char - DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId[] = - "X-DevTools-Emulate-Network-Conditions-Client-Id"; - -DevToolsNetworkTransaction::DevToolsNetworkTransaction( - DevToolsNetworkController* controller, - std::unique_ptr transaction) - : throttled_byte_count_(0), - controller_(controller), - transaction_(std::move(transaction)), - request_(nullptr), - failed_(false) { - DCHECK(controller); -} - -DevToolsNetworkTransaction::~DevToolsNetworkTransaction() { - if (interceptor_ && !throttle_callback_.is_null()) - interceptor_->StopThrottle(throttle_callback_); -} - -void DevToolsNetworkTransaction::IOCallback( - const net::CompletionCallback& callback, bool start, int result) { - result = Throttle(callback, start, result); - if (result != net::ERR_IO_PENDING) - callback.Run(result); -} - -int DevToolsNetworkTransaction::Throttle( - const net::CompletionCallback& callback, bool start, int result) { - if (failed_) - return net::ERR_INTERNET_DISCONNECTED; - if (!interceptor_ || result < 0) - return result; - - base::TimeTicks send_end; - if (start) { - throttled_byte_count_ += transaction_->GetTotalReceivedBytes(); - net::LoadTimingInfo load_timing_info; - if (GetLoadTimingInfo(&load_timing_info)) - send_end = load_timing_info.send_end; - if (send_end.is_null()) - send_end = base::TimeTicks::Now(); - } - if (result > 0) - throttled_byte_count_ += result; - - throttle_callback_ = base::Bind(&DevToolsNetworkTransaction::ThrottleCallback, - base::Unretained(this), - callback); - int rv = interceptor_->StartThrottle(result, throttled_byte_count_, send_end, - start, false, throttle_callback_); - if (rv != net::ERR_IO_PENDING) - throttle_callback_.Reset(); - if (rv == net::ERR_INTERNET_DISCONNECTED) - Fail(); - return rv; -} - -void DevToolsNetworkTransaction::ThrottleCallback( - const net::CompletionCallback& callback, int result, int64_t bytes) { - DCHECK(!throttle_callback_.is_null()); - throttle_callback_.Reset(); - if (result == net::ERR_INTERNET_DISCONNECTED) - Fail(); - throttled_byte_count_ = bytes; - callback.Run(result); -} - -void DevToolsNetworkTransaction::Fail() { - DCHECK(request_); - DCHECK(!failed_); - failed_ = true; - transaction_->SetBeforeNetworkStartCallback(BeforeNetworkStartCallback()); - if (interceptor_) - interceptor_.reset(); -} - -bool DevToolsNetworkTransaction::CheckFailed() { - if (failed_) - return true; - if (interceptor_ && interceptor_->IsOffline()) { - Fail(); - return true; - } - return false; -} - -int DevToolsNetworkTransaction::Start(const net::HttpRequestInfo* request, - const net::CompletionCallback& callback, - const net::NetLogWithSource& net_log) { - DCHECK(request); - request_ = request; - - std::string client_id; - bool has_devtools_client_id = request_->extra_headers.HasHeader( - kDevToolsEmulateNetworkConditionsClientId); - if (has_devtools_client_id) { - custom_request_.reset(new net::HttpRequestInfo(*request_)); - custom_request_->extra_headers.GetHeader( - kDevToolsEmulateNetworkConditionsClientId, &client_id); - custom_request_->extra_headers.RemoveHeader( - kDevToolsEmulateNetworkConditionsClientId); - - if (request_->upload_data_stream) { - custom_upload_data_stream_.reset( - new DevToolsNetworkUploadDataStream(request_->upload_data_stream)); - custom_request_->upload_data_stream = custom_upload_data_stream_.get(); - } - - request_ = custom_request_.get(); - } - - DevToolsNetworkInterceptor* interceptor = - controller_->GetInterceptor(client_id); - if (interceptor) { - interceptor_ = interceptor->GetWeakPtr(); - if (custom_upload_data_stream_) - custom_upload_data_stream_->SetInterceptor(interceptor); - } - - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - - if (!interceptor_) - return transaction_->Start(request_, callback, net_log); - - int result = transaction_->Start(request_, - base::Bind(&DevToolsNetworkTransaction::IOCallback, - base::Unretained(this), callback, true), - net_log); - return Throttle(callback, true, result); -} - -int DevToolsNetworkTransaction::RestartIgnoringLastError( - const net::CompletionCallback& callback) { - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - if (!interceptor_) - return transaction_->RestartIgnoringLastError(callback); - - int result = transaction_->RestartIgnoringLastError( - base::Bind(&DevToolsNetworkTransaction::IOCallback, - base::Unretained(this), callback, true)); - return Throttle(callback, true, result); -} - -int DevToolsNetworkTransaction::RestartWithCertificate( - scoped_refptr client_cert, - scoped_refptr client_private_key, - const net::CompletionCallback& callback) { - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - if (!interceptor_) { - return transaction_->RestartWithCertificate( - std::move(client_cert), std::move(client_private_key), callback); - } - - int result = transaction_->RestartWithCertificate( - std::move(client_cert), std::move(client_private_key), - base::Bind(&DevToolsNetworkTransaction::IOCallback, - base::Unretained(this), callback, true)); - return Throttle(callback, true, result); -} - -int DevToolsNetworkTransaction::RestartWithAuth( - const net::AuthCredentials& credentials, - const net::CompletionCallback& callback) { - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - if (!interceptor_) - return transaction_->RestartWithAuth(credentials, callback); - - int result = transaction_->RestartWithAuth(credentials, - base::Bind(&DevToolsNetworkTransaction::IOCallback, - base::Unretained(this), callback, true)); - return Throttle(callback, true, result); -} - -bool DevToolsNetworkTransaction::IsReadyToRestartForAuth() { - return transaction_->IsReadyToRestartForAuth(); -} - -int DevToolsNetworkTransaction::Read( - net::IOBuffer* buf, - int buf_len, - const net::CompletionCallback& callback) { - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - if (!interceptor_) - return transaction_->Read(buf, buf_len, callback); - - int result = transaction_->Read(buf, buf_len, - base::Bind(&DevToolsNetworkTransaction::IOCallback, - base::Unretained(this), callback, false)); - // URLRequestJob relies on synchronous end-of-stream notification. - if (result == 0) - return result; - return Throttle(callback, false, result); -} - -void DevToolsNetworkTransaction::StopCaching() { - transaction_->StopCaching(); -} - -bool DevToolsNetworkTransaction::GetFullRequestHeaders( - net::HttpRequestHeaders* headers) const { - return transaction_->GetFullRequestHeaders(headers); -} - -int64_t DevToolsNetworkTransaction::GetTotalReceivedBytes() const { - return transaction_->GetTotalReceivedBytes(); -} - -int64_t DevToolsNetworkTransaction::GetTotalSentBytes() const { - return transaction_->GetTotalSentBytes(); -} - -void DevToolsNetworkTransaction::DoneReading() { - transaction_->DoneReading(); -} - -const net::HttpResponseInfo* -DevToolsNetworkTransaction::GetResponseInfo() const { - return transaction_->GetResponseInfo(); -} - -net::LoadState DevToolsNetworkTransaction::GetLoadState() const { - return transaction_->GetLoadState(); -} - -void DevToolsNetworkTransaction::SetQuicServerInfo( - net::QuicServerInfo* info) { - transaction_->SetQuicServerInfo(info); -} - -bool DevToolsNetworkTransaction::GetLoadTimingInfo( - net::LoadTimingInfo* info) const { - return transaction_->GetLoadTimingInfo(info); -} - -bool DevToolsNetworkTransaction::GetRemoteEndpoint( - net::IPEndPoint* endpoint) const { - return transaction_->GetRemoteEndpoint(endpoint); -} - -void DevToolsNetworkTransaction::PopulateNetErrorDetails( - net::NetErrorDetails* details) const { - return transaction_->PopulateNetErrorDetails(details); -} - -void DevToolsNetworkTransaction::SetPriority(net::RequestPriority priority) { - transaction_->SetPriority(priority); -} - -void DevToolsNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( - net::WebSocketHandshakeStreamBase::CreateHelper* helper) { - transaction_->SetWebSocketHandshakeStreamCreateHelper(helper); -} - -void DevToolsNetworkTransaction::SetBeforeNetworkStartCallback( - const BeforeNetworkStartCallback& callback) { - transaction_->SetBeforeNetworkStartCallback(callback); -} - -void DevToolsNetworkTransaction::SetBeforeHeadersSentCallback( - const BeforeHeadersSentCallback& callback) { - transaction_->SetBeforeHeadersSentCallback(callback); -} - -int DevToolsNetworkTransaction::ResumeNetworkStart() { - if (CheckFailed()) - return net::ERR_INTERNET_DISCONNECTED; - return transaction_->ResumeNetworkStart(); -} - -void DevToolsNetworkTransaction::GetConnectionAttempts( - net::ConnectionAttempts* out) const { - transaction_->GetConnectionAttempts(out); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_transaction.h b/brightray/browser/net/devtools_network_transaction.h deleted file mode 100644 index d7317e47ac4..00000000000 --- a/brightray/browser/net/devtools_network_transaction.h +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_H_ - -#include - -#include "base/memory/weak_ptr.h" -#include "brightray/browser/net/devtools_network_interceptor.h" -#include "net/base/completion_callback.h" -#include "net/base/load_states.h" -#include "net/base/request_priority.h" -#include "net/http/http_raw_request_headers.h" -#include "net/http/http_transaction.h" - -namespace brightray { - -class DevToolsNetworkController; -class DevToolsNetworkUploadDataStream; - -class DevToolsNetworkTransaction : public net::HttpTransaction { - public: - static const char kDevToolsEmulateNetworkConditionsClientId[]; - - DevToolsNetworkTransaction( - DevToolsNetworkController* controller, - std::unique_ptr network_transaction); - ~DevToolsNetworkTransaction() override; - - // HttpTransaction methods: - int Start(const net::HttpRequestInfo* request, - const net::CompletionCallback& callback, - const net::NetLogWithSource& net_log) override; - int RestartIgnoringLastError( - const net::CompletionCallback& callback) override; - int RestartWithCertificate( - scoped_refptr client_cert, - scoped_refptr client_private_key, - const net::CompletionCallback& callback) override; - int RestartWithAuth(const net::AuthCredentials& credentials, - const net::CompletionCallback& callback) override; - bool IsReadyToRestartForAuth() override; - - int Read(net::IOBuffer* buf, - int buf_len, - const net::CompletionCallback& callback) override; - void StopCaching() override; - bool GetFullRequestHeaders(net::HttpRequestHeaders* headers) const override; - int64_t GetTotalReceivedBytes() const override; - int64_t GetTotalSentBytes() const override; - void DoneReading() override; - const net::HttpResponseInfo* GetResponseInfo() const override; - net::LoadState GetLoadState() const override; - void SetQuicServerInfo(net::QuicServerInfo* quic_server_info) override; - bool GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; - bool GetRemoteEndpoint(net::IPEndPoint* endpoint) const override; - void PopulateNetErrorDetails(net::NetErrorDetails* details) const override; - void SetPriority(net::RequestPriority priority) override; - void SetWebSocketHandshakeStreamCreateHelper( - net::WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; - void SetBeforeNetworkStartCallback( - const BeforeNetworkStartCallback& callback) override; - void SetBeforeHeadersSentCallback( - const BeforeHeadersSentCallback& callback) override; - int ResumeNetworkStart() override; - void GetConnectionAttempts(net::ConnectionAttempts* out) const override; - - // FIXME(torycl) Implement these methods properly - void SetRequestHeadersCallback(net::RequestHeadersCallback callback) {} - void SetResponseHeadersCallback(net::ResponseHeadersCallback callback) {} - - private: - void Fail(); - bool CheckFailed(); - - void IOCallback(const net::CompletionCallback& callback, - bool start, - int result); - int Throttle(const net::CompletionCallback& callback, - bool start, - int result); - void ThrottleCallback(const net::CompletionCallback& callback, - int result, - int64_t bytes); - - DevToolsNetworkInterceptor::ThrottleCallback throttle_callback_; - int64_t throttled_byte_count_; - - DevToolsNetworkController* controller_; - base::WeakPtr interceptor_; - - // Modified upload data stream. Should be destructed after |custom_request_|. - std::unique_ptr custom_upload_data_stream_; - - // Modified request. Should be destructed after |transaction_|. - std::unique_ptr custom_request_; - - // Original network transaction. - std::unique_ptr transaction_; - - const net::HttpRequestInfo* request_; - - // True if Fail was already invoked. - bool failed_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransaction); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_H_ diff --git a/brightray/browser/net/devtools_network_transaction_factory.cc b/brightray/browser/net/devtools_network_transaction_factory.cc deleted file mode 100644 index 98c7bb57e8a..00000000000 --- a/brightray/browser/net/devtools_network_transaction_factory.cc +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#include "brightray/browser/net/devtools_network_transaction_factory.h" - -#include -#include - -#include "brightray/browser/net/devtools_network_controller.h" -#include "brightray/browser/net/devtools_network_transaction.h" -#include "content/public/browser/service_worker_context.h" -#include "net/base/net_errors.h" -#include "net/http/http_network_layer.h" -#include "net/http/http_network_transaction.h" - -namespace brightray { - -DevToolsNetworkTransactionFactory::DevToolsNetworkTransactionFactory( - DevToolsNetworkController* controller, - net::HttpNetworkSession* session) - : controller_(controller), - network_layer_(new net::HttpNetworkLayer(session)) { - std::set headers; - headers.insert( - DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId); - content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent(headers); -} - -DevToolsNetworkTransactionFactory::~DevToolsNetworkTransactionFactory() { -} - -int DevToolsNetworkTransactionFactory::CreateTransaction( - net::RequestPriority priority, - std::unique_ptr* transaction) { - std::unique_ptr new_transaction; - int rv = network_layer_->CreateTransaction(priority, &new_transaction); - if (rv != net::OK) - return rv; - transaction->reset( - new DevToolsNetworkTransaction(controller_, std::move(new_transaction))); - return net::OK; -} - -net::HttpCache* DevToolsNetworkTransactionFactory::GetCache() { - return network_layer_->GetCache(); -} - -net::HttpNetworkSession* DevToolsNetworkTransactionFactory::GetSession() { - return network_layer_->GetSession(); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_transaction_factory.h b/brightray/browser/net/devtools_network_transaction_factory.h deleted file mode 100644 index e303cc24362..00000000000 --- a/brightray/browser/net/devtools_network_transaction_factory.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE-CHROMIUM file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_ - -#include "base/macros.h" -#include "net/base/request_priority.h" -#include "net/http/http_transaction_factory.h" - -namespace brightray { - -class DevToolsNetworkController; - -class DevToolsNetworkTransactionFactory : public net::HttpTransactionFactory { - public: - explicit DevToolsNetworkTransactionFactory( - DevToolsNetworkController* controller, - net::HttpNetworkSession* session); - ~DevToolsNetworkTransactionFactory() override; - - // net::HttpTransactionFactory: - int CreateTransaction( - net::RequestPriority priority, - std::unique_ptr* transaction) override; - net::HttpCache* GetCache() override; - net::HttpNetworkSession* GetSession() override; - - private: - DevToolsNetworkController* controller_; - std::unique_ptr network_layer_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkTransactionFactory); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_TRANSACTION_FACTORY_H_ diff --git a/brightray/browser/net/devtools_network_upload_data_stream.cc b/brightray/browser/net/devtools_network_upload_data_stream.cc deleted file mode 100644 index 73a37541875..00000000000 --- a/brightray/browser/net/devtools_network_upload_data_stream.cc +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "brightray/browser/net/devtools_network_upload_data_stream.h" - -#include "net/base/net_errors.h" - -namespace brightray { - -DevToolsNetworkUploadDataStream::DevToolsNetworkUploadDataStream( - net::UploadDataStream* upload_data_stream) - : net::UploadDataStream(upload_data_stream->is_chunked(), - upload_data_stream->identifier()), - throttle_callback_( - base::Bind(&DevToolsNetworkUploadDataStream::ThrottleCallback, - base::Unretained(this))), - throttled_byte_count_(0), - upload_data_stream_(upload_data_stream) { -} - -DevToolsNetworkUploadDataStream::~DevToolsNetworkUploadDataStream() { - if (interceptor_) - interceptor_->StopThrottle(throttle_callback_); -} - -void DevToolsNetworkUploadDataStream::SetInterceptor( - DevToolsNetworkInterceptor* interceptor) { - DCHECK(!interceptor_); - if (interceptor) - interceptor_ = interceptor->GetWeakPtr(); -} - -bool DevToolsNetworkUploadDataStream::IsInMemory() const { - return false; -} - -int DevToolsNetworkUploadDataStream::InitInternal( - const net::NetLogWithSource& net_log) { - throttled_byte_count_ = 0; - int result = upload_data_stream_->Init( - base::Bind(&DevToolsNetworkUploadDataStream::StreamInitCallback, - base::Unretained(this)), - net_log); - if (result == net::OK && !is_chunked()) - SetSize(upload_data_stream_->size()); - return result; -} - -void DevToolsNetworkUploadDataStream::StreamInitCallback(int result) { - if (!is_chunked()) - SetSize(upload_data_stream_->size()); - OnInitCompleted(result); -} - -int DevToolsNetworkUploadDataStream::ReadInternal( - net::IOBuffer* buf, int buf_len) { - int result = upload_data_stream_->Read(buf, buf_len, - base::Bind(&DevToolsNetworkUploadDataStream::StreamReadCallback, - base::Unretained(this))); - return ThrottleRead(result); -} - -void DevToolsNetworkUploadDataStream::StreamReadCallback(int result) { - result = ThrottleRead(result); - if (result != net::ERR_IO_PENDING) - OnReadCompleted(result); -} - -int DevToolsNetworkUploadDataStream::ThrottleRead(int result) { - if (is_chunked() && upload_data_stream_->IsEOF()) - SetIsFinalChunk(); - - if (!interceptor_ || result < 0) - return result; - - if (result > 0) - throttled_byte_count_ += result; - return interceptor_->StartThrottle(result, throttled_byte_count_, - base::TimeTicks(), false, true, throttle_callback_); -} - -void DevToolsNetworkUploadDataStream::ThrottleCallback( - int result, int64_t bytes) { - throttled_byte_count_ = bytes; - OnReadCompleted(result); -} - -void DevToolsNetworkUploadDataStream::ResetInternal() { - upload_data_stream_->Reset(); - throttled_byte_count_ = 0; - if (interceptor_) - interceptor_->StopThrottle(throttle_callback_); -} - -} // namespace brightray diff --git a/brightray/browser/net/devtools_network_upload_data_stream.h b/brightray/browser/net/devtools_network_upload_data_stream.h deleted file mode 100644 index 1f2921b61da..00000000000 --- a/brightray/browser/net/devtools_network_upload_data_stream.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_ -#define BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_ - -#include - -#include "base/macros.h" -#include "base/memory/weak_ptr.h" -#include "brightray/browser/net/devtools_network_interceptor.h" -#include "net/base/upload_data_stream.h" - -namespace brightray { - -class DevToolsNetworkUploadDataStream : public net::UploadDataStream { - public: - // Supplied |upload_data_stream| must outlive this object. - explicit DevToolsNetworkUploadDataStream( - net::UploadDataStream* upload_data_stream); - ~DevToolsNetworkUploadDataStream() override; - - void SetInterceptor(DevToolsNetworkInterceptor* interceptor); - - private: - // net::UploadDataStream implementation. - bool IsInMemory() const override; - int InitInternal(const net::NetLogWithSource& net_log) override; - int ReadInternal(net::IOBuffer* buf, int buf_len) override; - void ResetInternal() override; - - void StreamInitCallback(int result); - void StreamReadCallback(int result); - - int ThrottleRead(int result); - void ThrottleCallback(int result, int64_t bytes); - - DevToolsNetworkInterceptor::ThrottleCallback throttle_callback_; - int64_t throttled_byte_count_; - - net::UploadDataStream* upload_data_stream_; - base::WeakPtr interceptor_; - - DISALLOW_COPY_AND_ASSIGN(DevToolsNetworkUploadDataStream); -}; - -} // namespace brightray - -#endif // BRIGHTRAY_BROWSER_NET_DEVTOOLS_NETWORK_UPLOAD_DATA_STREAM_H_ diff --git a/brightray/browser/url_request_context_getter.cc b/brightray/browser/url_request_context_getter.cc index 80c2633f32b..e1a1af6c4b9 100644 --- a/brightray/browser/url_request_context_getter.cc +++ b/brightray/browser/url_request_context_getter.cc @@ -13,8 +13,6 @@ #include "base/task_scheduler/post_task.h" #include "base/threading/sequenced_worker_pool.h" #include "brightray/browser/browser_client.h" -#include "brightray/browser/net/devtools_network_controller_handle.h" -#include "brightray/browser/net/devtools_network_transaction_factory.h" #include "brightray/browser/net/require_ct_delegate.h" #include "brightray/browser/net_log.h" #include "brightray/browser/network_delegate.h" @@ -22,6 +20,7 @@ #include "components/network_session_configurator/common/network_switches.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/cookie_store_factory.h" +#include "content/public/browser/devtools_network_transaction_factory.h" #include "content/public/common/content_switches.h" #include "net/base/host_mapping_rules.h" #include "net/cert/cert_verifier.h" @@ -120,7 +119,6 @@ URLRequestContextGetter::Delegate::GetCookieableSchemes() { URLRequestContextGetter::URLRequestContextGetter( Delegate* delegate, - DevToolsNetworkControllerHandle* handle, NetLog* net_log, const base::FilePath& base_path, bool in_memory, @@ -128,7 +126,6 @@ URLRequestContextGetter::URLRequestContextGetter( content::ProtocolHandlerMap* protocol_handlers, content::URLRequestInterceptorScopedVector protocol_interceptors) : delegate_(delegate), - network_controller_handle_(handle), net_log_(net_log), base_path_(base_path), in_memory_(in_memory), @@ -335,20 +332,10 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() { backend.reset(delegate_->CreateHttpCacheBackendFactory(base_path_)); } - if (network_controller_handle_) { - storage_->set_http_transaction_factory(base::WrapUnique( - new net::HttpCache( - base::WrapUnique(new DevToolsNetworkTransactionFactory( - network_controller_handle_->GetController(), - http_network_session_.get())), - std::move(backend), - false))); - } else { - storage_->set_http_transaction_factory(base::WrapUnique( - new net::HttpCache(http_network_session_.get(), - std::move(backend), - false))); - } + storage_->set_http_transaction_factory(base::MakeUnique( + content::CreateDevToolsNetworkTransactionFactory( + http_network_session_.get()), + std::move(backend), false)); std::unique_ptr job_factory = delegate_->CreateURLRequestJobFactory(&protocol_handlers_); diff --git a/brightray/browser/url_request_context_getter.h b/brightray/browser/url_request_context_getter.h index 51b0ed53226..5310e1f48f5 100644 --- a/brightray/browser/url_request_context_getter.h +++ b/brightray/browser/url_request_context_getter.h @@ -35,7 +35,6 @@ class URLRequestJobFactory; namespace brightray { class RequireCTDelegate; -class DevToolsNetworkControllerHandle; class NetLog; class URLRequestContextGetter : public net::URLRequestContextGetter { @@ -64,7 +63,6 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { URLRequestContextGetter( Delegate* delegate, - DevToolsNetworkControllerHandle* handle, NetLog* net_log, const base::FilePath& base_path, bool in_memory, @@ -88,7 +86,6 @@ class URLRequestContextGetter : public net::URLRequestContextGetter { private: Delegate* delegate_; - DevToolsNetworkControllerHandle* network_controller_handle_; NetLog* net_log_; base::FilePath base_path_; bool in_memory_; diff --git a/brightray/filenames.gypi b/brightray/filenames.gypi index 9ddd990cfa5..c6c7d4e8e66 100644 --- a/brightray/filenames.gypi +++ b/brightray/filenames.gypi @@ -47,22 +47,6 @@ 'browser/media/media_device_id_salt.h', 'browser/media/media_stream_devices_controller.cc', 'browser/media/media_stream_devices_controller.h', - 'browser/net/devtools_network_conditions.cc', - 'browser/net/devtools_network_conditions.h', - 'browser/net/devtools_network_controller.cc', - 'browser/net/devtools_network_controller.h', - 'browser/net/devtools_network_controller_handle.cc', - 'browser/net/devtools_network_controller_handle.h', - 'browser/net/devtools_network_interceptor.cc', - 'browser/net/devtools_network_interceptor.h', - 'browser/net/devtools_network_protocol_handler.cc', - 'browser/net/devtools_network_protocol_handler.h', - 'browser/net/devtools_network_transaction_factory.cc', - 'browser/net/devtools_network_transaction_factory.h', - 'browser/net/devtools_network_transaction.cc', - 'browser/net/devtools_network_transaction.h', - 'browser/net/devtools_network_upload_data_stream.cc', - 'browser/net/devtools_network_upload_data_stream.h', 'browser/net/require_ct_delegate.cc', 'browser/net/require_ct_delegate.h', 'browser/net_log.cc',