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" |
| 8 | #include "components/page_image_service/features.h" |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 9 | #include "components/page_image_service/metrics_util.h" |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 10 | #include "components/sync/service/sync_service.h" |
| 11 | #include "components/unified_consent/consent_throttle.h" |
| 12 | #include "components/unified_consent/url_keyed_data_collection_consent_helper.h" |
| 13 | |
| 14 | namespace page_image_service { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | constexpr base::TimeDelta kTimeout = base::Seconds(10); |
| 19 | |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 20 | void RunConsentThrottleCallback( |
| 21 | base::OnceCallback<void(PageImageServiceConsentStatus)> callback, |
| 22 | bool success) { |
| 23 | std::move(callback).Run(success ? PageImageServiceConsentStatus::kSuccess |
| 24 | : PageImageServiceConsentStatus::kFailure); |
| 25 | } |
| 26 | |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 27 | } // namespace |
| 28 | |
| 29 | ImageServiceConsentHelper::ImageServiceConsentHelper( |
| 30 | syncer::SyncService* sync_service, |
| 31 | syncer::ModelType model_type) |
| 32 | : sync_service_(sync_service), model_type_(model_type) { |
| 33 | if (base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)) { |
| 34 | sync_service_observer_.Observe(sync_service); |
| 35 | } else if (model_type == syncer::ModelType::BOOKMARKS) { |
| 36 | consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( |
| 37 | unified_consent::UrlKeyedDataCollectionConsentHelper:: |
| 38 | NewPersonalizedBookmarksDataCollectionConsentHelper(sync_service), |
| 39 | kTimeout); |
| 40 | } else if (model_type == syncer::ModelType::HISTORY_DELETE_DIRECTIVES) { |
| 41 | consent_throttle_ = std::make_unique<unified_consent::ConsentThrottle>( |
| 42 | unified_consent::UrlKeyedDataCollectionConsentHelper:: |
| 43 | NewPersonalizedDataCollectionConsentHelper(sync_service), |
| 44 | kTimeout); |
| 45 | } else { |
| 46 | NOTREACHED(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | ImageServiceConsentHelper::~ImageServiceConsentHelper() = default; |
| 51 | |
| 52 | void ImageServiceConsentHelper::EnqueueRequest( |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 53 | base::OnceCallback<void(PageImageServiceConsentStatus)> callback) { |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 54 | if (consent_throttle_) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 55 | consent_throttle_->EnqueueRequest( |
| 56 | base::BindOnce(&RunConsentThrottleCallback, std::move(callback))); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | absl::optional<bool> consent_status = GetConsentStatus(); |
| 61 | if (consent_status.has_value()) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 62 | std::move(callback).Run(*consent_status |
| 63 | ? PageImageServiceConsentStatus::kSuccess |
| 64 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
| 68 | enqueued_request_callbacks_.emplace_back(std::move(callback)); |
| 69 | if (!request_processing_timer_.IsRunning()) { |
| 70 | request_processing_timer_.Start( |
| 71 | FROM_HERE, kTimeout, |
Sophie Chang | 9fabf70 | 2023-08-11 22:19:01 | [diff] [blame] | 72 | base::BindOnce(&ImageServiceConsentHelper::OnTimeoutExpired, |
| 73 | weak_ptr_factory_.GetWeakPtr())); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | void ImageServiceConsentHelper::OnStateChanged( |
| 78 | syncer::SyncService* sync_service) { |
| 79 | CHECK_EQ(sync_service_, sync_service); |
| 80 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 81 | |
| 82 | absl::optional<bool> consent_status = GetConsentStatus(); |
| 83 | if (!consent_status.has_value()) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | for (auto& request_callback : enqueued_request_callbacks_) { |
Sophie Chang | aae45dfc | 2023-09-11 18:42:21 | [diff] [blame^] | 88 | std::move(request_callback) |
| 89 | .Run(*consent_status ? PageImageServiceConsentStatus::kSuccess |
| 90 | : PageImageServiceConsentStatus::kFailure); |
Sophie Chang | 647ee3a | 2023-06-06 15:43:43 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | enqueued_request_callbacks_.clear(); |
| 94 | request_processing_timer_.Stop(); |
| 95 | } |
| 96 | |
| 97 | void ImageServiceConsentHelper::OnSyncShutdown( |
| 98 | syncer::SyncService* sync_service) { |
| 99 | CHECK_EQ(sync_service_, sync_service); |
| 100 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 101 | |
| 102 | sync_service_observer_.Reset(); |
| 103 | } |
| 104 | |
| 105 | absl::optional<bool> ImageServiceConsentHelper::GetConsentStatus() { |
| 106 | CHECK(base::FeatureList::IsEnabled(kImageServiceObserveSyncDownloadStatus)); |
| 107 | |
|
|