Chromium Code Reviews| Index: chrome/browser/extensions/extension_settings.cc |
| diff --git a/chrome/browser/extensions/extension_settings.cc b/chrome/browser/extensions/extension_settings.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b75c7ee86f5b7e1c8096178e5cc74bcbe40216a0 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_settings.cc |
| @@ -0,0 +1,204 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/extension_settings.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "content/browser/browser_thread.h" |
| +#include "chrome/browser/extensions/extension_settings_leveldb_storage.h" |
| +#include "chrome/browser/extensions/extension_settings_noop_storage.h" |
| +#include "chrome/browser/extensions/extension_settings_storage_cache.h" |
| +#include "third_party/leveldb/include/leveldb/iterator.h" |
| +#include "third_party/leveldb/include/leveldb/write_batch.h" |
| + |
| +// TODO(kalman): Policy for dots in the key names. Make sure the behaviour of |
| +// DictionaryValue and JSON classes is consistent with leveldb. |
| +// TODO(kalman): Integration tests. |
| +// TODO(kalman): More unit tests (see extension_settings_storage_unittest.cc). |
| +// TODO(kalman): Use structured cloning rather than JSON. |
| +// TODO(kalman): Quotas. |
| + |
| +namespace { |
| + |
| +// Creates a storage area of a requested type on the FILE thread. |
| +class CreateStorageClosure { |
| + public: |
| + // Ownership of callback is taken. |
| + CreateStorageClosure( |
| + const FilePath& base_path, |
| + std::map<std::string, ExtensionSettingsStorage*>* storage_objs, |
| + const std::string& extension_id, |
| + ExtensionSettingsStorage::Type type, |
| + ExtensionSettingsStorage::Type fallback_type, |
| + bool cached, |
| + ExtensionSettings::Callback* callback) |
| + : base_path_(base_path), storage_objs_(storage_objs), |
| + extension_id_(extension_id), type_(type), fallback_type_(fallback_type), |
| + cached_(cached), callback_(callback), storage_(NULL) {} |
| + |
| + ~CreateStorageClosure() {} |
| + |
| + void Run() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind( |
| + &CreateStorageClosure::RunOnFileThread, base::Unretained(this))); |
| + } |
| + |
| + private: |
| + void RunOnFileThread() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + storage_ = CreateStorage(type_); |
| + if (storage_ == NULL && type_ != fallback_type_) { |
| + storage_ = CreateStorage(fallback_type_); |
| + } |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&CreateStorageClosure::Done, base::Unretained(this))); |
| + } |
| + |
| + void Done() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(storage_ != NULL); |
| + // Cache the result now. To avoid a race condition, check again to see |
| + // whether a storage has been created already; if so, use that one. |
| + std::map<std::string, ExtensionSettingsStorage*>::iterator existing = |
| + storage_objs_->find(extension_id_); |
| + if (existing == storage_objs_->end()) { |
| + (*storage_objs_)[extension_id_] = storage_; |
| + } else { |
| + delete storage_; |
| + storage_ = existing->second; |
| + DCHECK(storage_ != NULL); |
| + } |
| + callback_->Run(storage_); |
| + delete this; |
| + } |
| + |
| + // Creates a storage object of a given type. If that fails, returns NULL. |
| + ExtensionSettingsStorage* CreateStorage(ExtensionSettingsStorage::Type type) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + ExtensionSettingsStorage* storage = NULL; |
| + |
| + switch (type) { |
| + case ExtensionSettingsStorage::NOOP: |
| + storage = new ExtensionSettingsNoopStorage(); |
| + break; |
| + case ExtensionSettingsStorage::LEVELDB: |
| + storage = ExtensionSettingsLeveldbStorage::Create( |
| + base_path_, extension_id_); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + if (storage != NULL && cached_) { |
| + storage = new ExtensionSettingsStorageCache(storage); |
| + } |
| + |
| + return storage; |
| + } |
| + |
| + const FilePath& base_path_; |
| + std::map<std::string, ExtensionSettingsStorage*>* storage_objs_; |
| + std::string extension_id_; |
| + ExtensionSettingsStorage::Type type_; |
| + ExtensionSettingsStorage::Type fallback_type_; |
| + bool cached_; |
| + scoped_ptr<ExtensionSettings::Callback> callback_; |
| + |
| + ExtensionSettingsStorage* storage_; |
| +}; |
| + |
| +// Returns a specified storage object on the message loop of the current thread. |
| +// Used to force an asynchronous API even when a storage area already exists. |
| +class GetExistingStorageClosure { |
| + public: |
| + GetExistingStorageClosure( |
| + ExtensionSettings::Callback* callback, |
| + ExtensionSettingsStorage* storage) |
| + : callback_(callback), storage_(storage) {} |
| + |
| + ~GetExistingStorageClosure() {} |
| + |
| + void Run() { |
| + MessageLoop::current()->PostTask(FROM_HERE, |
| + base::Bind(&GetExistingStorageClosure::Run2, base::Unretained(this))); |
| + } |
| + |
| + private: |
| + void Run2() { |
| + callback_->Run(storage_); |
| + delete this; |
| + } |
| + |
| + scoped_ptr<ExtensionSettings::Callback> callback_; |
| + ExtensionSettingsStorage* storage_; |
| +}; |
| + |
| +} // namespace |
| + |
| +ExtensionSettings::ExtensionSettings(const FilePath& base_path) |
| + : base_path_(base_path) { |
| +} |
| + |
| +ExtensionSettings::~ExtensionSettings() { |
| + std::map<std::string, ExtensionSettingsStorage*>::iterator it; |
| + for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) { |
| + it->second->DestroyEventually(); |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorage(const std::string& extension_id, |
| + ExtensionSettings::Callback* callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + (new CreateStorageClosure( |
| + base_path_, |
| + &storage_objs_, |
|
Matt Perry
2011/06/23 18:11:45
What if ExtensionSettings is deleted before Create
not at google - send to devlin
2011/06/27 08:51:02
Hm. So I guess in the case where
1. GetStorage()
|
| + extension_id, |
| + ExtensionSettingsStorage::LEVELDB, |
| + ExtensionSettingsStorage::NOOP, |
| + true, |
| + callback))->Run(); |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorageForTesting( |
| + ExtensionSettingsStorage::Type type, |
| + bool cached, |
| + const std::string& extension_id, |
| + Callback* callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + (new CreateStorageClosure( |
| + base_path_, |
| + &storage_objs_, |
| + extension_id, |
| + type, |
| + type, |
| + cached, |
| + callback))->Run(); |
| + } |
| +} |
| + |
| +bool ExtensionSettings::GetExistingStorage(const std::string& extension_id, |
| + ExtensionSettings::Callback* callback) { |
| + std::map<std::string, ExtensionSettingsStorage*>::iterator existing = |
| + storage_objs_.find(extension_id); |
| + if (existing == storage_objs_.end()) { |
| + // No existing storage. |
| + return false; |
| + } |
| + // Existing storage. Reply with that. |
| + ExtensionSettingsStorage* storage = existing->second; |
| + DCHECK(storage != NULL); |
| + (new GetExistingStorageClosure(callback, storage))->Run(); |
| + return true; |
| +} |