| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/safe_browsing/protocol_manager.h" | 5 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 6 | 6 |
| 7 #include "base/file_version_info.h" | 7 #include "base/file_version_info.h" |
| 8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| 11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
| 14 #include "base/task.h" | 14 #include "base/task.h" |
| 15 #include "base/timer.h" | 15 #include "base/timer.h" |
| 16 #include "chrome/browser/chrome_thread.h" | 16 #include "chrome/browser/chrome_thread.h" |
| 17 #include "chrome/browser/net/url_request_context_getter.h" |
| 17 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| 18 #include "chrome/browser/safe_browsing/protocol_parser.h" | 19 #include "chrome/browser/safe_browsing/protocol_parser.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 20 #include "chrome/common/env_vars.h" | 21 #include "chrome/common/env_vars.h" |
| 21 #include "net/base/base64.h" | 22 #include "net/base/base64.h" |
| 22 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 23 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 24 | 25 |
| 25 using base::Time; | 26 using base::Time; |
| 26 using base::TimeDelta; | 27 using base::TimeDelta; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 54 // Maximum back off multiplier. | 55 // Maximum back off multiplier. |
| 55 static const int kSbMaxBackOff = 8; | 56 static const int kSbMaxBackOff = 8; |
| 56 | 57 |
| 57 | 58 |
| 58 // SafeBrowsingProtocolManager implementation ---------------------------------- | 59 // SafeBrowsingProtocolManager implementation ---------------------------------- |
| 59 | 60 |
| 60 SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( | 61 SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( |
| 61 SafeBrowsingService* sb_service, | 62 SafeBrowsingService* sb_service, |
| 62 const std::string& client_name, | 63 const std::string& client_name, |
| 63 const std::string& client_key, | 64 const std::string& client_key, |
| 64 const std::string& wrapped_key) | 65 const std::string& wrapped_key, |
| 66 URLRequestContextGetter* request_context_getter) |
| 65 : sb_service_(sb_service), | 67 : sb_service_(sb_service), |
| 66 request_type_(NO_REQUEST), | 68 request_type_(NO_REQUEST), |
| 67 update_error_count_(0), | 69 update_error_count_(0), |
| 68 gethash_error_count_(0), | 70 gethash_error_count_(0), |
| 69 update_back_off_mult_(1), | 71 update_back_off_mult_(1), |
| 70 gethash_back_off_mult_(1), | 72 gethash_back_off_mult_(1), |
| 71 next_update_sec_(-1), | 73 next_update_sec_(-1), |
| 72 update_state_(FIRST_REQUEST), | 74 update_state_(FIRST_REQUEST), |
| 73 initial_request_(true), | 75 initial_request_(true), |
| 74 chunk_pending_to_write_(false), | 76 chunk_pending_to_write_(false), |
| 75 client_key_(client_key), | 77 client_key_(client_key), |
| 76 wrapped_key_(wrapped_key), | 78 wrapped_key_(wrapped_key), |
| 77 update_size_(0), | 79 update_size_(0), |
| 78 client_name_(client_name) { | 80 client_name_(client_name), |
| 81 request_context_getter_(request_context_getter) { |
| 79 // Set the backoff multiplier fuzz to a random value between 0 and 1. | 82 // Set the backoff multiplier fuzz to a random value between 0 and 1. |
| 80 back_off_fuzz_ = static_cast<float>(base::RandDouble()); | 83 back_off_fuzz_ = static_cast<float>(base::RandDouble()); |
| 81 | 84 |
| 82 // The first update must happen between 0-5 minutes of start up. | 85 // The first update must happen between 0-5 minutes of start up. |
| 83 next_update_sec_ = base::RandInt(60, kSbTimerStartIntervalSec); | 86 next_update_sec_ = base::RandInt(60, kSbTimerStartIntervalSec); |
| 84 | 87 |
| 85 scoped_ptr<FileVersionInfo> version_info( | 88 scoped_ptr<FileVersionInfo> version_info( |
| 86 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); | 89 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 87 if (!version_info.get()) | 90 if (!version_info.get()) |
| 88 version_ = "0.1"; | 91 version_ = "0.1"; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 131 |
| 129 GURL gethash_url(url); | 132 GURL gethash_url(url); |
| 130 URLFetcher* fetcher = new URLFetcher(gethash_url, URLFetcher::POST, this); | 133 URLFetcher* fetcher = new URLFetcher(gethash_url, URLFetcher::POST, this); |
| 131 hash_requests_[fetcher] = check; | 134 hash_requests_[fetcher] = check; |
| 132 | 135 |
| 133 std::string get_hash; | 136 std::string get_hash; |
| 134 SafeBrowsingProtocolParser parser; | 137 SafeBrowsingProtocolParser parser; |
| 135 parser.FormatGetHash(prefixes, &get_hash); | 138 parser.FormatGetHash(prefixes, &get_hash); |
| 136 | 139 |
| 137 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE); | 140 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 138 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 141 fetcher->set_request_context(request_context_getter_); |
| 139 fetcher->set_upload_data("text/plain", get_hash); | 142 fetcher->set_upload_data("text/plain", get_hash); |
| 140 fetcher->Start(); | 143 fetcher->Start(); |
| 141 } | 144 } |
| 142 | 145 |
| 143 void SafeBrowsingProtocolManager::GetNextUpdate() { | 146 void SafeBrowsingProtocolManager::GetNextUpdate() { |
| 144 if (initial_request_) { | 147 if (initial_request_) { |
| 145 if (client_key_.empty() || wrapped_key_.empty()) { | 148 if (client_key_.empty() || wrapped_key_.empty()) { |
| 146 IssueKeyRequest(); | 149 IssueKeyRequest(); |
| 147 return; | 150 return; |
| 148 } else { | 151 } else { |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 507 |
| 505 ChunkUrl next_chunk = chunk_request_urls_.front(); | 508 ChunkUrl next_chunk = chunk_request_urls_.front(); |
| 506 DCHECK(!next_chunk.url.empty()); | 509 DCHECK(!next_chunk.url.empty()); |
| 507 if (!StartsWithASCII(next_chunk.url, "http://", false) && | 510 if (!StartsWithASCII(next_chunk.url, "http://", false) && |
| 508 !StartsWithASCII(next_chunk.url, "https://", false)) | 511 !StartsWithASCII(next_chunk.url, "https://", false)) |
| 509 next_chunk.url = "http://" + next_chunk.url; | 512 next_chunk.url = "http://" + next_chunk.url; |
| 510 GURL chunk_url(next_chunk.url); | 513 GURL chunk_url(next_chunk.url); |
| 511 request_type_ = CHUNK_REQUEST; | 514 request_type_ = CHUNK_REQUEST; |
| 512 request_.reset(new URLFetcher(chunk_url, URLFetcher::GET, this)); | 515 request_.reset(new URLFetcher(chunk_url, URLFetcher::GET, this)); |
| 513 request_->set_load_flags(net::LOAD_DISABLE_CACHE); | 516 request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 514 request_->set_request_context(Profile::GetDefaultRequestContext()); | 517 request_->set_request_context(request_context_getter_); |
| 515 chunk_request_start_ = base::Time::Now(); | 518 chunk_request_start_ = base::Time::Now(); |
| 516 request_->Start(); | 519 request_->Start(); |
| 517 } | 520 } |
| 518 | 521 |
| 519 void SafeBrowsingProtocolManager::IssueKeyRequest() { | 522 void SafeBrowsingProtocolManager::IssueKeyRequest() { |
| 520 GURL key_url(StringPrintf(kSbNewKeyUrl, | 523 GURL key_url(StringPrintf(kSbNewKeyUrl, |
| 521 client_name_.c_str(), | 524 client_name_.c_str(), |
| 522 version_.c_str())); | 525 version_.c_str())); |
| 523 request_type_ = GETKEY_REQUEST; | 526 request_type_ = GETKEY_REQUEST; |
| 524 request_.reset(new URLFetcher(key_url, URLFetcher::GET, this)); | 527 request_.reset(new URLFetcher(key_url, URLFetcher::GET, this)); |
| 525 request_->set_load_flags(net::LOAD_DISABLE_CACHE); | 528 request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 526 request_->set_request_context(Profile::GetDefaultRequestContext()); | 529 request_->set_request_context(request_context_getter_); |
| 527 request_->Start(); | 530 request_->Start(); |
| 528 } | 531 } |
| 529 | 532 |
| 530 void SafeBrowsingProtocolManager::OnGetChunksComplete( | 533 void SafeBrowsingProtocolManager::OnGetChunksComplete( |
| 531 const std::vector<SBListChunkRanges>& lists, bool database_error) { | 534 const std::vector<SBListChunkRanges>& lists, bool database_error) { |
| 532 DCHECK(request_type_ == UPDATE_REQUEST); | 535 DCHECK(request_type_ == UPDATE_REQUEST); |
| 533 | 536 |
| 534 if (database_error) { | 537 if (database_error) { |
| 535 ScheduleNextUpdate(false); | 538 ScheduleNextUpdate(false); |
| 536 UpdateFinished(false); | 539 UpdateFinished(false); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 566 client_name_.c_str(), | 569 client_name_.c_str(), |
| 567 version_.c_str()); | 570 version_.c_str()); |
| 568 if (use_mac) { | 571 if (use_mac) { |
| 569 url.append("&wrkey="); | 572 url.append("&wrkey="); |
| 570 url.append(wrapped_key_); | 573 url.append(wrapped_key_); |
| 571 } | 574 } |
| 572 | 575 |
| 573 GURL update_url(url); | 576 GURL update_url(url); |
| 574 request_.reset(new URLFetcher(update_url, URLFetcher::POST, this)); | 577 request_.reset(new URLFetcher(update_url, URLFetcher::POST, this)); |
| 575 request_->set_load_flags(net::LOAD_DISABLE_CACHE); | 578 request_->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 576 request_->set_request_context(Profile::GetDefaultRequestContext()); | 579 request_->set_request_context(request_context_getter_); |
| 577 request_->set_upload_data("text/plain", list_data); | 580 request_->set_upload_data("text/plain", list_data); |
| 578 request_->Start(); | 581 request_->Start(); |
| 579 | 582 |
| 580 // Begin the update request timeout. | 583 // Begin the update request timeout. |
| 581 update_timer_.Start(TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), this, | 584 update_timer_.Start(TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), this, |
| 582 &SafeBrowsingProtocolManager::UpdateResponseTimeout); | 585 &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| 583 } | 586 } |
| 584 | 587 |
| 585 // If we haven't heard back from the server with an update response, this method | 588 // If we haven't heard back from the server with an update response, this method |
| 586 // will run. Close the current update session and schedule another update. | 589 // will run. Close the current update session and schedule another update. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 608 std::string report_str = StringPrintf( | 611 std::string report_str = StringPrintf( |
| 609 kSbMalwareReportUrl, | 612 kSbMalwareReportUrl, |
| 610 EscapeQueryParamValue(malware_url.spec()).c_str(), | 613 EscapeQueryParamValue(malware_url.spec()).c_str(), |
| 611 EscapeQueryParamValue(page_url.spec()).c_str(), | 614 EscapeQueryParamValue(page_url.spec()).c_str(), |
| 612 EscapeQueryParamValue(referrer_url.spec()).c_str(), | 615 EscapeQueryParamValue(referrer_url.spec()).c_str(), |
| 613 client_name_.c_str(), | 616 client_name_.c_str(), |
| 614 version_.c_str()); | 617 version_.c_str()); |
| 615 GURL report_url(report_str); | 618 GURL report_url(report_str); |
| 616 URLFetcher* report = new URLFetcher(report_url, URLFetcher::GET, this); | 619 URLFetcher* report = new URLFetcher(report_url, URLFetcher::GET, this); |
| 617 report->set_load_flags(net::LOAD_DISABLE_CACHE); | 620 report->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 618 report->set_request_context(Profile::GetDefaultRequestContext()); | 621 report->set_request_context(request_context_getter_); |
| 619 report->Start(); | 622 report->Start(); |
| 620 malware_reports_.insert(report); | 623 malware_reports_.insert(report); |
| 621 } | 624 } |
| 622 | 625 |
| 623 // static | 626 // static |
| 624 std::string SafeBrowsingProtocolManager::FormatList( | 627 std::string SafeBrowsingProtocolManager::FormatList( |
| 625 const SBListChunkRanges& list, bool use_mac) { | 628 const SBListChunkRanges& list, bool use_mac) { |
| 626 std::string formatted_results; | 629 std::string formatted_results; |
| 627 formatted_results.append(list.name); | 630 formatted_results.append(list.name); |
| 628 formatted_results.append(";"); | 631 formatted_results.append(";"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 652 void SafeBrowsingProtocolManager::HandleGetHashError(const Time& now) { | 655 void SafeBrowsingProtocolManager::HandleGetHashError(const Time& now) { |
| 653 int next = GetNextBackOffTime(&gethash_error_count_, &gethash_back_off_mult_); | 656 int next = GetNextBackOffTime(&gethash_error_count_, &gethash_back_off_mult_); |
| 654 next_gethash_time_ = now + TimeDelta::FromSeconds(next); | 657 next_gethash_time_ = now + TimeDelta::FromSeconds(next); |
| 655 } | 658 } |
| 656 | 659 |
| 657 void SafeBrowsingProtocolManager::UpdateFinished(bool success) { | 660 void SafeBrowsingProtocolManager::UpdateFinished(bool success) { |
| 658 UMA_HISTOGRAM_COUNTS("SB2.UpdateSize", update_size_); | 661 UMA_HISTOGRAM_COUNTS("SB2.UpdateSize", update_size_); |
| 659 update_size_ = 0; | 662 update_size_ = 0; |
| 660 sb_service_->UpdateFinished(success); | 663 sb_service_->UpdateFinished(success); |
| 661 } | 664 } |
| OLD | NEW |