File: app_list_feature_usage_metrics.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 4,329 bytes parent folder | download | duplicates (7)
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
// 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 "ash/app_list/app_list_feature_usage_metrics.h"

#include <memory>

#include "ash/app_list/app_list_controller_impl.h"
#include "ash/constants/ash_features.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "chromeos/ash/components/feature_usage/feature_usage_metrics.h"

namespace ash {
namespace {

// Feature name for clamshell mode launcher.
constexpr char kClamshellLauncher[] = "ClamshellLauncher";

// Feature name for tablet mode launcher.
constexpr char kTabletLauncher[] = "TabletLauncher";

// Supplies eligibility information for clamshell launcher.
class ClamshellDelegate : public feature_usage::FeatureUsageMetrics::Delegate {
 public:
  ClamshellDelegate() = default;
  ClamshellDelegate(const ClamshellDelegate&) = delete;
  ClamshellDelegate& operator=(const ClamshellDelegate&) = delete;
  ~ClamshellDelegate() override = default;

  // feature_usage::FeatureUsageMetrics::Delegate:
  bool IsEligible() const override {
    // Kiosk app sessions cannot use the launcher.
    return !Shell::Get()->session_controller()->IsRunningInAppMode();
  }

  bool IsEnabled() const override {
    // The launcher is always enabled for eligible sessions.
    return IsEligible();
  }
};

// Supplies eligibility information for tablet launcher.
class TabletDelegate : public feature_usage::FeatureUsageMetrics::Delegate {
 public:
  TabletDelegate() = default;
  TabletDelegate(const TabletDelegate&) = delete;
  TabletDelegate& operator=(const TabletDelegate&) = delete;
  ~TabletDelegate() override = default;

  // feature_usage::FeatureUsageMetrics::Delegate:
  bool IsEligible() const override {
    // Kiosk app sessions cannot use the launcher.
    if (Shell::Get()->session_controller()->IsRunningInAppMode())
      return false;

    // If the device cannot enter tablet mode, the tablet mode launcher is not
    // available.
    return Shell::Get()->tablet_mode_controller()->CanEnterTabletMode();
  }

  bool IsEnabled() const override {
    // The launcher is always enabled for eligible sessions.
    return IsEligible();
  }
};

}  // namespace

AppListFeatureUsageMetrics::AppListFeatureUsageMetrics()
    : clamshell_delegate_(std::make_unique<ClamshellDelegate>()),
      clamshell_metrics_(kClamshellLauncher, clamshell_delegate_.get()),
      tablet_delegate_(std::make_unique<TabletDelegate>()),
      tablet_metrics_(kTabletLauncher, tablet_delegate_.get()) {
  Shell::Get()->app_list_controller()->AddObserver(this);
}

AppListFeatureUsageMetrics::~AppListFeatureUsageMetrics() {
  Shell::Get()->app_list_controller()->RemoveObserver(this);
}

void AppListFeatureUsageMetrics::OnAppListVisibilityChanged(
    bool shown,
    int64_t display_id) {
  if (shown) {
    OnAppListShown();
  } else {
    OnAppListHidden();
  }
}

void AppListFeatureUsageMetrics::OnAppListShown() {
  if (Shell::Get()->IsInTabletMode()) {
    StartTabletUsage();
  } else {
    StartClamshellUsage();
  }
}

void AppListFeatureUsageMetrics::OnAppListHidden() {
  StopTabletUsage();
  StopClamshellUsage();
}

void AppListFeatureUsageMetrics::StartClamshellUsage() {
  if (is_using_clamshell_)
    return;
  DCHECK(clamshell_delegate_->IsEligible());
  clamshell_metrics_.RecordUsage(true);
  clamshell_metrics_.StartSuccessfulUsage();
  is_using_clamshell_ = true;
}

void AppListFeatureUsageMetrics::StopClamshellUsage() {
  if (!is_using_clamshell_)
    return;
  clamshell_metrics_.StopSuccessfulUsage();
  is_using_clamshell_ = false;
}

void AppListFeatureUsageMetrics::StartTabletUsage() {
  if (is_using_tablet_)
    return;

  // Ignore users on ineligible devices who have forced tablet mode via flags
  // or debug shortcut keys. FeatureUsageMetrics requires that RecordUsage()
  // is never called when IsEligible() is false.
  if (!tablet_delegate_->IsEligible())
    return;

  tablet_metrics_.RecordUsage(true);
  tablet_metrics_.StartSuccessfulUsage();
  is_using_tablet_ = true;
}

void AppListFeatureUsageMetrics::StopTabletUsage() {
  if (!is_using_tablet_)
    return;
  tablet_metrics_.StopSuccessfulUsage();
  is_using_tablet_ = false;
}

}  // namespace ash