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
|
// Copyright 2021 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/apps/app_service/metrics/app_platform_input_metrics.h"
#include <string_view>
#include "ash/shell.h"
#include "base/containers/fixed_flat_map.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics.h"
#include "chrome/browser/apps/app_service/metrics/app_platform_metrics_utils.h"
#include "chrome/browser/apps/app_service/web_contents_app_id_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chromeos/components/mgs/managed_guest_session_utils.h"
#include "components/app_constants/constants.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/services/app_service/public/cpp/instance_update.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "ui/aura/window.h"
#include "ui/events/event_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/views/widget/widget.h"
namespace apps {
namespace {
constexpr char kInputEventMouseKey[] = "mouse";
constexpr char kInputEventStylusKey[] = "stylus";
constexpr char kInputEventTouchKey[] = "touch";
constexpr char kInputEventKeyboardKey[] = "keyboard";
constexpr auto kInputEventSourceMap =
base::MakeFixedFlatMap<std::string_view, InputEventSource>({
{kInputEventMouseKey, InputEventSource::kMouse},
{kInputEventStylusKey, InputEventSource::kStylus},
{kInputEventTouchKey, InputEventSource::kTouch},
{kInputEventKeyboardKey, InputEventSource::kKeyboard},
});
InputEventSource GetInputEventSource(ui::EventPointerType type) {
switch (type) {
case ui::EventPointerType::kUnknown:
return InputEventSource::kUnknown;
case ui::EventPointerType::kMouse:
return InputEventSource::kMouse;
case ui::EventPointerType::kPen:
return InputEventSource::kStylus;
case ui::EventPointerType::kTouch:
return InputEventSource::kTouch;
case ui::EventPointerType::kEraser:
return InputEventSource::kStylus;
}
}
// Returns the input event source for the given `event_source` string.
InputEventSource GetInputEventSourceFromString(
const std::string& event_source) {
auto it = kInputEventSourceMap.find(event_source);
return (it != kInputEventSourceMap.end()) ? it->second
: InputEventSource::kUnknown;
}
// Returns the string key for `event_source` to save input events in the user
// pref.
std::string GetInputEventSourceKey(InputEventSource event_source) {
switch (event_source) {
case InputEventSource::kUnknown:
return std::string();
case InputEventSource::kMouse:
return kInputEventMouseKey;
case InputEventSource::kStylus:
return kInputEventStylusKey;
case InputEventSource::kTouch:
return kInputEventTouchKey;
case InputEventSource::kKeyboard:
return kInputEventKeyboardKey;
}
}
base::Value::Dict ConvertEventCountsToValue(
const AppPlatformInputMetrics::EventSourceToCounts& event_counts) {
base::Value::Dict event_counts_dict;
for (const auto& counts : event_counts) {
base::Value::Dict count_dict;
for (const auto& it : counts.second) {
count_dict.Set(GetAppTypeHistogramName(it.first), it.second);
}
event_counts_dict.Set(GetInputEventSourceKey(counts.first),
std::move(count_dict));
}
return event_counts_dict;
}
AppPlatformInputMetrics::EventSourceToCounts ConvertDictValueToEventCounts(
const base::Value::Dict& event_counts) {
AppPlatformInputMetrics::EventSourceToCounts ret;
for (const auto [app_id, counts] : event_counts) {
auto event_source = GetInputEventSourceFromString(app_id);
if (event_source == InputEventSource::kUnknown) {
continue;
}
const base::Value::Dict* counts_dict = counts.GetIfDict();
if (!counts_dict) {
continue;
}
for (const auto [app_type, count_value] : *counts_dict) {
auto app_type_name = GetAppTypeNameFromString(app_type);
if (app_type_name == AppTypeName::kUnknown) {
continue;
}
auto count = count_value.GetIfInt();
if (!count.has_value()) {
continue;
}
ret[event_source][app_type_name] = count.value();
}
}
return ret;
}
} // namespace
constexpr char kAppInputEventsKey[] = "app_platform_metrics.app_input_events";
AppPlatformInputMetrics::AppPlatformInputMetrics(
Profile* profile,
const apps::AppRegistryCache& app_registry_cache,
InstanceRegistry& instance_registry)
: profile_(profile), app_registry_cache_(app_registry_cache) {
instance_registry_observation_.Observe(&instance_registry);
if (chromeos::IsManagedGuestSession()) {
CHECK(ukm::UkmRecorder::Get());
ukm_recorder_observer_.Observe(ukm::UkmRecorder::Get());
}
if (ash::Shell::HasInstance()) {
ash::Shell::Get()->AddPreTargetHandler(this);
}
}
AppPlatformInputMetrics::~AppPlatformInputMetrics() {
if (ash::Shell::HasInstance()) {
ash::Shell::Get()->RemovePreTargetHandler(this);
}
}
void AppPlatformInputMetrics::OnMouseEvent(ui::MouseEvent* event) {
if (event->type() == ui::EventType::kMouseReleased) {
RecordEventCount(GetInputEventSource(event->pointer_details().pointer_type),
event->target());
}
}
void AppPlatformInputMetrics::OnKeyEvent(ui::KeyEvent* event) {
if (event->type() == ui::EventType::kKeyReleased) {
RecordEventCount(InputEventSource::kKeyboard, event->target());
}
}
void AppPlatformInputMetrics::OnTouchEvent(ui::TouchEvent* event) {
if (event->type() == ui::EventType::kTouchReleased) {
RecordEventCount(GetInputEventSource(event->pointer_details().pointer_type),
event->target());
}
}
void AppPlatformInputMetrics::OnFiveMinutes() {
// For the first five minutes, since the saved input events in pref haven't
// been recorded yet, read the input events saved in the user pref, and record
// the input events UKM, then save the new input events to the user pref.
if (should_record_ukm_from_pref_) {
RecordInputEventsAppKMFromPref();
should_record_ukm_from_pref_ = false;
}
SaveInputEvents();
}
void AppPlatformInputMetrics::OnTwoHours() {
RecordInputEventsAppKM();
}
void AppPlatformInputMetrics::OnInstanceUpdate(const InstanceUpdate& update) {
if (!update.StateChanged()) {
return;
}
aura::Window* window = update.Window();
if (update.State() & InstanceState::kDestroyed) {
window_to_app_info_.erase(window);
browser_to_tab_list_.RemoveActivatedTab(update.InstanceId());
return;
}
auto app_id = update.AppId();
auto app_type = GetAppType(profile_, app_id);
// For apps, not opened with browser windows, the app id and app type should
// not change. So if we have the app info for the window, we don't need to
// update it.
if (base::Contains(window_to_app_info_, window) &&
!IsAppOpenedWithBrowserWindow(profile_, app_type, app_id)) {
return;
}
if (update.State() & apps::InstanceState::kActive) {
SetAppInfoForActivatedWindow(app_type, app_id, window, update.InstanceId());
} else {
SetAppInfoForInactivatedWindow(update.InstanceId());
}
}
void AppPlatformInputMetrics::OnInstanceRegistryWillBeDestroyed(
InstanceRegistry* cache) {
instance_registry_observation_.Reset();
}
void AppPlatformInputMetrics::OnStartingShutdown() {
CHECK(chromeos::IsManagedGuestSession());
RecordInputEventsAppKM();
}
void AppPlatformInputMetrics::SetAppInfoForActivatedWindow(
AppType app_type,
const std::string& app_id,
aura::Window* window,
const base::UnguessableToken& instance_id) {
// For the browser window, if a tab of the browser is activated, we don't
// need to update, because we can reuse the active tab's app id.
if (app_id == app_constants::kChromeAppId &&
browser_to_tab_list_.HasActivatedTab(window)) {
return;
}
AppTypeName app_type_name =
GetAppTypeNameForWindow(profile_, app_type, app_id, window);
if (app_type_name == AppTypeName::kUnknown) {
return;
}
// For apps opened in browser windows, get the top level window, and modify
// `browser_to_tab_list_` to save the activated tab app id.
if (IsAppOpenedWithBrowserWindow(profile_, app_type, app_id)) {
window = window->GetToplevelWindow();
if (IsAppOpenedInTab(app_type_name, app_id)) {
// When the tab is pulled to a separate browser window, the instance id is
// not changed, but the parent browser window is changed. So remove the
// tab window instance from previous browser window, and add it to the new
// browser window.
browser_to_tab_list_.RemoveActivatedTab(instance_id);
browser_to_tab_list_.AddActivatedTab(window, instance_id, app_id);
}
}
window_to_app_info_[window].app_id = app_id;
window_to_app_info_[window].app_type_name = app_type_name;
}
void AppPlatformInputMetrics::SetAppInfoForInactivatedWindow(
const base::UnguessableToken& instance_id) {
// When the window is inactived, only modify the app info for browser windows,
// because the activated tab changing might affect the app id for browser
// windows.
//
// For apps, not opened with browser windows, the app id and app type should
// not be changed for non-browser windows, and they can be modified when the
// window is activated.
auto* browser_window = browser_to_tab_list_.GetBrowserWindow(instance_id);
if (!browser_window) {
return;
}
browser_to_tab_list_.RemoveActivatedTab(instance_id);
auto app_id = browser_to_tab_list_.GetActivatedTabAppId(browser_window);
if (app_id.empty()) {
app_id = app_constants::kChromeAppId;
}
window_to_app_info_[browser_window].app_id = app_id;
window_to_app_info_[browser_window].app_type_name =
apps::AppTypeName::kChromeBrowser;
}
void AppPlatformInputMetrics::RecordEventCount(InputEventSource event_source,
ui::EventTarget* event_target) {
views::Widget* target = views::Widget::GetTopLevelWidgetForNativeView(
static_cast<aura::Window*>(event_target));
if (!target) {
return;
}
aura::Window* top_window = target->GetNativeWindow();
if (!top_window) {
return;
}
auto it = window_to_app_info_.find(top_window);
if (it == window_to_app_info_.end()) {
return;
}
if (!ShouldRecordAppKMForApp(it->second.app_id)) {
return;
}
++app_id_to_event_count_per_two_hours_[it->second.app_id][event_source]
[it->second.app_type_name];
}
void AppPlatformInputMetrics::RecordInputEventsAppKM() {
if (!ShouldRecordAppKM(profile_)) {
return;
}
for (const auto& event_counts : app_id_to_event_count_per_two_hours_) {
if (!ShouldRecordAppKMForApp(event_counts.first)) {
continue;
}
// `event_counts.second` is the map from InputEventSource to the event
// counts.
RecordInputEventsAppKMForApp(event_counts.first, event_counts.second);
}
app_id_to_event_count_per_two_hours_.clear();
}
void AppPlatformInputMetrics::RecordInputEventsAppKMForApp(
const std::string& app_id,
const EventSourceToCounts& event_counts) {
for (const auto& counts : event_counts) {
InputEventSource event_source = counts.first;
// `counts.second` is the map from AppTypeName to the event count.
for (const auto& count : counts.second) {
auto source_id = AppPlatformMetrics::GetSourceId(profile_, app_id);
if (source_id == ukm::kInvalidSourceId) {
continue;
}
ukm::builders::ChromeOSApp_InputEvent builder(source_id);
builder.SetAppType((int)count.first)
.SetAppInputEventSource((int)event_source)
.SetAppInputEventCount(count.second)
.SetUserDeviceMatrix(GetUserTypeByDeviceTypeMetrics())
.Record(ukm::UkmRecorder::Get());
AppPlatformMetrics::RemoveSourceId(source_id);
}
}
}
void AppPlatformInputMetrics::SaveInputEvents() {
ScopedDictPrefUpdate input_events_update(profile_->GetPrefs(),
kAppInputEventsKey);
input_events_update->clear();
for (const auto& event_counts : app_id_to_event_count_per_two_hours_) {
input_events_update->SetByDottedPath(
event_counts.first, ConvertEventCountsToValue(event_counts.second));
}
}
void AppPlatformInputMetrics::RecordInputEventsAppKMFromPref() {
if (!ShouldRecordAppKM(profile_)) {
return;
}
ScopedDictPrefUpdate input_events_update(profile_->GetPrefs(),
kAppInputEventsKey);
for (const auto [app_id, events] : *input_events_update) {
if (!ShouldRecordAppKMForApp(app_id)) {
continue;
}
const base::Value::Dict* events_dict = events.GetIfDict();
if (!events_dict) {
continue;
}
EventSourceToCounts event_counts =
ConvertDictValueToEventCounts(*events_dict);
RecordInputEventsAppKMForApp(app_id, event_counts);
}
}
bool AppPlatformInputMetrics::ShouldRecordAppKMForApp(
const std::string& app_id) {
return ShouldRecordAppKMForAppId(profile_, app_registry_cache_.get(),
app_id) &&
ShouldRecordAppKMForAppTypeName(GetAppType(profile_, app_id));
}
} // namespace apps
|