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