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 // TODO(nparker): Add tests for this class once implemented. | |
15 RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager() | |
16 : enabled_(false) { | |
17 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
18 // TODO(nparker): Implement the initialization glue. | |
19 // Check flag to see if this is enabled. | |
20 // Ask AndroidSafeBrowsingAPIHandler if Java API is correct version. | |
21 } | |
22 | |
23 RemoteSafeBrowsingDatabaseManager::~RemoteSafeBrowsingDatabaseManager() { | |
24 DCHECK(!enabled_); | |
25 } | |
26 | |
27 bool RemoteSafeBrowsingDatabaseManager::CanCheckUrl(const GURL& url) const { | |
28 return url.SchemeIs(url::kHttpsScheme) || url.SchemeIs(url::kHttpScheme) || | |
29 url.SchemeIs(url::kFtpScheme); | |
30 } | |
31 | |
32 bool RemoteSafeBrowsingDatabaseManager::download_protection_enabled() | |
33 const { | |
34 return false; | |
35 } | |
36 | |
37 bool RemoteSafeBrowsingDatabaseManager::CheckDownloadUrl( | |
38 const std::vector<GURL>& url_chain, | |
39 Client* client) { | |
40 NOTREACHED(); | |
41 return true; | |
42 } | |
43 | |
44 bool RemoteSafeBrowsingDatabaseManager::CheckExtensionIDs( | |
45 const std::set<std::string>& extension_ids, | |
46 Client* client) { | |
47 NOTREACHED(); | |
48 return true; | |
49 } | |
50 | |
51 bool RemoteSafeBrowsingDatabaseManager::MatchMalwareIP( | |
52 const std::string& ip_address) { | |
53 NOTREACHED(); | |
54 return false; | |
55 } | |
56 | |
57 bool RemoteSafeBrowsingDatabaseManager::MatchCsdWhitelistUrl(const GURL& url) { | |
58 NOTREACHED(); | |
59 return true; | |
60 } | |
61 | |
62 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistUrl( | |
63 const GURL& url) { | |
64 NOTREACHED(); | |
65 return true; | |
66 } | |
67 | |
68 bool RemoteSafeBrowsingDatabaseManager::MatchDownloadWhitelistString( | |
69 const std::string& str) { | |
70 NOTREACHED(); | |
71 return true; | |
72 } | |
73 | |
74 bool RemoteSafeBrowsingDatabaseManager::MatchInclusionWhitelistUrl( | |
75 const GURL& url) { | |
76 NOTREACHED(); | |
77 return true; | |
78 } | |
79 | |
80 bool RemoteSafeBrowsingDatabaseManager::IsMalwareKillSwitchOn() { | |
81 NOTREACHED(); | |
82 return true; | |
83 } | |
84 | |
85 bool RemoteSafeBrowsingDatabaseManager::IsCsdWhitelistKillSwitchOn() { | |
86 NOTREACHED(); | |
87 return true; | |
88 } | |
89 | |
90 SafeBrowsingProtocolManagerDelegate* | |
91 RemoteSafeBrowsingDatabaseManager::GetProtocolManagerDelegate() { | |
92 return NULL; | |
93 } | |
94 | |
95 bool RemoteSafeBrowsingDatabaseManager::CheckBrowseUrl(const GURL& url, | |
96 Client* client) { | |
97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
98 if (!enabled_) | |
99 return true; | |
100 | |
101 if (!CanCheckUrl(url)) | |
102 return true; // Safe, continue right away. | |
103 | |
104 scoped_refptr<ClientRequest> req = new ClientRequest(); | |
mattm
2015/05/06 02:42:45
Refcounting is definitely more common in chrome, b
Nathan Parker
2015/05/06 22:27:36
Good idea. I think that's what you were suggestin
| |
105 req->db_manager = this; | |
106 req->client = client; | |
107 req->url = url; | |
108 | |
109 std::vector<SBThreatType> threat_types { SB_THREAT_TYPE_URL_MALWARE }; | |
110 | |
111 VLOG(1) << "Checking for client " << client << " and URL " << url; | |
112 bool started = api_handler_.StartURLCheck( | |
113 base::Bind(&ClientRequest::OnRequestDone, req), url, threat_types); | |
114 if (!started) { | |
115 LOG(DFATAL) << "Failed to start Safe Browsing request"; | |
116 return true; | |
117 } | |
118 | |
119 // Now api_handler_ holds a ref via the callback, and we hold a ref here. | |
120 current_requests_.push_back(req); | |
121 | |
122 // Defer the resource load. | |
123 return false; | |
124 } | |
125 | |
126 void RemoteSafeBrowsingDatabaseManager::ClientRequest::OnRequestDone( | |
127 SBThreatType matched_threat_type, | |
128 const std::string& metadata) { | |
129 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
130 VLOG(1) << "OnRequestDone for client " << client << " and URL " << url; | |
131 if (!client) | |
mattm
2015/05/06 02:42:45
if you do it with weakptrs I think you can also ge
Nathan Parker
2015/05/06 22:27:36
Done.
| |
132 return; // Already canceled. | |
133 client->OnCheckBrowseUrlResult(url, matched_threat_type, metadata); | |
134 db_manager->CancelCheck(client); | |
135 } | |
136 | |
137 void RemoteSafeBrowsingDatabaseManager::CancelCheck(Client* client) { | |
138 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
139 DCHECK(enabled_); | |
140 for (auto itr = current_requests_.begin(); itr != current_requests_.end(); | |
141 ++itr) { | |
142 if ((*itr)->client == client) { | |
143 // Mark the req as canceled. | |
144 (*itr)->client = NULL; | |
145 VLOG(1) << "Canceling check for URL " << (*itr)->url; | |
146 current_requests_.erase(itr); | |
147 return; | |
148 } | |
149 } | |
150 NOTREACHED(); | |
151 } | |
152 | |
153 void RemoteSafeBrowsingDatabaseManager::StartOnIOThread() { | |
154 VLOG(1) << "RemoteSafeBrowsing starting"; | |
155 enabled_ = true; | |
156 } | |
157 | |
158 void RemoteSafeBrowsingDatabaseManager::StopOnIOThread(bool shutdown) { | |
159 // |shutdown| is not used. | |
160 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
161 VLOG(1) << "RemoteSafeBrowsing stopping"; | |
162 | |
163 // Call back and remove any remaining clients. This modifies | |
164 // |current_requests_|, so make a copy first. | |
165 std::vector<scoped_refptr<ClientRequest>> to_callback = current_requests_; | |
166 for (auto req : to_callback) { | |
167 VLOG(1) << "Stopping: Invoking unfinished req for URL " << req->url; | |
168 req->OnRequestDone(SB_THREAT_TYPE_SAFE, std::string()); | |
169 } | |
170 enabled_ = false; | |
171 } | |
172 | |
OLD | NEW |