Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(900)

Unified Diff: chrome/browser/value_store/value_store_frontend.cc

Issue 10545128: Unrevert r141537: Add extensions::StateStore and use that instead of (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix.crash Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/value_store/value_store_frontend.cc
diff --git a/chrome/browser/value_store/value_store_frontend.cc b/chrome/browser/value_store/value_store_frontend.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7a87cba4d182d881db43502d13957687e5bbad49
--- /dev/null
+++ b/chrome/browser/value_store/value_store_frontend.cc
@@ -0,0 +1,114 @@
+// Copyright (c) 2012 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/value_store/value_store_frontend.h"
+
+#include "chrome/browser/value_store/failing_value_store.h"
+#include "chrome/browser/value_store/leveldb_value_store.h"
+#include "content/public/browser/browser_thread.h"
+
+using content::BrowserThread;
+
+class ValueStoreFrontend::Backend : public base::RefCountedThreadSafe<Backend> {
+ public:
+ explicit Backend(const FilePath& db_path) : storage_(NULL) {
+ }
+
+ void Init(const FilePath& db_path) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ DCHECK(!storage_);
+ storage_ = LeveldbValueStore::Create(db_path);
+ if (!storage_)
+ storage_ = new FailingValueStore();
+ }
+
+ void Get(const std::string& key,
+ const ValueStoreFrontend::ReadCallback& callback) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ ValueStore::ReadResult result = storage_->Get(key);
+
+ // Extract the value from the ReadResult and pass ownership of it to the
+ // callback.
+ base::Value* value = NULL;
+ if (!result->HasError())
+ result->settings()->RemoveWithoutPathExpansion(key, &value);
+
+ scoped_ptr<base::Value> passed_value(value);
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(&ValueStoreFrontend::Backend::RunCallback,
+ this, callback, base::Passed(passed_value.Pass())));
+ }
+
+ void Set(const std::string& key, scoped_ptr<base::Value> value) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ // We don't need the old value, so skip generating changes.
+ storage_->Set(ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES,
+ key, *value.get());
+ }
+
+ void Remove(const std::string& key) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ storage_->Remove(key);
+ }
+
+ private:
+ friend class base::RefCountedThreadSafe<Backend>;
+
+ virtual ~Backend() {
+ if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
+ delete storage_;
+ } else {
+ BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, storage_);
+ }
+ }
+
+ void RunCallback(const ValueStoreFrontend::ReadCallback& callback,
+ scoped_ptr<base::Value> value) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ callback.Run(value.Pass());
+ }
+
+ // The actual ValueStore that handles persisting the data to disk. Used
+ // exclusively on the FILE thread.
+ ValueStore* storage_;
+
+ DISALLOW_COPY_AND_ASSIGN(Backend);
+};
+
+ValueStoreFrontend::ValueStoreFrontend(const FilePath& db_path)
+ : backend_(new Backend(db_path)) {
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ValueStoreFrontend::Backend::Init,
+ backend_, db_path));
+}
+
+ValueStoreFrontend::~ValueStoreFrontend() {
+ DCHECK(CalledOnValidThread());
+}
+
+void ValueStoreFrontend::Get(const std::string& key,
+ const ReadCallback& callback) {
+ DCHECK(CalledOnValidThread());
+
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ValueStoreFrontend::Backend::Get,
+ backend_, key, callback));
+}
+
+void ValueStoreFrontend::Set(const std::string& key,
+ scoped_ptr<base::Value> value) {
+ DCHECK(CalledOnValidThread());
+
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ValueStoreFrontend::Backend::Set,
+ backend_, key, base::Passed(value.Pass())));
+}
+
+void ValueStoreFrontend::Remove(const std::string& key) {
+ DCHECK(CalledOnValidThread());
+
+ BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
+ base::Bind(&ValueStoreFrontend::Backend::Remove,
+ backend_, key));
+}
« no previous file with comments | « chrome/browser/value_store/value_store_frontend.h ('k') | chrome/browser/value_store/value_store_frontend_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698