blob: 34b5dcb96a11fb731a90e38f609ce1a4c68b5dc2 [file] [log] [blame]
cnwan1ed447862016-03-21 08:00:101// Copyright 2016 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
lhchavezde2de962016-07-13 04:43:015#ifndef COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_
6#define COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_
cnwan1ed447862016-03-21 08:00:107
Maajide30800a2018-07-27 15:36:538#include <memory>
Yusuke Satob4885ff2019-03-26 17:03:369#include <string>
lhchavezdb94ad72016-11-12 04:08:4710#include <vector>
11
cnwan1ed447862016-03-21 08:00:1012#include "base/macros.h"
13#include "base/memory/weak_ptr.h"
Hidehiko Abe3c446ed2017-10-11 10:31:2514#include "base/optional.h"
cnwan1ed447862016-03-21 08:00:1015#include "base/threading/thread_checker.h"
Shao-Chuan Lee678d55df2018-11-22 07:27:5116#include "base/time/time.h"
cnwan1ed447862016-03-21 08:00:1017#include "base/timer/timer.h"
Miyoung Shin314259e2019-08-05 06:41:4818#include "components/arc/mojom/metrics.mojom.h"
19#include "components/arc/mojom/process.mojom.h"
Michael Matenaa92cfe682019-11-22 16:23:5420#include "components/arc/session/arc_bridge_service.h"
Yusuke Satod41a2c92019-03-26 00:55:1421#include "components/arc/session/connection_observer.h"
Timothy Lohf2c18512019-09-18 04:07:3722#include "components/guest_os/guest_os_engagement_metrics.h"
Hidehiko Abef7f59ab62017-07-16 12:56:5823#include "components/keyed_service/core/keyed_service.h"
Tetsui Ohkubof06ccfc2019-05-14 21:42:2724#include "ui/events/ozone/gamepad/gamepad_observer.h"
Shao-Chuan Lee2fa23802018-09-21 07:29:4625#include "ui/wm/public/activation_change_observer.h"
Maajide30800a2018-07-27 15:36:5326
Shao-Chuan Lee678d55df2018-11-22 07:27:5127class BrowserContextKeyedServiceFactory;
Shao-Chuan Lee678d55df2018-11-22 07:27:5128
Maajide30800a2018-07-27 15:36:5329namespace aura {
30class Window;
31} // namespace aura
cnwan1ed447862016-03-21 08:00:1032
Hidehiko Abef7f59ab62017-07-16 12:56:5833namespace content {
34class BrowserContext;
35} // namespace content
36
cnwan1ed447862016-03-21 08:00:1037namespace arc {
38
Michael Matenaa92cfe682019-11-22 16:23:5439namespace mojom {
40class AppInstance;
41class IntentHelperInstance;
42} // namespace mojom
yusukes883612b2016-10-13 18:07:0543
cnwan1ed447862016-03-21 08:00:1044// Collects information from other ArcServices and send UMA metrics.
Hidehiko Abeac2e5512017-11-21 09:54:4645class ArcMetricsService : public KeyedService,
Shao-Chuan Lee2fa23802018-09-21 07:29:4646 public wm::ActivationChangeObserver,
Tetsui Ohkubof06ccfc2019-05-14 21:42:2747 public mojom::MetricsHost,
48 public ui::GamepadObserver {
cnwan1ed447862016-03-21 08:00:1049 public:
Michael Matenaa92cfe682019-11-22 16:23:5450 using HistogramNamer =
51 base::RepeatingCallback<std::string(const std::string& base_name)>;
52
Hidehiko Abef7f59ab62017-07-16 12:56:5853 // Returns singleton instance for the given BrowserContext,
54 // or nullptr if the browser |context| is not allowed to use ARC.
55 static ArcMetricsService* GetForBrowserContext(
56 content::BrowserContext* context);
yusukesc77383a2017-12-11 17:05:0057 static ArcMetricsService* GetForBrowserContextForTesting(
58 content::BrowserContext* context);
Hidehiko Abef7f59ab62017-07-16 12:56:5859
Shao-Chuan Lee678d55df2018-11-22 07:27:5160 // Returns factory instance for this class.
61 static BrowserContextKeyedServiceFactory* GetFactory();
62
Hidehiko Abef7f59ab62017-07-16 12:56:5863 ArcMetricsService(content::BrowserContext* context,
64 ArcBridgeService* bridge_service);
cnwan1ed447862016-03-21 08:00:1065 ~ArcMetricsService() override;
66
Michael Matenaa92cfe682019-11-22 16:23:5467 // Sets the histogram namer. Required to not have a dependency on browser
68 // codebase.
69 void SetHistogramNamer(HistogramNamer histogram_namer);
70
Hidehiko Abeac2e5512017-11-21 09:54:4671 // Implementations for ConnectionObserver<mojom::ProcessInstance>.
72 void OnProcessConnectionReady();
73 void OnProcessConnectionClosed();
cnwan1ed447862016-03-21 08:00:1074
cywang020b77ac2016-04-27 06:13:0975 // MetricsHost overrides.
yusukesd6ea165b2017-05-26 23:26:0476 void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events,
77 mojom::BootType boot_type) override;
Lev Rumyantsevdeae3252017-12-06 04:57:0578 void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override;
79
Shao-Chuan Lee2fa23802018-09-21 07:29:4680 // wm::ActivationChangeObserver overrides.
Maajide30800a2018-07-27 15:36:5381 // Records to UMA when a user has interacted with an ARC app window.
Shao-Chuan Lee2fa23802018-09-21 07:29:4682 void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
83 aura::Window* gained_active,
84 aura::Window* lost_active) override;
Maajide30800a2018-07-27 15:36:5385
Tetsui Ohkubof06ccfc2019-05-14 21:42:2786 // ui::GamepadObserver overrides.
87 void OnGamepadEvent(const ui::GamepadEvent& event) override;
88
Shao-Chuan Lee678d55df2018-11-22 07:27:5189 // ArcAppListPrefs::Observer callbacks which are called through
90 // ArcMetricsServiceProxy.
91 void OnTaskCreated(int32_t task_id,
92 const std::string& package_name,
93 const std::string& activity,
94 const std::string& intent);
95 void OnTaskDestroyed(int32_t task_id);
96
cnwan1ed447862016-03-21 08:00:1097 private:
lhchavezde2de962016-07-13 04:43:0198 // Adapter to be able to also observe ProcessInstance events.
Hidehiko Abeac2e5512017-11-21 09:54:4699 class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> {
lhchavezde2de962016-07-13 04:43:01100 public:
101 explicit ProcessObserver(ArcMetricsService* arc_metrics_service);
102 ~ProcessObserver() override;
103
104 private:
Hidehiko Abeac2e5512017-11-21 09:54:46105 // ConnectionObserver<mojom::ProcessInstance> overrides.
106 void OnConnectionReady() override;
107 void OnConnectionClosed() override;
lhchavezde2de962016-07-13 04:43:01108
109 ArcMetricsService* arc_metrics_service_;
yusukesdbfd3952017-01-16 02:48:56110
111 DISALLOW_COPY_AND_ASSIGN(ProcessObserver);
lhchavezde2de962016-07-13 04:43:01112 };
113
Michael Matenaa92cfe682019-11-22 16:23:54114 class ArcBridgeServiceObserver : public arc::ArcBridgeService::Observer {
115 public:
116 ArcBridgeServiceObserver();
117 ~ArcBridgeServiceObserver() override;
118
119 // Whether the arc bridge is in the process of closing.
120 bool arc_bridge_closing_ = false;
121
122 private:
123 // arc::ArcBridgeService::Observer overrides.
124 void BeforeArcBridgeClosed() override;
125 void AfterArcBridgeClosed() override;
126 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceObserver);
127 };
128
129 class IntentHelperObserver
130 : public ConnectionObserver<mojom::IntentHelperInstance> {
131 public:
132 IntentHelperObserver(ArcMetricsService* arc_metrics_service,
133 ArcBridgeServiceObserver* arc_bridge_service_observer);
134 ~IntentHelperObserver() override;
135
136 private:
137 // arc::internal::ConnectionObserver<mojom::IntentHelperInstance>
138 // overrides.
139 void OnConnectionClosed() override;
140
141 ArcMetricsService* arc_metrics_service_;
142 ArcBridgeServiceObserver* arc_bridge_service_observer_;
143
144 DISALLOW_COPY_AND_ASSIGN(IntentHelperObserver);
145 };
146
147 class AppLauncherObserver : public ConnectionObserver<mojom::AppInstance> {
148 public:
149 AppLauncherObserver(ArcMetricsService* arc_metrics_service,
150 ArcBridgeServiceObserver* arc_bridge_service_observer);
151 ~AppLauncherObserver() override;
152
153 private:
154 // arc::internal::ConnectionObserver<mojom::IntentHelperInstance>
155 // overrides.
156 void OnConnectionClosed() override;
157
158 ArcMetricsService* arc_metrics_service_;
159 ArcBridgeServiceObserver* arc_bridge_service_observer_;
160
161 DISALLOW_COPY_AND_ASSIGN(AppLauncherObserver);
162 };
163
Hidehiko Abea0cfefae2017-06-22 19:30:11164 void RequestProcessList();
165 void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes);
166
167 // DBus callbacks.
Hidehiko Abed0c8e462017-11-22 06:02:17168 void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events,
169 mojom::BootType boot_type,
170 base::Optional<base::TimeTicks> arc_start_time);
Hidehiko Abea0cfefae2017-06-22 19:30:11171
Hidehiko Abea0cfefae2017-06-22 19:30:11172 THREAD_CHECKER(thread_checker_);
173
Hidehiko Abef7f59ab62017-07-16 12:56:58174 ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
Timothy Loh888a10d42019-08-12 06:41:24175
Timothy Lohf2c18512019-09-18 04:07:37176 // Helper class for tracking engagement metrics.
177 guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_;
Hidehiko Abef7f59ab62017-07-16 12:56:58178
Michael Matenaa92cfe682019-11-22 16:23:54179 // A function that appends a suffix to the base of a histogram name based on
180 // the current user profile.
181 HistogramNamer histogram_namer_;
182
lhchavezde2de962016-07-13 04:43:01183 ProcessObserver process_observer_;
Shao-Chuan Lee678d55df2018-11-22 07:27:51184 base::RepeatingTimer request_process_list_timer_;
cnwan1ed447862016-03-21 08:00:10185
Michael Matenaa92cfe682019-11-22 16:23:54186 ArcBridgeServiceObserver arc_bridge_service_observer_;
187 IntentHelperObserver intent_helper_observer_;
188 AppLauncherObserver app_launcher_observer_;
189
Shao-Chuan Lee678d55df2018-11-22 07:27:51190 bool was_arc_window_active_ = false;
191 std::vector<int32_t> task_ids_;
192
Tetsui Ohkubof06ccfc2019-05-14 21:42:27193 bool gamepad_interaction_recorded_ = false;
194
cnwan1ed447862016-03-21 08:00:10195 // Always keep this the last member of this class to make sure it's the
196 // first thing to be destructed.
Jeremy Roman47d432e2019-08-20 14:24:00197 base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this};
cnwan1ed447862016-03-21 08:00:10198
199 DISALLOW_COPY_AND_ASSIGN(ArcMetricsService);
200};
201
202} // namespace arc
203
lhchavezde2de962016-07-13 04:43:01204#endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_