OLD | NEW |
| (Empty) |
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 "chrome/browser/extensions/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 "chrome/browser/extensions/api/storage/local_storage_backend.h" | |
13 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" | |
14 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" | |
15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage.
h" | |
16 #include "chrome/common/extensions/api/storage.h" | |
17 #include "content/public/browser/browser_thread.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 // chrome/common/extensions/api/storage.json. | |
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(), | |
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 : initialized_(false) { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
46 | |
47 // This post is safe since the destructor can only be invoked from the | |
48 // same message loop, and any potential post of a deletion task must come | |
49 // after the constructor returns. | |
50 BrowserThread::PostTask( | |
51 BrowserThread::FILE, FROM_HERE, | |
52 base::Bind(&LocalValueStoreCache::InitOnFileThread, | |
53 base::Unretained(this), | |
54 factory, profile_path)); | |
55 } | |
56 | |
57 LocalValueStoreCache::~LocalValueStoreCache() { | |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
59 } | |
60 | |
61 void LocalValueStoreCache::RunWithValueStoreForExtension( | |
62 const StorageCallback& callback, | |
63 scoped_refptr<const Extension> extension) { | |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
65 DCHECK(initialized_); | |
66 | |
67 SettingsBackend* backend = | |
68 extension->is_app() ? app_backend_.get() : extension_backend_.get(); | |
69 ValueStore* storage = backend->GetStorage(extension->id()); | |
70 | |
71 // A neat way to implement unlimited storage; if the extension has the | |
72 // unlimited storage permission, force through all calls to Set(). | |
73 if (extension->HasAPIPermission(APIPermission::kUnlimitedStorage)) { | |
74 WeakUnlimitedSettingsStorage unlimited_storage(storage); | |
75 callback.Run(&unlimited_storage); | |
76 } else { | |
77 callback.Run(storage); | |
78 } | |
79 } | |
80 | |
81 void LocalValueStoreCache::DeleteStorageSoon(const std::string& extension_id) { | |
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
83 DCHECK(initialized_); | |
84 app_backend_->DeleteStorage(extension_id); | |
85 extension_backend_->DeleteStorage(extension_id); | |
86 } | |
87 | |
88 void LocalValueStoreCache::InitOnFileThread( | |
89 const scoped_refptr<SettingsStorageFactory>& factory, | |
90 const base::FilePath& profile_path) { | |
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
92 DCHECK(!initialized_); | |
93 app_backend_.reset(new LocalStorageBackend( | |
94 factory, | |
95 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), | |
96 GetLocalQuotaLimits())); | |
97 extension_backend_.reset(new LocalStorageBackend( | |
98 factory, | |
99 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), | |
100 GetLocalQuotaLimits())); | |
101 initialized_ = true; | |
102 } | |
103 | |
104 } // namespace extensions | |
OLD | NEW |