blob: c13180b1198c089491a73532386f37506fea630e [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294//
5// The Safe Browsing service is responsible for downloading anti-phishing and
6// anti-malware tables and checking urls against them.
7
[email protected]5da98afc2008-09-20 11:42:498#ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
9#define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
[email protected]32b76ef2010-07-26 23:08:2410#pragma once
initial.commit09911bf2008-07-26 23:55:2911
12#include <deque>
[email protected]12463cd2011-07-19 09:40:2013#include <map>
initial.commit09911bf2008-07-26 23:55:2914#include <set>
15#include <string>
16#include <vector>
17
[email protected]23144032008-09-08 20:51:3018#include "base/hash_tables.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/ref_counted.h"
20#include "base/memory/scoped_ptr.h"
[email protected]340673a2011-07-22 03:08:5221#include "base/observer_list.h"
[email protected]20305ec2011-01-21 04:55:5222#include "base/synchronization/lock.h"
[email protected]d6159e82011-03-02 20:59:3423#include "base/task.h"
initial.commit09911bf2008-07-26 23:55:2924#include "base/time.h"
25#include "chrome/browser/safe_browsing/safe_browsing_util.h"
[email protected]b864a412011-07-07 10:51:4826#include "content/common/notification_observer.h"
27#include "content/common/notification_registrar.h"
initial.commit09911bf2008-07-26 23:55:2928#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2929
[email protected]3882f1f2010-12-10 04:42:2630class MalwareDetails;
[email protected]12463cd2011-07-19 09:40:2031class PrefChangeRegistrar;
initial.commit09911bf2008-07-26 23:55:2932class PrefService;
initial.commit09911bf2008-07-26 23:55:2933class SafeBrowsingDatabase;
34class SafeBrowsingProtocolManager;
[email protected]a6522822010-11-30 01:07:4535class SafeBrowsingServiceFactory;
initial.commit09911bf2008-07-26 23:55:2936
[email protected]90a3b242009-11-13 00:28:5437namespace base {
38class Thread;
39}
40
[email protected]abe2c032011-03-31 18:49:3441namespace net {
42class URLRequestContextGetter;
43}
44
[email protected]9efb46692011-08-23 12:56:0545namespace safe_browsing {
46class ClientSideDetectionService;
47}
48
initial.commit09911bf2008-07-26 23:55:2949// Construction needs to happen on the main thread.
50class SafeBrowsingService
[email protected]b864a412011-07-07 10:51:4851 : public base::RefCountedThreadSafe<SafeBrowsingService>,
52 public NotificationObserver {
initial.commit09911bf2008-07-26 23:55:2953 public:
[email protected]9cadfb342011-02-16 01:59:1154 class Client;
initial.commit09911bf2008-07-26 23:55:2955 // Users of this service implement this interface to be notified
56 // asynchronously of the result.
57 enum UrlCheckResult {
[email protected]9cadfb342011-02-16 01:59:1158 SAFE,
initial.commit09911bf2008-07-26 23:55:2959 URL_PHISHING,
60 URL_MALWARE,
[email protected]9cadfb342011-02-16 01:59:1161 BINARY_MALWARE_URL, // Binary url leads to a malware.
62 BINARY_MALWARE_HASH, // Binary hash indicates this is a malware.
[email protected]9bfaa002011-05-26 22:20:2763
64 // Url detected by the client-side phishing model. Note that unlike the
65 // above values, this does not correspond to a downloaded list.
66 CLIENT_SIDE_PHISHING_URL,
initial.commit09911bf2008-07-26 23:55:2967 };
68
[email protected]cbab76d2008-10-13 22:42:4769 // Structure used to pass parameters between the IO and UI thread when
70 // interacting with the blocking page.
[email protected]1b277e72009-01-23 21:05:3471 struct UnsafeResource {
[email protected]38e08982010-10-22 17:28:4372 UnsafeResource();
73 ~UnsafeResource();
74
[email protected]cbab76d2008-10-13 22:42:4775 GURL url;
[email protected]293f19252010-08-18 20:01:1876 GURL original_url;
[email protected]9d99c662011-02-10 23:50:5777 std::vector<GURL> redirect_urls;
[email protected]462a0ff2011-06-02 17:15:3478 bool is_subresource;
[email protected]1b277e72009-01-23 21:05:3479 UrlCheckResult threat_type;
[email protected]c3ff89492008-11-11 02:17:5180 Client* client;
81 int render_process_host_id;
82 int render_view_id;
[email protected]cbab76d2008-10-13 22:42:4783 };
84
[email protected]9cadfb342011-02-16 01:59:1185 // Bundle of SafeBrowsing state for one URL or hash prefix check.
[email protected]90a3b242009-11-13 00:28:5486 struct SafeBrowsingCheck {
[email protected]38e08982010-10-22 17:28:4387 SafeBrowsingCheck();
88 ~SafeBrowsingCheck();
89
[email protected]8799e542011-04-20 03:47:3490 // Either |urls| or |prefix| is used to lookup database.
91 std::vector<GURL> urls;
[email protected]9cadfb342011-02-16 01:59:1192 scoped_ptr<SBFullHash> full_hash;
93
[email protected]90a3b242009-11-13 00:28:5494 Client* client;
95 bool need_get_hash;
[email protected]9d943372011-02-16 22:25:5896 base::TimeTicks start; // When check was sent to SB service.
[email protected]90a3b242009-11-13 00:28:5497 UrlCheckResult result;
[email protected]9cadfb342011-02-16 01:59:1198 bool is_download; // If this check for download url or hash.
[email protected]90a3b242009-11-13 00:28:5499 std::vector<SBPrefix> prefix_hits;
100 std::vector<SBFullHashResult> full_hits;
[email protected]38e08982010-10-22 17:28:43101
[email protected]d6159e82011-03-02 20:59:34102 // Task to make the callback to safebrowsing clients in case
103 // safebrowsing check takes too long to finish. Not owned by
104 // this class.
105 // TODO(lzheng): We should consider to use this time out check
106 // for browsing too (instead of implementin in
107 // safe_browsing_resource_handler.cc).
108 CancelableTask* timeout_task;
109
[email protected]38e08982010-10-22 17:28:43110 private:
111 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck);
[email protected]90a3b242009-11-13 00:28:54112 };
113
[email protected]340673a2011-07-22 03:08:52114 // Observer class can be used to get notified when a SafeBrowsing hit
115 // was found.
116 class Observer {
117 public:
118 // The |resource| must not be accessed after OnSafeBrowsingHit returns.
119 // This method will be called on the UI thread.
120 virtual void OnSafeBrowsingHit(const UnsafeResource& resource) = 0;
121
122 protected:
123 Observer() {}
124 virtual ~Observer() {}
125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(Observer);
128 };
129
[email protected]9cadfb342011-02-16 01:59:11130 class Client {
131 public:
132 virtual ~Client() {}
133
134 void OnSafeBrowsingResult(const SafeBrowsingCheck& check);
135
136 // Called when the user has made a decision about how to handle the
137 // SafeBrowsing interstitial page.
138 virtual void OnBlockingPageComplete(bool proceed) {}
139
140 protected:
141 // Called when the result of checking a browse URL is known.
142 virtual void OnBrowseUrlCheckResult(const GURL& url,
143 UrlCheckResult result) {}
144
145 // Called when the result of checking a download URL is known.
[email protected]8799e542011-04-20 03:47:34146 virtual void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain,
[email protected]9cadfb342011-02-16 01:59:11147 UrlCheckResult result) {}
148
149 // Called when the result of checking a download binary hash is known.
[email protected]05a463572011-03-04 23:01:05150 virtual void OnDownloadHashCheckResult(const std::string& hash,
[email protected]9cadfb342011-02-16 01:59:11151 UrlCheckResult result) {}
152 };
153
[email protected]a6522822010-11-30 01:07:45154 // Makes the passed |factory| the factory used to instanciate
155 // a SafeBrowsingService. Useful for tests.
156 static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
157 factory_ = factory;
158 }
159
160 // Create an instance of the safe browsing service.
161 static SafeBrowsingService* CreateSafeBrowsingService();
initial.commit09911bf2008-07-26 23:55:29162
[email protected]90a3b242009-11-13 00:28:54163 // Called on the UI thread to initialize the service.
[email protected]d83d03aa2009-11-02 21:44:37164 void Initialize();
initial.commit09911bf2008-07-26 23:55:29165
[email protected]90a3b242009-11-13 00:28:54166 // Called on the main thread to let us know that the io_thread is going away.
167 void ShutDown();
168
169 // Returns true if the url's scheme can be checked.
170 bool CanCheckUrl(const GURL& url) const;
171
[email protected]d6159e82011-03-02 20:59:34172 // Called on UI thread to decide if safe browsing related stats
173 // could be reported.
174 bool CanReportStats() const;
175
[email protected]42930de42011-01-13 23:26:28176 // Called on UI thread to decide if the download file's sha256 hash
177 // should be calculated for safebrowsing.
178 bool DownloadBinHashNeeded() const;
179
[email protected]90a3b242009-11-13 00:28:54180 // Called on the IO thread to check if the given url is safe or not. If we
181 // can synchronously determine that the url is safe, CheckUrl returns true.
182 // Otherwise it returns false, and "client" is called asynchronously with the
183 // result when it is ready.
[email protected]6df44fb62010-12-15 17:42:46184 virtual bool CheckBrowseUrl(const GURL& url, Client* client);
185
186 // Check if the prefix for |url| is in safebrowsing download add lists.
187 // Result will be passed to callback in |client|.
[email protected]8799e542011-04-20 03:47:34188 bool CheckDownloadUrl(const std::vector<GURL>& url_chain, Client* client);
[email protected]90a3b242009-11-13 00:28:54189
[email protected]9cadfb342011-02-16 01:59:11190 // Check if the prefix for |full_hash| is in safebrowsing binhash add lists.
191 // Result will be passed to callback in |client|.
192 virtual bool CheckDownloadHash(const std::string& full_hash, Client* client);
193
[email protected]d4b7a5d62011-03-09 19:04:51194 // Check if the |url| matches any of the full-length hashes from the
195 // client-side phishing detection whitelist. Returns true if there was a
196 // match and false otherwise. To make sure we are conservative we will return
197 // true if an error occurs. This method is expected to be called on the IO
198 // thread.
[email protected]1fd6dcc2011-03-21 21:51:50199 virtual bool MatchCsdWhitelistUrl(const GURL& url);
[email protected]d4b7a5d62011-03-09 19:04:51200
[email protected]90a3b242009-11-13 00:28:54201 // Called on the IO thread to cancel a pending check if the result is no
202 // longer needed.
203 void CancelCheck(Client* client);
204
205 // Called on the IO thread to display an interstitial page.
[email protected]293f19252010-08-18 20:01:18206 // |url| is the url of the resource that matches a safe browsing list.
207 // If the request contained a chain of redirects, |url| is the last url
208 // in the chain, and |original_url| is the first one (the root of the
209 // chain). Otherwise, |original_url| = |url|.
[email protected]024e3c8b2011-06-01 06:21:59210 void DisplayBlockingPage(const GURL& url,
211 const GURL& original_url,
212 const std::vector<GURL>& redirect_urls,
[email protected]462a0ff2011-06-02 17:15:34213 bool is_subresource,
[email protected]024e3c8b2011-06-01 06:21:59214 UrlCheckResult result,
215 Client* client,
216 int render_process_host_id,
217 int render_view_id);
218
219 // Same as above but gets invoked on the UI thread.
220 virtual void DoDisplayBlockingPage(const UnsafeResource& resource);
221
222 // Returns true if we already displayed an interstitial for that resource.
223 // Called on the UI thread.
224 bool IsWhitelisted(const UnsafeResource& resource);
[email protected]90a3b242009-11-13 00:28:54225
226 // Called on the IO thread when the SafeBrowsingProtocolManager has received
227 // the full hash results for prefix hits detected in the database.
228 void HandleGetHashResults(
229 SafeBrowsingCheck* check,
230 const std::vector<SBFullHashResult>& full_hashes,
231 bool can_cache);
232
233 // Called on the IO thread.
[email protected]7b1e37102010-03-08 21:43:16234 void HandleChunk(const std::string& list, SBChunkList* chunks);
[email protected]90a3b242009-11-13 00:28:54235 void HandleChunkDelete(std::vector<SBChunkDelete>* chunk_deletes);
236
237 // Update management. Called on the IO thread.
238 void UpdateStarted();
239 void UpdateFinished(bool update_succeeded);
[email protected]894c4e82010-06-29 21:53:18240 // Whether there is an update in progress. Called on the IO thread.
241 bool IsUpdateInProgress() const;
[email protected]90a3b242009-11-13 00:28:54242
243 // The blocking page on the UI thread has completed.
244 void OnBlockingPageDone(const std::vector<UnsafeResource>& resources,
245 bool proceed);
246
247 // Called on the UI thread when the SafeBrowsingProtocolManager has received
248 // updated MAC keys.
249 void OnNewMacKeys(const std::string& client_key,
250 const std::string& wrapped_key);
251
[email protected]90a3b242009-11-13 00:28:54252 bool enabled() const { return enabled_; }
253
[email protected]e2ceaa492011-05-03 20:07:33254 bool download_protection_enabled() const {
255 return enabled_ && enable_download_protection_;
256 }
257
[email protected]9efb46692011-08-23 12:56:05258 safe_browsing::ClientSideDetectionService*
259 safe_browsing_detection_service() const {
260 return csd_service_.get();
261 }
262
[email protected]90a3b242009-11-13 00:28:54263 // Preference handling.
264 static void RegisterPrefs(PrefService* prefs);
265
266 // Called on the IO thread to reset the database.
267 void ResetDatabase();
268
269 // Log the user perceived delay caused by SafeBrowsing. This delay is the time
270 // delta starting from when we would have started reading data from the
271 // network, and ending when the SafeBrowsing check completes indicating that
272 // the current page is 'safe'.
273 void LogPauseDelay(base::TimeDelta time);
274
[email protected]ca034a52011-04-19 05:14:36275 // Called on the IO thread by the MalwareDetails with the serialized
276 // protocol buffer, so the service can send it over.
277 virtual void SendSerializedMalwareDetails(const std::string& serialized);
[email protected]3882f1f2010-12-10 04:42:26278
[email protected]287b86b2011-02-26 00:11:35279 // Report hits to the unsafe contents (malware, phishing, unsafe download URL)
[email protected]30917cb2011-05-10 21:40:47280 // to the server. Can only be called on UI thread. If |post_data| is
281 // non-empty, the request will be sent as a POST instead of a GET.
[email protected]f01338e2011-05-31 20:00:28282 virtual void ReportSafeBrowsingHit(const GURL& malicious_url,
283 const GURL& page_url,
284 const GURL& referrer_url,
285 bool is_subresource,
286 UrlCheckResult threat_type,
287 const std::string& post_data);
[email protected]287b86b2011-02-26 00:11:35288
[email protected]340673a2011-07-22 03:08:52289 // Add and remove observers. These methods must be invoked on the UI thread.
290 void AddObserver(Observer* observer);
291 void RemoveObserver(Observer* remove);
292
[email protected]a6522822010-11-30 01:07:45293 protected:
294 // Creates the safe browsing service. Need to initialize before using.
295 SafeBrowsingService();
296
297 virtual ~SafeBrowsingService();
298
[email protected]90a3b242009-11-13 00:28:54299 private:
[email protected]a6522822010-11-30 01:07:45300 friend class SafeBrowsingServiceFactoryImpl;
301
[email protected]90a3b242009-11-13 00:28:54302 typedef std::set<SafeBrowsingCheck*> CurrentChecks;
303 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors;
304 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests;
305
306 // Used for whitelisting a render view when the user ignores our warning.
[email protected]93aa89c72010-10-20 21:32:04307 struct WhiteListedEntry;
[email protected]90a3b242009-11-13 00:28:54308
309 // Clients that we've queued up for checking later once the database is ready.
310 struct QueuedCheck {
311 Client* client;
312 GURL url;
[email protected]9d943372011-02-16 22:25:58313 base::TimeTicks start; // When check was queued.
[email protected]90a3b242009-11-13 00:28:54314 };
315
316 friend class base::RefCountedThreadSafe<SafeBrowsingService>;
[email protected]484d38f2010-07-16 19:06:29317 friend class SafeBrowsingServiceTest;
[email protected]90a3b242009-11-13 00:28:54318
initial.commit09911bf2008-07-26 23:55:29319 // Called to initialize objects that are used on the io_thread.
[email protected]d83d03aa2009-11-02 21:44:37320 void OnIOInitialize(const std::string& client_key,
[email protected]d11f5662009-11-12 20:52:56321 const std::string& wrapped_key,
[email protected]abe2c032011-03-31 18:49:34322 net::URLRequestContextGetter* request_context_getter);
initial.commit09911bf2008-07-26 23:55:29323
initial.commit09911bf2008-07-26 23:55:29324 // Called to shutdown operations on the io_thread.
325 void OnIOShutdown();
326
[email protected]03addd92009-11-17 21:38:10327 // Returns whether |database_| exists and is accessible.
[email protected]34094312009-12-03 21:40:26328 bool DatabaseAvailable() const;
[email protected]03addd92009-11-17 21:38:10329
330 // Called on the IO thread. If the database does not exist, queues up a call
331 // on the db thread to create it. Returns whether the database is available.
[email protected]cb2a67e2009-11-17 00:48:53332 //
333 // Note that this is only needed outside the db thread, since functions on the
334 // db thread can call GetDatabase() directly.
335 bool MakeDatabaseAvailable();
336
[email protected]b864a412011-07-07 10:51:48337 // Called on the IO thread to try to close the database, freeing the memory
338 // associated with it. The database will be automatically reopened as needed.
339 //
340 // NOTE: Actual database closure is asynchronous, and until it happens, the IO
341 // thread is not allowed to access it; may not actually trigger a close if one
342 // is already pending or doing so would cause problems.
343 void CloseDatabase();
344
initial.commit09911bf2008-07-26 23:55:29345 // Should only be called on db thread as SafeBrowsingDatabase is not
346 // threadsafe.
347 SafeBrowsingDatabase* GetDatabase();
348
initial.commit09911bf2008-07-26 23:55:29349 // Called on the IO thread with the check result.
350 void OnCheckDone(SafeBrowsingCheck* info);
351
352 // Called on the database thread to retrieve chunks.
353 void GetAllChunksFromDatabase();
354
[email protected]90a3b242009-11-13 00:28:54355 // Called on the IO thread with the results of all chunks.
initial.commit09911bf2008-07-26 23:55:29356 void OnGetAllChunksFromDatabase(const std::vector<SBListChunkRanges>& lists,
357 bool database_error);
358
359 // Called on the IO thread after the database reports that it added a chunk.
360 void OnChunkInserted();
361
[email protected]03addd92009-11-17 21:38:10362 // Notification that the database is done loading its bloom filter. We may
363 // have had to queue checks until the database is ready, and if so, this
364 // checks them.
[email protected]90a3b242009-11-13 00:28:54365 void DatabaseLoadComplete();
366
initial.commit09911bf2008-07-26 23:55:29367 // Called on the database thread to add/remove chunks and host keys.
368 // Callee will free the data when it's done.
369 void HandleChunkForDatabase(const std::string& list,
[email protected]7b1e37102010-03-08 21:43:16370 SBChunkList* chunks);
[email protected]90a3b242009-11-13 00:28:54371
initial.commit09911bf2008-07-26 23:55:29372 void DeleteChunks(std::vector<SBChunkDelete>* chunk_deletes);
373
374 static UrlCheckResult GetResultFromListname(const std::string& list_name);
375
376 void NotifyClientBlockingComplete(Client* client, bool proceed);
377
[email protected]613a03b2008-10-24 23:02:00378 void DatabaseUpdateFinished(bool update_succeeded);
[email protected]aad08752008-10-02 22:13:41379
[email protected]90a3b242009-11-13 00:28:54380 // Start up SafeBrowsing objects. This can be called at browser start, or when
381 // the user checks the "Enable SafeBrowsing" option in the Advanced options
382 // UI.
initial.commit09911bf2008-07-26 23:55:29383 void Start();
initial.commit09911bf2008-07-26 23:55:29384
[email protected]9efb46692011-08-23 12:56:05385 // Stops the SafeBrowsingService. This can be called when the safe browsing
386 // preference is disabled.
387 void Stop();
388
[email protected]cb2a67e2009-11-17 00:48:53389 // Called on the db thread to close the database. See CloseDatabase().
390 void OnCloseDatabase();
391
initial.commit09911bf2008-07-26 23:55:29392 // Runs on the db thread to reset the database. We assume that resetting the
393 // database is a synchronous operation.
394 void OnResetDatabase();
395
[email protected]c3ff89492008-11-11 02:17:51396 // Store in-memory the GetHash response. Runs on the database thread.
[email protected]200abc32008-09-05 01:44:33397 void CacheHashResults(const std::vector<SBPrefix>& prefixes,
398 const std::vector<SBFullHashResult>& full_hashes);
initial.commit09911bf2008-07-26 23:55:29399
400 // Internal worker function for processing full hashes.
401 void OnHandleGetHashResults(SafeBrowsingCheck* check,
402 const std::vector<SBFullHashResult>& full_hashes);
403
[email protected]b10942562010-12-28 23:36:09404 // Run one check against |full_hashes|. Returns |true| if the check
405 // finds a match in |full_hashes|.
406 bool HandleOneCheck(SafeBrowsingCheck* check,
initial.commit09911bf2008-07-26 23:55:29407 const std::vector<SBFullHashResult>& full_hashes);
408
[email protected]287b86b2011-02-26 00:11:35409 // Call protocol manager on IO thread to report hits of unsafe contents.
410 void ReportSafeBrowsingHitOnIOThread(const GURL& malicious_url,
411 const GURL& page_url,
412 const GURL& referrer_url,
413 bool is_subresource,
[email protected]30917cb2011-05-10 21:40:47414 UrlCheckResult threat_type,
415 const std::string& post_data);
[email protected]90a3b242009-11-13 00:28:54416
[email protected]9cadfb342011-02-16 01:59:11417 // Checks the download hash on safe_browsing_thread_.
418 void CheckDownloadHashOnSBThread(SafeBrowsingCheck* check);
419
[email protected]6df44fb62010-12-15 17:42:46420 // Invoked by CheckDownloadUrl. It checks the download URL on
421 // safe_browsing_thread_.
[email protected]9fc1e6c12010-12-22 21:07:05422 void CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check);
[email protected]6df44fb62010-12-15 17:42:46423
[email protected]d6159e82011-03-02 20:59:34424 // The callback function when a safebrowsing check is timed out. Client will
425 // be notified that the safebrowsing check is SAFE when this happens.
426 void TimeoutCallback(SafeBrowsingCheck* check);
427
[email protected]9cadfb342011-02-16 01:59:11428 // Calls the Client's callback on IO thread after CheckDownloadUrl finishes.
429 void CheckDownloadUrlDone(SafeBrowsingCheck* check);
430
431 // Calls the Client's callback on IO thread after CheckDownloadHash finishes.
432 void CheckDownloadHashDone(SafeBrowsingCheck* check);
[email protected]6df44fb62010-12-15 17:42:46433
[email protected]d6159e82011-03-02 20:59:34434 // Helper function that calls safe browsing client and cleans up |checks_|.
435 void SafeBrowsingCheckDone(SafeBrowsingCheck* check);
436
437 // Helper function to set |check| with default values and start a safe
438 // browsing check with timeout of |timeout_ms|. |task| will be called upon
439 // success, otherwise TimeoutCallback will be called.
440 void StartDownloadCheck(SafeBrowsingCheck* check,
441 Client* client,
442 CancelableTask* task,
443 int64 timeout_ms);
444
[email protected]024e3c8b2011-06-01 06:21:59445 // Adds the given entry to the whitelist. Called on the UI thread.
[email protected]4baff382011-06-22 17:38:31446 void UpdateWhitelist(const UnsafeResource& resource);
[email protected]024e3c8b2011-06-01 06:21:59447
[email protected]b864a412011-07-07 10:51:48448 // NotificationObserver override
[email protected]432115822011-07-10 15:52:27449 virtual void Observe(int type,
[email protected]b864a412011-07-07 10:51:48450 const NotificationSource& source,
451 const NotificationDetails& details) OVERRIDE;
452
[email protected]12463cd2011-07-19 09:40:20453 // Starts following the safe browsing preference on |pref_service|.
454 void AddPrefService(PrefService* pref_service);
455
456 // Stop following the safe browsing preference on |pref_service|.
457 void RemovePrefService(PrefService* pref_service);
458
459 // Checks if any profile is currently using the safe browsing service, and
460 // starts or stops the service accordingly.
461 void RefreshState();
462
[email protected]a6522822010-11-30 01:07:45463 // The factory used to instanciate a SafeBrowsingService object.
464 // Useful for tests, so they can provide their own implementation of
465 // SafeBrowsingService.
466 static SafeBrowsingServiceFactory* factory_;
467
initial.commit09911bf2008-07-26 23:55:29468 CurrentChecks checks_;
469
470 // Used for issuing only one GetHash request for a given prefix.
initial.commit09911bf2008-07-26 23:55:29471 GetHashRequests gethash_requests_;
472
[email protected]5388e2d2011-02-09 05:42:33473 // The persistent database. We don't use a scoped_ptr because it
474 // needs to be destructed on a different thread than this object.
initial.commit09911bf2008-07-26 23:55:29475 SafeBrowsingDatabase* database_;
476
[email protected]34094312009-12-03 21:40:26477 // Lock used to prevent possible data races due to compiler optimizations.
[email protected]20305ec2011-01-21 04:55:52478 mutable base::Lock database_lock_;
[email protected]34094312009-12-03 21:40:26479
initial.commit09911bf2008-07-26 23:55:29480 // Handles interaction with SafeBrowsing servers.
481 SafeBrowsingProtocolManager* protocol_manager_;
482
[email protected]024e3c8b2011-06-01 06:21:59483 // Only access this whitelist from the UI thread.
initial.commit09911bf2008-07-26 23:55:29484 std::vector<WhiteListedEntry> white_listed_entries_;
485
486 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
487 // on the IO thread during normal operations.
488 bool enabled_;
489
[email protected]6df44fb62010-12-15 17:42:46490 // Indicate if download_protection is enabled by command switch
491 // so we allow this feature to be exersized.
492 bool enable_download_protection_;
493
[email protected]d4b7a5d62011-03-09 19:04:51494 // Indicate if client-side phishing detection whitelist should be enabled
495 // or not.
496 bool enable_csd_whitelist_;
497
initial.commit09911bf2008-07-26 23:55:29498 // The SafeBrowsing thread that runs database operations.
[email protected]cb2a67e2009-11-17 00:48:53499 //
500 // Note: Functions that run on this thread should run synchronously and return
501 // to the IO thread, not post additional tasks back to this thread, lest we
502 // cause a race condition at shutdown time that leads to a database leak.
[email protected]f3724ea2009-05-08 22:09:56503 scoped_ptr<base::Thread> safe_browsing_thread_;
initial.commit09911bf2008-07-26 23:55:29504
[email protected]57119c3f2008-12-04 00:33:04505 // Indicates if we're currently in an update cycle.
506 bool update_in_progress_;
507
[email protected]ef5b4822010-09-09 16:45:49508 // When true, newly fetched chunks may not in the database yet since the
509 // database is still updating.
510 bool database_update_in_progress_;
511
[email protected]cb2a67e2009-11-17 00:48:53512 // Indicates if we're in the midst of trying to close the database. If this
513 // is true, nothing on the IO thread should access the database.
514 bool closing_database_;
515
[email protected]613a03b2008-10-24 23:02:00516 std::deque<QueuedCheck> queued_checks_;
517
[email protected]d6159e82011-03-02 20:59:34518 // When download url check takes this long, client's callback will be called
519 // without waiting for the result.
520 int64 download_urlcheck_timeout_ms_;
521
522 // Similar to |download_urlcheck_timeout_ms_|, but for download hash checks.
523 int64 download_hashcheck_timeout_ms_;
524
[email protected]340673a2011-07-22 03:08:52525 ObserverList<Observer> observer_list_;
526
[email protected]12463cd2011-07-19 09:40:20527 // Used to track purge memory notifications. Lives on the IO thread.
[email protected]b864a412011-07-07 10:51:48528 NotificationRegistrar registrar_;
529
[email protected]12463cd2011-07-19 09:40:20530 // Tracks existing PrefServices, and the safe browsing preference on each.
531 // This is used to determine if any profile is currently using the safe
532 // browsing service, and to start it up or shut it down accordingly.
533 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_;
534
535 // Used to track creation and destruction of profiles on the UI thread.
536 NotificationRegistrar prefs_registrar_;
537
[email protected]9efb46692011-08-23 12:56:05538 // The ClientSideDetectionService is managed by the SafeBrowsingService,
539 // since its running state and lifecycle depends on SafeBrowsingService's.
540 scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service_;
541
[email protected]613a03b2008-10-24 23:02:00542 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
initial.commit09911bf2008-07-26 23:55:29543};
544
[email protected]a6522822010-11-30 01:07:45545// Factory for creating SafeBrowsingService. Useful for tests.
546class SafeBrowsingServiceFactory {
547 public:
548 SafeBrowsingServiceFactory() { }
549 virtual ~SafeBrowsingServiceFactory() { }
550 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
551 private:
552 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
553};
554
[email protected]5da98afc2008-09-20 11:42:49555#endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_