Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: components/safe_browsing_db/v4_database.h

Issue 2675063002: Browser tests for using the new SafeBrowsing protocol (v4) (Closed)
Patch Set: Nit: Add comments in tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::unique_ptr<V4Database> Create(
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.
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(std::unique_ptr<V4DatabaseFactory> factory) {
112 db_factory_ = std::move(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
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(
169 factory_ = factory; 175 std::unique_ptr<V4StoreFactory> factory) {
176 store_factory_ = std::move(factory);
170 } 177 }
171 178
172 // Factory method to create a V4Database. When the database creation is 179 // Factory method to create a V4Database. When the database creation is
173 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. 180 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|.
174 static void CreateOnTaskRunner( 181 static void CreateOnTaskRunner(
175 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 182 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
176 const base::FilePath& base_path, 183 const base::FilePath& base_path,
177 const ListInfos& list_infos, 184 const ListInfos& list_infos,
178 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 185 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
179 NewDatabaseReadyCallback callback, 186 NewDatabaseReadyCallback callback,
180 const base::TimeTicks create_start_time); 187 const base::TimeTicks create_start_time);
181 188
182 // Callback called when a new store has been created and is ready to be used. 189 // 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 190 // This method updates the store_map_ to point to the new store, which causes
184 // the old store to get deleted. 191 // the old store to get deleted.
185 void UpdatedStoreReady(ListIdentifier identifier, 192 void UpdatedStoreReady(ListIdentifier identifier,
186 std::unique_ptr<V4Store> store); 193 std::unique_ptr<V4Store> store);
187 194
188 // See |VerifyChecksum|. 195 // See |VerifyChecksum|.
189 void VerifyChecksumOnTaskRunner( 196 void VerifyChecksumOnTaskRunner(
190 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 197 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
191 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); 198 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback);
192 199
193 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; 200 protected:
194
195 // Map of ListIdentifier to the V4Store. 201 // Map of ListIdentifier to the V4Store.
196 const std::unique_ptr<StoreMap> store_map_; 202 const std::unique_ptr<StoreMap> store_map_;
197 203
204 private:
205 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_;
206
198 DatabaseUpdatedCallback db_updated_callback_; 207 DatabaseUpdatedCallback db_updated_callback_;
199 208
209 // The factory that controls the creation of the V4Database object.
210 static std::unique_ptr<V4DatabaseFactory> db_factory_;
Nathan Parker 2017/02/10 00:52:37 (I'm not sure if this ever gets deleted... but I t
vakh (use Gerrit instead) 2017/02/10 01:08:15 Acknowledged.
211
200 // The factory that controls the creation of V4Store objects. 212 // The factory that controls the creation of V4Store objects.
201 static V4StoreFactory* factory_; 213 static std::unique_ptr<V4StoreFactory> store_factory_;
202 214
203 // The number of stores for which the update request is pending. When this 215 // 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 216 // 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 217 // that needed updating and is ready for the next update. It should only be
206 // accessed on the IO thread. 218 // accessed on the IO thread.
207 int pending_store_updates_; 219 int pending_store_updates_;
208 220
209 // Only meant to be dereferenced and invalidated on the IO thread and hence 221 // 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 222 // named. For details, see the comment at the top of weak_ptr.h
211 base::WeakPtrFactory<V4Database> weak_factory_on_io_; 223 base::WeakPtrFactory<V4Database> weak_factory_on_io_;
212 224
213 DISALLOW_COPY_AND_ASSIGN(V4Database); 225 DISALLOW_COPY_AND_ASSIGN(V4Database);
214 }; 226 };
215 227
216 } // namespace safe_browsing 228 } // namespace safe_browsing
217 229
218 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ 230 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698