OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/safe_browsing_db/v4_update_protocol_manager.h" | 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // static | 93 // static |
94 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL; | 94 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL; |
95 | 95 |
96 // static | 96 // static |
97 std::unique_ptr<V4UpdateProtocolManager> V4UpdateProtocolManager::Create( | 97 std::unique_ptr<V4UpdateProtocolManager> V4UpdateProtocolManager::Create( |
98 net::URLRequestContextGetter* request_context_getter, | 98 net::URLRequestContextGetter* request_context_getter, |
99 const V4ProtocolConfig& config, | 99 const V4ProtocolConfig& config, |
100 const base::hash_map<UpdateListIdentifier, std::string>& | 100 const base::hash_map<UpdateListIdentifier, std::string>& |
101 current_list_states, | 101 current_list_states, |
102 V4UpdateCallback callback) { | 102 V4UpdateCallback callback) { |
103 if (!factory_) { | 103 if (!factory_) |
104 factory_ = new V4UpdateProtocolManagerFactoryImpl(); | 104 factory_ = new V4UpdateProtocolManagerFactoryImpl(); |
105 } | |
106 return factory_->CreateProtocolManager(request_context_getter, config, | 105 return factory_->CreateProtocolManager(request_context_getter, config, |
107 current_list_states, callback); | 106 current_list_states, callback); |
108 } | 107 } |
109 | 108 |
110 void V4UpdateProtocolManager::ResetUpdateErrors() { | 109 void V4UpdateProtocolManager::ResetUpdateErrors() { |
111 update_error_count_ = 0; | 110 update_error_count_ = 0; |
112 update_back_off_mult_ = 1; | 111 update_back_off_mult_ = 1; |
113 } | 112 } |
114 | 113 |
115 V4UpdateProtocolManager::V4UpdateProtocolManager( | 114 V4UpdateProtocolManager::V4UpdateProtocolManager( |
116 net::URLRequestContextGetter* request_context_getter, | 115 net::URLRequestContextGetter* request_context_getter, |
117 const V4ProtocolConfig& config, | 116 const V4ProtocolConfig& config, |
118 const base::hash_map<UpdateListIdentifier, std::string>& | 117 const base::hash_map<UpdateListIdentifier, std::string>& |
119 current_list_states, | 118 current_list_states, |
120 V4UpdateCallback update_callback) | 119 V4UpdateCallback callback) |
121 : current_list_states_(current_list_states), | 120 : current_list_states_(current_list_states), |
122 update_error_count_(0), | 121 update_error_count_(0), |
123 update_back_off_mult_(1), | 122 update_back_off_mult_(1), |
124 next_update_interval_(base::TimeDelta::FromSeconds( | 123 next_update_interval_(base::TimeDelta::FromSeconds( |
125 base::RandInt(kV4TimerStartIntervalSecMin, | 124 base::RandInt(kV4TimerStartIntervalSecMin, |
126 kV4TimerStartIntervalSecMax))), | 125 kV4TimerStartIntervalSecMax))), |
127 config_(config), | 126 config_(config), |
128 request_context_getter_(request_context_getter), | 127 request_context_getter_(request_context_getter), |
129 url_fetcher_id_(0), | 128 url_fetcher_id_(0), |
130 update_callback_(update_callback) { | 129 callback_(callback) { |
131 // Do not auto-schedule updates. Let the owner (V4LocalDatabaseManager) do it | 130 ScheduleNextUpdate(false /* no back off */); |
132 // when it is ready to process updates. | |
133 DVLOG(1) << "V4UpdateProtocolManager::V4UpdateProtocolManager: " | |
134 << "next_update_interval_: " << next_update_interval_; | |
135 } | 131 } |
136 | 132 |
137 V4UpdateProtocolManager::~V4UpdateProtocolManager() {} | 133 V4UpdateProtocolManager::~V4UpdateProtocolManager() {} |
138 | 134 |
139 bool V4UpdateProtocolManager::IsUpdateScheduled() const { | 135 bool V4UpdateProtocolManager::IsUpdateScheduled() const { |
140 return update_timer_.IsRunning(); | 136 return update_timer_.IsRunning(); |
141 } | 137 } |
142 | 138 |
143 void V4UpdateProtocolManager::ScheduleNextUpdate() { | 139 void V4UpdateProtocolManager::ScheduleNextUpdate(bool back_off) { |
144 ScheduleNextUpdateWithBackoff(false); | |
145 } | |
146 | |
147 void V4UpdateProtocolManager::ScheduleNextUpdateWithBackoff(bool back_off) { | |
148 DCHECK(CalledOnValidThread()); | |
149 | |
150 // TODO(vakh): Set disable_auto_update correctly using the command line | 140 // TODO(vakh): Set disable_auto_update correctly using the command line |
151 // switch. | 141 // switch. |
152 if (config_.disable_auto_update) { | 142 if (config_.disable_auto_update) { |
153 DCHECK(!IsUpdateScheduled()); | 143 DCHECK(!IsUpdateScheduled()); |
154 return; | 144 return; |
155 } | 145 } |
156 | 146 |
157 // Reschedule with the new update. | 147 // Reschedule with the new update. |
158 base::TimeDelta next_update_interval = GetNextUpdateInterval(back_off); | 148 base::TimeDelta next_update_interval = GetNextUpdateInterval(back_off); |
159 ScheduleNextUpdateAfterInterval(next_update_interval); | 149 ScheduleNextUpdateAfterInterval(next_update_interval); |
160 } | 150 } |
161 | 151 |
162 // According to section 5 of the SafeBrowsing protocol specification, we must | 152 // According to section 5 of the SafeBrowsing protocol specification, we must |
163 // back off after a certain number of errors. | 153 // back off after a certain number of errors. |
164 base::TimeDelta V4UpdateProtocolManager::GetNextUpdateInterval(bool back_off) { | 154 base::TimeDelta V4UpdateProtocolManager::GetNextUpdateInterval(bool back_off) { |
165 DCHECK(CalledOnValidThread()); | 155 DCHECK(CalledOnValidThread()); |
166 DCHECK(next_update_interval_ > base::TimeDelta()); | 156 DCHECK(next_update_interval_ > base::TimeDelta()); |
167 | |
168 base::TimeDelta next = next_update_interval_; | 157 base::TimeDelta next = next_update_interval_; |
169 if (back_off) { | 158 if (back_off) { |
170 next = V4ProtocolManagerUtil::GetNextBackOffInterval( | 159 next = V4ProtocolManagerUtil::GetNextBackOffInterval( |
171 &update_error_count_, &update_back_off_mult_); | 160 &update_error_count_, &update_back_off_mult_); |
172 } | 161 } |
173 | |
174 if (!last_response_time_.is_null()) { | |
175 // The callback spent some time updating the database, including disk I/O. | |
176 // Do not wait that extra time. | |
177 base::TimeDelta callback_time = Time::Now() - last_response_time_; | |
178 if (callback_time < next) { | |
179 next -= callback_time; | |
180 } else { | |
181 // If the callback took too long, schedule the next update with no delay. | |
182 next = base::TimeDelta(); | |
183 } | |
184 } | |
185 DVLOG(1) << "V4UpdateProtocolManager::GetNextUpdateInterval: " | 162 DVLOG(1) << "V4UpdateProtocolManager::GetNextUpdateInterval: " |
186 << "next_interval: " << next; | 163 << "next_interval: " << next; |
187 return next; | 164 return next; |
188 } | 165 } |
189 | 166 |
190 void V4UpdateProtocolManager::ScheduleNextUpdateAfterInterval( | 167 void V4UpdateProtocolManager::ScheduleNextUpdateAfterInterval( |
191 base::TimeDelta interval) { | 168 base::TimeDelta interval) { |
192 DCHECK(CalledOnValidThread()); | 169 DCHECK(CalledOnValidThread()); |
193 DCHECK(interval >= base::TimeDelta()); | 170 DCHECK(interval >= base::TimeDelta()); |
194 | |
195 // Unschedule any current timer. | 171 // Unschedule any current timer. |
196 update_timer_.Stop(); | 172 update_timer_.Stop(); |
197 update_timer_.Start(FROM_HERE, interval, this, | 173 update_timer_.Start(FROM_HERE, interval, this, |
198 &V4UpdateProtocolManager::IssueUpdateRequest); | 174 &V4UpdateProtocolManager::IssueUpdateRequest); |
199 } | 175 } |
200 | 176 |
201 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( | 177 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( |
202 const base::hash_map<UpdateListIdentifier, std::string>& | 178 const base::hash_map<UpdateListIdentifier, std::string>& |
203 current_list_states) { | 179 current_list_states) { |
204 // Build the request. Client info and client states are not added to the | 180 // Build the request. Client info and client states are not added to the |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 245 |
270 void V4UpdateProtocolManager::IssueUpdateRequest() { | 246 void V4UpdateProtocolManager::IssueUpdateRequest() { |
271 DCHECK(CalledOnValidThread()); | 247 DCHECK(CalledOnValidThread()); |
272 | 248 |
273 // If an update request is already pending, record and return silently. | 249 // If an update request is already pending, record and return silently. |
274 if (request_.get()) { | 250 if (request_.get()) { |
275 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); | 251 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); |
276 return; | 252 return; |
277 } | 253 } |
278 | 254 |
279 std::string req_base64 = | 255 std::string req_base64 = GetBase64SerializedUpdateRequestProto( |
280 GetBase64SerializedUpdateRequestProto(current_list_states_); | 256 current_list_states_); |
281 GURL update_url = GetUpdateUrl(req_base64); | 257 GURL update_url = GetUpdateUrl(req_base64); |
282 | 258 |
283 request_.reset(net::URLFetcher::Create(url_fetcher_id_++, update_url, | 259 request_.reset(net::URLFetcher::Create(url_fetcher_id_++, update_url, |
284 net::URLFetcher::GET, this) | 260 net::URLFetcher::GET, this) |
285 .release()); | 261 .release()); |
286 | 262 |
287 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 263 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
288 request_->SetRequestContext(request_context_getter_.get()); | 264 request_->SetRequestContext(request_context_getter_.get()); |
289 request_->Start(); | 265 request_->Start(); |
290 // TODO(vakh): Handle request timeout. | 266 // TODO(vakh): Handle request timeout. |
291 } | 267 } |
292 | 268 |
293 // net::URLFetcherDelegate implementation ---------------------------------- | 269 // net::URLFetcherDelegate implementation ---------------------------------- |
294 | 270 |
295 // SafeBrowsing request responses are handled here. | 271 // SafeBrowsing request responses are handled here. |
296 void V4UpdateProtocolManager::OnURLFetchComplete( | 272 void V4UpdateProtocolManager::OnURLFetchComplete( |
297 const net::URLFetcher* source) { | 273 const net::URLFetcher* source) { |
298 DCHECK(CalledOnValidThread()); | 274 DCHECK(CalledOnValidThread()); |
299 | 275 |
300 int response_code = source->GetResponseCode(); | 276 int response_code = source->GetResponseCode(); |
301 net::URLRequestStatus status = source->GetStatus(); | 277 net::URLRequestStatus status = source->GetStatus(); |
302 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 278 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
303 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code); | 279 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code); |
304 | 280 |
305 last_response_time_ = Time::Now(); | |
306 | |
307 std::vector<ListUpdateResponse> list_update_responses; | 281 std::vector<ListUpdateResponse> list_update_responses; |
| 282 bool back_off; |
308 if (status.is_success() && response_code == net::HTTP_OK) { | 283 if (status.is_success() && response_code == net::HTTP_OK) { |
| 284 back_off = false; |
309 RecordUpdateResult(V4OperationResult::STATUS_200); | 285 RecordUpdateResult(V4OperationResult::STATUS_200); |
310 ResetUpdateErrors(); | 286 ResetUpdateErrors(); |
311 std::string data; | 287 std::string data; |
312 source->GetResponseAsString(&data); | 288 source->GetResponseAsString(&data); |
313 if (!ParseUpdateResponse(data, &list_update_responses)) { | 289 if (!ParseUpdateResponse(data, &list_update_responses)) { |
314 list_update_responses.clear(); | 290 list_update_responses.clear(); |
315 RecordUpdateResult(V4OperationResult::PARSE_ERROR); | 291 RecordUpdateResult(V4OperationResult::PARSE_ERROR); |
316 } | 292 } |
317 request_.reset(); | |
318 | |
319 // Invoke the callback with list_update_responses. | 293 // Invoke the callback with list_update_responses. |
320 // The caller should update its state now, based on list_update_responses. | 294 // The caller should update its state now, based on list_update_responses. |
321 // The callback must call ScheduleNextUpdate() at the end to resume | 295 callback_.Run(list_update_responses); |
322 // downloading updates. | |
323 update_callback_.Run(list_update_responses); | |
324 } else { | 296 } else { |
| 297 back_off = true; |
325 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " | 298 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " |
326 << source->GetURL() << " failed with error: " << status.error() | 299 << source->GetURL() << " failed with error: " << status.error() |
327 << " and response code: " << response_code; | 300 << " and response code: " << response_code; |
328 | 301 |
329 if (status.status() == net::URLRequestStatus::FAILED) { | 302 if (status.status() == net::URLRequestStatus::FAILED) { |
330 RecordUpdateResult(V4OperationResult::NETWORK_ERROR); | 303 RecordUpdateResult(V4OperationResult::NETWORK_ERROR); |
331 } else { | 304 } else { |
332 RecordUpdateResult(V4OperationResult::HTTP_ERROR); | 305 RecordUpdateResult(V4OperationResult::HTTP_ERROR); |
333 } | 306 } |
334 // TODO(vakh): Figure out whether it is just a network error vs backoff vs | 307 // TODO(vakh): Figure out whether it is just a network error vs backoff vs |
335 // another condition and RecordUpdateResult more accurately. | 308 // another condition and RecordUpdateResult more accurately. |
336 | |
337 request_.reset(); | |
338 ScheduleNextUpdateWithBackoff(true); | |
339 } | 309 } |
| 310 request_.reset(); |
| 311 ScheduleNextUpdate(back_off); |
340 } | 312 } |
341 | 313 |
342 GURL V4UpdateProtocolManager::GetUpdateUrl( | 314 GURL V4UpdateProtocolManager::GetUpdateUrl( |
343 const std::string& req_base64) const { | 315 const std::string& req_base64) const { |
344 GURL url = V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedUpdates", | 316 GURL url = V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedUpdates", |
345 config_); | 317 config_); |
346 DVLOG(1) << "V4UpdateProtocolManager::GetUpdateUrl: " | 318 DVLOG(1) << "V4UpdateProtocolManager::GetUpdateUrl: " |
347 << "url: " << url; | 319 << "url: " << url; |
348 return url; | 320 return url; |
349 } | 321 } |
350 | 322 |
351 } // namespace safe_browsing | 323 } // namespace safe_browsing |
OLD | NEW |