Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager.cc

Issue 1848973004: Makes V4UpdateProtocolManager auto-schedule update fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_
Patch Set: formatting and removed ClearCurrentListStates from test Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/rand_util.h"
12 #include "base/timer/timer.h" 13 #include "base/timer/timer.h"
13 #include "components/safe_browsing_db/safebrowsing.pb.h" 14 #include "components/safe_browsing_db/safebrowsing.pb.h"
14 #include "net/base/load_flags.h" 15 #include "net/base/load_flags.h"
15 #include "net/http/http_response_headers.h" 16 #include "net/http/http_response_headers.h"
16 #include "net/http/http_status_code.h" 17 #include "net/http/http_status_code.h"
17 #include "net/url_request/url_fetcher.h" 18 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_request_context_getter.h" 19 #include "net/url_request/url_request_context_getter.h"
19 20
20 using base::Time; 21 using base::Time;
21 using base::TimeDelta; 22 using base::TimeDelta;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 void RecordUpdateResult(safe_browsing::V4OperationResult result) { 55 void RecordUpdateResult(safe_browsing::V4OperationResult result) {
55 UMA_HISTOGRAM_ENUMERATION( 56 UMA_HISTOGRAM_ENUMERATION(
56 "SafeBrowsing.V4UpdateResult", result, 57 "SafeBrowsing.V4UpdateResult", result,
57 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX); 58 safe_browsing::V4OperationResult::OPERATION_RESULT_MAX);
58 } 59 }
59 60
60 } // namespace 61 } // namespace
61 62
62 namespace safe_browsing { 63 namespace safe_browsing {
63 64
65 // Minimum time, in seconds, from start up before we must issue an update query.
66 static const int kV4TimerStartIntervalSecMin = 60;
67
68 // Maximum time, in seconds, from start up before we must issue an update query.
69 static const int kV4TimerStartIntervalSecMax = 300;
70
64 // The default V4UpdateProtocolManagerFactory. 71 // The default V4UpdateProtocolManagerFactory.
65 class V4UpdateProtocolManagerFactoryImpl 72 class V4UpdateProtocolManagerFactoryImpl
66 : public V4UpdateProtocolManagerFactory { 73 : public V4UpdateProtocolManagerFactory {
67 public: 74 public:
68 V4UpdateProtocolManagerFactoryImpl() {} 75 V4UpdateProtocolManagerFactoryImpl() {}
69 ~V4UpdateProtocolManagerFactoryImpl() override {} 76 ~V4UpdateProtocolManagerFactoryImpl() override {}
70 V4UpdateProtocolManager* CreateProtocolManager( 77 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager(
71 net::URLRequestContextGetter* request_context_getter, 78 net::URLRequestContextGetter* request_context_getter,
72 const V4ProtocolConfig& config) override { 79 const V4ProtocolConfig& config,
73 return new V4UpdateProtocolManager(request_context_getter, config); 80 const base::hash_map<UpdateListIdentifier, std::string>&
81 current_list_states,
82 V4UpdateCallback callback,
83 bool is_headless) override {
84 return scoped_ptr<V4UpdateProtocolManager>(new V4UpdateProtocolManager(
Nathan Parker 2016/04/01 01:04:52 I think you can drop the scoped_ptr<..> bit, and i
vakh (use Gerrit instead) 2016/04/01 02:23:02 Tried. It doesn't.
85 request_context_getter, config, current_list_states, callback,
86 is_headless));
74 } 87 }
75 88
76 private: 89 private:
77 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactoryImpl); 90 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactoryImpl);
78 }; 91 };
79 92
80 // V4UpdateProtocolManager implementation -------------------------------- 93 // V4UpdateProtocolManager implementation --------------------------------
81 94
82 // static 95 // static
83 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL; 96 V4UpdateProtocolManagerFactory* V4UpdateProtocolManager::factory_ = NULL;
84 97
85 // static 98 // static
86 V4UpdateProtocolManager* V4UpdateProtocolManager::Create( 99 scoped_ptr<V4UpdateProtocolManager> V4UpdateProtocolManager::Create(
87 net::URLRequestContextGetter* request_context_getter, 100 net::URLRequestContextGetter* request_context_getter,
88 const V4ProtocolConfig& config) { 101 const V4ProtocolConfig& config,
102 const base::hash_map<UpdateListIdentifier, std::string>&
103 current_list_states,
104 V4UpdateCallback callback,
105 bool is_headless) {
89 if (!factory_) 106 if (!factory_)
90 factory_ = new V4UpdateProtocolManagerFactoryImpl(); 107 factory_ = new V4UpdateProtocolManagerFactoryImpl();
91 return factory_->CreateProtocolManager(request_context_getter, config); 108 return factory_->CreateProtocolManager(request_context_getter, config,
109 current_list_states, callback,
110 is_headless);
92 } 111 }
93 112
94 void V4UpdateProtocolManager::ResetUpdateErrors() { 113 void V4UpdateProtocolManager::ResetUpdateErrors() {
95 update_error_count_ = 0; 114 update_error_count_ = 0;
96 update_back_off_mult_ = 1; 115 update_back_off_mult_ = 1;
97 } 116 }
98 117
99 V4UpdateProtocolManager::V4UpdateProtocolManager( 118 V4UpdateProtocolManager::V4UpdateProtocolManager(
100 net::URLRequestContextGetter* request_context_getter, 119 net::URLRequestContextGetter* request_context_getter,
101 const V4ProtocolConfig& config) 120 const V4ProtocolConfig& config,
102 : update_error_count_(0), 121 const base::hash_map<UpdateListIdentifier, std::string>&
122 current_list_states,
123 V4UpdateCallback callback,
124 bool is_headless)
125 : current_list_states_(current_list_states),
126 update_error_count_(0),
103 update_back_off_mult_(1), 127 update_back_off_mult_(1),
104 next_update_time_(base::Time()), 128 next_update_interval_(base::TimeDelta::FromSeconds(
129 base::RandInt(kV4TimerStartIntervalSecMin,
130 kV4TimerStartIntervalSecMax))),
105 config_(config), 131 config_(config),
106 request_context_getter_(request_context_getter), 132 request_context_getter_(request_context_getter),
107 url_fetcher_id_(0) {} 133 url_fetcher_id_(0),
134 callback_(callback) {
135 if (!is_headless) {
Nathan Parker 2016/04/01 01:04:52 The is_headless check seems like the wrong thing..
136 ScheduleNextUpdate(false /* no back off */);
137 }
138 }
108 139
109 V4UpdateProtocolManager::~V4UpdateProtocolManager() { 140 V4UpdateProtocolManager::~V4UpdateProtocolManager() {}
141
142 bool V4UpdateProtocolManager::IsUpdateScheduled() const {
143 return update_timer_.IsRunning();
144 }
145
146 void V4UpdateProtocolManager::ScheduleNextUpdate(bool back_off) {
147 // TODO(vakh): Set disable_auto_update correctly using the command line
148 // switch.
149 if (config_.disable_auto_update) {
Nathan Parker 2016/04/01 01:04:52 Can this change at runtime? If not, the update_ti
vakh (use Gerrit instead) 2016/04/01 02:23:02 Done.
150 // Unschedule any current timer.
151 update_timer_.Stop();
152 return;
153 }
154
155 // Reschedule with the new update.
156 base::TimeDelta next_update_interval = GetNextUpdateInterval(back_off);
157 ScheduleNextUpdateAfterInterval(next_update_interval);
158 }
159
160 // According to section 5 of the SafeBrowsing protocol specification, we must
161 // back off after a certain number of errors.
162 base::TimeDelta V4UpdateProtocolManager::GetNextUpdateInterval(bool back_off) {
163 DCHECK(CalledOnValidThread());
164 DCHECK(next_update_interval_ > base::TimeDelta());
165 base::TimeDelta next = next_update_interval_;
166 if (back_off) {
167 next = V4ProtocolManagerUtil::GetNextBackOffInterval(
168 &update_error_count_, &update_back_off_mult_);
169 }
170 return next;
171 }
172
173 void V4UpdateProtocolManager::ScheduleNextUpdateAfterInterval(
174 base::TimeDelta interval) {
175 DCHECK(CalledOnValidThread());
176 DCHECK(interval >= base::TimeDelta());
177 // Unschedule any current timer.
178 update_timer_.Stop();
179 update_timer_.Start(FROM_HERE, interval, this,
180 &V4UpdateProtocolManager::GetNextUpdate);
181 }
182
183 void V4UpdateProtocolManager::GetNextUpdate() {
Nathan Parker 2016/04/01 01:04:52 Can you drop this function altogether? IssueUpdate
vakh (use Gerrit instead) 2016/04/01 02:23:02 Done.
184 DCHECK(CalledOnValidThread());
185 if (request_.get())
186 return;
187
188 IssueUpdateRequest();
110 } 189 }
111 190
112 std::string V4UpdateProtocolManager::GetUpdateRequest( 191 std::string V4UpdateProtocolManager::GetUpdateRequest(
Nathan Parker 2016/04/01 01:04:52 Nit: "get" and "request" are slightly confounding.
vakh (use Gerrit instead) 2016/04/01 02:23:02 Done.
113 const base::hash_set<UpdateListIdentifier>& lists_to_update,
114 const base::hash_map<UpdateListIdentifier, std::string>& 192 const base::hash_map<UpdateListIdentifier, std::string>&
115 current_list_states) { 193 current_list_states) {
116 // Build the request. Client info and client states are not added to the 194 // Build the request. Client info and client states are not added to the
117 // request protocol buffer. Client info is passed as params in the url. 195 // request protocol buffer. Client info is passed as params in the url.
118 FetchThreatListUpdatesRequest request; 196 FetchThreatListUpdatesRequest request;
119 for (const auto& list_to_update : lists_to_update) { 197 for (const auto& entry : current_list_states) {
198 const auto& list_to_update = entry.first;
199 const auto& state = entry.second;
120 ListUpdateRequest* list_update_request = request.add_list_update_requests(); 200 ListUpdateRequest* list_update_request = request.add_list_update_requests();
121 list_update_request->set_platform_type(list_to_update.platform_type); 201 list_update_request->set_platform_type(list_to_update.platform_type);
122 list_update_request->set_threat_entry_type( 202 list_update_request->set_threat_entry_type(
123 list_to_update.threat_entry_type); 203 list_to_update.threat_entry_type);
124 list_update_request->set_threat_type(list_to_update.threat_type); 204 list_update_request->set_threat_type(list_to_update.threat_type);
125 205
126 // If the current state of the list is available, add that to the proto. 206 if (!state.empty()) {
127 base::hash_map<UpdateListIdentifier, std::string>::const_iterator 207 list_update_request->set_state(state);
128 list_iter = current_list_states.find(list_to_update);
129 if (list_iter != current_list_states.end()) {
130 list_update_request->set_state(list_iter->second);
131 } 208 }
132 } 209 }
133 210
134 // Serialize and Base64 encode. 211 // Serialize and Base64 encode.
135 std::string req_data, req_base64; 212 std::string req_data, req_base64;
136 request.SerializeToString(&req_data); 213 request.SerializeToString(&req_data);
137 base::Base64Encode(req_data, &req_base64); 214 base::Base64Encode(req_data, &req_base64);
138 215
139 return req_base64; 216 return req_base64;
140 } 217 }
141 218
142 bool V4UpdateProtocolManager::ParseUpdateResponse( 219 bool V4UpdateProtocolManager::ParseUpdateResponse(
143 const std::string& data, 220 const std::string& data,
144 std::vector<ListUpdateResponse>* list_update_responses) { 221 std::vector<ListUpdateResponse>* list_update_responses) {
145 FetchThreatListUpdatesResponse response; 222 FetchThreatListUpdatesResponse response;
146 223
147 if (!response.ParseFromString(data)) { 224 if (!response.ParseFromString(data)) {
148 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR); 225 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR);
149 return false; 226 return false;
150 } 227 }
151 228
152 if (response.has_minimum_wait_duration()) { 229 if (response.has_minimum_wait_duration()) {
153 // Seconds resolution is good enough so we ignore the nanos field. 230 // Seconds resolution is good enough so we ignore the nanos field.
154 base::TimeDelta next_update_interval = base::TimeDelta::FromSeconds( 231 next_update_interval_ = base::TimeDelta::FromSeconds(
155 response.minimum_wait_duration().seconds()); 232 response.minimum_wait_duration().seconds());
Nathan Parker 2016/04/01 01:04:52 a thought: We might want a sanity check on this, l
vakh (use Gerrit instead) 2016/04/01 02:23:02 Done.
156 next_update_time_ = Time::Now() + next_update_interval;
157 } 233 }
158 234
159 // TODO(vakh): Do something useful with this response. 235 // TODO(vakh): Do something useful with this response.
160 for (const ListUpdateResponse& list_update_response : 236 for (const ListUpdateResponse& list_update_response :
161 response.list_update_responses()) { 237 response.list_update_responses()) {
162 if (!list_update_response.has_platform_type()) { 238 if (!list_update_response.has_platform_type()) {
163 RecordParseUpdateResult(NO_PLATFORM_TYPE_ERROR); 239 RecordParseUpdateResult(NO_PLATFORM_TYPE_ERROR);
164 } else if (!list_update_response.has_threat_entry_type()) { 240 } else if (!list_update_response.has_threat_entry_type()) {
165 RecordParseUpdateResult(NO_THREAT_ENTRY_TYPE_ERROR); 241 RecordParseUpdateResult(NO_THREAT_ENTRY_TYPE_ERROR);
166 } else if (!list_update_response.has_threat_type()) { 242 } else if (!list_update_response.has_threat_type()) {
167 RecordParseUpdateResult(NO_THREAT_TYPE_ERROR); 243 RecordParseUpdateResult(NO_THREAT_TYPE_ERROR);
168 } else if (!list_update_response.has_new_client_state()) { 244 } else if (!list_update_response.has_new_client_state()) {
169 RecordParseUpdateResult(NO_STATE_ERROR); 245 RecordParseUpdateResult(NO_STATE_ERROR);
170 } else { 246 } else {
171 list_update_responses->push_back(list_update_response); 247 list_update_responses->push_back(list_update_response);
172 } 248 }
173 } 249 }
174 return true; 250 return true;
175 } 251 }
176 252
177 void V4UpdateProtocolManager::GetUpdates( 253 void V4UpdateProtocolManager::IssueUpdateRequest() {
178 const base::hash_set<UpdateListIdentifier>& lists_to_update,
179 const base::hash_map<UpdateListIdentifier, std::string>&
180 current_list_states,
181 UpdateCallback callback) {
182 DCHECK(CalledOnValidThread()); 254 DCHECK(CalledOnValidThread());
183 255
184 // If an update request is already pending, return an empty result. 256 // If an update request is already pending, record and return silently.
185 if (request_) { 257 if (request_.get()) {
186 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); 258 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR);
187 std::vector<ListUpdateResponse> list_update_responses;
188 callback.Run(list_update_responses);
189 return; 259 return;
190 } 260 }
191 261
192 // We need to wait the minimum waiting duration, and if we are in backoff, 262 std::string req_base64 = GetUpdateRequest(current_list_states_);
193 // we need to check if we're past the next allowed time. If we are, we can
194 // proceed with the request. If not, we are required to return empty results.
195 if (Time::Now() <= next_update_time_) {
196 if (update_error_count_) {
197 RecordUpdateResult(V4OperationResult::BACKOFF_ERROR);
198 } else {
199 RecordUpdateResult(V4OperationResult::MIN_WAIT_DURATION_ERROR);
200 }
201 std::vector<ListUpdateResponse> list_update_responses;
202 callback.Run(list_update_responses);
203 return;
204 }
205
206 std::string req_base64 =
207 GetUpdateRequest(lists_to_update, current_list_states);
208 GURL update_url = GetUpdateUrl(req_base64); 263 GURL update_url = GetUpdateUrl(req_base64);
209 264
210 request_.reset(net::URLFetcher::Create(url_fetcher_id_++, update_url, 265 request_.reset(net::URLFetcher::Create(url_fetcher_id_++, update_url,
211 net::URLFetcher::GET, this) 266 net::URLFetcher::GET, this)
212 .release()); 267 .release());
213 callback_ = callback;
214 268
215 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); 269 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE);
216 request_->SetRequestContext(request_context_getter_.get()); 270 request_->SetRequestContext(request_context_getter_.get());
217 request_->Start(); 271 request_->Start();
218 //TODO(vakh): Handle request timeout. 272 // TODO(vakh): Handle request timeout.
219 } 273 }
220 274
221 // net::URLFetcherDelegate implementation ---------------------------------- 275 // net::URLFetcherDelegate implementation ----------------------------------
222 276
223 // SafeBrowsing request responses are handled here. 277 // SafeBrowsing request responses are handled here.
224 void V4UpdateProtocolManager::OnURLFetchComplete( 278 void V4UpdateProtocolManager::OnURLFetchComplete(
225 const net::URLFetcher* source) { 279 const net::URLFetcher* source) {
226 DCHECK(CalledOnValidThread()); 280 DCHECK(CalledOnValidThread());
227 281
228 int response_code = source->GetResponseCode(); 282 int response_code = source->GetResponseCode();
229 net::URLRequestStatus status = source->GetStatus(); 283 net::URLRequestStatus status = source->GetStatus();
230 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( 284 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode(
231 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code); 285 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code);
232 286
233 std::vector<ListUpdateResponse> list_update_responses; 287 std::vector<ListUpdateResponse> list_update_responses;
288 bool back_off;
234 if (status.is_success() && response_code == net::HTTP_OK) { 289 if (status.is_success() && response_code == net::HTTP_OK) {
290 back_off = false;
235 RecordUpdateResult(V4OperationResult::STATUS_200); 291 RecordUpdateResult(V4OperationResult::STATUS_200);
236 ResetUpdateErrors(); 292 ResetUpdateErrors();
237 std::string data; 293 std::string data;
238 source->GetResponseAsString(&data); 294 source->GetResponseAsString(&data);
239 if (!ParseUpdateResponse(data, &list_update_responses)) { 295 if (!ParseUpdateResponse(data, &list_update_responses)) {
240 list_update_responses.clear(); 296 list_update_responses.clear();
241 RecordUpdateResult(V4OperationResult::PARSE_ERROR); 297 RecordUpdateResult(V4OperationResult::PARSE_ERROR);
242 } 298 }
299 // Invoke the callback with list_update_responses, even if there was a parse
300 // error or an error response code (in which case list_update_responses will
301 // be empty). The caller can't be blocked indefinitely.
302 callback_.Run(list_update_responses);
Nathan Parker 2016/04/01 01:04:52 What do you want the callback semantics to be? Sh
vakh (use Gerrit instead) 2016/04/01 02:23:02 Agree. That was the idea but the comment was incor
243 } else { 303 } else {
244 HandleUpdateError(Time::Now()); 304 back_off = true;
245
246 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " 305 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: "
247 << source->GetURL() << " failed with error: " << status.error() 306 << source->GetURL() << " failed with error: " << status.error()
248 << " and response code: " << response_code; 307 << " and response code: " << response_code;
249 308
250 if (status.status() == net::URLRequestStatus::FAILED) { 309 if (status.status() == net::URLRequestStatus::FAILED) {
251 RecordUpdateResult(V4OperationResult::NETWORK_ERROR); 310 RecordUpdateResult(V4OperationResult::NETWORK_ERROR);
252 } else { 311 } else {
253 RecordUpdateResult(V4OperationResult::HTTP_ERROR); 312 RecordUpdateResult(V4OperationResult::HTTP_ERROR);
254 } 313 }
314 // TODO(vakh): Figure out whether it is just a network error vs backoff vs
315 // another condition and RecordUpdateResult more accurately.
255 } 316 }
256
257 // Invoke the callback with list_update_responses, even if there was a parse
258 // error or an error response code (in which case list_update_responses will
259 // be empty). The caller can't be blocked indefinitely.
260 callback_.Run(list_update_responses);
261 request_.reset(); 317 request_.reset();
262 } 318 ScheduleNextUpdate(back_off);
Nathan Parker 2016/04/01 01:04:52 One concern: Now that this class owns the "state"
vakh (use Gerrit instead) 2016/04/01 02:23:02 I think it would be much cleaner and verifiable to
Nathan Parker 2016/04/01 17:56:58 That sounds good. Even without corruption, an upd
263
264 void V4UpdateProtocolManager::HandleUpdateError(const Time& now) {
265 DCHECK(CalledOnValidThread());
266 base::TimeDelta next = V4ProtocolManagerUtil::GetNextBackOffInterval(
267 &update_error_count_, &update_back_off_mult_);
268 next_update_time_ = now + next;
269 } 319 }
270 320
271 GURL V4UpdateProtocolManager::GetUpdateUrl( 321 GURL V4UpdateProtocolManager::GetUpdateUrl(
272 const std::string& req_base64) const { 322 const std::string& req_base64) const {
273 return V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedUpdates", 323 return V4ProtocolManagerUtil::GetRequestUrl(req_base64, "encodedUpdates",
274 config_); 324 config_);
275 } 325 }
276 326
277 } // namespace safe_browsing 327 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698