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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 ListInfo(); | 71 ListInfo(); |
72 }; | 72 }; |
73 | 73 |
74 typedef std::vector<ListInfo> ListInfos; | 74 typedef std::vector<ListInfo> ListInfos; |
75 | 75 |
76 // Factory for creating V4Database. Tests implement this factory to create fake | 76 // Factory for creating V4Database. Tests implement this factory to create fake |
77 // databases for testing. | 77 // databases for testing. |
78 class V4DatabaseFactory { | 78 class V4DatabaseFactory { |
79 public: | 79 public: |
80 virtual ~V4DatabaseFactory() {} | 80 virtual ~V4DatabaseFactory() {} |
81 virtual V4Database* CreateV4Database( | 81 virtual V4Database* Create( |
Nathan Parker
2017/02/08 01:22:54
Should this return a unique_ptr? That's the canoni
| |
82 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 82 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
83 const base::FilePath& base_dir_path, | 83 std::unique_ptr<StoreMap> store_map); |
84 const ListInfos& list_infos) = 0; | |
85 }; | 84 }; |
86 | 85 |
87 // The on-disk databases are shared among all profiles, as it doesn't contain | 86 // The on-disk databases are shared among all profiles, as it doesn't contain |
88 // user-specific data. This object is not thread-safe, i.e. all its methods | 87 // user-specific data. This object is not thread-safe, i.e. all its methods |
89 // should be used on the same thread that it was created on, unless specified | 88 // should be used on the same thread that it was created on, unless specified |
90 // otherwise. | 89 // otherwise. |
91 // The hash-prefixes of each type are managed by a V4Store (including saving to | 90 // The hash-prefixes of each type are managed by a V4Store (including saving to |
92 // and reading from disk). | 91 // and reading from disk). |
93 // The V4Database serves as a single place to manage all the V4Stores. | 92 // The V4Database serves as a single place to manage all the V4Stores. |
94 class V4Database { | 93 class V4Database { |
95 public: | 94 public: |
96 // Factory method to create a V4Database. It creates the database on the | 95 // Factory method to create a V4Database. It creates the database on the |
97 // provided |db_task_runner| containing stores in |store_file_name_map|. When | 96 // provided |db_task_runner| containing stores in |store_file_name_map|. When |
98 // the database creation is complete, it runs the NewDatabaseReadyCallback on | 97 // the database creation is complete, it runs the NewDatabaseReadyCallback on |
99 // the same thread as it was called. | 98 // the same thread as it was called. |
Nathan Parker
2017/02/08 01:22:54
// Uses the registered factory or a default implem
| |
100 static void Create( | 99 static void Create( |
101 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 100 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
102 const base::FilePath& base_path, | 101 const base::FilePath& base_path, |
103 const ListInfos& list_infos, | 102 const ListInfos& list_infos, |
104 NewDatabaseReadyCallback callback); | 103 NewDatabaseReadyCallback new_db_callback); |
105 | 104 |
106 // Destroys the provided v4_database on its task_runner since this may be a | 105 // Destroys the provided v4_database on its task_runner since this may be a |
107 // long operation. | 106 // long operation. |
108 static void Destroy(std::unique_ptr<V4Database> v4_database); | 107 static void Destroy(std::unique_ptr<V4Database> v4_database); |
109 | 108 |
109 // Makes the passed |factory| the factory used to instantiate a V4Database. | |
110 // Only for tests. | |
111 static void RegisterFactory(V4DatabaseFactory* factory) { | |
112 db_factory_ = factory; | |
113 } | |
114 | |
110 virtual ~V4Database(); | 115 virtual ~V4Database(); |
111 | 116 |
112 // Updates the stores with the response received from the SafeBrowsing service | 117 // Updates the stores with the response received from the SafeBrowsing service |
113 // and calls the db_updated_callback when done. | 118 // and calls the db_updated_callback when done. |
114 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, | 119 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, |
115 DatabaseUpdatedCallback db_updated_callback); | 120 DatabaseUpdatedCallback db_updated_callback); |
116 | 121 |
117 // Returns the current state of each of the stores being managed. | 122 // Returns the current state of each of the stores being managed. |
118 std::unique_ptr<StoreStateMap> GetStoreStateMap(); | 123 std::unique_ptr<StoreStateMap> GetStoreStateMap(); |
119 | 124 |
(...skipping 26 matching lines...) Expand all Loading... | |
146 | 151 |
147 // Records the size of each of the stores managed by this database, along | 152 // Records the size of each of the stores managed by this database, along |
148 // with the combined size of all the stores. | 153 // with the combined size of all the stores. |
149 void RecordFileSizeHistograms(); | 154 void RecordFileSizeHistograms(); |
150 | 155 |
151 protected: | 156 protected: |
152 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 157 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
153 std::unique_ptr<StoreMap> store_map); | 158 std::unique_ptr<StoreMap> store_map); |
154 | 159 |
155 private: | 160 private: |
161 friend class V4DatabaseFactory; | |
156 friend class V4DatabaseTest; | 162 friend class V4DatabaseTest; |
157 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); | 163 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); |
158 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, | 164 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, |
159 TestSetupDatabaseWithFakeStoresFailsReset); | 165 TestSetupDatabaseWithFakeStoresFailsReset); |
160 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); | 166 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); |
161 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); | 167 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); |
162 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); | 168 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); |
163 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); | 169 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); |
164 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSomeStoresMatchFullHash); | 170 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSomeStoresMatchFullHash); |
165 | 171 |
166 // Makes the passed |factory| the factory used to instantiate a V4Store. Only | 172 // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
167 // for tests. | 173 // for tests. |
168 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 174 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
169 factory_ = factory; | 175 store_factory_ = factory; |
170 } | 176 } |
171 | 177 |
172 // Factory method to create a V4Database. When the database creation is | 178 // Factory method to create a V4Database. When the database creation is |
173 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 179 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
174 static void CreateOnTaskRunner( | 180 static void CreateOnTaskRunner( |
175 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 181 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
176 const base::FilePath& base_path, | 182 const base::FilePath& base_path, |
177 const ListInfos& list_infos, | 183 const ListInfos& list_infos, |
178 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 184 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
179 NewDatabaseReadyCallback callback, | 185 NewDatabaseReadyCallback callback, |
180 const base::TimeTicks create_start_time); | 186 const base::TimeTicks create_start_time); |
181 | 187 |
182 // Callback called when a new store has been created and is ready to be used. | 188 // Callback called when a new store has been created and is ready to be used. |
183 // This method updates the store_map_ to point to the new store, which causes | 189 // This method updates the store_map_ to point to the new store, which causes |
184 // the old store to get deleted. | 190 // the old store to get deleted. |
185 void UpdatedStoreReady(ListIdentifier identifier, | 191 void UpdatedStoreReady(ListIdentifier identifier, |
186 std::unique_ptr<V4Store> store); | 192 std::unique_ptr<V4Store> store); |
187 | 193 |
188 // See |VerifyChecksum|. | 194 // See |VerifyChecksum|. |
189 void VerifyChecksumOnTaskRunner( | 195 void VerifyChecksumOnTaskRunner( |
190 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 196 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
191 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | 197 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); |
192 | 198 |
193 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 199 protected: |
194 | |
195 // Map of ListIdentifier to the V4Store. | 200 // Map of ListIdentifier to the V4Store. |
196 const std::unique_ptr<StoreMap> store_map_; | 201 const std::unique_ptr<StoreMap> store_map_; |
197 | 202 |
203 private: | |
204 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | |
205 | |
198 DatabaseUpdatedCallback db_updated_callback_; | 206 DatabaseUpdatedCallback db_updated_callback_; |
199 | 207 |
208 // The factory that controls the creation of the V4Database object. | |
209 static V4DatabaseFactory* db_factory_; | |
210 | |
200 // The factory that controls the creation of V4Store objects. | 211 // The factory that controls the creation of V4Store objects. |
201 static V4StoreFactory* factory_; | 212 static V4StoreFactory* store_factory_; |
202 | 213 |
203 // The number of stores for which the update request is pending. When this | 214 // The number of stores for which the update request is pending. When this |
204 // goes down to 0, that indicates that the database has updated all the stores | 215 // goes down to 0, that indicates that the database has updated all the stores |
205 // that needed updating and is ready for the next update. It should only be | 216 // that needed updating and is ready for the next update. It should only be |
206 // accessed on the IO thread. | 217 // accessed on the IO thread. |
207 int pending_store_updates_; | 218 int pending_store_updates_; |
208 | 219 |
209 // Only meant to be dereferenced and invalidated on the IO thread and hence | 220 // Only meant to be dereferenced and invalidated on the IO thread and hence |
210 // named. For details, see the comment at the top of weak_ptr.h | 221 // named. For details, see the comment at the top of weak_ptr.h |
211 base::WeakPtrFactory<V4Database> weak_factory_on_io_; | 222 base::WeakPtrFactory<V4Database> weak_factory_on_io_; |
212 | 223 |
213 DISALLOW_COPY_AND_ASSIGN(V4Database); | 224 DISALLOW_COPY_AND_ASSIGN(V4Database); |
214 }; | 225 }; |
215 | 226 |
216 } // namespace safe_browsing | 227 } // namespace safe_browsing |
217 | 228 |
218 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 229 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |