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
ash / quick_insert / metrics / quick_insert_session_metrics.cc [blame]
// Copyright 2024 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/quick_insert/metrics/quick_insert_session_metrics.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/quick_insert/quick_insert_category.h"
#include "ash/quick_insert/quick_insert_search_result.h"
#include "base/functional/overloaded.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "components/metrics/structured/structured_events.h"
#include "components/metrics/structured/structured_metrics_client.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/ime/text_input_client.h"
namespace ash {
namespace {
namespace cros_events = metrics::structured::events::v2::cr_os_events;
constexpr int kCapsLockCountThreshold = 20;
cros_events::PickerInputFieldType GetInputFieldType(
ui::TextInputClient* client) {
if (client == nullptr) {
return cros_events::PickerInputFieldType::NONE;
}
switch (client->GetTextInputType()) {
case ui::TEXT_INPUT_TYPE_NONE:
return cros_events::PickerInputFieldType::NONE;
case ui::TEXT_INPUT_TYPE_TEXT:
case ui::TEXT_INPUT_TYPE_TEXT_AREA:
if (client->CanInsertImage()) {
return cros_events::PickerInputFieldType::RICH_TEXT;
} else {
return cros_events::PickerInputFieldType::PLAIN_TEXT;
}
case ui::TEXT_INPUT_TYPE_PASSWORD:
return cros_events::PickerInputFieldType::PASSWORD;
case ui::TEXT_INPUT_TYPE_SEARCH:
return cros_events::PickerInputFieldType::SEARCH;
case ui::TEXT_INPUT_TYPE_EMAIL:
return cros_events::PickerInputFieldType::EMAIL;
case ui::TEXT_INPUT_TYPE_NUMBER:
return cros_events::PickerInputFieldType::NUMBER;
case ui::TEXT_INPUT_TYPE_TELEPHONE:
return cros_events::PickerInputFieldType::TELEPHONE;
case ui::TEXT_INPUT_TYPE_URL:
return cros_events::PickerInputFieldType::URL;
case ui::TEXT_INPUT_TYPE_DATE:
case ui::TEXT_INPUT_TYPE_DATE_TIME:
case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
case ui::TEXT_INPUT_TYPE_MONTH:
case ui::TEXT_INPUT_TYPE_TIME:
case ui::TEXT_INPUT_TYPE_WEEK:
case ui::TEXT_INPUT_TYPE_DATE_TIME_FIELD:
return cros_events::PickerInputFieldType::DATE_TIME;
case ui::TEXT_INPUT_TYPE_CONTENT_EDITABLE:
case ui::TEXT_INPUT_TYPE_NULL:
return cros_events::PickerInputFieldType::OTHER;
}
}
int GetSelectionLength(ui::TextInputClient* client) {
if (!client) {
return 0;
}
gfx::Range selection_range;
client->GetEditableSelectionRange(&selection_range);
if (selection_range.IsValid() && !selection_range.is_empty()) {
return selection_range.length();
}
return 0;
}
cros_events::PickerSessionOutcome ConvertToCrosEventSessionOutcome(
QuickInsertSessionMetrics::SessionOutcome outcome) {
switch (outcome) {
case QuickInsertSessionMetrics::SessionOutcome::kUnknown:
return cros_events::PickerSessionOutcome::UNKNOWN;
case QuickInsertSessionMetrics::SessionOutcome::kInsertedOrCopied:
return cros_events::PickerSessionOutcome::INSERTED_OR_COPIED;
case QuickInsertSessionMetrics::SessionOutcome::kAbandoned:
return cros_events::PickerSessionOutcome::ABANDONED;
case QuickInsertSessionMetrics::SessionOutcome::kRedirected:
return cros_events::PickerSessionOutcome::REDIRECTED;
case QuickInsertSessionMetrics::SessionOutcome::kFormat:
return cros_events::PickerSessionOutcome::FORMAT;
case QuickInsertSessionMetrics::SessionOutcome::kOpenFile:
return cros_events::PickerSessionOutcome::OPEN_FILE;
case QuickInsertSessionMetrics::SessionOutcome::kOpenLink:
return cros_events::PickerSessionOutcome::OPEN_LINK;
case QuickInsertSessionMetrics::SessionOutcome::kCreate:
return cros_events::PickerSessionOutcome::CREATE;
}
}
cros_events::PickerAction ConvertToCrosEventAction(
std::optional<QuickInsertCategory> action) {
if (!action.has_value()) {
return cros_events::PickerAction::UNKNOWN;
}
switch (*action) {
case QuickInsertCategory::kEditorWrite:
return cros_events::PickerAction::OPEN_EDITOR_WRITE;
case QuickInsertCategory::kEditorRewrite:
return cros_events::PickerAction::OPEN_EDITOR_REWRITE;
case QuickInsertCategory::kLobsterWithSelectedText:
case QuickInsertCategory::kLobsterWithNoSelectedText:
return cros_events::PickerAction::OPEN_LOBSTER;
case QuickInsertCategory::kLinks:
return cros_events::PickerAction::OPEN_LINKS;
case QuickInsertCategory::kEmojisGifs:
case QuickInsertCategory::kEmojis:
return cros_events::PickerAction::OPEN_EXPRESSIONS;
case QuickInsertCategory::kGifs:
return cros_events::PickerAction::OPEN_GIFS;
case QuickInsertCategory::kClipboard:
return cros_events::PickerAction::OPEN_CLIPBOARD;
case QuickInsertCategory::kDriveFiles:
return cros_events::PickerAction::OPEN_DRIVE_FILES;
case QuickInsertCategory::kLocalFiles:
return cros_events::PickerAction::OPEN_LOCAL_FILES;
case QuickInsertCategory::kDatesTimes:
return cros_events::PickerAction::OPEN_DATES_TIMES;
case QuickInsertCategory::kUnitsMaths:
return cros_events::PickerAction::OPEN_UNITS_MATHS;
}
}
cros_events::PickerResultSource GetResultSource(
std::optional<QuickInsertSearchResult> result) {
if (!result.has_value()) {
return cros_events::PickerResultSource::UNKNOWN;
}
using ReturnType = cros_events::PickerResultSource;
return std::visit(
base::Overloaded{
[](const QuickInsertTextResult& data) {
switch (data.source) {
case QuickInsertTextResult::Source::kUnknown:
return cros_events::PickerResultSource::UNKNOWN;
case QuickInsertTextResult::Source::kDate:
return cros_events::PickerResultSource::DATES_TIMES;
case QuickInsertTextResult::Source::kMath:
return cros_events::PickerResultSource::UNITS_MATHS;
case QuickInsertTextResult::Source::kCaseTransform:
return cros_events::PickerResultSource::CASE_TRANSFORM;
case QuickInsertTextResult::Source::kOmnibox:
return cros_events::PickerResultSource::OMNIBOX;
}
},
[](const QuickInsertEmojiResult& data) {
return cros_events::PickerResultSource::EMOJI;
},
[](const QuickInsertClipboardResult& data) {
return cros_events::PickerResultSource::CLIPBOARD;
},
[](const QuickInsertGifResult& data) {
return cros_events::PickerResultSource::TENOR;
},
[](const QuickInsertBrowsingHistoryResult& data) {
return cros_events::PickerResultSource::OMNIBOX;
},
[](const QuickInsertLocalFileResult& data) {
return cros_events::PickerResultSource::LOCAL_FILES;
},
[](const QuickInsertDriveFileResult& data) {
return cros_events::PickerResultSource::DRIVE_FILES;
},
[](const QuickInsertCategoryResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertSearchRequestResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertEditorResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertLobsterResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertNewWindowResult& data) -> ReturnType {
return cros_events::PickerResultSource::UNKNOWN;
},
[](const QuickInsertCapsLockResult& data) -> ReturnType {
return cros_events::PickerResultSource::UNKNOWN;
},
[](const QuickInsertCaseTransformResult& data) -> ReturnType {
return cros_events::PickerResultSource::CASE_TRANSFORM;
},
},
*result);
}
cros_events::PickerResultType GetResultType(
std::optional<QuickInsertSearchResult> result) {
if (!result.has_value()) {
return cros_events::PickerResultType::UNKNOWN;
}
using ReturnType = cros_events::PickerResultType;
return std::visit(
base::Overloaded{
[](const QuickInsertTextResult& data) {
return cros_events::PickerResultType::TEXT;
},
[](const QuickInsertEmojiResult& data) {
switch (data.type) {
case QuickInsertEmojiResult::Type::kEmoji:
return cros_events::PickerResultType::EMOJI;
case QuickInsertEmojiResult::Type::kSymbol:
return cros_events::PickerResultType::SYMBOL;
case QuickInsertEmojiResult::Type::kEmoticon:
return cros_events::PickerResultType::EMOTICON;
}
},
[](const QuickInsertClipboardResult& data) {
switch (data.display_format) {
case QuickInsertClipboardResult::DisplayFormat::kFile:
return cros_events::PickerResultType::CLIPBOARD_FILE;
case QuickInsertClipboardResult::DisplayFormat::kText:
return cros_events::PickerResultType::CLIPBOARD_TEXT;
case QuickInsertClipboardResult::DisplayFormat::kImage:
return cros_events::PickerResultType::CLIPBOARD_IMAGE;
case QuickInsertClipboardResult::DisplayFormat::kHtml:
return cros_events::PickerResultType::CLIPBOARD_HTML;
}
},
[](const QuickInsertGifResult& data) {
return cros_events::PickerResultType::GIF;
},
[](const QuickInsertBrowsingHistoryResult& data) {
return cros_events::PickerResultType::LINK;
},
[](const QuickInsertLocalFileResult& data) {
return cros_events::PickerResultType::LOCAL_FILE;
},
[](const QuickInsertDriveFileResult& data) {
return cros_events::PickerResultType::DRIVE_FILE;
},
[](const QuickInsertCategoryResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertSearchRequestResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertEditorResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertLobsterResult& data) -> ReturnType {
NOTREACHED();
},
[](const QuickInsertNewWindowResult& data) -> ReturnType {
return cros_events::PickerResultType::UNKNOWN;
},
[](const QuickInsertCapsLockResult& data) -> ReturnType {
return cros_events::PickerResultType::UNKNOWN;
},
[](const QuickInsertCaseTransformResult& data) -> ReturnType {
return cros_events::PickerResultType::TEXT;
},
},
*result);
}
} // namespace
QuickInsertSessionMetrics::QuickInsertSessionMetrics() = default;
QuickInsertSessionMetrics::QuickInsertSessionMetrics(PrefService* prefs)
: prefs_(prefs) {}
QuickInsertSessionMetrics::~QuickInsertSessionMetrics() {
OnFinishSession();
}
void QuickInsertSessionMetrics::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(prefs::kQuickInsertLockSelectedCountPrefName,
0);
registry->RegisterIntegerPref(
prefs::kQuickInsertCapsLockDisplayedCountPrefName, 0);
}
void QuickInsertSessionMetrics::SetOutcome(SessionOutcome outcome) {
if (outcome_ == SessionOutcome::kUnknown) {
outcome_ = outcome;
}
}
void QuickInsertSessionMetrics::SetSelectedCategory(
QuickInsertCategory category) {
if (!last_category_.has_value()) {
last_category_ = category;
}
}
void QuickInsertSessionMetrics::SetSelectedResult(
QuickInsertSearchResult selected_result,
int index) {
if (!selected_result_.has_value()) {
selected_result_ = std::move(selected_result);
result_index_ = index;
}
}
void QuickInsertSessionMetrics::UpdateSearchQuery(
std::u16string_view search_query) {
int new_length = static_cast<int>(search_query.length());
search_query_total_edits_ += abs(new_length - search_query_length_);
search_query_length_ = new_length;
}
void QuickInsertSessionMetrics::OnStartSession(ui::TextInputClient* client) {
metrics::structured::StructuredMetricsClient::Record(
std::move(cros_events::Picker_StartSession()
.SetInputFieldType(GetInputFieldType(client))
.SetSelectionLength(
static_cast<int64_t>(GetSelectionLength(client)))));
}
void QuickInsertSessionMetrics::OnFinishSession() {
if (caps_lock_displayed_) {
UpdateCapLockPrefs(
selected_result_.has_value() &&
std::holds_alternative<QuickInsertCapsLockResult>(*selected_result_));
}
base::UmaHistogramEnumeration("Ash.Picker.Session.Outcome", outcome_);
metrics::structured::StructuredMetricsClient::Record(
cros_events::Picker_FinishSession()
.SetOutcome(ConvertToCrosEventSessionOutcome(outcome_))
.SetAction(ConvertToCrosEventAction(last_category_))
.SetResultSource(GetResultSource(std::move(selected_result_)))
.SetResultType(GetResultType(std::move(selected_result_)))
.SetTotalEdits(search_query_total_edits_)
.SetFinalQuerySize(search_query_length_)
.SetResultIndex(result_index_));
}
void QuickInsertSessionMetrics::SetCapsLockDisplayed(bool displayed) {
caps_lock_displayed_ = displayed;
}
void QuickInsertSessionMetrics::UpdateCapLockPrefs(bool caps_lock_selected) {
if (prefs_ == nullptr) {
return;
}
int caps_lock_displayed_count =
prefs_->GetInteger(prefs::kQuickInsertCapsLockDisplayedCountPrefName) + 1;
int caps_lock_selected_count =
prefs_->GetInteger(prefs::kQuickInsertLockSelectedCountPrefName);
if (caps_lock_selected) {
++caps_lock_selected_count;
}
// We will only use caps_lock_selected_count / caps_lock_displayed_count to
// decide the position of caps lock toggle. We halves both numbers so that
// they don't grow infinitely and later usages have more weights in decision
// making. The remainders in division is not significant in our use cases.
if (caps_lock_displayed_count >= kCapsLockCountThreshold) {
caps_lock_displayed_count /= 2;
caps_lock_selected_count /= 2;
}
prefs_->SetInteger(prefs::kQuickInsertCapsLockDisplayedCountPrefName,
caps_lock_displayed_count);
prefs_->SetInteger(prefs::kQuickInsertLockSelectedCountPrefName,
caps_lock_selected_count);
}
} // namespace ash