mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add support for Chrome Storage Extension API (issue #1947)
- Supports chrome.storage.local and chrome.storage.sync - chrome.storage.sync behaves identically to chrome.storage.local
This commit is contained in:
committed by
Marshall Greenblatt
parent
a028976ac4
commit
d8a602ed2f
@ -0,0 +1,97 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2014 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 "libcef/browser/extensions/api/storage/sync_value_store_cache.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "extensions/browser/api/storage/backend_task_runner.h"
|
||||
#include "extensions/browser/api/storage/weak_unlimited_settings_storage.h"
|
||||
#include "extensions/browser/value_store/value_store_factory.h"
|
||||
#include "extensions/common/api/storage.h"
|
||||
#include "extensions/common/extension.h"
|
||||
#include "extensions/common/permissions/permissions_data.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace extensions {
|
||||
|
||||
namespace cef {
|
||||
|
||||
namespace {
|
||||
|
||||
// Returns the quota limit for local storage, taken from the schema in
|
||||
// extensions/common/api/storage.json.
|
||||
SettingsStorageQuotaEnforcer::Limits GetLocalQuotaLimits() {
|
||||
SettingsStorageQuotaEnforcer::Limits limits = {
|
||||
static_cast<size_t>(api::storage::local::QUOTA_BYTES),
|
||||
std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
|
||||
return limits;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SyncValueStoreCache::SyncValueStoreCache(
|
||||
const scoped_refptr<ValueStoreFactory>& factory)
|
||||
: storage_factory_(factory), quota_(GetLocalQuotaLimits()) {
|
||||
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
||||
}
|
||||
|
||||
SyncValueStoreCache::~SyncValueStoreCache() {
|
||||
DCHECK(IsOnBackendSequence());
|
||||
}
|
||||
|
||||
void SyncValueStoreCache::RunWithValueStoreForExtension(
|
||||
const StorageCallback& callback,
|
||||
scoped_refptr<const Extension> extension) {
|
||||
DCHECK(IsOnBackendSequence());
|
||||
|
||||
ValueStore* storage = GetStorage(extension.get());
|
||||
|
||||
// A neat way to implement unlimited storage; if the extension has the
|
||||
// unlimited storage permission, force through all calls to Set().
|
||||
if (extension->permissions_data()->HasAPIPermission(
|
||||
APIPermission::kUnlimitedStorage)) {
|
||||
WeakUnlimitedSettingsStorage unlimited_storage(storage);
|
||||
callback.Run(&unlimited_storage);
|
||||
} else {
|
||||
callback.Run(storage);
|
||||
}
|
||||
}
|
||||
|
||||
void SyncValueStoreCache::DeleteStorageSoon(const std::string& extension_id) {
|
||||
DCHECK(IsOnBackendSequence());
|
||||
storage_map_.erase(extension_id);
|
||||
storage_factory_->DeleteSettings(settings_namespace::SYNC,
|
||||
ValueStoreFactory::ModelType::APP,
|
||||
extension_id);
|
||||
storage_factory_->DeleteSettings(settings_namespace::SYNC,
|
||||
ValueStoreFactory::ModelType::EXTENSION,
|
||||
extension_id);
|
||||
}
|
||||
|
||||
ValueStore* SyncValueStoreCache::GetStorage(const Extension* extension) {
|
||||
StorageMap::iterator iter = storage_map_.find(extension->id());
|
||||
if (iter != storage_map_.end())
|
||||
return iter->second.get();
|
||||
|
||||
ValueStoreFactory::ModelType model_type =
|
||||
extension->is_app() ? ValueStoreFactory::ModelType::APP
|
||||
: ValueStoreFactory::ModelType::EXTENSION;
|
||||
std::unique_ptr<ValueStore> store = storage_factory_->CreateSettingsStore(
|
||||
settings_namespace::SYNC, model_type, extension->id());
|
||||
std::unique_ptr<SettingsStorageQuotaEnforcer> storage(
|
||||
new SettingsStorageQuotaEnforcer(quota_, std::move(store)));
|
||||
DCHECK(storage.get());
|
||||
|
||||
ValueStore* storage_ptr = storage.get();
|
||||
storage_map_[extension->id()] = std::move(storage);
|
||||
return storage_ptr;
|
||||
}
|
||||
} // namespace cef
|
||||
} // namespace extensions
|
@ -0,0 +1,56 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2014 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_API_STORAGE_SYNC_VALUE_STORE_CACHE_H_
|
||||
#define CEF_LIBCEF_BROWSER_EXTENSIONS_API_STORAGE_SYNC_VALUE_STORE_CACHE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/macros.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
|
||||
#include "extensions/browser/api/storage/value_store_cache.h"
|
||||
|
||||
namespace extensions {
|
||||
|
||||
class ValueStoreFactory;
|
||||
|
||||
namespace cef {
|
||||
|
||||
// Based on LocalValueStoreCache
|
||||
// ValueStoreCache for the SYNC namespace. It owns a backend for apps and
|
||||
// another for extensions. Each backend takes care of persistence.
|
||||
class SyncValueStoreCache : public ValueStoreCache {
|
||||
public:
|
||||
explicit SyncValueStoreCache(const scoped_refptr<ValueStoreFactory>& factory);
|
||||
~SyncValueStoreCache() override;
|
||||
|
||||
// ValueStoreCache implementation:
|
||||
void RunWithValueStoreForExtension(
|
||||
const StorageCallback& callback,
|
||||
scoped_refptr<const Extension> extension) override;
|
||||
void DeleteStorageSoon(const std::string& extension_id) override;
|
||||
|
||||
private:
|
||||
using StorageMap = std::map<std::string, std::unique_ptr<ValueStore>>;
|
||||
|
||||
ValueStore* GetStorage(const Extension* extension);
|
||||
|
||||
// The Factory to use for creating new ValueStores.
|
||||
const scoped_refptr<ValueStoreFactory> storage_factory_;
|
||||
|
||||
// Quota limits (see SettingsStorageQuotaEnforcer).
|
||||
const SettingsStorageQuotaEnforcer::Limits quota_;
|
||||
|
||||
// The collection of ValueStores for local storage.
|
||||
StorageMap storage_map_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(SyncValueStoreCache);
|
||||
};
|
||||
} // namespace cef
|
||||
} // namespace extensions
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_EXTENSIONS_API_STORAGE_SYNC_VALUE_STORE_CACHE_H_
|
@ -8,6 +8,7 @@
|
||||
#include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
|
||||
#include "chrome/browser/ui/prefs/prefs_tab_helper.h"
|
||||
#include "extensions/browser/api/alarms/alarm_manager.h"
|
||||
#include "extensions/browser/api/storage/storage_frontend.h"
|
||||
#include "extensions/browser/renderer_startup_helper.h"
|
||||
|
||||
namespace extensions {
|
||||
@ -18,6 +19,7 @@ void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
|
||||
CookieSettingsFactory::GetInstance();
|
||||
PrefsTabHelper::GetServiceInstance();
|
||||
RendererStartupHelperFactory::GetInstance();
|
||||
StorageFrontend::GetFactoryInstance();
|
||||
StreamsPrivateAPI::GetFactoryInstance();
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@
|
||||
#include "chrome/browser/extensions/api/resources_private/resources_private_api.h"
|
||||
#include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
|
||||
#include "extensions/browser/api/alarms/alarms_api.h"
|
||||
#include "extensions/browser/api/storage/storage_api.h"
|
||||
#include "extensions/browser/extension_function_registry.h"
|
||||
|
||||
namespace extensions {
|
||||
@ -36,6 +37,12 @@ const char* const kSupportedAPIs[] = {
|
||||
EXTENSION_FUNCTION_NAME(AlarmsGetAllFunction),
|
||||
EXTENSION_FUNCTION_NAME(AlarmsClearFunction),
|
||||
EXTENSION_FUNCTION_NAME(AlarmsClearAllFunction),
|
||||
"storage",
|
||||
EXTENSION_FUNCTION_NAME(StorageStorageAreaGetFunction),
|
||||
EXTENSION_FUNCTION_NAME(StorageStorageAreaSetFunction),
|
||||
EXTENSION_FUNCTION_NAME(StorageStorageAreaRemoveFunction),
|
||||
EXTENSION_FUNCTION_NAME(StorageStorageAreaClearFunction),
|
||||
EXTENSION_FUNCTION_NAME(StorageStorageAreaGetBytesInUseFunction),
|
||||
"tabs",
|
||||
EXTENSION_FUNCTION_NAME(cefimpl::TabsGetFunction),
|
||||
EXTENSION_FUNCTION_NAME(cefimpl::TabsExecuteScriptFunction),
|
||||
@ -67,6 +74,11 @@ void ChromeFunctionRegistry::RegisterAll(ExtensionFunctionRegistry* registry) {
|
||||
registry->RegisterFunction<AlarmsGetAllFunction>();
|
||||
registry->RegisterFunction<AlarmsClearFunction>();
|
||||
registry->RegisterFunction<AlarmsClearAllFunction>();
|
||||
registry->RegisterFunction<StorageStorageAreaGetFunction>();
|
||||
registry->RegisterFunction<StorageStorageAreaSetFunction>();
|
||||
registry->RegisterFunction<StorageStorageAreaRemoveFunction>();
|
||||
registry->RegisterFunction<StorageStorageAreaClearFunction>();
|
||||
registry->RegisterFunction<StorageStorageAreaGetBytesInUseFunction>();
|
||||
registry->RegisterFunction<cefimpl::TabsExecuteScriptFunction>();
|
||||
registry->RegisterFunction<cefimpl::TabsInsertCSSFunction>();
|
||||
registry->RegisterFunction<cefimpl::TabsGetFunction>();
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "libcef/browser/extension_impl.h"
|
||||
#include "libcef/browser/extensions/pdf_extension_util.h"
|
||||
#include "libcef/browser/extensions/value_store/cef_value_store_factory.h"
|
||||
#include "libcef/browser/thread_util.h"
|
||||
#include "libcef/common/extensions/extensions_util.h"
|
||||
|
||||
@ -21,6 +22,7 @@
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "base/task_scheduler/post_task.h"
|
||||
#include "base/threading/thread_restrictions.h"
|
||||
#include "chrome/browser/profiles/profile.h"
|
||||
#include "chrome/common/chrome_paths.h"
|
||||
#include "components/crx_file/id_util.h"
|
||||
#include "content/public/browser/browser_context.h"
|
||||
@ -40,7 +42,7 @@
|
||||
#include "extensions/browser/renderer_startup_helper.h"
|
||||
#include "extensions/browser/runtime_data.h"
|
||||
#include "extensions/browser/service_worker_manager.h"
|
||||
#include "extensions/browser/value_store/value_store_factory.h"
|
||||
#include "extensions/browser/state_store.h"
|
||||
#include "extensions/common/constants.h"
|
||||
#include "extensions/common/extension_messages.h"
|
||||
#include "extensions/common/file_util.h"
|
||||
@ -153,7 +155,9 @@ CefExtensionSystem::CefExtensionSystem(BrowserContext* browser_context)
|
||||
renderer_helper_(
|
||||
extensions::RendererStartupHelperFactory::GetForBrowserContext(
|
||||
browser_context)),
|
||||
weak_ptr_factory_(this) {}
|
||||
weak_ptr_factory_(this) {
|
||||
InitPrefs();
|
||||
}
|
||||
|
||||
CefExtensionSystem::~CefExtensionSystem() {}
|
||||
|
||||
@ -384,15 +388,15 @@ SharedUserScriptMaster* CefExtensionSystem::shared_user_script_master() {
|
||||
}
|
||||
|
||||
StateStore* CefExtensionSystem::state_store() {
|
||||
return nullptr;
|
||||
return state_store_.get();
|
||||
}
|
||||
|
||||
StateStore* CefExtensionSystem::rules_store() {
|
||||
return nullptr;
|
||||
return rules_store_.get();
|
||||
}
|
||||
|
||||
scoped_refptr<ValueStoreFactory> CefExtensionSystem::store_factory() {
|
||||
return nullptr;
|
||||
return store_factory_;
|
||||
}
|
||||
|
||||
InfoMap* CefExtensionSystem::info_map() {
|
||||
@ -467,6 +471,21 @@ CefExtensionSystem::ComponentExtensionInfo::ComponentExtensionInfo(
|
||||
}
|
||||
}
|
||||
|
||||
void CefExtensionSystem::InitPrefs() {
|
||||
store_factory_ = new CefValueStoreFactory(browser_context_->GetPath());
|
||||
|
||||
Profile* profile = Profile::FromBrowserContext(browser_context_);
|
||||
|
||||
// Two state stores. The latter, which contains declarative rules, must be
|
||||
// loaded immediately so that the rules are ready before we issue network
|
||||
// requests.
|
||||
state_store_.reset(new StateStore(
|
||||
profile, store_factory_, ValueStoreFrontend::BackendType::STATE, true));
|
||||
|
||||
rules_store_.reset(new StateStore(
|
||||
profile, store_factory_, ValueStoreFrontend::BackendType::RULES, false));
|
||||
}
|
||||
|
||||
// Implementation based on ComponentLoader::CreateExtension.
|
||||
scoped_refptr<const Extension> CefExtensionSystem::CreateExtension(
|
||||
const ComponentExtensionInfo& info,
|
||||
|
@ -116,6 +116,8 @@ class CefExtensionSystem : public ExtensionSystem {
|
||||
bool initialized() const { return initialized_; }
|
||||
|
||||
private:
|
||||
virtual void InitPrefs();
|
||||
|
||||
// Information about a registered component extension.
|
||||
struct ComponentExtensionInfo {
|
||||
ComponentExtensionInfo(const base::DictionaryValue* manifest,
|
||||
@ -169,6 +171,10 @@ class CefExtensionSystem : public ExtensionSystem {
|
||||
std::unique_ptr<QuotaService> quota_service_;
|
||||
std::unique_ptr<AppSorting> app_sorting_;
|
||||
|
||||
std::unique_ptr<StateStore> state_store_;
|
||||
std::unique_ptr<StateStore> rules_store_;
|
||||
scoped_refptr<ValueStoreFactory> store_factory_;
|
||||
|
||||
// Signaled when the extension system has completed its startup tasks.
|
||||
OneShotEvent ready_;
|
||||
|
||||
|
@ -7,6 +7,7 @@
|
||||
|
||||
#include "include/internal/cef_types_wrappers.h"
|
||||
#include "libcef/browser/browser_context_impl.h"
|
||||
#include "libcef/browser/extensions/api/storage/sync_value_store_cache.h"
|
||||
#include "libcef/browser/extensions/extension_web_contents_observer.h"
|
||||
#include "libcef/browser/extensions/mime_handler_view_guest_delegate.h"
|
||||
#include "libcef/browser/extensions/pdf_web_contents_helper_client.h"
|
||||
@ -66,4 +67,17 @@ void CefExtensionsAPIClient::AttachWebContentsHelpers(
|
||||
zoom::ZoomController::CreateForWebContents(web_contents);
|
||||
}
|
||||
|
||||
void CefExtensionsAPIClient::AddAdditionalValueStoreCaches(
|
||||
content::BrowserContext* context,
|
||||
const scoped_refptr<ValueStoreFactory>& factory,
|
||||
const scoped_refptr<base::ObserverListThreadSafe<SettingsObserver>>&
|
||||
observers,
|
||||
std::map<settings_namespace::Namespace, ValueStoreCache*>* caches) {
|
||||
// Add support for chrome.storage.sync.
|
||||
// Because we don't support syncing with Google, we follow the behavior of
|
||||
// chrome.storage.sync as if Chrome were permanently offline, by using a local
|
||||
// store see: https://developer.chrome.com/apps/storage for more information
|
||||
(*caches)[settings_namespace::SYNC] = new cef::SyncValueStoreCache(factory);
|
||||
}
|
||||
|
||||
} // namespace extensions
|
||||
|
@ -24,6 +24,18 @@ class CefExtensionsAPIClient : public ExtensionsAPIClient {
|
||||
MimeHandlerViewGuest* guest) const override;
|
||||
void AttachWebContentsHelpers(
|
||||
content::WebContents* web_contents) const override;
|
||||
|
||||
// Storage API support.
|
||||
|
||||
// Add any additional value store caches (e.g. for chrome.storage.managed)
|
||||
// to |caches|. By default adds nothing.
|
||||
void AddAdditionalValueStoreCaches(
|
||||
content::BrowserContext* context,
|
||||
const scoped_refptr<ValueStoreFactory>& factory,
|
||||
const scoped_refptr<base::ObserverListThreadSafe<SettingsObserver>>&
|
||||
observers,
|
||||
std::map<settings_namespace::Namespace, ValueStoreCache*>* caches)
|
||||
override;
|
||||
};
|
||||
|
||||
} // namespace extensions
|
||||
|
110
libcef/browser/extensions/value_store/cef_value_store.cc
Normal file
110
libcef/browser/extensions/value_store/cef_value_store.cc
Normal file
@ -0,0 +1,110 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2016 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 "libcef/browser/extensions/value_store/cef_value_store.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/memory/ptr_util.h"
|
||||
|
||||
CefValueStore::CefValueStore() : read_count_(0), write_count_(0) {}
|
||||
|
||||
CefValueStore::~CefValueStore() {}
|
||||
|
||||
size_t CefValueStore::GetBytesInUse(const std::string& key) {
|
||||
// Let SettingsStorageQuotaEnforcer implement this.
|
||||
NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t CefValueStore::GetBytesInUse(const std::vector<std::string>& keys) {
|
||||
// Let SettingsStorageQuotaEnforcer implement this.
|
||||
NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t CefValueStore::GetBytesInUse() {
|
||||
// Let SettingsStorageQuotaEnforcer implement this.
|
||||
NOTREACHED();
|
||||
return 0;
|
||||
}
|
||||
|
||||
ValueStore::ReadResult CefValueStore::Get(const std::string& key) {
|
||||
return Get(std::vector<std::string>(1, key));
|
||||
}
|
||||
|
||||
ValueStore::ReadResult CefValueStore::Get(
|
||||
const std::vector<std::string>& keys) {
|
||||
read_count_++;
|
||||
base::DictionaryValue* settings = new base::DictionaryValue();
|
||||
for (std::vector<std::string>::const_iterator it = keys.begin();
|
||||
it != keys.end(); ++it) {
|
||||
base::Value* value = NULL;
|
||||
if (storage_.GetWithoutPathExpansion(*it, &value)) {
|
||||
settings->SetWithoutPathExpansion(*it, value->CreateDeepCopy());
|
||||
}
|
||||
}
|
||||
return MakeReadResult(base::WrapUnique(settings), status_);
|
||||
}
|
||||
|
||||
ValueStore::ReadResult CefValueStore::Get() {
|
||||
read_count_++;
|
||||
return MakeReadResult(storage_.CreateDeepCopy(), status_);
|
||||
}
|
||||
|
||||
ValueStore::WriteResult CefValueStore::Set(WriteOptions options,
|
||||
const std::string& key,
|
||||
const base::Value& value) {
|
||||
base::DictionaryValue settings;
|
||||
settings.SetWithoutPathExpansion(key, value.CreateDeepCopy());
|
||||
return Set(options, settings);
|
||||
}
|
||||
|
||||
ValueStore::WriteResult CefValueStore::Set(
|
||||
WriteOptions options,
|
||||
const base::DictionaryValue& settings) {
|
||||
write_count_++;
|
||||
std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
|
||||
for (base::DictionaryValue::Iterator it(settings); !it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
base::Value* old_value = NULL;
|
||||
if (!storage_.GetWithoutPathExpansion(it.key(), &old_value) ||
|
||||
!old_value->Equals(&it.value())) {
|
||||
changes->push_back(ValueStoreChange(
|
||||
it.key(), old_value ? old_value->CreateDeepCopy() : nullptr,
|
||||
it.value().CreateDeepCopy()));
|
||||
storage_.SetWithoutPathExpansion(it.key(), it.value().CreateDeepCopy());
|
||||
}
|
||||
}
|
||||
return MakeWriteResult(std::move(changes), status_);
|
||||
}
|
||||
|
||||
ValueStore::WriteResult CefValueStore::Remove(const std::string& key) {
|
||||
return Remove(std::vector<std::string>(1, key));
|
||||
}
|
||||
|
||||
ValueStore::WriteResult CefValueStore::Remove(
|
||||
const std::vector<std::string>& keys) {
|
||||
write_count_++;
|
||||
std::unique_ptr<ValueStoreChangeList> changes(new ValueStoreChangeList());
|
||||
for (std::vector<std::string>::const_iterator it = keys.begin();
|
||||
it != keys.end(); ++it) {
|
||||
std::unique_ptr<base::Value> old_value;
|
||||
if (storage_.RemoveWithoutPathExpansion(*it, &old_value)) {
|
||||
changes->push_back(ValueStoreChange(*it, std::move(old_value), nullptr));
|
||||
}
|
||||
}
|
||||
return MakeWriteResult(std::move(changes), status_);
|
||||
}
|
||||
|
||||
ValueStore::WriteResult CefValueStore::Clear() {
|
||||
std::vector<std::string> keys;
|
||||
for (base::DictionaryValue::Iterator it(storage_); !it.IsAtEnd();
|
||||
it.Advance()) {
|
||||
keys.push_back(it.key());
|
||||
}
|
||||
return Remove(keys);
|
||||
}
|
59
libcef/browser/extensions/value_store/cef_value_store.h
Normal file
59
libcef/browser/extensions/value_store/cef_value_store.h
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2016 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_H_
|
||||
#define CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/macros.h"
|
||||
#include "extensions/browser/value_store/value_store.h"
|
||||
|
||||
// Implementation Based on TestingValueStore
|
||||
// ValueStore with an in-memory storage but the ability to
|
||||
// optionally fail all operations.
|
||||
class CefValueStore : public ValueStore {
|
||||
public:
|
||||
CefValueStore();
|
||||
~CefValueStore() override;
|
||||
|
||||
// Accessors for the number of reads/writes done by this value store. Each
|
||||
// Get* operation (except for the BytesInUse ones) counts as one read, and
|
||||
// each Set*/Remove/Clear operation counts as one write. This is useful in
|
||||
// tests seeking to assert that some number of reads/writes to their
|
||||
// underlying value store have (or have not) happened.
|
||||
int read_count() const { return read_count_; }
|
||||
int write_count() const { return write_count_; }
|
||||
|
||||
// ValueStore implementation.
|
||||
size_t GetBytesInUse(const std::string& key) override;
|
||||
size_t GetBytesInUse(const std::vector<std::string>& keys) override;
|
||||
size_t GetBytesInUse() override;
|
||||
ReadResult Get(const std::string& key) override;
|
||||
ReadResult Get(const std::vector<std::string>& keys) override;
|
||||
ReadResult Get() override;
|
||||
WriteResult Set(WriteOptions options,
|
||||
const std::string& key,
|
||||
const base::Value& value) override;
|
||||
WriteResult Set(WriteOptions options,
|
||||
const base::DictionaryValue& values) override;
|
||||
WriteResult Remove(const std::string& key) override;
|
||||
WriteResult Remove(const std::vector<std::string>& keys) override;
|
||||
WriteResult Clear() override;
|
||||
|
||||
private:
|
||||
base::DictionaryValue storage_;
|
||||
int read_count_;
|
||||
int write_count_;
|
||||
ValueStore::Status status_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefValueStore);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_H_
|
191
libcef/browser/extensions/value_store/cef_value_store_factory.cc
Normal file
191
libcef/browser/extensions/value_store/cef_value_store_factory.cc
Normal file
@ -0,0 +1,191 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2016 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 "libcef/browser/extensions/value_store/cef_value_store_factory.h"
|
||||
|
||||
#include "base/memory/ptr_util.h"
|
||||
#include "extensions/browser/value_store/leveldb_value_store.h"
|
||||
#include "libcef/browser/extensions/value_store/cef_value_store.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const char kUMAClientName[] = "Cef";
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace extensions {
|
||||
|
||||
using SettingsNamespace = settings_namespace::Namespace;
|
||||
|
||||
CefValueStoreFactory::StorageHelper::StorageHelper() = default;
|
||||
|
||||
CefValueStoreFactory::StorageHelper::~StorageHelper() = default;
|
||||
|
||||
std::set<ExtensionId> CefValueStoreFactory::StorageHelper::GetKnownExtensionIDs(
|
||||
ModelType model_type) const {
|
||||
std::set<ExtensionId> ids;
|
||||
switch (model_type) {
|
||||
case ValueStoreFactory::ModelType::APP:
|
||||
for (const auto& key : app_stores_)
|
||||
ids.insert(key.first);
|
||||
break;
|
||||
case ValueStoreFactory::ModelType::EXTENSION:
|
||||
for (const auto& key : extension_stores_)
|
||||
ids.insert(key.first);
|
||||
break;
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
void CefValueStoreFactory::StorageHelper::Reset() {
|
||||
app_stores_.clear();
|
||||
extension_stores_.clear();
|
||||
}
|
||||
|
||||
ValueStore* CefValueStoreFactory::StorageHelper::AddValueStore(
|
||||
const ExtensionId& extension_id,
|
||||
ValueStore* value_store,
|
||||
ModelType model_type) {
|
||||
if (model_type == ValueStoreFactory::ModelType::APP) {
|
||||
DCHECK(app_stores_.find(extension_id) == app_stores_.end());
|
||||
app_stores_[extension_id] = value_store;
|
||||
} else {
|
||||
DCHECK(extension_stores_.find(extension_id) == extension_stores_.end());
|
||||
extension_stores_[extension_id] = value_store;
|
||||
}
|
||||
return value_store;
|
||||
}
|
||||
|
||||
void CefValueStoreFactory::StorageHelper::DeleteSettings(
|
||||
const ExtensionId& extension_id,
|
||||
ModelType model_type) {
|
||||
switch (model_type) {
|
||||
case ValueStoreFactory::ModelType::APP:
|
||||
app_stores_.erase(extension_id);
|
||||
break;
|
||||
case ValueStoreFactory::ModelType::EXTENSION:
|
||||
extension_stores_.erase(extension_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool CefValueStoreFactory::StorageHelper::HasSettings(
|
||||
const ExtensionId& extension_id,
|
||||
ModelType model_type) const {
|
||||
switch (model_type) {
|
||||
case ValueStoreFactory::ModelType::APP:
|
||||
return app_stores_.find(extension_id) != app_stores_.end();
|
||||
case ValueStoreFactory::ModelType::EXTENSION:
|
||||
return extension_stores_.find(extension_id) != extension_stores_.end();
|
||||
}
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
ValueStore* CefValueStoreFactory::StorageHelper::GetExisting(
|
||||
const ExtensionId& extension_id) const {
|
||||
auto it = app_stores_.find(extension_id);
|
||||
if (it != app_stores_.end())
|
||||
return it->second;
|
||||
it = extension_stores_.find(extension_id);
|
||||
if (it != extension_stores_.end())
|
||||
return it->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
CefValueStoreFactory::CefValueStoreFactory() = default;
|
||||
|
||||
CefValueStoreFactory::CefValueStoreFactory(const base::FilePath& db_path)
|
||||
: db_path_(db_path) {}
|
||||
|
||||
CefValueStoreFactory::~CefValueStoreFactory() {}
|
||||
|
||||
std::unique_ptr<ValueStore> CefValueStoreFactory::CreateRulesStore() {
|
||||
if (db_path_.empty())
|
||||
last_created_store_ = new CefValueStore();
|
||||
else
|
||||
last_created_store_ = new LeveldbValueStore(kUMAClientName, db_path_);
|
||||
return base::WrapUnique(last_created_store_);
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueStore> CefValueStoreFactory::CreateStateStore() {
|
||||
return CreateRulesStore();
|
||||
}
|
||||
|
||||
CefValueStoreFactory::StorageHelper& CefValueStoreFactory::GetStorageHelper(
|
||||
SettingsNamespace settings_namespace) {
|
||||
switch (settings_namespace) {
|
||||
case settings_namespace::LOCAL:
|
||||
return local_helper_;
|
||||
case settings_namespace::SYNC:
|
||||
return sync_helper_;
|
||||
case settings_namespace::MANAGED:
|
||||
return managed_helper_;
|
||||
case settings_namespace::INVALID:
|
||||
break;
|
||||
}
|
||||
NOTREACHED();
|
||||
return local_helper_;
|
||||
}
|
||||
|
||||
std::unique_ptr<ValueStore> CefValueStoreFactory::CreateSettingsStore(
|
||||
SettingsNamespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) {
|
||||
std::unique_ptr<ValueStore> settings_store(CreateRulesStore());
|
||||
// Note: This factory is purposely keeping the raw pointers to each ValueStore
|
||||
// created. Tests using CefValueStoreFactory must be careful to keep
|
||||
// those ValueStore's alive for the duration of their test.
|
||||
GetStorageHelper(settings_namespace)
|
||||
.AddValueStore(extension_id, settings_store.get(), model_type);
|
||||
return settings_store;
|
||||
}
|
||||
|
||||
ValueStore* CefValueStoreFactory::LastCreatedStore() const {
|
||||
return last_created_store_;
|
||||
}
|
||||
|
||||
void CefValueStoreFactory::DeleteSettings(SettingsNamespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) {
|
||||
GetStorageHelper(settings_namespace).DeleteSettings(extension_id, model_type);
|
||||
}
|
||||
|
||||
bool CefValueStoreFactory::HasSettings(SettingsNamespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) {
|
||||
return GetStorageHelper(settings_namespace)
|
||||
.HasSettings(extension_id, model_type);
|
||||
}
|
||||
|
||||
std::set<ExtensionId> CefValueStoreFactory::GetKnownExtensionIDs(
|
||||
SettingsNamespace settings_namespace,
|
||||
ModelType model_type) const {
|
||||
return const_cast<CefValueStoreFactory*>(this)
|
||||
->GetStorageHelper(settings_namespace)
|
||||
.GetKnownExtensionIDs(model_type);
|
||||
}
|
||||
|
||||
ValueStore* CefValueStoreFactory::GetExisting(
|
||||
const ExtensionId& extension_id) const {
|
||||
ValueStore* existing_store = local_helper_.GetExisting(extension_id);
|
||||
if (existing_store)
|
||||
return existing_store;
|
||||
existing_store = sync_helper_.GetExisting(extension_id);
|
||||
if (existing_store)
|
||||
return existing_store;
|
||||
existing_store = managed_helper_.GetExisting(extension_id);
|
||||
DCHECK(existing_store != nullptr);
|
||||
return existing_store;
|
||||
}
|
||||
|
||||
void CefValueStoreFactory::Reset() {
|
||||
last_created_store_ = nullptr;
|
||||
local_helper_.Reset();
|
||||
sync_helper_.Reset();
|
||||
managed_helper_.Reset();
|
||||
}
|
||||
|
||||
} // namespace extensions
|
@ -0,0 +1,97 @@
|
||||
// Copyright 2017 The Chromium Embedded Framework Authors.
|
||||
// Portions copyright 2016 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.
|
||||
|
||||
#ifndef CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_
|
||||
#define CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
#include "base/files/file_path.h"
|
||||
#include "extensions/browser/value_store/value_store_factory.h"
|
||||
#include "extensions/common/extension_id.h"
|
||||
|
||||
class ValueStore;
|
||||
|
||||
namespace extensions {
|
||||
|
||||
// Will either open a database on disk (if path provided) returning a
|
||||
// |LeveldbValueStore|. Otherwise a new |CefValueStore| instance will be
|
||||
// returned.
|
||||
class CefValueStoreFactory : public ValueStoreFactory {
|
||||
public:
|
||||
CefValueStoreFactory();
|
||||
explicit CefValueStoreFactory(const base::FilePath& db_path);
|
||||
|
||||
// ValueStoreFactory
|
||||
std::unique_ptr<ValueStore> CreateRulesStore() override;
|
||||
std::unique_ptr<ValueStore> CreateStateStore() override;
|
||||
std::unique_ptr<ValueStore> CreateSettingsStore(
|
||||
settings_namespace::Namespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) override;
|
||||
void DeleteSettings(settings_namespace::Namespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) override;
|
||||
bool HasSettings(settings_namespace::Namespace settings_namespace,
|
||||
ModelType model_type,
|
||||
const ExtensionId& extension_id) override;
|
||||
std::set<ExtensionId> GetKnownExtensionIDs(
|
||||
settings_namespace::Namespace settings_namespace,
|
||||
ModelType model_type) const override;
|
||||
|
||||
// Return the last created |ValueStore|. Use with caution as this may return
|
||||
// a dangling pointer since the creator now owns the ValueStore which can be
|
||||
// deleted at any time.
|
||||
ValueStore* LastCreatedStore() const;
|
||||
// Return a previously created |ValueStore| for an extension.
|
||||
ValueStore* GetExisting(const ExtensionId& extension_id) const;
|
||||
// Reset this class (as if just created).
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
// Manages a collection of |ValueStore|'s created for an app/extension.
|
||||
// One of these exists for each setting type.
|
||||
class StorageHelper {
|
||||
public:
|
||||
StorageHelper();
|
||||
~StorageHelper();
|
||||
std::set<ExtensionId> GetKnownExtensionIDs(ModelType model_type) const;
|
||||
ValueStore* AddValueStore(const ExtensionId& extension_id,
|
||||
ValueStore* value_store,
|
||||
ModelType model_type);
|
||||
void DeleteSettings(const ExtensionId& extension_id, ModelType model_type);
|
||||
bool HasSettings(const ExtensionId& extension_id,
|
||||
ModelType model_type) const;
|
||||
void Reset();
|
||||
ValueStore* GetExisting(const ExtensionId& extension_id) const;
|
||||
|
||||
private:
|
||||
std::map<ExtensionId, ValueStore*> app_stores_;
|
||||
std::map<ExtensionId, ValueStore*> extension_stores_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(StorageHelper);
|
||||
};
|
||||
|
||||
StorageHelper& GetStorageHelper(
|
||||
settings_namespace::Namespace settings_namespace);
|
||||
|
||||
~CefValueStoreFactory() override;
|
||||
base::FilePath db_path_;
|
||||
ValueStore* last_created_store_ = nullptr;
|
||||
|
||||
// None of these value stores are owned by this factory, so care must be
|
||||
// taken when calling GetExisting.
|
||||
StorageHelper local_helper_;
|
||||
StorageHelper sync_helper_;
|
||||
StorageHelper managed_helper_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefValueStoreFactory);
|
||||
};
|
||||
|
||||
} // namespace extensions
|
||||
|
||||
#endif // CEF_LIBCEF_BROWSER_EXTENSIONS_VALUE_STORE_CEF_VALUE_STORE_FACTORY_H_
|
Reference in New Issue
Block a user