Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 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 "components/page_image_service/image_service_consent_helper.h" |
| 6 | |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 7 | #include "base/metrics/histogram_functions.h" |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 8 | #include "components/page_image_service/metrics_util.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 9 | #include "components/sync/service/sync_service.h" |
Sophie Chang | 6045e22 | 2024-02-29 21:02:09 | [diff] [blame] | 10 | #include "components/sync/service/sync_service_utils.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 11 | |
| 12 | namespace page_image_service { |
| 13 | |
| 14 | namespace { |
| 15 | |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 16 | PageImageServiceConsentStatus ConsentStatusToUmaStatus( |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 17 | std::optional<bool> consent_status) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 18 | if (!consent_status) { |
| 19 | return PageImageServiceConsentStatus::kTimedOut; |
| 20 | } |
| 21 | return consent_status.value() ? PageImageServiceConsentStatus::kSuccess |
| 22 | : PageImageServiceConsentStatus::kFailure; |
| 23 | } |
| 24 | |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 25 | } // namespace |
| 26 | |
| 27 | ImageServiceConsentHelper::ImageServiceConsentHelper( |
| 28 | syncer::SyncService* sync_service, |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 29 | syncer::DataType data_type) |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 30 | : sync_service_(sync_service), |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 31 | data_type_(data_type), |
Marc Treib | 0872cf7 | 2024-06-26 19:39:30 | [diff] [blame] | 32 | timeout_duration_(base::Seconds(10)) { |
Ankush Singh | 53678e3 | 2024-07-09 17:36:20 | [diff] [blame] | 33 | // `sync_service` can be null, for example when disabled via flags. |
| 34 | if (sync_service) { |
| 35 | sync_service_observer_.Observe(sync_service); |
| 36 | } |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ImageServiceConsentHelper::~ImageServiceConsentHelper() = default; |
| 40 | |
| 41 | void ImageServiceConsentHelper::EnqueueRequest( |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 42 | base::OnceCallback<void(PageImageServiceConsentStatus)> callback, |
| 43 | mojom::ClientId client_id) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 44 | base::UmaHistogramBoolean("PageImageService.ConsentStatusRequestCount", true); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 45 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 46 | std::optional<bool> consent_status = GetConsentStatus(); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 47 | if (consent_status.has_value()) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 48 | std::move(callback).Run(*consent_status |
| 49 | ? PageImageServiceConsentStatus::kSuccess |
| 50 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 54 | enqueued_request_callbacks_.emplace_back(std::move(callback), client_id); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 55 | if (!request_processing_timer_.IsRunning()) { |
| 56 | request_processing_timer_.Start( |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 57 | FROM_HERE, timeout_duration_, |
Sophie Chang | 9fabf70 | 2023-08-11 22:19:01 | [diff] [blame] | 58 | base::BindOnce(&ImageServiceConsentHelper::OnTimeoutExpired, |
| 59 | weak_ptr_factory_.GetWeakPtr())); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | void ImageServiceConsentHelper::OnStateChanged( |
| 64 | syncer::SyncService* sync_service) { |
| 65 | CHECK_EQ(sync_service_, sync_service); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 66 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 67 | std::optional<bool> consent_status = GetConsentStatus(); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 68 | if (!consent_status.has_value()) { |
| 69 | return; |
| 70 | } |
| 71 | |
Sophie Chang | eee5afeff | 2023-12-18 20:23:04 | [diff] [blame] | 72 | request_processing_timer_.Stop(); |
| 73 | |
| 74 | // The request callbacks can modify the vector while running. Swap the vector |
| 75 | // onto the stack to prevent crashing. https://crbug.com/1472360. |
| 76 | std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>, |
| 77 | mojom::ClientId>> |
| 78 | callbacks; |
| 79 | std::swap(callbacks, enqueued_request_callbacks_); |
| 80 | for (auto& request_callback_with_client_id : callbacks) { |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 81 | std::move(request_callback_with_client_id.first) |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 82 | .Run(*consent_status ? PageImageServiceConsentStatus::kSuccess |
| 83 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 84 | } |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void ImageServiceConsentHelper::OnSyncShutdown( |
| 88 | syncer::SyncService* sync_service) { |
| 89 | CHECK_EQ(sync_service_, sync_service); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 90 | |
| 91 | sync_service_observer_.Reset(); |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 92 | sync_service_ = nullptr; |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 93 | } |
| 94 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 95 | std::optional<bool> ImageServiceConsentHelper::GetConsentStatus() { |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 96 | if (!sync_service_) { |
| 97 | return false; |
| 98 | } |
| 99 | |
Victor Hugo Vianna Silva | 2bdd6261 | 2024-08-08 09:46:45 | [diff] [blame] | 100 | // If upload of the given DataType is disabled (or inactive due to an |
Sophie Chang | 6045e22 | 2024-02-29 21:02:09 | [diff] [blame] | 101 | // error), then consent must be assumed to be NOT given. |
| 102 | // Note that the "INITIALIZING" state is good enough: It means the data |
| 103 | // type is enabled in principle, Sync just hasn't fully finished |
| 104 | // initializing yet. This case is handled by the DownloadStatus check |
| 105 | // below. |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 106 | if (syncer::GetUploadToGoogleState(sync_service_, data_type_) == |
Sophie Chang | 6045e22 | 2024-02-29 21:02:09 | [diff] [blame] | 107 | syncer::UploadState::NOT_ACTIVE) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Ensure Sync has downloaded all relevant updates (i.e. any deletions from |
| 112 | // other devices are known). |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 113 | syncer::SyncService::DataTypeDownloadStatus download_status = |
| 114 | sync_service_->GetDownloadStatusFor(data_type_); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 115 | switch (download_status) { |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 116 | case syncer::SyncService::DataTypeDownloadStatus::kWaitingForUpdates: |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 117 | return std::nullopt; |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 118 | case syncer::SyncService::DataTypeDownloadStatus::kUpToDate: |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 119 | return true; |
Mikel Astiz | 2504913 | 2024-08-07 15:06:59 | [diff] [blame] | 120 | case syncer::SyncService::DataTypeDownloadStatus::kError: |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void ImageServiceConsentHelper::OnTimeoutExpired() { |
Sophie Chang | eee5afeff | 2023-12-18 20:23:04 | [diff] [blame] | 126 | // The request callbacks can modify the vector while running. Swap the vector |
| 127 | // onto the stack to prevent crashing. https://crbug.com/1472360. |
| 128 | std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>, |
| 129 | mojom::ClientId>> |
| 130 | callbacks; |
| 131 | std::swap(callbacks, enqueued_request_callbacks_); |
| 132 | for (auto& request_callback_with_client_id : callbacks) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 133 | // Report consent status on timeout for each request to compare against the |
| 134 | // number of all requests. |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 135 | PageImageServiceConsentStatus consent_status = |
| 136 | ConsentStatusToUmaStatus(GetConsentStatus()); |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 137 | base::UmaHistogramEnumeration("PageImageService.ConsentStatusOnTimeout", |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 138 | consent_status); |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 139 | std::move(request_callback_with_client_id.first).Run(consent_status); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 140 | } |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | } // namespace page_image_service |