Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / safe_browsing / remote_database_manager.cc
blobb62dd782155c4adf0e84d959ec919189fb9e369a
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/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 {
19 public:
20 ClientRequest(Client* client,
21 RemoteSafeBrowsingDatabaseManager* db_manager,
22 const GURL& url);
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);
30 // Accessors
31 Client* client() const { return client_; }
32 const GURL& url() const { return url_; }
33 base::WeakPtr<ClientRequest> GetWeakPtr() {
34 return weak_factory_.GetWeakPtr();
37 private:
38 Client* client_;
39 RemoteSafeBrowsingDatabaseManager* db_manager_;
40 GURL url_;
41 base::ElapsedTimer timer_;
42 base::WeakPtrFactory<ClientRequest> weak_factory_;
45 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest(
46 Client* client,
47 RemoteSafeBrowsingDatabaseManager* db_manager,
48 const GURL& url)
49 : client_(client), db_manager_(db_manager), url_(url), weak_factory_(this) {
52 // Static
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);
58 if (!req)
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()
78 : enabled_(false) {
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
82 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() {
83 DCHECK(!enabled_);
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()
92 const {
93 return false;
96 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl(
97 const std::vector<GURL>& url_chain,
98 Client* client) {
99 NOTREACHED();
100 return true;
103 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs(
104 const std::set<std::string>& extension_ids,
105 Client* client) {
106 NOTREACHED();
107 return true;
110 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP(
111 const std::string& ip_address) {
112 NOTREACHED();
113 return false;
116 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) {
117 NOTREACHED();
118 return true;
121 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl(
122 const GURL& url) {
123 NOTREACHED();
124 return true;
127 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString(
128 const std::string& str) {
129 NOTREACHED();
130 return true;
133 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl(
134 const GURL& url) {
135 NOTREACHED();
136 return true;
139 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() {
140 NOTREACHED();
141 return true;
144 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() {
145 NOTREACHED();
146 return true;
149 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url,
150 Client* client) {
151 DCHECK_CURRENTLY_ON(BrowserThread::IO);
152 if (!enabled_)
153 return true;
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,
168 threat_types);
170 current_requests_.push_back(req.release());
172 // Defer the resource load.
173 return false;
176 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client* client) {
177 DCHECK_CURRENTLY_ON(BrowserThread::IO);
178 DCHECK(enabled_);
179 for (auto itr = current_requests_.begin(); itr != current_requests_.end();
180 ++itr) {
181 if ((*itr)->client() == client) {
182 DVLOG(2) << "Canceling check for URL " << (*itr)->url();
183 delete *itr;
184 current_requests_.erase(itr);
185 return;
188 NOTREACHED();
191 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() {
192 VLOG(1) << "RemoteSafeBrowsingDatabaseManager starting";
193 enabled_ = true;
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());
208 enabled_ = false;