OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
7 | 7 |
8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. | 8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. |
9 // | 9 // |
10 // The V4GetHashProtocolManager handles formatting and making requests of, and | 10 // The V4GetHashProtocolManager handles formatting and making requests of, and |
11 // handling responses from, Google's SafeBrowsing servers. The purpose of this | 11 // handling responses from, Google's SafeBrowsing servers. The purpose of this |
12 // class is to get full hash matches from the SB server for the given set of | 12 // class is to get full hash matches from the SB server for the given set of |
13 // hash prefixes. | 13 // hash prefixes. |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 #include <string> | 16 #include <string> |
| 17 #include <unordered_map> |
17 #include <utility> | 18 #include <utility> |
18 #include <vector> | 19 #include <vector> |
19 | 20 |
20 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
21 #include "base/macros.h" | 22 #include "base/macros.h" |
22 #include "base/threading/non_thread_safe.h" | 23 #include "base/threading/non_thread_safe.h" |
23 #include "base/time/default_clock.h" | 24 #include "base/time/default_clock.h" |
24 #include "base/time/time.h" | 25 #include "base/time/time.h" |
25 #include "base/timer/timer.h" | 26 #include "base/timer/timer.h" |
26 #include "components/safe_browsing_db/safebrowsing.pb.h" | 27 #include "components/safe_browsing_db/safebrowsing.pb.h" |
27 #include "components/safe_browsing_db/util.h" | 28 #include "components/safe_browsing_db/util.h" |
28 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 29 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
29 #include "net/url_request/url_fetcher_delegate.h" | 30 #include "net/url_request/url_fetcher_delegate.h" |
30 | 31 |
31 class GURL; | 32 class GURL; |
32 | 33 |
33 namespace net { | 34 namespace net { |
34 class URLFetcher; | 35 class URLFetcher; |
35 class URLRequestContextGetter; | 36 class URLRequestContextGetter; |
36 } // namespace net | 37 } // namespace net |
37 | 38 |
38 namespace safe_browsing { | 39 namespace safe_browsing { |
39 | 40 |
40 // The matching hash prefixes and corresponding stores, for each full hash | 41 // The matching hash prefixes and corresponding stores, for each full hash |
41 // generated for a given URL. | 42 // generated for a given URL. |
42 typedef base::hash_map<FullHash, StoreAndHashPrefixes> | 43 typedef std::unordered_map<FullHash, StoreAndHashPrefixes> |
43 FullHashToStoreAndHashPrefixesMap; | 44 FullHashToStoreAndHashPrefixesMap; |
44 | 45 |
45 // ---------------------------------------------------------------- | 46 // ---------------------------------------------------------------- |
46 | 47 |
47 // All information about a particular full hash i.e. negative TTL, store for | 48 // All information about a particular full hash i.e. negative TTL, store for |
48 // which it is valid, and metadata associated with that store. | 49 // which it is valid, and metadata associated with that store. |
49 struct FullHashInfo { | 50 struct FullHashInfo { |
50 public: | 51 public: |
51 FullHash full_hash; | 52 FullHash full_hash; |
52 | 53 |
(...skipping 30 matching lines...) Expand all Loading... |
83 // particular hash prefix and are known to be unsafe. | 84 // particular hash prefix and are known to be unsafe. |
84 std::vector<FullHashInfo> full_hash_infos; | 85 std::vector<FullHashInfo> full_hash_infos; |
85 | 86 |
86 CachedHashPrefixInfo(); | 87 CachedHashPrefixInfo(); |
87 CachedHashPrefixInfo(const CachedHashPrefixInfo& other); | 88 CachedHashPrefixInfo(const CachedHashPrefixInfo& other); |
88 ~CachedHashPrefixInfo(); | 89 ~CachedHashPrefixInfo(); |
89 }; | 90 }; |
90 | 91 |
91 // Cached full hashes received from the server for the corresponding hash | 92 // Cached full hashes received from the server for the corresponding hash |
92 // prefixes. | 93 // prefixes. |
93 typedef base::hash_map<HashPrefix, CachedHashPrefixInfo> FullHashCache; | 94 typedef std::unordered_map<HashPrefix, CachedHashPrefixInfo> FullHashCache; |
94 | 95 |
95 // FullHashCallback is invoked when GetFullHashes completes. The parameter is | 96 // FullHashCallback is invoked when GetFullHashes completes. The parameter is |
96 // the vector of full hash results. If empty, indicates that there were no | 97 // the vector of full hash results. If empty, indicates that there were no |
97 // matches, and that the resource is safe. | 98 // matches, and that the resource is safe. |
98 typedef base::Callback<void(const std::vector<FullHashInfo>&)> FullHashCallback; | 99 typedef base::Callback<void(const std::vector<FullHashInfo>&)> FullHashCallback; |
99 | 100 |
100 // Information needed to update the cache and call the callback to post the | 101 // Information needed to update the cache and call the callback to post the |
101 // results. | 102 // results. |
102 struct FullHashCallbackInfo { | 103 struct FullHashCallbackInfo { |
103 FullHashCallbackInfo(); | 104 FullHashCallbackInfo(); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 const std::vector<FullHashInfo>& full_hash_infos, | 289 const std::vector<FullHashInfo>& full_hash_infos, |
289 const base::Time& negative_cache_expire); | 290 const base::Time& negative_cache_expire); |
290 | 291 |
291 protected: | 292 protected: |
292 // A cache of full hash results. | 293 // A cache of full hash results. |
293 FullHashCache full_hash_cache_; | 294 FullHashCache full_hash_cache_; |
294 | 295 |
295 private: | 296 private: |
296 // Map of GetHash requests to parameters which created it. | 297 // Map of GetHash requests to parameters which created it. |
297 using PendingHashRequests = | 298 using PendingHashRequests = |
298 base::hash_map<const net::URLFetcher*, | 299 std::unordered_map<const net::URLFetcher*, |
299 std::unique_ptr<FullHashCallbackInfo>>; | 300 std::unique_ptr<FullHashCallbackInfo>>; |
300 | 301 |
301 // The factory that controls the creation of V4GetHashProtocolManager. | 302 // The factory that controls the creation of V4GetHashProtocolManager. |
302 // This is used by tests. | 303 // This is used by tests. |
303 static V4GetHashProtocolManagerFactory* factory_; | 304 static V4GetHashProtocolManagerFactory* factory_; |
304 | 305 |
305 // Current active request (in case we need to cancel) for updates or chunks | 306 // Current active request (in case we need to cancel) for updates or chunks |
306 // from the SafeBrowsing service. We can only have one of these outstanding | 307 // from the SafeBrowsing service. We can only have one of these outstanding |
307 // at any given time unlike GetHash requests, which are tracked separately. | 308 // at any given time unlike GetHash requests, which are tracked separately. |
308 std::unique_ptr<net::URLFetcher> request_; | 309 std::unique_ptr<net::URLFetcher> request_; |
309 | 310 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); | 358 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); |
358 }; | 359 }; |
359 | 360 |
360 #ifndef NDEBUG | 361 #ifndef NDEBUG |
361 std::ostream& operator<<(std::ostream& os, const FullHashInfo& id); | 362 std::ostream& operator<<(std::ostream& os, const FullHashInfo& id); |
362 #endif | 363 #endif |
363 | 364 |
364 } // namespace safe_browsing | 365 } // namespace safe_browsing |
365 | 366 |
366 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 367 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
OLD | NEW |