Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / safe_browsing / remote_database_manager.cc
blob1577191c71ae9a9cb0ce3ac40ad99b5457d841fa
1 // Copyright 2015 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.
5 #include "chrome/browser/safe_browsing/remote_database_manager.h"
7 #include <vector>
9 #include "base/metrics/histogram_macros.h"
10 #include "base/timer/elapsed_timer.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_api_handler.h"
12 #include "content/public/browser/browser_thread.h"
14 using content::BrowserThread;
17 // RemoteSafeBrowsingDatabaseManager::ClientRequest methods
19 class RemoteSafeBrowsingDatabaseManager::ClientRequest {
20 public:
21 ClientRequest(Client* client,
22 RemoteSafeBrowsingDatabaseManager* db_manager,
23 const GURL& url);
25 static void OnRequestDoneWeak(const base::WeakPtr<ClientRequest>& req,
26 SBThreatType matched_threat_type,
27 const std::string& metadata);
28 void OnRequestDone(SBThreatType matched_threat_type,
29 const std::string& metadata);
31 // Accessors
32 Client* client() const { return client_; }
33 const GURL& url() const { return url_; }
34 base::WeakPtr<ClientRequest> GetWeakPtr() {
35 return weak_factory_.GetWeakPtr();
38 private:
39 Client* client_;
40 RemoteSafeBrowsingDatabaseManager* db_manager_;
41 GURL url_;
42 base::ElapsedTimer timer_;
43 base::WeakPtrFactory<ClientRequest> weak_factory_;
46 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest(
47 Client* client,
48 RemoteSafeBrowsingDatabaseManager* db_manager,
49 const GURL& url)
50 : client_(client), db_manager_(db_manager), url_(url), weak_factory_(this) {
53 // Static
54 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDoneWeak(
55 const base::WeakPtr<ClientRequest>& req,
56 SBThreatType matched_threat_type,
57 const std::string& metadata) {
58 DCHECK_CURRENTLY_ON(BrowserThread::IO);
59 if (!req)
60 return; // Previously canceled
61 req->OnRequestDone(matched_threat_type, metadata);
64 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone(
65 SBThreatType matched_threat_type,
66 const std::string& metadata) {
67 DVLOG(1) << "OnRequestDone took " << timer_.Elapsed().InMilliseconds()
68 << " ms for client " << client_ << " and URL " << url_;
69 client_->OnCheckBrowseUrlResult(url_, matched_threat_type, metadata);
70 db_manager_->CancelCheck(client_);
71 UMA_HISTOGRAM_TIMES("SB2.RemoteCall.Elapsed", timer_.Elapsed());
75 // RemoteSafeBrowsingDatabaseManager methods
78 // TODO(nparker): Add tests for this class
79 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager()
80 : enabled_(false) {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() {
85 DCHECK(!enabled_);
88 bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const {
89 return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) ||
90 url.SchemeIs(url::kFtpScheme);
93 bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled()
94 const {
95 return false;
98 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl(
99 const std::vector<GURL>& url_chain,
100 Client* client) {
101 NOTREACHED();
102 return true;
105 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs(
106 const std::set<std::string>& extension_ids,
107 Client* client) {
108 NOTREACHED();
109 return true;
112 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP(
113 const std::string& ip_address) {
114 NOTREACHED();
115 return false;
118 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) {
119 NOTREACHED();
120 return true;
123 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(
124 const GURL& url) {
125 NOTREACHED();
126 return true;
129 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString(
130 const std::string& str) {
131 NOTREACHED();
132 return true;
135 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(
136 const GURL& url) {
137 NOTREACHED();
138 return true;
141 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() {
142 NOTREACHED();
143 return true;
146 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
147 NOTREACHED();
148 return true;
151 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url,
152 Client* client) {
153 DCHECK_CURRENTLY_ON(BrowserThread::IO);
154 if (!enabled_)
155 return true;
157 bool can_check_url = CanCheckUrl(url);
158 UMA_HISTOGRAM_BOOLEAN("SB2.RemoteCall.CanCheckUrl", can_check_url);
159 if (!can_check_url)
160 return true; // Safe, continue right away.
162 scoped_ptr<ClientRequest> req(new ClientRequest(client, this, url));
163 std::vector<SBThreatType> threat_types; // Not currently used.
165 DVLOG(1) << "Checking for client " << client << " and URL " << url;
166 SafeBrowsingApiHandler* api_handler = SafeBrowsingApiHandler::GetInstance();
167 // If your build hits this at run time, then you should have either not built
168 // with safe_browsing=3, or set a SafeBrowingApiHandler singleton at startup.
169 DCHECK(api_handler) << "SafeBrowsingApiHandler was never constructed";
170 api_handler->StartURLCheck(
171 base::Bind(&ClientRequest::OnRequestDoneWeak, req->GetWeakPtr()), url,
172 threat_types);
174 UMA_HISTOGRAM_COUNTS_10000("SB2.RemoteCall.ChecksPending",
175 current_requests_.size());
176 current_requests_.push_back(req.release());
178 // Defer the resource load.
179 return false;
182 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client* client) {
183 DCHECK_CURRENTLY_ON(BrowserThread::IO);
184 DCHECK(enabled_);
185 for (auto itr = current_requests_.begin(); itr != current_requests_.end();
186 ++itr) {
187 if ((*itr)->client() == client) {
188 DVLOG(2) << "Canceling check for URL " << (*itr)->url();
189 delete *itr;
190 current_requests_.erase(itr);
191 return;
194 NOTREACHED();
197 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() {
198 VLOG(1) << "RemoteSafeBrowsingDatabaseManager starting";
199 enabled_ = true;
202 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) {
203 // |shutdown| is not used.
204 DCHECK_CURRENTLY_ON(BrowserThread::IO);
205 DVLOG(1) << "RemoteSafeBrowsingDatabaseManager stopping";
207 // Call back and delete any remaining clients. OnRequestDone() modifies
208 // |current_requests_|, so we make a copy first.
209 std::vector<ClientRequest*> to_callback(current_requests_);
210 for (auto req : to_callback) {
211 DVLOG(1) << "Stopping: Invoking unfinished req for URL " << req->url();
212 req->OnRequestDone(SB_THREAT_TYPE_SAFE, std::string());
214 enabled_ = false;