blob: 63d8d70ef305802ed4c70fc1719658d85b0545e1 [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>
Willie Koomson07b723fd2021-02-17 21:35:3010#include <utility>
lhchavezdb94ad72016-11-12 04:08:4711#include <vector>
12
cnwan1ed447862016-03-21 08:00:1013#include "base/macros.h"
14#include "base/memory/weak_ptr.h"
Willie Koomson8223faa2020-10-20 22:02:4115#include "base/observer_list.h"
16#include "base/observer_list_types.h"
Hidehiko Abe3c446ed2017-10-11 10:31:2517#include "base/optional.h"
cnwan1ed447862016-03-21 08:00:1018#include "base/threading/thread_checker.h"
Shao-Chuan Lee678d55df2018-11-22 07:27:5119#include "base/time/time.h"
cnwan1ed447862016-03-21 08:00:1020#include "base/timer/timer.h"
Miyoung Shin314259e2019-08-05 06:41:4821#include "components/arc/mojom/metrics.mojom.h"
22#include "components/arc/mojom/process.mojom.h"
Michael Matenaa92cfe682019-11-22 16:23:5423#include "components/arc/session/arc_bridge_service.h"
Yusuke Satod41a2c92019-03-26 00:55:1424#include "components/arc/session/connection_observer.h"
Timothy Lohf2c18512019-09-18 04:07:3725#include "components/guest_os/guest_os_engagement_metrics.h"
Hidehiko Abef7f59ab62017-07-16 12:56:5826#include "components/keyed_service/core/keyed_service.h"
Tetsui Ohkubof06ccfc2019-05-14 21:42:2727#include "ui/events/ozone/gamepad/gamepad_observer.h"
Shao-Chuan Lee2fa23802018-09-21 07:29:4628#include "ui/wm/public/activation_change_observer.h"
Maajide30800a2018-07-27 15:36:5329
Shao-Chuan Lee678d55df2018-11-22 07:27:5130class BrowserContextKeyedServiceFactory;
Shao-Chuan Lee678d55df2018-11-22 07:27:5131
Maajide30800a2018-07-27 15:36:5332namespace aura {
33class Window;
34} // namespace aura
cnwan1ed447862016-03-21 08:00:1035
Hidehiko Abef7f59ab62017-07-16 12:56:5836namespace content {
37class BrowserContext;
38} // namespace content
39
cnwan1ed447862016-03-21 08:00:1040namespace arc {
41
Michael Matenaa92cfe682019-11-22 16:23:5442namespace mojom {
43class AppInstance;
44class IntentHelperInstance;
45} // namespace mojom
yusukes883612b2016-10-13 18:07:0546
cnwan1ed447862016-03-21 08:00:1047// Collects information from other ArcServices and send UMA metrics.
Hidehiko Abeac2e5512017-11-21 09:54:4648class ArcMetricsService : public KeyedService,
Shao-Chuan Lee2fa23802018-09-21 07:29:4649 public wm::ActivationChangeObserver,
Tetsui Ohkubof06ccfc2019-05-14 21:42:2750 public mojom::MetricsHost,
51 public ui::GamepadObserver {
cnwan1ed447862016-03-21 08:00:1052 public:
Michael Matenaa92cfe682019-11-22 16:23:5453 using HistogramNamer =
54 base::RepeatingCallback<std::string(const std::string& base_name)>;
55
Willie Koomson8223faa2020-10-20 22:02:4156 class AppKillObserver : public base::CheckedObserver {
57 public:
58 virtual void OnArcLowMemoryKill() = 0;
59 virtual void OnArcOOMKillCount(unsigned long count) = 0;
60 virtual void OnArcMetricsServiceDestroyed() {}
61 };
62
Hidehiko Abef7f59ab62017-07-16 12:56:5863 // Returns singleton instance for the given BrowserContext,
64 // or nullptr if the browser |context| is not allowed to use ARC.
65 static ArcMetricsService* GetForBrowserContext(
66 content::BrowserContext* context);
yusukesc77383a2017-12-11 17:05:0067 static ArcMetricsService* GetForBrowserContextForTesting(
68 content::BrowserContext* context);
Hidehiko Abef7f59ab62017-07-16 12:56:5869
Shao-Chuan Lee678d55df2018-11-22 07:27:5170 // Returns factory instance for this class.
71 static BrowserContextKeyedServiceFactory* GetFactory();
72
Hidehiko Abef7f59ab62017-07-16 12:56:5873 ArcMetricsService(content::BrowserContext* context,
74 ArcBridgeService* bridge_service);
cnwan1ed447862016-03-21 08:00:1075 ~ArcMetricsService() override;
76
Willie Koomson8223faa2020-10-20 22:02:4177 // KeyedService overrides.
78 void Shutdown() override;
79
Michael Matenaa92cfe682019-11-22 16:23:5480 // Sets the histogram namer. Required to not have a dependency on browser
81 // codebase.
82 void SetHistogramNamer(HistogramNamer histogram_namer);
83
Hidehiko Abeac2e5512017-11-21 09:54:4684 // Implementations for ConnectionObserver<mojom::ProcessInstance>.
85 void OnProcessConnectionReady();
86 void OnProcessConnectionClosed();
cnwan1ed447862016-03-21 08:00:1087
cywang020b77ac2016-04-27 06:13:0988 // MetricsHost overrides.
yusukesd6ea165b2017-05-26 23:26:0489 void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events,
90 mojom::BootType boot_type) override;
Lev Rumyantsevdeae3252017-12-06 04:57:0591 void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override;
Shengsong Tanc76913a2020-04-23 02:46:5192 void ReportCompanionLibApiUsage(mojom::CompanionLibApiId api_id) override;
Willie Koomson8223faa2020-10-20 22:02:4193 void ReportAppKill(mojom::AppKillPtr app_kill) override;
Vaibhav Raheja61ecbac22020-11-03 23:27:2194 void ReportArcCorePriAbiMigEvent(
95 mojom::ArcCorePriAbiMigEvent event_type) override;
96 void ReportArcCorePriAbiMigFailedTries(uint32_t failed_attempts) override;
97 void ReportArcCorePriAbiMigDowngradeDelay(base::TimeDelta delay) override;
98 void ReportArcCorePriAbiMigBootTime(base::TimeDelta duration) override;
Lev Rumyantsevdeae3252017-12-06 04:57:0599
Shao-Chuan Lee2fa23802018-09-21 07:29:46100 // wm::ActivationChangeObserver overrides.
Maajide30800a2018-07-27 15:36:53101 // Records to UMA when a user has interacted with an ARC app window.
Shao-Chuan Lee2fa23802018-09-21 07:29:46102 void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
103 aura::Window* gained_active,
104 aura::Window* lost_active) override;
Maajide30800a2018-07-27 15:36:53105
Tetsui Ohkubof06ccfc2019-05-14 21:42:27106 // ui::GamepadObserver overrides.
107 void OnGamepadEvent(const ui::GamepadEvent& event) override;
108
Shao-Chuan Lee678d55df2018-11-22 07:27:51109 // ArcAppListPrefs::Observer callbacks which are called through
110 // ArcMetricsServiceProxy.
111 void OnTaskCreated(int32_t task_id,
112 const std::string& package_name,
113 const std::string& activity,
114 const std::string& intent);
115 void OnTaskDestroyed(int32_t task_id);
116
Willie Koomson8223faa2020-10-20 22:02:41117 void AddAppKillObserver(AppKillObserver* obs);
118 void RemoveAppKillObserver(AppKillObserver* obs);
119
Willie Koomson07b723fd2021-02-17 21:35:30120 // Finds the boot_progress_arc_upgraded event, removes it from |events|, and
121 // returns the event time. If the boot_progress_arc_upgraded event is not
122 // found, base::nullopt is returned. This function is public for testing
123 // purposes.
124 base::Optional<base::TimeTicks> GetArcStartTimeFromEvents(
125 std::vector<mojom::BootProgressEventPtr>& events);
126
cnwan1ed447862016-03-21 08:00:10127 private:
lhchavezde2de962016-07-13 04:43:01128 // Adapter to be able to also observe ProcessInstance events.
Hidehiko Abeac2e5512017-11-21 09:54:46129 class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> {
lhchavezde2de962016-07-13 04:43:01130 public:
131 explicit ProcessObserver(ArcMetricsService* arc_metrics_service);
132 ~ProcessObserver() override;
133
134 private:
Hidehiko Abeac2e5512017-11-21 09:54:46135 // ConnectionObserver<mojom::ProcessInstance> overrides.
136 void OnConnectionReady() override;
137 void OnConnectionClosed() override;
lhchavezde2de962016-07-13 04:43:01138
139 ArcMetricsService* arc_metrics_service_;
yusukesdbfd3952017-01-16 02:48:56140
141 DISALLOW_COPY_AND_ASSIGN(ProcessObserver);
lhchavezde2de962016-07-13 04:43:01142 };
143
Michael Matenaa92cfe682019-11-22 16:23:54144 class ArcBridgeServiceObserver : public arc::ArcBridgeService::Observer {
145 public:
146 ArcBridgeServiceObserver();
147 ~ArcBridgeServiceObserver() override;
148
149 // Whether the arc bridge is in the process of closing.
150 bool arc_bridge_closing_ = false;
151
152 private:
153 // arc::ArcBridgeService::Observer overrides.
154 void BeforeArcBridgeClosed() override;
155 void AfterArcBridgeClosed() override;
156 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceObserver);
157 };
158
159 class IntentHelperObserver
160 : public ConnectionObserver<mojom::IntentHelperInstance> {
161 public:
162 IntentHelperObserver(ArcMetricsService* arc_metrics_service,
163 ArcBridgeServiceObserver* arc_bridge_service_observer);
164 ~IntentHelperObserver() override;
165
166 private:
167 // arc::internal::ConnectionObserver<mojom::IntentHelperInstance>
168 // overrides.
169 void OnConnectionClosed() override;
170
171 ArcMetricsService* arc_metrics_service_;
172 ArcBridgeServiceObserver* arc_bridge_service_observer_;
173
174 DISALLOW_COPY_AND_ASSIGN(IntentHelperObserver);
175 };
176
177 class AppLauncherObserver : public ConnectionObserver<mojom::AppInstance> {
178 public:
179 AppLauncherObserver(ArcMetricsService* arc_metrics_service,
180 ArcBridgeServiceObserver* arc_bridge_service_observer);
181 ~AppLauncherObserver() override;
182
183 private:
184 // arc::internal::ConnectionObserver<mojom::IntentHelperInstance>
185 // overrides.
186 void OnConnectionClosed() override;
187
188 ArcMetricsService* arc_metrics_service_;
189 ArcBridgeServiceObserver* arc_bridge_service_observer_;
190
191 DISALLOW_COPY_AND_ASSIGN(AppLauncherObserver);
192 };
193
Hidehiko Abea0cfefae2017-06-22 19:30:11194 void RequestProcessList();
195 void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes);
196
197 // DBus callbacks.
Hidehiko Abed0c8e462017-11-22 06:02:17198 void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events,
199 mojom::BootType boot_type,
200 base::Optional<base::TimeTicks> arc_start_time);
Vaibhav Raheja61ecbac22020-11-03 23:27:21201 void OnArcStartTimeForPriAbiMigration(
202 base::TimeTicks durationTicks,
203 base::Optional<base::TimeTicks> arc_start_time);
Hidehiko Abea0cfefae2017-06-22 19:30:11204
Willie Koomson8223faa2020-10-20 22:02:41205 // Notify AppKillObservers.
206 void NotifyLowMemoryKill();
207 void NotifyOOMKillCount(unsigned long count);
208
Hidehiko Abea0cfefae2017-06-22 19:30:11209 THREAD_CHECKER(thread_checker_);
210
Hidehiko Abef7f59ab62017-07-16 12:56:58211 ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
Timothy Loh888a10d42019-08-12 06:41:24212
Timothy Lohf2c18512019-09-18 04:07:37213 // Helper class for tracking engagement metrics.
214 guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_;
Hidehiko Abef7f59ab62017-07-16 12:56:58215
Michael Matenaa92cfe682019-11-22 16:23:54216 // A function that appends a suffix to the base of a histogram name based on
217 // the current user profile.
218 HistogramNamer histogram_namer_;
219
lhchavezde2de962016-07-13 04:43:01220 ProcessObserver process_observer_;
Shao-Chuan Lee678d55df2018-11-22 07:27:51221 base::RepeatingTimer request_process_list_timer_;
cnwan1ed447862016-03-21 08:00:10222
Michael Matenaa92cfe682019-11-22 16:23:54223 ArcBridgeServiceObserver arc_bridge_service_observer_;
224 IntentHelperObserver intent_helper_observer_;
225 AppLauncherObserver app_launcher_observer_;
226
Shao-Chuan Lee678d55df2018-11-22 07:27:51227 bool was_arc_window_active_ = false;
228 std::vector<int32_t> task_ids_;
229
Tetsui Ohkubof06ccfc2019-05-14 21:42:27230 bool gamepad_interaction_recorded_ = false;
231
Willie Koomson8223faa2020-10-20 22:02:41232 base::ObserverList<AppKillObserver> app_kill_observers_;
233
cnwan1ed447862016-03-21 08:00:10234 // Always keep this the last member of this class to make sure it's the
235 // first thing to be destructed.
Jeremy Roman47d432e2019-08-20 14:24:00236 base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this};
cnwan1ed447862016-03-21 08:00:10237
238 DISALLOW_COPY_AND_ASSIGN(ArcMetricsService);
239};
240
241} // namespace arc
242
lhchavezde2de962016-07-13 04:43:01243#endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_