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