Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/settings/settings_storage_frontend.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/file_path.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/extensions/settings/settings_backend.h" | |
| 12 #include "chrome/browser/extensions/settings/settings_storage_quota_enforcer.h" | |
| 13 #include "chrome/browser/extensions/settings/weak_unlimited_settings_storage.h" | |
| 14 #include "chrome/common/extensions/extension.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 | |
| 17 using content::BrowserThread; | |
| 18 | |
| 19 namespace extensions { | |
| 20 | |
| 21 namespace { | |
| 22 | |
|
not at google - send to devlin
2012/07/18 07:46:18
Yeah, so I think a lot of this file will change/si
Joao da Silva
2012/07/18 21:40:25
Yep, managed to do away with the BackendWrapper on
| |
| 23 void CallbackWithStorage( | |
| 24 const std::string& extension_id, | |
| 25 const SettingsNamespaceFrontend::StorageCallback& callback, | |
| 26 SettingsBackend* backend) { | |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 28 callback.Run(backend->GetStorage(extension_id)); | |
| 29 } | |
| 30 | |
| 31 void CallbackWithUnlimitedStorage( | |
| 32 const std::string& extension_id, | |
| 33 const SettingsNamespaceFrontend::StorageCallback& callback, | |
| 34 SettingsBackend* backend) { | |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 36 WeakUnlimitedSettingsStorage unlimited_storage( | |
|
not at google - send to devlin
2012/07/18 07:46:18
If it's made synchronous then this WeakUnlimited t
Joao da Silva
2012/07/18 21:40:25
I'm not entirely sure of what you're proposing to
not at google - send to devlin
2012/07/19 06:43:17
Oh, I remember now. Yeah, ignore this.
| |
| 37 backend->GetStorage(extension_id)); | |
| 38 callback.Run(&unlimited_storage); | |
| 39 } | |
| 40 | |
| 41 void DeleteStorageOnFileThread( | |
| 42 const std::string& extension_id, SettingsBackend* backend) { | |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 44 backend->DeleteStorage(extension_id); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 // Ref-counted container for a SettingsBackend object. | |
| 50 class SettingsStorageFrontend::BackendWrapper | |
| 51 : public base::RefCountedThreadSafe<BackendWrapper> { | |
| 52 public: | |
| 53 // Creates a new BackendWrapper and initializes it on the FILE thread. | |
| 54 static scoped_refptr<BackendWrapper> CreateAndInit( | |
| 55 const scoped_refptr<SettingsStorageFactory>& factory, | |
| 56 const SettingsStorageQuotaEnforcer::Limits& quota, | |
| 57 const scoped_refptr<SettingsObserverList>& observers, | |
| 58 const FilePath& path) { | |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 60 scoped_refptr<BackendWrapper> backend_wrapper = new BackendWrapper(); | |
| 61 BrowserThread::PostTask( | |
| 62 BrowserThread::FILE, FROM_HERE, | |
| 63 base::Bind( | |
| 64 &BackendWrapper::InitOnFileThread, | |
| 65 backend_wrapper, factory, quota, observers, path)); | |
| 66 return backend_wrapper; | |
| 67 } | |
| 68 | |
| 69 typedef base::Callback<void(SettingsBackend*)> BackendCallback; | |
| 70 | |
| 71 // Runs |callback| with the wrapped Backend on the FILE thread. | |
| 72 void RunWithBackend(const BackendCallback& callback) { | |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 74 BrowserThread::PostTask( | |
| 75 BrowserThread::FILE, FROM_HERE, | |
| 76 base::Bind( | |
| 77 &BackendWrapper::RunWithBackendOnFileThread, this, callback)); | |
| 78 } | |
| 79 | |
| 80 SettingsBackend* GetBackend() const { | |
| 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 82 return backend_; | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 friend class base::RefCountedThreadSafe<BackendWrapper>; | |
| 87 | |
| 88 BackendWrapper() : backend_(NULL) { | |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 90 } | |
| 91 | |
| 92 virtual ~BackendWrapper() { | |
| 93 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
| 94 delete backend_; | |
| 95 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 96 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backend_); | |
| 97 } else { | |
| 98 NOTREACHED(); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void InitOnFileThread( | |
| 103 const scoped_refptr<SettingsStorageFactory>& storage_factory, | |
| 104 const SettingsStorageQuotaEnforcer::Limits& quota, | |
| 105 const scoped_refptr<SettingsObserverList>& observers, | |
| 106 const FilePath& path) { | |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 108 DCHECK(!backend_); | |
| 109 backend_ = new SettingsBackend(storage_factory, path, quota, observers); | |
| 110 } | |
| 111 | |
| 112 void RunWithBackendOnFileThread(const BackendCallback& callback) { | |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 114 DCHECK(backend_); | |
| 115 callback.Run(backend_); | |
| 116 } | |
| 117 | |
| 118 // Wrapped Backend. Used exclusively on the FILE thread, and is created on | |
| 119 // the FILE thread in InitOnFileThread. | |
| 120 SettingsBackend* backend_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(BackendWrapper); | |
| 123 }; | |
| 124 | |
| 125 SettingsStorageFrontend::SettingsStorageFrontend( | |
| 126 settings_namespace::Namespace settings_namespace, | |
| 127 const scoped_refptr<SettingsStorageFactory>& factory, | |
| 128 const SettingsStorageQuotaEnforcer::Limits& quota, | |
| 129 const scoped_refptr<SettingsObserverList>& observers, | |
| 130 const FilePath& profile_path) | |
| 131 : settings_namespace_(settings_namespace) { | |
| 132 DCHECK(settings_namespace_ == settings_namespace::LOCAL || | |
| 133 settings_namespace_ == settings_namespace::SYNC); | |
| 134 const bool local = settings_namespace_ == settings_namespace::LOCAL; | |
| 135 const FilePath app_path = profile_path.AppendASCII( | |
| 136 local ? ExtensionService::kLocalAppSettingsDirectoryName | |
| 137 : ExtensionService::kSyncAppSettingsDirectoryName); | |
| 138 const FilePath extension_path = profile_path.AppendASCII( | |
| 139 local ? ExtensionService::kLocalExtensionSettingsDirectoryName | |
| 140 : ExtensionService::kSyncExtensionSettingsDirectoryName); | |
| 141 app_backend_ = | |
| 142 BackendWrapper::CreateAndInit(factory, quota, observers, app_path); | |
| 143 extension_backend_ = | |
| 144 BackendWrapper::CreateAndInit(factory, quota, observers, extension_path); | |
| 145 } | |
| 146 | |
| 147 SettingsStorageFrontend::~SettingsStorageFrontend() {} | |
| 148 | |
| 149 void SettingsStorageFrontend::RunWithStorage(const Extension* extension, | |
| 150 const StorageCallback& callback) { | |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 152 // A neat way to implement unlimited storage; if the extension has the | |
| 153 // unlimited storage permission, force through all calls to Set() (in the | |
| 154 // same way that writes from sync ignore quota). | |
| 155 // But only if it's local storage (bad stuff would happen if sync'ed | |
| 156 // storage is allowed to be unlimited). | |
| 157 bool is_unlimited = | |
| 158 settings_namespace_ == settings_namespace::LOCAL && | |
| 159 extension->HasAPIPermission(APIPermission::kUnlimitedStorage); | |
| 160 | |
| 161 scoped_refptr<BackendWrapper> backend = | |
| 162 extension->is_app() ? app_backend_ : extension_backend_; | |
| 163 | |
| 164 backend->RunWithBackend( | |
| 165 base::Bind( | |
| 166 is_unlimited ? &CallbackWithUnlimitedStorage : &CallbackWithStorage, | |
| 167 extension->id(), | |
| 168 callback)); | |
| 169 } | |
| 170 | |
| 171 void SettingsStorageFrontend::DeleteStorageSoon( | |
| 172 const std::string& extension_id) { | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 174 BackendWrapper::BackendCallback callback = | |
| 175 base::Bind(&DeleteStorageOnFileThread, extension_id); | |
| 176 app_backend_->RunWithBackend(callback); | |
| 177 extension_backend_->RunWithBackend(callback); | |
| 178 } | |
| 179 | |
| 180 SettingsBackend* SettingsStorageFrontend::GetAppBackend() const { | |
| 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 182 return app_backend_->GetBackend(); | |
| 183 } | |
| 184 | |
| 185 SettingsBackend* SettingsStorageFrontend::GetExtensionBackend() const { | |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 187 return extension_backend_->GetBackend(); | |
| 188 } | |
| 189 | |
| 190 } // namespace extensions | |
| OLD | NEW |