diff options
author | lzheng@chromium.org <lzheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 21:53:18 +0000 |
---|---|---|
committer | lzheng@chromium.org <lzheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-29 21:53:18 +0000 |
commit | 894c4e8a8146bfa89c66d4e4ab629d589fabd9ba (patch) | |
tree | c8bb18e20e6115b340cc9e62b3f9639626d58cac /chrome/browser/safe_browsing/protocol_manager.cc | |
parent | 878f43f91009570f2ddf3380236259635d26c82a (diff) | |
download | chromium_src-894c4e8a8146bfa89c66d4e4ab629d589fabd9ba.zip chromium_src-894c4e8a8146bfa89c66d4e4ab629d589fabd9ba.tar.gz chromium_src-894c4e8a8146bfa89c66d4e4ab629d589fabd9ba.tar.bz2 |
Add switches and apis in safebrowsing protocol_manager that will allow end-to-end test later.
BUG=6787,47318
TEST=protocol_manager_unittest.cc
Review URL: http://codereview.chromium.org/2868030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51177 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/safe_browsing/protocol_manager.cc')
-rw-r--r-- | chrome/browser/safe_browsing/protocol_manager.cc | 164 |
1 files changed, 98 insertions, 66 deletions
diff --git a/chrome/browser/safe_browsing/protocol_manager.cc b/chrome/browser/safe_browsing/protocol_manager.cc index 5a8baae..ff4338a 100644 --- a/chrome/browser/safe_browsing/protocol_manager.cc +++ b/chrome/browser/safe_browsing/protocol_manager.cc @@ -34,26 +34,6 @@ static const int kSbTimerStartIntervalSec = 5 * 60; // The maximum time, in seconds, to wait for a response to an update request. static const int kSbMaxUpdateWaitSec = 10; -// Update URL for querying about the latest set of chunk updates. -static const char* const kSbUpdateUrl = - "http://safebrowsing.clients.google.com/safebrowsing/downloads?client=%s" - "&appver=%s&pver=2.2"; - -// GetHash request URL for retrieving full hashes. -static const char* const kSbGetHashUrl = - "http://safebrowsing.clients.google.com/safebrowsing/gethash?client=%s" - "&appver=%s&pver=2.2"; - -// New MAC client key requests URL. -static const char* const kSbNewKeyUrl = - "https://sb-ssl.google.com/safebrowsing/newkey?client=%s&appver=%s" - "&pver=2.2"; - -// URL for reporting malware pages. -static const char* const kSbMalwareReportUrl = - "http://safebrowsing.clients.google.com/safebrowsing/report?evts=malblhit" - "&evtd=%s&evtr=%s&evhr=%s&client=%s&appver=%s"; - // Maximum back off multiplier. static const int kSbMaxBackOff = 8; @@ -65,7 +45,10 @@ SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( const std::string& client_name, const std::string& client_key, const std::string& wrapped_key, - URLRequestContextGetter* request_context_getter) + URLRequestContextGetter* request_context_getter, + const std::string& info_url_prefix, + const std::string& mackey_url_prefix, + bool disable_auto_update) : sb_service_(sb_service), request_type_(NO_REQUEST), update_error_count_(0), @@ -80,15 +63,18 @@ SafeBrowsingProtocolManager::SafeBrowsingProtocolManager( wrapped_key_(wrapped_key), update_size_(0), client_name_(client_name), - request_context_getter_(request_context_getter) { + request_context_getter_(request_context_getter), + info_url_prefix_(info_url_prefix), + mackey_url_prefix_(mackey_url_prefix), + disable_auto_update_(disable_auto_update) { + DCHECK(!info_url_prefix_.empty() && !mackey_url_prefix_.empty()); + // Set the backoff multiplier fuzz to a random value between 0 and 1. back_off_fuzz_ = static_cast<float>(base::RandDouble()); - // The first update must happen between 1-5 minutes of start up. next_update_sec_ = base::RandInt(60, kSbTimerStartIntervalSec); - scoped_ptr<FileVersionInfo> version_info( - chrome_app::GetChromeVersionInfo()); + scoped_ptr<FileVersionInfo> version_info(chrome_app::GetChromeVersionInfo()); if (!version_info.get()) version_ = "0.1"; else @@ -122,16 +108,8 @@ void SafeBrowsingProtocolManager::GetFullHash( sb_service_->HandleGetHashResults(check, full_hashes, false); return; } - - std::string url = StringPrintf(kSbGetHashUrl, - client_name_.c_str(), - version_.c_str()); - if (!client_key_.empty()) { - url.append("&wrkey="); - url.append(wrapped_key_); - } - - GURL gethash_url(url); + bool use_mac = !client_key_.empty(); + GURL gethash_url = GetHashUrl(use_mac); URLFetcher* fetcher = new URLFetcher(gethash_url, URLFetcher::POST, this); hash_requests_[fetcher] = check; @@ -448,14 +426,24 @@ void SafeBrowsingProtocolManager::Initialize() { } void SafeBrowsingProtocolManager::ScheduleNextUpdate(bool back_off) { - DCHECK(next_update_sec_ > 0); - - // Unschedule any current timer. - update_timer_.Stop(); + DCHECK_GT(next_update_sec_, 0); + if (disable_auto_update_) { + // Unschedule any current timer. + update_timer_.Stop(); + return; + } // Reschedule with the new update. const int next_update = GetNextUpdateTime(back_off); - update_timer_.Start(TimeDelta::FromMilliseconds(next_update), this, + ForceScheduleNextUpdate(next_update); +} + +void SafeBrowsingProtocolManager::ForceScheduleNextUpdate( + const int next_update_msec) { + DCHECK_GE(next_update_msec, 0); + // Unschedule any current timer. + update_timer_.Stop(); + update_timer_.Start(TimeDelta::FromMilliseconds(next_update_msec), this, &SafeBrowsingProtocolManager::GetNextUpdate); } @@ -513,10 +501,7 @@ void SafeBrowsingProtocolManager::IssueChunkRequest() { ChunkUrl next_chunk = chunk_request_urls_.front(); DCHECK(!next_chunk.url.empty()); - if (!StartsWithASCII(next_chunk.url, "http://", false) && - !StartsWithASCII(next_chunk.url, "https://", false)) - next_chunk.url = "http://" + next_chunk.url; - GURL chunk_url(next_chunk.url); + GURL chunk_url = NextChunkUrl(next_chunk.url); request_type_ = CHUNK_REQUEST; request_.reset(new URLFetcher(chunk_url, URLFetcher::GET, this)); request_->set_load_flags(net::LOAD_DISABLE_CACHE); @@ -526,9 +511,7 @@ void SafeBrowsingProtocolManager::IssueChunkRequest() { } void SafeBrowsingProtocolManager::IssueKeyRequest() { - GURL key_url(StringPrintf(kSbNewKeyUrl, - client_name_.c_str(), - version_.c_str())); + GURL key_url = MacKeyUrl(); request_type_ = GETKEY_REQUEST; request_.reset(new URLFetcher(key_url, URLFetcher::GET, this)); request_->set_load_flags(net::LOAD_DISABLE_CACHE); @@ -538,7 +521,7 @@ void SafeBrowsingProtocolManager::IssueKeyRequest() { void SafeBrowsingProtocolManager::OnGetChunksComplete( const std::vector<SBListChunkRanges>& lists, bool database_error) { - DCHECK(request_type_ == UPDATE_REQUEST); + DCHECK_EQ(request_type_, UPDATE_REQUEST); if (database_error) { UpdateFinished(false); ScheduleNextUpdate(false); @@ -570,15 +553,7 @@ void SafeBrowsingProtocolManager::OnGetChunksComplete( list_data.append(FormatList( SBListChunkRanges(safe_browsing_util::kMalwareList), use_mac)); - std::string url = StringPrintf(kSbUpdateUrl, - client_name_.c_str(), - version_.c_str()); - if (use_mac) { - url.append("&wrkey="); - url.append(wrapped_key_); - } - - GURL update_url(url); + GURL update_url = UpdateUrl(use_mac); request_.reset(new URLFetcher(update_url, URLFetcher::POST, this)); request_->set_load_flags(net::LOAD_DISABLE_CACHE); request_->set_request_context(request_context_getter_); @@ -593,7 +568,7 @@ void SafeBrowsingProtocolManager::OnGetChunksComplete( // If we haven't heard back from the server with an update response, this method // will run. Close the current update session and schedule another update. void SafeBrowsingProtocolManager::UpdateResponseTimeout() { - DCHECK(request_type_ == UPDATE_REQUEST); + DCHECK_EQ(request_type_, UPDATE_REQUEST); request_.reset(); UpdateFinished(false); ScheduleNextUpdate(false); @@ -613,14 +588,7 @@ void SafeBrowsingProtocolManager::OnChunkInserted() { void SafeBrowsingProtocolManager::ReportMalware(const GURL& malware_url, const GURL& page_url, const GURL& referrer_url) { - std::string report_str = StringPrintf( - kSbMalwareReportUrl, - EscapeQueryParamValue(malware_url.spec(), true).c_str(), - EscapeQueryParamValue(page_url.spec(), true).c_str(), - EscapeQueryParamValue(referrer_url.spec(), true).c_str(), - client_name_.c_str(), - version_.c_str()); - GURL report_url(report_str); + GURL report_url = MalwareReportUrl(malware_url, page_url, referrer_url); URLFetcher* report = new URLFetcher(report_url, URLFetcher::GET, this); report->set_load_flags(net::LOAD_DISABLE_CACHE); report->set_request_context(request_context_getter_); @@ -667,3 +635,67 @@ void SafeBrowsingProtocolManager::UpdateFinished(bool success) { update_size_ = 0; sb_service_->UpdateFinished(success); } + +std::string SafeBrowsingProtocolManager::ComposeUrl( + const std::string& prefix, const std::string& method, + const std::string& client_name, const std::string& version, + const std::string& additional_query) { + DCHECK(!prefix.empty() && !method.empty() && + !client_name.empty() && !version.empty()); + std::string url = StringPrintf("%s/%s?client=%s&appver=%s&pver=2.2", + prefix.c_str(), method.c_str(), + client_name.c_str(), version.c_str()); + if (!additional_query.empty()) { + url.append(additional_query); + } + return url; +} + +GURL SafeBrowsingProtocolManager::UpdateUrl(bool use_mac) const { + std::string url = ComposeUrl(info_url_prefix_, "downloads", client_name_, + version_, additional_query_); + if (use_mac) { + url.append("&wrkey="); + url.append(wrapped_key_); + } + return GURL(url); +} + +GURL SafeBrowsingProtocolManager::GetHashUrl(bool use_mac) const { + std::string url= ComposeUrl(info_url_prefix_, "gethash", client_name_, + version_, additional_query_); + if (use_mac) { + url.append("&wrkey="); + url.append(wrapped_key_); + } + return GURL(url); +} + +GURL SafeBrowsingProtocolManager::MacKeyUrl() const { + return GURL(ComposeUrl(mackey_url_prefix_, "newkey", client_name_, version_, + additional_query_)); +} + +GURL SafeBrowsingProtocolManager::MalwareReportUrl( + const GURL& malware_url, const GURL& page_url, + const GURL& referrer_url) const { + std::string url = ComposeUrl(info_url_prefix_, "report", client_name_, + version_, additional_query_); + return GURL(StringPrintf("%s&evts=malblhit&evtd=%s&evtr=%s&evhr=%s", + url.c_str(), EscapeQueryParamValue(malware_url.spec(), true).c_str(), + EscapeQueryParamValue(page_url.spec(), true).c_str(), + EscapeQueryParamValue(referrer_url.spec(), true).c_str())); +} + +GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { + std::string next_url; + if (!StartsWithASCII(url, "http://", false) && + !StartsWithASCII(url, "https://", false)) { + next_url = "http://" + url; + } else { + next_url = url; + } + if (!additional_query_.empty()) + next_url += additional_query_; + return GURL(next_url); +} |