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