OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/storage/sync_or_local_value_store_cache. h" | 5 #include "chrome/browser/extensions/api/storage/local_value_store_cache.h" |
6 | |
7 #include <limits> | |
6 | 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
10 #include "base/sequenced_task_runner.h" | |
11 #include "chrome/browser/extensions/api/storage/local_storage_backend.h" | 12 #include "chrome/browser/extensions/api/storage/local_storage_backend.h" |
12 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | 13 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
13 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer. h" | 14 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer. h" |
14 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h" | |
15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage. h" | 15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage. h" |
16 #include "chrome/browser/sync/glue/sync_start_util.h" | 16 #include "chrome/common/extensions/api/storage.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
20 #include "extensions/common/permissions/api_permission.h" | 20 #include "extensions/common/permissions/api_permission.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 SyncOrLocalValueStoreCache::SyncOrLocalValueStoreCache( | 26 namespace { |
27 settings_namespace::Namespace settings_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( | |
28 const scoped_refptr<SettingsStorageFactory>& factory, | 42 const scoped_refptr<SettingsStorageFactory>& factory, |
29 const SettingsStorageQuotaEnforcer::Limits& quota, | 43 const base::FilePath& profile_path) { |
30 const scoped_refptr<SettingsObserverList>& observers, | |
31 const base::FilePath& profile_path) | |
32 : settings_namespace_(settings_namespace), initialized_(false) { | |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 DCHECK(settings_namespace_ == settings_namespace::LOCAL || | |
35 settings_namespace_ == settings_namespace::SYNC); | |
36 | 45 |
37 // This post is safe since the destructor can only be invoked from the | 46 // This post is safe since the destructor can only be invoked from the |
38 // same message loop, and any potential post of a deletion task must come | 47 // same message loop, and any potential post of a deletion task must come |
39 // after the constructor returns. | 48 // after the constructor returns. |
40 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
41 BrowserThread::FILE, FROM_HERE, | 50 BrowserThread::FILE, FROM_HERE, |
42 base::Bind(&SyncOrLocalValueStoreCache::InitOnFileThread, | 51 base::Bind(&LocalValueStoreCache::InitOnFileThread, |
43 base::Unretained(this), | 52 base::Unretained(this), |
44 factory, quota, observers, profile_path)); | 53 factory, profile_path)); |
45 } | 54 } |
46 | 55 |
47 SyncOrLocalValueStoreCache::~SyncOrLocalValueStoreCache() { | 56 LocalValueStoreCache::~LocalValueStoreCache() { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
49 } | 58 } |
50 | 59 |
51 syncer::SyncableService* SyncOrLocalValueStoreCache::GetSyncableService( | 60 void LocalValueStoreCache::RunWithValueStoreForExtension( |
52 syncer::ModelType type) const { | |
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
54 DCHECK(initialized_); | |
55 switch (type) { | |
56 case syncer::APP_SETTINGS: | |
57 return app_backend_->GetAsSyncableService(); | |
58 case syncer::EXTENSION_SETTINGS: | |
59 return extension_backend_->GetAsSyncableService(); | |
60 default: | |
61 NOTREACHED(); | |
62 return NULL; | |
63 } | |
64 } | |
65 | |
66 void SyncOrLocalValueStoreCache::RunWithValueStoreForExtension( | |
67 const StorageCallback& callback, | 61 const StorageCallback& callback, |
68 scoped_refptr<const Extension> extension) { | 62 scoped_refptr<const Extension> extension) { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
70 DCHECK(initialized_); | |
71 | 64 |
72 SettingsBackend* backend = | 65 SettingsBackend* backend = |
73 extension->is_app() ? app_backend_.get() : extension_backend_.get(); | 66 extension->is_app() ? app_backend_.get() : extension_backend_.get(); |
74 ValueStore* storage = backend->GetStorage(extension->id()); | 67 ValueStore* storage = backend->GetStorage(extension->id()); |
Devlin
2014/03/05 19:02:55
If !initialized, we crash and burn at this line.
James Cook
2014/03/05 23:14:28
Added back DCHECK(initialized_).
| |
75 | 68 |
76 // A neat way to implement unlimited storage; if the extension has the | 69 // A neat way to implement unlimited storage; if the extension has the |
77 // unlimited storage permission, force through all calls to Set() (in the | 70 // unlimited storage permission, force through all calls to Set() (in the |
78 // same way that writes from sync ignore quota). | 71 // same way that writes from sync ignore quota). |
79 // But only if it's local storage (bad stuff would happen if sync'ed | 72 // But only if it's local storage (bad stuff would happen if sync'ed |
Devlin
2014/03/05 19:02:55
you can take out the stuff about sync'ed storage i
James Cook
2014/03/05 23:14:28
Done.
| |
80 // storage is allowed to be unlimited). | 73 // storage is allowed to be unlimited). |
81 bool is_unlimited = | 74 if (extension->HasAPIPermission(APIPermission::kUnlimitedStorage)) { |
82 settings_namespace_ == settings_namespace::LOCAL && | |
83 extension->HasAPIPermission(APIPermission::kUnlimitedStorage); | |
84 | |
85 if (is_unlimited) { | |
86 WeakUnlimitedSettingsStorage unlimited_storage(storage); | 75 WeakUnlimitedSettingsStorage unlimited_storage(storage); |
87 callback.Run(&unlimited_storage); | 76 callback.Run(&unlimited_storage); |
88 } else { | 77 } else { |
89 callback.Run(storage); | 78 callback.Run(storage); |
90 } | 79 } |
91 } | 80 } |
92 | 81 |
93 void SyncOrLocalValueStoreCache::DeleteStorageSoon( | 82 void LocalValueStoreCache::DeleteStorageSoon(const std::string& extension_id) { |
94 const std::string& extension_id) { | |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
96 DCHECK(initialized_); | |
97 app_backend_->DeleteStorage(extension_id); | 84 app_backend_->DeleteStorage(extension_id); |
Devlin
2014/03/05 19:02:55
that DCHECK(initialized_) made sure we don't deref
James Cook
2014/03/05 23:14:28
Done.
| |
98 extension_backend_->DeleteStorage(extension_id); | 85 extension_backend_->DeleteStorage(extension_id); |
99 } | 86 } |
100 | 87 |
101 void SyncOrLocalValueStoreCache::InitOnFileThread( | 88 void LocalValueStoreCache::InitOnFileThread( |
102 const scoped_refptr<SettingsStorageFactory>& factory, | 89 const scoped_refptr<SettingsStorageFactory>& factory, |
103 const SettingsStorageQuotaEnforcer::Limits& quota, | |
104 const scoped_refptr<SettingsObserverList>& observers, | |
105 const base::FilePath& profile_path) { | 90 const base::FilePath& profile_path) { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
Devlin
2014/03/05 19:02:55
DCHECK(!initialized_) was useful here, too, IMO.
James Cook
2014/03/05 23:14:28
Done.
| |
107 DCHECK(!initialized_); | 92 app_backend_.reset(new LocalStorageBackend( |
108 switch (settings_namespace_) { | 93 factory, |
109 case settings_namespace::LOCAL: | 94 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), |
110 app_backend_.reset(new LocalStorageBackend( | 95 GetLocalQuotaLimits())); |
111 factory, | 96 extension_backend_.reset(new LocalStorageBackend( |
112 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), | 97 factory, |
113 quota)); | 98 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), |
114 extension_backend_.reset(new LocalStorageBackend( | 99 GetLocalQuotaLimits())); |
115 factory, | |
116 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), | |
117 quota)); | |
118 break; | |
119 case settings_namespace::SYNC: | |
120 app_backend_.reset(new SyncStorageBackend( | |
121 factory, | |
122 profile_path.AppendASCII(kSyncAppSettingsDirectoryName), | |
123 quota, | |
124 observers, | |
125 syncer::APP_SETTINGS, | |
126 sync_start_util::GetFlareForSyncableService(profile_path))); | |
127 extension_backend_.reset(new SyncStorageBackend( | |
128 factory, | |
129 profile_path.AppendASCII(kSyncExtensionSettingsDirectoryName), | |
130 quota, | |
131 observers, | |
132 syncer::EXTENSION_SETTINGS, | |
133 sync_start_util::GetFlareForSyncableService(profile_path))); | |
134 break; | |
135 default: | |
136 NOTREACHED(); | |
137 } | |
138 | |
139 initialized_ = true; | |
140 } | 100 } |
141 | 101 |
142 } // namespace extensions | 102 } // namespace extensions |
OLD | NEW |