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/files/file_path.h" | 9 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
13 #include "components/safe_browsing_db/v4_store.h" | 14 #include "components/safe_browsing_db/v4_store.h" |
14 | 15 |
15 namespace safe_browsing { | 16 namespace safe_browsing { |
16 | 17 |
17 class V4Database; | 18 class V4Database; |
18 | 19 |
19 typedef base::Callback<void(std::unique_ptr<V4Database>)> | 20 typedef base::Callback<void(std::unique_ptr<V4Database>)> |
20 NewDatabaseReadyCallback; | 21 NewDatabaseReadyCallback; |
21 | 22 |
22 // This defines a hash_map that is used to create the backing files for the | 23 // This callback is Run once the database has finished processing the update |
23 // stores that contain the hash-prefixes. The map key identifies the list that | 24 // requests for all stores and is ready to process the next set of update |
24 // we're interested in and the value represents the ASCII file-name that'll be | 25 // requests. |
25 // created on-disk to store the hash-prefixes for that list. This file is | 26 typedef base::Callback<void()> DatabaseUpdatedCallback; |
26 // created inside the user's profile directory. | |
27 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | |
28 // on Windows platform, and the corresponding file on disk could be named: | |
29 // "uws_win_url.store" | |
30 typedef base::hash_map<UpdateListIdentifier, std::string> ListInfoMap; | |
31 | 27 |
32 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory | 28 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory |
33 // stores, which contain the hash prefixes for that UpdateListIdentifier as well | 29 // stores, which contain the hash prefixes for that UpdateListIdentifier as well |
34 // as manage their storage on disk. | 30 // as manage their storage on disk. |
35 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 31 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
36 | 32 |
37 // Factory for creating V4Database. Tests implement this factory to create fake | 33 // Factory for creating V4Database. Tests implement this factory to create fake |
38 // databases for testing. | 34 // databases for testing. |
39 class V4DatabaseFactory { | 35 class V4DatabaseFactory { |
40 public: | 36 public: |
41 virtual ~V4DatabaseFactory() {} | 37 virtual ~V4DatabaseFactory() {} |
42 virtual V4Database* CreateV4Database( | 38 virtual V4Database* CreateV4Database( |
43 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 39 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
44 const base::FilePath& base_dir_path, | 40 const base::FilePath& base_dir_path, |
45 const ListInfoMap& list_info_map) = 0; | 41 const StoreFileNameMap& store_file_name_map) = 0; |
46 }; | 42 }; |
47 | 43 |
48 // The on-disk databases are shared among all profiles, as it doesn't contain | 44 // The on-disk databases are shared among all profiles, as it doesn't contain |
49 // user-specific data. This object is not thread-safe, i.e. all its methods | 45 // user-specific data. This object is not thread-safe, i.e. all its methods |
50 // should be used on the same thread that it was created on, unless specified | 46 // should be used on the same thread that it was created on, unless specified |
51 // otherwise. | 47 // otherwise. |
52 // The hash-prefixes of each type are managed by a V4Store (including saving to | 48 // The hash-prefixes of each type are managed by a V4Store (including saving to |
53 // and reading from disk). | 49 // and reading from disk). |
54 // The V4Database serves as a single place to manage all the V4Stores. | 50 // The V4Database serves as a single place to manage all the V4Stores. |
55 class V4Database { | 51 class V4Database { |
56 public: | 52 public: |
57 // Factory method to create a V4Database. It creates the database on the | 53 // Factory method to create a V4Database. It creates the database on the |
58 // provided |db_task_runner|. When the database creation is complete, it calls | 54 // provided |db_task_runner| containing stores for which the filename has |
59 // the NewDatabaseReadyCallback on the same thread as it was called. | 55 // been specified in \store_file_name_map\. The created V4Database runs |
60 static void Create( | 56 // |db_updated_callback| after processing updates passed to the ApplyUpdate |
57 // method. When the database creation is complete, it runs the | |
58 // NewDatabaseReadyCallback on the same thread as it was called. If the | |
59 // |base_path| argument isn't an absolute path or if the | |
60 // |store_file_name_map| is empty, it returns false right away and doesn't | |
61 // attempt to create the database at all. | |
62 static bool Create( | |
61 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 63 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
62 const base::FilePath& base_path, | 64 const base::FilePath& base_path, |
63 const ListInfoMap& list_info_map, | 65 const StoreFileNameMap& store_file_name_map, |
66 DatabaseUpdatedCallback db_updated_callback, | |
64 NewDatabaseReadyCallback callback); | 67 NewDatabaseReadyCallback callback); |
65 | 68 |
66 // Destroys the provided v4_database on its task_runner since this may be a | 69 // Destroys the provided v4_database on its task_runner since this may be a |
67 // long operation. | 70 // long operation. |
68 static void Destroy(std::unique_ptr<V4Database> v4_database); | 71 static void Destroy(std::unique_ptr<V4Database> v4_database); |
69 | 72 |
70 virtual ~V4Database(); | 73 virtual ~V4Database(); |
71 | 74 |
75 // Updates the stores with the reponse received from the SafeBrowsing service | |
Nathan Parker
2016/06/15 21:24:13
nit: response
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
| |
76 // and calls the db_updated_callback_ when done. | |
77 void ApplyUpdate(const std::vector<ListUpdateResponse>&); | |
78 | |
79 const StoreStateMap* store_state_map() { return store_state_map_.get(); } | |
80 | |
72 // Deletes the current database and creates a new one. | 81 // Deletes the current database and creates a new one. |
73 virtual bool ResetDatabase(); | 82 virtual bool ResetDatabase(); |
74 | 83 |
75 protected: | 84 protected: |
76 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 85 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
77 std::unique_ptr<StoreMap> store_map); | 86 std::unique_ptr<StoreMap> store_map, |
87 DatabaseUpdatedCallback db_updated_callback); | |
78 | 88 |
79 private: | 89 private: |
80 friend class SafeBrowsingV4DatabaseTest; | 90 friend class SafeBrowsingV4DatabaseTest; |
81 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | 91 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
82 TestSetupDatabaseWithFakeStores); | 92 TestSetupDatabaseWithFakeStores); |
83 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | 93 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
84 TestSetupDatabaseWithFakeStoresFailsReset); | 94 TestSetupDatabaseWithFakeStoresFailsReset); |
95 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | |
96 TestApplyUpdateWithNewStates); | |
97 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | |
98 TestApplyUpdateWithNoNewState); | |
99 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | |
100 TestApplyUpdateWithEmptyUpdate); | |
85 | 101 |
86 // Makes the passed |factory| the factory used to instantiate a V4Store. Only | 102 // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
87 // for tests. | 103 // for tests. |
88 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 104 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
89 factory_ = factory; | 105 factory_ = factory; |
90 } | 106 } |
91 | 107 |
92 // Factory method to create a V4Database. When the database creation is | 108 // Factory method to create a V4Database. When the database creation is |
93 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 109 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
94 static void CreateOnTaskRunner( | 110 static void CreateOnTaskRunner( |
95 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 111 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
96 const base::FilePath& base_path, | 112 const base::FilePath& base_path, |
97 const ListInfoMap& list_info_map, | 113 const StoreFileNameMap& store_file_name_map, |
114 DatabaseUpdatedCallback db_updated_callback, | |
98 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 115 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
99 NewDatabaseReadyCallback callback); | 116 NewDatabaseReadyCallback callback); |
100 | 117 |
118 // Callback called when a new store has been created and is ready to be used. | |
119 // This method updates the store_map_ to point to the new store, which causes | |
120 // the old store to get deleted. | |
121 void UpdatedStoreReady(UpdateListIdentifier, std::unique_ptr<V4Store>); | |
Nathan Parker
2016/06/15 21:24:15
missing argument names.
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
| |
122 | |
123 // Updates the store state map after reading the stores from disk on startup. | |
124 void UpdateStoreStateMap(); | |
125 | |
101 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 126 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
102 | 127 |
103 // Map of UpdateListIdentifier to the V4Store. | 128 // Map of UpdateListIdentifier to the V4Store. |
104 const std::unique_ptr<StoreMap> store_map_; | 129 const std::unique_ptr<StoreMap> store_map_; |
105 | 130 |
131 // Map of UpdateListIdentifier to the state of the V4Store it represents. | |
132 const std::unique_ptr<StoreStateMap> store_state_map_; | |
Nathan Parker
2016/06/15 21:24:14
Could this be just a StoreStateMap, and not a ptr?
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
| |
133 | |
134 DatabaseUpdatedCallback db_updated_callback_; | |
135 | |
106 // The factory that controls the creation of V4Store objects. | 136 // The factory that controls the creation of V4Store objects. |
107 static V4StoreFactory* factory_; | 137 static V4StoreFactory* factory_; |
108 | 138 |
139 // The number of stores for which the update request is pending. When this | |
140 // goes down to 0, that indicates that the database has updated all the stores | |
141 // that needed updating and is ready for the next update. | |
142 unsigned pending_store_updates_; | |
143 | |
109 DISALLOW_COPY_AND_ASSIGN(V4Database); | 144 DISALLOW_COPY_AND_ASSIGN(V4Database); |
110 }; | 145 }; |
111 | 146 |
112 } // namespace safe_browsing | 147 } // namespace safe_browsing |
113 | 148 |
114 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 149 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |