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

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

Issue 2103693002: SafeBrowsing PVer4: Send mutable response to the database and the stores (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@02_ReadFromDisk
Patch Set: git pull Created 4 years, 5 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/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
12 #include "base/rand_util.h" 13 #include "base/rand_util.h"
13 #include "base/timer/timer.h" 14 #include "base/timer/timer.h"
14 #include "components/safe_browsing_db/safebrowsing.pb.h" 15 #include "components/safe_browsing_db/safebrowsing.pb.h"
15 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
16 #include "net/http/http_response_headers.h" 17 #include "net/http/http_response_headers.h"
17 #include "net/http/http_status_code.h" 18 #include "net/http/http_status_code.h"
18 #include "net/url_request/url_fetcher.h" 19 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_context_getter.h" 20 #include "net/url_request/url_request_context_getter.h"
20 21
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 // Serialize and Base64 encode. 216 // Serialize and Base64 encode.
216 std::string req_data, req_base64; 217 std::string req_data, req_base64;
217 request.SerializeToString(&req_data); 218 request.SerializeToString(&req_data);
218 base::Base64Encode(req_data, &req_base64); 219 base::Base64Encode(req_data, &req_base64);
219 220
220 return req_base64; 221 return req_base64;
221 } 222 }
222 223
223 bool V4UpdateProtocolManager::ParseUpdateResponse( 224 bool V4UpdateProtocolManager::ParseUpdateResponse(
224 const std::string& data, 225 const std::string& data,
225 std::vector<ListUpdateResponse>* list_update_responses) { 226 ParsedServerResponse* parsed_server_response) {
226 FetchThreatListUpdatesResponse response; 227 FetchThreatListUpdatesResponse response;
227 228
228 if (!response.ParseFromString(data)) { 229 if (!response.ParseFromString(data)) {
229 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR); 230 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR);
230 return false; 231 return false;
231 } 232 }
232 233
233 if (response.has_minimum_wait_duration()) { 234 if (response.has_minimum_wait_duration()) {
234 // Seconds resolution is good enough so we ignore the nanos field. 235 // Seconds resolution is good enough so we ignore the nanos field.
235 int64_t minimum_wait_duration_seconds = 236 int64_t minimum_wait_duration_seconds =
236 response.minimum_wait_duration().seconds(); 237 response.minimum_wait_duration().seconds();
237 238
238 // Do not let the next_update_interval_ to be too low. 239 // Do not let the next_update_interval_ to be too low.
239 if (minimum_wait_duration_seconds < kV4TimerStartIntervalSecMin) { 240 if (minimum_wait_duration_seconds < kV4TimerStartIntervalSecMin) {
240 minimum_wait_duration_seconds = kV4TimerStartIntervalSecMin; 241 minimum_wait_duration_seconds = kV4TimerStartIntervalSecMin;
241 } 242 }
242 next_update_interval_ = 243 next_update_interval_ =
243 base::TimeDelta::FromSeconds(minimum_wait_duration_seconds); 244 base::TimeDelta::FromSeconds(minimum_wait_duration_seconds);
244 } 245 }
245 246
246 // TODO(vakh): Do something useful with this response. 247 // TODO(vakh): Do something useful with this response.
247 for (const ListUpdateResponse& list_update_response : 248 for (ListUpdateResponse list_update_response :
Nathan Parker 2016/06/28 19:00:38 I think this should be a reference, so you can swa
vakh (use Gerrit instead) 2016/06/28 21:34:14 Done.
248 response.list_update_responses()) { 249 *response.mutable_list_update_responses()) {
249 if (!list_update_response.has_platform_type()) { 250 if (!list_update_response.has_platform_type()) {
250 RecordParseUpdateResult(NO_PLATFORM_TYPE_ERROR); 251 RecordParseUpdateResult(NO_PLATFORM_TYPE_ERROR);
251 } else if (!list_update_response.has_threat_entry_type()) { 252 } else if (!list_update_response.has_threat_entry_type()) {
252 RecordParseUpdateResult(NO_THREAT_ENTRY_TYPE_ERROR); 253 RecordParseUpdateResult(NO_THREAT_ENTRY_TYPE_ERROR);
253 } else if (!list_update_response.has_threat_type()) { 254 } else if (!list_update_response.has_threat_type()) {
254 RecordParseUpdateResult(NO_THREAT_TYPE_ERROR); 255 RecordParseUpdateResult(NO_THREAT_TYPE_ERROR);
255 } else if (!list_update_response.has_new_client_state()) { 256 } else if (!list_update_response.has_new_client_state()) {
256 RecordParseUpdateResult(NO_STATE_ERROR); 257 RecordParseUpdateResult(NO_STATE_ERROR);
257 } else { 258 } else {
258 list_update_responses->push_back(list_update_response); 259 ListUpdateResponse* add = new ListUpdateResponse(list_update_response);
Nathan Parker 2016/06/28 19:00:38 Rather than copying, you could swap: ListUpdateRe
vakh (use Gerrit instead) 2016/06/28 21:34:14 Done.
260 parsed_server_response->push_back(base::WrapUnique(add));
259 } 261 }
260 } 262 }
261 return true; 263 return true;
262 } 264 }
263 265
264 void V4UpdateProtocolManager::IssueUpdateRequest() { 266 void V4UpdateProtocolManager::IssueUpdateRequest() {
265 DCHECK(CalledOnValidThread()); 267 DCHECK(CalledOnValidThread());
266 268
267 // If an update request is already pending, record and return silently. 269 // If an update request is already pending, record and return silently.
268 if (request_.get()) { 270 if (request_.get()) {
(...skipping 26 matching lines...) Expand all
295 const net::URLFetcher* source) { 297 const net::URLFetcher* source) {
296 DCHECK(CalledOnValidThread()); 298 DCHECK(CalledOnValidThread());
297 299
298 int response_code = source->GetResponseCode(); 300 int response_code = source->GetResponseCode();
299 net::URLRequestStatus status = source->GetStatus(); 301 net::URLRequestStatus status = source->GetStatus();
300 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode( 302 V4ProtocolManagerUtil::RecordHttpResponseOrErrorCode(
301 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code); 303 "SafeBrowsing.V4UpdateHttpResponseOrErrorCode", status, response_code);
302 304
303 last_response_time_ = Time::Now(); 305 last_response_time_ = Time::Now();
304 306
305 std::vector<ListUpdateResponse> list_update_responses; 307 std::unique_ptr<ParsedServerResponse> parsed_server_response(
308 new ParsedServerResponse);
306 if (status.is_success() && response_code == net::HTTP_OK) { 309 if (status.is_success() && response_code == net::HTTP_OK) {
307 RecordUpdateResult(V4OperationResult::STATUS_200); 310 RecordUpdateResult(V4OperationResult::STATUS_200);
308 ResetUpdateErrors(); 311 ResetUpdateErrors();
309 std::string data; 312 std::string data;
310 source->GetResponseAsString(&data); 313 source->GetResponseAsString(&data);
311 if (!ParseUpdateResponse(data, &list_update_responses)) { 314 if (!ParseUpdateResponse(data, parsed_server_response.get())) {
312 list_update_responses.clear(); 315 parsed_server_response->clear();
313 RecordUpdateResult(V4OperationResult::PARSE_ERROR); 316 RecordUpdateResult(V4OperationResult::PARSE_ERROR);
314 } 317 }
315 request_.reset(); 318 request_.reset();
316 319
317 UMA_HISTOGRAM_COUNTS("SafeBrowsing.V4UpdateResponseSizeKB", 320 UMA_HISTOGRAM_COUNTS("SafeBrowsing.V4UpdateResponseSizeKB",
318 data.size() / 1024); 321 data.size() / 1024);
319 322
320 // Invoke the callback with list_update_responses. 323 // The caller should update its state now, based on parsed_server_response.
321 // The caller should update its state now, based on list_update_responses.
322 // The callback must call ScheduleNextUpdate() at the end to resume 324 // The callback must call ScheduleNextUpdate() at the end to resume
323 // downloading updates. 325 // downloading updates.
324 update_callback_.Run(list_update_responses); 326 update_callback_.Run(std::move(parsed_server_response));
325 } else { 327 } else {
326 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: " 328 DVLOG(1) << "SafeBrowsing GetEncodedUpdates request for: "
327 << source->GetURL() << " failed with error: " << status.error() 329 << source->GetURL() << " failed with error: " << status.error()
328 << " and response code: " << response_code; 330 << " and response code: " << response_code;
329 331
330 if (status.status() == net::URLRequestStatus::FAILED) { 332 if (status.status() == net::URLRequestStatus::FAILED) {
331 RecordUpdateResult(V4OperationResult::NETWORK_ERROR); 333 RecordUpdateResult(V4OperationResult::NETWORK_ERROR);
332 } else { 334 } else {
333 RecordUpdateResult(V4OperationResult::HTTP_ERROR); 335 RecordUpdateResult(V4OperationResult::HTTP_ERROR);
334 } 336 }
335 // TODO(vakh): Figure out whether it is just a network error vs backoff vs 337 // TODO(vakh): Figure out whether it is just a network error vs backoff vs
336 // another condition and RecordUpdateResult more accurately. 338 // another condition and RecordUpdateResult more accurately.
337 339
338 request_.reset(); 340 request_.reset();
339 ScheduleNextUpdateWithBackoff(true); 341 ScheduleNextUpdateWithBackoff(true);
340 } 342 }
341 } 343 }
342 344
343 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( 345 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders(
344 const std::string& req_base64, 346 const std::string& req_base64,
345 GURL* gurl, 347 GURL* gurl,
346 net::HttpRequestHeaders* headers) const { 348 net::HttpRequestHeaders* headers) const {
347 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( 349 V4ProtocolManagerUtil::GetRequestUrlAndHeaders(
348 req_base64, "threatListUpdates:fetch", config_, gurl, headers); 350 req_base64, "threatListUpdates:fetch", config_, gurl, headers);
349 } 351 }
350 352
351 } // namespace safe_browsing 353 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698