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 "chrome/browser/safe_browsing/android_safe_browsing_api_handler.h"
10 #include "content/public/browser/browser_thread.h"
12 using content::BrowserThread
;
15 // RemoteSafeBrowsingDatabaseManager::ClientRequest methods
17 class RemoteSafeBrowsingDatabaseManager::ClientRequest
{
19 ClientRequest(Client
* client
,
20 RemoteSafeBrowsingDatabaseManager
* db_manager
,
23 static void OnRequestDoneWeak(const base::WeakPtr
<ClientRequest
>& req
,
24 SBThreatType matched_threat_type
,
25 const std::string
& metadata
);
26 void OnRequestDone(SBThreatType matched_threat_type
,
27 const std::string
& metadata
);
30 Client
* client() const { return client_
; }
31 const GURL
& url() const { return url_
; }
32 base::WeakPtr
<ClientRequest
> GetWeakPtr() {
33 return weak_factory_
.GetWeakPtr();
38 RemoteSafeBrowsingDatabaseManager
* db_manager_
;
40 base::WeakPtrFactory
<ClientRequest
> weak_factory_
;
43 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest(
45 RemoteSafeBrowsingDatabaseManager
* db_manager
,
47 : client_(client
), db_manager_(db_manager
), url_(url
), weak_factory_(this) {
51 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDoneWeak(
52 const base::WeakPtr
<ClientRequest
>& req
,
53 SBThreatType matched_threat_type
,
54 const std::string
& metadata
) {
55 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
57 return; // Previously canceled
58 req
->OnRequestDone(matched_threat_type
, metadata
);
61 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone(
62 SBThreatType matched_threat_type
,
63 const std::string
& metadata
) {
64 VLOG(1) << "OnRequestDone for client " << client_
<< " and URL " << url_
;
65 client_
->OnCheckBrowseUrlResult(url_
, matched_threat_type
, metadata
);
66 db_manager_
->CancelCheck(client_
);
70 // RemoteSafeBrowsingDatabaseManager methods
73 // TODO(nparker): Add tests for this class once implemented.
74 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager()
76 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
77 // TODO(nparker): Implement the initialization glue.
78 // Check flag to see if this is enabled.
79 // Ask AndroidSafeBrowsingAPIHandler if Java API is correct version.
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
;
160 threat_types
.push_back(SB_THREAT_TYPE_URL_MALWARE
);
162 VLOG(1) << "Checking for client " << client
<< " and URL " << url
;
163 bool started
= api_handler_
.StartURLCheck(
164 base::Bind(&ClientRequest::OnRequestDoneWeak
, req
->GetWeakPtr()), url
,
167 LOG(DFATAL
) << "Failed to start Safe Browsing request";
171 current_requests_
.push_back(req
.release());
173 // Defer the resource load.
177 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client
* client
) {
178 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
180 for (auto itr
= current_requests_
.begin(); itr
!= current_requests_
.end();
182 if ((*itr
)->client() == client
) {
183 VLOG(1) << "Canceling check for URL " << (*itr
)->url();
185 current_requests_
.erase(itr
);
192 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() {
193 VLOG(1) << "RemoteSafeBrowsing starting";
197 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown
) {
198 // |shutdown| is not used.
199 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
200 VLOG(1) << "RemoteSafeBrowsing stopping";
202 // Call back and delete any remaining clients. OnRequestDone() modifies
203 // |current_requests_|, so we make a copy first.
204 std::vector
<ClientRequest
*> to_callback(current_requests_
);
205 for (auto req
: to_callback
) {
206 VLOG(1) << "Stopping: Invoking unfinished req for URL " << req
->url();
207 req
->OnRequestDone(SB_THREAT_TYPE_SAFE
, std::string());