Chromium Code Reviews| Index: chrome/browser/devtools/devtools_network_interceptor.cc |
| diff --git a/chrome/browser/devtools/devtools_network_controller.cc b/chrome/browser/devtools/devtools_network_interceptor.cc |
| similarity index 53% |
| copy from chrome/browser/devtools/devtools_network_controller.cc |
| copy to chrome/browser/devtools/devtools_network_interceptor.cc |
| index 176ac6655dba216d1251f908d5f0fcba088fceca..5f95f714520be73449a22add8437c7e79c4dfde9 100644 |
| --- a/chrome/browser/devtools/devtools_network_controller.cc |
| +++ b/chrome/browser/devtools/devtools_network_interceptor.cc |
| @@ -2,139 +2,95 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/devtools/devtools_network_controller.h" |
| +#include "chrome/browser/devtools/devtools_network_interceptor.h" |
| #include "base/time/time.h" |
| #include "chrome/browser/devtools/devtools_network_conditions.h" |
| #include "chrome/browser/devtools/devtools_network_transaction.h" |
| -#include "chrome/browser/profiles/profile.h" |
| -#include "chrome/browser/profiles/profile_io_data.h" |
| -#include "content/public/browser/browser_thread.h" |
| -#include "content/public/browser/resource_context.h" |
| - |
| -using content::BrowserThread; |
| namespace { |
| -const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; |
| int64_t kPacketSize = 1500; |
| } // namespace |
| -DevToolsNetworkController::DevToolsNetworkController() |
| - : weak_ptr_factory_(this) { |
| +DevToolsNetworkInterceptor::DevToolsNetworkInterceptor() |
| + : conditions_(new DevToolsNetworkConditions()), |
| + weak_ptr_factory_(this) { |
| +} |
| + |
| +DevToolsNetworkInterceptor::~DevToolsNetworkInterceptor() { |
| } |
| -DevToolsNetworkController::~DevToolsNetworkController() { |
| +base::WeakPtr<DevToolsNetworkInterceptor> |
| +DevToolsNetworkInterceptor::GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| } |
| -void DevToolsNetworkController::AddTransaction( |
| +void DevToolsNetworkInterceptor::AddTransaction( |
| DevToolsNetworkTransaction* transaction) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(transactions_.find(transaction) == transactions_.end()); |
| transactions_.insert(transaction); |
| } |
| -void DevToolsNetworkController::RemoveTransaction( |
| +void DevToolsNetworkInterceptor::RemoveTransaction( |
| DevToolsNetworkTransaction* transaction) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| DCHECK(transactions_.find(transaction) != transactions_.end()); |
| transactions_.erase(transaction); |
| - if (conditions_ && conditions_->IsThrottling()) { |
| - UpdateThrottles(); |
| - throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), |
| - throttled_transactions_.end(), transaction), |
| - throttled_transactions_.end()); |
| - ArmTimer(); |
| - } |
| -} |
| + if (!conditions_->IsThrottling()) |
| + return; |
| -void DevToolsNetworkController::SetNetworkState( |
| - const scoped_refptr<DevToolsNetworkConditions> conditions) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - BrowserThread::PostTask( |
| - content::BrowserThread::IO, |
| - FROM_HERE, |
| - base::Bind( |
| - &DevToolsNetworkController::SetNetworkStateOnIO, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - conditions)); |
| + UpdateThrottles(); |
| + throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), |
| + throttled_transactions_.end(), transaction), |
| + throttled_transactions_.end()); |
| + ArmTimer(); |
| } |
| -void DevToolsNetworkController::SetNetworkStateOnIO( |
| +void DevToolsNetworkInterceptor::UpdateConditions( |
| const scoped_refptr<DevToolsNetworkConditions> conditions) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - if (conditions_ && conditions_->IsThrottling()) |
| + DCHECK(conditions); |
| + if (conditions_->IsThrottling()) |
| UpdateThrottles(); |
| conditions_ = conditions; |
| - if (!conditions || !(conditions->IsThrottling() || conditions->IsOffline())) { |
| - timer_.Stop(); |
| - int64_t length = throttled_transactions_.size(); |
| - for (int64_t i = 0; i < length; ++i) |
| - throttled_transactions_[i]->FireThrottledCallback(); |
| - throttled_transactions_.clear(); |
| - } |
| - if (!conditions) |
| - return; |
| - |
| - if (conditions_->IsOffline()) { |
| - // Iterate over a copy of set, because failing of transaction could |
| - // result in creating a new one, or (theoretically) destroying one. |
| + if (conditions->offline()) { |
| + throttled_transactions_.clear(); |
| Transactions old_transactions(transactions_); |
| - for (Transactions::iterator it = old_transactions.begin(); |
| - it != old_transactions.end(); ++it) { |
| + Transactions::iterator it = old_transactions.begin(); |
| + for (;it != old_transactions.end(); ++it) { |
| if (transactions_.find(*it) == transactions_.end()) |
| continue; |
| if (!(*it)->request() || (*it)->failed()) |
| continue; |
| - if (ShouldFail((*it)->request())) |
| + if (ShouldFail(*it)) |
| (*it)->Fail(); |
| } |
|
vsevik
2014/06/18 13:18:32
timer_.Stop();
return;
eustas
2014/06/18 13:27:53
Done.
|
| } |
| - if (conditions_->IsThrottling()) { |
| - DCHECK(conditions_->maximal_throughput() != 0); |
| + if (conditions->IsThrottling()) { |
| + DCHECK(conditions->download_throughput() != 0); |
| offset_ = base::TimeTicks::Now(); |
| last_tick_ = 0; |
| int64_t us_tick_length = |
| - (1000000L * kPacketSize) / conditions_->maximal_throughput(); |
| + (1000000L * kPacketSize) / conditions->download_throughput(); |
| DCHECK(us_tick_length != 0); |
| if (us_tick_length == 0) |
| us_tick_length = 1; |
| tick_length_ = base::TimeDelta::FromMicroseconds(us_tick_length); |
| ArmTimer(); |
| + } else { |
| + timer_.Stop(); |
| + int64_t length = throttled_transactions_.size(); |
| + for (int64_t i = 0; i < length; ++i) |
| + throttled_transactions_[i]->FireThrottledCallback(); |
| + throttled_transactions_.clear(); |
| } |
| } |
| -bool DevToolsNetworkController::ShouldFail( |
| - const net::HttpRequestInfo* request) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - DCHECK(request); |
| - if (!conditions_ || !conditions_->IsOffline()) |
| - return false; |
| - |
| - if (!conditions_->HasMatchingDomain(request->url)) |
| - return false; |
| - |
| - return !request->extra_headers.HasHeader(kDevToolsRequestInitiator); |
| -} |
| - |
| -bool DevToolsNetworkController::ShouldThrottle( |
| - const net::HttpRequestInfo* request) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - DCHECK(request); |
| - if (!conditions_ || !conditions_->IsThrottling()) |
| - return false; |
| - |
| - if (!conditions_->HasMatchingDomain(request->url)) |
| - return false; |
| - |
| - return !request->extra_headers.HasHeader(kDevToolsRequestInitiator); |
| -} |
| - |
| -void DevToolsNetworkController::UpdateThrottles() { |
| +void DevToolsNetworkInterceptor::UpdateThrottles() { |
| int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_; |
| int64_t ticks = last_tick - last_tick_; |
| last_tick_ = last_tick; |
| @@ -152,7 +108,7 @@ void DevToolsNetworkController::UpdateThrottles() { |
| throttled_transactions_.begin() + shift, throttled_transactions_.end()); |
| } |
| -void DevToolsNetworkController::OnTimer() { |
| +void DevToolsNetworkInterceptor::OnTimer() { |
| UpdateThrottles(); |
| std::vector<DevToolsNetworkTransaction*> active_transactions; |
| @@ -173,7 +129,7 @@ void DevToolsNetworkController::OnTimer() { |
| ArmTimer(); |
| } |
| -void DevToolsNetworkController::ArmTimer() { |
| +void DevToolsNetworkInterceptor::ArmTimer() { |
| size_t length = throttled_transactions_.size(); |
| if (!length) |
| return; |
| @@ -191,13 +147,35 @@ void DevToolsNetworkController::ArmTimer() { |
| FROM_HERE, |
| desired_time - base::TimeTicks::Now(), |
| base::Bind( |
| - &DevToolsNetworkController::OnTimer, |
| - weak_ptr_factory_.GetWeakPtr())); |
| + &DevToolsNetworkInterceptor::OnTimer, |
| + base::Unretained(this))); |
| } |
| -void DevToolsNetworkController::ThrottleTransaction( |
| +void DevToolsNetworkInterceptor::ThrottleTransaction( |
| DevToolsNetworkTransaction* transaction) { |
| UpdateThrottles(); |
| throttled_transactions_.push_back(transaction); |
| ArmTimer(); |
| } |
| + |
| +bool DevToolsNetworkInterceptor::ShouldFail( |
| + const DevToolsNetworkTransaction* transaction) { |
| + if (!conditions_->offline()) |
| + return false; |
| + |
| + if (!transaction->request_initiator().empty()) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +bool DevToolsNetworkInterceptor::ShouldThrottle( |
| + const DevToolsNetworkTransaction* transaction) { |
| + if (!conditions_->IsThrottling()) |
| + return false; |
| + |
| + if (!transaction->request_initiator().empty()) |
| + return false; |
| + |
| + return true; |
| +} |