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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_dialog_handler.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
namespace {
using enum PrivacySandboxService::AdsDialogCallbackNoArgsEvents;
using enum PrivacySandboxService::PromptAction;
bool IsConsent(PrivacySandboxService::PromptType prompt_type) {
return prompt_type == PrivacySandboxService::PromptType::kM1Consent;
}
bool IsRestrictedNotice(PrivacySandboxService::PromptType prompt_type) {
return prompt_type == PrivacySandboxService::PromptType::kM1NoticeRestricted;
}
void NotifyServiceAboutPromptAction(
PrivacySandboxService::PromptAction action,
PrivacySandboxService* privacy_sandbox_service) {
CHECK(privacy_sandbox_service);
privacy_sandbox_service->PromptActionOccurred(
action, PrivacySandboxService::SurfaceType::kDesktop);
}
} // namespace
PrivacySandboxDialogHandler::PrivacySandboxDialogHandler(
base::RepeatingCallback<void(
PrivacySandboxService::AdsDialogCallbackNoArgsEvents)> dialog_callback,
base::OnceCallback<void(int)> resize_callback,
PrivacySandboxService::PromptType prompt_type)
: dialog_callback_(std::move(dialog_callback)),
resize_callback_(std::move(resize_callback)),
prompt_type_(prompt_type) {
DCHECK(dialog_callback_);
}
PrivacySandboxDialogHandler::~PrivacySandboxDialogHandler() {
DisallowJavascript();
}
void PrivacySandboxDialogHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"promptActionOccurred",
base::BindRepeating(
&PrivacySandboxDialogHandler::HandlePromptActionOccurred,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"resizeDialog",
base::BindRepeating(&PrivacySandboxDialogHandler::HandleResizeDialog,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"showDialog",
base::BindRepeating(&PrivacySandboxDialogHandler::HandleShowDialog,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"recordPrivacyPolicyLoadTime",
base::BindRepeating(
&PrivacySandboxDialogHandler::HandleRecordPrivacyPolicyLoadTime,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"shouldShowAdTopicsContentParity",
base::BindRepeating(
&PrivacySandboxDialogHandler::HandleShouldShowAdTopicsContentParity,
base::Unretained(this)));
}
void PrivacySandboxDialogHandler::HandleRecordPrivacyPolicyLoadTime(
const base::Value::List& args) {
if (!IsJavascriptAllowed()) {
return;
}
auto privacy_policy_page_load_duration = args[0].GetDouble();
// This just means the page was already preloaded so there is no load time.
if (privacy_policy_page_load_duration < 0) {
privacy_policy_page_load_duration = 0;
}
base::UmaHistogramTimes(
"PrivacySandbox.PrivacyPolicy.LoadingTime",
base::Milliseconds(privacy_policy_page_load_duration));
}
void PrivacySandboxDialogHandler::HandleShouldShowAdTopicsContentParity(
const base::Value::List& args) {
AllowJavascript();
ResolveJavascriptCallback(
args[0], base::FeatureList::IsEnabled(
privacy_sandbox::kPrivacySandboxAdTopicsContentParity));
}
void PrivacySandboxDialogHandler::OnJavascriptAllowed() {
// Initialize the service reference here because `web_ui()` was already
// initialized. `web_ui()` isn't ready in the constructor.
privacy_sandbox_service_ =
PrivacySandboxServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
DCHECK(privacy_sandbox_service_);
}
void PrivacySandboxDialogHandler::OnJavascriptDisallowed() {
if (did_user_make_decision_) {
return;
}
// If user hasn't made a decision, notify the service.
if (IsConsent(prompt_type_)) {
NotifyServiceAboutPromptAction(kConsentClosedNoDecision,
privacy_sandbox_service_);
} else if (IsRestrictedNotice(prompt_type_)) {
NotifyServiceAboutPromptAction(kRestrictedNoticeClosedNoInteraction,
privacy_sandbox_service_);
} else {
NotifyServiceAboutPromptAction(kNoticeClosedNoInteraction,
privacy_sandbox_service_);
}
}
void PrivacySandboxDialogHandler::HandlePromptActionOccurred(
const base::Value::List& args) {
if (!IsJavascriptAllowed()) {
return;
}
CHECK_EQ(1U, args.size());
auto action =
static_cast<PrivacySandboxService::PromptAction>(args[0].GetInt());
switch (action) {
case kNoticeAcknowledge:
case kRestrictedNoticeAcknowledge:
case kNoticeDismiss: {
final_decision_count_++;
CloseDialog();
break;
}
case kNoticeOpenSettings: {
dialog_callback_.Run(kOpenAdsPrivacySettings);
final_decision_count_++;
CloseDialog();
break;
}
case kRestrictedNoticeOpenSettings: {
dialog_callback_.Run(kOpenMeasurementSettings);
final_decision_count_++;
CloseDialog();
break;
}
case kConsentAccepted:
case kConsentDeclined: {
did_user_make_decision_ = true;
break;
}
default:
break;
}
NotifyServiceAboutPromptAction(action, privacy_sandbox_service_);
}
void PrivacySandboxDialogHandler::HandleResizeDialog(
const base::Value::List& args) {
AllowJavascript();
const base::Value& callback_id = args[0];
int height = args[1].GetInt();
DCHECK(resize_callback_);
std::move(resize_callback_).Run(height);
ResolveJavascriptCallback(callback_id, base::Value());
}
void PrivacySandboxDialogHandler::HandleShowDialog(
const base::Value::List& args) {
AllowJavascript();
CHECK(dialog_callback_);
did_dialog_show_ = true;
dialog_callback_.Run(kShowDialog);
}
void PrivacySandboxDialogHandler::CloseDialog() {
did_user_make_decision_ = true;
DisallowJavascript();
if (!dialog_callback_) {
if (!did_dialog_show_) {
base::UmaHistogramEnumeration(
"PrivacySandbox.Notice.CloseDialogCallbackState",
PrivacySandboxDialogCallbackState::kCallbackUnknownBeforeShown);
} else if (final_decision_count_ > 1) {
base::UmaHistogramEnumeration(
"PrivacySandbox.Notice.CloseDialogCallbackState",
PrivacySandboxDialogCallbackState::kMultiActionCallbackDNE);
} else {
base::UmaHistogramEnumeration(
"PrivacySandbox.Notice.CloseDialogCallbackState",
PrivacySandboxDialogCallbackState::kSingleActionCallbackDNE);
}
}
dialog_callback_.Run(kCloseDialog);
}
void PrivacySandboxDialogHandler::SetDialogCallbackForTesting(
const base::RepeatingCallback<
void(PrivacySandboxService::AdsDialogCallbackNoArgsEvents)>& callback) {
dialog_callback_ = std::move(callback);
}
|