Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / safe_browsing / database_manager.h
blobe93a65e6bc127f915c4498c9fed216f682be0a96
1 // Copyright (c) 2012 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 // The Safe Browsing service is responsible for downloading anti-phishing and
6 // anti-malware tables and checking urls against them.
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_
11 #include <deque>
12 #include <map>
13 #include <set>
14 #include <string>
15 #include <vector>
17 #include "base/callback.h"
18 #include "base/containers/hash_tables.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/synchronization/lock.h"
22 #include "base/time/time.h"
23 #include "chrome/browser/safe_browsing/protocol_manager.h"
24 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
25 #include "url/gurl.h"
27 class SafeBrowsingService;
28 class SafeBrowsingDatabase;
30 namespace base {
31 class Thread;
34 namespace net {
35 class URLRequestContext;
36 class URLRequestContextGetter;
39 namespace safe_browsing {
40 class ClientSideDetectionService;
41 class DownloadProtectionService;
44 // Construction needs to happen on the main thread.
45 class SafeBrowsingDatabaseManager
46 : public base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>,
47 public SafeBrowsingProtocolManagerDelegate {
48 public:
49 class Client;
51 // Bundle of SafeBrowsing state while performing a URL or hash prefix check.
52 struct SafeBrowsingCheck {
53 // |check_type| should correspond to the type of item that is being
54 // checked, either a URL or a binary hash/URL. We store this for two
55 // purposes: to know which of Client's methods to call when a result is
56 // known, and for logging purposes. It *isn't* used to predict the response
57 // list type, that is information that the server gives us.
58 SafeBrowsingCheck(const std::vector<GURL>& urls,
59 const std::vector<SBFullHash>& full_hashes,
60 Client* client,
61 safe_browsing_util::ListType check_type,
62 const std::vector<SBThreatType>& expected_threats);
63 ~SafeBrowsingCheck();
65 // Either |urls| or |full_hashes| is used to lookup database. |*_results|
66 // are parallel vectors containing the results. They are initialized to
67 // contain SB_THREAT_TYPE_SAFE.
68 std::vector<GURL> urls;
69 std::vector<SBThreatType> url_results;
70 std::vector<std::string> url_metadata;
71 std::vector<SBFullHash> full_hashes;
72 std::vector<SBThreatType> full_hash_results;
74 Client* client;
75 bool need_get_hash;
76 base::TimeTicks start; // When check was sent to SB service.
77 safe_browsing_util::ListType check_type; // See comment in constructor.
78 std::vector<SBThreatType> expected_threats;
79 std::vector<SBPrefix> prefix_hits;
80 std::vector<SBFullHashResult> cache_hits;
82 // Vends weak pointers for TimeoutCallback(). If the response is
83 // received before the timeout fires, factory is destructed and
84 // the timeout won't be fired.
85 // TODO(lzheng): We should consider to use this time out check
86 // for browsing too (instead of implementin in
87 // safe_browsing_resource_handler.cc).
88 scoped_ptr<base::WeakPtrFactory<
89 SafeBrowsingDatabaseManager> > timeout_factory_;
91 private:
92 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck);
95 class Client {
96 public:
97 void OnSafeBrowsingResult(const SafeBrowsingCheck& check);
99 protected:
100 virtual ~Client() {}
102 // Called when the result of checking a browse URL is known.
103 virtual void OnCheckBrowseUrlResult(const GURL& url,
104 SBThreatType threat_type,
105 const std::string& metadata) {}
107 // Called when the result of checking a download URL is known.
108 virtual void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
109 SBThreatType threat_type) {}
111 // Called when the result of checking a set of extensions is known.
112 virtual void OnCheckExtensionsResult(
113 const std::set<std::string>& threats) {}
116 // Creates the safe browsing service. Need to initialize before using.
117 explicit SafeBrowsingDatabaseManager(
118 const scoped_refptr<SafeBrowsingService>& service);
120 // Returns true if the url's scheme can be checked.
121 bool CanCheckUrl(const GURL& url) const;
123 // Returns whether download protection is enabled.
124 bool download_protection_enabled() const {
125 return enable_download_protection_;
128 // Called on the IO thread to check if the given url is safe or not. If we
129 // can synchronously determine that the url is safe, CheckUrl returns true.
130 // Otherwise it returns false, and "client" is called asynchronously with the
131 // result when it is ready.
132 virtual bool CheckBrowseUrl(const GURL& url, Client* client);
134 // Check if the prefix for |url| is in safebrowsing download add lists.
135 // Result will be passed to callback in |client|.
136 virtual bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
137 Client* client);
139 // Check which prefixes in |extension_ids| are in the safebrowsing blacklist.
140 // Returns true if not, false if further checks need to be made in which case
141 // the result will be passed to |client|.
142 virtual bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
143 Client* client);
145 // Check if the given url is on the side-effect free whitelist.
146 // Can be called on any thread. Returns false if the check cannot be performed
147 // (e.g. because we are disabled or because of an invalid scheme in the URL).
148 // Otherwise, returns true if the URL is on the whitelist based on matching
149 // the hash prefix only (so there may be false positives).
150 virtual bool CheckSideEffectFreeWhitelistUrl(const GURL& url);
152 // Check if the |url| matches any of the full-length hashes from the
153 // client-side phishing detection whitelist. Returns true if there was a
154 // match and false otherwise. To make sure we are conservative we will return
155 // true if an error occurs. This method is expected to be called on the IO
156 // thread.
157 virtual bool MatchCsdWhitelistUrl(const GURL& url);
159 // Check if the given IP address (either IPv4 or IPv6) matches the malware
160 // IP blacklist.
161 virtual bool MatchMalwareIP(const std::string& ip_address);
163 // Check if the |url| matches any of the full-length hashes from the
164 // download whitelist. Returns true if there was a match and false otherwise.
165 // To make sure we are conservative we will return true if an error occurs.
166 // This method is expected to be called on the IO thread.
167 virtual bool MatchDownloadWhitelistUrl(const GURL& url);
169 // Check if |str| matches any of the full-length hashes from the download
170 // whitelist. Returns true if there was a match and false otherwise.
171 // To make sure we are conservative we will return true if an error occurs.
172 // This method is expected to be called on the IO thread.
173 virtual bool MatchDownloadWhitelistString(const std::string& str);
175 // Check if the CSD malware IP matching kill switch is turned on.
176 virtual bool IsMalwareKillSwitchOn();
178 // Check if the CSD whitelist kill switch is turned on.
179 virtual bool IsCsdWhitelistKillSwitchOn();
181 // Called on the IO thread to cancel a pending check if the result is no
182 // longer needed.
183 void CancelCheck(Client* client);
185 // Called on the IO thread when the SafeBrowsingProtocolManager has received
186 // the full hash results for prefix hits detected in the database.
187 void HandleGetHashResults(SafeBrowsingCheck* check,
188 const std::vector<SBFullHashResult>& full_hashes,
189 const base::TimeDelta& cache_lifetime);
191 // Called to initialize objects that are used on the io_thread. This may be
192 // called multiple times during the life of the DatabaseManager. Should be
193 // called on IO thread.
194 void StartOnIOThread();
196 // Called to stop or shutdown operations on the io_thread. This may be called
197 // multiple times during the life of the DatabaseManager. Should be called
198 // on IO thread. If shutdown is true, the manager is disabled permanently.
199 void StopOnIOThread(bool shutdown);
201 protected:
202 ~SafeBrowsingDatabaseManager() override;
204 // protected for tests.
205 void NotifyDatabaseUpdateFinished(bool update_succeeded);
207 private:
208 friend class base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>;
209 friend class SafeBrowsingServerTest;
210 friend class SafeBrowsingServiceTest;
211 friend class SafeBrowsingServiceTestHelper;
212 friend class SafeBrowsingDatabaseManagerTest;
213 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest,
214 GetUrlSeverestThreatType);
216 typedef std::set<SafeBrowsingCheck*> CurrentChecks;
217 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors;
218 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests;
220 // Clients that we've queued up for checking later once the database is ready.
221 struct QueuedCheck {
222 QueuedCheck(const safe_browsing_util::ListType check_type,
223 Client* client,
224 const GURL& url,
225 const std::vector<SBThreatType>& expected_threats,
226 const base::TimeTicks& start);
227 ~QueuedCheck();
228 safe_browsing_util::ListType check_type;
229 Client* client;
230 GURL url;
231 std::vector<SBThreatType> expected_threats;
232 base::TimeTicks start; // When check was queued.
235 // Return the threat type of the severest entry in |full_hashes| which matches
236 // |hash|, or SAFE if none match.
237 static SBThreatType GetHashSeverestThreatType(
238 const SBFullHash& hash,
239 const std::vector<SBFullHashResult>& full_hashes);
241 // Given a URL, compare all the possible host + path full hashes to the set of
242 // provided full hashes. Returns the threat type of the severest matching
243 // result from |full_hashes|, or SAFE if none match.
244 static SBThreatType GetUrlSeverestThreatType(
245 const GURL& url,
246 const std::vector<SBFullHashResult>& full_hashes,
247 size_t* index);
249 // Called to stop operations on the io_thread. This may be called multiple
250 // times during the life of the DatabaseManager. Should be called on IO
251 // thread.
252 void DoStopOnIOThread();
254 // Returns whether |database_| exists and is accessible.
255 bool DatabaseAvailable() const;
257 // Called on the IO thread. If the database does not exist, queues up a call
258 // on the db thread to create it. Returns whether the database is available.
260 // Note that this is only needed outside the db thread, since functions on the
261 // db thread can call GetDatabase() directly.
262 bool MakeDatabaseAvailable();
264 // Should only be called on db thread as SafeBrowsingDatabase is not
265 // threadsafe.
266 SafeBrowsingDatabase* GetDatabase();
268 // Called on the IO thread with the check result.
269 void OnCheckDone(SafeBrowsingCheck* info);
271 // Called on the database thread to retrieve chunks.
272 void GetAllChunksFromDatabase(GetChunksCallback callback);
274 // Called on the IO thread with the results of all chunks.
275 void OnGetAllChunksFromDatabase(const std::vector<SBListChunkRanges>& lists,
276 bool database_error,
277 GetChunksCallback callback);
279 // Called on the IO thread after the database reports that it added a chunk.
280 void OnAddChunksComplete(AddChunksCallback callback);
282 // Notification that the database is done loading its bloom filter. We may
283 // have had to queue checks until the database is ready, and if so, this
284 // checks them.
285 void DatabaseLoadComplete();
287 // Called on the database thread to add/remove chunks and host keys.
288 void AddDatabaseChunks(const std::string& list,
289 scoped_ptr<ScopedVector<SBChunkData> > chunks,
290 AddChunksCallback callback);
292 void DeleteDatabaseChunks(
293 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes);
295 void NotifyClientBlockingComplete(Client* client, bool proceed);
297 void DatabaseUpdateFinished(bool update_succeeded);
299 // Called on the db thread to close the database. See CloseDatabase().
300 void OnCloseDatabase();
302 // Runs on the db thread to reset the database. We assume that resetting the
303 // database is a synchronous operation.
304 void OnResetDatabase();
306 // Internal worker function for processing full hashes.
307 void OnHandleGetHashResults(SafeBrowsingCheck* check,
308 const std::vector<SBFullHashResult>& full_hashes);
310 // Run one check against |full_hashes|. Returns |true| if the check
311 // finds a match in |full_hashes|.
312 bool HandleOneCheck(SafeBrowsingCheck* check,
313 const std::vector<SBFullHashResult>& full_hashes);
315 // Invoked by CheckDownloadUrl. It checks the download URL on
316 // safe_browsing_thread_.
317 void CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check);
319 // The callback function when a safebrowsing check is timed out. Client will
320 // be notified that the safebrowsing check is SAFE when this happens.
321 void TimeoutCallback(SafeBrowsingCheck* check);
323 // Calls the Client's callback on IO thread after CheckDownloadUrl finishes.
324 void CheckDownloadUrlDone(SafeBrowsingCheck* check);
326 // Checks all extension ID hashes on safe_browsing_thread_.
327 void CheckExtensionIDsOnSBThread(SafeBrowsingCheck* check);
329 // Helper function that calls safe browsing client and cleans up |checks_|.
330 void SafeBrowsingCheckDone(SafeBrowsingCheck* check);
332 // Helper function to set |check| with default values and start a safe
333 // browsing check with timeout of |timeout|. |task| will be called on
334 // success, otherwise TimeoutCallback will be called.
335 void StartSafeBrowsingCheck(SafeBrowsingCheck* check,
336 const base::Closure& task);
338 // SafeBrowsingProtocolManageDelegate override
339 void ResetDatabase() override;
340 void UpdateStarted() override;
341 void UpdateFinished(bool success) override;
342 void GetChunks(GetChunksCallback callback) override;
343 void AddChunks(const std::string& list,
344 scoped_ptr<ScopedVector<SBChunkData>> chunks,
345 AddChunksCallback callback) override;
346 void DeleteChunks(
347 scoped_ptr<std::vector<SBChunkDelete>> chunk_deletes) override;
349 scoped_refptr<SafeBrowsingService> sb_service_;
351 CurrentChecks checks_;
353 // Used for issuing only one GetHash request for a given prefix.
354 GetHashRequests gethash_requests_;
356 // The persistent database. We don't use a scoped_ptr because it
357 // needs to be destroyed on a different thread than this object.
358 SafeBrowsingDatabase* database_;
360 // Lock used to prevent possible data races due to compiler optimizations.
361 mutable base::Lock database_lock_;
363 // Whether the service is running. 'enabled_' is used by the
364 // SafeBrowsingDatabaseManager on the IO thread during normal operations.
365 bool enabled_;
367 // Indicate if download_protection is enabled by command switch
368 // so we allow this feature to be exersized.
369 bool enable_download_protection_;
371 // Indicate if client-side phishing detection whitelist should be enabled
372 // or not.
373 bool enable_csd_whitelist_;
375 // Indicate if the download whitelist should be enabled or not.
376 bool enable_download_whitelist_;
378 // Indicate if the extension blacklist should be enabled.
379 bool enable_extension_blacklist_;
381 // Indicate if the side effect free whitelist should be enabled.
382 bool enable_side_effect_free_whitelist_;
384 // Indicate if the csd malware IP blacklist should be enabled.
385 bool enable_ip_blacklist_;
387 // Indicate if the unwanted software blacklist should be enabled.
388 bool enable_unwanted_software_blacklist_;
390 // The SafeBrowsing thread that runs database operations.
392 // Note: Functions that run on this thread should run synchronously and return
393 // to the IO thread, not post additional tasks back to this thread, lest we
394 // cause a race condition at shutdown time that leads to a database leak.
395 scoped_ptr<base::Thread> safe_browsing_thread_;
397 // Indicates if we're currently in an update cycle.
398 bool update_in_progress_;
400 // When true, newly fetched chunks may not in the database yet since the
401 // database is still updating.
402 bool database_update_in_progress_;
404 // Indicates if we're in the midst of trying to close the database. If this
405 // is true, nothing on the IO thread should access the database.
406 bool closing_database_;
408 std::deque<QueuedCheck> queued_checks_;
410 // Timeout to use for safe browsing checks.
411 base::TimeDelta check_timeout_;
413 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManager);
416 #endif // CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_