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