[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // The Safe Browsing service is responsible for downloading anti-phishing and |
| 6 | // anti-malware tables and checking urls against them. |
| 7 | |
[email protected] | 5da98afc | 2008-09-20 11:42:49 | [diff] [blame] | 8 | #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
| 9 | #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 10 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
| 12 | #include <deque> |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 13 | #include <map> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <set> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 18 | #include "base/hash_tables.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 19 | #include "base/memory/ref_counted.h" |
| 20 | #include "base/memory/scoped_ptr.h" |
[email protected] | 340673a | 2011-07-22 03:08:52 | [diff] [blame] | 21 | #include "base/observer_list.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 22 | #include "base/synchronization/lock.h" |
[email protected] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 23 | #include "base/task.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | #include "base/time.h" |
| 25 | #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
[email protected] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 26 | #include "content/common/notification_observer.h" |
| 27 | #include "content/common/notification_registrar.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "googleurl/src/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 3882f1f | 2010-12-10 04:42:26 | [diff] [blame] | 30 | class MalwareDetails; |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 31 | class PrefChangeRegistrar; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | class PrefService; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | class SafeBrowsingDatabase; |
| 34 | class SafeBrowsingProtocolManager; |
[email protected] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 35 | class SafeBrowsingServiceFactory; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 37 | namespace base { |
| 38 | class Thread; |
| 39 | } |
| 40 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 41 | namespace net { |
| 42 | class URLRequestContextGetter; |
| 43 | } |
| 44 | |
[email protected] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 45 | namespace safe_browsing { |
| 46 | class ClientSideDetectionService; |
| 47 | } |
| 48 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | // Construction needs to happen on the main thread. |
| 50 | class SafeBrowsingService |
[email protected] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 51 | : public base::RefCountedThreadSafe<SafeBrowsingService>, |
| 52 | public NotificationObserver { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | public: |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 54 | class Client; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 55 | // Users of this service implement this interface to be notified |
| 56 | // asynchronously of the result. |
| 57 | enum UrlCheckResult { |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 58 | SAFE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | URL_PHISHING, |
| 60 | URL_MALWARE, |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 61 | BINARY_MALWARE_URL, // Binary url leads to a malware. |
| 62 | BINARY_MALWARE_HASH, // Binary hash indicates this is a malware. |
[email protected] | 9bfaa00 | 2011-05-26 22:20:27 | [diff] [blame] | 63 | |
| 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | }; |
| 68 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 69 | // Structure used to pass parameters between the IO and UI thread when |
| 70 | // interacting with the blocking page. |
[email protected] | 1b277e7 | 2009-01-23 21:05:34 | [diff] [blame] | 71 | struct UnsafeResource { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 72 | UnsafeResource(); |
| 73 | ~UnsafeResource(); |
| 74 | |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 75 | GURL url; |
[email protected] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 76 | GURL original_url; |
[email protected] | 9d99c66 | 2011-02-10 23:50:57 | [diff] [blame] | 77 | std::vector<GURL> redirect_urls; |
[email protected] | 462a0ff | 2011-06-02 17:15:34 | [diff] [blame] | 78 | bool is_subresource; |
[email protected] | 1b277e7 | 2009-01-23 21:05:34 | [diff] [blame] | 79 | UrlCheckResult threat_type; |
[email protected] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 80 | Client* client; |
| 81 | int render_process_host_id; |
| 82 | int render_view_id; |
[email protected] | cbab76d | 2008-10-13 22:42:47 | [diff] [blame] | 83 | }; |
| 84 | |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 85 | // Bundle of SafeBrowsing state for one URL or hash prefix check. |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 86 | struct SafeBrowsingCheck { |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 87 | SafeBrowsingCheck(); |
| 88 | ~SafeBrowsingCheck(); |
| 89 | |
[email protected] | 8799e54 | 2011-04-20 03:47:34 | [diff] [blame] | 90 | // Either |urls| or |prefix| is used to lookup database. |
| 91 | std::vector<GURL> urls; |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 92 | scoped_ptr<SBFullHash> full_hash; |
| 93 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 94 | Client* client; |
| 95 | bool need_get_hash; |
[email protected] | 9d94337 | 2011-02-16 22:25:58 | [diff] [blame] | 96 | base::TimeTicks start; // When check was sent to SB service. |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 97 | UrlCheckResult result; |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 98 | bool is_download; // If this check for download url or hash. |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 99 | std::vector<SBPrefix> prefix_hits; |
| 100 | std::vector<SBFullHashResult> full_hits; |
[email protected] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 101 | |
[email protected] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 102 | // 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] | 38e0898 | 2010-10-22 17:28:43 | [diff] [blame] | 110 | private: |
| 111 | DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 112 | }; |
| 113 | |
[email protected] | 340673a | 2011-07-22 03:08:52 | [diff] [blame] | 114 | // 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] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 130 | 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] | 8799e54 | 2011-04-20 03:47:34 | [diff] [blame] | 146 | virtual void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain, |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 147 | UrlCheckResult result) {} |
| 148 | |
| 149 | // Called when the result of checking a download binary hash is known. |
[email protected] | 05a46357 | 2011-03-04 23:01:05 | [diff] [blame] | 150 | virtual void OnDownloadHashCheckResult(const std::string& hash, |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 151 | UrlCheckResult result) {} |
| 152 | }; |
| 153 | |
[email protected] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 154 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 163 | // Called on the UI thread to initialize the service. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 164 | void Initialize(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 166 | // 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] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 172 | // Called on UI thread to decide if safe browsing related stats |
| 173 | // could be reported. |
| 174 | bool CanReportStats() const; |
| 175 | |
[email protected] | 42930de4 | 2011-01-13 23:26:28 | [diff] [blame] | 176 | // 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] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 180 | // 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] | 6df44fb6 | 2010-12-15 17:42:46 | [diff] [blame] | 184 | 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] | 8799e54 | 2011-04-20 03:47:34 | [diff] [blame] | 188 | bool CheckDownloadUrl(const std::vector<GURL>& url_chain, Client* client); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 189 | |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 190 | // 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] | d4b7a5d6 | 2011-03-09 19:04:51 | [diff] [blame] | 194 | // 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] | 1fd6dcc | 2011-03-21 21:51:50 | [diff] [blame] | 199 | virtual bool MatchCsdWhitelistUrl(const GURL& url); |
[email protected] | d4b7a5d6 | 2011-03-09 19:04:51 | [diff] [blame] | 200 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 201 | // 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] | 293f1925 | 2010-08-18 20:01:18 | [diff] [blame] | 206 | // |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] | 024e3c8b | 2011-06-01 06:21:59 | [diff] [blame] | 210 | void DisplayBlockingPage(const GURL& url, |
| 211 | const GURL& original_url, |
| 212 | const std::vector<GURL>& redirect_urls, |
[email protected] | 462a0ff | 2011-06-02 17:15:34 | [diff] [blame] | 213 | bool is_subresource, |
[email protected] | 024e3c8b | 2011-06-01 06:21:59 | [diff] [blame] | 214 | 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] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 225 | |
| 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] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 234 | void HandleChunk(const std::string& list, SBChunkList* chunks); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 235 | 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] | 894c4e8 | 2010-06-29 21:53:18 | [diff] [blame] | 240 | // Whether there is an update in progress. Called on the IO thread. |
| 241 | bool IsUpdateInProgress() const; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 242 | |
| 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] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 252 | bool enabled() const { return enabled_; } |
| 253 | |
[email protected] | e2ceaa49 | 2011-05-03 20:07:33 | [diff] [blame] | 254 | bool download_protection_enabled() const { |
| 255 | return enabled_ && enable_download_protection_; |
| 256 | } |
| 257 | |
[email protected] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 258 | safe_browsing::ClientSideDetectionService* |
| 259 | safe_browsing_detection_service() const { |
| 260 | return csd_service_.get(); |
| 261 | } |
| 262 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 263 | // 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] | ca034a5 | 2011-04-19 05:14:36 | [diff] [blame] | 275 | // 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] | 3882f1f | 2010-12-10 04:42:26 | [diff] [blame] | 278 | |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 279 | // Report hits to the unsafe contents (malware, phishing, unsafe download URL) |
[email protected] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 280 | // 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] | f01338e | 2011-05-31 20:00:28 | [diff] [blame] | 282 | 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] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 288 | |
[email protected] | 340673a | 2011-07-22 03:08:52 | [diff] [blame] | 289 | // 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] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 293 | protected: |
| 294 | // Creates the safe browsing service. Need to initialize before using. |
| 295 | SafeBrowsingService(); |
| 296 | |
| 297 | virtual ~SafeBrowsingService(); |
| 298 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 299 | private: |
[email protected] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 300 | friend class SafeBrowsingServiceFactoryImpl; |
| 301 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 302 | 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] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 307 | struct WhiteListedEntry; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 308 | |
| 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] | 9d94337 | 2011-02-16 22:25:58 | [diff] [blame] | 313 | base::TimeTicks start; // When check was queued. |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | friend class base::RefCountedThreadSafe<SafeBrowsingService>; |
[email protected] | 484d38f | 2010-07-16 19:06:29 | [diff] [blame] | 317 | friend class SafeBrowsingServiceTest; |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 318 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | // Called to initialize objects that are used on the io_thread. |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 320 | void OnIOInitialize(const std::string& client_key, |
[email protected] | d11f566 | 2009-11-12 20:52:56 | [diff] [blame] | 321 | const std::string& wrapped_key, |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 322 | net::URLRequestContextGetter* request_context_getter); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | // Called to shutdown operations on the io_thread. |
| 325 | void OnIOShutdown(); |
| 326 | |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 327 | // Returns whether |database_| exists and is accessible. |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 328 | bool DatabaseAvailable() const; |
[email protected] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 329 | |
| 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] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 332 | // |
| 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] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 337 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | // Should only be called on db thread as SafeBrowsingDatabase is not |
| 346 | // threadsafe. |
| 347 | SafeBrowsingDatabase* GetDatabase(); |
| 348 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | // 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] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 355 | // Called on the IO thread with the results of all chunks. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | 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] | 03addd9 | 2009-11-17 21:38:10 | [diff] [blame] | 362 | // 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] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 365 | void DatabaseLoadComplete(); |
| 366 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | // 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] | 7b1e3710 | 2010-03-08 21:43:16 | [diff] [blame] | 370 | SBChunkList* chunks); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 371 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | 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] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 378 | void DatabaseUpdateFinished(bool update_succeeded); |
[email protected] | aad0875 | 2008-10-02 22:13:41 | [diff] [blame] | 379 | |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 380 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | void Start(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 384 | |
[email protected] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 385 | // Stops the SafeBrowsingService. This can be called when the safe browsing |
| 386 | // preference is disabled. |
| 387 | void Stop(); |
| 388 | |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 389 | // Called on the db thread to close the database. See CloseDatabase(). |
| 390 | void OnCloseDatabase(); |
| 391 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | // 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] | c3ff8949 | 2008-11-11 02:17:51 | [diff] [blame] | 396 | // Store in-memory the GetHash response. Runs on the database thread. |
[email protected] | 200abc3 | 2008-09-05 01:44:33 | [diff] [blame] | 397 | void CacheHashResults(const std::vector<SBPrefix>& prefixes, |
| 398 | const std::vector<SBFullHashResult>& full_hashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 399 | |
| 400 | // Internal worker function for processing full hashes. |
| 401 | void OnHandleGetHashResults(SafeBrowsingCheck* check, |
| 402 | const std::vector<SBFullHashResult>& full_hashes); |
| 403 | |
[email protected] | b1094256 | 2010-12-28 23:36:09 | [diff] [blame] | 404 | // Run one check against |full_hashes|. Returns |true| if the check |
| 405 | // finds a match in |full_hashes|. |
| 406 | bool HandleOneCheck(SafeBrowsingCheck* check, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | const std::vector<SBFullHashResult>& full_hashes); |
| 408 | |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 409 | // 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] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 414 | UrlCheckResult threat_type, |
| 415 | const std::string& post_data); |
[email protected] | 90a3b24 | 2009-11-13 00:28:54 | [diff] [blame] | 416 | |
[email protected] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 417 | // Checks the download hash on safe_browsing_thread_. |
| 418 | void CheckDownloadHashOnSBThread(SafeBrowsingCheck* check); |
| 419 | |
[email protected] | 6df44fb6 | 2010-12-15 17:42:46 | [diff] [blame] | 420 | // Invoked by CheckDownloadUrl. It checks the download URL on |
| 421 | // safe_browsing_thread_. |
[email protected] | 9fc1e6c1 | 2010-12-22 21:07:05 | [diff] [blame] | 422 | void CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check); |
[email protected] | 6df44fb6 | 2010-12-15 17:42:46 | [diff] [blame] | 423 | |
[email protected] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 424 | // 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] | 9cadfb34 | 2011-02-16 01:59:11 | [diff] [blame] | 428 | // 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] | 6df44fb6 | 2010-12-15 17:42:46 | [diff] [blame] | 433 | |
[email protected] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 434 | // 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] | 024e3c8b | 2011-06-01 06:21:59 | [diff] [blame] | 445 | // Adds the given entry to the whitelist. Called on the UI thread. |
[email protected] | 4baff38 | 2011-06-22 17:38:31 | [diff] [blame] | 446 | void UpdateWhitelist(const UnsafeResource& resource); |
[email protected] | 024e3c8b | 2011-06-01 06:21:59 | [diff] [blame] | 447 | |
[email protected] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 448 | // NotificationObserver override |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 449 | virtual void Observe(int type, |
[email protected] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 450 | const NotificationSource& source, |
| 451 | const NotificationDetails& details) OVERRIDE; |
| 452 | |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 453 | // 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] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 463 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | CurrentChecks checks_; |
| 469 | |
| 470 | // Used for issuing only one GetHash request for a given prefix. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | GetHashRequests gethash_requests_; |
| 472 | |
[email protected] | 5388e2d | 2011-02-09 05:42:33 | [diff] [blame] | 473 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | SafeBrowsingDatabase* database_; |
| 476 | |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 477 | // Lock used to prevent possible data races due to compiler optimizations. |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 478 | mutable base::Lock database_lock_; |
[email protected] | 3409431 | 2009-12-03 21:40:26 | [diff] [blame] | 479 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 480 | // Handles interaction with SafeBrowsing servers. |
| 481 | SafeBrowsingProtocolManager* protocol_manager_; |
| 482 | |
[email protected] | 024e3c8b | 2011-06-01 06:21:59 | [diff] [blame] | 483 | // Only access this whitelist from the UI thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | 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] | 6df44fb6 | 2010-12-15 17:42:46 | [diff] [blame] | 490 | // 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] | d4b7a5d6 | 2011-03-09 19:04:51 | [diff] [blame] | 494 | // Indicate if client-side phishing detection whitelist should be enabled |
| 495 | // or not. |
| 496 | bool enable_csd_whitelist_; |
| 497 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 498 | // The SafeBrowsing thread that runs database operations. |
[email protected] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 499 | // |
| 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] | f3724ea | 2009-05-08 22:09:56 | [diff] [blame] | 503 | scoped_ptr<base::Thread> safe_browsing_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | |
[email protected] | 57119c3f | 2008-12-04 00:33:04 | [diff] [blame] | 505 | // Indicates if we're currently in an update cycle. |
| 506 | bool update_in_progress_; |
| 507 | |
[email protected] | ef5b482 | 2010-09-09 16:45:49 | [diff] [blame] | 508 | // 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] | cb2a67e | 2009-11-17 00:48:53 | [diff] [blame] | 512 | // 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] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 516 | std::deque<QueuedCheck> queued_checks_; |
| 517 | |
[email protected] | d6159e8 | 2011-03-02 20:59:34 | [diff] [blame] | 518 | // 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] | 340673a | 2011-07-22 03:08:52 | [diff] [blame] | 525 | ObserverList<Observer> observer_list_; |
| 526 | |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 527 | // Used to track purge memory notifications. Lives on the IO thread. |
[email protected] | b864a41 | 2011-07-07 10:51:48 | [diff] [blame] | 528 | NotificationRegistrar registrar_; |
| 529 | |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 530 | // 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] | 9efb4669 | 2011-08-23 12:56:05 | [diff] [blame] | 538 | // 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] | 613a03b | 2008-10-24 23:02:00 | [diff] [blame] | 542 | DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 543 | }; |
| 544 | |
[email protected] | a652282 | 2010-11-30 01:07:45 | [diff] [blame] | 545 | // Factory for creating SafeBrowsingService. Useful for tests. |
| 546 | class 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] | 5da98afc | 2008-09-20 11:42:49 | [diff] [blame] | 555 | #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |