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" |
11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
14 #include "components/safe_browsing_db/v4_store.h" | 14 #include "components/safe_browsing_db/v4_store.h" |
15 | 15 |
16 namespace safe_browsing { | 16 namespace safe_browsing { |
17 | 17 |
18 class V4Database; | 18 class V4Database; |
19 | 19 |
20 typedef base::Callback<void(std::unique_ptr<V4Database>)> | 20 typedef base::Callback<void(std::unique_ptr<V4Database>)> |
21 NewDatabaseReadyCallback; | 21 NewDatabaseReadyCallback; |
22 | 22 |
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 // The set of interesting lists and ASCII filenames for their hash prefix | 28 // Maps the UpdateListIdentifiers to their corresponding in-memory stores, which |
29 // stores. The stores are created inside the user-data directory. | 29 // contain the hash prefixes for that UpdateListIdentifier as well as manage |
30 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | 30 // their storage on disk. |
31 // on Windows platform, and the corresponding file on disk could be named: | 31 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
32 // "uws_win_url.store" | 32 |
33 // TODO(vakh): Find the canonical place where these are defined and update the | 33 // TODO(vakh): Find the canonical place where these are defined and update the |
34 // comment to point to that place. | 34 // comment to point to that place. |
35 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | 35 struct StoreIdAndFileName { |
| 36 // The list being read from/written to the disk. |
| 37 UpdateListIdentifier list_id; |
36 | 38 |
37 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory | 39 // The ASCII name of the file on disk. This file is created inside the |
38 // stores, which contain the hash prefixes for that UpdateListIdentifier as well | 40 // user-data directory. For instance, the UpdateListIdentifier could be for |
39 // as manage their storage on disk. | 41 // URL expressions for UwS on Windows platform, and the corresponding file on |
40 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 42 // disk could be named: "UrlUws.store" |
| 43 std::string filename; |
| 44 |
| 45 StoreIdAndFileName(const UpdateListIdentifier& list_id, |
| 46 const std::string& filename); |
| 47 ~StoreIdAndFileName(); |
| 48 |
| 49 private: |
| 50 StoreIdAndFileName(); |
| 51 }; |
| 52 |
| 53 using StoreIdAndFileNames = std::vector<StoreIdAndFileName>; |
41 | 54 |
42 // Factory for creating V4Database. Tests implement this factory to create fake | 55 // Factory for creating V4Database. Tests implement this factory to create fake |
43 // databases for testing. | 56 // databases for testing. |
44 class V4DatabaseFactory { | 57 class V4DatabaseFactory { |
45 public: | 58 public: |
46 virtual ~V4DatabaseFactory() {} | 59 virtual ~V4DatabaseFactory() {} |
47 virtual V4Database* CreateV4Database( | 60 virtual V4Database* CreateV4Database( |
48 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 61 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
49 const base::FilePath& base_dir_path, | 62 const base::FilePath& base_dir_path, |
50 const StoreFileNameMap& store_file_name_map) = 0; | 63 const StoreIdAndFileNames& store_id_file_names) = 0; |
51 }; | 64 }; |
52 | 65 |
53 // The on-disk databases are shared among all profiles, as it doesn't contain | 66 // The on-disk databases are shared among all profiles, as it doesn't contain |
54 // user-specific data. This object is not thread-safe, i.e. all its methods | 67 // user-specific data. This object is not thread-safe, i.e. all its methods |
55 // should be used on the same thread that it was created on, unless specified | 68 // should be used on the same thread that it was created on, unless specified |
56 // otherwise. | 69 // otherwise. |
57 // The hash-prefixes of each type are managed by a V4Store (including saving to | 70 // The hash-prefixes of each type are managed by a V4Store (including saving to |
58 // and reading from disk). | 71 // and reading from disk). |
59 // The V4Database serves as a single place to manage all the V4Stores. | 72 // The V4Database serves as a single place to manage all the V4Stores. |
60 class V4Database { | 73 class V4Database { |
61 public: | 74 public: |
62 // Factory method to create a V4Database. It creates the database on the | 75 // Factory method to create a V4Database. It creates the database on the |
63 // provided |db_task_runner| containing stores in |store_file_name_map|. When | 76 // provided |db_task_runner| containing stores in |store_file_name_map|. When |
64 // the database creation is complete, it runs the NewDatabaseReadyCallback on | 77 // the database creation is complete, it runs the NewDatabaseReadyCallback on |
65 // the same thread as it was called. | 78 // the same thread as it was called. |
66 static void Create( | 79 static void Create( |
67 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 80 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
68 const base::FilePath& base_path, | 81 const base::FilePath& base_path, |
69 const StoreFileNameMap& store_file_name_map, | 82 const StoreIdAndFileNames& store_id_file_names, |
70 NewDatabaseReadyCallback callback); | 83 NewDatabaseReadyCallback callback); |
71 | 84 |
72 // Destroys the provided v4_database on its task_runner since this may be a | 85 // Destroys the provided v4_database on its task_runner since this may be a |
73 // long operation. | 86 // long operation. |
74 static void Destroy(std::unique_ptr<V4Database> v4_database); | 87 static void Destroy(std::unique_ptr<V4Database> v4_database); |
75 | 88 |
76 virtual ~V4Database(); | 89 virtual ~V4Database(); |
77 | 90 |
78 // Updates the stores with the response received from the SafeBrowsing service | 91 // Updates the stores with the response received from the SafeBrowsing service |
79 // and calls the db_updated_callback when done. | 92 // and calls the db_updated_callback when done. |
80 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, | 93 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, |
81 DatabaseUpdatedCallback db_updated_callback); | 94 DatabaseUpdatedCallback db_updated_callback); |
82 | 95 |
83 // Returns the current state of each of the stores being managed. | 96 // Returns the current state of each of the stores being managed. |
84 std::unique_ptr<StoreStateMap> GetStoreStateMap(); | 97 std::unique_ptr<StoreStateMap> GetStoreStateMap(); |
85 | 98 |
86 // Searches for a hash prefix matching the |full_hash| in stores in the | 99 // Searches for a hash prefix matching the |full_hash| in stores in the |
87 // database, filtered by |stores_to_look|, and returns the identifier of the | 100 // database, filtered by |stores_to_look|, and returns the identifier of the |
88 // store along with the matching hash prefix in |matched_hash_prefix_map|. | 101 // store along with the matching hash prefix in |matched_hash_prefix_map|. |
89 virtual void GetStoresMatchingFullHash( | 102 virtual void GetStoresMatchingFullHash( |
90 const FullHash& full_hash, | 103 const FullHash& full_hash, |
91 const base::hash_set<UpdateListIdentifier>& stores_to_look, | 104 const std::unordered_set<UpdateListIdentifier>& stores_to_look, |
92 StoreAndHashPrefixes* matched_store_and_full_hashes); | 105 StoreAndHashPrefixes* matched_store_and_full_hashes); |
93 | 106 |
94 // Deletes the current database and creates a new one. | 107 // Deletes the current database and creates a new one. |
95 virtual bool ResetDatabase(); | 108 virtual bool ResetDatabase(); |
96 | 109 |
97 protected: | 110 protected: |
98 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 111 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
99 std::unique_ptr<StoreMap> store_map); | 112 std::unique_ptr<StoreMap> store_map); |
100 | 113 |
101 private: | 114 private: |
(...skipping 11 matching lines...) Expand all Loading... |
113 // for tests. | 126 // for tests. |
114 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 127 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
115 factory_ = factory; | 128 factory_ = factory; |
116 } | 129 } |
117 | 130 |
118 // Factory method to create a V4Database. When the database creation is | 131 // Factory method to create a V4Database. When the database creation is |
119 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 132 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
120 static void CreateOnTaskRunner( | 133 static void CreateOnTaskRunner( |
121 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 134 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
122 const base::FilePath& base_path, | 135 const base::FilePath& base_path, |
123 const StoreFileNameMap& store_file_name_map, | 136 const StoreIdAndFileNames& store_id_file_names, |
124 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 137 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
125 NewDatabaseReadyCallback callback); | 138 NewDatabaseReadyCallback callback); |
126 | 139 |
127 // Callback called when a new store has been created and is ready to be used. | 140 // Callback called when a new store has been created and is ready to be used. |
128 // This method updates the store_map_ to point to the new store, which causes | 141 // This method updates the store_map_ to point to the new store, which causes |
129 // the old store to get deleted. | 142 // the old store to get deleted. |
130 void UpdatedStoreReady(UpdateListIdentifier identifier, | 143 void UpdatedStoreReady(UpdateListIdentifier identifier, |
131 std::unique_ptr<V4Store> store); | 144 std::unique_ptr<V4Store> store); |
132 | 145 |
133 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 146 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
(...skipping 11 matching lines...) Expand all Loading... |
145 // that needed updating and is ready for the next update. It should only be | 158 // that needed updating and is ready for the next update. It should only be |
146 // accessed on the IO thread. | 159 // accessed on the IO thread. |
147 int pending_store_updates_; | 160 int pending_store_updates_; |
148 | 161 |
149 DISALLOW_COPY_AND_ASSIGN(V4Database); | 162 DISALLOW_COPY_AND_ASSIGN(V4Database); |
150 }; | 163 }; |
151 | 164 |
152 } // namespace safe_browsing | 165 } // namespace safe_browsing |
153 | 166 |
154 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 167 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |