1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// 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 "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/file_path.h"
#include "base/logging.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:
Backend() : storage_(NULL) {}
void Init(const FilePath& db_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DCHECK(!storage_);
TRACE_EVENT0("ValueStoreFrontend::Backend", "Init");
db_path_ = db_path;
storage_ = new LeveldbValueStore(db_path);
}
// This variant is useful for testing (using a mock ValueStore).
void InitWithStore(scoped_ptr<ValueStore> storage) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
DCHECK(!storage_);
storage_ = storage.release();
}
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);
else
LOG(INFO) << "Reading " << key << " from " << db_path_.value()
<< " failed: " << result->error();
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_;
FilePath db_path_;
DISALLOW_COPY_AND_ASSIGN(Backend);
};
ValueStoreFrontend::ValueStoreFrontend()
: backend_(new Backend()) {
}
ValueStoreFrontend::ValueStoreFrontend(const FilePath& db_path)
: backend_(new Backend()) {
Init(db_path);
}
ValueStoreFrontend::ValueStoreFrontend(ValueStore* value_store)
: backend_(new Backend()) {
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&ValueStoreFrontend::Backend::InitWithStore,
backend_, base::Passed(scoped_ptr<ValueStore>(value_store))));
}
ValueStoreFrontend::~ValueStoreFrontend() {
DCHECK(CalledOnValidThread());
}
void ValueStoreFrontend::Init(const FilePath& db_path) {
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&ValueStoreFrontend::Backend::Init,
backend_, db_path));
}
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));
}
|