1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/safe_browsing/protocol_manager.h"
7 #include "base/environment.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/metrics/histogram.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/rand_util.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/timer/timer.h"
17 #include "chrome/browser/safe_browsing/protocol_parser.h"
18 #include "chrome/common/env_vars.h"
19 #include "google_apis/google_api_keys.h"
20 #include "net/base/escape.h"
21 #include "net/base/load_flags.h"
22 #include "net/base/net_errors.h"
23 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "net/url_request/url_request_status.h"
27 #if defined(OS_ANDROID)
28 #include "net/base/network_change_notifier.h"
32 using base::TimeDelta
;
36 // UpdateResult indicates what happened with the primary and/or backup update
37 // requests. The ordering of the values must stay the same for UMA consistency,
38 // and is also ordered in this way to match ProtocolManager::BackupUpdateReason.
41 UPDATE_RESULT_SUCCESS
,
42 UPDATE_RESULT_BACKUP_CONNECT_FAIL
,
43 UPDATE_RESULT_BACKUP_CONNECT_SUCCESS
,
44 UPDATE_RESULT_BACKUP_HTTP_FAIL
,
45 UPDATE_RESULT_BACKUP_HTTP_SUCCESS
,
46 UPDATE_RESULT_BACKUP_NETWORK_FAIL
,
47 UPDATE_RESULT_BACKUP_NETWORK_SUCCESS
,
49 UPDATE_RESULT_BACKUP_START
= UPDATE_RESULT_BACKUP_CONNECT_FAIL
,
52 void RecordUpdateResult(UpdateResult result
) {
53 DCHECK(result
>= 0 && result
< UPDATE_RESULT_MAX
);
54 UMA_HISTOGRAM_ENUMERATION("SB2.UpdateResult", result
, UPDATE_RESULT_MAX
);
59 // Minimum time, in seconds, from start up before we must issue an update query.
60 static const int kSbTimerStartIntervalSecMin
= 60;
62 // Maximum time, in seconds, from start up before we must issue an update query.
63 static const int kSbTimerStartIntervalSecMax
= 300;
65 // The maximum time, in seconds, to wait for a response to an update request.
66 static const int kSbMaxUpdateWaitSec
= 30;
68 // Maximum back off multiplier.
69 static const size_t kSbMaxBackOff
= 8;
71 // The default SBProtocolManagerFactory.
72 class SBProtocolManagerFactoryImpl
: public SBProtocolManagerFactory
{
74 SBProtocolManagerFactoryImpl() { }
75 ~SBProtocolManagerFactoryImpl() override
{}
76 SafeBrowsingProtocolManager
* CreateProtocolManager(
77 SafeBrowsingProtocolManagerDelegate
* delegate
,
78 net::URLRequestContextGetter
* request_context_getter
,
79 const SafeBrowsingProtocolConfig
& config
) override
{
80 return new SafeBrowsingProtocolManager(
81 delegate
, request_context_getter
, config
);
84 DISALLOW_COPY_AND_ASSIGN(SBProtocolManagerFactoryImpl
);
87 // SafeBrowsingProtocolManager implementation ----------------------------------
90 SBProtocolManagerFactory
* SafeBrowsingProtocolManager::factory_
= NULL
;
93 SafeBrowsingProtocolManager
* SafeBrowsingProtocolManager::Create(
94 SafeBrowsingProtocolManagerDelegate
* delegate
,
95 net::URLRequestContextGetter
* request_context_getter
,
96 const SafeBrowsingProtocolConfig
& config
) {
97 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/483689 is fixed.
98 tracked_objects::ScopedTracker
tracking_profile(
99 FROM_HERE_WITH_EXPLICIT_FUNCTION(
100 "483689 SafeBrowsingProtocolManager::Create"));
102 factory_
= new SBProtocolManagerFactoryImpl();
103 return factory_
->CreateProtocolManager(
104 delegate
, request_context_getter
, config
);
107 SafeBrowsingProtocolManager::SafeBrowsingProtocolManager(
108 SafeBrowsingProtocolManagerDelegate
* delegate
,
109 net::URLRequestContextGetter
* request_context_getter
,
110 const SafeBrowsingProtocolConfig
& config
)
111 : delegate_(delegate
),
112 request_type_(NO_REQUEST
),
113 update_error_count_(0),
114 gethash_error_count_(0),
115 update_back_off_mult_(1),
116 gethash_back_off_mult_(1),
117 next_update_interval_(base::TimeDelta::FromSeconds(
118 base::RandInt(kSbTimerStartIntervalSecMin
,
119 kSbTimerStartIntervalSecMax
))),
120 update_state_(FIRST_REQUEST
),
121 chunk_pending_to_write_(false),
122 version_(config
.version
),
124 client_name_(config
.client_name
),
125 request_context_getter_(request_context_getter
),
126 url_prefix_(config
.url_prefix
),
127 backup_update_reason_(BACKUP_UPDATE_REASON_MAX
),
128 disable_auto_update_(config
.disable_auto_update
),
129 #if defined(OS_ANDROID)
130 disable_connection_check_(config
.disable_connection_check
),
133 app_in_foreground_(true) {
134 DCHECK(!url_prefix_
.empty());
136 backup_url_prefixes_
[BACKUP_UPDATE_REASON_CONNECT
] =
137 config
.backup_connect_error_url_prefix
;
138 backup_url_prefixes_
[BACKUP_UPDATE_REASON_HTTP
] =
139 config
.backup_http_error_url_prefix
;
140 backup_url_prefixes_
[BACKUP_UPDATE_REASON_NETWORK
] =
141 config
.backup_network_error_url_prefix
;
143 // Set the backoff multiplier fuzz to a random value between 0 and 1.
144 back_off_fuzz_
= static_cast<float>(base::RandDouble());
145 if (version_
.empty())
146 version_
= SafeBrowsingProtocolManagerHelper::Version();
150 void SafeBrowsingProtocolManager::RecordGetHashResult(
151 bool is_download
, ResultType result_type
) {
153 UMA_HISTOGRAM_ENUMERATION("SB2.GetHashResultDownload", result_type
,
154 GET_HASH_RESULT_MAX
);
156 UMA_HISTOGRAM_ENUMERATION("SB2.GetHashResult", result_type
,
157 GET_HASH_RESULT_MAX
);
161 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const {
162 return update_timer_
.IsRunning();
165 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() {
166 // Delete in-progress SafeBrowsing requests.
167 STLDeleteContainerPairFirstPointers(hash_requests_
.begin(),
168 hash_requests_
.end());
169 hash_requests_
.clear();
172 // We can only have one update or chunk request outstanding, but there may be
173 // multiple GetHash requests pending since we don't want to serialize them and
174 // slow down the user.
175 void SafeBrowsingProtocolManager::GetFullHash(
176 const std::vector
<SBPrefix
>& prefixes
,
177 FullHashCallback callback
,
179 bool is_extended_reporting
) {
180 DCHECK(CalledOnValidThread());
181 // If we are in GetHash backoff, we need to check if we're past the next
182 // allowed time. If we are, we can proceed with the request. If not, we are
183 // required to return empty results (i.e. treat the page as safe).
184 if (gethash_error_count_
&& Time::Now() <= next_gethash_time_
) {
185 RecordGetHashResult(is_download
, GET_HASH_BACKOFF_ERROR
);
186 std::vector
<SBFullHashResult
> full_hashes
;
187 callback
.Run(full_hashes
, base::TimeDelta());
190 GURL gethash_url
= GetHashUrl(is_extended_reporting
);
191 net::URLFetcher
* fetcher
=
192 net::URLFetcher::Create(url_fetcher_id_
++, gethash_url
,
193 net::URLFetcher::POST
, this).release();
194 hash_requests_
[fetcher
] = FullHashDetails(callback
, is_download
);
196 const std::string get_hash
= safe_browsing::FormatGetHash(prefixes
);
198 fetcher
->SetLoadFlags(net::LOAD_DISABLE_CACHE
);
199 fetcher
->SetRequestContext(request_context_getter_
.get());
200 fetcher
->SetUploadData("text/plain", get_hash
);
204 void SafeBrowsingProtocolManager::GetNextUpdate() {
205 DCHECK(CalledOnValidThread());
206 if (request_
.get() || request_type_
!= NO_REQUEST
)
209 #if defined(OS_ANDROID)
210 if (!disable_connection_check_
) {
211 net::NetworkChangeNotifier::ConnectionType type
=
212 net::NetworkChangeNotifier::GetConnectionType();
213 if (type
!= net::NetworkChangeNotifier::CONNECTION_WIFI
) {
214 ScheduleNextUpdate(false /* no back off */);
220 IssueUpdateRequest();
223 // net::URLFetcherDelegate implementation ----------------------------------
225 // All SafeBrowsing request responses are handled here.
226 // TODO(paulg): Clarify with the SafeBrowsing team whether a failed parse of a
227 // chunk should retry the download and parse of that chunk (and
228 // what back off / how many times to try), and if that effects the
229 // update back off. For now, a failed parse of the chunk means we
230 // drop it. This isn't so bad because the next UPDATE_REQUEST we
231 // do will report all the chunks we have. If that chunk is still
232 // required, the SafeBrowsing servers will tell us to get it again.
233 void SafeBrowsingProtocolManager::OnURLFetchComplete(
234 const net::URLFetcher
* source
) {
235 DCHECK(CalledOnValidThread());
236 scoped_ptr
<const net::URLFetcher
> fetcher
;
238 HashRequests::iterator it
= hash_requests_
.find(source
);
239 if (it
!= hash_requests_
.end()) {
241 fetcher
.reset(it
->first
);
242 const FullHashDetails
& details
= it
->second
;
243 std::vector
<SBFullHashResult
> full_hashes
;
244 base::TimeDelta cache_lifetime
;
245 if (source
->GetStatus().is_success() &&
246 (source
->GetResponseCode() == 200 ||
247 source
->GetResponseCode() == 204)) {
248 // For tracking our GetHash false positive (204) rate, compared to real
250 if (source
->GetResponseCode() == 200)
251 RecordGetHashResult(details
.is_download
, GET_HASH_STATUS_200
);
253 RecordGetHashResult(details
.is_download
, GET_HASH_STATUS_204
);
255 gethash_error_count_
= 0;
256 gethash_back_off_mult_
= 1;
258 source
->GetResponseAsString(&data
);
259 if (!safe_browsing::ParseGetHash(
260 data
.data(), data
.length(), &cache_lifetime
, &full_hashes
)) {
262 RecordGetHashResult(details
.is_download
, GET_HASH_PARSE_ERROR
);
263 // TODO(cbentzel): Should cache_lifetime be set to 0 here? (See
264 // http://crbug.com/360232.)
267 HandleGetHashError(Time::Now());
268 if (source
->GetStatus().status() == net::URLRequestStatus::FAILED
) {
269 RecordGetHashResult(details
.is_download
, GET_HASH_NETWORK_ERROR
);
270 DVLOG(1) << "SafeBrowsing GetHash request for: " << source
->GetURL()
271 << " failed with error: " << source
->GetStatus().error();
273 RecordGetHashResult(details
.is_download
, GET_HASH_HTTP_ERROR
);
274 DVLOG(1) << "SafeBrowsing GetHash request for: " << source
->GetURL()
275 << " failed with error: " << source
->GetResponseCode();
279 // Invoke the callback with full_hashes, even if there was a parse error or
280 // an error response code (in which case full_hashes will be empty). The
281 // caller can't be blocked indefinitely.
282 details
.callback
.Run(full_hashes
, cache_lifetime
);
284 hash_requests_
.erase(it
);
286 // Update or chunk response.
287 fetcher
.reset(request_
.release());
289 if (request_type_
== UPDATE_REQUEST
||
290 request_type_
== BACKUP_UPDATE_REQUEST
) {
291 if (!fetcher
.get()) {
292 // We've timed out waiting for an update response, so we've cancelled
293 // the update request and scheduled a new one. Ignore this response.
297 // Cancel the update response timeout now that we have the response.
298 timeout_timer_
.Stop();
301 net::URLRequestStatus status
= source
->GetStatus();
302 if (status
.is_success() && source
->GetResponseCode() == 200) {
303 // We have data from the SafeBrowsing service.
305 source
->GetResponseAsString(&data
);
307 // TODO(shess): Cleanup the flow of this code so that |parsed_ok| can be
308 // removed or omitted.
309 const bool parsed_ok
= HandleServiceResponse(
310 source
->GetURL(), data
.data(), data
.length());
312 DVLOG(1) << "SafeBrowsing request for: " << source
->GetURL()
314 chunk_request_urls_
.clear();
315 if (request_type_
== UPDATE_REQUEST
&&
316 IssueBackupUpdateRequest(BACKUP_UPDATE_REASON_HTTP
)) {
319 UpdateFinished(false);
322 switch (request_type_
) {
325 chunk_request_urls_
.pop_front();
326 if (chunk_request_urls_
.empty() && !chunk_pending_to_write_
)
327 UpdateFinished(true);
331 case BACKUP_UPDATE_REQUEST
:
332 if (chunk_request_urls_
.empty() && parsed_ok
) {
333 // We are up to date since the servers gave us nothing new, so we
334 // are done with this update cycle.
335 UpdateFinished(true);
339 // This can happen if HandleServiceResponse fails above.
346 if (status
.status() == net::URLRequestStatus::FAILED
) {
347 DVLOG(1) << "SafeBrowsing request for: " << source
->GetURL()
348 << " failed with error: " << source
->GetStatus().error();
350 DVLOG(1) << "SafeBrowsing request for: " << source
->GetURL()
351 << " failed with error: " << source
->GetResponseCode();
353 if (request_type_
== CHUNK_REQUEST
) {
354 // The SafeBrowsing service error, or very bad response code: back off.
355 chunk_request_urls_
.clear();
356 } else if (request_type_
== UPDATE_REQUEST
) {
357 BackupUpdateReason backup_update_reason
= BACKUP_UPDATE_REASON_MAX
;
358 if (status
.is_success()) {
359 backup_update_reason
= BACKUP_UPDATE_REASON_HTTP
;
361 switch (status
.error()) {
362 case net::ERR_INTERNET_DISCONNECTED
:
363 case net::ERR_NETWORK_CHANGED
:
364 backup_update_reason
= BACKUP_UPDATE_REASON_NETWORK
;
367 backup_update_reason
= BACKUP_UPDATE_REASON_CONNECT
;
371 if (backup_update_reason
!= BACKUP_UPDATE_REASON_MAX
&&
372 IssueBackupUpdateRequest(backup_update_reason
)) {
376 UpdateFinished(false);
380 // Get the next chunk if available.
384 bool SafeBrowsingProtocolManager::HandleServiceResponse(
385 const GURL
& url
, const char* data
, size_t length
) {
386 DCHECK(CalledOnValidThread());
388 switch (request_type_
) {
390 case BACKUP_UPDATE_REQUEST
: {
391 size_t next_update_sec
= 0;
393 scoped_ptr
<std::vector
<SBChunkDelete
> > chunk_deletes(
394 new std::vector
<SBChunkDelete
>);
395 std::vector
<ChunkUrl
> chunk_urls
;
396 if (!safe_browsing::ParseUpdate(data
, length
, &next_update_sec
, &reset
,
397 chunk_deletes
.get(), &chunk_urls
)) {
401 base::TimeDelta next_update_interval
=
402 base::TimeDelta::FromSeconds(next_update_sec
);
403 last_update_
= Time::Now();
405 if (update_state_
== FIRST_REQUEST
)
406 update_state_
= SECOND_REQUEST
;
407 else if (update_state_
== SECOND_REQUEST
)
408 update_state_
= NORMAL_REQUEST
;
410 // New time for the next update.
411 if (next_update_interval
> base::TimeDelta()) {
412 next_update_interval_
= next_update_interval
;
413 } else if (update_state_
== SECOND_REQUEST
) {
414 next_update_interval_
= base::TimeDelta::FromSeconds(
415 base::RandInt(15, 45));
418 // New chunks to download.
419 if (!chunk_urls
.empty()) {
420 UMA_HISTOGRAM_COUNTS("SB2.UpdateUrls", chunk_urls
.size());
421 for (size_t i
= 0; i
< chunk_urls
.size(); ++i
)
422 chunk_request_urls_
.push_back(chunk_urls
[i
]);
425 // Handle the case were the SafeBrowsing service tells us to dump our
428 delegate_
->ResetDatabase();
432 // Chunks to delete from our storage.
433 if (!chunk_deletes
->empty())
434 delegate_
->DeleteChunks(chunk_deletes
.Pass());
438 case CHUNK_REQUEST
: {
439 UMA_HISTOGRAM_TIMES("SB2.ChunkRequest",
440 base::Time::Now() - chunk_request_start_
);
442 const ChunkUrl chunk_url
= chunk_request_urls_
.front();
443 scoped_ptr
<ScopedVector
<SBChunkData
> >
444 chunks(new ScopedVector
<SBChunkData
>);
445 UMA_HISTOGRAM_COUNTS("SB2.ChunkSize", length
);
446 update_size_
+= length
;
447 if (!safe_browsing::ParseChunk(data
, length
, chunks
.get()))
450 // Chunks to add to storage. Pass ownership of |chunks|.
451 if (!chunks
->empty()) {
452 chunk_pending_to_write_
= true;
453 delegate_
->AddChunks(
454 chunk_url
.list_name
, chunks
.Pass(),
455 base::Bind(&SafeBrowsingProtocolManager::OnAddChunksComplete
,
456 base::Unretained(this)));
469 void SafeBrowsingProtocolManager::Initialize() {
470 DCHECK(CalledOnValidThread());
471 // TODO(cbentzel): Remove ScopedTracker below once crbug.com/483689 is fixed.
472 tracked_objects::ScopedTracker
tracking_profile(
473 FROM_HERE_WITH_EXPLICIT_FUNCTION(
474 "483689 SafeBrowsingProtocolManager::Initialize"));
475 // Don't want to hit the safe browsing servers on build/chrome bots.
476 scoped_ptr
<base::Environment
> env(base::Environment::Create());
477 if (env
->HasVar(env_vars::kHeadless
))
479 ScheduleNextUpdate(false /* no back off */);
482 void SafeBrowsingProtocolManager::ScheduleNextUpdate(bool back_off
) {
483 DCHECK(CalledOnValidThread());
484 if (disable_auto_update_
) {
485 // Unschedule any current timer.
486 update_timer_
.Stop();
489 // Reschedule with the new update.
490 base::TimeDelta next_update_interval
= GetNextUpdateInterval(back_off
);
491 ForceScheduleNextUpdate(next_update_interval
);
494 void SafeBrowsingProtocolManager::ForceScheduleNextUpdate(
495 base::TimeDelta interval
) {
496 DCHECK(CalledOnValidThread());
497 DCHECK(interval
>= base::TimeDelta());
498 // Unschedule any current timer.
499 update_timer_
.Stop();
500 update_timer_
.Start(FROM_HERE
, interval
, this,
501 &SafeBrowsingProtocolManager::GetNextUpdate
);
504 // According to section 5 of the SafeBrowsing protocol specification, we must
505 // back off after a certain number of errors. We only change |next_update_sec_|
506 // when we receive a response from the SafeBrowsing service.
507 base::TimeDelta
SafeBrowsingProtocolManager::GetNextUpdateInterval(
509 DCHECK(CalledOnValidThread());
510 DCHECK(next_update_interval_
> base::TimeDelta());
511 base::TimeDelta next
= next_update_interval_
;
513 next
= GetNextBackOffInterval(&update_error_count_
, &update_back_off_mult_
);
515 // Successful response means error reset.
516 update_error_count_
= 0;
517 update_back_off_mult_
= 1;
522 base::TimeDelta
SafeBrowsingProtocolManager::GetNextBackOffInterval(
523 size_t* error_count
, size_t* multiplier
) const {
524 DCHECK(CalledOnValidThread());
525 DCHECK(multiplier
&& error_count
);
527 if (*error_count
> 1 && *error_count
< 6) {
528 base::TimeDelta next
= base::TimeDelta::FromMinutes(
529 *multiplier
* (1 + back_off_fuzz_
) * 30);
531 if (*multiplier
> kSbMaxBackOff
)
532 *multiplier
= kSbMaxBackOff
;
535 if (*error_count
>= 6)
536 return base::TimeDelta::FromHours(8);
537 return base::TimeDelta::FromMinutes(1);
540 // This request requires getting a list of all the chunks for each list from the
541 // database asynchronously. The request will be issued when we're called back in
542 // OnGetChunksComplete.
543 // TODO(paulg): We should get this at start up and maintain a ChunkRange cache
544 // to avoid hitting the database with each update request. On the
545 // otherhand, this request will only occur ~20-30 minutes so there
546 // isn't that much overhead. Measure!
547 void SafeBrowsingProtocolManager::IssueUpdateRequest() {
548 DCHECK(CalledOnValidThread());
549 request_type_
= UPDATE_REQUEST
;
550 delegate_
->UpdateStarted();
551 delegate_
->GetChunks(
552 base::Bind(&SafeBrowsingProtocolManager::OnGetChunksComplete
,
553 base::Unretained(this)));
556 // The backup request can run immediately since the chunks have already been
557 // retrieved from the DB.
558 bool SafeBrowsingProtocolManager::IssueBackupUpdateRequest(
559 BackupUpdateReason backup_update_reason
) {
560 DCHECK(CalledOnValidThread());
561 DCHECK_EQ(request_type_
, UPDATE_REQUEST
);
562 DCHECK(backup_update_reason
>= 0 &&
563 backup_update_reason
< BACKUP_UPDATE_REASON_MAX
);
564 if (backup_url_prefixes_
[backup_update_reason
].empty())
566 request_type_
= BACKUP_UPDATE_REQUEST
;
567 backup_update_reason_
= backup_update_reason
;
569 GURL backup_update_url
= BackupUpdateUrl(backup_update_reason
);
570 request_
= net::URLFetcher::Create(url_fetcher_id_
++, backup_update_url
,
571 net::URLFetcher::POST
, this);
572 request_
->SetLoadFlags(net::LOAD_DISABLE_CACHE
);
573 request_
->SetRequestContext(request_context_getter_
.get());
574 request_
->SetUploadData("text/plain", update_list_data_
);
577 // Begin the update request timeout.
578 timeout_timer_
.Start(FROM_HERE
, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec
),
580 &SafeBrowsingProtocolManager::UpdateResponseTimeout
);
585 void SafeBrowsingProtocolManager::IssueChunkRequest() {
586 DCHECK(CalledOnValidThread());
587 // We are only allowed to have one request outstanding at any time. Also,
588 // don't get the next url until the previous one has been written to disk so
589 // that we don't use too much memory.
590 if (request_
.get() || chunk_request_urls_
.empty() || chunk_pending_to_write_
)
593 ChunkUrl next_chunk
= chunk_request_urls_
.front();
594 DCHECK(!next_chunk
.url
.empty());
595 GURL chunk_url
= NextChunkUrl(next_chunk
.url
);
596 request_type_
= CHUNK_REQUEST
;
597 request_
= net::URLFetcher::Create(url_fetcher_id_
++, chunk_url
,
598 net::URLFetcher::GET
, this);
599 request_
->SetLoadFlags(net::LOAD_DISABLE_CACHE
);
600 request_
->SetRequestContext(request_context_getter_
.get());
601 chunk_request_start_
= base::Time::Now();
605 void SafeBrowsingProtocolManager::OnGetChunksComplete(
606 const std::vector
<SBListChunkRanges
>& lists
,
608 bool is_extended_reporting
) {
609 DCHECK(CalledOnValidThread());
610 DCHECK_EQ(request_type_
, UPDATE_REQUEST
);
611 DCHECK(update_list_data_
.empty());
612 if (database_error
) {
613 // The update was not successful, but don't back off.
614 UpdateFinished(false, false);
618 // Format our stored chunks:
619 bool found_malware
= false;
620 bool found_phishing
= false;
621 for (size_t i
= 0; i
< lists
.size(); ++i
) {
622 update_list_data_
.append(safe_browsing::FormatList(lists
[i
]));
623 if (lists
[i
].name
== safe_browsing_util::kPhishingList
)
624 found_phishing
= true;
626 if (lists
[i
].name
== safe_browsing_util::kMalwareList
)
627 found_malware
= true;
630 // If we have an empty database, let the server know we want data for these
632 // TODO(shess): These cases never happen because the database fills in the
633 // lists in GetChunks(). Refactor the unit tests so that this code can be
635 if (!found_phishing
) {
636 update_list_data_
.append(safe_browsing::FormatList(
637 SBListChunkRanges(safe_browsing_util::kPhishingList
)));
639 if (!found_malware
) {
640 update_list_data_
.append(safe_browsing::FormatList(
641 SBListChunkRanges(safe_browsing_util::kMalwareList
)));
644 // Large requests are (probably) a sign of database corruption.
645 // Record stats to inform decisions about whether to automate
646 // deletion of such databases. http://crbug.com/120219
647 UMA_HISTOGRAM_COUNTS("SB2.UpdateRequestSize", update_list_data_
.size());
649 GURL update_url
= UpdateUrl(is_extended_reporting
);
650 request_
= net::URLFetcher::Create(url_fetcher_id_
++, update_url
,
651 net::URLFetcher::POST
, this);
652 request_
->SetLoadFlags(net::LOAD_DISABLE_CACHE
);
653 request_
->SetRequestContext(request_context_getter_
.get());
654 request_
->SetUploadData("text/plain", update_list_data_
);
657 // Begin the update request timeout.
658 timeout_timer_
.Start(FROM_HERE
, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec
),
660 &SafeBrowsingProtocolManager::UpdateResponseTimeout
);
663 // If we haven't heard back from the server with an update response, this method
664 // will run. Close the current update session and schedule another update.
665 void SafeBrowsingProtocolManager::UpdateResponseTimeout() {
666 DCHECK(CalledOnValidThread());
667 DCHECK(request_type_
== UPDATE_REQUEST
||
668 request_type_
== BACKUP_UPDATE_REQUEST
);
670 if (request_type_
== UPDATE_REQUEST
&&
671 IssueBackupUpdateRequest(BACKUP_UPDATE_REASON_CONNECT
)) {
674 UpdateFinished(false);
677 void SafeBrowsingProtocolManager::OnAddChunksComplete() {
678 DCHECK(CalledOnValidThread());
679 chunk_pending_to_write_
= false;
681 if (chunk_request_urls_
.empty()) {
682 UMA_HISTOGRAM_LONG_TIMES("SB2.Update", Time::Now() - last_update_
);
683 UpdateFinished(true);
689 void SafeBrowsingProtocolManager::HandleGetHashError(const Time
& now
) {
690 DCHECK(CalledOnValidThread());
691 base::TimeDelta next
= GetNextBackOffInterval(
692 &gethash_error_count_
, &gethash_back_off_mult_
);
693 next_gethash_time_
= now
+ next
;
696 void SafeBrowsingProtocolManager::UpdateFinished(bool success
) {
697 UpdateFinished(success
, !success
);
700 void SafeBrowsingProtocolManager::UpdateFinished(bool success
, bool back_off
) {
701 DCHECK(CalledOnValidThread());
702 #if defined(OS_ANDROID)
703 if (app_in_foreground_
)
704 UMA_HISTOGRAM_COUNTS("SB2.UpdateSizeForeground", update_size_
);
706 UMA_HISTOGRAM_COUNTS("SB2.UpdateSizeBackground", update_size_
);
708 UMA_HISTOGRAM_COUNTS("SB2.UpdateSize", update_size_
);
710 bool update_success
= success
|| request_type_
== CHUNK_REQUEST
;
711 if (backup_update_reason_
== BACKUP_UPDATE_REASON_MAX
) {
713 update_success
? UPDATE_RESULT_SUCCESS
: UPDATE_RESULT_FAIL
);
715 UpdateResult update_result
= static_cast<UpdateResult
>(
716 UPDATE_RESULT_BACKUP_START
+
717 (static_cast<int>(backup_update_reason_
) * 2) +
719 RecordUpdateResult(update_result
);
721 backup_update_reason_
= BACKUP_UPDATE_REASON_MAX
;
722 request_type_
= NO_REQUEST
;
723 update_list_data_
.clear();
724 delegate_
->UpdateFinished(success
);
725 ScheduleNextUpdate(back_off
);
728 GURL
SafeBrowsingProtocolManager::UpdateUrl(bool is_extended_reporting
) const {
729 std::string url
= SafeBrowsingProtocolManagerHelper::ComposeUrl(
730 url_prefix_
, "downloads", client_name_
, version_
, additional_query_
,
731 is_extended_reporting
);
735 GURL
SafeBrowsingProtocolManager::BackupUpdateUrl(
736 BackupUpdateReason backup_update_reason
) const {
737 DCHECK(backup_update_reason
>= 0 &&
738 backup_update_reason
< BACKUP_UPDATE_REASON_MAX
);
739 DCHECK(!backup_url_prefixes_
[backup_update_reason
].empty());
740 std::string url
= SafeBrowsingProtocolManagerHelper::ComposeUrl(
741 backup_url_prefixes_
[backup_update_reason
], "downloads", client_name_
,
742 version_
, additional_query_
);
746 GURL
SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting
) const {
747 std::string url
= SafeBrowsingProtocolManagerHelper::ComposeUrl(
748 url_prefix_
, "gethash", client_name_
, version_
, additional_query_
,
749 is_extended_reporting
);
753 GURL
SafeBrowsingProtocolManager::NextChunkUrl(const std::string
& url
) const {
754 DCHECK(CalledOnValidThread());
755 std::string next_url
;
756 if (!base::StartsWith(url
, "http://",
757 base::CompareCase::INSENSITIVE_ASCII
) &&
758 !base::StartsWith(url
, "https://",
759 base::CompareCase::INSENSITIVE_ASCII
)) {
760 // Use https if we updated via https, otherwise http (useful for testing).
761 if (base::StartsWith(url_prefix_
, "https://",
762 base::CompareCase::INSENSITIVE_ASCII
))
763 next_url
.append("https://");
765 next_url
.append("http://");
766 next_url
.append(url
);
770 if (!additional_query_
.empty()) {
771 if (next_url
.find("?") != std::string::npos
) {
772 next_url
.append("&");
774 next_url
.append("?");
776 next_url
.append(additional_query_
);
778 return GURL(next_url
);
781 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails()
786 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails(
787 FullHashCallback callback
, bool is_download
)
788 : callback(callback
),
789 is_download(is_download
) {
792 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {
795 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {