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/item_storage.h

Issue 2934293003: The chrome.lockScreen.data API implementation (Closed)
Patch Set: remove FilePath*UTF8Unsafe methods Created 3 years, 6 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_ITEM_STORAGE_H_
6 #define EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_ITEM_STORAGE_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/scoped_observer.h"
18 #include "base/sequenced_task_runner.h"
19 #include "extensions/browser/api/lock_screen_data/data_item.h"
20 #include "extensions/browser/extension_registry_observer.h"
21
22 class PrefRegistrySimple;
23 class PrefService;
24
25 namespace base {
26 class Value;
27 }
28
29 namespace content {
30 class BrowserContext;
31 }
32
33 namespace extensions {
34
35 class Extension;
36 class ExtensionRegistry;
37
38 namespace lock_screen_data {
39
40 class DataItem;
41 enum class OperationResult;
42
43 // Keeps track of lock screen data items created by extensions.
44 // Note only single instance is allowed per process - once created, the instance
45 // can be retrieved using ItemStorage::Get().
46 class ItemStorage : public ExtensionRegistryObserver {
47 public:
48 using WriteCallback = DataItem::WriteCallback;
49 using ReadCallback = DataItem::ReadCallback;
50
51 static void RegisterLocalState(PrefRegistrySimple* registry);
52
53 // Gets the global ItemStorage instance.
54 // |context| - Context from which the item storage is needed, if the context
55 // is not allowed to use ItemStorage, this will return null.
56 // ItemStorage can be only used from:
57 // * lock screen browser context while session state is set to locked
58 // * from the browser process passed to the ItemStorage ctor when the
rkc 2017/06/22 18:35:48 Should ItemStorage be context keyed? What happens
tbarzic 2017/06/26 22:21:42 It will be supported only for the primary user.
59 // session state is set to unlocked.
60 static ItemStorage* Get(content::BrowserContext* context);
61
62 // Creates a ItemStorage instance. Note that only one ItemStorage can be
63 // present at a time - the constructor will set a global reference to the
64 // created object that can be retrieved using ItemStorage::Get (the reference
65 // will be reset when the instance goes out of scoped, i.e. in ~ItemStorage).
66 //
67 // |context| - primary user context, only apps from that context should be
68 // able to use the storage.
69 // |local_state| - Local state preference - used to persist set of created
70 // item metadata.
71 // |crypto_key| - Symmetric key that should be used to encrypt data content
72 // when it's persisted on the disk.
73 // |storage_root| - Directory on the disk to which data item content should be
74 // persisted.
75 ItemStorage(content::BrowserContext* context,
76 PrefService* local_state,
77 const std::string& crypto_key,
78 const base::FilePath& storage_root);
79 ~ItemStorage() override;
80
81 using ItemFactoryCallback =
82 base::Callback<std::unique_ptr<DataItem>(const std::string& id)>;
83 static void SetItemFactoryForTesting(ItemFactoryCallback* callback);
84
85 // Updates the ItemStorage's view of whether the user session is locked.
86 void SetSessionLocked(bool session_locked);
87
88 // Creates a new data item for the extension.
89 const DataItem* CreateItem(const std::string& extension_id);
90
91 // Returns all existing data items associated with the extension.
92 std::vector<const DataItem*> GetAllForExtension(
93 const std::string& extension_id);
94
95 // Updates the content of the item identified by |item_id| that is associated
96 // with the provided extension.
97 OperationResult SetItemContent(const std::string& extension_id,
rkc 2017/06/22 18:35:48 Same comment as on the DataItem class. Can we avoi
tbarzic 2017/06/26 22:21:42 Done.
98 const std::string& item_id,
99 const std::vector<char>& data,
100 const WriteCallback& callback);
101
102 // Retrieves the content of the item identified by |item_id| that is
103 // associated with the provided extension.
104 OperationResult GetItemContent(const std::string& extension_id,
105 const std::string& item_id,
106 const ReadCallback& callback);
107
108 // Deletes the data item associated with the extension.
109 OperationResult DeleteItem(const std::string& extension_id,
110 const std::string& item_id);
111
112 // extensions::ExtensionRegistryObserver:
113 void OnExtensionUninstalled(content::BrowserContext* browser_context,
114 const Extension* extension,
115 UninstallReason reason) override;
116
117 // Exposed so test code can ensure that all tasks on the task runner are run
118 // before testing an expectation.
119 scoped_refptr<base::SequencedTaskRunner> task_runner_for_testing() {
120 return task_runner_;
121 }
122
123 private:
124 // Maps a data item ID to the data item instance.
125 using DataItemMap = std::map<std::string, std::unique_ptr<DataItem>>;
rkc 2017/06/22 18:35:48 would using unordered_map here and below be better
tbarzic 2017/06/26 22:21:42 hm, I'm not sure. it might.
126 // Maps an extension ID to data items associated with the extension.
127 using ExtensionDataItemMap = std::map<std::string, DataItemMap>;
128
129 // The session state as seen by the ItemStorage.
130 enum class SessionLockedState { kUnknown, kLocked, kNotLocked };
131
132 // Gets the key in lock screen data preferences for the item.
133 std::string GetDataItemPrefKey(const std::string& extension_id,
134 const std::string& item_id);
135
136 // Whether the context is allowed to use ItemStorage.
137 // Result depends on the current ItemStorage state - see ItemStorage::Get.
138 bool IsContextAllowed(content::BrowserContext* context);
139
140 // Finds a data item associated with the extension.
141 DataItem* FindItem(const std::string& extension_id,
142 const std::string& item_id);
143
144 // Clears all data items associated with the extension.
145 void ClearDataForExtension(const std::string& extension_id);
146
147 // Used on startup to load extension -> data item mapping from local state
148 // prefs.
149 void ReloadDataItems();
150 bool LoadDataItemsForExtension(const std::string& extension_id,
151 const base::Value& extension_pref);
152
153 content::BrowserContext* context_;
154
155 // The user associated with the primary profile.
156 const std::string user_id_;
157 const std::string crypto_key_;
158 PrefService* local_state_;
159 const base::FilePath storage_root_;
160
161 SessionLockedState session_locked_state_ = SessionLockedState::kUnknown;
162
163 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
164 extension_registry_observer_;
165
166 // Mapping containing all known data items for extensions.
167 ExtensionDataItemMap data_items_;
168
169 scoped_refptr<base::SequencedTaskRunner> task_runner_;
170
171 DISALLOW_COPY_AND_ASSIGN(ItemStorage);
172 };
173
174 } // namespace lock_screen_data
175 } // namespace extensions
176
177 #endif // EXTENSIONS_BROWSER_API_LOCK_SCREEN_DATA_ITEM_STORAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698