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 |
Scott Hess - ex-Googler
2016/06/17 22:53:42
I don't think Run is a proper noun in this context
vakh (use Gerrit instead)
2016/06/20 22:28:42
Changed to scheduled.
Scott Hess - ex-Googler
2016/06/21 21:03:44
:-). Should have been more clear, I meant that "R
vakh (use Gerrit instead)
2016/06/21 23:19:35
I had a hunch that's what you meant but wasn't sur
| |
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 |
Scott Hess - ex-Googler
2016/06/17 22:53:42
Why the backslashes for this case?
What does it m
vakh (use Gerrit instead)
2016/06/20 22:28:42
Done.
| |
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 | |
Scott Hess - ex-Googler
2016/06/17 22:53:43
The ApplyUpdate snippet feels pretty contrived, li
vakh (use Gerrit instead)
2016/06/20 22:28:42
Removed the ApplyUpdate part of the comment since
| |
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. | |
Scott Hess - ex-Googler
2016/06/17 22:53:42
This last bit seems like an API mis-use, rather th
vakh (use Gerrit instead)
2016/06/20 22:28:42
Done.
| |
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 response received from the SafeBrowsing service | |
76 // and calls the db_updated_callback_ when done. | |
77 void ApplyUpdate(const std::vector<ListUpdateResponse>& response); | |
78 | |
79 const StoreStateMap* store_state_map() { return &store_state_map_; } | |
Scott Hess - ex-Googler
2016/06/17 22:53:42
This feels like it shouldn't be exposed to public:
vakh (use Gerrit instead)
2016/06/21 00:29:36
Done.
| |
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 identifier, | |
122 std::unique_ptr<V4Store> store); | |
123 | |
124 // Updates the store state map after reading the stores from disk on startup. | |
Scott Hess - ex-Googler
2016/06/17 22:53:42
This is called on startup?
vakh (use Gerrit instead)
2016/06/20 22:28:42
Updated to say: "when the database is being create
| |
125 void UpdateStoreStateMap(); | |
126 | |
101 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 127 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
102 | 128 |
103 // Map of UpdateListIdentifier to the V4Store. | 129 // Map of UpdateListIdentifier to the V4Store. |
104 const std::unique_ptr<StoreMap> store_map_; | 130 const std::unique_ptr<StoreMap> store_map_; |
105 | 131 |
132 // Map of UpdateListIdentifier to the state of the V4Store it represents. | |
133 StoreStateMap store_state_map_; | |
134 | |
135 DatabaseUpdatedCallback db_updated_callback_; | |
136 | |
106 // The factory that controls the creation of V4Store objects. | 137 // The factory that controls the creation of V4Store objects. |
107 static V4StoreFactory* factory_; | 138 static V4StoreFactory* factory_; |
108 | 139 |
140 // The number of stores for which the update request is pending. When this | |
141 // goes down to 0, that indicates that the database has updated all the stores | |
142 // that needed updating and is ready for the next update. | |
143 unsigned pending_store_updates_; | |
Scott Hess - ex-Googler
2016/06/17 22:53:42
I think countable things are recommended to be int
vakh (use Gerrit instead)
2016/06/20 22:28:42
Changed to int.
Scott Hess - ex-Googler
2016/06/21 21:03:44
OK. Mostly just worried about whether counting th
vakh (use Gerrit instead)
2016/06/21 23:19:35
Yes, I agree but I think in this particular case,
| |
144 | |
109 DISALLOW_COPY_AND_ASSIGN(V4Database); | 145 DISALLOW_COPY_AND_ASSIGN(V4Database); |
110 }; | 146 }; |
111 | 147 |
112 } // namespace safe_browsing | 148 } // namespace safe_browsing |
113 | 149 |
114 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 150 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |