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..70df4d75040679a9d3e1603bbe14b8b0e291a95a |
--- /dev/null |
+++ b/chrome/browser/extensions/extension_settings.cc |
@@ -0,0 +1,185 @@ |
+// 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: |
+ 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, |
+ const 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))); |
Matt Perry
2011/06/23 19:14:11
nit: indent +2
not at google - send to devlin
2011/06/27 08:51:02
Done.
|
+ } |
+ |
+ 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_; |
+ 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, |
+ const ExtensionSettings::Callback& callback) { |
+ if (!GetExistingStorage(extension_id, callback)) { |
+ (new CreateStorageClosure( |
+ base_path_, |
+ &storage_objs_, |
+ extension_id, |
+ ExtensionSettingsStorage::LEVELDB, |
+ ExtensionSettingsStorage::NOOP, |
+ true, |
+ callback))->Run(); |
+ } |
+} |
+ |
+void ExtensionSettings::GetStorageForTesting( |
+ ExtensionSettingsStorage::Type type, |
+ bool cached, |
+ const std::string& extension_id, |
+ const 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, const 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); |
+ MessageLoop::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&Callback::Run, base::Unretained(&callback), storage)); |
+ return true; |
+} |