Chromium Code Reviews| Index: chrome/browser/devtools/devtools_network_controller.cc |
| diff --git a/chrome/browser/devtools/devtools_network_controller.cc b/chrome/browser/devtools/devtools_network_controller.cc |
| index 362dc467fe645c4b010b55f2cdf8779671135647..ab7c315d8fcf604cdfe66e467dafcd3a7cc88367 100644 |
| --- a/chrome/browser/devtools/devtools_network_controller.cc |
| +++ b/chrome/browser/devtools/devtools_network_controller.cc |
| @@ -4,6 +4,7 @@ |
| #include "chrome/browser/devtools/devtools_network_controller.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" |
| @@ -16,6 +17,7 @@ using content::BrowserThread; |
| namespace { |
| const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; |
| +int64_t kPacketSize = 1500; |
| } // namespace |
| @@ -37,10 +39,17 @@ void DevToolsNetworkController::RemoveTransaction( |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| DCHECK(transactions_.find(transaction) != transactions_.end()); |
| transactions_.erase(transaction); |
| + |
| + if (conditions_ && conditions_->IsThrottling()) { |
| + UpdateThrottles(); |
| + throttles_.erase( |
|
vsevik
2014/06/11 14:29:39
nit: throttled_transactions?
eustas
2014/06/11 14:42:47
Done.
|
| + std::remove(throttles_.begin(), throttles_.end(), transaction), |
| + throttles_.end()); |
| + ArmTimer(); |
| + } |
| } |
| void DevToolsNetworkController::SetNetworkState( |
| - const std::string& client_id, |
| const scoped_refptr<DevToolsNetworkConditions> conditions) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| BrowserThread::PostTask( |
| @@ -49,35 +58,53 @@ void DevToolsNetworkController::SetNetworkState( |
| base::Bind( |
| &DevToolsNetworkController::SetNetworkStateOnIO, |
| weak_ptr_factory_.GetWeakPtr(), |
| - client_id, |
| conditions)); |
| } |
| void DevToolsNetworkController::SetNetworkStateOnIO( |
| - const std::string& client_id, |
| const scoped_refptr<DevToolsNetworkConditions> conditions) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| - if (!conditions) { |
| - if (client_id == active_client_id_) { |
| - conditions_ = NULL; |
| - active_client_id_ = std::string(); |
| - } |
| + if (conditions_ && conditions_->IsThrottling()) |
| + UpdateThrottles(); |
| + |
| + conditions_ = conditions; |
| + if (!conditions || !conditions->IsThrottling()) { |
|
vsevik
2014/06/11 14:29:39
Shall we drop pending packets in case we went offl
eustas
2014/06/11 14:42:47
Done.
|
| + timer_.Stop(); |
| + int64_t length = throttles_.size(); |
| + for (int64_t i = 0; i < length; ++i) |
| + throttles_[i]->FireThrottledCallback(); |
| + throttles_.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. |
| + Transactions old_transactions(transactions_); |
| + for (Transactions::iterator it = old_transactions.begin(); |
| + it != old_transactions.end(); ++it) { |
| + if (transactions_.find(*it) == transactions_.end()) |
| + continue; |
| + if (!(*it)->request() || (*it)->failed()) |
| + continue; |
| + if (ShouldFail((*it)->request())) |
| + (*it)->Fail(); |
| + } |
| } |
| - conditions_ = conditions; |
| - active_client_id_ = client_id; |
| - |
| - // Iterate over a copy of set, because failing of transaction could result in |
| - // creating a new one, or (theoretically) destroying one. |
| - Transactions old_transactions(transactions_); |
| - for (Transactions::iterator it = old_transactions.begin(); |
| - it != old_transactions.end(); ++it) { |
| - if (transactions_.find(*it) == transactions_.end()) |
| - continue; |
| - if (!(*it)->request() || (*it)->failed()) |
| - continue; |
| - if (ShouldFail((*it)->request())) |
| - (*it)->Fail(); |
| + |
| + if (conditions_->IsThrottling()) { |
| + DCHECK(conditions_->maximal_throughput() != 0); |
| + offset_ = base::TimeTicks::Now(); |
| + last_tick_ = 0; |
| + int64_t us_tick_length = |
| + (1000000L * kPacketSize) / conditions_->maximal_throughput(); |
| + DCHECK(us_tick_length != 0); |
| + if (us_tick_length == 0) |
| + us_tick_length = 1; |
| + tick_length_ = base::TimeDelta::FromMicroseconds(us_tick_length); |
| + ArmTimer(); |
| } |
| } |
| @@ -93,3 +120,87 @@ bool DevToolsNetworkController::ShouldFail( |
| 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() { |
| + int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_; |
| + int64_t ticks = last_tick - last_tick_; |
| + last_tick_ = last_tick; |
| + |
| + int64_t length = throttles_.size(); |
| + if (!length) |
| + return; |
| + |
| + int64_t shift = ticks % length; |
| + for (int64_t i = 0; i < length; ++i) { |
| + throttles_[i]->DecreaseThrottledByteCount( |
| + (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); |
| + } |
| + std::rotate( |
| + throttles_.begin(), throttles_.begin() + shift, throttles_.end()); |
| +} |
| + |
| +void DevToolsNetworkController::OnTimer() { |
| + UpdateThrottles(); |
| + |
| + size_t length = throttles_.size(); |
| + if (!length) |
|
vsevik
2014/06/11 14:29:39
This could never happen, let's DCHECK instead.
eustas
2014/06/11 14:42:47
Done.
|
| + return; |
| + |
| + Throttles active_transactions; |
| + Throttles finished_transactions; |
| + for (size_t i = 0; i < length; ++i) { |
| + if (throttles_[i]->throttled_byte_count() < 0) |
| + finished_transactions.push_back(throttles_[i]); |
| + else |
| + active_transactions.push_back(throttles_[i]); |
| + } |
| + throttles_.swap(active_transactions); |
| + |
| + length = finished_transactions.size(); |
| + for (size_t i = 0; i < length; ++i) |
| + finished_transactions[i]->FireThrottledCallback(); |
| + |
| + ArmTimer(); |
| +} |
| + |
| +void DevToolsNetworkController::ArmTimer() { |
| + size_t length = throttles_.size(); |
| + if (!length) |
| + return; |
| + int64_t min_ticks_left; |
| + for (size_t i = 0; i < length; ++i) { |
| + int64_t packets_left = |
| + (throttles_[i]->throttled_byte_count() + kPacketSize - 1) / kPacketSize; |
| + int64_t ticks_left = (i + 1) + length * (packets_left - 1); |
| + if (i == 0 || ticks_left < min_ticks_left) |
| + min_ticks_left = ticks_left; |
| + } |
| + base::TimeTicks desired_time = |
| + offset_ + tick_length_ * (last_tick_ + min_ticks_left); |
| + timer_.Start( |
| + FROM_HERE, |
| + desired_time - base::TimeTicks::Now(), |
| + base::Bind( |
| + &DevToolsNetworkController::OnTimer, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void DevToolsNetworkController::ThrottleTransaction( |
| + DevToolsNetworkTransaction* transaction) { |
| + UpdateThrottles(); |
| + throttles_.push_back(transaction); |
| + ArmTimer(); |
| +} |