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