OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #include "chrome/browser/extensions/blacklist_state_fetcher.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" |
| 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 12 #include "google_apis/google_api_keys.h" |
| 13 #include "net/base/escape.h" |
| 14 #include "net/url_request/url_request_context_getter.h" |
| 15 #include "net/url_request/url_request_status.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 using content::BrowserThread; |
| 19 |
| 20 namespace extensions { |
| 21 |
| 22 BlacklistStateFetcher::BlacklistStateFetcher() |
| 23 : safe_browsing_config_initialized_(false), |
| 24 url_fetcher_id_(0) { |
| 25 } |
| 26 |
| 27 BlacklistStateFetcher::~BlacklistStateFetcher() { |
| 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 29 STLDeleteContainerPairFirstPointers(requests_.begin(), requests_.end()); |
| 30 requests_.clear(); |
| 31 } |
| 32 |
| 33 void BlacklistStateFetcher::Request(const std::string& id, |
| 34 const RequestCallback& callback) { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 36 if (!safe_browsing_config_initialized_) { |
| 37 if (g_browser_process && g_browser_process->safe_browsing_service()) { |
| 38 SetSafeBrowsingConfig( |
| 39 g_browser_process->safe_browsing_service()->GetProtocolConfig()); |
| 40 } else { |
| 41 // If safe browsing is not initialized, then it is probably turned off |
| 42 // completely. |
| 43 NOTREACHED(); |
| 44 base::MessageLoopProxy::current()->PostTask( |
| 45 FROM_HERE, base::Bind(callback, BLACKLISTED_UNKNOWN)); |
| 46 return; |
| 47 } |
| 48 } |
| 49 |
| 50 bool request_already_sent = ContainsKey(callbacks_, id); |
| 51 callbacks_.insert(std::make_pair(id, callback)); |
| 52 if (request_already_sent) |
| 53 return; |
| 54 |
| 55 ClientCRXListInfoRequest request; |
| 56 |
| 57 request.set_id(id); |
| 58 std::string request_str; |
| 59 request.SerializeToString(&request_str); |
| 60 |
| 61 GURL request_url = RequestUrl(); |
| 62 net::URLFetcher* fetcher = net::URLFetcher::Create(url_fetcher_id_++, |
| 63 request_url, |
| 64 net::URLFetcher::POST, |
| 65 this); |
| 66 requests_[fetcher] = id; |
| 67 fetcher->SetAutomaticallyRetryOn5xx(false); // Don't retry on error. |
| 68 if (g_browser_process && g_browser_process->safe_browsing_service()) { |
| 69 // Unless we are in unit test, safebrowsing service should be initialized. |
| 70 fetcher->SetRequestContext( |
| 71 g_browser_process->safe_browsing_service()->url_request_context()); |
| 72 } |
| 73 fetcher->SetUploadData("application/octet-stream", request_str); |
| 74 fetcher->Start(); |
| 75 } |
| 76 |
| 77 void BlacklistStateFetcher::SetSafeBrowsingConfig( |
| 78 const SafeBrowsingProtocolConfig& config) { |
| 79 safe_browsing_config_ = config; |
| 80 safe_browsing_config_initialized_ = true; |
| 81 } |
| 82 |
| 83 GURL BlacklistStateFetcher::RequestUrl() const { |
| 84 std::string url = base::StringPrintf( |
| 85 "%s/%s?client=%s&appver=%s&pver=2.2", |
| 86 safe_browsing_config_.url_prefix.c_str(), |
| 87 "clientreport/crx-list-info", |
| 88 safe_browsing_config_.client_name.c_str(), |
| 89 safe_browsing_config_.version.c_str()); |
| 90 std::string api_key = google_apis::GetAPIKey(); |
| 91 if (!api_key.empty()) { |
| 92 base::StringAppendF(&url, "&key=%s", |
| 93 net::EscapeQueryParamValue(api_key, true).c_str()); |
| 94 } |
| 95 return GURL(url); |
| 96 } |
| 97 |
| 98 void BlacklistStateFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 100 |
| 101 std::map<const net::URLFetcher*, std::string>::iterator it = |
| 102 requests_.find(source); |
| 103 if (it == requests_.end()) { |
| 104 NOTREACHED(); |
| 105 return; |
| 106 } |
| 107 |
| 108 scoped_ptr<const net::URLFetcher> fetcher; |
| 109 |
| 110 fetcher.reset(it->first); |
| 111 std::string id = it->second; |
| 112 requests_.erase(it); |
| 113 |
| 114 BlacklistState state; |
| 115 |
| 116 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 117 std::string data; |
| 118 source->GetResponseAsString(&data); |
| 119 ClientCRXListInfoResponse response; |
| 120 if (response.ParseFromString(data)) { |
| 121 state = static_cast<BlacklistState>(response.verdict()); |
| 122 } else { |
| 123 state = BLACKLISTED_UNKNOWN; |
| 124 } |
| 125 } else { |
| 126 if (source->GetStatus().status() == net::URLRequestStatus::FAILED) { |
| 127 VLOG(1) << "Blacklist request for: " << id |
| 128 << " failed with error: " << source->GetStatus().error(); |
| 129 } else { |
| 130 VLOG(1) << "Blacklist request for: " << id |
| 131 << " failed with error: " << source->GetResponseCode(); |
| 132 } |
| 133 |
| 134 state = BLACKLISTED_UNKNOWN; |
| 135 } |
| 136 |
| 137 std::pair<CallbackMultiMap::iterator, CallbackMultiMap::iterator> range = |
| 138 callbacks_.equal_range(id); |
| 139 for (CallbackMultiMap::const_iterator callback_it = range.first; |
| 140 callback_it != range.second; |
| 141 ++callback_it) { |
| 142 callback_it->second.Run(state); |
| 143 } |
| 144 |
| 145 callbacks_.erase(range.first, range.second); |
| 146 } |
| 147 |
| 148 } // namespace extensions |
| 149 |
OLD | NEW |