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 throttles_.erase( | |
vsevik
2014/06/11 14:29:39
nit: throttled_transactions?
eustas
2014/06/11 14:42:47
Done.
| |
46 std::remove(throttles_.begin(), throttles_.end(), transaction), | |
47 throttles_.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()) { | |
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.
| |
72 timer_.Stop(); | |
73 int64_t length = throttles_.size(); | |
74 for (int64_t i = 0; i < length; ++i) | |
75 throttles_[i]->FireThrottledCallback(); | |
76 throttles_.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 = throttles_.size(); | |
143 if (!length) | |
144 return; | |
145 | |
146 int64_t shift = ticks % length; | |
147 for (int64_t i = 0; i < length; ++i) { | |
148 throttles_[i]->DecreaseThrottledByteCount( | |
149 (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); | |
150 } | |
151 std::rotate( | |
152 throttles_.begin(), throttles_.begin() + shift, throttles_.end()); | |
153 } | |
154 | |
155 void DevToolsNetworkController::OnTimer() { | |
156 UpdateThrottles(); | |
157 | |
158 size_t length = throttles_.size(); | |
159 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.
| |
160 return; | |
161 | |
162 Throttles active_transactions; | |
163 Throttles finished_transactions; | |
164 for (size_t i = 0; i < length; ++i) { | |
165 if (throttles_[i]->throttled_byte_count() < 0) | |
166 finished_transactions.push_back(throttles_[i]); | |
167 else | |
168 active_transactions.push_back(throttles_[i]); | |
169 } | |
170 throttles_.swap(active_transactions); | |
171 | |
172 length = finished_transactions.size(); | |
173 for (size_t i = 0; i < length; ++i) | |
174 finished_transactions[i]->FireThrottledCallback(); | |
175 | |
176 ArmTimer(); | |
177 } | |
178 | |
179 void DevToolsNetworkController::ArmTimer() { | |
180 size_t length = throttles_.size(); | |
181 if (!length) | |
182 return; | |
183 int64_t min_ticks_left; | |
184 for (size_t i = 0; i < length; ++i) { | |
185 int64_t packets_left = | |
186 (throttles_[i]->throttled_byte_count() + kPacketSize - 1) / kPacketSize; | |
187 int64_t ticks_left = (i + 1) + length * (packets_left - 1); | |
188 if (i == 0 || ticks_left < min_ticks_left) | |
189 min_ticks_left = ticks_left; | |
190 } | |
191 base::TimeTicks desired_time = | |
192 offset_ + tick_length_ * (last_tick_ + min_ticks_left); | |
193 timer_.Start( | |
194 FROM_HERE, | |
195 desired_time - base::TimeTicks::Now(), | |
196 base::Bind( | |
197 &DevToolsNetworkController::OnTimer, | |
198 weak_ptr_factory_.GetWeakPtr())); | |
199 } | |
200 | |
201 void DevToolsNetworkController::ThrottleTransaction( | |
202 DevToolsNetworkTransaction* transaction) { | |
203 UpdateThrottles(); | |
204 throttles_.push_back(transaction); | |
205 ArmTimer(); | |
206 } | |
OLD | NEW |