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..fff4375974582c3141075625fb696f9449e2356d |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_settings.cc |
| @@ -0,0 +1,167 @@ |
| +// 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. |
| + |
| +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->DeleteSoon(); |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorage( |
| + const std::string& extension_id, |
| + const ExtensionSettings::Callback& callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + StartCreationOfStorage( |
| + extension_id, |
| + ExtensionSettingsStorage::LEVELDB, |
| + ExtensionSettingsStorage::NOOP, |
| + true, |
| + callback); |
| + } |
| +} |
| + |
| +void ExtensionSettings::GetStorageForTesting( |
| + ExtensionSettingsStorage::Type type, |
| + bool cached, |
| + const std::string& extension_id, |
| + const Callback& callback) { |
| + if (!GetExistingStorage(extension_id, callback)) { |
| + StartCreationOfStorage(extension_id, type, type, cached, callback); |
| + } |
| +} |
| + |
| +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(new Callback(callback)), |
|
Matt Perry
2011/06/29 18:08:11
you're leaking the new Callback here. I don't beli
not at google - send to devlin
2011/08/03 06:36:51
Done.
|
| + storage)); |
| + return true; |
| +} |
| + |
| +void ExtensionSettings::StartCreationOfStorage( |
| + const std::string& extension_id, |
| + ExtensionSettingsStorage::Type type, |
| + ExtensionSettingsStorage::Type fallback_type, |
| + bool cached, |
| + const Callback& callback) { |
| + // Make sure we're not deleted mid-callback. |
| + // Released in EndCreationOfStorage(). |
| + AddRef(); |
|
Matt Perry
2011/06/29 18:08:11
base::Bind normally handles this for you - except
not at google - send to devlin
2011/08/03 06:36:51
Actually it should be fine to just let Bind() refc
Matt Perry
2011/08/03 19:33:52
if Bind holds the last reference, then the Release
Matt Perry
2011/08/03 19:33:52
Bind will release its reference when the task is r
not at google - send to devlin
2011/08/03 22:06:55
Changed to RefCountedThreadSafe.
The last referen
|
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind( |
| + &ExtensionSettings::CreateStorageOnFileThread, |
| + base::Unretained(this), |
| + extension_id, |
| + type, |
| + fallback_type, |
| + cached, |
| + callback)); |
| +} |
| + |
| +void ExtensionSettings::CreateStorageOnFileThread( |
| + const std::string& extension_id, |
| + ExtensionSettingsStorage::Type type, |
| + ExtensionSettingsStorage::Type fallback_type, |
| + bool cached, |
| + const Callback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + ExtensionSettingsStorage* storage = CreateStorage(extension_id, type, cached); |
| + if (storage == NULL && fallback_type != type) { |
| + storage = CreateStorage(extension_id, fallback_type, cached); |
| + } |
| + DCHECK(storage != NULL); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind( |
| + &ExtensionSettings::EndCreationOfStorage, |
| + base::Unretained(this), |
| + extension_id, |
| + storage, |
| + callback)); |
| +} |
| + |
| +ExtensionSettingsStorage* ExtensionSettings::CreateStorage( |
| + const std::string& extension_id, |
| + ExtensionSettingsStorage::Type type, |
| + bool cached) { |
| + 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; |
| +} |
| + |
| +void ExtensionSettings::EndCreationOfStorage( |
| + const std::string& extension_id, |
| + ExtensionSettingsStorage* storage, |
| + const Callback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + // 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); |
| + // Release reference added by StartCreationOfStorage(). |
| + Release(); |
| +} |