| 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/base64url.h" | 9 #include "base/base64url.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 kV4TimerStartIntervalSecMax))), | 146 kV4TimerStartIntervalSecMax))), |
| 147 config_(config), | 147 config_(config), |
| 148 request_context_getter_(request_context_getter), | 148 request_context_getter_(request_context_getter), |
| 149 url_fetcher_id_(0), | 149 url_fetcher_id_(0), |
| 150 update_callback_(update_callback), | 150 update_callback_(update_callback), |
| 151 extended_reporting_level_callback_(extended_reporting_level_callback) { | 151 extended_reporting_level_callback_(extended_reporting_level_callback) { |
| 152 // Do not auto-schedule updates. Let the owner (V4LocalDatabaseManager) do it | 152 // Do not auto-schedule updates. Let the owner (V4LocalDatabaseManager) do it |
| 153 // when it is ready to process updates. | 153 // when it is ready to process updates. |
| 154 } | 154 } |
| 155 | 155 |
| 156 V4UpdateProtocolManager::~V4UpdateProtocolManager() {} | 156 V4UpdateProtocolManager::~V4UpdateProtocolManager() { |
| 157 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 158 } |
| 157 | 159 |
| 158 bool V4UpdateProtocolManager::IsUpdateScheduled() const { | 160 bool V4UpdateProtocolManager::IsUpdateScheduled() const { |
| 159 return update_timer_.IsRunning(); | 161 return update_timer_.IsRunning(); |
| 160 } | 162 } |
| 161 | 163 |
| 162 void V4UpdateProtocolManager::ScheduleNextUpdate( | 164 void V4UpdateProtocolManager::ScheduleNextUpdate( |
| 163 std::unique_ptr<StoreStateMap> store_state_map) { | 165 std::unique_ptr<StoreStateMap> store_state_map) { |
| 164 store_state_map_ = std::move(store_state_map); | 166 store_state_map_ = std::move(store_state_map); |
| 165 ScheduleNextUpdateWithBackoff(false); | 167 ScheduleNextUpdateWithBackoff(false); |
| 166 } | 168 } |
| 167 | 169 |
| 168 void V4UpdateProtocolManager::ScheduleNextUpdateWithBackoff(bool back_off) { | 170 void V4UpdateProtocolManager::ScheduleNextUpdateWithBackoff(bool back_off) { |
| 169 DCHECK(CalledOnValidThread()); | 171 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 170 | 172 |
| 171 if (config_.disable_auto_update) { | 173 if (config_.disable_auto_update) { |
| 172 DCHECK(!IsUpdateScheduled()); | 174 DCHECK(!IsUpdateScheduled()); |
| 173 return; | 175 return; |
| 174 } | 176 } |
| 175 | 177 |
| 176 // Reschedule with the new update. | 178 // Reschedule with the new update. |
| 177 base::TimeDelta next_update_interval = GetNextUpdateInterval(back_off); | 179 base::TimeDelta next_update_interval = GetNextUpdateInterval(back_off); |
| 178 ScheduleNextUpdateAfterInterval(next_update_interval); | 180 ScheduleNextUpdateAfterInterval(next_update_interval); |
| 179 } | 181 } |
| 180 | 182 |
| 181 // According to section 5 of the SafeBrowsing protocol specification, we must | 183 // According to section 5 of the SafeBrowsing protocol specification, we must |
| 182 // back off after a certain number of errors. | 184 // back off after a certain number of errors. |
| 183 base::TimeDelta V4UpdateProtocolManager::GetNextUpdateInterval(bool back_off) { | 185 base::TimeDelta V4UpdateProtocolManager::GetNextUpdateInterval(bool back_off) { |
| 184 DCHECK(CalledOnValidThread()); | 186 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 185 DCHECK(next_update_interval_ > base::TimeDelta()); | 187 DCHECK(next_update_interval_ > base::TimeDelta()); |
| 186 | 188 |
| 187 base::TimeDelta next = next_update_interval_; | 189 base::TimeDelta next = next_update_interval_; |
| 188 if (back_off) { | 190 if (back_off) { |
| 189 next = V4ProtocolManagerUtil::GetNextBackOffInterval( | 191 next = V4ProtocolManagerUtil::GetNextBackOffInterval( |
| 190 &update_error_count_, &update_back_off_mult_); | 192 &update_error_count_, &update_back_off_mult_); |
| 191 } | 193 } |
| 192 | 194 |
| 193 if (!last_response_time_.is_null()) { | 195 if (!last_response_time_.is_null()) { |
| 194 // The callback spent some time updating the database, including disk I/O. | 196 // The callback spent some time updating the database, including disk I/O. |
| 195 // Do not wait that extra time. | 197 // Do not wait that extra time. |
| 196 base::TimeDelta callback_time = Time::Now() - last_response_time_; | 198 base::TimeDelta callback_time = Time::Now() - last_response_time_; |
| 197 if (callback_time < next) { | 199 if (callback_time < next) { |
| 198 next -= callback_time; | 200 next -= callback_time; |
| 199 } else { | 201 } else { |
| 200 // If the callback took too long, schedule the next update with no delay. | 202 // If the callback took too long, schedule the next update with no delay. |
| 201 next = base::TimeDelta(); | 203 next = base::TimeDelta(); |
| 202 } | 204 } |
| 203 } | 205 } |
| 204 DVLOG(1) << "V4UpdateProtocolManager::GetNextUpdateInterval: " | 206 DVLOG(1) << "V4UpdateProtocolManager::GetNextUpdateInterval: " |
| 205 << "next_interval: " << next; | 207 << "next_interval: " << next; |
| 206 return next; | 208 return next; |
| 207 } | 209 } |
| 208 | 210 |
| 209 void V4UpdateProtocolManager::ScheduleNextUpdateAfterInterval( | 211 void V4UpdateProtocolManager::ScheduleNextUpdateAfterInterval( |
| 210 base::TimeDelta interval) { | 212 base::TimeDelta interval) { |
| 211 DCHECK(CalledOnValidThread()); | 213 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 212 DCHECK(interval >= base::TimeDelta()); | 214 DCHECK(interval >= base::TimeDelta()); |
| 213 | 215 |
| 214 // Unschedule any current timer. | 216 // Unschedule any current timer. |
| 215 update_timer_.Stop(); | 217 update_timer_.Stop(); |
| 216 update_timer_.Start(FROM_HERE, interval, this, | 218 update_timer_.Start(FROM_HERE, interval, this, |
| 217 &V4UpdateProtocolManager::IssueUpdateRequest); | 219 &V4UpdateProtocolManager::IssueUpdateRequest); |
| 218 } | 220 } |
| 219 | 221 |
| 220 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto() { | 222 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto() { |
| 221 DCHECK(!store_state_map_->empty()); | 223 DCHECK(!store_state_map_->empty()); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 } else { | 294 } else { |
| 293 std::unique_ptr<ListUpdateResponse> add(new ListUpdateResponse); | 295 std::unique_ptr<ListUpdateResponse> add(new ListUpdateResponse); |
| 294 add->Swap(&list_update_response); | 296 add->Swap(&list_update_response); |
| 295 parsed_server_response->push_back(std::move(add)); | 297 parsed_server_response->push_back(std::move(add)); |
| 296 } | 298 } |
| 297 } | 299 } |
| 298 return true; | 300 return true; |
| 299 } | 301 } |
| 300 | 302 |
| 301 void V4UpdateProtocolManager::IssueUpdateRequest() { | 303 void V4UpdateProtocolManager::IssueUpdateRequest() { |
| 302 DCHECK(CalledOnValidThread()); | 304 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 303 | 305 |
| 304 // If an update request is already pending, record and return silently. | 306 // If an update request is already pending, record and return silently. |
| 305 if (request_.get()) { | 307 if (request_.get()) { |
| 306 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); | 308 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); |
| 307 return; | 309 return; |
| 308 } | 310 } |
| 309 | 311 |
| 310 std::string req_base64 = GetBase64SerializedUpdateRequestProto(); | 312 std::string req_base64 = GetBase64SerializedUpdateRequestProto(); |
| 311 GURL update_url; | 313 GURL update_url; |
| 312 net::HttpRequestHeaders headers; | 314 net::HttpRequestHeaders headers; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 UMA_HISTOGRAM_BOOLEAN("SafeBrowsing.V4Update.TimedOut", true); | 365 UMA_HISTOGRAM_BOOLEAN("SafeBrowsing.V4Update.TimedOut", true); |
| 364 request_.reset(); | 366 request_.reset(); |
| 365 ScheduleNextUpdateWithBackoff(false); | 367 ScheduleNextUpdateWithBackoff(false); |
| 366 } | 368 } |
| 367 | 369 |
| 368 // net::URLFetcherDelegate implementation ---------------------------------- | 370 // net::URLFetcherDelegate implementation ---------------------------------- |
| 369 | 371 |
| 370 // SafeBrowsing request responses are handled here. | 372 // SafeBrowsing request responses are handled here. |
| 371 void V4UpdateProtocolManager::OnURLFetchComplete( | 373 void V4UpdateProtocolManager::OnURLFetchComplete( |
| 372 const net::URLFetcher* source) { | 374 const net::URLFetcher* source) { |
| 373 DCHECK(CalledOnValidThread()); | 375 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 374 | 376 |
| 375 timeout_timer_.Stop(); | 377 timeout_timer_.Stop(); |
| 376 | 378 |
| 377 int response_code = source->GetResponseCode(); | 379 int response_code = source->GetResponseCode(); |
| 378 net::URLRequestStatus status = source->GetStatus(); | 380 net::URLRequestStatus status = source->GetStatus(); |
| 379 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( | 381 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( |
| 380 "SafeBrowsing.V4Update.Network.Result", status, response_code); | 382 "SafeBrowsing.V4Update.Network.Result", status, response_code); |
| 381 UMA_HISTOGRAM_BOOLEAN("SafeBrowsing.V4Update.TimedOut", false); | 383 UMA_HISTOGRAM_BOOLEAN("SafeBrowsing.V4Update.TimedOut", false); |
| 382 | 384 |
| 383 last_response_time_ = Time::Now(); | 385 last_response_time_ = Time::Now(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 | 424 |
| 423 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( | 425 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( |
| 424 const std::string& req_base64, | 426 const std::string& req_base64, |
| 425 GURL* gurl, | 427 GURL* gurl, |
| 426 net::HttpRequestHeaders* headers) const { | 428 net::HttpRequestHeaders* headers) const { |
| 427 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( | 429 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( |
| 428 req_base64, "threatListUpdates:fetch", config_, gurl, headers); | 430 req_base64, "threatListUpdates:fetch", config_, gurl, headers); |
| 429 } | 431 } |
| 430 | 432 |
| 431 } // namespace safe_browsing | 433 } // namespace safe_browsing |
| OLD | NEW |