| 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 70 |
| 71 // The default V4UpdateProtocolManagerFactory. | 71 // The default V4UpdateProtocolManagerFactory. |
| 72 class V4UpdateProtocolManagerFactoryImpl | 72 class V4UpdateProtocolManagerFactoryImpl |
| 73 : public V4UpdateProtocolManagerFactory { | 73 : public V4UpdateProtocolManagerFactory { |
| 74 public: | 74 public: |
| 75 V4UpdateProtocolManagerFactoryImpl() {} | 75 V4UpdateProtocolManagerFactoryImpl() {} |
| 76 ~V4UpdateProtocolManagerFactoryImpl() override {} | 76 ~V4UpdateProtocolManagerFactoryImpl() override {} |
| 77 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( | 77 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( |
| 78 net::URLRequestContextGetter* request_context_getter, | 78 net::URLRequestContextGetter* request_context_getter, |
| 79 const V4ProtocolConfig& config, | 79 const V4ProtocolConfig& config, |
| 80 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 81 current_list_states, | |
| 82 V4UpdateCallback callback) override { | 80 V4UpdateCallback callback) override { |
| 83 return std::unique_ptr<V4UpdateProtocolManager>(new V4UpdateProtocolManager( | 81 return std::unique_ptr<V4UpdateProtocolManager>( |
| 84 request_context_getter, config, current_list_states, callback)); | 82 new V4UpdateProtocolManager(request_context_getter, config, callback)); |
| 85 } | 83 } |
| 86 | 84 |
| 87 private: | 85 private: |
| 88 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactoryImpl); | 86 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactoryImpl); |
| 89 }; | 87 }; |
| 90 | 88 |
| 91 // V4UpdateProtocolManager implementation -------------------------------- | 89 // V4UpdateProtocolManager implementation -------------------------------- |
| 92 | 90 |
| 93 // static | 91 // static |
| 94 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL; | 92 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL; |
| 95 | 93 |
| 96 // static | 94 // static |
| 97 std::unique_ptr<V4UpdateProtocolManager> V4UpdateProtocolManager::Create( | 95 std::unique_ptr<V4UpdateProtocolManager> V4UpdateProtocolManager::Create( |
| 98 net::URLRequestContextGetter* request_context_getter, | 96 net::URLRequestContextGetter* request_context_getter, |
| 99 const V4ProtocolConfig& config, | 97 const V4ProtocolConfig& config, |
| 100 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 101 current_list_states, | |
| 102 V4UpdateCallback callback) { | 98 V4UpdateCallback callback) { |
| 103 if (!factory_) { | 99 if (!factory_) { |
| 104 factory_ = new V4UpdateProtocolManagerFactoryImpl(); | 100 factory_ = new V4UpdateProtocolManagerFactoryImpl(); |
| 105 } | 101 } |
| 106 return factory_->CreateProtocolManager(request_context_getter, config, | 102 return factory_->CreateProtocolManager(request_context_getter, config, |
| 107 current_list_states, callback); | 103 callback); |
| 108 } | 104 } |
| 109 | 105 |
| 110 void V4UpdateProtocolManager::ResetUpdateErrors() { | 106 void V4UpdateProtocolManager::ResetUpdateErrors() { |
| 111 update_error_count_ = 0; | 107 update_error_count_ = 0; |
| 112 update_back_off_mult_ = 1; | 108 update_back_off_mult_ = 1; |
| 113 } | 109 } |
| 114 | 110 |
| 115 V4UpdateProtocolManager::V4UpdateProtocolManager( | 111 V4UpdateProtocolManager::V4UpdateProtocolManager( |
| 116 net::URLRequestContextGetter* request_context_getter, | 112 net::URLRequestContextGetter* request_context_getter, |
| 117 const V4ProtocolConfig& config, | 113 const V4ProtocolConfig& config, |
| 118 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 119 current_list_states, | |
| 120 V4UpdateCallback update_callback) | 114 V4UpdateCallback update_callback) |
| 121 : current_list_states_(current_list_states), | 115 : store_state_map_(nullptr), |
| 122 update_error_count_(0), | 116 update_error_count_(0), |
| 123 update_back_off_mult_(1), | 117 update_back_off_mult_(1), |
| 124 next_update_interval_(base::TimeDelta::FromSeconds( | 118 next_update_interval_(base::TimeDelta::FromSeconds( |
| 125 base::RandInt(kV4TimerStartIntervalSecMin, | 119 base::RandInt(kV4TimerStartIntervalSecMin, |
| 126 kV4TimerStartIntervalSecMax))), | 120 kV4TimerStartIntervalSecMax))), |
| 127 config_(config), | 121 config_(config), |
| 128 request_context_getter_(request_context_getter), | 122 request_context_getter_(request_context_getter), |
| 129 url_fetcher_id_(0), | 123 url_fetcher_id_(0), |
| 130 update_callback_(update_callback) { | 124 update_callback_(update_callback) { |
| 131 // Do not auto-schedule updates. Let the owner (V4LocalDatabaseManager) do it | 125 // Do not auto-schedule updates. Let the owner (V4LocalDatabaseManager) do it |
| 132 // when it is ready to process updates. | 126 // when it is ready to process updates. |
| 133 DVLOG(1) << "V4UpdateProtocolManager::V4UpdateProtocolManager: " | |
| 134 << "next_update_interval_: " << next_update_interval_; | |
| 135 } | 127 } |
| 136 | 128 |
| 137 V4UpdateProtocolManager::~V4UpdateProtocolManager() {} | 129 V4UpdateProtocolManager::~V4UpdateProtocolManager() {} |
| 138 | 130 |
| 139 bool V4UpdateProtocolManager::IsUpdateScheduled() const { | 131 bool V4UpdateProtocolManager::IsUpdateScheduled() const { |
| 140 return update_timer_.IsRunning(); | 132 return update_timer_.IsRunning(); |
| 141 } | 133 } |
| 142 | 134 |
| 143 void V4UpdateProtocolManager::ScheduleNextUpdate() { | 135 void V4UpdateProtocolManager::ScheduleNextUpdate( |
| 136 const StoreStateMap* store_state_map) { |
| 137 DCHECK(store_state_map); |
| 138 set_store_state_map(store_state_map); |
| 144 ScheduleNextUpdateWithBackoff(false); | 139 ScheduleNextUpdateWithBackoff(false); |
| 145 } | 140 } |
| 146 | 141 |
| 147 void V4UpdateProtocolManager::ScheduleNextUpdateWithBackoff(bool back_off) { | 142 void V4UpdateProtocolManager::ScheduleNextUpdateWithBackoff(bool back_off) { |
| 148 DCHECK(CalledOnValidThread()); | 143 DCHECK(CalledOnValidThread()); |
| 149 | 144 |
| 150 // TODO(vakh): Set disable_auto_update correctly using the command line | 145 // TODO(vakh): Set disable_auto_update correctly using the command line |
| 151 // switch. | 146 // switch. |
| 152 if (config_.disable_auto_update) { | 147 if (config_.disable_auto_update) { |
| 153 DCHECK(!IsUpdateScheduled()); | 148 DCHECK(!IsUpdateScheduled()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 base::TimeDelta interval) { | 186 base::TimeDelta interval) { |
| 192 DCHECK(CalledOnValidThread()); | 187 DCHECK(CalledOnValidThread()); |
| 193 DCHECK(interval >= base::TimeDelta()); | 188 DCHECK(interval >= base::TimeDelta()); |
| 194 | 189 |
| 195 // Unschedule any current timer. | 190 // Unschedule any current timer. |
| 196 update_timer_.Stop(); | 191 update_timer_.Stop(); |
| 197 update_timer_.Start(FROM_HERE, interval, this, | 192 update_timer_.Start(FROM_HERE, interval, this, |
| 198 &V4UpdateProtocolManager::IssueUpdateRequest); | 193 &V4UpdateProtocolManager::IssueUpdateRequest); |
| 199 } | 194 } |
| 200 | 195 |
| 196 // static |
| 201 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( | 197 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( |
| 202 const base::hash_map<UpdateListIdentifier, std::string>& | 198 const StoreStateMap* store_state_map) { |
| 203 current_list_states) { | |
| 204 // Build the request. Client info and client states are not added to the | 199 // Build the request. Client info and client states are not added to the |
| 205 // request protocol buffer. Client info is passed as params in the url. | 200 // request protocol buffer. Client info is passed as params in the url. |
| 206 FetchThreatListUpdatesRequest request; | 201 FetchThreatListUpdatesRequest request; |
| 207 for (const auto& entry : current_list_states) { | 202 for (const auto& entry : *store_state_map) { |
| 208 const auto& list_to_update = entry.first; | 203 const auto& list_to_update = entry.first; |
| 209 const auto& state = entry.second; | 204 const auto& state = entry.second; |
| 210 ListUpdateRequest* list_update_request = request.add_list_update_requests(); | 205 ListUpdateRequest* list_update_request = request.add_list_update_requests(); |
| 211 list_update_request->set_platform_type(list_to_update.platform_type); | 206 list_update_request->set_platform_type(list_to_update.platform_type); |
| 212 list_update_request->set_threat_entry_type( | 207 list_update_request->set_threat_entry_type( |
| 213 list_to_update.threat_entry_type); | 208 list_to_update.threat_entry_type); |
| 214 list_update_request->set_threat_type(list_to_update.threat_type); | 209 list_update_request->set_threat_type(list_to_update.threat_type); |
| 215 | 210 |
| 216 if (!state.empty()) { | 211 if (!state.empty()) { |
| 217 list_update_request->set_state(state); | 212 list_update_request->set_state(state); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 void V4UpdateProtocolManager::IssueUpdateRequest() { | 265 void V4UpdateProtocolManager::IssueUpdateRequest() { |
| 271 DCHECK(CalledOnValidThread()); | 266 DCHECK(CalledOnValidThread()); |
| 272 | 267 |
| 273 // If an update request is already pending, record and return silently. | 268 // If an update request is already pending, record and return silently. |
| 274 if (request_.get()) { | 269 if (request_.get()) { |
| 275 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); | 270 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); |
| 276 return; | 271 return; |
| 277 } | 272 } |
| 278 | 273 |
| 279 std::string req_base64 = | 274 std::string req_base64 = |
| 280 GetBase64SerializedUpdateRequestProto(current_list_states_); | 275 GetBase64SerializedUpdateRequestProto(store_state_map_); |
| 281 GURL update_url; | 276 GURL update_url; |
| 282 net::HttpRequestHeaders headers; | 277 net::HttpRequestHeaders headers; |
| 283 GetUpdateUrlAndHeaders(req_base64, &update_url, &headers); | 278 GetUpdateUrlAndHeaders(req_base64, &update_url, &headers); |
| 284 | 279 |
| 285 std::unique_ptr<net::URLFetcher> fetcher = net::URLFetcher::Create( | 280 std::unique_ptr<net::URLFetcher> fetcher = net::URLFetcher::Create( |
| 286 url_fetcher_id_++, update_url, net::URLFetcher::GET, this); | 281 url_fetcher_id_++, update_url, net::URLFetcher::GET, this); |
| 287 fetcher->SetExtraRequestHeaders(headers.ToString()); | 282 fetcher->SetExtraRequestHeaders(headers.ToString()); |
| 288 | 283 |
| 289 request_.reset(fetcher.release()); | 284 request_.reset(fetcher.release()); |
| 290 | 285 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 313 RecordUpdateResult(V4OperationResult::STATUS_200); | 308 RecordUpdateResult(V4OperationResult::STATUS_200); |
| 314 ResetUpdateErrors(); | 309 ResetUpdateErrors(); |
| 315 std::string data; | 310 std::string data; |
| 316 source->GetResponseAsString(&data); | 311 source->GetResponseAsString(&data); |
| 317 if (!ParseUpdateResponse(data, &list_update_responses)) { | 312 if (!ParseUpdateResponse(data, &list_update_responses)) { |
| 318 list_update_responses.clear(); | 313 list_update_responses.clear(); |
| 319 RecordUpdateResult(V4OperationResult::PARSE_ERROR); | 314 RecordUpdateResult(V4OperationResult::PARSE_ERROR); |
| 320 } | 315 } |
| 321 request_.reset(); | 316 request_.reset(); |
| 322 | 317 |
| 318 DVLOG(1) << "OnURLFetchComplete: response_size: " << data.size(); |
| 319 UMA_HISTOGRAM_COUNTS("SafeBrowsing.V4UpdateResponseSizeKB", |
| 320 data.size() / 1024); |
| 321 |
| 323 // Invoke the callback with list_update_responses. | 322 // Invoke the callback with list_update_responses. |
| 324 // The caller should update its state now, based on list_update_responses. | 323 // The caller should update its state now, based on list_update_responses. |
| 325 // The callback must call ScheduleNextUpdate() at the end to resume | 324 // The callback must call ScheduleNextUpdate() at the end to resume |
| 326 // downloading updates. | 325 // downloading updates. |
| 327 update_callback_.Run(list_update_responses); | 326 update_callback_.Run(list_update_responses); |
| 328 } else { | 327 } else { |
| 329 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " | 328 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " |
| 330 << source->GetURL() << " failed with error: " << status.error() | 329 << source->GetURL() << " failed with error: " << status.error() |
| 331 << " and response code: " << response_code; | 330 << " and response code: " << response_code; |
| 332 | 331 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 345 | 344 |
| 346 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( | 345 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( |
| 347 const std::string& req_base64, | 346 const std::string& req_base64, |
| 348 GURL* gurl, | 347 GURL* gurl, |
| 349 net::HttpRequestHeaders* headers) const { | 348 net::HttpRequestHeaders* headers) const { |
| 350 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( | 349 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( |
| 351 req_base64, "threatListUpdates:fetch", config_, gurl, headers); | 350 req_base64, "threatListUpdates:fetch", config_, gurl, headers); |
| 352 } | 351 } |
| 353 | 352 |
| 354 } // namespace safe_browsing | 353 } // namespace safe_browsing |
| OLD | NEW |