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 12 matching lines...) Expand all Loading... |
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 // Maps the ListIdentifiers to their corresponding in-memory stores, which | 28 // Maps the ListIdentifiers to their corresponding in-memory stores, which |
29 // contain the hash prefixes for that ListIdentifier as well as manage their | 29 // contain the hash prefixes for that ListIdentifier as well as manage their |
30 // storage on disk. | 30 // storage on disk. |
31 typedef base::hash_map<ListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 31 typedef base::hash_map<ListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
32 | 32 |
33 // TODO(vakh): Find the canonical place where these are defined and update the | 33 // Associates metadata for a list with its ListIdentifier. |
34 // comment to point to that place. | 34 struct ListInfo { |
35 struct StoreIdAndFileName { | 35 ListInfo(const bool fetch_updates, |
36 // The list being read from/written to the disk. | 36 const std::string& filename, |
37 ListIdentifier list_id; | 37 const ListIdentifier& list_id, |
| 38 const SBThreatType sb_threat_type); |
| 39 ~ListInfo(); |
| 40 |
| 41 ListIdentifier list_id() const { return list_id_; } |
| 42 std::string filename() const { return filename_; } |
| 43 SBThreatType sb_threat_type() const { return sb_threat_type_; } |
| 44 bool fetch_updates() const { return fetch_updates_; } |
| 45 |
| 46 private: |
| 47 // Whether to fetch and store updates for this list. |
| 48 bool fetch_updates_; |
38 | 49 |
39 // The ASCII name of the file on disk. This file is created inside the | 50 // The ASCII name of the file on disk. This file is created inside the |
40 // user-data directory. For instance, the ListIdentifier could be for URL | 51 // user-data directory. For instance, the ListIdentifier could be for URL |
41 // expressions for UwS on Windows platform, and the corresponding file on disk | 52 // expressions for UwS on Windows platform, and the corresponding file on disk |
42 // could be named: "UrlUws.store" | 53 // could be named: "UrlUws.store" |
43 std::string filename; | 54 std::string filename_; |
44 | 55 |
45 StoreIdAndFileName(const ListIdentifier& list_id, | 56 // The list being read from/written to the disk. |
46 const std::string& filename); | 57 ListIdentifier list_id_; |
47 ~StoreIdAndFileName(); | |
48 | 58 |
49 private: | 59 // The threat type enum value for this store. |
50 StoreIdAndFileName(); | 60 SBThreatType sb_threat_type_; |
| 61 |
| 62 ListInfo(); |
51 }; | 63 }; |
52 | 64 |
53 using StoreIdAndFileNames = std::vector<StoreIdAndFileName>; | 65 typedef std::vector<ListInfo> ListInfos; |
54 | 66 |
55 // Factory for creating V4Database. Tests implement this factory to create fake | 67 // Factory for creating V4Database. Tests implement this factory to create fake |
56 // databases for testing. | 68 // databases for testing. |
57 class V4DatabaseFactory { | 69 class V4DatabaseFactory { |
58 public: | 70 public: |
59 virtual ~V4DatabaseFactory() {} | 71 virtual ~V4DatabaseFactory() {} |
60 virtual V4Database* CreateV4Database( | 72 virtual V4Database* CreateV4Database( |
61 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 73 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
62 const base::FilePath& base_dir_path, | 74 const base::FilePath& base_dir_path, |
63 const StoreIdAndFileNames& store_id_file_names) = 0; | 75 const ListInfos& list_infos) = 0; |
64 }; | 76 }; |
65 | 77 |
66 // The on-disk databases are shared among all profiles, as it doesn't contain | 78 // The on-disk databases are shared among all profiles, as it doesn't contain |
67 // user-specific data. This object is not thread-safe, i.e. all its methods | 79 // user-specific data. This object is not thread-safe, i.e. all its methods |
68 // should be used on the same thread that it was created on, unless specified | 80 // should be used on the same thread that it was created on, unless specified |
69 // otherwise. | 81 // otherwise. |
70 // The hash-prefixes of each type are managed by a V4Store (including saving to | 82 // The hash-prefixes of each type are managed by a V4Store (including saving to |
71 // and reading from disk). | 83 // and reading from disk). |
72 // The V4Database serves as a single place to manage all the V4Stores. | 84 // The V4Database serves as a single place to manage all the V4Stores. |
73 class V4Database { | 85 class V4Database { |
74 public: | 86 public: |
75 // Factory method to create a V4Database. It creates the database on the | 87 // Factory method to create a V4Database. It creates the database on the |
76 // provided |db_task_runner| containing stores in |store_file_name_map|. When | 88 // provided |db_task_runner| containing stores in |store_file_name_map|. When |
77 // the database creation is complete, it runs the NewDatabaseReadyCallback on | 89 // the database creation is complete, it runs the NewDatabaseReadyCallback on |
78 // the same thread as it was called. | 90 // the same thread as it was called. |
79 static void Create( | 91 static void Create( |
80 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 92 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
81 const base::FilePath& base_path, | 93 const base::FilePath& base_path, |
82 const StoreIdAndFileNames& store_id_file_names, | 94 const ListInfos& list_infos, |
83 NewDatabaseReadyCallback callback); | 95 NewDatabaseReadyCallback callback); |
84 | 96 |
85 // Destroys the provided v4_database on its task_runner since this may be a | 97 // Destroys the provided v4_database on its task_runner since this may be a |
86 // long operation. | 98 // long operation. |
87 static void Destroy(std::unique_ptr<V4Database> v4_database); | 99 static void Destroy(std::unique_ptr<V4Database> v4_database); |
88 | 100 |
89 virtual ~V4Database(); | 101 virtual ~V4Database(); |
90 | 102 |
91 // Updates the stores with the response received from the SafeBrowsing service | 103 // Updates the stores with the response received from the SafeBrowsing service |
92 // and calls the db_updated_callback when done. | 104 // and calls the db_updated_callback when done. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // for tests. | 138 // for tests. |
127 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 139 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
128 factory_ = factory; | 140 factory_ = factory; |
129 } | 141 } |
130 | 142 |
131 // Factory method to create a V4Database. When the database creation is | 143 // Factory method to create a V4Database. When the database creation is |
132 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 144 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
133 static void CreateOnTaskRunner( | 145 static void CreateOnTaskRunner( |
134 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 146 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
135 const base::FilePath& base_path, | 147 const base::FilePath& base_path, |
136 const StoreIdAndFileNames& store_id_file_names, | 148 const ListInfos& list_infos, |
137 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 149 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
138 NewDatabaseReadyCallback callback); | 150 NewDatabaseReadyCallback callback); |
139 | 151 |
140 // Callback called when a new store has been created and is ready to be used. | 152 // Callback called when a new store has been created and is ready to be used. |
141 // This method updates the store_map_ to point to the new store, which causes | 153 // This method updates the store_map_ to point to the new store, which causes |
142 // the old store to get deleted. | 154 // the old store to get deleted. |
143 void UpdatedStoreReady(ListIdentifier identifier, | 155 void UpdatedStoreReady(ListIdentifier identifier, |
144 std::unique_ptr<V4Store> store); | 156 std::unique_ptr<V4Store> store); |
145 | 157 |
146 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 158 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
(...skipping 11 matching lines...) Expand all Loading... |
158 // that needed updating and is ready for the next update. It should only be | 170 // that needed updating and is ready for the next update. It should only be |
159 // accessed on the IO thread. | 171 // accessed on the IO thread. |
160 int pending_store_updates_; | 172 int pending_store_updates_; |
161 | 173 |
162 DISALLOW_COPY_AND_ASSIGN(V4Database); | 174 DISALLOW_COPY_AND_ASSIGN(V4Database); |
163 }; | 175 }; |
164 | 176 |
165 } // namespace safe_browsing | 177 } // namespace safe_browsing |
166 | 178 |
167 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 179 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |