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

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

Issue 2062013002: Fetch incremental updates. Store new state in V4Store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor: Fix DCHECK Created 4 years, 5 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/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 scheduled once the database has finished processing the
23 // stores that contain the hash-prefixes. The map key identifies the list that 24 // update 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> StoreFileNameMap;
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 StoreFileNameMap& store_file_name_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 in |store_file_name_map|. When
59 // the NewDatabaseReadyCallback on the same thread as it was called. 55 // the database creation is complete, it runs the NewDatabaseReadyCallback on
56 // the same thread as it was called.
60 static void Create( 57 static void Create(
61 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 58 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
62 const base::FilePath& base_path, 59 const base::FilePath& base_path,
63 const StoreFileNameMap& store_file_name_map, 60 const StoreFileNameMap& store_file_name_map,
64 NewDatabaseReadyCallback callback); 61 NewDatabaseReadyCallback callback);
65 62
66 // Destroys the provided v4_database on its task_runner since this may be a 63 // Destroys the provided v4_database on its task_runner since this may be a
67 // long operation. 64 // long operation.
68 static void Destroy(std::unique_ptr<V4Database> v4_database); 65 static void Destroy(std::unique_ptr<V4Database> v4_database);
69 66
70 virtual ~V4Database(); 67 virtual ~V4Database();
71 68
69 // Updates the stores with the response received from the SafeBrowsing service
70 // and calls the db_updated_callback when done.
71 void ApplyUpdate(const std::vector<ListUpdateResponse>& response,
72 DatabaseUpdatedCallback db_updated_callback);
73
74 // Returns the current state of each of the stores being managed.
75 std::unique_ptr<StoreStateMap> GetStoreStateMap();
76
72 // Deletes the current database and creates a new one. 77 // Deletes the current database and creates a new one.
73 virtual bool ResetDatabase(); 78 virtual bool ResetDatabase();
74 79
75 protected: 80 protected:
76 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 81 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
77 std::unique_ptr<StoreMap> store_map); 82 std::unique_ptr<StoreMap> store_map);
78 83
79 private: 84 private:
80 friend class SafeBrowsingV4DatabaseTest; 85 friend class SafeBrowsingV4DatabaseTest;
81 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, 86 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest,
82 TestSetupDatabaseWithFakeStores); 87 TestSetupDatabaseWithFakeStores);
83 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, 88 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest,
84 TestSetupDatabaseWithFakeStoresFailsReset); 89 TestSetupDatabaseWithFakeStoresFailsReset);
90 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest,
91 TestApplyUpdateWithNewStates);
92 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest,
93 TestApplyUpdateWithNoNewState);
94 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest,
95 TestApplyUpdateWithEmptyUpdate);
85 96
86 // Makes the passed |factory| the factory used to instantiate a V4Store. Only 97 // Makes the passed |factory| the factory used to instantiate a V4Store. Only
87 // for tests. 98 // for tests.
88 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { 99 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) {
89 factory_ = factory; 100 factory_ = factory;
90 } 101 }
91 102
92 // Factory method to create a V4Database. When the database creation is 103 // Factory method to create a V4Database. When the database creation is
93 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. 104 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|.
94 static void CreateOnTaskRunner( 105 static void CreateOnTaskRunner(
95 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 106 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
96 const base::FilePath& base_path, 107 const base::FilePath& base_path,
97 const StoreFileNameMap& store_file_name_map, 108 const StoreFileNameMap& store_file_name_map,
98 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 109 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
99 NewDatabaseReadyCallback callback); 110 NewDatabaseReadyCallback callback);
100 111
112 // Callback called when a new store has been created and is ready to be used.
113 // This method updates the store_map_ to point to the new store, which causes
114 // the old store to get deleted.
115 void UpdatedStoreReady(UpdateListIdentifier identifier,
116 std::unique_ptr<V4Store> store);
117
101 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; 118 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_;
102 119
103 // Map of UpdateListIdentifier to the V4Store. 120 // Map of UpdateListIdentifier to the V4Store.
104 const std::unique_ptr<StoreMap> store_map_; 121 const std::unique_ptr<StoreMap> store_map_;
105 122
123 DatabaseUpdatedCallback db_updated_callback_;
124
106 // The factory that controls the creation of V4Store objects. 125 // The factory that controls the creation of V4Store objects.
107 static V4StoreFactory* factory_; 126 static V4StoreFactory* factory_;
108 127
128 // The number of stores for which the update request is pending. When this
129 // goes down to 0, that indicates that the database has updated all the stores
130 // that needed updating and is ready for the next update. It should only be
131 // accessed on the IO thread.
132 int pending_store_updates_;
133
109 DISALLOW_COPY_AND_ASSIGN(V4Database); 134 DISALLOW_COPY_AND_ASSIGN(V4Database);
110 }; 135 };
111 136
112 } // namespace safe_browsing 137 } // namespace safe_browsing
113 138
114 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ 139 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698