File: image_service_consent_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,270 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/page_image_service/image_service_consent_helper.h"

#include "base/metrics/histogram_functions.h"
#include "components/page_image_service/metrics_util.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_service_utils.h"

namespace page_image_service {

namespace {

PageImageServiceConsentStatus ConsentStatusToUmaStatus(
    std::optional<bool> consent_status) {
  if (!consent_status) {
    return PageImageServiceConsentStatus::kTimedOut;
  }
  return consent_status.value() ? PageImageServiceConsentStatus::kSuccess
                                : PageImageServiceConsentStatus::kFailure;
}

}  // namespace

ImageServiceConsentHelper::ImageServiceConsentHelper(
    syncer::SyncService* sync_service,
    syncer::DataType data_type)
    : sync_service_(sync_service),
      data_type_(data_type),
      timeout_duration_(base::Seconds(10)) {
  // `sync_service` can be null, for example when disabled via flags.
  if (sync_service) {
    sync_service_observer_.Observe(sync_service);
  }
}

ImageServiceConsentHelper::~ImageServiceConsentHelper() = default;

void ImageServiceConsentHelper::EnqueueRequest(
    base::OnceCallback<void(PageImageServiceConsentStatus)> callback,
    mojom::ClientId client_id) {
  base::UmaHistogramBoolean("PageImageService.ConsentStatusRequestCount", true);

  std::optional<bool> consent_status = GetConsentStatus();
  if (consent_status.has_value()) {
    std::move(callback).Run(*consent_status
                                ? PageImageServiceConsentStatus::kSuccess
                                : PageImageServiceConsentStatus::kFailure);
    return;
  }

  enqueued_request_callbacks_.emplace_back(std::move(callback), client_id);
  if (!request_processing_timer_.IsRunning()) {
    request_processing_timer_.Start(
        FROM_HERE, timeout_duration_,
        base::BindOnce(&ImageServiceConsentHelper::OnTimeoutExpired,
                       weak_ptr_factory_.GetWeakPtr()));
  }
}

void ImageServiceConsentHelper::OnStateChanged(
    syncer::SyncService* sync_service) {
  CHECK_EQ(sync_service_, sync_service);

  std::optional<bool> consent_status = GetConsentStatus();
  if (!consent_status.has_value()) {
    return;
  }

  request_processing_timer_.Stop();

  // The request callbacks can modify the vector while running. Swap the vector
  // onto the stack to prevent crashing. https://crbug.com/1472360.
  std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>,
                        mojom::ClientId>>
      callbacks;
  std::swap(callbacks, enqueued_request_callbacks_);
  for (auto& request_callback_with_client_id : callbacks) {
    std::move(request_callback_with_client_id.first)
        .Run(*consent_status ? PageImageServiceConsentStatus::kSuccess
                             : PageImageServiceConsentStatus::kFailure);
  }
}

void ImageServiceConsentHelper::OnSyncShutdown(
    syncer::SyncService* sync_service) {
  CHECK_EQ(sync_service_, sync_service);

  sync_service_observer_.Reset();
  sync_service_ = nullptr;
}

std::optional<bool> ImageServiceConsentHelper::GetConsentStatus() {
  if (!sync_service_) {
    return false;
  }

  // If upload of the given DataType is disabled (or inactive due to an
  // error), then consent must be assumed to be NOT given.
  // Note that the "INITIALIZING" state is good enough: It means the data
  // type is enabled in principle, Sync just hasn't fully finished
  // initializing yet. This case is handled by the DownloadStatus check
  // below.
  if (syncer::GetUploadToGoogleState(sync_service_, data_type_) ==
      syncer::UploadState::NOT_ACTIVE) {
    return false;
  }

  // Ensure Sync has downloaded all relevant updates (i.e. any deletions from
  // other devices are known).
  syncer::SyncService::DataTypeDownloadStatus download_status =
      sync_service_->GetDownloadStatusFor(data_type_);
  switch (download_status) {
    case syncer::SyncService::DataTypeDownloadStatus::kWaitingForUpdates:
      return std::nullopt;
    case syncer::SyncService::DataTypeDownloadStatus::kUpToDate:
      return true;
    case syncer::SyncService::DataTypeDownloadStatus::kError:
      return false;
  }
}

void ImageServiceConsentHelper::OnTimeoutExpired() {
  // The request callbacks can modify the vector while running. Swap the vector
  // onto the stack to prevent crashing. https://crbug.com/1472360.
  std::vector<std::pair<base::OnceCallback<void(PageImageServiceConsentStatus)>,
                        mojom::ClientId>>
      callbacks;
  std::swap(callbacks, enqueued_request_callbacks_);
  for (auto& request_callback_with_client_id : callbacks) {
    // Report consent status on timeout for each request to compare against the
    // number of all requests.
    PageImageServiceConsentStatus consent_status =
        ConsentStatusToUmaStatus(GetConsentStatus());
    base::UmaHistogramEnumeration("PageImageService.ConsentStatusOnTimeout",
                                  consent_status);
    std::move(request_callback_with_client_id.first).Run(consent_status);
  }
}

}  // namespace page_image_service