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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// Copyright 2015 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/android/history_report/delta_file_service.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/android/history_report/delta_file_backend_leveldb.h"
#include "chrome/browser/android/history_report/delta_file_commons.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
namespace {
void DoAddPage(history_report::DeltaFileBackend* backend, const GURL& url) {
backend->PageAdded(url);
}
void DoDeletePage(history_report::DeltaFileBackend* backend, const GURL& url) {
backend->PageDeleted(url);
}
void DoTrim(history_report::DeltaFileBackend* backend,
int64_t lower_bound,
base::WaitableEvent* finished,
int64_t* result) {
*result = backend->Trim(lower_bound);
finished->Signal();
}
void DoQuery(
history_report::DeltaFileBackend* backend,
int64_t last_seq_no,
int32_t limit,
base::WaitableEvent* finished,
scoped_ptr<std::vector<history_report::DeltaFileEntryWithData>>* result) {
*result = backend->Query(last_seq_no, limit);
finished->Signal();
}
void DoRecreate(history_report::DeltaFileBackend* backend,
const std::vector<std::string>& urls,
base::WaitableEvent* finished,
bool* result) {
*result = backend->Recreate(urls);
finished->Signal();
}
void DoClear(history_report::DeltaFileBackend* backend) {
backend->Clear();
}
void DoDump(history_report::DeltaFileBackend* backend,
base::WaitableEvent* finished,
std::string* result) {
result->append(backend->Dump());
finished->Signal();
}
} // namespace
namespace history_report {
using content::BrowserThread;
DeltaFileService::DeltaFileService(const base::FilePath& dir)
: worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()),
delta_file_backend_(new DeltaFileBackend(dir)) {
}
DeltaFileService::~DeltaFileService() {}
void DeltaFileService::PageAdded(const GURL& url) {
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoAddPage,
base::Unretained(delta_file_backend_.get()),
url),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
}
void DeltaFileService::PageDeleted(const GURL& url) {
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoDeletePage,
base::Unretained(delta_file_backend_.get()),
url),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
}
int64_t DeltaFileService::Trim(int64_t lower_bound) {
int64_t result;
base::WaitableEvent finished(false, false);
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoTrim,
base::Unretained(delta_file_backend_.get()),
lower_bound,
base::Unretained(&finished),
base::Unretained(&result)),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
finished.Wait();
return result;
}
scoped_ptr<std::vector<DeltaFileEntryWithData>> DeltaFileService::Query(
int64_t last_seq_no,
int32_t limit) {
scoped_ptr<std::vector<DeltaFileEntryWithData> > result;
base::WaitableEvent finished(false, false);
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoQuery,
base::Unretained(delta_file_backend_.get()),
last_seq_no,
limit,
base::Unretained(&finished),
base::Unretained(&result)),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
finished.Wait();
return result;
}
bool DeltaFileService::Recreate(const std::vector<std::string>& urls) {
bool result = false;
base::WaitableEvent finished(false, false);
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoRecreate,
base::Unretained(delta_file_backend_.get()),
urls,
base::Unretained(&finished),
base::Unretained(&result)),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
finished.Wait();
return result;
}
void DeltaFileService::Clear() {
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoClear,
base::Unretained(delta_file_backend_.get())),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
}
std::string DeltaFileService::Dump() {
std::string dump;
base::WaitableEvent finished(false, false);
base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
pool->PostSequencedWorkerTaskWithShutdownBehavior(
worker_pool_token_,
FROM_HERE,
base::Bind(&DoDump,
base::Unretained(delta_file_backend_.get()),
base::Unretained(&finished),
base::Unretained(&dump)),
base::SequencedWorkerPool::BLOCK_SHUTDOWN);
finished.Wait();
return dump;
}
} // namespace history_report
|