Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / safe_browsing / local_database_manager.h
blob55abaf67711d0af7917fabe3d225682a1841f9b4
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 // Safe Browsing Database Manager implementation that manages a local
6 // database. This is used by Desktop Chromium.
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_LOCAL_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/memory/weak_ptr.h"
22 #include "base/synchronization/lock.h"
23 #include "base/time/time.h"
24 #include "chrome/browser/safe_browsing/database_manager.h"
25 #include "chrome/browser/safe_browsing/protocol_manager.h"
26 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
27 #include "url/gurl.h"
29 class SafeBrowsingService;
30 class SafeBrowsingDatabase;
32 namespace net {
33 class URLRequestContext;
34 class URLRequestContextGetter;
37 namespace safe_browsing {
38 class ClientSideDetectionService;
39 class DownloadProtectionService;
42 // Implemetation that manages a local database on disk.
44 // Construction needs to happen on the main thread.
45 class LocalSafeBrowsingDatabaseManager
46 : public SafeBrowsingDatabaseManager,
47 public SafeBrowsingProtocolManagerDelegate {
48 public:
49 // Bundle of SafeBrowsing state while performing a URL or hash prefix check.
50 struct SafeBrowsingCheck {
51 // |check_type| should correspond to the type of item that is being
52 // checked, either a URL or a binary hash/URL. We store this for two
53 // purposes: to know which of Client's methods to call when a result is
54 // known, and for logging purposes. It *isn't* used to predict the response
55 // list type, that is information that the server gives us.
56 SafeBrowsingCheck(const std::vector<GURL>& urls,
57 const std::vector<SBFullHash>& full_hashes,
58 Client* client,
59 safe_browsing_util::ListType check_type,
60 const std::vector<SBThreatType>& expected_threats);
61 ~SafeBrowsingCheck();
63 // Either |urls| or |full_hashes| is used to lookup database. |*_results|
64 // are parallel vectors containing the results. They are initialized to
65 // contain SB_THREAT_TYPE_SAFE.
66 std::vector<GURL> urls;
67 std::vector<SBThreatType> url_results;
68 std::vector<std::string> url_metadata;
69 std::vector<SBFullHash> full_hashes;
70 std::vector<SBThreatType> full_hash_results;
72 SafeBrowsingDatabaseManager::Client* client;
73 bool is_extended_reporting;
74 bool need_get_hash;
75 base::TimeTicks start; // When check was sent to SB service.
76 safe_browsing_util::ListType check_type; // See comment in constructor.
77 std::vector<SBThreatType> expected_threats;
78 std::vector<SBPrefix> prefix_hits;
79 std::vector<SBFullHashResult> cache_hits;
81 // Invoke one of client's callbacks with these results.
82 void OnSafeBrowsingResult();
84 // Vends weak pointers for async callbacks on the IO thread, such as
85 // timeout checks and replies from checks performed on the SB task runner.
86 // TODO(lzheng): We should consider to use this time out check
87 // for browsing too (instead of implementing in
88 // safe_browsing_resource_handler.cc).
89 scoped_ptr<base::WeakPtrFactory<LocalSafeBrowsingDatabaseManager>>
90 weak_ptr_factory_;
92 private:
93 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck);
96 // Creates the safe browsing service. Need to initialize before using.
97 explicit LocalSafeBrowsingDatabaseManager(
98 const scoped_refptr<SafeBrowsingService>& service);
101 // SafeBrowsingDatabaseManager overrides
104 bool CanCheckUrl(const GURL& url) const override;
106 bool CheckBrowseUrl(const GURL& url, Client* client) override;
107 bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
108 Client* client) override;
109 bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
110 Client* client) override;
111 bool MatchCsdWhitelistUrl(const GURL& url) override;
112 bool MatchMalwareIP(const std::string& ip_address) override;
113 bool MatchDownloadWhitelistUrl(const GURL& url) override;
114 bool MatchDownloadWhitelistString(const std::string& str) override;
115 bool MatchInclusionWhitelistUrl(const GURL& url) override;
116 bool IsMalwareKillSwitchOn() override;
117 bool IsCsdWhitelistKillSwitchOn() override;
118 void CancelCheck(Client* client) override;
119 void StartOnIOThread() override;
120 void StopOnIOThread(bool shutdown) override;
121 bool download_protection_enabled() const override;
123 protected:
124 ~LocalSafeBrowsingDatabaseManager() override;
126 // protected for tests.
127 void NotifyDatabaseUpdateFinished(bool update_succeeded);
129 private:
130 // Called on the IO thread when the SafeBrowsingProtocolManager has received
131 // the full hash results for prefix hits detected in the database.
132 void HandleGetHashResults(SafeBrowsingCheck* check,
133 const std::vector<SBFullHashResult>& full_hashes,
134 const base::TimeDelta& cache_lifetime);
136 friend class base::RefCountedThreadSafe<LocalSafeBrowsingDatabaseManager>;
137 friend class SafeBrowsingServerTest;
138 friend class SafeBrowsingServiceTest;
139 friend class SafeBrowsingServiceTestHelper;
140 // TODO(nparker): Rename this test to LocalSafeBrowsingDatabaseManagerTest
141 friend class SafeBrowsingDatabaseManagerTest;
142 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest,
143 GetUrlSeverestThreatType);
144 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest,
145 ServiceStopWithPendingChecks);
147 typedef std::set<SafeBrowsingCheck*> CurrentChecks;
148 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors;
149 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests;
151 // Clients that we've queued up for checking later once the database is ready.
152 struct QueuedCheck {
153 QueuedCheck(const safe_browsing_util::ListType check_type,
154 Client* client,
155 const GURL& url,
156 const std::vector<SBThreatType>& expected_threats,
157 const base::TimeTicks& start);
158 ~QueuedCheck();
159 safe_browsing_util::ListType check_type;
160 Client* client;
161 GURL url;
162 std::vector<SBThreatType> expected_threats;
163 base::TimeTicks start; // When check was queued.
166 // Return the threat type of the severest entry in |full_hashes| which matches
167 // |hash|, or SAFE if none match.
168 static SBThreatType GetHashSeverestThreatType(
169 const SBFullHash& hash,
170 const std::vector<SBFullHashResult>& full_hashes);
172 // Given a URL, compare all the possible host + path full hashes to the set of
173 // provided full hashes. Returns the threat type of the severest matching
174 // result from |full_hashes|, or SAFE if none match.
175 static SBThreatType GetUrlSeverestThreatType(
176 const GURL& url,
177 const std::vector<SBFullHashResult>& full_hashes,
178 size_t* index);
180 // Called to stop operations on the io_thread. This may be called multiple
181 // times during the life of the DatabaseManager. Should be called on IO
182 // thread.
183 void DoStopOnIOThread();
185 // Returns whether |database_| exists and is accessible.
186 bool DatabaseAvailable() const;
188 // Called on the IO thread. If the database does not exist, queues up a call
189 // on the db thread to create it. Returns whether the database is available.
191 // Note that this is only needed outside the db thread, since functions on the
192 // db thread can call GetDatabase() directly.
193 bool MakeDatabaseAvailable();
195 // Should only be called on db thread as SafeBrowsingDatabase is not
196 // threadsafe.
197 SafeBrowsingDatabase* GetDatabase();
199 // Called on the IO thread with the check result.
200 void OnCheckDone(SafeBrowsingCheck* info);
202 // Called on the UI thread to prepare hash request.
203 void OnRequestFullHash(SafeBrowsingCheck* check);
205 // Called on the UI thread to determine if current profile is opted into
206 // extended reporting.
207 bool GetExtendedReporting();
209 // Called on the IO thread to request full hash.
210 void RequestFullHash(SafeBrowsingCheck* check);
212 // Called on the database thread to retrieve chunks.
213 void GetAllChunksFromDatabase(GetChunksCallback callback);
215 // Called on the UI thread to prepare GetAllChunksFromDatabase.
216 void BeforeGetAllChunksFromDatabase(
217 const std::vector<SBListChunkRanges>& lists,
218 bool database_error,
219 GetChunksCallback callback);
221 // Called on the IO thread with the results of all chunks.
222 void OnGetAllChunksFromDatabase(const std::vector<SBListChunkRanges>& lists,
223 bool database_error,
224 bool is_extended_reporting,
225 GetChunksCallback callback);
227 // Called on the IO thread after the database reports that it added a chunk.
228 void OnAddChunksComplete(AddChunksCallback callback);
230 // Notification that the database is done loading its bloom filter. We may
231 // have had to queue checks until the database is ready, and if so, this
232 // checks them.
233 void DatabaseLoadComplete();
235 // Called on the database thread to add/remove chunks and host keys.
236 void AddDatabaseChunks(const std::string& list,
237 scoped_ptr<ScopedVector<SBChunkData> > chunks,
238 AddChunksCallback callback);
240 void DeleteDatabaseChunks(
241 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes);
243 void NotifyClientBlockingComplete(Client* client, bool proceed);
245 void DatabaseUpdateFinished(bool update_succeeded);
247 // Called on the db thread to close the database. See CloseDatabase().
248 void OnCloseDatabase();
250 // Runs on the db thread to reset the database. We assume that resetting the
251 // database is a synchronous operation.
252 void OnResetDatabase();
254 // Internal worker function for processing full hashes.
255 void OnHandleGetHashResults(SafeBrowsingCheck* check,
256 const std::vector<SBFullHashResult>& full_hashes);
258 // Run one check against |full_hashes|. Returns |true| if the check
259 // finds a match in |full_hashes|.
260 bool HandleOneCheck(SafeBrowsingCheck* check,
261 const std::vector<SBFullHashResult>& full_hashes);
263 // Invoked by CheckDownloadUrl. It checks the download URL on
264 // |safe_browsing_task_runner_|.
265 std::vector<SBPrefix> CheckDownloadUrlOnSBThread(
266 const std::vector<SBPrefix>& prefixes);
268 // The callback function when a safebrowsing check is timed out. Client will
269 // be notified that the safebrowsing check is SAFE when this happens.
270 void TimeoutCallback(SafeBrowsingCheck* check);
272 // Calls the Client's callback on IO thread after CheckDownloadUrl finishes.
273 void OnAsyncCheckDone(SafeBrowsingCheck* check,
274 const std::vector<SBPrefix>& prefix_hits);
276 // Checks all extension ID hashes on |safe_browsing_task_runner_|.
277 std::vector<SBPrefix> CheckExtensionIDsOnSBThread(
278 const std::vector<SBPrefix>& prefixes);
280 // Helper function that calls safe browsing client and cleans up |checks_|.
281 void SafeBrowsingCheckDone(SafeBrowsingCheck* check);
283 // Helper function to set |check| with default values and start a safe
284 // browsing check with timeout of |timeout|. |task| will be called on
285 // success, otherwise TimeoutCallback will be called.
286 void StartSafeBrowsingCheck(
287 SafeBrowsingCheck* check,
288 const base::Callback<std::vector<SBPrefix>(void)>& task);
290 // SafeBrowsingProtocolManageDelegate override
291 void ResetDatabase() override;
292 void UpdateStarted() override;
293 void UpdateFinished(bool success) override;
294 void GetChunks(GetChunksCallback callback) override;
295 void AddChunks(const std::string& list,
296 scoped_ptr<ScopedVector<SBChunkData>> chunks,
297 AddChunksCallback callback) override;
298 void DeleteChunks(
299 scoped_ptr<std::vector<SBChunkDelete>> chunk_deletes) override;
301 scoped_refptr<SafeBrowsingService> sb_service_;
303 CurrentChecks checks_;
305 // Used for issuing only one GetHash request for a given prefix.
306 GetHashRequests gethash_requests_;
308 // The persistent database. We don't use a scoped_ptr because it
309 // needs to be destroyed on a different thread than this object.
310 SafeBrowsingDatabase* database_;
312 // Lock used to prevent possible data races due to compiler optimizations.
313 mutable base::Lock database_lock_;
315 // Whether the service is running. 'enabled_' is used by the
316 // SafeBrowsingDatabaseManager on the IO thread during normal operations.
317 bool enabled_;
319 // Indicate if download_protection is enabled by command switch
320 // so we allow this feature to be exersized.
321 bool enable_download_protection_;
323 // Indicate if client-side phishing detection whitelist should be enabled
324 // or not.
325 bool enable_csd_whitelist_;
327 // Indicate if the download whitelist should be enabled or not.
328 bool enable_download_whitelist_;
330 // Indicate if the extension blacklist should be enabled.
331 bool enable_extension_blacklist_;
333 // Indicate if the csd malware IP blacklist should be enabled.
334 bool enable_ip_blacklist_;
336 // Indicate if the unwanted software blacklist should be enabled.
337 bool enable_unwanted_software_blacklist_;
339 // The sequenced task runner for running safe browsing database operations.
340 scoped_refptr<base::SequencedTaskRunner> safe_browsing_task_runner_;
342 // Indicates if we're currently in an update cycle.
343 bool update_in_progress_;
345 // When true, newly fetched chunks may not in the database yet since the
346 // database is still updating.
347 bool database_update_in_progress_;
349 // Indicates if we're in the midst of trying to close the database. If this
350 // is true, nothing on the IO thread should access the database.
351 bool closing_database_;
353 std::deque<QueuedCheck> queued_checks_;
355 // Timeout to use for safe browsing checks.
356 base::TimeDelta check_timeout_;
358 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager);
359 }; // class LocalSafeBrowsingDatabaseManager
361 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_