Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(599)

Side by Side Diff: chrome/browser/safe_browsing/local_database_manager.h

Issue 1110723002: Split to SafeBrowsingDatabaseManager into Local* and Remote*. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Respond to review. Tweak comments and list initializer. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // Safe Browsing Database Manager implementation that manages a local
6 // anti-malware tables and checking urls against them. 6 // database. This is used by Desktop Chromium.
7 7
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ 9 #define CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_
10 10
11 #include <deque> 11 #include <deque>
12 #include <map> 12 #include <map>
13 #include <set> 13 #include <set>
14 #include <string> 14 #include <string>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/callback.h" 17 #include "base/callback.h"
18 #include "base/containers/hash_tables.h" 18 #include "base/containers/hash_tables.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h" 20 #include "base/memory/scoped_ptr.h"
21 #include "base/memory/weak_ptr.h"
21 #include "base/synchronization/lock.h" 22 #include "base/synchronization/lock.h"
22 #include "base/time/time.h" 23 #include "base/time/time.h"
24 #include "chrome/browser/safe_browsing/database_manager.h"
23 #include "chrome/browser/safe_browsing/protocol_manager.h" 25 #include "chrome/browser/safe_browsing/protocol_manager.h"
24 #include "chrome/browser/safe_browsing/safe_browsing_util.h" 26 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
25 #include "url/gurl.h" 27 #include "url/gurl.h"
26 28
27 class SafeBrowsingService; 29 class SafeBrowsingService;
28 class SafeBrowsingDatabase; 30 class SafeBrowsingDatabase;
29 31
30 namespace net { 32 namespace net {
31 class URLRequestContext; 33 class URLRequestContext;
32 class URLRequestContextGetter; 34 class URLRequestContextGetter;
33 } 35 }
34 36
35 namespace safe_browsing { 37 namespace safe_browsing {
36 class ClientSideDetectionService; 38 class ClientSideDetectionService;
37 class DownloadProtectionService; 39 class DownloadProtectionService;
38 } 40 }
39 41
42 // Implemetation that manages a local database on disk.
43 //
40 // Construction needs to happen on the main thread. 44 // Construction needs to happen on the main thread.
41 class SafeBrowsingDatabaseManager 45 class LocalSafeBrowsingDatabaseManager
42 : public base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>, 46 : public SafeBrowsingDatabaseManager,
43 public SafeBrowsingProtocolManagerDelegate { 47 public SafeBrowsingProtocolManagerDelegate {
44 public: 48 public:
45 class Client;
46
47 // Bundle of SafeBrowsing state while performing a URL or hash prefix check. 49 // Bundle of SafeBrowsing state while performing a URL or hash prefix check.
48 struct SafeBrowsingCheck { 50 struct SafeBrowsingCheck {
49 // |check_type| should correspond to the type of item that is being 51 // |check_type| should correspond to the type of item that is being
50 // checked, either a URL or a binary hash/URL. We store this for two 52 // checked, either a URL or a binary hash/URL. We store this for two
51 // purposes: to know which of Client's methods to call when a result is 53 // purposes: to know which of Client's methods to call when a result is
52 // known, and for logging purposes. It *isn't* used to predict the response 54 // known, and for logging purposes. It *isn't* used to predict the response
53 // list type, that is information that the server gives us. 55 // list type, that is information that the server gives us.
54 SafeBrowsingCheck(const std::vector<GURL>& urls, 56 SafeBrowsingCheck(const std::vector<GURL>& urls,
55 const std::vector<SBFullHash>& full_hashes, 57 const std::vector<SBFullHash>& full_hashes,
56 Client* client, 58 Client* client,
57 safe_browsing_util::ListType check_type, 59 safe_browsing_util::ListType check_type,
58 const std::vector<SBThreatType>& expected_threats); 60 const std::vector<SBThreatType>& expected_threats);
59 ~SafeBrowsingCheck(); 61 ~SafeBrowsingCheck();
60 62
61 // Either |urls| or |full_hashes| is used to lookup database. |*_results| 63 // Either |urls| or |full_hashes| is used to lookup database. |*_results|
62 // are parallel vectors containing the results. They are initialized to 64 // are parallel vectors containing the results. They are initialized to
63 // contain SB_THREAT_TYPE_SAFE. 65 // contain SB_THREAT_TYPE_SAFE.
64 std::vector<GURL> urls; 66 std::vector<GURL> urls;
65 std::vector<SBThreatType> url_results; 67 std::vector<SBThreatType> url_results;
66 std::vector<std::string> url_metadata; 68 std::vector<std::string> url_metadata;
67 std::vector<SBFullHash> full_hashes; 69 std::vector<SBFullHash> full_hashes;
68 std::vector<SBThreatType> full_hash_results; 70 std::vector<SBThreatType> full_hash_results;
69 71
70 Client* client; 72 SafeBrowsingDatabaseManager::Client* client;
71 bool need_get_hash; 73 bool need_get_hash;
72 base::TimeTicks start; // When check was sent to SB service. 74 base::TimeTicks start; // When check was sent to SB service.
73 safe_browsing_util::ListType check_type; // See comment in constructor. 75 safe_browsing_util::ListType check_type; // See comment in constructor.
74 std::vector<SBThreatType> expected_threats; 76 std::vector<SBThreatType> expected_threats;
75 std::vector<SBPrefix> prefix_hits; 77 std::vector<SBPrefix> prefix_hits;
76 std::vector<SBFullHashResult> cache_hits; 78 std::vector<SBFullHashResult> cache_hits;
77 79
80 // Invoke one of client's callbacks with these results.
81 void OnSafeBrowsingResult();
82
78 // Vends weak pointers for async callbacks on the IO thread, such as 83 // Vends weak pointers for async callbacks on the IO thread, such as
79 // timeout checks and replies from checks performed on the SB task runner. 84 // timeout checks and replies from checks performed on the SB task runner.
80 // TODO(lzheng): We should consider to use this time out check 85 // TODO(lzheng): We should consider to use this time out check
81 // for browsing too (instead of implementing in 86 // for browsing too (instead of implementing in
82 // safe_browsing_resource_handler.cc). 87 // safe_browsing_resource_handler.cc).
83 scoped_ptr<base::WeakPtrFactory< 88 scoped_ptr<base::WeakPtrFactory<LocalSafeBrowsingDatabaseManager>>
84 SafeBrowsingDatabaseManager> > weak_ptr_factory_; 89 weak_ptr_factory_;
85 90
86 private: 91 private:
87 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck); 92 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck);
88 }; 93 };
89 94
90 class Client {
91 public:
92 void OnSafeBrowsingResult(const SafeBrowsingCheck& check);
93
94 protected:
95 virtual ~Client() {}
96
97 // Called when the result of checking a browse URL is known.
98 virtual void OnCheckBrowseUrlResult(const GURL& url,
99 SBThreatType threat_type,
100 const std::string& metadata) {}
101
102 // Called when the result of checking a download URL is known.
103 virtual void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
104 SBThreatType threat_type) {}
105
106 // Called when the result of checking a set of extensions is known.
107 virtual void OnCheckExtensionsResult(
108 const std::set<std::string>& threats) {}
109 };
110
111 // Creates the safe browsing service. Need to initialize before using. 95 // Creates the safe browsing service. Need to initialize before using.
112 explicit SafeBrowsingDatabaseManager( 96 explicit LocalSafeBrowsingDatabaseManager(
113 const scoped_refptr<SafeBrowsingService>& service); 97 const scoped_refptr<SafeBrowsingService>& service);
114 98
115 // Returns true if the url's scheme can be checked. 99 //
116 bool CanCheckUrl(const GURL& url) const; 100 // SafeBrowsingDatabaseManager overrides
101 //
117 102
118 // Returns whether download protection is enabled. 103 bool CanCheckUrl(const GURL& url) const override;
119 bool download_protection_enabled() const {
120 return enable_download_protection_;
121 }
122 104
123 // Called on the IO thread to check if the given url is safe or not. If we 105 bool CheckBrowseUrl(const GURL& url, Client* client) override;
124 // can synchronously determine that the url is safe, CheckUrl returns true. 106 bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
125 // Otherwise it returns false, and "client" is called asynchronously with the 107 Client* client) override;
126 // result when it is ready. 108 bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
127 virtual bool CheckBrowseUrl(const GURL& url, Client* client); 109 Client* client) override;
110 bool MatchCsdWhitelistUrl(const GURL& url) override;
111 bool MatchMalwareIP(const std::string& ip_address) override;
112 bool MatchDownloadWhitelistUrl(const GURL& url) override;
113 bool MatchDownloadWhitelistString(const std::string& str) override;
114 bool MatchInclusionWhitelistUrl(const GURL& url) override;
115 bool IsMalwareKillSwitchOn() override;
116 bool IsCsdWhitelistKillSwitchOn() override;
117 void CancelCheck(Client* client) override;
118 void StartOnIOThread() override;
119 void StopOnIOThread(bool shutdown) override;
120 bool download_protection_enabled() const override;
128 121
129 // Check if the prefix for |url| is in safebrowsing download add lists. 122 protected:
130 // Result will be passed to callback in |client|. 123 ~LocalSafeBrowsingDatabaseManager() override;
131 virtual bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
132 Client* client);
133 124
134 // Check which prefixes in |extension_ids| are in the safebrowsing blacklist. 125 // protected for tests.
135 // Returns true if not, false if further checks need to be made in which case 126 void NotifyDatabaseUpdateFinished(bool update_succeeded);
136 // the result will be passed to |client|.
137 virtual bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
138 Client* client);
139 127
140 // Check if the |url| matches any of the full-length hashes from the client- 128 private:
141 // side phishing detection whitelist. Returns true if there was a match and
142 // false otherwise. To make sure we are conservative we will return true if
143 // an error occurs. This method must be called on the IO thread.
144 virtual bool MatchCsdWhitelistUrl(const GURL& url);
145
146 // Check if the given IP address (either IPv4 or IPv6) matches the malware
147 // IP blacklist.
148 virtual bool MatchMalwareIP(const std::string& ip_address);
149
150 // Check if the |url| matches any of the full-length hashes from the download
151 // whitelist. Returns true if there was a match and false otherwise. To make
152 // sure we are conservative we will return true if an error occurs. This
153 // method must be called on the IO thread.
154 virtual bool MatchDownloadWhitelistUrl(const GURL& url);
155
156 // Check if |str| matches any of the full-length hashes from the download
157 // whitelist. Returns true if there was a match and false otherwise. To make
158 // sure we are conservative we will return true if an error occurs. This
159 // method must be called on the IO thread.
160 virtual bool MatchDownloadWhitelistString(const std::string& str);
161
162 // Check if the |url| matches any of the full-length hashes from the off-
163 // domain inclusion whitelist. Returns true if there was a match and false
164 // otherwise. To make sure we are conservative, we will return true if an
165 // error occurs. This method must be called on the IO thread.
166 virtual bool MatchInclusionWhitelistUrl(const GURL& url);
167
168 // Check if the CSD malware IP matching kill switch is turned on.
169 virtual bool IsMalwareKillSwitchOn();
170
171 // Check if the CSD whitelist kill switch is turned on.
172 virtual bool IsCsdWhitelistKillSwitchOn();
173
174 // Called on the IO thread to cancel a pending check if the result is no
175 // longer needed.
176 void CancelCheck(Client* client);
177
178 // Called on the IO thread when the SafeBrowsingProtocolManager has received 129 // Called on the IO thread when the SafeBrowsingProtocolManager has received
179 // the full hash results for prefix hits detected in the database. 130 // the full hash results for prefix hits detected in the database.
180 void HandleGetHashResults(SafeBrowsingCheck* check, 131 void HandleGetHashResults(SafeBrowsingCheck* check,
181 const std::vector<SBFullHashResult>& full_hashes, 132 const std::vector<SBFullHashResult>& full_hashes,
182 const base::TimeDelta& cache_lifetime); 133 const base::TimeDelta& cache_lifetime);
183 134
184 // Called to initialize objects that are used on the io_thread. This may be 135 friend class base::RefCountedThreadSafe<LocalSafeBrowsingDatabaseManager>;
185 // called multiple times during the life of the DatabaseManager. Must be
186 // called on IO thread.
187 void StartOnIOThread();
188
189 // Called to stop or shutdown operations on the io_thread. This may be called
190 // multiple times during the life of the DatabaseManager. Must be called
191 // on IO thread. If shutdown is true, the manager is disabled permanently.
192 void StopOnIOThread(bool shutdown);
193
194 protected:
195 ~SafeBrowsingDatabaseManager() override;
196
197 // protected for tests.
198 void NotifyDatabaseUpdateFinished(bool update_succeeded);
199
200 private:
201 friend class base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>;
202 friend class SafeBrowsingServerTest; 136 friend class SafeBrowsingServerTest;
203 friend class SafeBrowsingServiceTest; 137 friend class SafeBrowsingServiceTest;
204 friend class SafeBrowsingServiceTestHelper; 138 friend class SafeBrowsingServiceTestHelper;
139 // TODO(nparker): Rename this test to LocalSafeBrowsingDatabaseManagerTest
205 friend class SafeBrowsingDatabaseManagerTest; 140 friend class SafeBrowsingDatabaseManagerTest;
206 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest, 141 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest,
207 GetUrlSeverestThreatType); 142 GetUrlSeverestThreatType);
208 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest, 143 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest,
209 ServiceStopWithPendingChecks); 144 ServiceStopWithPendingChecks);
210 145
211 typedef std::set<SafeBrowsingCheck*> CurrentChecks; 146 typedef std::set<SafeBrowsingCheck*> CurrentChecks;
212 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; 147 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors;
213 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; 148 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests;
214 149
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 330
396 // Indicates if we're in the midst of trying to close the database. If this 331 // Indicates if we're in the midst of trying to close the database. If this
397 // is true, nothing on the IO thread should access the database. 332 // is true, nothing on the IO thread should access the database.
398 bool closing_database_; 333 bool closing_database_;
399 334
400 std::deque<QueuedCheck> queued_checks_; 335 std::deque<QueuedCheck> queued_checks_;
401 336
402 // Timeout to use for safe browsing checks. 337 // Timeout to use for safe browsing checks.
403 base::TimeDelta check_timeout_; 338 base::TimeDelta check_timeout_;
404 339
405 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManager); 340 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager);
406 }; 341 }; // class LocalSafeBrowsingDatabaseManager
407 342
408 #endif // CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_ 343 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698