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_realm.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 DevToolsNetworkRealm::DevToolsNetworkRealm() |
vsevik
2014/06/18 12:33:37
DevToolsNetworkInterceptor
eustas
2014/06/18 12:57:47
Done.
| |
25 : weak_ptr_factory_(this) { | 18 : conditions_(new DevToolsNetworkConditions()), |
19 weak_ptr_factory_(this) { | |
26 } | 20 } |
27 | 21 |
28 DevToolsNetworkController::~DevToolsNetworkController() { | 22 DevToolsNetworkRealm::~DevToolsNetworkRealm() { |
29 } | 23 } |
30 | 24 |
31 void DevToolsNetworkController::AddTransaction( | 25 base::WeakPtr<DevToolsNetworkRealm> DevToolsNetworkRealm::GetWeakPtr() { |
26 return weak_ptr_factory_.GetWeakPtr(); | |
27 } | |
28 | |
29 void DevToolsNetworkRealm::AddTransaction( | |
32 DevToolsNetworkTransaction* transaction) { | 30 DevToolsNetworkTransaction* transaction) { |
33 DCHECK(thread_checker_.CalledOnValidThread()); | 31 DCHECK(transactions_.find(transaction) == transactions_.end()); |
34 transactions_.insert(transaction); | 32 transactions_.insert(transaction); |
35 } | 33 } |
36 | 34 |
37 void DevToolsNetworkController::RemoveTransaction( | 35 void DevToolsNetworkRealm::RemoveTransaction( |
38 DevToolsNetworkTransaction* transaction) { | 36 DevToolsNetworkTransaction* transaction) { |
39 DCHECK(thread_checker_.CalledOnValidThread()); | |
40 DCHECK(transactions_.find(transaction) != transactions_.end()); | 37 DCHECK(transactions_.find(transaction) != transactions_.end()); |
41 transactions_.erase(transaction); | 38 transactions_.erase(transaction); |
42 | 39 |
43 if (conditions_ && conditions_->IsThrottling()) { | 40 if (!conditions_->IsThrottling()) |
44 UpdateThrottles(); | 41 return; |
45 throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), | 42 |
46 throttled_transactions_.end(), transaction), | 43 UpdateThrottles(); |
47 throttled_transactions_.end()); | 44 throttled_transactions_.erase(std::remove(throttled_transactions_.begin(), |
48 ArmTimer(); | 45 throttled_transactions_.end(), transaction), |
49 } | 46 throttled_transactions_.end()); |
47 ArmTimer(); | |
50 } | 48 } |
51 | 49 |
52 void DevToolsNetworkController::SetNetworkState( | 50 void DevToolsNetworkRealm::UpdateConditions( |
53 const scoped_refptr<DevToolsNetworkConditions> conditions) { | 51 const scoped_refptr<DevToolsNetworkConditions> conditions) { |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 52 DCHECK(conditions); |
55 BrowserThread::PostTask( | 53 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(); | 54 UpdateThrottles(); |
69 | 55 |
70 conditions_ = conditions; | 56 conditions_ = conditions; |
71 if (!conditions || !(conditions->IsThrottling() || conditions->IsOffline())) { | 57 |
58 if (conditions->IsThrottling()) { | |
59 DCHECK(conditions->download_throughput() != 0); | |
60 offset_ = base::TimeTicks::Now(); | |
61 last_tick_ = 0; | |
62 int64_t us_tick_length = | |
63 (1000000L * kPacketSize) / conditions->download_throughput(); | |
64 DCHECK(us_tick_length != 0); | |
65 if (us_tick_length == 0) | |
66 us_tick_length = 1; | |
67 tick_length_ = base::TimeDelta::FromMicroseconds(us_tick_length); | |
68 ArmTimer(); | |
69 } else { | |
72 timer_.Stop(); | 70 timer_.Stop(); |
vsevik
2014/06/18 12:33:37
Why are you now doing this if we went offline?
eustas
2014/06/18 12:57:47
OK. I've changed logic - now we process offline mo
| |
73 int64_t length = throttled_transactions_.size(); | 71 int64_t length = throttled_transactions_.size(); |
74 for (int64_t i = 0; i < length; ++i) | 72 for (int64_t i = 0; i < length; ++i) |
75 throttled_transactions_[i]->FireThrottledCallback(); | 73 throttled_transactions_[i]->FireThrottledCallback(); |
76 throttled_transactions_.clear(); | 74 throttled_transactions_.clear(); |
77 } | 75 } |
78 | 76 |
79 if (!conditions) | 77 if (conditions->offline()) { |
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_); | 78 Transactions old_transactions(transactions_); |
86 for (Transactions::iterator it = old_transactions.begin(); | 79 Transactions::iterator it = old_transactions.begin(); |
87 it != old_transactions.end(); ++it) { | 80 for (;it != old_transactions.end(); ++it) { |
88 if (transactions_.find(*it) == transactions_.end()) | 81 if (transactions_.find(*it) == transactions_.end()) |
89 continue; | 82 continue; |
90 if (!(*it)->request() || (*it)->failed()) | 83 if (!(*it)->request() || (*it)->failed()) |
91 continue; | 84 continue; |
92 if (ShouldFail((*it)->request())) | 85 if (ShouldFail(*it)) |
93 (*it)->Fail(); | 86 (*it)->Fail(); |
94 } | 87 } |
95 } | 88 } |
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 } | 89 } |
110 | 90 |
111 bool DevToolsNetworkController::ShouldFail( | 91 void DevToolsNetworkRealm::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_; | 92 int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_; |
139 int64_t ticks = last_tick - last_tick_; | 93 int64_t ticks = last_tick - last_tick_; |
140 last_tick_ = last_tick; | 94 last_tick_ = last_tick; |
141 | 95 |
142 int64_t length = throttled_transactions_.size(); | 96 int64_t length = throttled_transactions_.size(); |
143 if (!length) | 97 if (!length) |
144 return; | 98 return; |
145 | 99 |
146 int64_t shift = ticks % length; | 100 int64_t shift = ticks % length; |
147 for (int64_t i = 0; i < length; ++i) { | 101 for (int64_t i = 0; i < length; ++i) { |
148 throttled_transactions_[i]->DecreaseThrottledByteCount( | 102 throttled_transactions_[i]->DecreaseThrottledByteCount( |
149 (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); | 103 (ticks / length) * kPacketSize + (i < shift ? kPacketSize : 0)); |
150 } | 104 } |
151 std::rotate(throttled_transactions_.begin(), | 105 std::rotate(throttled_transactions_.begin(), |
152 throttled_transactions_.begin() + shift, throttled_transactions_.end()); | 106 throttled_transactions_.begin() + shift, throttled_transactions_.end()); |
153 } | 107 } |
154 | 108 |
155 void DevToolsNetworkController::OnTimer() { | 109 void DevToolsNetworkRealm::OnTimer() { |
156 UpdateThrottles(); | 110 UpdateThrottles(); |
157 | 111 |
158 std::vector<DevToolsNetworkTransaction*> active_transactions; | 112 std::vector<DevToolsNetworkTransaction*> active_transactions; |
159 std::vector<DevToolsNetworkTransaction*> finished_transactions; | 113 std::vector<DevToolsNetworkTransaction*> finished_transactions; |
160 size_t length = throttled_transactions_.size(); | 114 size_t length = throttled_transactions_.size(); |
161 for (size_t i = 0; i < length; ++i) { | 115 for (size_t i = 0; i < length; ++i) { |
162 if (throttled_transactions_[i]->throttled_byte_count() < 0) | 116 if (throttled_transactions_[i]->throttled_byte_count() < 0) |
163 finished_transactions.push_back(throttled_transactions_[i]); | 117 finished_transactions.push_back(throttled_transactions_[i]); |
164 else | 118 else |
165 active_transactions.push_back(throttled_transactions_[i]); | 119 active_transactions.push_back(throttled_transactions_[i]); |
166 } | 120 } |
167 throttled_transactions_.swap(active_transactions); | 121 throttled_transactions_.swap(active_transactions); |
168 | 122 |
169 length = finished_transactions.size(); | 123 length = finished_transactions.size(); |
170 for (size_t i = 0; i < length; ++i) | 124 for (size_t i = 0; i < length; ++i) |
171 finished_transactions[i]->FireThrottledCallback(); | 125 finished_transactions[i]->FireThrottledCallback(); |
172 | 126 |
173 ArmTimer(); | 127 ArmTimer(); |
174 } | 128 } |
175 | 129 |
176 void DevToolsNetworkController::ArmTimer() { | 130 void DevToolsNetworkRealm::ArmTimer() { |
177 size_t length = throttled_transactions_.size(); | 131 size_t length = throttled_transactions_.size(); |
178 if (!length) | 132 if (!length) |
179 return; | 133 return; |
180 int64_t min_ticks_left = 0x10000L; | 134 int64_t min_ticks_left = 0x10000L; |
181 for (size_t i = 0; i < length; ++i) { | 135 for (size_t i = 0; i < length; ++i) { |
182 int64_t packets_left = (throttled_transactions_[i]->throttled_byte_count() + | 136 int64_t packets_left = (throttled_transactions_[i]->throttled_byte_count() + |
183 kPacketSize - 1) / kPacketSize; | 137 kPacketSize - 1) / kPacketSize; |
184 int64_t ticks_left = (i + 1) + length * (packets_left - 1); | 138 int64_t ticks_left = (i + 1) + length * (packets_left - 1); |
185 if (i == 0 || ticks_left < min_ticks_left) | 139 if (i == 0 || ticks_left < min_ticks_left) |
186 min_ticks_left = ticks_left; | 140 min_ticks_left = ticks_left; |
187 } | 141 } |
188 base::TimeTicks desired_time = | 142 base::TimeTicks desired_time = |
189 offset_ + tick_length_ * (last_tick_ + min_ticks_left); | 143 offset_ + tick_length_ * (last_tick_ + min_ticks_left); |
190 timer_.Start( | 144 timer_.Start( |
191 FROM_HERE, | 145 FROM_HERE, |
192 desired_time - base::TimeTicks::Now(), | 146 desired_time - base::TimeTicks::Now(), |
193 base::Bind( | 147 base::Bind( |
194 &DevToolsNetworkController::OnTimer, | 148 &DevToolsNetworkRealm::OnTimer, |
195 weak_ptr_factory_.GetWeakPtr())); | 149 base::Unretained(this))); |
196 } | 150 } |
197 | 151 |
198 void DevToolsNetworkController::ThrottleTransaction( | 152 void DevToolsNetworkRealm::ThrottleTransaction( |
199 DevToolsNetworkTransaction* transaction) { | 153 DevToolsNetworkTransaction* transaction) { |
200 UpdateThrottles(); | 154 UpdateThrottles(); |
201 throttled_transactions_.push_back(transaction); | 155 throttled_transactions_.push_back(transaction); |
202 ArmTimer(); | 156 ArmTimer(); |
203 } | 157 } |
158 | |
159 bool DevToolsNetworkRealm::ShouldFail( | |
160 const DevToolsNetworkTransaction* transaction) { | |
161 if (!conditions_->offline()) | |
162 return false; | |
163 | |
164 if (!transaction->request_initiator().empty()) | |
165 return false; | |
166 | |
167 return true; | |
168 } | |
169 | |
170 bool DevToolsNetworkRealm::ShouldThrottle( | |
171 const DevToolsNetworkTransaction* transaction) { | |
172 if (!conditions_->IsThrottling()) | |
173 return false; | |
174 | |
175 if (!transaction->request_initiator().empty()) | |
176 return false; | |
177 | |
178 return true; | |
179 } | |
OLD | NEW |