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 | |
| 7 | #include "base/feature_list.h" |
Sophie Chang | f47da29 | 2023-09-20 18:30:20 | [diff] [blame] | 8 | #include "base/metrics/field_trial_params.h" |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 9 | #include "base/metrics/histogram_functions.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 10 | #include "components/page_image_service/features.h" |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 11 | #include "components/page_image_service/metrics_util.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 12 | #include "components/sync/service/sync_service.h" |
Sophie Chang | 6045e22 | 2024-02-29 21:02:09 | [diff] [blame^] | 13 | #include "components/sync/service/sync_service_utils.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 14 | #include "components/unified_consent/consent_throttle.h" |
| 15 | #include "components/unified_consent/url_keyed_data_collection_consent_helper.h" |
| 16 | |
| 17 | namespace page_image_service { |
| 18 | |
| 19 | namespace { |
| 20 | |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 21 | void RunConsentThrottleCallback( |
| 22 | base::OnceCallback<void(PageImageServiceConsentStatus)> callback, |
| 23 | bool success) { |
| 24 | std::move(callback).Run(success ? PageImageServiceConsentStatus::kSuccess |
| 25 | : PageImageServiceConsentStatus::kFailure); |
| 26 | } |
| 27 | |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 28 | PageImageServiceConsentStatus ConsentStatusToUmaStatus( |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 29 | std::optional<bool> consent_status) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 30 | if (!consent_status) { |
| 31 | return PageImageServiceConsentStatus::kTimedOut; |
| 32 | } |
| 33 | return consent_status.value() ? PageImageServiceConsentStatus::kSuccess |
| 34 | : PageImageServiceConsentStatus::kFailure; |
| 35 | } |
| 36 | |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | ImageServiceConsentHelper::ImageServiceConsentHelper( |
| 40 | syncer::SyncService* sync_service, |
| 41 | syncer::ModelType model_type) |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 42 | : sync_service_(sync_service), |
| 43 | model_type_(model_type), |
| 44 | timeout_duration_(base::Seconds(GetFieldTrialParamByFeatureAsInt( |
| 45 | kImageServiceObserveSyncDownloadStatus, |
| 46 | "timeout_seconds", |
| 47 | 10))) { |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 48 | if (base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)) { |
| 49 | sync_service_observer_.Observe(sync_service); |
| 50 | } else if (model_type == syncer::ModelType::BOOKMARKS) { |
Mikel Astiz | daff318 | 2024-02-08 10:49:29 | [diff] [blame] | 51 | // TODO(crbug.com/40067770): Migrate to require_sync_feature_enabled = |
| 52 | // false. |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 53 | consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( |
| 54 | unified_consent::UrlKeyedDataCollectionConsentHelper:: |
Boris Sazonov | 679ebbd | 2023-09-14 14:38:33 | [diff] [blame] | 55 | NewPersonalizedBookmarksDataCollectionConsentHelper( |
| 56 | sync_service, |
| 57 | /*require_sync_feature_enabled=*/true), |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 58 | timeout_duration_); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 59 | } else if (model_type == syncer::ModelType::HISTORY_DELETE_DIRECTIVES) { |
| 60 | consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( |
| 61 | unified_consent::UrlKeyedDataCollectionConsentHelper:: |
| 62 | NewPersonalizedDataCollectionConsentHelper(sync_service), |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 63 | timeout_duration_); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 64 | } else { |
| 65 | NOTREACHED(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ImageServiceConsentHelper::~ImageServiceConsentHelper() = default; |
| 70 | |
| 71 | void ImageServiceConsentHelper::EnqueueRequest( |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 72 | base::OnceCallback<void(PageImageServiceConsentStatus)> callback, |
| 73 | mojom::ClientId client_id) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 74 | base::UmaHistogramBoolean("PageImageService.ConsentStatusRequestCount", true); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 75 | if (consent_throttle_) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 76 | consent_throttle_->EnqueueRequest( |
| 77 | base::BindOnce(&RunConsentThrottleCallback, std::move(callback))); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 81 | std::optional<bool> consent_status = GetConsentStatus(); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 82 | if (consent_status.has_value()) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 83 | std::move(callback).Run(*consent_status |
| 84 | ? PageImageServiceConsentStatus::kSuccess |
| 85 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 89 | enqueued_request_callbacks_.emplace_back(std::move(callback), client_id); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 90 | if (!request_processing_timer_.IsRunning()) { |
| 91 | request_processing_timer_.Start( |
Sophie Chang | dce8d941 | 2023-09-29 05:33:37 | [diff] [blame] | 92 | FROM_HERE, timeout_duration_, |
Sophie Chang | 9fabf70 | 2023-08-11 22:19:01 | [diff] [blame] | 93 | base::BindOnce(&ImageServiceConsentHelper::OnTimeoutExpired, |
| 94 | weak_ptr_factory_.GetWeakPtr())); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | void ImageServiceConsentHelper::OnStateChanged( |
| 99 | syncer::SyncService* sync_service) { |
| 100 | CHECK_EQ(sync_service_, sync_service); |
| 101 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 102 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 103 | std::optional<bool> consent_status = GetConsentStatus(); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 104 | if (!consent_status.has_value()) { |
| 105 | return; |
| 106 | } |
| 107 | |
Sophie Chang | eee5afeff | 2023-12-18 20:23:04 | [diff] [blame] | 108 | request_processing_timer_.Stop(); |
| 109 | |
| 110 | // The request callbacks can modify the vector while running. Swap the vector |
| 111 | // onto the stack to prevent crashing. https://crbug.com/1472360. |
| 112 | std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>, |
| 113 | mojom::ClientId>> |
| 114 | callbacks; |
| 115 | std::swap(callbacks, enqueued_request_callbacks_); |
| 116 | for (auto& request_callback_with_client_id : callbacks) { |
Rushan Suleymanov | e978a87d | 2023-10-12 16:39:09 | [diff] [blame] | 117 | std::move(request_callback_with_client_id.first) |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame] | 118 | .Run(*consent_status ? PageImageServiceConsentStatus::kSuccess |
| 119 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 120 | } |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void ImageServiceConsentHelper::OnSyncShutdown( |
| 124 | syncer::SyncService* sync_service) { |
| 125 | CHECK_EQ(sync_service_, sync_service); |
| 126 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 127 | |
| 128 | sync_service_observer_.Reset(); |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 129 | sync_service_ = nullptr; |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 130 | } |
| 131 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 132 | std::optional<bool> ImageServiceConsentHelper::GetConsentStatus() { |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 133 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 134 | |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 135 | if (!sync_service_) { |
| 136 | return false; |
| 137 | } |
| 138 | |
Sophie Chang | 6045e22 | 2024-02-29 21:02:09 | [diff] [blame^] | 139 | // If upload of the given ModelType is disabled (or inactive due to an |
| 140 | // error), then consent must be assumed to be NOT given. |
| 141 | // Note that the "INITIALIZING" state is good enough: It means the data |
| 142 | // type is enabled in principle, Sync just hasn't fully finished |
| 143 | // initializing yet. This case is handled by the DownloadStatus check |
| 144 | // below. |
| 145 | if (syncer::GetUploadToGoogleState(sync_service_, model_type_) == |
| 146 | syncer::UploadState::NOT_ACTIVE) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | // Ensure Sync has downloaded all relevant updates (i.e. any deletions from |
| 151 | // other devices are known). |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 152 | syncer::SyncService::ModelTypeDownloadStatus download_status = |
| 153 | sync_service_->GetDownloadStatusFor(model_type_); |
| 154 | switch (download_status) { |
| 155 | case syncer::SyncService::ModelTypeDownloadStatus::kWaitingForUpdates: |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 156 | return std::nullopt; |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 157 | case syncer::SyncService::ModelTypeDownloadStatus::kUpToDate: |
| 158 | return true; |
| 159 | case syncer::SyncService::ModelTypeDownloadStatus::kError: |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void ImageServiceConsentHelper::OnTimeoutExpired() { |
Sophie Chang | eee5afeff | 2023-12-18 20:23:04 | [diff] [blame] | 165 | // The request callbacks can modify the vector while running. Swap the vector |
| 166 | // onto the stack to prevent crashing. https://crbug.com/1472360. |
| 167 | std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>, |
| 168 | mojom::ClientId>> |
| 169 | callbacks; |
| 170 | std::swap(callbacks, enqueued_request_callbacks_); |
| 171 | for (auto& request_callback_with_client_id : callbacks) { |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 172 | // Report consent status on timeout for each request to compare against the |
| 173 | // number of all requests. |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 174 | PageImageServiceConsentStatus consent_status = |
| 175 | ConsentStatusToUmaStatus(GetConsentStatus()); |
Rushan Suleymanov | b11a297 | 2023-09-29 17:45:35 | [diff] [blame] | 176 | base::UmaHistogramEnumeration("PageImageService.ConsentStatusOnTimeout", |
Sophie Chang | fba27912 | 2023-12-12 21:22:21 | [diff] [blame] | 177 | consent_status); |
| 178 | if (sync_service_) { |
| 179 | sync_service_->RecordReasonIfWaitingForUpdates( |
| 180 | model_type_, kConsentTimeoutReasonHistogramName); |
| 181 | sync_service_->RecordReasonIfWaitingForUpdates( |
| 182 | model_type_, |
| 183 | std::string(kConsentTimeoutReasonHistogramName) + "." + |
| 184 | ClientIdToString(request_callback_with_client_id.second)); |
| 185 | } |
| 186 | std::move(request_callback_with_client_id.first).Run(consent_status); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 187 | } |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace page_image_service |