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"
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
{
21 ClientRequest(Client
* client
,
22 RemoteSafeBrowsingDatabaseManager
* db_manager
,
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
);
32 Client
* client() const { return client_
; }
33 const GURL
& url() const { return url_
; }
34 base::WeakPtr
<ClientRequest
> GetWeakPtr() {
35 return weak_factory_
.GetWeakPtr();
40 RemoteSafeBrowsingDatabaseManager
* db_manager_
;
42 base::ElapsedTimer timer_
;
43 base::WeakPtrFactory
<ClientRequest
> weak_factory_
;
46 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest(
48 RemoteSafeBrowsingDatabaseManager
* db_manager
,
50 : client_(client
), db_manager_(db_manager
), url_(url
), weak_factory_(this) {
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
);
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()
81 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
84 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() {
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()
98 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl(
99 const std::vector
<GURL
>& url_chain
,
105 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs(
106 const std::set
<std::string
>& extension_ids
,
112 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP(
113 const std::string
& ip_address
) {
118 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL
& url
) {
123 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(
129 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString(
130 const std::string
& str
) {
135 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(
141 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() {
146 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
151 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL
& url
,
153 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
157 bool can_check_url
= CanCheckUrl(url
);
158 UMA_HISTOGRAM_BOOLEAN("SB2.RemoteCall.CanCheckUrl", 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
,
174 UMA_HISTOGRAM_COUNTS_10000("SB2.RemoteCall.ChecksPending",
175 current_requests_
.size());
176 current_requests_
.push_back(req
.release());
178 // Defer the resource load.
182 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client
* client
) {
183 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
185 for (auto itr
= current_requests_
.begin(); itr
!= current_requests_
.end();
187 if ((*itr
)->client() == client
) {
188 DVLOG(2) << "Canceling check for URL " << (*itr
)->url();
190 current_requests_
.erase(itr
);
197 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() {
198 VLOG(1) << "RemoteSafeBrowsingDatabaseManager starting";
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());