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 |
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/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
21 #include "base/memory/scoped_ptr.h" | 21 #include "base/memory/scoped_ptr.h" |
22 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
25 #include "chrome/browser/safe_browsing/database_manager.h" | 25 #include "chrome/browser/safe_browsing/database_manager.h" |
26 #include "chrome/browser/safe_browsing/protocol_manager.h" | 26 #include "chrome/browser/safe_browsing/protocol_manager.h" |
27 #include "components/safe_browsing_db/util.h" | 27 #include "components/safe_browsing_db/util.h" |
28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
29 | 29 |
30 class SafeBrowsingService; | |
31 class SafeBrowsingDatabase; | |
32 | |
33 namespace net { | 30 namespace net { |
34 class URLRequestContext; | 31 class URLRequestContext; |
35 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
36 } | 33 } |
37 | 34 |
38 namespace safe_browsing { | 35 namespace safe_browsing { |
| 36 |
| 37 class SafeBrowsingService; |
| 38 class SafeBrowsingDatabase; |
39 class ClientSideDetectionService; | 39 class ClientSideDetectionService; |
40 class DownloadProtectionService; | 40 class DownloadProtectionService; |
41 } | |
42 | 41 |
43 // Implemetation that manages a local database on disk. | 42 // Implemetation that manages a local database on disk. |
44 // | 43 // |
45 // Construction needs to happen on the main thread. | 44 // Construction needs to happen on the main thread. |
46 class LocalSafeBrowsingDatabaseManager | 45 class LocalSafeBrowsingDatabaseManager |
47 : public SafeBrowsingDatabaseManager, | 46 : public SafeBrowsingDatabaseManager, |
48 public SafeBrowsingProtocolManagerDelegate { | 47 public SafeBrowsingProtocolManagerDelegate { |
49 public: | 48 public: |
50 // 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. |
51 struct SafeBrowsingCheck { | 50 struct SafeBrowsingCheck { |
52 // |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 |
53 // 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 |
54 // 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 |
55 // 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 |
56 // list type, that is information that the server gives us. | 55 // list type, that is information that the server gives us. |
57 SafeBrowsingCheck(const std::vector<GURL>& urls, | 56 SafeBrowsingCheck(const std::vector<GURL>& urls, |
58 const std::vector<SBFullHash>& full_hashes, | 57 const std::vector<SBFullHash>& full_hashes, |
59 Client* client, | 58 Client* client, |
60 safe_browsing::ListType check_type, | 59 ListType check_type, |
61 const std::vector<SBThreatType>& expected_threats); | 60 const std::vector<SBThreatType>& expected_threats); |
62 ~SafeBrowsingCheck(); | 61 ~SafeBrowsingCheck(); |
63 | 62 |
64 // Either |urls| or |full_hashes| is used to lookup database. |*_results| | 63 // Either |urls| or |full_hashes| is used to lookup database. |*_results| |
65 // are parallel vectors containing the results. They are initialized to | 64 // are parallel vectors containing the results. They are initialized to |
66 // contain SB_THREAT_TYPE_SAFE. | 65 // contain SB_THREAT_TYPE_SAFE. |
67 std::vector<GURL> urls; | 66 std::vector<GURL> urls; |
68 std::vector<SBThreatType> url_results; | 67 std::vector<SBThreatType> url_results; |
69 std::vector<std::string> url_metadata; | 68 std::vector<std::string> url_metadata; |
70 std::vector<SBFullHash> full_hashes; | 69 std::vector<SBFullHash> full_hashes; |
71 std::vector<SBThreatType> full_hash_results; | 70 std::vector<SBThreatType> full_hash_results; |
72 | 71 |
73 SafeBrowsingDatabaseManager::Client* client; | 72 SafeBrowsingDatabaseManager::Client* client; |
74 bool is_extended_reporting; | 73 bool is_extended_reporting; |
75 bool need_get_hash; | 74 bool need_get_hash; |
76 base::TimeTicks start; // When check was sent to SB service. | 75 base::TimeTicks start; // When check was sent to SB service. |
77 safe_browsing::ListType check_type; // See comment in constructor. | 76 ListType check_type; // See comment in constructor. |
78 std::vector<SBThreatType> expected_threats; | 77 std::vector<SBThreatType> expected_threats; |
79 std::vector<SBPrefix> prefix_hits; | 78 std::vector<SBPrefix> prefix_hits; |
80 std::vector<SBFullHashResult> cache_hits; | 79 std::vector<SBFullHashResult> cache_hits; |
81 | 80 |
82 // Invoke one of client's callbacks with these results. | 81 // Invoke one of client's callbacks with these results. |
83 void OnSafeBrowsingResult(); | 82 void OnSafeBrowsingResult(); |
84 | 83 |
85 // Vends weak pointers for async callbacks on the IO thread, such as | 84 // Vends weak pointers for async callbacks on the IO thread, such as |
86 // timeout checks and replies from checks performed on the SB task runner. | 85 // timeout checks and replies from checks performed on the SB task runner. |
87 // TODO(lzheng): We should consider to use this time out check | 86 // TODO(lzheng): We should consider to use this time out check |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 GetUrlSeverestThreatType); | 146 GetUrlSeverestThreatType); |
148 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest, | 147 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest, |
149 ServiceStopWithPendingChecks); | 148 ServiceStopWithPendingChecks); |
150 | 149 |
151 typedef std::set<SafeBrowsingCheck*> CurrentChecks; | 150 typedef std::set<SafeBrowsingCheck*> CurrentChecks; |
152 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; | 151 typedef std::vector<SafeBrowsingCheck*> GetHashRequestors; |
153 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; | 152 typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests; |
154 | 153 |
155 // Clients that we've queued up for checking later once the database is ready. | 154 // Clients that we've queued up for checking later once the database is ready. |
156 struct QueuedCheck { | 155 struct QueuedCheck { |
157 QueuedCheck(const safe_browsing::ListType check_type, | 156 QueuedCheck(const ListType check_type, |
158 Client* client, | 157 Client* client, |
159 const GURL& url, | 158 const GURL& url, |
160 const std::vector<SBThreatType>& expected_threats, | 159 const std::vector<SBThreatType>& expected_threats, |
161 const base::TimeTicks& start); | 160 const base::TimeTicks& start); |
162 ~QueuedCheck(); | 161 ~QueuedCheck(); |
163 safe_browsing::ListType check_type; | 162 ListType check_type; |
164 Client* client; | 163 Client* client; |
165 GURL url; | 164 GURL url; |
166 std::vector<SBThreatType> expected_threats; | 165 std::vector<SBThreatType> expected_threats; |
167 base::TimeTicks start; // When check was queued. | 166 base::TimeTicks start; // When check was queued. |
168 }; | 167 }; |
169 | 168 |
170 // Return the threat type of the severest entry in |full_hashes| which matches | 169 // Return the threat type of the severest entry in |full_hashes| which matches |
171 // |hash|, or SAFE if none match. | 170 // |hash|, or SAFE if none match. |
172 static SBThreatType GetHashSeverestThreatType( | 171 static SBThreatType GetHashSeverestThreatType( |
173 const SBFullHash& hash, | 172 const SBFullHash& hash, |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 bool closing_database_; | 354 bool closing_database_; |
356 | 355 |
357 std::deque<QueuedCheck> queued_checks_; | 356 std::deque<QueuedCheck> queued_checks_; |
358 | 357 |
359 // Timeout to use for safe browsing checks. | 358 // Timeout to use for safe browsing checks. |
360 base::TimeDelta check_timeout_; | 359 base::TimeDelta check_timeout_; |
361 | 360 |
362 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager); | 361 DISALLOW_COPY_AND_ASSIGN(LocalSafeBrowsingDatabaseManager); |
363 }; // class LocalSafeBrowsingDatabaseManager | 362 }; // class LocalSafeBrowsingDatabaseManager |
364 | 363 |
| 364 } // namespace safe_browsing |
| 365 |
365 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ | 366 #endif // CHROME_BROWSER_SAFE_BROWSING_LOCAL_DATABASE_MANAGER_H_ |
OLD | NEW |