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, | |
21 const ValueStoreFrontend::ReadCallback& callback) { | |
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
23 ValueStore::ReadResult result = storage_->Get(key); | |
24 | |
25 // Extract the value from the ReadResult and pass ownership of it to the | |
26 // callback. | |
27 base::Value* value = NULL; | |
28 if (!result->HasError()) | |
29 result->settings()->RemoveWithoutPathExpansion(key, &value); | |
30 | |
31 scoped_ptr<base::Value> passed_value(value); | |
32 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
33 base::Bind(&ValueStoreFrontend::Backend::RunCallback, | |
34 this, callback, base::Passed(passed_value.Pass()))); | |
35 } | |
36 | |
37 void Set(const std::string& key, scoped_ptr<base::Value> value) { | |
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
39 // We don't need the old value, so skip generating changes. | |
40 storage_->Set(ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, | |
41 key, *value.get()); | |
42 } | |
43 | |
44 void Remove(const std::string& key) { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
46 storage_->Remove(key); | |
47 } | |
48 | |
49 private: | |
50 friend class base::RefCountedThreadSafe<Backend>; | |
51 | |
52 virtual ~Backend() { | |
53 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
54 delete storage_; | |
55 } else { | |
56 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, storage_); | |
57 } | |
58 } | |
59 | |
60 void InitOnFileThread(const FilePath& db_path) { | |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
62 DCHECK(!storage_); | |
63 storage_ = LeveldbValueStore::Create(db_path); | |
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 LeveldbValueStore* storage_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(Backend); | |
77 }; | |
78 | |
79 ValueStoreFrontend::ValueStoreFrontend(const FilePath& db_path) | |
80 : backend_(new Backend(db_path)) { | |
81 } | |
82 | |
83 ValueStoreFrontend::~ValueStoreFrontend() { | |
84 DCHECK(CalledOnValidThread()); | |
85 } | |
86 | |
87 void ValueStoreFrontend::Get(const std::string& key, | |
88 const ReadCallback& callback) { | |
89 DCHECK(CalledOnValidThread()); | |
90 | |
91 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
92 base::Bind(&ValueStoreFrontend::Backend::Get, | |
93 backend_, key, callback)); | |
94 } | |
95 | |
96 void ValueStoreFrontend::Set(const std::string& key, | |
97 scoped_ptr<base::Value> value) { | |
98 DCHECK(CalledOnValidThread()); | |
99 | |
100 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
101 base::Bind(&ValueStoreFrontend::Backend::Set, | |
102 backend_, key, base::Passed(value.Pass()))); | |
103 } | |
104 | |
105 void ValueStoreFrontend::Remove(const std::string& key) { | |
106 DCHECK(CalledOnValidThread()); | |
107 | |
108 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
109 base::Bind(&ValueStoreFrontend::Backend::Remove, | |
110 backend_, key)); | |
111 } | |
OLD | NEW |