Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/value_store/value_store_frontend.h" | |
| 6 | |
| 7 #include "chrome/browser/value_store/leveldb_value_store.h" | |
| 8 #include "content/public/browser/browser_thread.h" | |
| 9 | |
| 10 using content::BrowserThread; | |
| 11 | |
| 12 class ValueStoreFrontend::Backend : public base::RefCountedThreadSafe<Backend> { | |
| 13 public: | |
| 14 explicit Backend(const FilePath& db_path) : storage_(NULL) { | |
| 15 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 16 base::Bind(&ValueStoreFrontend::Backend::InitOnFileThread, | |
| 17 this, db_path)); | |
| 18 } | |
| 19 | |
| 20 void Get(const std::string& key, ValueStoreFrontend::ReadCallback callback) { | |
|
not at google - send to devlin
2012/06/10 11:06:08
ditto, const& ?
| |
| 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 22 ValueStore::ReadResult result = storage_->Get(key); | |
| 23 | |
| 24 // Extract the value from the ReadResult and pass ownership of it to the | |
| 25 // callback. | |
| 26 base::Value* value = NULL; | |
| 27 if (!result->HasError()) | |
| 28 result->settings()->RemoveWithoutPathExpansion(key, &value); | |
| 29 | |
| 30 scoped_ptr<base::Value> passed_value(value); | |
| 31 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 32 base::Bind(&ValueStoreFrontend::Backend::RunCallback, | |
| 33 this, callback, base::Passed(passed_value.Pass()))); | |
| 34 } | |
| 35 | |
| 36 void Set(const std::string& key, scoped_ptr<base::Value> value) { | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 38 // We don't need the old value, so skip generating changes. | |
| 39 storage_->Set(ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, | |
| 40 key, *value.get()); | |
| 41 } | |
| 42 | |
| 43 void Remove(const std::string& key) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 45 storage_->Remove(key); | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 friend class base::RefCountedThreadSafe<Backend>; | |
| 50 | |
| 51 virtual ~Backend() { | |
| 52 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
| 53 delete storage_; | |
| 54 } else { | |
| 55 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, storage_); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void InitOnFileThread(const FilePath& db_path) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 61 DCHECK(!storage_); | |
| 62 storage_ = LeveldbValueStore::Create(db_path); | |
| 63 } | |
| 64 | |
| 65 void RunCallback(ValueStoreFrontend::ReadCallback callback, | |
| 66 scoped_ptr<base::Value> value) { | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 68 callback.Run(value.Pass()); | |
| 69 } | |
| 70 | |
| 71 // The actual ValueStore that handles persisting the data to disk. Used | |
| 72 // exclusively on the FILE thread. | |
| 73 LeveldbValueStore* storage_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(Backend); | |
| 76 }; | |
| 77 | |
| 78 ValueStoreFrontend::ValueStoreFrontend(const FilePath& db_path) | |
| 79 : backend_(new Backend(db_path)) { | |
| 80 } | |
| 81 | |
| 82 ValueStoreFrontend::~ValueStoreFrontend() { | |
| 83 DCHECK(CalledOnValidThread()); | |
| 84 } | |
| 85 | |
| 86 void ValueStoreFrontend::Get(const std::string& key, ReadCallback callback) { | |
| 87 DCHECK(CalledOnValidThread()); | |
| 88 | |
| 89 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 90 base::Bind(&ValueStoreFrontend::Backend::Get, | |
| 91 backend_, key, callback)); | |
| 92 } | |
| 93 | |
| 94 void ValueStoreFrontend::Set(const std::string& key, | |
| 95 scoped_ptr<base::Value> value) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 | |
| 98 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 99 base::Bind(&ValueStoreFrontend::Backend::Set, | |
| 100 backend_, key, base::Passed(value.Pass()))); | |
| 101 } | |
| 102 | |
| 103 void ValueStoreFrontend::Remove(const std::string& key) { | |
| 104 DCHECK(CalledOnValidThread()); | |
| 105 | |
| 106 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 107 base::Bind(&ValueStoreFrontend::Backend::Remove, | |
| 108 backend_, key)); | |
| 109 } | |
| OLD | NEW |