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/usage_reports_buffer_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/usage_reports_buffer_backend.h" |
| 11 #include "chrome/browser/android/proto/delta_file.pb.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 |
| 14 |
| 15 namespace { |
| 16 |
| 17 void DoInit(history_report::UsageReportsBufferBackend* backend) { |
| 18 backend->Init(); |
| 19 } |
| 20 |
| 21 void DoAddVisit(history_report::UsageReportsBufferBackend* backend, |
| 22 const std::string id, |
| 23 int64 timestamp_ms, |
| 24 bool typed_visit) { |
| 25 backend->AddVisit(id, timestamp_ms, typed_visit); |
| 26 } |
| 27 |
| 28 void DoRemove(history_report::UsageReportsBufferBackend* backend, |
| 29 const std::vector<std::string>* reports, |
| 30 base::WaitableEvent* finished) { |
| 31 backend->Remove(*reports); |
| 32 finished->Signal(); |
| 33 } |
| 34 |
| 35 void DoGetUsageReportsBatch( |
| 36 history_report::UsageReportsBufferBackend* backend, |
| 37 int32 batch_size, |
| 38 base::WaitableEvent* finished, |
| 39 scoped_ptr<std::vector<history_report::UsageReport> >* result) { |
| 40 *result = backend->GetUsageReportsBatch(batch_size).Pass(); |
| 41 finished->Signal(); |
| 42 } |
| 43 |
| 44 void DoClear( |
| 45 history_report::UsageReportsBufferBackend* backend, |
| 46 base::WaitableEvent* finished) { |
| 47 backend->Clear(); |
| 48 finished->Signal(); |
| 49 } |
| 50 |
| 51 void DoDump( |
| 52 history_report::UsageReportsBufferBackend* backend, |
| 53 base::WaitableEvent* finished, |
| 54 std::string* result) { |
| 55 result->append(backend->Dump()); |
| 56 finished->Signal(); |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 namespace history_report { |
| 62 |
| 63 using content::BrowserThread; |
| 64 |
| 65 UsageReportsBufferService::UsageReportsBufferService(const base::FilePath& dir) |
| 66 : worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), |
| 67 backend_(new UsageReportsBufferBackend(dir)) { |
| 68 } |
| 69 |
| 70 UsageReportsBufferService::~UsageReportsBufferService() {} |
| 71 |
| 72 void UsageReportsBufferService::Init() { |
| 73 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 74 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 75 worker_pool_token_, |
| 76 FROM_HERE, |
| 77 base::Bind(&DoInit, base::Unretained(backend_.get())), |
| 78 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 79 } |
| 80 |
| 81 void UsageReportsBufferService::AddVisit(const std::string& id, |
| 82 int64 timestamp_ms, |
| 83 bool typed_visit) { |
| 84 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 85 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 86 worker_pool_token_, |
| 87 FROM_HERE, |
| 88 base::Bind(&DoAddVisit, |
| 89 base::Unretained(backend_.get()), |
| 90 id, |
| 91 timestamp_ms, |
| 92 typed_visit), |
| 93 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 94 } |
| 95 |
| 96 scoped_ptr<std::vector<UsageReport> > |
| 97 UsageReportsBufferService::GetUsageReportsBatch(int32 batch_size) { |
| 98 scoped_ptr<std::vector<UsageReport> > result; |
| 99 base::WaitableEvent finished(false, false); |
| 100 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 101 // It's ok to pass unretained pointers here because this is a synchronous |
| 102 // call. |
| 103 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 104 worker_pool_token_, |
| 105 FROM_HERE, |
| 106 base::Bind(&DoGetUsageReportsBatch, |
| 107 base::Unretained(backend_.get()), |
| 108 batch_size, |
| 109 base::Unretained(&finished), |
| 110 base::Unretained(&result)), |
| 111 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
| 112 finished.Wait(); |
| 113 return result.Pass(); |
| 114 } |
| 115 |
| 116 void UsageReportsBufferService::Remove( |
| 117 const std::vector<std::string>& report_ids) { |
| 118 base::WaitableEvent finished(false, false); |
| 119 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 120 // It's ok to pass unretained pointers here because this is a synchronous |
| 121 // call. |
| 122 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 123 worker_pool_token_, |
| 124 FROM_HERE, |
| 125 base::Bind(&DoRemove, |
| 126 base::Unretained(backend_.get()), |
| 127 base::Unretained(&report_ids), |
| 128 base::Unretained(&finished)), |
| 129 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 130 finished.Wait(); |
| 131 } |
| 132 |
| 133 void UsageReportsBufferService::Clear() { |
| 134 base::WaitableEvent finished(false, false); |
| 135 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 136 // It's ok to pass unretained pointers here because this is a synchronous |
| 137 // call. |
| 138 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 139 worker_pool_token_, |
| 140 FROM_HERE, |
| 141 base::Bind(&DoClear, |
| 142 base::Unretained(backend_.get()), |
| 143 base::Unretained(&finished)), |
| 144 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 145 finished.Wait(); |
| 146 } |
| 147 |
| 148 std::string UsageReportsBufferService::Dump() { |
| 149 std::string dump; |
| 150 base::WaitableEvent finished(false, false); |
| 151 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 152 // It's ok to pass unretained pointers here because this is a synchronous |
| 153 // call. |
| 154 pool->PostSequencedWorkerTaskWithShutdownBehavior( |
| 155 worker_pool_token_, |
| 156 FROM_HERE, |
| 157 base::Bind(&DoDump, |
| 158 base::Unretained(backend_.get()), |
| 159 base::Unretained(&finished), |
| 160 base::Unretained(&dump)), |
| 161 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 162 finished.Wait(); |
| 163 return dump; |
| 164 } |
| 165 |
| 166 } // namespace history_report |
OLD | NEW |