| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // The Safe Browsing service is responsible for downloading anti-phishing and | 5 // The Safe Browsing service is responsible for downloading anti-phishing and |
| 6 // anti-malware tables and checking urls against them. | 6 // anti-malware tables and checking urls against them. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ |
| 9 #define CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ |
| 10 | 10 |
| 11 #include <deque> | 11 #include <deque> |
| 12 #include <map> |
| 12 #include <set> | 13 #include <set> |
| 13 #include <string> | 14 #include <string> |
| 14 #include <vector> | 15 #include <vector> |
| 15 | 16 |
| 16 #include "base/callback.h" | 17 #include "base/callback.h" |
| 17 #include "base/hash_tables.h" | 18 #include "base/hash_tables.h" |
| 18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 21 #include "base/time.h" | 22 #include "base/time.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 40 class DownloadProtectionService; | 41 class DownloadProtectionService; |
| 41 } | 42 } |
| 42 | 43 |
| 43 // Construction needs to happen on the main thread. | 44 // Construction needs to happen on the main thread. |
| 44 class SafeBrowsingDatabaseManager | 45 class SafeBrowsingDatabaseManager |
| 45 : public base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>, | 46 : public base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>, |
| 46 public SafeBrowsingProtocolManagerDelegate { | 47 public SafeBrowsingProtocolManagerDelegate { |
| 47 public: | 48 public: |
| 48 class Client; | 49 class Client; |
| 49 | 50 |
| 50 // Bundle of SafeBrowsing state for one URL or hash prefix check. | 51 // Bundle of SafeBrowsing state while performing a URL or hash prefix check. |
| 51 struct SafeBrowsingCheck { | 52 struct SafeBrowsingCheck { |
| 52 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); |
| 53 ~SafeBrowsingCheck(); | 62 ~SafeBrowsingCheck(); |
| 54 | 63 |
| 55 // Either |urls| or |prefix| is used to lookup database. | 64 // Either |urls| or |full_hashes| is used to lookup database. |*_results| |
| 65 // are parallel vectors containing the results. They are initialized to |
| 66 // contain SB_THREAT_TYPE_SAFE. |
| 56 std::vector<GURL> urls; | 67 std::vector<GURL> urls; |
| 57 scoped_ptr<SBFullHash> full_hash; | 68 std::vector<SBThreatType> url_results; |
| 69 std::vector<SBFullHash> full_hashes; |
| 70 std::vector<SBThreatType> full_hash_results; |
| 58 | 71 |
| 59 Client* client; | 72 Client* client; |
| 60 bool need_get_hash; | 73 bool need_get_hash; |
| 61 base::TimeTicks start; // When check was sent to SB service. | 74 base::TimeTicks start; // When check was sent to SB service. |
| 62 SBThreatType threat_type; | 75 safe_browsing_util::ListType check_type; // See comment in constructor. |
| 63 bool is_download; // If this check for download url or hash. | |
| 64 std::vector<SBPrefix> prefix_hits; | 76 std::vector<SBPrefix> prefix_hits; |
| 65 std::vector<SBFullHashResult> full_hits; | 77 std::vector<SBFullHashResult> full_hits; |
| 66 | 78 |
| 67 // Vends weak pointers for TimeoutCallback(). If the response is | 79 // Vends weak pointers for TimeoutCallback(). If the response is |
| 68 // received before the timeout fires, factory is destructed and | 80 // received before the timeout fires, factory is destructed and |
| 69 // the timeout won't be fired. | 81 // the timeout won't be fired. |
| 70 // TODO(lzheng): We should consider to use this time out check | 82 // TODO(lzheng): We should consider to use this time out check |
| 71 // for browsing too (instead of implementin in | 83 // for browsing too (instead of implementin in |
| 72 // safe_browsing_resource_handler.cc). | 84 // safe_browsing_resource_handler.cc). |
| 73 scoped_ptr<base::WeakPtrFactory< | 85 scoped_ptr<base::WeakPtrFactory< |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 friend class SafeBrowsingServerTest; | 193 friend class SafeBrowsingServerTest; |
| 182 friend class SafeBrowsingServiceTest; | 194 friend class SafeBrowsingServiceTest; |
| 183 friend class SafeBrowsingServiceTestHelper; | 195 friend class SafeBrowsingServiceTestHelper; |
| 184 | 196 |
| 185 typedef std::set<SafeBrowsingCheck*> CurrentChecks; | 197 typedef std::set<SafeBrowsingCheck*> CurrentChecks; |
| 186 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; | 198 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; |
| 187 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; | 199 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; |
| 188 | 200 |
| 189 // Clients that we've queued up for checking later once the database is ready. | 201 // Clients that we've queued up for checking later once the database is ready. |
| 190 struct QueuedCheck { | 202 struct QueuedCheck { |
| 203 safe_browsing_util::ListType check_type; |
| 191 Client* client; | 204 Client* client; |
| 192 GURL url; | 205 GURL url; |
| 193 base::TimeTicks start; // When check was queued. | 206 base::TimeTicks start; // When check was queued. |
| 194 }; | 207 }; |
| 195 | 208 |
| 196 // Called to stop operations on the io_thread. This may be called multiple | 209 // Called to stop operations on the io_thread. This may be called multiple |
| 197 // times during the life of the DatabaseManager. Should be called on IO | 210 // times during the life of the DatabaseManager. Should be called on IO |
| 198 // thread. | 211 // thread. |
| 199 void DoStopOnIOThread(); | 212 void DoStopOnIOThread(); |
| 200 | 213 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 // Calls the Client's callback on IO thread after CheckDownloadUrl finishes. | 301 // Calls the Client's callback on IO thread after CheckDownloadUrl finishes. |
| 289 void CheckDownloadUrlDone(SafeBrowsingCheck* check); | 302 void CheckDownloadUrlDone(SafeBrowsingCheck* check); |
| 290 | 303 |
| 291 // Calls the Client's callback on IO thread after CheckDownloadHash finishes. | 304 // Calls the Client's callback on IO thread after CheckDownloadHash finishes. |
| 292 void CheckDownloadHashDone(SafeBrowsingCheck* check); | 305 void CheckDownloadHashDone(SafeBrowsingCheck* check); |
| 293 | 306 |
| 294 // Helper function that calls safe browsing client and cleans up |checks_|. | 307 // Helper function that calls safe browsing client and cleans up |checks_|. |
| 295 void SafeBrowsingCheckDone(SafeBrowsingCheck* check); | 308 void SafeBrowsingCheckDone(SafeBrowsingCheck* check); |
| 296 | 309 |
| 297 // Helper function to set |check| with default values and start a safe | 310 // Helper function to set |check| with default values and start a safe |
| 298 // browsing check with timeout of |timeout_ms|. |task| will be called upon | 311 // browsing check with timeout of |timeout|. |task| will be called on |
| 299 // success, otherwise TimeoutCallback will be called. | 312 // success, otherwise TimeoutCallback will be called. |
| 300 void StartDownloadCheck(SafeBrowsingCheck* check, | 313 void StartSafeBrowsingCheck(SafeBrowsingCheck* check, |
| 301 Client* client, | 314 const base::Closure& task); |
| 302 const base::Closure& task, | |
| 303 int64 timeout_ms); | |
| 304 | 315 |
| 305 // SafeBrowsingProtocolManageDelegate override | 316 // SafeBrowsingProtocolManageDelegate override |
| 306 virtual void ResetDatabase() OVERRIDE; | 317 virtual void ResetDatabase() OVERRIDE; |
| 307 virtual void UpdateStarted() OVERRIDE; | 318 virtual void UpdateStarted() OVERRIDE; |
| 308 virtual void UpdateFinished(bool success) OVERRIDE; | 319 virtual void UpdateFinished(bool success) OVERRIDE; |
| 309 virtual void GetChunks(GetChunksCallback callback) OVERRIDE; | 320 virtual void GetChunks(GetChunksCallback callback) OVERRIDE; |
| 310 virtual void AddChunks(const std::string& list, SBChunkList* chunks, | 321 virtual void AddChunks(const std::string& list, SBChunkList* chunks, |
| 311 AddChunksCallback callback) OVERRIDE; | 322 AddChunksCallback callback) OVERRIDE; |
| 312 virtual void DeleteChunks( | 323 virtual void DeleteChunks( |
| 313 std::vector<SBChunkDelete>* delete_chunks) OVERRIDE; | 324 std::vector<SBChunkDelete>* delete_chunks) OVERRIDE; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 // When true, newly fetched chunks may not in the database yet since the | 365 // When true, newly fetched chunks may not in the database yet since the |
| 355 // database is still updating. | 366 // database is still updating. |
| 356 bool database_update_in_progress_; | 367 bool database_update_in_progress_; |
| 357 | 368 |
| 358 // Indicates if we're in the midst of trying to close the database. If this | 369 // Indicates if we're in the midst of trying to close the database. If this |
| 359 // is true, nothing on the IO thread should access the database. | 370 // is true, nothing on the IO thread should access the database. |
| 360 bool closing_database_; | 371 bool closing_database_; |
| 361 | 372 |
| 362 std::deque<QueuedCheck> queued_checks_; | 373 std::deque<QueuedCheck> queued_checks_; |
| 363 | 374 |
| 364 // When download url check takes this long, client's callback will be called | 375 // Timeout to use for safe browsing checks. |
| 365 // without waiting for the result. | 376 base::TimeDelta check_timeout_; |
| 366 int64 download_urlcheck_timeout_ms_; | |
| 367 | |
| 368 // Similar to |download_urlcheck_timeout_ms_|, but for download hash checks. | |
| 369 int64 download_hashcheck_timeout_ms_; | |
| 370 | 377 |
| 371 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManager); | 378 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManager); |
| 372 }; | 379 }; |
| 373 | 380 |
| 374 #endif // CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ | 381 #endif // CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ |
| OLD | NEW |