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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
ash / clipboard / clipboard_nudge_controller.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/clipboard/clipboard_nudge_controller.h"
#include "ash/clipboard/clipboard_history_item.h"
#include "ash/clipboard/clipboard_history_util.h"
#include "ash/clipboard/clipboard_nudge_constants.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/public/cpp/system/anchored_nudge_manager.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/json/values_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "ui/base/clipboard/clipboard_monitor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace ash {
namespace {
// Clock that can be overridden for testing.
base::Clock* g_clock_override = nullptr;
// Capped nudge constants ------------------------------------------------------
// The pref keys used by the capped nudges (i.e. the nudges that have a
// limited number of times they can be shown to a user). The associated pref
// data are recorded across user sessions.
// The last time shown, shared by all capped nudges. Updated when a nudge shows.
constexpr char kCappedNudgeLastTimeShown[] = "last_time_shown";
// The shown count of duplicate copy nudges.
constexpr char kShownCountDuplicateCopyNudge[] =
"shown_count_duplicate_copy_nudge";
// The shown count of onboarding nudges.
constexpr char kShownCountOnboardingNudge[] = "shown_count";
// Constants -------------------------------------------------------------------
// The id used for clipboard nudges.
constexpr char kClipboardNudgeId[] = "ClipboardContextualNudge";
// The maximum number of 1 second buckets, used to record the time delta between
// when a nudge shows and when the clipboard history menu shows or clipboard
// history data is pasted.
constexpr int kMaxSeconds = 61;
// Helpers ---------------------------------------------------------------------
int GetBodyTextStringId(ClipboardNudgeType nudge_type) {
switch (nudge_type) {
case ClipboardNudgeType::kDuplicateCopyNudge:
return IDS_ASH_MULTIPASTE_DUPLICATE_COPY_NUDGE;
case ClipboardNudgeType::kOnboardingNudge:
return IDS_ASH_MULTIPASTE_CONTEXTUAL_NUDGE;
case ClipboardNudgeType::kScreenshotNotificationNudge:
return IDS_ASH_MULTIPASTE_SCREENSHOT_NOTIFICATION_NUDGE;
case ClipboardNudgeType::kZeroStateNudge:
return IDS_ASH_MULTIPASTE_ZERO_STATE_CONTEXTUAL_NUDGE;
}
}
NudgeCatalogName GetCatalogName(ClipboardNudgeType type) {
switch (type) {
case kOnboardingNudge:
return NudgeCatalogName::kClipboardHistoryOnboarding;
case kZeroStateNudge:
return NudgeCatalogName::kClipboardHistoryZeroState;
case kScreenshotNotificationNudge:
NOTREACHED();
case kDuplicateCopyNudge:
return NudgeCatalogName::kClipboardHistoryDuplicateCopy;
}
NOTREACHED();
}
ui::ImageModel GetImage(ClipboardNudgeType type) {
switch (type) {
case kDuplicateCopyNudge:
case kOnboardingNudge:
return ui::ResourceBundle::GetSharedInstance().GetThemedLottieImageNamed(
IDR_CLIPBOARD_NUDGE_COPIED_IMAGE);
case kScreenshotNotificationNudge:
case kZeroStateNudge:
return ui::ResourceBundle::GetSharedInstance().GetThemedLottieImageNamed(
IDR_CLIPBOARD_NUDGE_SELECT_IMAGE);
}
}
base::Time GetTime() {
return g_clock_override ? g_clock_override->Now() : base::Time::Now();
}
// Capped nudge helpers --------------------------------------------------------
// Returns true if `type` indicates a capped nudge.
bool IsCappedNudge(ClipboardNudgeType type) {
switch (type) {
case kOnboardingNudge:
case kDuplicateCopyNudge:
return true;
case kScreenshotNotificationNudge:
case kZeroStateNudge:
return false;
}
}
// Gets the pref key to the shown count of the specified capped nudge.
const char* GetCappedNudgeShownCountPrefKey(ClipboardNudgeType type) {
CHECK(IsCappedNudge(type));
switch (type) {
case kOnboardingNudge:
return kShownCountOnboardingNudge;
case kDuplicateCopyNudge:
return kShownCountDuplicateCopyNudge;
case kScreenshotNotificationNudge:
case kZeroStateNudge:
NOTREACHED();
}
}
// Gets the number of times the specified capped nudge has shown across user
// sessions.
int GetCappedNudgeShownCount(const PrefService& prefs,
ClipboardNudgeType type) {
return prefs.GetDict(prefs::kMultipasteNudges)
.FindInt(GetCappedNudgeShownCountPrefKey(type))
.value_or(0);
}
// Gets the last time the capped nudge was shown across user sessions.
base::Time GetCappedNudgeLastShownTime(const PrefService& prefs) {
const std::optional<base::Time> last_shown_time = base::ValueToTime(
prefs.GetDict(prefs::kMultipasteNudges).Find(kCappedNudgeLastTimeShown));
return last_shown_time.value_or(base::Time());
}
// Checks if a capped nudge of the specified `type` can be shown. Returns true
// if:
// 1. The specified nudge's shown count is below the threshold; AND
// 2. Enough time has elapsed since the last capped nudge, if any, was shown.
bool ShouldShowCappedNudge(const PrefService& prefs, ClipboardNudgeType type) {
// We should not show more nudges after hitting the limit.
if (GetCappedNudgeShownCount(prefs, type) >= kCappedNudgeShownLimit) {
return false;
}
// Returns true if:
// 1. No capped nudge has been shown; OR
// 2. Enough time has elapsed since the last capped nudge was shown.
const base::Time last_shown_time = GetCappedNudgeLastShownTime(prefs);
return last_shown_time.is_null() ||
GetTime() - last_shown_time > kCappedNudgeMinInterval;
}
} // namespace
// ClipboardNudgeController::NudgeTimeDeltaRecorder ---------------------------
constexpr ClipboardNudgeController::NudgeTimeDeltaRecorder::
NudgeTimeDeltaRecorder(ClipboardNudgeType nudge_type)
: nudge_type_(nudge_type) {}
ClipboardNudgeController::NudgeTimeDeltaRecorder::~NudgeTimeDeltaRecorder() {
Reset();
}
void ClipboardNudgeController::NudgeTimeDeltaRecorder::OnNudgeShown() {
Reset();
nudge_shown_time_ = GetTime();
}
void ClipboardNudgeController::NudgeTimeDeltaRecorder::
OnClipboardHistoryPasted() {
if (ShouldRecordClipboardHistoryPasteTimeDelta()) {
base::UmaHistogramExactLinear(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type_),
GetTimeSinceNudgeShown().InSeconds(), kMaxSeconds);
has_recorded_paste_ = true;
}
}
void ClipboardNudgeController::NudgeTimeDeltaRecorder::
OnClipboardHistoryMenuShown() {
if (ShouldRecordMenuOpenTimeDelta()) {
base::UmaHistogramExactLinear(GetMenuOpenTimeDeltaHistogram(nudge_type_),
GetTimeSinceNudgeShown().InSeconds(),
kMaxSeconds);
has_recorded_menu_shown_ = true;
}
}
void ClipboardNudgeController::NudgeTimeDeltaRecorder::Reset() {
// Record `kMaxSeconds` if the standalone clipboard history menu has never
// shown since the last nudge shown, if any.
if (ShouldRecordMenuOpenTimeDelta()) {
base::UmaHistogramExactLinear(GetMenuOpenTimeDeltaHistogram(nudge_type_),
kMaxSeconds, kMaxSeconds);
}
// Record `kMaxSeconds` if the clipboard history data has never been pasted
// since the last nudge shown, if any.
if (ShouldRecordClipboardHistoryPasteTimeDelta()) {
base::UmaHistogramExactLinear(
GetClipboardHistoryPasteTimeDeltaHistogram(nudge_type_), kMaxSeconds,
kMaxSeconds);
}
nudge_shown_time_ = base::Time();
has_recorded_menu_shown_ = false;
has_recorded_paste_ = false;
}
base::TimeDelta
ClipboardNudgeController::NudgeTimeDeltaRecorder::GetTimeSinceNudgeShown()
const {
CHECK(!nudge_shown_time_.is_null());
return GetTime() - nudge_shown_time_;
}
bool ClipboardNudgeController::NudgeTimeDeltaRecorder::
ShouldRecordClipboardHistoryPasteTimeDelta() const {
return !nudge_shown_time_.is_null() && !has_recorded_paste_;
}
bool ClipboardNudgeController::NudgeTimeDeltaRecorder::
ShouldRecordMenuOpenTimeDelta() const {
return !nudge_shown_time_.is_null() && !has_recorded_menu_shown_;
}
// ClipboardNudgeController ----------------------------------------------------
ClipboardNudgeController::ClipboardNudgeController(
ClipboardHistory* clipboard_history) {
clipboard_history_observation_.Observe(clipboard_history);
clipboard_history_controller_observation_.Observe(
ClipboardHistoryController::Get());
clipboard_monitor_observation_.Observe(ui::ClipboardMonitor::GetInstance());
}
ClipboardNudgeController::~ClipboardNudgeController() = default;
// static
void ClipboardNudgeController::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kMultipasteNudges);
}
void ClipboardNudgeController::OnClipboardHistoryItemAdded(
const ClipboardHistoryItem& item,
bool is_duplicate) {
const PrefService* const prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!prefs) {
return;
}
if (ShouldShowCappedNudge(*prefs, ClipboardNudgeType::kOnboardingNudge)) {
switch (onboarding_state_) {
case OnboardingState::kInit:
onboarding_state_ = OnboardingState::kFirstCopy;
break;
case OnboardingState::kFirstPaste:
onboarding_state_ = OnboardingState::kSecondCopy;
break;
case OnboardingState::kFirstCopy:
case OnboardingState::kSecondCopy:
break;
}
}
if (is_duplicate &&
ShouldShowCappedNudge(*prefs, ClipboardNudgeType::kDuplicateCopyNudge)) {
ShowNudge(ClipboardNudgeType::kDuplicateCopyNudge);
}
}
std::optional<base::Time> ClipboardNudgeController::GetNudgeLastTimeShown()
const {
const base::Time& nudge_last_time_shown =
base::ranges::max(
{&duplicate_copy_nudge_recorder_, &onboarding_nudge_recorder_,
&screenshot_nudge_recorder_, &zero_state_nudge_recorder_},
/*comp=*/{}, /*proj=*/&NudgeTimeDeltaRecorder::nudge_shown_time)
->nudge_shown_time();
return nudge_last_time_shown.is_null()
? std::nullopt
: std::make_optional(nudge_last_time_shown);
}
void ClipboardNudgeController::MarkScreenshotNotificationShown() {
base::UmaHistogramBoolean(kClipboardHistoryScreenshotNotificationShowCount,
true);
screenshot_nudge_recorder_.OnNudgeShown();
}
void ClipboardNudgeController::OnClipboardDataRead() {
if (const PrefService* const prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
clipboard_history_util::IsEnabledInCurrentMode() && prefs &&
ShouldShowCappedNudge(*prefs, ClipboardNudgeType::kOnboardingNudge)) {
switch (onboarding_state_) {
case OnboardingState::kInit:
return;
case OnboardingState::kFirstCopy:
onboarding_state_ = OnboardingState::kFirstPaste;
last_paste_timestamp_ = GetTime();
return;
case OnboardingState::kFirstPaste:
// Subsequent pastes should reset the timestamp.
last_paste_timestamp_ = GetTime();
return;
case OnboardingState::kSecondCopy:
if (GetTime() - last_paste_timestamp_ < kMaxTimeBetweenPaste) {
ShowNudge(ClipboardNudgeType::kOnboardingNudge);
} else {
// Reset `onboarding_state_` to `kFirstPaste` when too much time has
// elapsed since the last paste.
onboarding_state_ = OnboardingState::kFirstPaste;
last_paste_timestamp_ = GetTime();
}
return;
}
}
}
void ClipboardNudgeController::OnClipboardHistoryMenuShown(
crosapi::mojom::ClipboardHistoryControllerShowSource show_source) {
// The clipboard history nudges specifically suggest trying the Search+V
// shortcut. Opening the menu any other way should not count as the user
// responding to the nudge.
if (show_source !=
crosapi::mojom::ClipboardHistoryControllerShowSource::kAccelerator) {
return;
}
onboarding_nudge_recorder_.OnClipboardHistoryMenuShown();
zero_state_nudge_recorder_.OnClipboardHistoryMenuShown();
screenshot_nudge_recorder_.OnClipboardHistoryMenuShown();
AnchoredNudgeManager::Get()->MaybeRecordNudgeAction(
NudgeCatalogName::kClipboardHistoryOnboarding);
AnchoredNudgeManager::Get()->MaybeRecordNudgeAction(
NudgeCatalogName::kClipboardHistoryZeroState);
duplicate_copy_nudge_recorder_.OnClipboardHistoryMenuShown();
AnchoredNudgeManager::Get()->MaybeRecordNudgeAction(
NudgeCatalogName::kClipboardHistoryDuplicateCopy);
}
void ClipboardNudgeController::OnClipboardHistoryPasted() {
onboarding_nudge_recorder_.OnClipboardHistoryPasted();
zero_state_nudge_recorder_.OnClipboardHistoryPasted();
screenshot_nudge_recorder_.OnClipboardHistoryPasted();
duplicate_copy_nudge_recorder_.OnClipboardHistoryPasted();
}
void ClipboardNudgeController::ShowNudge(ClipboardNudgeType nudge_type) {
current_nudge_type_ = nudge_type;
const std::u16string shortcut_key =
clipboard_history_util::GetShortcutKeyName();
const std::u16string body_text = l10n_util::GetStringFUTF16(
GetBodyTextStringId(current_nudge_type_), shortcut_key);
AnchoredNudgeData nudge_data(kClipboardNudgeId,
GetCatalogName(current_nudge_type_), body_text);
nudge_data.image_model = GetImage(current_nudge_type_);
AnchoredNudgeManager::Get()->Show(nudge_data);
switch (nudge_type) {
case ClipboardNudgeType::kOnboardingNudge:
onboarding_nudge_recorder_.OnNudgeShown();
base::UmaHistogramBoolean(kClipboardHistoryOnboardingNudgeShowCount,
true);
break;
case ClipboardNudgeType::kZeroStateNudge:
zero_state_nudge_recorder_.OnNudgeShown();
base::UmaHistogramBoolean(kClipboardHistoryZeroStateNudgeShowCount, true);
break;
case ClipboardNudgeType::kScreenshotNotificationNudge:
NOTREACHED();
case ClipboardNudgeType::kDuplicateCopyNudge:
duplicate_copy_nudge_recorder_.OnNudgeShown();
base::UmaHistogramBoolean(kClipboardHistoryDuplicateCopyNudgeShowCount,
true);
break;
}
// Reset `onboarding_state_`.
onboarding_state_ = OnboardingState::kInit;
if (PrefService* const prefs =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
prefs && IsCappedNudge(nudge_type)) {
ScopedDictPrefUpdate update(prefs, prefs::kMultipasteNudges);
update->Set(GetCappedNudgeShownCountPrefKey(nudge_type),
GetCappedNudgeShownCount(*prefs, nudge_type) + 1);
update->Set(kCappedNudgeLastTimeShown, base::TimeToValue(GetTime()));
}
}
void ClipboardNudgeController::OverrideClockForTesting(
base::Clock* test_clock) {
DCHECK(!g_clock_override);
g_clock_override = test_clock;
}
void ClipboardNudgeController::ClearClockOverrideForTesting() {
DCHECK(g_clock_override);
g_clock_override = nullptr;
}
} // namespace ash