Index: extensions/browser/api/lock_screen_data/item_storage.cc |
diff --git a/extensions/browser/api/lock_screen_data/item_storage.cc b/extensions/browser/api/lock_screen_data/item_storage.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..68d9b5f3dd1919c7a843a22e0299355135230cb3 |
--- /dev/null |
+++ b/extensions/browser/api/lock_screen_data/item_storage.cc |
@@ -0,0 +1,353 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "extensions/browser/api/lock_screen_data/item_storage.h" |
+ |
+#include <set> |
+#include <utility> |
+ |
+#include "base/files/file_util.h" |
+#include "base/guid.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/task_scheduler/post_task.h" |
+#include "base/task_scheduler/task_traits.h" |
+#include "base/values.h" |
+#include "components/prefs/pref_registry_simple.h" |
+#include "components/prefs/pref_service.h" |
+#include "components/prefs/scoped_user_pref_update.h" |
+#include "extensions/browser/api/lock_screen_data/data_item.h" |
+#include "extensions/browser/api/lock_screen_data/operation_result.h" |
+#include "extensions/browser/event_router.h" |
+#include "extensions/browser/extension_registry.h" |
+#include "extensions/browser/extensions_browser_client.h" |
+#include "extensions/common/api/lock_screen_data.h" |
+ |
+namespace extensions { |
+ |
+namespace lock_screen_data { |
+ |
+namespace { |
+ |
+ItemStorage* g_data_item_storage = nullptr; |
+ |
+ItemStorage::ItemFactoryCallback* g_test_item_factory_callback = nullptr; |
+ |
+const char kLockScreenDataPrefKey[] = "lockScreenDataItems"; |
+ |
+void DeleteImpl(const base::FilePath& file_path, bool recursive) { |
+ base::DeleteFile(file_path, recursive); |
+} |
+ |
+void DoNothingOnDelete(OperationResult result) {} |
+ |
+std::unique_ptr<DataItem> CreateDataItem(const std::string& item_id) { |
+ return g_test_item_factory_callback |
+ ? g_test_item_factory_callback->Run(item_id) |
+ : base::MakeUnique<DataItem>(item_id); |
+} |
+ |
+} // namespace |
+ |
+// static |
+ItemStorage* ItemStorage::Get(content::BrowserContext* context) { |
+ if (g_data_item_storage && !g_data_item_storage->IsContextAllowed(context)) |
+ return nullptr; |
+ return g_data_item_storage; |
rkc
2017/06/22 18:35:47
Nothing guarantees g_data_item_storage is not null
tbarzic
2017/06/26 22:21:42
Yes it's OK - what would happen is that the API me
|
+} |
+ |
+// static |
+void ItemStorage::RegisterLocalState(PrefRegistrySimple* registry) { |
+ registry->RegisterDictionaryPref(kLockScreenDataPrefKey); |
+} |
+ |
+ItemStorage::ItemStorage(content::BrowserContext* context, |
+ PrefService* local_state, |
+ const std::string& crypto_key, |
+ const base::FilePath& storage_root) |
+ : context_(context), |
+ user_id_( |
+ ExtensionsBrowserClient::Get()->GetUserIdHashFromContext(context)), |
+ crypto_key_(crypto_key), |
+ local_state_(local_state), |
+ storage_root_(storage_root.Append(user_id_)), |
+ extension_registry_observer_(this) { |
+ CHECK(!user_id_.empty()); |
+ extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
+ task_runner_ = base::CreateSequencedTaskRunnerWithTraits( |
+ {base::MayBlock(), base::TaskPriority::BACKGROUND, |
+ base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
+ |
+ ReloadDataItems(); |
+ |
+ DCHECK(!g_data_item_storage); |
+ g_data_item_storage = this; |
+} |
+ |
+ItemStorage::~ItemStorage() { |
+ DCHECK_EQ(g_data_item_storage, this); |
+ g_data_item_storage = nullptr; |
+} |
+ |
+// static |
+void ItemStorage::SetItemFactoryForTesting(ItemFactoryCallback* callback) { |
+ g_test_item_factory_callback = callback; |
+} |
+ |
+void ItemStorage::SetSessionLocked(bool session_locked) { |
+ SessionLockedState new_state = session_locked |
+ ? SessionLockedState::kLocked |
+ : SessionLockedState::kNotLocked; |
+ if (new_state == session_locked_state_) |
+ return; |
+ |
+ bool was_locked = session_locked_state_ == SessionLockedState::kLocked; |
+ session_locked_state_ = new_state; |
+ |
+ for (auto& extension_items : data_items_) { |
+ if (extension_items.second.empty()) |
+ continue; |
+ |
+ // If the session state is unlocked, dispatch Item availability events to |
+ // apps with available data items. |
+ if (session_locked_state_ == SessionLockedState::kNotLocked) { |
+ api::lock_screen_data::DataItemsAvailableEvent event_args; |
+ event_args.was_locked = was_locked; |
+ |
+ std::unique_ptr<Event> event = base::MakeUnique<Event>( |
+ events::LOCK_SCREEN_DATA_ON_DATA_ITEMS_AVAILABLE, |
+ api::lock_screen_data::OnDataItemsAvailable::kEventName, |
+ api::lock_screen_data::OnDataItemsAvailable::Create(event_args)); |
+ EventRouter::Get(context_)->DispatchEventToExtension( |
+ extension_items.first /* extension ID */, std::move(event)); |
+ } |
+ } |
+} |
+ |
+const DataItem* ItemStorage::CreateItem(const std::string& extension_id) { |
+ if (!ExtensionRegistry::Get(context_)->GetExtensionById( |
+ extension_id, ExtensionRegistry::ENABLED)) { |
+ return nullptr; |
+ } |
+ |
+ const std::string item_id = base::GenerateGUID(); |
+ std::unique_ptr<DataItem> item = CreateDataItem(item_id); |
+ DataItem* item_ptr = item.get(); |
+ |
+ { |
+ // Create item entry in local state prefs, ensuring that all parent paths |
+ // are properly set-up (in case this is the first entry added for a |
+ // user/extension). |
+ DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
+ if (!update->HasKey(user_id_)) { |
+ update->SetDictionary(user_id_, |
+ base::MakeUnique<base::DictionaryValue>()); |
+ } |
+ |
+ base::DictionaryValue* user_dict = nullptr; |
+ if (!update->GetDictionary(user_id_, &user_dict)) |
+ return nullptr; |
+ |
+ if (!user_dict->HasKey(extension_id)) { |
+ user_dict->SetDictionary(extension_id, |
+ base::MakeUnique<base::DictionaryValue>()); |
+ } |
+ |
+ base::DictionaryValue* extension_dict = nullptr; |
+ if (!user_dict->GetDictionary(extension_id, &extension_dict)) |
+ return nullptr; |
+ |
+ extension_dict->SetDictionary(item_id, item->ToValue()); |
+ } |
+ |
+ data_items_[extension_id].emplace(item_id, std::move(item)); |
+ return item_ptr; |
+} |
+ |
+std::vector<const DataItem*> ItemStorage::GetAllForExtension( |
+ const std::string& extension_id) { |
+ std::vector<const DataItem*> items; |
+ ExtensionDataItemMap::iterator extension_items = |
+ data_items_.find(extension_id); |
+ if (extension_items == data_items_.end()) |
+ return items; |
+ |
+ for (const auto& item : extension_items->second) { |
+ if (!item.second) |
+ continue; |
+ items.push_back(item.second.get()); |
+ } |
+ |
+ return items; |
+} |
+ |
+OperationResult ItemStorage::SetItemContent( |
+ const std::string& extension_id, |
+ const std::string& item_id, |
+ const std::vector<char>& data, |
+ const ItemStorage::WriteCallback& callback) { |
+ DataItem* item = FindItem(extension_id, item_id); |
+ if (!item) |
+ return OperationResult::kNotFound; |
+ |
+ if (item->backing_file().empty()) { |
+ item->set_backing_file(storage_root_.Append(extension_id).Append(item_id)); |
+ |
+ DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
+ update->SetDictionary(GetDataItemPrefKey(extension_id, item->id()), |
+ item->ToValue()); |
+ } |
+ |
+ return item->Write(data, crypto_key_, task_runner_, callback); |
+} |
+ |
+OperationResult ItemStorage::GetItemContent( |
+ const std::string& extension_id, |
+ const std::string& item_id, |
+ const ItemStorage::ReadCallback& callback) { |
+ DataItem* item = FindItem(extension_id, item_id); |
+ if (!item) |
+ return OperationResult::kNotFound; |
+ |
+ if (item->backing_file().empty()) |
+ return OperationResult::kNoBackingFile; |
+ |
+ return item->Read(crypto_key_, task_runner_, callback); |
+} |
+ |
+OperationResult ItemStorage::DeleteItem(const std::string& extension_id, |
+ const std::string& item_id) { |
+ DataItem* item = FindItem(extension_id, item_id); |
+ if (!item) |
+ return OperationResult::kNotFound; |
+ |
+ { |
+ DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
+ update->Remove(GetDataItemPrefKey(extension_id, item_id), nullptr); |
+ } |
+ |
+ if (!item->backing_file().empty()) |
+ item->Delete(task_runner_, base::Bind(&DoNothingOnDelete)); |
+ |
+ data_items_[extension_id].erase(item_id); |
+ return OperationResult::kSuccess; |
+} |
+ |
+void ItemStorage::OnExtensionUninstalled( |
+ content::BrowserContext* browser_context, |
+ const Extension* extension, |
+ UninstallReason reason) { |
+ ClearDataForExtension(extension->id()); |
+} |
+ |
+std::string ItemStorage::GetDataItemPrefKey(const std::string& extension_id, |
+ const std::string& item_id) { |
+ return user_id_ + "." + extension_id + "." + item_id; |
+} |
+ |
+bool ItemStorage::IsContextAllowed(content::BrowserContext* context) { |
+ switch (session_locked_state_) { |
+ case SessionLockedState::kUnknown: |
+ return false; |
+ case SessionLockedState::kLocked: |
+ return ExtensionsBrowserClient::Get()->IsLockScreenContext(context); |
+ case SessionLockedState::kNotLocked: |
+ return context_ == context; |
+ } |
+ NOTREACHED() << "Unknown session locked state"; |
+ return false; |
+} |
+ |
+void ItemStorage::ReloadDataItems() { |
+ data_items_.clear(); |
rkc
2017/06/22 18:35:47
Since this is called exactly once and only from th
tbarzic
2017/06/26 22:21:42
Done.
|
+ |
+ const base::DictionaryValue* user_data = nullptr; |
+ const base::DictionaryValue* items = |
+ local_state_->GetDictionary(kLockScreenDataPrefKey); |
+ if (!items || !items->GetDictionary(user_id_, &user_data) || !user_data) |
+ return; |
+ |
+ // Set of extensions pref entries that should be remove from prefs (for |
+ // example, entries for extensions that are not installed anymore). |
+ // Note that entries are not removed directly during iteration over the |
+ // dictionary to avoid invalidating data under the iterator. |
+ std::set<std::string> extensions_to_remove; |
+ |
+ for (base::DictionaryValue::Iterator extension_iter(*user_data); |
+ !extension_iter.IsAtEnd(); extension_iter.Advance()) { |
+ if (!ExtensionRegistry::Get(context_)->GetInstalledExtension( |
+ extension_iter.key())) { |
+ extensions_to_remove.insert(extension_iter.key()); |
+ } else { |
+ if (!LoadDataItemsForExtension(extension_iter.key(), |
+ extension_iter.value())) { |
+ extensions_to_remove.insert(extension_iter.key()); |
+ } |
+ } |
+ } |
+ |
+ // Remove stale/invalid entries. |
+ for (const auto& extension_id : extensions_to_remove) |
+ ClearDataForExtension(extension_id); |
+} |
+ |
+bool ItemStorage::LoadDataItemsForExtension(const std::string& extension_id, |
+ const base::Value& extension_pref) { |
+ const base::DictionaryValue* items = nullptr; |
+ if (!extension_pref.GetAsDictionary(&items)) |
+ return false; |
+ |
+ std::set<std::string> items_to_remove; |
+ base::FilePath expected_file_parent = storage_root_.Append(extension_id); |
+ for (base::DictionaryValue::Iterator item_iter(*items); !item_iter.IsAtEnd(); |
+ item_iter.Advance()) { |
+ std::unique_ptr<DataItem> item = CreateDataItem(item_iter.key()); |
+ |
+ // Invalid items (including items pointing to an unexpected location) should |
+ // be removed from prefs - schedule removal after the loop to avoid |
+ // invalidating the iterator. |
+ if (!item->InitFromValue(item_iter.value()) || |
+ (!item->backing_file().empty() && |
+ !expected_file_parent.IsParent(item->backing_file()))) { |
+ items_to_remove.insert(item_iter.key()); |
+ continue; |
+ } |
+ data_items_[extension_id].emplace(item_iter.key(), std::move(item)); |
+ } |
+ |
+ DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
+ for (const auto& item_id : items_to_remove) { |
+ update->Remove(GetDataItemPrefKey(extension_id, item_id), nullptr); |
+ } |
+ return true; |
+} |
+ |
+void ItemStorage::ClearDataForExtension(const std::string& extension_id) { |
+ task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DeleteImpl, storage_root_.Append(extension_id), true)); |
+ |
+ data_items_.erase(extension_id); |
+ |
+ DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
+ update->Remove(user_id_ + "." + extension_id, nullptr); |
+} |
+ |
+DataItem* ItemStorage::FindItem(const std::string& extension_id, |
+ const std::string& item_id) { |
+ ExtensionDataItemMap::iterator extension_items = |
+ data_items_.find(extension_id); |
+ if (extension_items == data_items_.end()) |
+ return nullptr; |
+ |
+ DataItemMap::iterator item_it = extension_items->second.find(item_id); |
+ if (item_it == extension_items->second.end()) |
+ return nullptr; |
+ |
+ if (!item_it->second) |
+ return nullptr; |
+ return item_it->second.get(); |
+} |
+ |
+} // namespace lock_screen_data |
+} // namespace extensions |