-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
consented_message_android.cc
114 lines (101 loc) · 4.62 KB
/
consented_message_android.cc
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/tailored_security/consented_message_android.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/android/resource_mapper.h"
#include "chrome/browser/safe_browsing/android/safe_browsing_settings_launcher_android.h"
#include "chrome/browser/safe_browsing/tailored_security/tailored_security_outcome.h"
#include "chrome/grit/generated_resources.h"
#include "components/messages/android/message_dispatcher_bridge.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
namespace safe_browsing {
namespace {
void LogOutcome(TailoredSecurityOutcome outcome, bool enable) {
std::string histogram =
enable ? "SafeBrowsing.TailoredSecurityConsentedEnabledMessageOutcome"
: "SafeBrowsing.TailoredSecurityConsentedDisabledMessageOutcome";
base::UmaHistogramEnumeration(histogram, outcome);
}
} // namespace
TailoredSecurityConsentedModalAndroid::TailoredSecurityConsentedModalAndroid(
content::WebContents* web_contents,
bool enable,
base::OnceClosure dismiss_callback)
: web_contents_(web_contents),
dismiss_callback_(std::move(dismiss_callback)),
is_enable_message_(enable) {
message_ = std::make_unique<messages::MessageWrapper>(
is_enable_message_
? messages::MessageIdentifier::TAILORED_SECURITY_ENABLED
: messages::MessageIdentifier::TAILORED_SECURITY_DISABLED,
base::BindOnce(
&TailoredSecurityConsentedModalAndroid::HandleMessageAccepted,
base::Unretained(this)),
base::BindOnce(
&TailoredSecurityConsentedModalAndroid::HandleMessageDismissed,
base::Unretained(this)));
std::u16string title, description;
int icon_resource_id;
if (is_enable_message_) {
title = l10n_util::GetStringUTF16(
IDS_TAILORED_SECURITY_CONSENTED_ENABLE_MESSAGE_TITLE);
description = l10n_util::GetStringUTF16(
IDS_TAILORED_SECURITY_CONSENTED_ENABLE_MESSAGE_DESCRIPTION);
icon_resource_id =
ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_MESSAGE_SAFETY_CHECK);
} else {
title = l10n_util::GetStringUTF16(
IDS_TAILORED_SECURITY_CONSENTED_DISABLE_MESSAGE_TITLE);
description = l10n_util::GetStringUTF16(
IDS_TAILORED_SECURITY_CONSENTED_DISABLE_MESSAGE_DESCRIPTION);
icon_resource_id =
ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_MESSAGE_SHIELD);
message_->DisableIconTint();
}
message_->SetTitle(title);
message_->SetDescription(description);
message_->SetPrimaryButtonText(l10n_util::GetStringUTF16(
IDS_TAILORED_SECURITY_CONSENTED_MESSAGE_OK_BUTTON));
message_->SetIconResourceId(icon_resource_id);
message_->SetSecondaryIconResourceId(
ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_MESSAGE_SETTINGS));
message_->SetSecondaryActionCallback(base::BindOnce(
&TailoredSecurityConsentedModalAndroid::HandleSettingsClicked,
base::Unretained(this)));
messages::MessageDispatcherBridge::Get()->EnqueueWindowScopedMessage(
message_.get(), web_contents_->GetTopLevelNativeWindow(),
messages::MessagePriority::kNormal);
LogOutcome(TailoredSecurityOutcome::kShown, is_enable_message_);
}
TailoredSecurityConsentedModalAndroid::
~TailoredSecurityConsentedModalAndroid() {
DismissMessageInternal(messages::DismissReason::UNKNOWN);
}
void TailoredSecurityConsentedModalAndroid::DismissMessageInternal(
messages::DismissReason dismiss_reason) {
if (!message_)
return;
messages::MessageDispatcherBridge::Get()->DismissMessage(message_.get(),
dismiss_reason);
}
void TailoredSecurityConsentedModalAndroid::HandleSettingsClicked() {
ShowSafeBrowsingSettings(web_contents_,
SettingsAccessPoint::kTailoredSecurity);
LogOutcome(TailoredSecurityOutcome::kSettings, is_enable_message_);
DismissMessageInternal(messages::DismissReason::SECONDARY_ACTION);
}
void TailoredSecurityConsentedModalAndroid::HandleMessageDismissed(
messages::DismissReason dismiss_reason) {
LogOutcome(TailoredSecurityOutcome::kDismissed, is_enable_message_);
message_.reset();
if (dismiss_callback_)
std::move(dismiss_callback_).Run();
}
void TailoredSecurityConsentedModalAndroid::HandleMessageAccepted() {
LogOutcome(TailoredSecurityOutcome::kAccepted, is_enable_message_);
}
} // namespace safe_browsing