OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "chrome/browser/safe_browsing/remote_database_manager.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "chrome/browser/safe_browsing/android_safe_browsing_api_handler.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 | |
12 using content::BrowserThread; | |
13 | |
14 // | |
15 // RemoteSafeBrowsingDatabaseManager::ClientRequest methods | |
16 // | |
17 class RemoteSafeBrowsingDatabaseManager::ClientRequest { | |
18 public: | |
19 ClientRequest(Client* client, | |
20 RemoteSafeBrowsingDatabaseManager* db_manager, | |
21 const GURL& url); | |
22 | |
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); | |
28 | |
29 // Accessors | |
30 Client* client() const { return client_; } | |
31 const GURL& url() const { return url_; } | |
32 base::WeakPtr<ClientRequest> GetWeakPtr() { | |
33 return weak_factory_.GetWeakPtr(); | |
34 } | |
35 | |
36 private: | |
37 Client* client_; | |
38 RemoteSafeBrowsingDatabaseManager* db_manager_; | |
39 GURL url_; | |
40 base::WeakPtrFactory<ClientRequest> weak_factory_; | |
41 }; | |
42 | |
43 RemoteSafeBrowsingDatabaseManager::ClientRequest::ClientRequest( | |
44 Client* client, | |
45 RemoteSafeBrowsingDatabaseManager* db_manager, | |
46 const GURL& url) | |
47 : client_(client), db_manager_(db_manager), url_(url), weak_factory_(this) { | |
48 } | |
49 | |
50 // Static | |
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); | |
56 if (!req) | |
57 return; // Previously canceled | |
58 req->OnRequestDone(matched_threat_type, metadata); | |
59 } | |
60 | |
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_); | |
67 } | |
68 | |
69 // | |
70 // RemoteSafeBrowsingDatabaseManager methods | |
71 // | |
72 | |
73 // TODO(nparker): Add tests for this class once implemented. | |
74 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager() | |
75 : enabled_(false) { | |
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. | |
80 } | |
81 | |
82 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() { | |
83 DCHECK(!enabled_); | |
84 } | |
85 | |
86 bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { | |
87 return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) || | |
88 url.SchemeIs(url::kFtpScheme); | |
89 } | |
90 | |
91 bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled() | |
92 const { | |
93 return false; | |
94 } | |
95 | |
96 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl( | |
97 const std::vector<GURL>& url_chain, | |
98 Client* client) { | |
99 NOTREACHED(); | |
100 return true; | |
101 } | |
102 | |
103 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs( | |
104 const std::set<std::string>& extension_ids, | |
105 Client* client) { | |
106 NOTREACHED(); | |
107 return true; | |
108 } | |
109 | |
110 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP( | |
111 const std::string& ip_address) { | |
112 NOTREACHED(); | |
113 return false; | |
114 } | |
115 | |
116 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { | |
117 NOTREACHED(); | |
118 return true; | |
119 } | |
120 | |
121 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl( | |
122 const GURL& url) { | |
123 NOTREACHED(); | |
124 return true; | |
125 } | |
126 | |
127 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString( | |
128 const std::string& str) { | |
129 NOTREACHED(); | |
130 return true; | |
131 } | |
132 | |
133 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl( | |
134 const GURL& url) { | |
135 NOTREACHED(); | |
136 return true; | |
137 } | |
138 | |
139 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() { | |
140 NOTREACHED(); | |
141 return true; | |
142 } | |
143 | |
144 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { | |
145 NOTREACHED(); | |
146 return true; | |
147 } | |
148 | |
149 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, | |
150 Client* client) { | |
151 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
152 if (!enabled_) | |
153 return true; | |
154 | |
155 if (!CanCheckUrl(url)) | |
156 return true; // Safe, continue right away. | |
157 | |
158 scoped_ptr<ClientRequest> req(new ClientRequest(client, this, url)); | |
159 std::vector<SBThreatType> threat_types{SB_THREAT_TYPE_URL_MALWARE}; | |
mattm
2015/05/12 07:00:41
C++11 uniform initialization syntax isn't allowed
Nathan Parker
2015/05/12 18:23:20
Done.
| |
160 | |
161 VLOG(1) << "Checking for client " << client << " and URL " << url; | |
162 bool started = api_handler_.StartURLCheck( | |
163 base::Bind(&ClientRequest::OnRequestDoneWeak, req->GetWeakPtr()), url, | |
164 threat_types); | |
165 if (!started) { | |
166 LOG(DFATAL) << "Failed to start Safe Browsing request"; | |
167 return true; | |
168 } | |
169 | |
170 current_requests_.push_back(req.release()); | |
171 | |
172 // Defer the resource load. | |
173 return false; | |
174 } | |
175 | |
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 VLOG(1) << "Canceling check for URL " << (*itr)->url(); | |
183 delete *itr; | |
184 current_requests_.erase(itr); | |
185 return; | |
186 } | |
187 } | |
188 NOTREACHED(); | |
189 } | |
190 | |
191 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() { | |
192 VLOG(1) << "RemoteSafeBrowsing starting"; | |
193 enabled_ = true; | |
194 } | |
195 | |
196 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { | |
197 // |shutdown| is not used. | |
198 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
199 VLOG(1) << "RemoteSafeBrowsing stopping"; | |
200 | |
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 VLOG(1) << "Stopping: Invoking unfinished req for URL " << req->url(); | |
206 req->OnRequestDone(SB_THREAT_TYPE_SAFE, std::string()); | |
207 } | |
208 enabled_ = false; | |
209 } | |
210 | |
OLD | NEW |