| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/devtools/devtools_network_controller.h" | 5 #include "chrome/browser/devtools/devtools_network_interceptor.h" |
| 6 | 6 |
| 7 #include "base/time/time.h" | 7 #include "base/time/time.h" |
| 8 #include "chrome/browser/devtools/devtools_network_conditions.h" | 8 #include "chrome/browser/devtools/devtools_network_conditions.h" |
| 9 #include "chrome/browser/devtools/devtools_network_transaction.h" | 9 #include "chrome/browser/devtools/devtools_network_transaction.h" |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/profiles/profile_io_data.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/resource_context.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
| 16 | 10 |
| 17 namespace { | 11 namespace { |
| 18 | 12 |
| 19 const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; | |
| 20 int64_t kPacketSize = 1500; | 13 int64_t kPacketSize = 1500; |
| 21 | 14 |
| 22 } // namespace | 15 } // namespace |
| 23 | 16 |
| 24 DevToolsNetworkController::DevToolsNetworkController() | 17 DevToolsNetworkInterceptor::DevToolsNetworkInterceptor() |
| 25 : weak_ptr_factory_(this) { | 18 : conditions_(new DevToolsNetworkConditions()), |
| 19 weak_ptr_factory_(this) { |
| 26 } | 20 } |
| 27 | 21 |
| 28 DevToolsNetworkController::~DevToolsNetworkController() { | 22 DevToolsNetworkInterceptor::~DevToolsNetworkInterceptor() { |
| 29 } | 23 } |
| 30 | 24 |
| 31 void DevToolsNetworkController::AddTransaction( | 25 base::WeakPtr<DevToolsNetworkInterceptor> |
| 26 DevToolsNetworkInterceptor::GetWeakPtr() { |
| 27 return weak_ptr_factory_.GetWeakPtr(); |
| 28 } |
| 29 |
| 30 void DevToolsNetworkInterceptor::AddTransaction( |
| 32 DevToolsNetworkTransaction* transaction) { | 31 DevToolsNetworkTransaction* transaction) { |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | 32 DCHECK(transactions_.find(transaction) == transactions_.end()); |
| 34 transactions_.insert(transaction); | 33 transactions_.insert(transaction); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void DevToolsNetworkController::RemoveTransaction( | 36 void DevToolsNetworkInterceptor::RemoveTransaction( |
| 38 DevToolsNetworkTransaction* transaction) { | 37 DevToolsNetworkTransaction* transaction) { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 40 DCHECK(transactions_.find(transaction) != transactions_.end()); | 38 DCHECK(transactions_.find(transaction) != transactions_.end()); |
| 41 transactions_.erase(transaction); | 39 transactions_.erase(transaction); |
| 42 | 40 |
| 43 if (conditions_ && conditions_->IsThrottling()) { | 41 if (!conditions_->IsThrottling()) |
| 44 UpdateThrottles(); | 42 return; |
| 45 throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), | 43 |
| 46 throttled_transactions_.end(), transaction), | 44 UpdateThrottles(); |
| 47 throttled_transactions_.end()); | 45 throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), |
| 48 ArmTimer(); | 46 throttled_transactions_.end(), transaction), |
| 49 } | 47 throttled_transactions_.end()); |
| 48 ArmTimer(); |
| 50 } | 49 } |
| 51 | 50 |
| 52 void DevToolsNetworkController::SetNetworkState( | 51 void DevToolsNetworkInterceptor::UpdateConditions( |
| 53 const scoped_refptr<DevToolsNetworkConditions> conditions) { | 52 const scoped_refptr<DevToolsNetworkConditions> conditions) { |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 53 DCHECK(conditions); |
| 55 BrowserThread::PostTask( | 54 if (conditions_->IsThrottling()) |
| 56 content::BrowserThread::IO, | |
| 57 FROM_HERE, | |
| 58 base::Bind( | |
| 59 &DevToolsNetworkController::SetNetworkStateOnIO, | |
| 60 weak_ptr_factory_.GetWeakPtr(), | |
| 61 conditions)); | |
| 62 } | |
| 63 | |
| 64 void DevToolsNetworkController::SetNetworkStateOnIO( | |
| 65 const scoped_refptr<DevToolsNetworkConditions> conditions) { | |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 67 if (conditions_ && conditions_->IsThrottling()) | |
| 68 UpdateThrottles(); | 55 UpdateThrottles(); |
| 69 | 56 |
| 70 conditions_ = conditions; | 57 conditions_ = conditions; |
| 71 if (!conditions || !(conditions->IsThrottling() || conditions->IsOffline())) { | 58 |
| 59 if (conditions->offline()) { |
| 60 timer_.Stop(); |
| 61 throttled_transactions_.clear(); |
| 62 Transactions old_transactions(transactions_); |
| 63 Transactions::iterator it = old_transactions.begin(); |
| 64 for (;it != old_transactions.end(); ++it) { |
| 65 if (transactions_.find(*it) == transactions_.end()) |
| 66 continue; |
| 67 if (!(*it)->request() || (*it)->failed()) |
| 68 continue; |
| 69 if (ShouldFail(*it)) |
| 70 (*it)->Fail(); |
| 71 } |
| 72 return; |
| 73 } |
| 74 |
| 75 if (conditions->IsThrottling()) { |
| 76 DCHECK(conditions->download_throughput() != 0); |
| 77 offset_ = base::TimeTicks::Now(); |
| 78 last_tick_ = 0; |
| 79 int64_t us_tick_length = |
| 80 (1000000L * kPacketSize) / conditions->download_throughput(); |
| 81 DCHECK(us_tick_length != 0); |
| 82 if (us_tick_length == 0) |
| 83 us_tick_length = 1; |
| 84 tick_length_ = base::TimeDelta::FromMicroseconds(us_tick_length); |
| 85 ArmTimer(); |
| 86 } else { |
| 72 timer_.Stop(); | 87 timer_.Stop(); |
| 73 int64_t length = throttled_transactions_.size(); | 88 int64_t length = throttled_transactions_.size(); |
| 74 for (int64_t i = 0; i < length; ++i) | 89 for (int64_t i = 0; i < length; ++i) |
| 75 throttled_transactions_[i]->FireThrottledCallback(); | 90 throttled_transactions_[i]->FireThrottledCallback(); |
| 76 throttled_transactions_.clear(); | 91 throttled_transactions_.clear(); |
| 77 } | 92 } |
| 78 | |
| 79 if (!conditions) | |
| 80 return; | |
| 81 | |
| 82 if (conditions_->IsOffline()) { | |
| 83 // Iterate over a copy of set, because failing of transaction could | |
| 84 // result in creating a new one, or (theoretically) destroying one. | |
| 85 Transactions old_transactions(transactions_); | |
| 86 for (Transactions::iterator it = old_transactions.begin(); | |
| 87 it != old_transactions.end(); ++it) { | |
| 88 if (transactions_.find(*it) == transactions_.end()) | |
| 89 continue; | |
| 90 if (!(*it)->request() || (*it)->failed()) | |
| 91 continue; | |
| 92 if (ShouldFail((*it)->request())) | |
| 93 (*it)->Fail(); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 if (conditions_->IsThrottling()) { | |
| 98 DCHECK(conditions_->maximal_throughput() != 0); | |
| 99 offset_ = base::TimeTicks::Now(); | |
| 100 last_tick_ = 0; | |
| 101 int64_t us_tick_length = | |
| 102 (1000000L * kPacketSize) / conditions_->maximal_throughput(); | |
| 103 DCHECK(us_tick_length != 0); | |
| 104 if (us_tick_length == 0) | |
| 105 us_tick_length = 1; | |
| 106 tick_length_ = base::TimeDelta::FromMicroseconds(us_tick_length); | |
| 107 ArmTimer(); | |
| 108 } | |
| 109 } | 93 } |
| 110 | 94 |
| 111 bool DevToolsNetworkController::ShouldFail( | 95 void DevToolsNetworkInterceptor::UpdateThrottles() { |
| 112 const net::HttpRequestInfo* request) { | |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 114 DCHECK(request); | |
| 115 if (!conditions_ || !conditions_->IsOffline()) | |
| 116 return false; | |
| 117 | |
| 118 if (!conditions_->HasMatchingDomain(request->url)) | |
| 119 return false; | |
| 120 | |
| 121 return !request->extra_headers.HasHeader(kDevToolsRequestInitiator); | |
| 122 } | |
| 123 | |
| 124 bool DevToolsNetworkController::ShouldThrottle( | |
| 125 const net::HttpRequestInfo* request) { | |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 127 DCHECK(request); | |
| 128 if (!conditions_ || !conditions_->IsThrottling()) | |
| 129 return false; | |
| 130 | |
| 131 if (!conditions_->HasMatchingDomain(request->url)) | |
| 132 return false; | |
| 133 | |
| 134 return !request->extra_headers.HasHeader(kDevToolsRequestInitiator); | |
| 135 } | |
| 136 | |
| 137 void DevToolsNetworkController::UpdateThrottles() { | |
| 138 int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_; | 96 int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_; |
| 139 int64_t ticks = last_tick - last_tick_; | 97 int64_t ticks = last_tick - last_tick_; |
| 140 last_tick_ = last_tick; | 98 last_tick_ = last_tick; |
| 141 | 99 |
| 142 int64_t length = throttled_transactions_.size(); | 100 int64_t length = throttled_transactions_.size(); |
| 143 if (!length) | 101 if (!length) |
| 144 return; | 102 return; |
| 145 | 103 |
| 146 int64_t shift = ticks % length; | 104 int64_t shift = ticks % length; |
| 147 for (int64_t i = 0; i < length; ++i) { | 105 for (int64_t i = 0; i < length; ++i) { |
| 148 throttled_transactions_[i]->DecreaseThrottledByteCount( | 106 throttled_transactions_[i]->DecreaseThrottledByteCount( |
| 149 (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); | 107 (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); |
| 150 } | 108 } |
| 151 std::rotate(throttled_transactions_.begin(), | 109 std::rotate(throttled_transactions_.begin(), |
| 152 throttled_transactions_.begin() + shift, throttled_transactions_.end()); | 110 throttled_transactions_.begin() + shift, throttled_transactions_.end()); |
| 153 } | 111 } |
| 154 | 112 |
| 155 void DevToolsNetworkController::OnTimer() { | 113 void DevToolsNetworkInterceptor::OnTimer() { |
| 156 UpdateThrottles(); | 114 UpdateThrottles(); |
| 157 | 115 |
| 158 std::vector<DevToolsNetworkTransaction*> active_transactions; | 116 std::vector<DevToolsNetworkTransaction*> active_transactions; |
| 159 std::vector<DevToolsNetworkTransaction*> finished_transactions; | 117 std::vector<DevToolsNetworkTransaction*> finished_transactions; |
| 160 size_t length = throttled_transactions_.size(); | 118 size_t length = throttled_transactions_.size(); |
| 161 for (size_t i = 0; i < length; ++i) { | 119 for (size_t i = 0; i < length; ++i) { |
| 162 if (throttled_transactions_[i]->throttled_byte_count() < 0) | 120 if (throttled_transactions_[i]->throttled_byte_count() < 0) |
| 163 finished_transactions.push_back(throttled_transactions_[i]); | 121 finished_transactions.push_back(throttled_transactions_[i]); |
| 164 else | 122 else |
| 165 active_transactions.push_back(throttled_transactions_[i]); | 123 active_transactions.push_back(throttled_transactions_[i]); |
| 166 } | 124 } |
| 167 throttled_transactions_.swap(active_transactions); | 125 throttled_transactions_.swap(active_transactions); |
| 168 | 126 |
| 169 length = finished_transactions.size(); | 127 length = finished_transactions.size(); |
| 170 for (size_t i = 0; i < length; ++i) | 128 for (size_t i = 0; i < length; ++i) |
| 171 finished_transactions[i]->FireThrottledCallback(); | 129 finished_transactions[i]->FireThrottledCallback(); |
| 172 | 130 |
| 173 ArmTimer(); | 131 ArmTimer(); |
| 174 } | 132 } |
| 175 | 133 |
| 176 void DevToolsNetworkController::ArmTimer() { | 134 void DevToolsNetworkInterceptor::ArmTimer() { |
| 177 size_t length = throttled_transactions_.size(); | 135 size_t length = throttled_transactions_.size(); |
| 178 if (!length) | 136 if (!length) |
| 179 return; | 137 return; |
| 180 int64_t min_ticks_left = 0x10000L; | 138 int64_t min_ticks_left = 0x10000L; |
| 181 for (size_t i = 0; i < length; ++i) { | 139 for (size_t i = 0; i < length; ++i) { |
| 182 int64_t packets_left = (throttled_transactions_[i]->throttled_byte_count() + | 140 int64_t packets_left = (throttled_transactions_[i]->throttled_byte_count() + |
| 183 kPacketSize - 1) / kPacketSize; | 141 kPacketSize - 1) / kPacketSize; |
| 184 int64_t ticks_left = (i + 1) + length * (packets_left - 1); | 142 int64_t ticks_left = (i + 1) + length * (packets_left - 1); |
| 185 if (i == 0 || ticks_left < min_ticks_left) | 143 if (i == 0 || ticks_left < min_ticks_left) |
| 186 min_ticks_left = ticks_left; | 144 min_ticks_left = ticks_left; |
| 187 } | 145 } |
| 188 base::TimeTicks desired_time = | 146 base::TimeTicks desired_time = |
| 189 offset_ + tick_length_ * (last_tick_ + min_ticks_left); | 147 offset_ + tick_length_ * (last_tick_ + min_ticks_left); |
| 190 timer_.Start( | 148 timer_.Start( |
| 191 FROM_HERE, | 149 FROM_HERE, |
| 192 desired_time - base::TimeTicks::Now(), | 150 desired_time - base::TimeTicks::Now(), |
| 193 base::Bind( | 151 base::Bind( |
| 194 &DevToolsNetworkController::OnTimer, | 152 &DevToolsNetworkInterceptor::OnTimer, |
| 195 weak_ptr_factory_.GetWeakPtr())); | 153 base::Unretained(this))); |
| 196 } | 154 } |
| 197 | 155 |
| 198 void DevToolsNetworkController::ThrottleTransaction( | 156 void DevToolsNetworkInterceptor::ThrottleTransaction( |
| 199 DevToolsNetworkTransaction* transaction) { | 157 DevToolsNetworkTransaction* transaction) { |
| 200 UpdateThrottles(); | 158 UpdateThrottles(); |
| 201 throttled_transactions_.push_back(transaction); | 159 throttled_transactions_.push_back(transaction); |
| 202 ArmTimer(); | 160 ArmTimer(); |
| 203 } | 161 } |
| 162 |
| 163 bool DevToolsNetworkInterceptor::ShouldFail( |
| 164 const DevToolsNetworkTransaction* transaction) { |
| 165 if (!conditions_->offline()) |
| 166 return false; |
| 167 |
| 168 if (!transaction->request_initiator().empty()) |
| 169 return false; |
| 170 |
| 171 return true; |
| 172 } |
| 173 |
| 174 bool DevToolsNetworkInterceptor::ShouldThrottle( |
| 175 const DevToolsNetworkTransaction* transaction) { |
| 176 if (!conditions_->IsThrottling()) |
| 177 return false; |
| 178 |
| 179 if (!transaction->request_initiator().empty()) |
| 180 return false; |
| 181 |
| 182 return true; |
| 183 } |
| OLD | NEW |