1 // Copyright 2015 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.
5 #include "chrome/browser/android/history_report/delta_file_service.h"
8 #include "base/files/file_path.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "chrome/browser/android/history_report/delta_file_backend_leveldb.h"
11 #include "chrome/browser/android/history_report/delta_file_commons.h"
12 #include "content/public/browser/browser_thread.h"
17 void DoAddPage(history_report::DeltaFileBackend
* backend
, const GURL
& url
) {
18 backend
->PageAdded(url
);
21 void DoDeletePage(history_report::DeltaFileBackend
* backend
, const GURL
& url
) {
22 backend
->PageDeleted(url
);
25 void DoTrim(history_report::DeltaFileBackend
* backend
,
27 base::WaitableEvent
* finished
,
29 *result
= backend
->Trim(lower_bound
);
34 history_report::DeltaFileBackend
* backend
,
37 base::WaitableEvent
* finished
,
38 scoped_ptr
<std::vector
<history_report::DeltaFileEntryWithData
> >* result
) {
39 *result
= backend
->Query(last_seq_no
, limit
).Pass();
43 void DoRecreate(history_report::DeltaFileBackend
* backend
,
44 const std::vector
<std::string
>& urls
,
45 base::WaitableEvent
* finished
,
47 *result
= backend
->Recreate(urls
);
51 void DoClear(history_report::DeltaFileBackend
* backend
) {
55 void DoDump(history_report::DeltaFileBackend
* backend
,
56 base::WaitableEvent
* finished
,
57 std::string
* result
) {
58 result
->append(backend
->Dump());
64 namespace history_report
{
66 using content::BrowserThread
;
68 DeltaFileService::DeltaFileService(const base::FilePath
& dir
)
69 : worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()),
70 delta_file_backend_(new DeltaFileBackend(dir
)) {
73 DeltaFileService::~DeltaFileService() {}
75 void DeltaFileService::PageAdded(const GURL
& url
) {
76 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
77 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
80 base::Bind(&DoAddPage
,
81 base::Unretained(delta_file_backend_
.get()),
83 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
86 void DeltaFileService::PageDeleted(const GURL
& url
) {
87 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
88 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
91 base::Bind(&DoDeletePage
,
92 base::Unretained(delta_file_backend_
.get()),
94 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
97 int64
DeltaFileService::Trim(int64 lower_bound
) {
99 base::WaitableEvent
finished(false, false);
100 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
101 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
105 base::Unretained(delta_file_backend_
.get()),
107 base::Unretained(&finished
),
108 base::Unretained(&result
)),
109 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
114 scoped_ptr
<std::vector
<DeltaFileEntryWithData
> > DeltaFileService::Query(
117 scoped_ptr
<std::vector
<DeltaFileEntryWithData
> > result
;
118 base::WaitableEvent
finished(false, false);
119 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
120 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
124 base::Unretained(delta_file_backend_
.get()),
127 base::Unretained(&finished
),
128 base::Unretained(&result
)),
129 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
131 return result
.Pass();
134 bool DeltaFileService::Recreate(const std::vector
<std::string
>& urls
) {
136 base::WaitableEvent
finished(false, false);
137 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
138 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
141 base::Bind(&DoRecreate
,
142 base::Unretained(delta_file_backend_
.get()),
144 base::Unretained(&finished
),
145 base::Unretained(&result
)),
146 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
151 void DeltaFileService::Clear() {
152 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
153 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
157 base::Unretained(delta_file_backend_
.get())),
158 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
161 std::string
DeltaFileService::Dump() {
163 base::WaitableEvent
finished(false, false);
164 base::SequencedWorkerPool
* pool
= BrowserThread::GetBlockingPool();
165 pool
->PostSequencedWorkerTaskWithShutdownBehavior(
169 base::Unretained(delta_file_backend_
.get()),
170 base::Unretained(&finished
),
171 base::Unretained(&dump
)),
172 base::SequencedWorkerPool::BLOCK_SHUTDOWN
);
177 } // namespace history_report