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