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