Chromium Code Reviews| 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 // Safe Browsing Database Manager implementation that manages a local | 5 // Safe Browsing Database Manager implementation that manages a local |
| 6 // database. This is used by Desktop Chromium. | 6 // database. This is used by Desktop Chromium. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ |
| 9 #define CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
| 22 #include "base/macros.h" | 22 #include "base/macros.h" |
| 23 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 24 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
| 25 #include "base/memory/weak_ptr.h" | 25 #include "base/memory/weak_ptr.h" |
| 26 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
| 27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 28 #include "chrome/browser/safe_browsing/protocol_manager.h" | 28 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 29 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 29 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 30 #include "components/safe_browsing_db/database_manager.h" | 30 #include "components/safe_browsing_db/database_manager.h" |
| 31 #include "components/safe_browsing_db/safebrowsing.pb.h" | |
| 31 #include "components/safe_browsing_db/util.h" | 32 #include "components/safe_browsing_db/util.h" |
| 33 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | |
| 34 #include "components/safe_browsing_db/v4_update_protocol_manager.h" | |
| 32 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 33 | 36 |
| 34 namespace net { | 37 namespace net { |
| 35 class URLRequestContext; | 38 class URLRequestContext; |
| 36 class URLRequestContextGetter; | 39 class URLRequestContextGetter; |
| 37 } | 40 } |
| 38 | 41 |
| 39 namespace safe_browsing { | 42 namespace safe_browsing { |
| 40 | 43 |
| 41 class SafeBrowsingService; | 44 class SafeBrowsingService; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 std::vector<SBThreatType> url_results; | 78 std::vector<SBThreatType> url_results; |
| 76 std::vector<ThreatMetadata> url_metadata; | 79 std::vector<ThreatMetadata> url_metadata; |
| 77 std::vector<std::string> url_hit_hash; | 80 std::vector<std::string> url_hit_hash; |
| 78 std::vector<SBFullHash> full_hashes; | 81 std::vector<SBFullHash> full_hashes; |
| 79 std::vector<SBThreatType> full_hash_results; | 82 std::vector<SBThreatType> full_hash_results; |
| 80 | 83 |
| 81 SafeBrowsingDatabaseManager::Client* client; | 84 SafeBrowsingDatabaseManager::Client* client; |
| 82 bool is_extended_reporting; | 85 bool is_extended_reporting; |
| 83 bool need_get_hash; | 86 bool need_get_hash; |
| 84 base::TimeTicks start; // When check was sent to SB service. | 87 base::TimeTicks start; // When check was sent to SB service. |
| 85 ListType check_type; // See comment in constructor. | 88 ListType check_type; // See comment in constructor. |
| 86 std::vector<SBThreatType> expected_threats; | 89 std::vector<SBThreatType> expected_threats; |
| 87 std::vector<SBPrefix> prefix_hits; | 90 std::vector<SBPrefix> prefix_hits; |
| 88 std::vector<SBFullHashResult> cache_hits; | 91 std::vector<SBFullHashResult> cache_hits; |
| 89 | 92 |
| 90 // Invoke one of client's callbacks with these results. | 93 // Invoke one of client's callbacks with these results. |
| 91 void OnSafeBrowsingResult(); | 94 void OnSafeBrowsingResult(); |
| 92 | 95 |
| 93 // Vends weak pointers for async callbacks on the IO thread, such as | 96 // Vends weak pointers for async callbacks on the IO thread, such as |
| 94 // timeout checks and replies from checks performed on the SB task runner. | 97 // timeout checks and replies from checks performed on the SB task runner. |
| 95 // TODO(lzheng): We should consider to use this time out check | 98 // TODO(lzheng): We should consider to use this time out check |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 124 bool CheckResourceUrl(const GURL& url, Client* client) override; | 127 bool CheckResourceUrl(const GURL& url, Client* client) override; |
| 125 bool MatchCsdWhitelistUrl(const GURL& url) override; | 128 bool MatchCsdWhitelistUrl(const GURL& url) override; |
| 126 bool MatchMalwareIP(const std::string& ip_address) override; | 129 bool MatchMalwareIP(const std::string& ip_address) override; |
| 127 bool MatchDownloadWhitelistUrl(const GURL& url) override; | 130 bool MatchDownloadWhitelistUrl(const GURL& url) override; |
| 128 bool MatchDownloadWhitelistString(const std::string& str) override; | 131 bool MatchDownloadWhitelistString(const std::string& str) override; |
| 129 bool MatchInclusionWhitelistUrl(const GURL& url) override; | 132 bool MatchInclusionWhitelistUrl(const GURL& url) override; |
| 130 bool MatchModuleWhitelistString(const std::string& str) override; | 133 bool MatchModuleWhitelistString(const std::string& str) override; |
| 131 bool IsMalwareKillSwitchOn() override; | 134 bool IsMalwareKillSwitchOn() override; |
| 132 bool IsCsdWhitelistKillSwitchOn() override; | 135 bool IsCsdWhitelistKillSwitchOn() override; |
| 133 void CancelCheck(Client* client) override; | 136 void CancelCheck(Client* client) override; |
| 134 void StartOnIOThread( | 137 void StartOnIOThread(net::URLRequestContextGetter* request_context_getter, |
| 135 net::URLRequestContextGetter* request_context_getter, | 138 const V4ProtocolConfig& config) override; |
| 136 const V4ProtocolConfig& config) override; | |
| 137 void StopOnIOThread(bool shutdown) override; | 139 void StopOnIOThread(bool shutdown) override; |
| 138 bool download_protection_enabled() const override; | 140 bool download_protection_enabled() const override; |
| 139 | 141 |
| 140 protected: | 142 protected: |
| 141 ~LocalSafeBrowsingDatabaseManager() override; | 143 ~LocalSafeBrowsingDatabaseManager() override; |
| 142 | 144 |
| 143 // protected for tests. | 145 // protected for tests. |
| 144 void NotifyDatabaseUpdateFinished(bool update_succeeded); | 146 void NotifyDatabaseUpdateFinished(bool update_succeeded); |
| 145 | 147 |
| 146 private: | 148 private: |
| 147 // Called on the IO thread when the SafeBrowsingProtocolManager has received | 149 // Called on the IO thread when the SafeBrowsingProtocolManager has received |
| 148 // the full hash results for prefix hits detected in the database. | 150 // the full hash results for prefix hits detected in the database. |
| 149 void HandleGetHashResults(SafeBrowsingCheck* check, | 151 void HandleGetHashResults(SafeBrowsingCheck* check, |
| 150 const std::vector<SBFullHashResult>& full_hashes, | 152 const std::vector<SBFullHashResult>& full_hashes, |
| 151 const base::TimeDelta& cache_lifetime); | 153 const base::TimeDelta& cache_lifetime); |
| 152 | 154 |
| 153 friend class base::RefCountedThreadSafe<LocalSafeBrowsingDatabaseManager>; | 155 friend class base::RefCountedThreadSafe<LocalSafeBrowsingDatabaseManager>; |
| 154 friend class SafeBrowsingServerTest; | 156 friend class SafeBrowsingServerTest; |
| 155 friend class SafeBrowsingServiceTest; | 157 friend class SafeBrowsingServiceTest; |
| 156 friend class SafeBrowsingServiceTestHelper; | 158 friend class SafeBrowsingServiceTestHelper; |
| 157 friend class LocalDatabaseManagerTest; | 159 friend class LocalDatabaseManagerTest; |
| 158 FRIEND_TEST_ALL_PREFIXES(LocalDatabaseManagerTest, | 160 FRIEND_TEST_ALL_PREFIXES(LocalDatabaseManagerTest, GetUrlSeverestThreatType); |
| 159 GetUrlSeverestThreatType); | |
| 160 FRIEND_TEST_ALL_PREFIXES(LocalDatabaseManagerTest, | 161 FRIEND_TEST_ALL_PREFIXES(LocalDatabaseManagerTest, |
| 161 ServiceStopWithPendingChecks); | 162 ServiceStopWithPendingChecks); |
| 162 | 163 |
| 163 typedef std::set<SafeBrowsingCheck*> CurrentChecks; | 164 typedef std::set<SafeBrowsingCheck*> CurrentChecks; |
| 164 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; | 165 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; |
| 165 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; | 166 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; |
| 166 | 167 |
| 167 // Clients that we've queued up for checking later once the database is ready. | 168 // Clients that we've queued up for checking later once the database is ready. |
| 168 struct QueuedCheck { | 169 struct QueuedCheck { |
| 169 QueuedCheck(const ListType check_type, | 170 QueuedCheck(const ListType check_type, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 187 const std::vector<SBFullHashResult>& full_hashes); | 188 const std::vector<SBFullHashResult>& full_hashes); |
| 188 | 189 |
| 189 // Given a URL, compare all the possible host + path full hashes to the set of | 190 // Given a URL, compare all the possible host + path full hashes to the set of |
| 190 // provided full hashes. Returns the threat type of the severest matching | 191 // provided full hashes. Returns the threat type of the severest matching |
| 191 // result from |full_hashes|, or SAFE if none match. | 192 // result from |full_hashes|, or SAFE if none match. |
| 192 static SBThreatType GetUrlSeverestThreatType( | 193 static SBThreatType GetUrlSeverestThreatType( |
| 193 const GURL& url, | 194 const GURL& url, |
| 194 const std::vector<SBFullHashResult>& full_hashes, | 195 const std::vector<SBFullHashResult>& full_hashes, |
| 195 size_t* index); | 196 size_t* index); |
| 196 | 197 |
| 198 // Return whether we are running headless, i.e. on a bot. | |
| 199 static bool IsRunningHeadless(); | |
| 200 | |
| 201 void UpdateRequestCompleted(const std::vector<ListUpdateResponse>& responses); | |
| 202 | |
| 197 // Called to stop operations on the io_thread. This may be called multiple | 203 // Called to stop operations on the io_thread. This may be called multiple |
| 198 // times during the life of the DatabaseManager. Should be called on IO | 204 // times during the life of the DatabaseManager. Should be called on IO |
| 199 // thread. | 205 // thread. |
| 200 void DoStopOnIOThread(); | 206 void DoStopOnIOThread(); |
| 201 | 207 |
| 202 // Returns whether |database_| exists and is accessible. | 208 // Returns whether |database_| exists and is accessible. |
| 203 bool DatabaseAvailable() const; | 209 bool DatabaseAvailable() const; |
| 204 | 210 |
| 205 // Called on the IO thread. If the database does not exist, queues up a call | 211 // Called on the IO thread. If the database does not exist, queues up a call |
| 206 // on the db thread to create it. Returns whether the database is available. | 212 // on the db thread to create it. Returns whether the database is available. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 // checks them. | 255 // checks them. |
| 250 void DatabaseLoadComplete(); | 256 void DatabaseLoadComplete(); |
| 251 | 257 |
| 252 // Called on the database thread to add/remove chunks and host keys. | 258 // Called on the database thread to add/remove chunks and host keys. |
| 253 void AddDatabaseChunks( | 259 void AddDatabaseChunks( |
| 254 const std::string& list, | 260 const std::string& list, |
| 255 scoped_ptr<std::vector<scoped_ptr<SBChunkData>>> chunks, | 261 scoped_ptr<std::vector<scoped_ptr<SBChunkData>>> chunks, |
| 256 AddChunksCallback callback); | 262 AddChunksCallback callback); |
| 257 | 263 |
| 258 void DeleteDatabaseChunks( | 264 void DeleteDatabaseChunks( |
| 259 scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes); | 265 scoped_ptr<std::vector<SBChunkDelete>> chunk_deletes); |
| 260 | 266 |
| 261 void NotifyClientBlockingComplete(Client* client, bool proceed); | 267 void NotifyClientBlockingComplete(Client* client, bool proceed); |
| 262 | 268 |
| 263 void DatabaseUpdateFinished(bool update_succeeded); | 269 void DatabaseUpdateFinished(bool update_succeeded); |
| 264 | 270 |
| 265 // Called on the db thread to close the database. See CloseDatabase(). | 271 // Called on the db thread to close the database. See CloseDatabase(). |
| 266 void OnCloseDatabase(); | 272 void OnCloseDatabase(); |
| 267 | 273 |
| 268 // Runs on the db thread to reset the database. We assume that resetting the | 274 // Runs on the db thread to reset the database. We assume that resetting the |
| 269 // database is a synchronous operation. | 275 // database is a synchronous operation. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 373 | 379 |
| 374 // Indicates if we're in the midst of trying to close the database. If this | 380 // Indicates if we're in the midst of trying to close the database. If this |
| 375 // is true, nothing on the IO thread should access the database. | 381 // is true, nothing on the IO thread should access the database. |
| 376 bool closing_database_; | 382 bool closing_database_; |
| 377 | 383 |
| 378 std::deque<QueuedCheck> queued_checks_; | 384 std::deque<QueuedCheck> queued_checks_; |
| 379 | 385 |
| 380 // Timeout to use for safe browsing checks. | 386 // Timeout to use for safe browsing checks. |
| 381 base::TimeDelta check_timeout_; | 387 base::TimeDelta check_timeout_; |
| 382 | 388 |
| 389 // Stores the current status of the lists to download from the SafeBrowsing | |
| 390 // servers. | |
| 391 base::hash_map<UpdateListIdentifier, std::string> current_list_states_; | |
|
Nathan Parker
2016/04/01 01:04:52
Do you want this in the local_database_manager, or
vakh (use Gerrit instead)
2016/04/01 02:29:09
I want to keep this CL small so I don't want to cr
Nathan Parker
2016/04/01 17:56:58
Sure, though it's probably better to not add it to
vakh (use Gerrit instead)
2016/04/01 19:35:48
Good point. Removed. Will add it in the V4DBManage
| |
| 392 | |
| 393 // The protocol manager that downloads the hash prefix updates. | |
| 394 scoped_ptr<V4UpdateProtocolManager> v4_update_protocol_manager_; | |
| 395 | |
| 383 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager); | 396 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager); |
| 384 }; // class LocalSafeBrowsingDatabaseManager | 397 }; // class LocalSafeBrowsingDatabaseManager |
| 385 | 398 |
| 386 } // namespace safe_browsing | 399 } // namespace safe_browsing |
| 387 | 400 |
| 388 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ | 401 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ |
| OLD | NEW |