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