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..88a243808fef90d6712889c083e0e35705e16a8c |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_settings.cc |
| @@ -0,0 +1,249 @@ |
| +// 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. |
| + |
| +namespace { |
| + |
| +const char kDatabaseName[] = "settings"; |
| + |
| +// Creates a storage area of a requested type on the FILE thread. |
| +class CreateStorageClosure { |
| + public: |
| + // Fallback type may be 0 to not have any fallback type. |
| + // Ownership of callback is taken. |
| + CreateStorageClosure(const FilePath& base_path, |
| + std::map<std::string, ExtensionSettingsStorage*>* storage_objs, |
| + const std::string& extension_id, int type, int fallback_type, |
| + ExtensionSettings::Callback* callback) |
| + : base_path_(base_path), storage_objs_(storage_objs), |
| + extension_id_(extension_id), type_(type), fallback_type_(fallback_type), |
| + 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 && fallback_type_ > 0) { |
| + 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(int type) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + ExtensionSettingsStorage* storage = NULL; |
| + |
| + switch (type & ~ExtensionSettingsStorage::CACHED) { |
| + case ExtensionSettingsStorage::NOOP: |
| + storage = new ExtensionSettingsNoopStorage(); |
| + break; |
| + |
| + case ExtensionSettingsStorage::LEVELDB: { |
| + FilePath path = base_path_.Append(extension_id_).Append(kDatabaseName); |
| + // TODO(kalman): why doesn't FilePath/leveldb handle this internally? |
| +#if defined(OS_POSIX) |
| + std::string os_path(path.value()); |
| +#elif defined(OS_WIN) |
| + std::string os_path = base::SysWideToUTF8(path.value()); |
| +#endif |
| + leveldb::Options options; |
| + options.create_if_missing = true; |
| + leveldb::DB* db; |
| + leveldb::Status status = leveldb::DB::Open(options, os_path, &db); |
| + if (!status.ok()) { |
| + LOG(WARNING) << "Failed to create leveldb at " << path.value(); |
|
dgrogan
2011/06/18 01:54:40
You can also print out status.ToString()
not at google - send to devlin
2011/06/20 05:33:11
Done.
|
| + return NULL; |
| + } |
| + storage = new ExtensionSettingsLeveldbStorage(db); |
| + break; |
| + } |
| + |
| + default: |
| + NOTREACHED(); |
| + } |
| + |
| + if (type & ExtensionSettingsStorage::CACHED) { |
| + storage = new ExtensionSettingsStorageCache(storage); |
| + } |
| + |
| + return storage; |
| + } |
| + |
| + const FilePath& base_path_; |
| + std::map<std::string, ExtensionSettingsStorage*>* storage_objs_; |
| + std::string extension_id_; |
| + int type_; |
| + int fallback_type_; |
| + 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() { |
| + delete callback_; |
| + } |
| + |
| + void Run() { |
| + MessageLoop::current()->PostTask(FROM_HERE, |
| + base::Bind(&GetExistingStorageClosure::Run2, base::Unretained(this))); |
| + } |
| + |
| + private: |
| + void Run2() { |
| + callback_->Run(storage_); |
| + delete this; |
| + } |
| + |
| + ExtensionSettings::Callback* callback_; |
| + ExtensionSettingsStorage* storage_; |
| +}; |
| + |
| +// Deletes a storage area on the FILE thread. |
| +class DeleteStorageClosure { |
| + public: |
| + explicit DeleteStorageClosure(ExtensionSettingsStorage* storage) |
| + : storage_(storage) {} |
| + |
| + ~DeleteStorageClosure() {} |
| + |
| + void Run() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, |
|
dgrogan
2011/06/18 01:54:40
Thread-lifetime issues are fresh in my mind, so: d
not at google - send to devlin
2011/06/20 05:33:11
Hmm. I guess not. Excuse my naivety with respect
dgrogan
2011/06/21 02:36:05
The former, I saw it was called from the destructo
not at google - send to devlin
2011/06/22 09:40:38
Ah. Sadly not :)
|
| + FROM_HERE, |
| + base::Bind( |
| + &DeleteStorageClosure::RunOnFileThread, base::Unretained(this))); |
| + } |
| + |
| + private: |
| + void RunOnFileThread() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + delete storage_; |
| + delete this; |
| + } |
| + |
| + ExtensionSettingsStorage* storage_; |
| +}; |
| + |
| +} // namespace |
| + |
| +ExtensionSettings::ExtensionSettings(const FilePath& base_path) |
| + : base_path_(base_path) { |
| +} |
| + |
| +ExtensionSettings::~ExtensionSettings() { |
| + // Need to delete all the storage objects we created; and any leveldb-based |
| + // ones must be deleted on the FILE thread. |
| + std::map<std::string, ExtensionSettingsStorage*>::iterator it; |
| + for (it = storage_objs_.begin(); it != storage_objs_.end(); ++it) { |
| + ExtensionSettingsStorage* storage = it->second; |
| + if (storage->type() & ExtensionSettingsStorage::LEVELDB) { |
| + (new DeleteStorageClosure(storage))->Run(); |
| + } else { |
| + delete storage; |
| + } |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorage(const std::string& extension_id, |
| + ExtensionSettings::Callback* callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + (new CreateStorageClosure( |
| + base_path_, |
| + &storage_objs_, |
| + extension_id, |
| + ExtensionSettingsStorage::LEVELDB | ExtensionSettingsStorage::CACHED, |
| + ExtensionSettingsStorage::NOOP | ExtensionSettingsStorage::CACHED, |
| + callback))->Run(); |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorageForTesting(int type, const std::string& |
| + extension_id, |
| + Callback* callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + (new CreateStorageClosure( |
| + base_path_, |
| + &storage_objs_, |
| + extension_id, |
| + type, |
| + 0, |
| + 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; |
| +} |