1 // Copyright 2014 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 #include "extensions/browser/api/storage/local_value_store_cache.h"
6
7 #include <limits>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "extensions/browser/api/storage/settings_storage_factory.h"
14 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
15 #include "extensions/browser/api/storage/weak_unlimited_settings_storage.h"
16 #include "extensions/browser/value_store/value_store.h"
17 #include "extensions/common/api/storage.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/permissions/api_permission.h"
21 #include "extensions/common/permissions/permissions_data.h"
22
23 using content::BrowserThread;
24
25 namespace extensions {
26
27 namespace {
28
29 // Returns the quota limit for local storage, taken from the schema in
30 // extensions/common/api/storage.json.
GetLocalQuotaLimits()31 SettingsStorageQuotaEnforcer::Limits GetLocalQuotaLimits() {
32 SettingsStorageQuotaEnforcer::Limits limits = {
33 static_cast<size_t>(core_api::storage::local::QUOTA_BYTES),
34 std::numeric_limits<size_t>::max(),
35 std::numeric_limits<size_t>::max()
36 };
37 return limits;
38 }
39
40 } // namespace
41
LocalValueStoreCache(const scoped_refptr<SettingsStorageFactory> & factory,const base::FilePath & profile_path)42 LocalValueStoreCache::LocalValueStoreCache(
43 const scoped_refptr<SettingsStorageFactory>& factory,
44 const base::FilePath& profile_path)
45 : storage_factory_(factory),
46 extension_base_path_(
47 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName)),
48 app_base_path_(profile_path.AppendASCII(kLocalAppSettingsDirectoryName)),
49 quota_(GetLocalQuotaLimits()) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI);
51 }
52
~LocalValueStoreCache()53 LocalValueStoreCache::~LocalValueStoreCache() {
54 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
55 }
56
RunWithValueStoreForExtension(const StorageCallback & callback,scoped_refptr<const Extension> extension)57 void LocalValueStoreCache::RunWithValueStoreForExtension(
58 const StorageCallback& callback,
59 scoped_refptr<const Extension> extension) {
60 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
61
62 ValueStore* storage = GetStorage(extension);
63
64 // A neat way to implement unlimited storage; if the extension has the
65 // unlimited storage permission, force through all calls to Set().
66 if (extension->permissions_data()->HasAPIPermission(
67 APIPermission::kUnlimitedStorage)) {
68 WeakUnlimitedSettingsStorage unlimited_storage(storage);
69 callback.Run(&unlimited_storage);
70 } else {
71 callback.Run(storage);
72 }
73 }
74
DeleteStorageSoon(const std::string & extension_id)75 void LocalValueStoreCache::DeleteStorageSoon(const std::string& extension_id) {
76 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
77 storage_map_.erase(extension_id);
78 storage_factory_->DeleteDatabaseIfExists(app_base_path_, extension_id);
79 storage_factory_->DeleteDatabaseIfExists(extension_base_path_, extension_id);
80 }
81
GetStorage(scoped_refptr<const Extension> extension)82 ValueStore* LocalValueStoreCache::GetStorage(
83 scoped_refptr<const Extension> extension) {
84 StorageMap::iterator iter = storage_map_.find(extension->id());
85 if (iter != storage_map_.end())
86 return iter->second.get();
87
88 const base::FilePath& file_path =
89 extension->is_app() ? app_base_path_ : extension_base_path_;
90 linked_ptr<SettingsStorageQuotaEnforcer> storage(
91 new SettingsStorageQuotaEnforcer(
92 quota_, storage_factory_->Create(file_path, extension->id())));
93 DCHECK(storage.get());
94 storage_map_[extension->id()] = storage;
95 return storage.get();
96 }
97
98 } // namespace extensions
99