| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/extensions/blacklist_state_fetcher.h" | 5 #include "chrome/browser/extensions/blacklist_state_fetcher.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | |
| 8 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" | 10 #include "chrome/browser/safe_browsing/protocol_manager_helper.h" |
| 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 11 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 13 #include "chrome/common/safe_browsing/crx_info.pb.h" | 12 #include "chrome/common/safe_browsing/crx_info.pb.h" |
| 14 #include "google_apis/google_api_keys.h" | 13 #include "google_apis/google_api_keys.h" |
| 15 #include "net/base/escape.h" | 14 #include "net/base/escape.h" |
| 16 #include "net/url_request/url_request_context.h" | 15 #include "net/url_request/url_request_context.h" |
| 17 #include "net/url_request/url_request_context_getter.h" | 16 #include "net/url_request/url_request_context_getter.h" |
| 18 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 19 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 20 | 19 |
| 21 using content::BrowserThread; | 20 using content::BrowserThread; |
| 22 | 21 |
| 23 namespace extensions { | 22 namespace extensions { |
| 24 | 23 |
| 25 BlacklistStateFetcher::BlacklistStateFetcher() | 24 BlacklistStateFetcher::BlacklistStateFetcher() |
| 26 : url_fetcher_id_(0), | 25 : url_fetcher_id_(0), |
| 27 weak_ptr_factory_(this) {} | 26 weak_ptr_factory_(this) {} |
| 28 | 27 |
| 29 BlacklistStateFetcher::~BlacklistStateFetcher() { | 28 BlacklistStateFetcher::~BlacklistStateFetcher() { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 29 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 31 base::STLDeleteContainerPairFirstPointers(requests_.begin(), requests_.end()); | |
| 32 requests_.clear(); | |
| 33 } | 30 } |
| 34 | 31 |
| 35 void BlacklistStateFetcher::Request(const std::string& id, | 32 void BlacklistStateFetcher::Request(const std::string& id, |
| 36 const RequestCallback& callback) { | 33 const RequestCallback& callback) { |
| 37 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 38 if (!safe_browsing_config_) { | 35 if (!safe_browsing_config_) { |
| 39 if (g_browser_process && g_browser_process->safe_browsing_service()) { | 36 if (g_browser_process && g_browser_process->safe_browsing_service()) { |
| 40 SetSafeBrowsingConfig( | 37 SetSafeBrowsingConfig( |
| 41 g_browser_process->safe_browsing_service()->GetProtocolConfig()); | 38 g_browser_process->safe_browsing_service()->GetProtocolConfig()); |
| 42 } else { | 39 } else { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 | 59 |
| 63 void BlacklistStateFetcher::SendRequest(const std::string& id) { | 60 void BlacklistStateFetcher::SendRequest(const std::string& id) { |
| 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 65 | 62 |
| 66 ClientCRXListInfoRequest request; | 63 ClientCRXListInfoRequest request; |
| 67 request.set_id(id); | 64 request.set_id(id); |
| 68 std::string request_str; | 65 std::string request_str; |
| 69 request.SerializeToString(&request_str); | 66 request.SerializeToString(&request_str); |
| 70 | 67 |
| 71 GURL request_url = RequestUrl(); | 68 GURL request_url = RequestUrl(); |
| 72 net::URLFetcher* fetcher = | 69 std::unique_ptr<net::URLFetcher> fetcher_ptr = net::URLFetcher::Create( |
| 73 net::URLFetcher::Create(url_fetcher_id_++, request_url, | 70 url_fetcher_id_++, request_url, net::URLFetcher::POST, this); |
| 74 net::URLFetcher::POST, this).release(); | 71 net::URLFetcher* fetcher = fetcher_ptr.get(); |
| 75 requests_[fetcher] = id; | 72 requests_[fetcher] = {std::move(fetcher_ptr), id}; |
| 76 fetcher->SetAutomaticallyRetryOn5xx(false); // Don't retry on error. | 73 fetcher->SetAutomaticallyRetryOn5xx(false); // Don't retry on error. |
| 77 fetcher->SetRequestContext(url_request_context_getter_.get()); | 74 fetcher->SetRequestContext(url_request_context_getter_.get()); |
| 78 fetcher->SetUploadData("application/octet-stream", request_str); | 75 fetcher->SetUploadData("application/octet-stream", request_str); |
| 79 fetcher->Start(); | 76 fetcher->Start(); |
| 80 } | 77 } |
| 81 | 78 |
| 82 void BlacklistStateFetcher::SetSafeBrowsingConfig( | 79 void BlacklistStateFetcher::SetSafeBrowsingConfig( |
| 83 const safe_browsing::SafeBrowsingProtocolConfig& config) { | 80 const safe_browsing::SafeBrowsingProtocolConfig& config) { |
| 84 safe_browsing_config_.reset( | 81 safe_browsing_config_.reset( |
| 85 new safe_browsing::SafeBrowsingProtocolConfig(config)); | 82 new safe_browsing::SafeBrowsingProtocolConfig(config)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 101 if (!api_key.empty()) { | 98 if (!api_key.empty()) { |
| 102 base::StringAppendF(&url, "&key=%s", | 99 base::StringAppendF(&url, "&key=%s", |
| 103 net::EscapeQueryParamValue(api_key, true).c_str()); | 100 net::EscapeQueryParamValue(api_key, true).c_str()); |
| 104 } | 101 } |
| 105 return GURL(url); | 102 return GURL(url); |
| 106 } | 103 } |
| 107 | 104 |
| 108 void BlacklistStateFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 105 void BlacklistStateFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 106 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 110 | 107 |
| 111 std::map<const net::URLFetcher*, std::string>::iterator it = | 108 auto it = requests_.find(source); |
| 112 requests_.find(source); | |
| 113 if (it == requests_.end()) { | 109 if (it == requests_.end()) { |
| 114 NOTREACHED(); | 110 NOTREACHED(); |
| 115 return; | 111 return; |
| 116 } | 112 } |
| 117 | 113 |
| 118 std::unique_ptr<const net::URLFetcher> fetcher; | 114 std::unique_ptr<net::URLFetcher> fetcher = std::move(it->second.first); |
| 119 | 115 std::string id = it->second.second; |
| 120 fetcher.reset(it->first); | |
| 121 std::string id = it->second; | |
| 122 requests_.erase(it); | 116 requests_.erase(it); |
| 123 | 117 |
| 124 BlacklistState state; | 118 BlacklistState state; |
| 125 | 119 |
| 126 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { | 120 if (source->GetStatus().is_success() && source->GetResponseCode() == 200) { |
| 127 std::string data; | 121 std::string data; |
| 128 source->GetResponseAsString(&data); | 122 source->GetResponseAsString(&data); |
| 129 ClientCRXListInfoResponse response; | 123 ClientCRXListInfoResponse response; |
| 130 if (response.ParseFromString(data)) { | 124 if (response.ParseFromString(data)) { |
| 131 state = static_cast<BlacklistState>(response.verdict()); | 125 state = static_cast<BlacklistState>(response.verdict()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 149 for (CallbackMultiMap::const_iterator callback_it = range.first; | 143 for (CallbackMultiMap::const_iterator callback_it = range.first; |
| 150 callback_it != range.second; | 144 callback_it != range.second; |
| 151 ++callback_it) { | 145 ++callback_it) { |
| 152 callback_it->second.Run(state); | 146 callback_it->second.Run(state); |
| 153 } | 147 } |
| 154 | 148 |
| 155 callbacks_.erase(range.first, range.second); | 149 callbacks_.erase(range.first, range.second); |
| 156 } | 150 } |
| 157 | 151 |
| 158 } // namespace extensions | 152 } // namespace extensions |
| OLD | NEW |