Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 1 | // Copyright 2020 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 | |
Leonid Baraz | f10cae8 | 2021-09-14 00:59:38 | [diff] [blame] | 5 | #include "components/reporting/client/report_queue_impl.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
Leonid Baraz | f84aa6c | 2021-12-13 19:38:20 | [diff] [blame] | 8 | #include <queue> |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
| 11 | |
| 12 | #include "base/bind.h" |
| 13 | #include "base/callback.h" |
Ahmed Nasr | f21e843 | 2022-07-30 00:48:37 | [diff] [blame] | 14 | #include "base/check.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 16 | #include "base/memory/scoped_refptr.h" |
Leonid Baraz | b8c27535 | 2021-08-05 00:59:09 | [diff] [blame] | 17 | #include "base/notreached.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 18 | #include "base/sequence_checker.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 19 | #include "base/task/bind_post_task.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 20 | #include "base/task/task_traits.h" |
| 21 | #include "base/task/thread_pool.h" |
| 22 | #include "base/time/time.h" |
Leonid Baraz | caac7c9 | 2021-03-04 17:34:05 | [diff] [blame] | 23 | #include "components/reporting/client/report_queue_configuration.h" |
Josh Hilke | e7a4699 | 2021-10-21 20:21:19 | [diff] [blame] | 24 | #include "components/reporting/proto/synced/record.pb.h" |
| 25 | #include "components/reporting/proto/synced/record_constants.pb.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 26 | #include "components/reporting/storage/storage_module_interface.h" |
| 27 | #include "components/reporting/util/status.h" |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 28 | #include "components/reporting/util/statusor.h" |
| 29 | |
| 30 | namespace reporting { |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 31 | namespace { |
| 32 | // Calls |record_producer|, checks the result and in case of success, forwards |
| 33 | // it to the storage. In production code should be invoked asynchronously, on a |
| 34 | // thread pool (no synchronization expected). |
| 35 | void AddRecordToStorage(scoped_refptr<StorageModuleInterface> storage, |
| 36 | Priority priority, |
| 37 | std::string dm_token, |
| 38 | Destination destination, |
| 39 | ReportQueue::RecordProducer record_producer, |
| 40 | StorageModuleInterface::EnqueueCallback callback) { |
Leonid Baraz | 8e2830e | 2022-06-02 20:26:50 | [diff] [blame] | 41 | // Generate record data. |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 42 | auto record_result = std::move(record_producer).Run(); |
| 43 | if (!record_result.ok()) { |
| 44 | std::move(callback).Run(record_result.status()); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | // Augment data. |
| 49 | Record record; |
| 50 | *record.mutable_data() = std::move(record_result.ValueOrDie()); |
| 51 | record.set_destination(destination); |
| 52 | |
Leonid Baraz | 8e2830e | 2022-06-02 20:26:50 | [diff] [blame] | 53 | // |record| with no DM token is assumed to be associated with device DM token |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 54 | if (!dm_token.empty()) { |
| 55 | *record.mutable_dm_token() = std::move(dm_token); |
| 56 | } |
| 57 | |
| 58 | // Calculate timestamp in microseconds - to match Spanner expectations. |
| 59 | const int64_t time_since_epoch_us = |
| 60 | base::Time::Now().ToJavaTime() * base::Time::kMicrosecondsPerMillisecond; |
| 61 | record.set_timestamp_us(time_since_epoch_us); |
| 62 | if (!record_result.ok()) { |
| 63 | std::move(callback).Run(record_result.status()); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // Add resulting Record to the storage. |
| 68 | storage->AddRecord(priority, std::move(record), std::move(callback)); |
| 69 | } |
| 70 | } // namespace |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 71 | |
Leonid Baraz | 4d49766a | 2021-10-16 23:50:48 | [diff] [blame] | 72 | void ReportQueueImpl::Create( |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 73 | std::unique_ptr<ReportQueueConfiguration> config, |
Leonid Baraz | 4d49766a | 2021-10-16 23:50:48 | [diff] [blame] | 74 | scoped_refptr<StorageModuleInterface> storage, |
| 75 | base::OnceCallback<void(StatusOr<std::unique_ptr<ReportQueue>>)> cb) { |
| 76 | std::move(cb).Run(base::WrapUnique<ReportQueueImpl>( |
| 77 | new ReportQueueImpl(std::move(config), storage))); |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 78 | } |
| 79 | |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 80 | ReportQueueImpl::ReportQueueImpl( |
| 81 | std::unique_ptr<ReportQueueConfiguration> config, |
| 82 | scoped_refptr<StorageModuleInterface> storage) |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 83 | : config_(std::move(config)), storage_(storage) {} |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 84 | |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 85 | ReportQueueImpl::~ReportQueueImpl() = default; |
| 86 | |
| 87 | void ReportQueueImpl::AddProducedRecord(RecordProducer record_producer, |
| 88 | Priority priority, |
| 89 | EnqueueCallback callback) const { |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 90 | const Status status = config_->CheckPolicy(); |
| 91 | if (!status.ok()) { |
| 92 | std::move(callback).Run(status); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if (priority == Priority::UNDEFINED_PRIORITY) { |
| 97 | std::move(callback).Run( |
| 98 | Status(error::INVALID_ARGUMENT, "Priority must be defined")); |
| 99 | return; |
| 100 | } |
| 101 | |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [blame] | 102 | // Execute |record_producer| on arbitrary thread, analyze the result and send |
| 103 | // it to the Storage, returning with the callback. |
| 104 | base::ThreadPool::PostTask( |
| 105 | FROM_HERE, {base::TaskPriority::BEST_EFFORT}, |
| 106 | base::BindOnce(&AddRecordToStorage, storage_, priority, |
| 107 | config_->dm_token(), config_->destination(), |
| 108 | std::move(record_producer), std::move(callback))); |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 109 | } |
| 110 | |
Leonid Baraz | 4542951 | 2021-03-12 18:20:12 | [diff] [blame] | 111 | void ReportQueueImpl::Flush(Priority priority, FlushCallback callback) { |
| 112 | storage_->Flush(priority, std::move(callback)); |
| 113 | } |
| 114 | |
Leonid Baraz | b8c27535 | 2021-08-05 00:59:09 | [diff] [blame] | 115 | base::OnceCallback<void(StatusOr<std::unique_ptr<ReportQueue>>)> |
| 116 | ReportQueueImpl::PrepareToAttachActualQueue() const { |
| 117 | NOTREACHED(); |
| 118 | return base::BindOnce( |
| 119 | [](StatusOr<std::unique_ptr<ReportQueue>>) { NOTREACHED(); }); |
| 120 | } |
| 121 | |
Leonid Baraz | 8e2830e | 2022-06-02 20:26:50 | [diff] [blame] | 122 | // Implementation of SpeculativeReportQueueImpl::PendingRecordProducer |
| 123 | |
| 124 | SpeculativeReportQueueImpl::PendingRecordProducer::PendingRecordProducer( |
| 125 | RecordProducer producer, |
| 126 | Priority priority) |
| 127 | : record_producer(std::move(producer)), record_priority(priority) {} |
| 128 | |
| 129 | SpeculativeReportQueueImpl::PendingRecordProducer::PendingRecordProducer( |
| 130 | PendingRecordProducer&& other) |
| 131 | : record_producer(std::move(other.record_producer)), |
| 132 | record_priority(other.record_priority) {} |
| 133 | |
| 134 | SpeculativeReportQueueImpl::PendingRecordProducer::~PendingRecordProducer() = |
| 135 | default; |
| 136 | |
| 137 | SpeculativeReportQueueImpl::PendingRecordProducer& |
| 138 | SpeculativeReportQueueImpl::PendingRecordProducer::operator=( |
| 139 | PendingRecordProducer&& other) { |
| 140 | record_producer = std::move(other.record_producer); |
| 141 | record_priority = other.record_priority; |
| 142 | return *this; |
| 143 | } |
| 144 | |
Leonid Baraz | b8c27535 | 2021-08-05 00:59:09 | [diff] [blame] | 145 | // static |
| 146 | std::unique_ptr<SpeculativeReportQueueImpl, base::OnTaskRunnerDeleter> |
| 147 | SpeculativeReportQueueImpl::Create() { |
| 148 | scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner = |
| 149 | base::ThreadPool::CreateSequencedTaskRunner( |
| 150 | {base::TaskPriority::BEST_EFFORT, base::MayBlock()}); |
| 151 | return std::unique_ptr<SpeculativeReportQueueImpl, base::OnTaskRunnerDeleter>( |
| 152 | new SpeculativeReportQueueImpl(sequenced_task_runner), |
| 153 | base::OnTaskRunnerDeleter(sequenced_task_runner)); |
| 154 | } |
| 155 | |
| 156 | SpeculativeReportQueueImpl::SpeculativeReportQueueImpl( |
| 157 | scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner) |
| 158 | : sequenced_task_runner_(sequenced_task_runner) { |
| 159 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 160 | } |
| 161 | |
| 162 | SpeculativeReportQueueImpl::~SpeculativeReportQueueImpl() { |
| 163 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 164 | } |
| 165 | |
| 166 | void SpeculativeReportQueueImpl::Flush(Priority priority, |
| 167 | FlushCallback callback) { |
| 168 | sequenced_task_runner_->PostTask( |
| 169 | FROM_HERE, |
| 170 | base::BindOnce( |
| 171 | [](Priority priority, FlushCallback callback, |
| 172 | base::WeakPtr<SpeculativeReportQueueImpl> self) { |
| 173 | if (!self) { |
| 174 | std::move(callback).Run( |
| 175 | Status(error::UNAVAILABLE, "Queue has been destructed")); |
| 176 | return; |
| 177 | } |
| 178 | DCHECK_CALLED_ON_VALID_SEQUENCE(self->sequence_checker_); |
Ahmed Nasr | f21e843 | 2022-07-30 00:48:37 | [diff] [blame] | 179 | if (!self->status_or_report_queue_.has_value()) { |
Leonid Baraz | b8c27535 | 2021-08-05 00:59:09 | [diff] [blame] | 180 | std::move(callback).Run(Status(error::FAILED_PRECONDITION, |
| 181 | "ReportQueue is not ready yet.")); |
| 182 | return; |
| 183 | } |
Ahmed Nasr | f21e843 | 2022-07-30 00:48:37 | [diff] [blame] | 184 | if (!self->status_or_report_queue_->ok()) { |
| 185 | std::move(callback).Run(self->status_or_report_queue_->status()); |
| 186 | return; |
| 187 | } |
| 188 | const std::unique_ptr<ReportQueue>& report_queue = |
| 189 | self->status_or_report_queue_->ValueOrDie(); |
| 190 | report_queue->Flush(priority, std::move(callback)); |
Leonid Baraz | b8c27535 | 2021-08-05 00:59:09 | [diff] [blame] | 191 | }, |
| 192 | priority, std::move(callback), weak_ptr_factory_.GetWeakPtr())); |
| 193 | } |
| 194 | |
Leonid Baraz | f8a9daf | 2022-06-02 01:09:35 | [diff] [
|