Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium Authors |
Leonid Baraz | 61437cb | 2021-02-26 20:43:06 | [diff] [blame] | 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( |
|
|