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

Side by Side Diff: extensions/browser/api/lock_screen_data/lock_screen_item_storage.h

Issue 2934293003: The chrome.lockScreen.data API implementation (Closed)
Patch Set: switch to BackendTaskRunner Created 3 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
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_LOCK_SCREEN_ITEM_STORAGE_H_
6 #define EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_LOCK_SCREEN_ITEM_STORAGE_H_
7
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/scoped_observer.h"
19 #include "base/sequenced_task_runner.h"
20 #include "extensions/browser/api/lock_screen_data/data_item.h"
21 #include "extensions/browser/extension_registry_observer.h"
22
23 class PrefRegistrySimple;
24 class PrefService;
25
26 namespace content {
27 class BrowserContext;
28 }
29
30 namespace extensions {
31
32 class Extension;
33 class ExtensionRegistry;
34 class LocalValueStoreCache;
35
36 namespace lock_screen_data {
37
38 class DataItem;
39 enum class OperationResult;
40
41 // Keeps track of lock screen data items created by extensions.
42 // Note that only a single instance is allowed per process - once created, the
43 // instance can be retrieved using LockScreenItemStorage::GetIfAllowed().
44 class LockScreenItemStorage : public ExtensionRegistryObserver {
45 public:
46 using CreateCallback =
47 base::Callback<void(OperationResult result, const DataItem* item)>;
48 using DataItemListCallback =
49 base::Callback<void(const std::vector<const DataItem*>& items)>;
50 using WriteCallback = DataItem::WriteCallback;
51 using ReadCallback = DataItem::ReadCallback;
52
53 static void RegisterLocalState(PrefRegistrySimple* registry);
54
55 // Gets the global LockScreenItemStorage instance if lock screen item storage
56 // can be used from the browser context.
57 // |context| - Context from which the item storage is needed, if the context
58 // is not allowed to use LockScreenItemStorage, this will return nullptr.
59 // LockScreenItemStorage can be only used from:
60 // * lock screen browser context while session state is set to locked
61 // * from the browser process passed to the LockScreenItemStorage ctor when
62 // the session state is set to unlocked.
63 static LockScreenItemStorage* GetIfAllowed(content::BrowserContext* context);
64
65 // Creates a LockScreenItemStorage instance. Note that only one
66 // LockScreenItemStorage can be present at a time - the constructor will set a
67 // global reference to the created object that can be retrieved using
68 // LockScreenItemStorage::GetIfAllowed (the reference will be reset when the
69 // instance goes out of scoped, i.e. in ~LockScreenItemStorage).
70 //
71 // |context| - primary user context, only apps from that context should be
72 // able to use the storage.
73 // |local_state| - Local state preference.
74 // |crypto_key| - Symmetric key that should be used to encrypt data content
75 // when it's persisted on the disk.
76 // |storage_root| - Directory on the disk to which data item content should be
77 // persisted.
78 LockScreenItemStorage(content::BrowserContext* context,
79 PrefService* local_state,
80 const std::string& crypto_key,
81 const base::FilePath& storage_root);
82 ~LockScreenItemStorage() override;
83
84 // Updates the LockScreenItemStorage's view of whether the user session is
85 // locked.
86 void SetSessionLocked(bool session_locked);
87
88 // Creates a new data item for the extension.
89 void CreateItem(const std::string& extension_id,
90 const CreateCallback& callback);
91
92 // Returns all existing data items associated with the extension.
93 void GetAllForExtension(const std::string& extension_id,
94 const DataItemListCallback& callback);
95
96 // Updates the content of the item identified by |item_id| that is associated
97 // with the provided extension.
98 void SetItemContent(const std::string& extension_id,
99 const std::string& item_id,
100 const std::vector<char>& data,
101 const WriteCallback& callback);
102
103 // Retrieves the content of the item identified by |item_id| that is
104 // associated with the provided extension.
105 void GetItemContent(const std::string& extension_id,
106 const std::string& item_id,
107 const ReadCallback& callback);
108
109 // Deletes the data item associated with the extension.
110 void DeleteItem(const std::string& extension_id,
111 const std::string& item_id,
112 const WriteCallback& callback);
113
114 // extensions::ExtensionRegistryObserver:
115 void OnExtensionUninstalled(content::BrowserContext* browser_context,
116 const Extension* extension,
117 UninstallReason reason) override;
118
119 // Used in tests to inject fake data items implementations.
120 using ItemFactoryCallback =
121 base::Callback<std::unique_ptr<DataItem>(const std::string& id,
122 const std::string& extension_id,
123 const std::string& crypto_key)>;
124 using RegisteredItemsGetter =
125 base::Callback<void(const std::string& extension_id,
126 const DataItem::RegisteredValuesCallback& callback)>;
127 using ItemStoreDeleter = base::Callback<void(const std::string& extension_id,
128 const base::Closure& callback)>;
129 static void SetItemProvidersForTesting(
130 RegisteredItemsGetter* item_fetch_callback,
131 ItemFactoryCallback* factory_callback,
132 ItemStoreDeleter* deleter_callback);
133
134 private:
135 // Maps a data item ID to the data item instance.
136 using DataItemMap =
137 std::unordered_map<std::string, std::unique_ptr<DataItem>>;
138
139 // Contains information about cached data item information for an extension -
140 // in particular, the list of existing data items.
141 struct CachedExtensionData {
142 enum class State {
143 // The set of registered data items has not been requested.
144 kInitial,
145 // The initial set of registered data items is being loaded.
146 kLoading,
147 // The set of registered data items has been loaded; the cached list of
148 // items should be up to date.
149 kLoaded,
150 };
151
152 CachedExtensionData();
153 ~CachedExtensionData();
154
155 State state = State::kInitial;
156 // The set of registered data items.
157 DataItemMap data_items;
158 // When the initial data item list is being loaded, contains the callback
159 // waiting for the load to finish. When the initial data item set is loaded,
160 // all of the callbacks in this list will be run.
161 std::vector<base::Closure> load_callbacks;
162 };
163
164 // Maps an extension ID to data items associated with the extension.
165 using ExtensionDataMap = std::unordered_map<std::string, CachedExtensionData>;
166
167 // The session state as seen by the LockScreenItemStorage.
168 enum class SessionLockedState { kUnknown, kLocked, kNotLocked };
169
170 // Whether the context is allowed to use LockScreenItemStorage.
171 // Result depends on the current LockScreenItemStorage state - see
172 // |LockScreenItemStorage::GetIfAllowed|.
173 bool IsContextAllowed(content::BrowserContext* context);
174
175 // Implementations for data item management methods - called when the data
176 // cache for the associated extension was initialized:
177 void CreateItemImpl(const std::string& extension_id,
178 const CreateCallback& callback);
179 void GetAllForExtensionImpl(const std::string& extension_id,
180 const DataItemListCallback& callback);
181 void SetItemContentImpl(const std::string& extension_id,
182 const std::string& item_id,
183 const std::vector<char>& data,
184 const WriteCallback& callback);
185 void GetItemContentImpl(const std::string& extension_id,
186 const std::string& item_id,
187 const ReadCallback& callback);
188 void DeleteItemImpl(const std::string& extension_id,
189 const std::string& item_id,
190 const WriteCallback& callback);
191
192 // Defers |callback| until the cached data item set is loaded for the
193 // extension. If the data is already loaded, |callback| will be run
194 // immediately.
195 void EnsureCacheForExtensionLoaded(const std::string& extension_id,
196 const base::Closure& callback);
197
198 // Callback for loading initial set of known data items for an extension.
199 void OnGotExtensionItems(const std::string& extension_id,
200 OperationResult result,
201 std::unique_ptr<base::DictionaryValue> items);
202
203 // Callback for registration operation of a newly created data item - it adds
204 // the item to the cached data item state, and invoked the callback.
205 void OnItemRegistered(std::unique_ptr<DataItem> item,
206 const std::string& extension_id,
207 const CreateCallback& callback,
208 OperationResult result);
209
210 // Callback for item deletion operation. It removes the item from the cached
211 // state and invokes the callback with the operation result.
212 void OnItemDeleted(const std::string& extension_id,
213 const std::string& item_id,
214 const WriteCallback& callback,
215 OperationResult result);
216
217 // Finds a data item associated with the extension.
218 DataItem* FindItem(const std::string& extension_id,
219 const std::string& item_id);
220
221 // Gets the set of extensions that are known to have registered data items -
222 // this information is kept in the local state so it can be used to
223 // synchronously determine the set of extensions that should be sent a
224 // DataItemsAvailable event, and to determine the set of extensions whose data
225 // should be deleted (for example if they are not installed anymore).
226 std::set<std::string> GetExtensionsWithDataItems(bool include_empty);
227
228 // Deletes data item data for all extensions that have existing data items,
229 // but are not currently installed.
230 void ClearUninstalledAppData();
231
232 // Clears all data items associated with the extension.
233 void ClearExtensionData(const std::string& id);
234
235 // Removes local state entry for the extension.
236 void RemoveExtensionFromLocalState(const std::string& id);
237
238 content::BrowserContext* context_;
239
240 // The user associated with the primary profile.
241 const std::string user_id_;
242 const std::string crypto_key_;
243 PrefService* local_state_;
244 const base::FilePath storage_root_;
245
246 SessionLockedState session_locked_state_ = SessionLockedState::kUnknown;
247
248 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
249 extension_registry_observer_;
250
251 // Expected to be used only on FILE thread.
252 std::unique_ptr<LocalValueStoreCache> value_store_cache_;
253
254 // Mapping containing all known data items for extensions.
255 ExtensionDataMap data_item_cache_;
256
257 scoped_refptr<base::SequencedTaskRunner> task_runner_;
258
259 base::WeakPtrFactory<LockScreenItemStorage> weak_ptr_factory_;
260
261 DISALLOW_COPY_AND_ASSIGN(LockScreenItemStorage);
262 };
263
264 } // namespace lock_screen_data
265 } // namespace extensions
266
267 #endif // EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_LOCK_SCREEN_ITEM_STORAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698