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/timer/elapsed_timer.h"
10 #include "chrome/browser/safe_browsing/safe_browsing_api_handler.h"
11 #include "content/public/browser/browser_thread.h"
13 using content::BrowserThread
;
16 // RemoteSafeBrowsingDatabaseManager::ClientRequest methods
18 class RemoteSafeBrowsingDatabaseManager::ClientRequest
{
20 ClientRequest(Client
* client
,
21 RemoteSafeBrowsingDatabaseManager
* db_manager
,
24 static void OnRequestDoneWeak(const base::WeakPtr
<ClientRequest
>& req
,
25 SBThreatType matched_threat_type
,
26 const std::string
& metadata
);
27 void OnRequestDone(SBThreatType matched_threat_type
,
28 const std::string
& metadata
);
31 Client
* client() const { return client_
; }
32 const GURL
& url() const { return url_
; }
33 base::WeakPtr
<ClientRequest
> GetWeakPtr() {
34 return weak_factory_
.GetWeakPtr();
39 RemoteSafeBrowsingDatabaseManager
* db_manager_
;
41 base::ElapsedTimer timer_
;
42 base::WeakPtrFactory
<ClientRequest
> weak_factory_
;
45 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest(
47 RemoteSafeBrowsingDatabaseManager
* db_manager
,
49 : client_(client
), db_manager_(db_manager
), url_(url
), weak_factory_(this) {
53 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDoneWeak(
54 const base::WeakPtr
<ClientRequest
>& req
,
55 SBThreatType matched_threat_type
,
56 const std::string
& metadata
) {
57 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
59 return; // Previously canceled
60 req
->OnRequestDone(matched_threat_type
, metadata
);
63 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone(
64 SBThreatType matched_threat_type
,
65 const std::string
& metadata
) {
66 DVLOG(1) << "OnRequestDone took " << timer_
.Elapsed().InMilliseconds()
67 << " ms for client " << client_
<< " and URL " << url_
;
68 client_
->OnCheckBrowseUrlResult(url_
, matched_threat_type
, metadata
);
69 db_manager_
->CancelCheck(client_
);
73 // RemoteSafeBrowsingDatabaseManager methods
76 // TODO(nparker): Add tests for this class once implemented.
77 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager()
79 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
82 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() {
86 bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL
& url
) const {
87 return url
.SchemeIs(url::kHttpsScheme
) || url
.SchemeIs(url::kHttpScheme
) ||
88 url
.SchemeIs(url::kFtpScheme
);
91 bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled()
96 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl(
97 const std::vector
<GURL
>& url_chain
,
103 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs(
104 const std::set
<std::string
>& extension_ids
,
110 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP(
111 const std::string
& ip_address
) {
116 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL
& url
) {
121 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(
127 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString(
128 const std::string
& str
) {
133 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(
139 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() {
144 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
149 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL
& url
,
151 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
155 if (!CanCheckUrl(url
))
156 return true; // Safe, continue right away.
158 scoped_ptr
<ClientRequest
> req(new ClientRequest(client
, this, url
));
159 std::vector
<SBThreatType
> threat_types
; // Not currently used.
161 DVLOG(1) << "Checking for client " << client
<< " and URL " << url
;
162 SafeBrowsingApiHandler
* api_handler
= SafeBrowsingApiHandler::GetInstance();
163 // If your build hits this at run time, then you should have either no built
164 // with safe_browsing=3, or set a SafeBrowingApiHandler singleton at startup.
165 DCHECK(api_handler
) << "SafeBrowsingApiHandler was never constructed";
166 api_handler
->StartURLCheck(
167 base::Bind(&ClientRequest::OnRequestDoneWeak
, req
->GetWeakPtr()), url
,
170 current_requests_
.push_back(req
.release());
172 // Defer the resource load.
176 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client
* client
) {
177 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
179 for (auto itr
= current_requests_
.begin(); itr
!= current_requests_
.end();
181 if ((*itr
)->client() == client
) {
182 DVLOG(2) << "Canceling check for URL " << (*itr
)->url();
184 current_requests_
.erase(itr
);
191 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() {
192 VLOG(1) << "RemoteSafeBrowsingDatabaseManager starting";
196 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown
) {
197 // |shutdown| is not used.
198 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
199 DVLOG(1) << "RemoteSafeBrowsingDatabaseManager stopping";
201 // Call back and delete any remaining clients. OnRequestDone() modifies
202 // |current_requests_|, so we make a copy first.
203 std::vector
<ClientRequest
*> to_callback(current_requests_
);
204 for (auto req
: to_callback
) {
205 DVLOG(1) << "Stopping: Invoking unfinished req for URL " << req
->url();
206 req
->OnRequestDone(SB_THREAT_TYPE_SAFE
, std::string());