OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/android/history_report/delta_file_service.h" |
| 6 |
| 7 #include "base/bind.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" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 void DoAddPage(history_report::DeltaFileBackend* backend, const GURL& url) { |
| 18 backend->PageAdded(url); |
| 19 } |
| 20 |
| 21 void DoDeletePage(history_report::DeltaFileBackend* backend, const GURL& url) { |
| 22 backend->PageDeleted(url); |
| 23 } |
| 24 |
| 25 void DoTrim(history_report::DeltaFileBackend* backend, |
| 26 int64 lower_bound, |
| 27 base::WaitableEvent* finished, |
| 28 int64* result) { |
| 29 *result = backend->Trim(lower_bound); |
| 30 finished->Signal(); |
| 31 } |
| 32 |
| 33 void DoQuery( |
| 34 history_report::DeltaFileBackend* backend, |
| 35 int64 last_seq_no, |
| 36 int32 limit, |
| 37 base::WaitableEvent* finished, |
| 38 scoped_ptr<std::vector<history_report::DeltaFileEntryWithData> >* result) { |
| 39 *result = backend->Query(last_seq_no, limit).Pass(); |
| 40 finished->Signal(); |
| 41 } |
| 42 |
| 43 void DoRecreate(history_report::DeltaFileBackend* backend, |
| 44 const std::vector<std::string>& urls, |
| 45 base::WaitableEvent* finished, |
| 46 bool* result) { |
| 47 *result = backend->Recreate(urls); |
| 48 finished->Signal(); |
| 49 } |
| 50 |
| 51 void DoClear(history_report::DeltaFileBackend* backend) { |
| 52 backend->Clear(); |
| 53 } |
| 54 |
| 55 void DoDump(history_report::DeltaFileBackend* backend, |
| 56 base::WaitableEvent* finished, |
| 57 std::string* result) { |
| 58 result->append(backend->Dump()); |
| 59 finished->Signal(); |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 |
| 64 namespace history_report { |
| 65 |
| 66 using content::BrowserThread; |
| 67 |
| 68 DeltaFileService::DeltaFileService(const base::FilePath& dir) |
| 69 : worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), |
| 70 delta_file_backend_(new DeltaFileBackend(dir)) { |
| 71 } |
| 72 |
| 73 DeltaFileService::~DeltaFileService() {} |
| 74 |
| 75 void DeltaFileService::PageAdded(const GURL& url) { |
| 76 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 77 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 78 worker_pool_token_, |
| 79 FROM_HERE, |
| 80 base::Bind(&DoAddPage, |
| 81 base::Unretained(delta_file_backend_.get()), |
| 82 url), |
| 83 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 84 } |
| 85 |
| 86 void DeltaFileService::PageDeleted(const GURL& url) { |
| 87 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 88 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 89 worker_pool_token_, |
| 90 FROM_HERE, |
| 91 base::Bind(&DoDeletePage, |
| 92 base::Unretained(delta_file_backend_.get()), |
| 93 url), |
| 94 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 95 } |
| 96 |
| 97 int64 DeltaFileService::Trim(int64 lower_bound) { |
| 98 int64 result; |
| 99 base::WaitableEvent finished(false, false); |
| 100 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 101 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 102 worker_pool_token_, |
| 103 FROM_HERE, |
| 104 base::Bind(&DoTrim, |
| 105 base::Unretained(delta_file_backend_.get()), |
| 106 lower_bound, |
| 107 base::Unretained(&finished), |
| 108 base::Unretained(&result)), |
| 109 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 110 finished.Wait(); |
| 111 return result; |
| 112 } |
| 113 |
| 114 scoped_ptr<std::vector<DeltaFileEntryWithData> > DeltaFileService::Query( |
| 115 int64 last_seq_no, |
| 116 int32 limit) { |
| 117 scoped_ptr<std::vector<DeltaFileEntryWithData> > result; |
| 118 base::WaitableEvent finished(false, false); |
| 119 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 120 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 121 worker_pool_token_, |
| 122 FROM_HERE, |
| 123 base::Bind(&DoQuery, |
| 124 base::Unretained(delta_file_backend_.get()), |
| 125 last_seq_no, |
| 126 limit, |
| 127 base::Unretained(&finished), |
| 128 base::Unretained(&result)), |
| 129 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 130 finished.Wait(); |
| 131 return result.Pass(); |
| 132 } |
| 133 |
| 134 bool DeltaFileService::Recreate(const std::vector<std::string>& urls) { |
| 135 bool result = false; |
| 136 base::WaitableEvent finished(false, false); |
| 137 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 138 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 139 worker_pool_token_, |
| 140 FROM_HERE, |
| 141 base::Bind(&DoRecreate, |
| 142 base::Unretained(delta_file_backend_.get()), |
| 143 urls, |
| 144 base::Unretained(&finished), |
| 145 base::Unretained(&result)), |
| 146 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 147 finished.Wait(); |
| 148 return result; |
| 149 } |
| 150 |
| 151 void DeltaFileService::Clear() { |
| 152 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 153 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 154 worker_pool_token_, |
| 155 FROM_HERE, |
| 156 base::Bind(&DoClear, |
| 157 base::Unretained(delta_file_backend_.get())), |
| 158 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 159 } |
| 160 |
| 161 std::string DeltaFileService::Dump() { |
| 162 std::string dump; |
| 163 base::WaitableEvent finished(false, false); |
| 164 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 165 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 166 worker_pool_token_, |
| 167 FROM_HERE, |
| 168 base::Bind(&DoDump, |
| 169 base::Unretained(delta_file_backend_.get()), |
| 170 base::Unretained(&finished), |
| 171 base::Unretained(&dump)), |
| 172 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 173 finished.Wait(); |
| 174 return dump; |
| 175 } |
| 176 |
| 177 } // namespace history_report |
OLD | NEW |