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