app_list_feature_usage_metrics.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/app_list/app_list_feature_usage_metrics.h"
  5. #include <memory>
  6. #include "ash/app_list/app_list_controller_impl.h"
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/session/session_controller_impl.h"
  9. #include "ash/shell.h"
  10. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  11. #include "chromeos/ash/components/feature_usage/feature_usage_metrics.h"
  12. namespace ash {
  13. namespace {
  14. // Feature name for clamshell mode launcher.
  15. constexpr char kClamshellLauncher[] = "ClamshellLauncher";
  16. // Feature name for tablet mode launcher.
  17. constexpr char kTabletLauncher[] = "TabletLauncher";
  18. // Supplies eligibility information for clamshell launcher.
  19. class ClamshellDelegate : public feature_usage::FeatureUsageMetrics::Delegate {
  20. public:
  21. ClamshellDelegate() = default;
  22. ClamshellDelegate(const ClamshellDelegate&) = delete;
  23. ClamshellDelegate& operator=(const ClamshellDelegate&) = delete;
  24. ~ClamshellDelegate() override = default;
  25. // feature_usage::FeatureUsageMetrics::Delegate:
  26. bool IsEligible() const override {
  27. // Kiosk app sessions cannot use the launcher.
  28. return !Shell::Get()->session_controller()->IsRunningInAppMode();
  29. }
  30. bool IsEnabled() const override {
  31. // The launcher is always enabled for eligible sessions.
  32. return IsEligible();
  33. }
  34. };
  35. // Supplies eligibility information for tablet launcher.
  36. class TabletDelegate : public feature_usage::FeatureUsageMetrics::Delegate {
  37. public:
  38. TabletDelegate() = default;
  39. TabletDelegate(const TabletDelegate&) = delete;
  40. TabletDelegate& operator=(const TabletDelegate&) = delete;
  41. ~TabletDelegate() override = default;
  42. // feature_usage::FeatureUsageMetrics::Delegate:
  43. bool IsEligible() const override {
  44. // Kiosk app sessions cannot use the launcher.
  45. if (Shell::Get()->session_controller()->IsRunningInAppMode())
  46. return false;
  47. // If the device cannot enter tablet mode, the tablet mode launcher is not
  48. // available.
  49. return Shell::Get()->tablet_mode_controller()->CanEnterTabletMode();
  50. }
  51. bool IsEnabled() const override {
  52. // The launcher is always enabled for eligible sessions.
  53. return IsEligible();
  54. }
  55. };
  56. } // namespace
  57. AppListFeatureUsageMetrics::AppListFeatureUsageMetrics()
  58. : clamshell_delegate_(std::make_unique<ClamshellDelegate>()),
  59. clamshell_metrics_(kClamshellLauncher, clamshell_delegate_.get()),
  60. tablet_delegate_(std::make_unique<TabletDelegate>()),
  61. tablet_metrics_(kTabletLauncher, tablet_delegate_.get()) {
  62. Shell::Get()->app_list_controller()->AddObserver(this);
  63. Shell::Get()->tablet_mode_controller()->AddObserver(this);
  64. }
  65. AppListFeatureUsageMetrics::~AppListFeatureUsageMetrics() {
  66. Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  67. Shell::Get()->app_list_controller()->RemoveObserver(this);
  68. }
  69. void AppListFeatureUsageMetrics::OnTabletModeStarted() {
  70. // The legacy launcher can transition directly from clamshell to tablet mode
  71. // without triggering a visibility update. ProductivityLauncher explicitly
  72. // closes the clamshell launcher before showing the tablet launcher.
  73. if (is_using_clamshell_ && !features::IsProductivityLauncherEnabled()) {
  74. StopClamshellUsage();
  75. if (Shell::Get()->app_list_controller()->IsVisible())
  76. StartTabletUsage();
  77. }
  78. }
  79. void AppListFeatureUsageMetrics::OnAppListVisibilityChanged(
  80. bool shown,
  81. int64_t display_id) {
  82. if (shown) {
  83. OnAppListShown();
  84. } else {
  85. OnAppListHidden();
  86. }
  87. }
  88. void AppListFeatureUsageMetrics::OnAppListShown() {
  89. if (Shell::Get()->IsInTabletMode()) {
  90. StartTabletUsage();
  91. } else {
  92. StartClamshellUsage();
  93. }
  94. }
  95. void AppListFeatureUsageMetrics::OnAppListHidden() {
  96. StopTabletUsage();
  97. StopClamshellUsage();
  98. }
  99. void AppListFeatureUsageMetrics::StartClamshellUsage() {
  100. if (is_using_clamshell_)
  101. return;
  102. DCHECK(clamshell_delegate_->IsEligible());
  103. clamshell_metrics_.RecordUsage(true);
  104. clamshell_metrics_.StartSuccessfulUsage();
  105. is_using_clamshell_ = true;
  106. }
  107. void AppListFeatureUsageMetrics::StopClamshellUsage() {
  108. if (!is_using_clamshell_)
  109. return;
  110. clamshell_metrics_.StopSuccessfulUsage();
  111. is_using_clamshell_ = false;
  112. }
  113. void AppListFeatureUsageMetrics::StartTabletUsage() {
  114. if (is_using_tablet_)
  115. return;
  116. // Ignore users on ineligible devices who have forced tablet mode via flags
  117. // or debug shortcut keys. FeatureUsageMetrics requires that RecordUsage()
  118. // is never called when IsEligible() is false.
  119. if (!tablet_delegate_->IsEligible())
  120. return;
  121. tablet_metrics_.RecordUsage(true);
  122. tablet_metrics_.StartSuccessfulUsage();
  123. is_using_tablet_ = true;
  124. }
  125. void AppListFeatureUsageMetrics::StopTabletUsage() {
  126. if (!is_using_tablet_)
  127. return;
  128. tablet_metrics_.StopSuccessfulUsage();
  129. is_using_tablet_ = false;
  130. }
  131. } // namespace ash