1 // Copyright 2017 The Chromium Embedded Framework Authors.
2 // Portions copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "libcef/browser/extensions/api/storage/sync_value_store_cache.h"
7
8 #include <stddef.h>
9
10 #include <limits>
11
12 #include "content/public/browser/browser_thread.h"
13 #include "extensions/browser/api/storage/backend_task_runner.h"
14 #include "extensions/browser/api/storage/weak_unlimited_settings_storage.h"
15 #include "extensions/browser/value_store/value_store_factory.h"
16 #include "extensions/common/api/storage.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/permissions/permissions_data.h"
19
20 using content::BrowserThread;
21
22 namespace extensions {
23
24 namespace cef {
25
26 namespace {
27
28 // Returns the quota limit for local storage, taken from the schema in
29 // extensions/common/api/storage.json.
GetLocalQuotaLimits()30 SettingsStorageQuotaEnforcer::Limits GetLocalQuotaLimits() {
31 SettingsStorageQuotaEnforcer::Limits limits = {
32 static_cast<size_t>(api::storage::local::QUOTA_BYTES),
33 std::numeric_limits<size_t>::max(), std::numeric_limits<size_t>::max()};
34 return limits;
35 }
36
37 } // namespace
38
SyncValueStoreCache(const scoped_refptr<ValueStoreFactory> & factory)39 SyncValueStoreCache::SyncValueStoreCache(
40 const scoped_refptr<ValueStoreFactory>& factory)
41 : storage_factory_(factory), quota_(GetLocalQuotaLimits()) {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI);
43 }
44
~SyncValueStoreCache()45 SyncValueStoreCache::~SyncValueStoreCache() {
46 DCHECK(IsOnBackendSequence());
47 }
48
RunWithValueStoreForExtension(StorageCallback callback,scoped_refptr<const Extension> extension)49 void SyncValueStoreCache::RunWithValueStoreForExtension(
50 StorageCallback callback,
51 scoped_refptr<const Extension> extension) {
52 DCHECK(IsOnBackendSequence());
53
54 ValueStore* storage = GetStorage(extension.get());
55
56 // A neat way to implement unlimited storage; if the extension has the
57 // unlimited storage permission, force through all calls to Set().
58 if (extension->permissions_data()->HasAPIPermission(
59 mojom::APIPermissionID::kUnlimitedStorage)) {
60 WeakUnlimitedSettingsStorage unlimited_storage(storage);
61 std::move(callback).Run(&unlimited_storage);
62 } else {
63 std::move(callback).Run(storage);
64 }
65 }
66
DeleteStorageSoon(const std::string & extension_id)67 void SyncValueStoreCache::DeleteStorageSoon(const std::string& extension_id) {
68 DCHECK(IsOnBackendSequence());
69 storage_map_.erase(extension_id);
70 storage_factory_->DeleteSettings(settings_namespace::SYNC,
71 ValueStoreFactory::ModelType::APP,
72 extension_id);
73 storage_factory_->DeleteSettings(settings_namespace::SYNC,
74 ValueStoreFactory::ModelType::EXTENSION,
75 extension_id);
76 }
77
GetStorage(const Extension * extension)78 ValueStore* SyncValueStoreCache::GetStorage(const Extension* extension) {
79 StorageMap::iterator iter = storage_map_.find(extension->id());
80 if (iter != storage_map_.end())
81 return iter->second.get();
82
83 ValueStoreFactory::ModelType model_type =
84 extension->is_app() ? ValueStoreFactory::ModelType::APP
85 : ValueStoreFactory::ModelType::EXTENSION;
86 std::unique_ptr<ValueStore> store = storage_factory_->CreateSettingsStore(
87 settings_namespace::SYNC, model_type, extension->id());
88 std::unique_ptr<SettingsStorageQuotaEnforcer> storage(
89 new SettingsStorageQuotaEnforcer(quota_, std::move(store)));
90 DCHECK(storage.get());
91
92 ValueStore* storage_ptr = storage.get();
93 storage_map_[extension->id()] = std::move(storage);
94 return storage_ptr;
95 }
96 } // namespace cef
97 } // namespace extensions
98