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_DATABASE_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 // This callback is scheduled once the database has finished processing the | 23 // This callback is scheduled once the database has finished processing the |
24 // update requests for all stores and is ready to process the next set of update | 24 // update requests for all stores and is ready to process the next set of update |
25 // requests. | 25 // requests. |
26 typedef base::Callback<void()> DatabaseUpdatedCallback; | 26 typedef base::Callback<void()> DatabaseUpdatedCallback; |
27 | 27 |
28 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory | 28 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory |
29 // stores, which contain the hash prefixes for that UpdateListIdentifier as well | 29 // stores, which contain the hash prefixes for that UpdateListIdentifier as well |
30 // as manage their storage on disk. | 30 // as manage their storage on disk. |
31 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 31 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
32 | 32 |
| 33 // Map of identifier for any store that had a hash prefix matching the given |
| 34 // full hash to the matching hash prefix. |
| 35 typedef base::hash_map<UpdateListIdentifier, HashPrefix> MatchedHashPrefixMap; |
| 36 |
33 // Factory for creating V4Database. Tests implement this factory to create fake | 37 // Factory for creating V4Database. Tests implement this factory to create fake |
34 // databases for testing. | 38 // databases for testing. |
35 class V4DatabaseFactory { | 39 class V4DatabaseFactory { |
36 public: | 40 public: |
37 virtual ~V4DatabaseFactory() {} | 41 virtual ~V4DatabaseFactory() {} |
38 virtual V4Database* CreateV4Database( | 42 virtual V4Database* CreateV4Database( |
39 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 43 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
40 const base::FilePath& base_dir_path, | 44 const base::FilePath& base_dir_path, |
41 const StoreFileNameMap& store_file_name_map) = 0; | 45 const StoreFileNameMap& store_file_name_map) = 0; |
42 }; | 46 }; |
(...skipping 24 matching lines...) Expand all Loading... |
67 virtual ~V4Database(); | 71 virtual ~V4Database(); |
68 | 72 |
69 // Updates the stores with the response received from the SafeBrowsing service | 73 // Updates the stores with the response received from the SafeBrowsing service |
70 // and calls the db_updated_callback when done. | 74 // and calls the db_updated_callback when done. |
71 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, | 75 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, |
72 DatabaseUpdatedCallback db_updated_callback); | 76 DatabaseUpdatedCallback db_updated_callback); |
73 | 77 |
74 // Returns the current state of each of the stores being managed. | 78 // Returns the current state of each of the stores being managed. |
75 std::unique_ptr<StoreStateMap> GetStoreStateMap(); | 79 std::unique_ptr<StoreStateMap> GetStoreStateMap(); |
76 | 80 |
| 81 // Searches for a hash prefix matching the |full_hash| in stores in the |
| 82 // database, filtered by |stores_to_look|, and returns the identifier of the |
| 83 // store along with the matching hash prefix in |matched_hash_prefix_map|. |
| 84 void GetStoresMatchingFullHash( |
| 85 const FullHash& full_hash, |
| 86 const base::hash_set<UpdateListIdentifier>& stores_to_look, |
| 87 MatchedHashPrefixMap* matched_hash_prefix_map); |
| 88 |
77 // Deletes the current database and creates a new one. | 89 // Deletes the current database and creates a new one. |
78 virtual bool ResetDatabase(); | 90 virtual bool ResetDatabase(); |
79 | 91 |
80 protected: | 92 protected: |
81 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 93 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
82 std::unique_ptr<StoreMap> store_map); | 94 std::unique_ptr<StoreMap> store_map); |
83 | 95 |
84 private: | 96 private: |
85 friend class V4DatabaseTest; | 97 friend class V4DatabaseTest; |
86 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); | 98 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); |
87 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, | 99 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, |
88 TestSetupDatabaseWithFakeStoresFailsReset); | 100 TestSetupDatabaseWithFakeStoresFailsReset); |
89 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); | 101 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); |
90 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); | 102 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); |
91 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); | 103 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); |
92 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); | 104 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); |
| 105 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSomeStoresMatchFullHash); |
93 | 106 |
94 // Makes the passed |factory| the factory used to instantiate a V4Store. Only | 107 // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
95 // for tests. | 108 // for tests. |
96 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 109 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
97 factory_ = factory; | 110 factory_ = factory; |
98 } | 111 } |
99 | 112 |
100 // Factory method to create a V4Database. When the database creation is | 113 // Factory method to create a V4Database. When the database creation is |
101 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 114 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
102 static void CreateOnTaskRunner( | 115 static void CreateOnTaskRunner( |
(...skipping 24 matching lines...) Expand all Loading... |
127 // that needed updating and is ready for the next update. It should only be | 140 // that needed updating and is ready for the next update. It should only be |
128 // accessed on the IO thread. | 141 // accessed on the IO thread. |
129 int pending_store_updates_; | 142 int pending_store_updates_; |
130 | 143 |
131 DISALLOW_COPY_AND_ASSIGN(V4Database); | 144 DISALLOW_COPY_AND_ASSIGN(V4Database); |
132 }; | 145 }; |
133 | 146 |
134 } // namespace safe_browsing | 147 } // namespace safe_browsing |
135 | 148 |
136 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 149 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |