blob: f855f58f4e9c8fee7f809b6088e2e9f82023b17c [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"
cnwan1ed447862016-03-21 08:00:1017#include "base/threading/thread_checker.h"
Shao-Chuan Lee678d55df2018-11-22 07:27:5118#include "base/time/time.h"
cnwan1ed447862016-03-21 08:00:1019#include "base/timer/timer.h"
Yusuke Satod58d302f2021-05-07 04:37:2620#include "components/arc/arc_browser_context_keyed_service_factory_base.h"
Yusuke Sato6954fb8d2021-05-05 19:21:4021#include "components/arc/metrics/arc_metrics_constants.h"
Miyoung Shin314259e2019-08-05 06:41:4822#include "components/arc/mojom/metrics.mojom.h"
23#include "components/arc/mojom/process.mojom.h"
Michael Matenaa92cfe682019-11-22 16:23:5424#include "components/arc/session/arc_bridge_service.h"
Yusuke Satod41a2c92019-03-26 00:55:1425#include "components/arc/session/connection_observer.h"
Timothy Lohf2c18512019-09-18 04:07:3726#include "components/guest_os/guest_os_engagement_metrics.h"
Hidehiko Abef7f59ab62017-07-16 12:56:5827#include "components/keyed_service/core/keyed_service.h"
Anton Bikineev1156b5f2021-05-15 22:35:3628#include "third_party/abseil-cpp/absl/types/optional.h"
Tetsui Ohkubof06ccfc2019-05-14 21:42:2729#include "ui/events/ozone/gamepad/gamepad_observer.h"
Shao-Chuan Lee2fa23802018-09-21 07:29:4630#include "ui/wm/public/activation_change_observer.h"
Maajide30800a2018-07-27 15:36:5331
Shao-Chuan Lee678d55df2018-11-22 07:27:5132class BrowserContextKeyedServiceFactory;
Shao-Chuan Lee678d55df2018-11-22 07:27:5133
Maajide30800a2018-07-27 15:36:5334namespace aura {
35class Window;
36} // namespace aura
cnwan1ed447862016-03-21 08:00:1037
Hidehiko Abef7f59ab62017-07-16 12:56:5838namespace content {
39class BrowserContext;
40} // namespace content
41
cnwan1ed447862016-03-21 08:00:1042namespace arc {
43
Michael Matenaa92cfe682019-11-22 16:23:5444namespace mojom {
45class AppInstance;
46class IntentHelperInstance;
47} // namespace mojom
yusukes883612b2016-10-13 18:07:0548
cnwan1ed447862016-03-21 08:00:1049// Collects information from other ArcServices and send UMA metrics.
Hidehiko Abeac2e5512017-11-21 09:54:4650class ArcMetricsService : public KeyedService,
Shao-Chuan Lee2fa23802018-09-21 07:29:4651 public wm::ActivationChangeObserver,
Tetsui Ohkubof06ccfc2019-05-14 21:42:2752 public mojom::MetricsHost,
53 public ui::GamepadObserver {
cnwan1ed447862016-03-21 08:00:1054 public:
Michael Matenaa92cfe682019-11-22 16:23:5455 using HistogramNamer =
56 base::RepeatingCallback<std::string(const std::string& base_name)>;
57
Willie Koomson8223faa2020-10-20 22:02:4158 class AppKillObserver : public base::CheckedObserver {
59 public:
60 virtual void OnArcLowMemoryKill() = 0;
61 virtual void OnArcOOMKillCount(unsigned long count) = 0;
62 virtual void OnArcMetricsServiceDestroyed() {}
63 };
64
Yusuke Sato0bc79f62021-05-05 23:46:4565 class UserInteractionObserver : public base::CheckedObserver {
66 public:
67 virtual void OnUserInteraction(UserInteractionType type) = 0;
68 };
69
Hidehiko Abef7f59ab62017-07-16 12:56:5870 // Returns singleton instance for the given BrowserContext,
71 // or nullptr if the browser |context| is not allowed to use ARC.
72 static ArcMetricsService* GetForBrowserContext(
73 content::BrowserContext* context);
yusukesc77383a2017-12-11 17:05:0074 static ArcMetricsService* GetForBrowserContextForTesting(
75 content::BrowserContext* context);
Hidehiko Abef7f59ab62017-07-16 12:56:5876
Shao-Chuan Lee678d55df2018-11-22 07:27:5177 // Returns factory instance for this class.
78 static BrowserContextKeyedServiceFactory* GetFactory();
79
Hidehiko Abef7f59ab62017-07-16 12:56:5880 ArcMetricsService(content::BrowserContext* context,
81 ArcBridgeService* bridge_service);
cnwan1ed447862016-03-21 08:00:1082 ~ArcMetricsService() override;
83
Willie Koomson8223faa2020-10-20 22:02:4184 // KeyedService overrides.
85 void Shutdown() override;
86
Yusuke Sato6954fb8d2021-05-05 19:21:4087 // Records one of Arc.UserInteraction UMA stats. |context| cannot be null.
88 static void RecordArcUserInteraction(content::BrowserContext* context,
89 UserInteractionType type);
90
Michael Matenaa92cfe682019-11-22 16:23:5491 // Sets the histogram namer. Required to not have a dependency on browser
92 // codebase.
93 void SetHistogramNamer(HistogramNamer histogram_namer);
94
Hidehiko Abeac2e5512017-11-21 09:54:4695 // Implementations for ConnectionObserver<mojom::ProcessInstance>.
96 void OnProcessConnectionReady();
97 void OnProcessConnectionClosed();
cnwan1ed447862016-03-21 08:00:1098
cywang020b77ac2016-04-27 06:13:0999 // MetricsHost overrides.
yusukesd6ea165b2017-05-26 23:26:04100 void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events,
101 mojom::BootType boot_type) override;
Lev Rumyantsevdeae3252017-12-06 04:57:05102 void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override;
Shengsong Tanc76913a2020-04-23 02:46:51103 void ReportCompanionLibApiUsage(mojom::CompanionLibApiId api_id) override;
Willie Koomson8223faa2020-10-20 22:02:41104 void ReportAppKill(mojom::AppKillPtr app_kill) override;
Vaibhav Raheja61ecbac22020-11-03 23:27:21105 void ReportArcCorePriAbiMigEvent(
106 mojom::ArcCorePriAbiMigEvent event_type) override;
107 void ReportArcCorePriAbiMigFailedTries(uint32_t failed_attempts) override;
108 void ReportArcCorePriAbiMigDowngradeDelay(base::TimeDelta delay) override;
109 void ReportArcCorePriAbiMigBootTime(base::TimeDelta duration) override;
Tetsui Ohkubo971a915e2021-02-22 07:26:52110 void ReportClipboardDragDropEvent(
111 mojom::ArcClipboardDragDropEvent event_type) override;
Yury Khmel35a0bf82021-05-10 23:45:49112 void ReportAnr(mojom::AnrPtr anr) override;
Lev Rumyantsevdeae3252017-12-06 04:57:05113
Shao-Chuan Lee2fa23802018-09-21 07:29:46114 // wm::ActivationChangeObserver overrides.
Maajide30800a2018-07-27 15:36:53115 // Records to UMA when a user has interacted with an ARC app window.
Shao-Chuan Lee2fa23802018-09-21 07:29:46116 void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
117 aura::Window* gained_active,
118 aura::Window* lost_active) override;
Maajide30800a2018-07-27 15:36:53119
Tetsui Ohkubof06ccfc2019-05-14 21:42:27120 // ui::GamepadObserver overrides.
121 void OnGamepadEvent(const ui::GamepadEvent& event) override;
122
Shao-Chuan Lee678d55df2018-11-22 07:27:51123 // ArcAppListPrefs::Observer callbacks which are called through
124 // ArcMetricsServiceProxy.
125 void OnTaskCreated(int32_t task_id,
126 const std::string& package_name,
127 const std::string& activity,
128 const std::string& intent);
129 void OnTaskDestroyed(int32_t task_id);
130
Willie Koomson8223faa2020-10-20 22:02:41131 void AddAppKillObserver(AppKillObserver* obs);
132 void RemoveAppKillObserver(AppKillObserver* obs);
133
Yusuke Sato0bc79f62021-05-05 23:46:45134 void AddUserInteractionObserver(UserInteractionObserver* obs);
135 void RemoveUserInteractionObserver(UserInteractionObserver* obs);
136
Willie Koomson07b723fd2021-02-17 21:35:30137 // Finds the boot_progress_arc_upgraded event, removes it from |events|, and
138 // returns the event time. If the boot_progress_arc_upgraded event is not
Anton Bikineev1156b5f2021-05-15 22:35:36139 // found, absl::nullopt is returned. This function is public for testing
Willie Koomson07b723fd2021-02-17 21:35:30140 // purposes.
Anton Bikineev1156b5f2021-05-15 22:35:36141 absl::optional<base::TimeTicks> GetArcStartTimeFromEvents(
Willie Koomson07b723fd2021-02-17 21:35:30142 std::vector<mojom::BootProgressEventPtr>& events);
143
cnwan1ed447862016-03-21 08:00:10144 private:
lhchavezde2de962016-07-13 04:43:01145 // Adapter to be able to also observe ProcessInstance events.
Hidehiko Abeac2e5512017-11-21 09:54:46146 class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> {
lhchavezde2de962016-07-13 04:43:01147 public:
148 explicit ProcessObserver(ArcMetricsService* arc_metrics_service);
149 ~ProcessObserver() override;
150
151 private:
Hidehiko Abeac2e5512017-11-21 09:54:46152 // ConnectionObserver<mojom::ProcessInstance> overrides.
153 void OnConnectionReady() override;
154 void OnConnectionClosed() override;
lhchavezde2de962016-07-13 04:43:01155
156 ArcMetricsService* arc_metrics_service_;
yusukesdbfd3952017-01-16 02:48:56157
158 DISALLOW_COPY_AND_ASSIGN(ProcessObserver);
lhchavezde2de962016-07-13 04:43:01159 };
160
Michael Matenaa92cfe682019-11-22 16:23:54161 class ArcBridgeServiceObserver : public arc::ArcBridgeService::Observer {
162 public:
163 ArcBridgeServiceObserver();
164 ~ArcBridgeServiceObserver() override;
165
166 // Whether the arc bridge is in the process of closing.
167 bool arc_bridge_closing_ = false;
168
169 private:
170 // arc::ArcBridgeService::Observer overrides.
171 void BeforeArcBridgeClosed() override;
172 void AfterArcBridgeClosed() override;
173 DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceObserver);
174 };
175
176 class IntentHelperObserver
177 : public ConnectionObserver<mojom::IntentHelperInstance> {
178 public:
179 IntentHelperObserver(ArcMetricsService* arc_metrics_service,
180 ArcBridgeServiceObserver* arc_bridge_service_observer);
181 ~IntentHelperObserver() 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(IntentHelperObserver);
192 };
193
194 class AppLauncherObserver : public ConnectionObserver<mojom::AppInstance> {
195 public:
196 AppLauncherObserver(ArcMetricsService* arc_metrics_service,
197 ArcBridgeServiceObserver* arc_bridge_service_observer);
198 ~AppLauncherObserver() override;
199
200 private:
201 // arc::internal::ConnectionObserver<mojom::IntentHelperInstance>
202 // overrides.
203 void OnConnectionClosed() override;
204
205 ArcMetricsService* arc_metrics_service_;
206 ArcBridgeServiceObserver* arc_bridge_service_observer_;
207
208 DISALLOW_COPY_AND_ASSIGN(AppLauncherObserver);
209 };
210
Yusuke Sato6954fb8d2021-05-05 19:21:40211 void RecordArcUserInteraction(UserInteractionType type);
Hidehiko Abea0cfefae2017-06-22 19:30:11212 void RequestProcessList();
213 void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes);
214
215 // DBus callbacks.
Hidehiko Abed0c8e462017-11-22 06:02:17216 void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events,
217 mojom::BootType boot_type,
Anton Bikineev1156b5f2021-05-15 22:35:36218 absl::optional<base::TimeTicks> arc_start_time);
Vaibhav Raheja61ecbac22020-11-03 23:27:21219 void OnArcStartTimeForPriAbiMigration(
220 base::TimeTicks durationTicks,
Anton Bikineev1156b5f2021-05-15 22:35:36221 absl::optional<base::TimeTicks> arc_start_time);
Hidehiko Abea0cfefae2017-06-22 19:30:11222
Willie Koomson8223faa2020-10-20 22:02:41223 // Notify AppKillObservers.
224 void NotifyLowMemoryKill();
225 void NotifyOOMKillCount(unsigned long count);
226
Hidehiko Abea0cfefae2017-06-22 19:30:11227 THREAD_CHECKER(thread_checker_);
228
Hidehiko Abef7f59ab62017-07-16 12:56:58229 ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
Timothy Loh888a10d42019-08-12 06:41:24230
Timothy Lohf2c18512019-09-18 04:07:37231 // Helper class for tracking engagement metrics.
232 guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_;
Hidehiko Abef7f59ab62017-07-16 12:56:58233
Michael Matenaa92cfe682019-11-22 16:23:54234 // A function that appends a suffix to the base of a histogram name based on
235 // the current user profile.
236 HistogramNamer histogram_namer_;
237
lhchavezde2de962016-07-13 04:43:01238 ProcessObserver process_observer_;
Shao-Chuan Lee678d55df2018-11-22 07:27:51239 base::RepeatingTimer request_process_list_timer_;
cnwan1ed447862016-03-21 08:00:10240
Michael Matenaa92cfe682019-11-22 16:23:54241 ArcBridgeServiceObserver arc_bridge_service_observer_;
242 IntentHelperObserver intent_helper_observer_;
243 AppLauncherObserver app_launcher_observer_;
244
Shao-Chuan Lee678d55df2018-11-22 07:27:51245 bool was_arc_window_active_ = false;
246 std::vector<int32_t> task_ids_;
247
Tetsui Ohkubof06ccfc2019-05-14 21:42:27248 bool gamepad_interaction_recorded_ = false;
249
Willie Koomson8223faa2020-10-20 22:02:41250 base::ObserverList<AppKillObserver> app_kill_observers_;
Yusuke Sato0bc79f62021-05-05 23:46:45251 base::ObserverList<UserInteractionObserver> user_interaction_observers_;
Willie Koomson8223faa2020-10-20 22:02:41252
cnwan1ed447862016-03-21 08:00:10253 // Always keep this the last member of this class to make sure it's the
254 // first thing to be destructed.
Jeremy Roman47d432e2019-08-20 14:24:00255 base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this};
cnwan1ed447862016-03-21 08:00:10256
257 DISALLOW_COPY_AND_ASSIGN(ArcMetricsService);
258};
259
Yusuke Satod58d302f2021-05-07 04:37:26260// Singleton factory for ArcMetricsService.
261class ArcMetricsServiceFactory
262 : public internal::ArcBrowserContextKeyedServiceFactoryBase<
263 ArcMetricsService,
264 ArcMetricsServiceFactory> {
265 public:
266 // Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
267 static constexpr const char* kName = "ArcMetricsServiceFactory";
268
269 static ArcMetricsServiceFactory* GetInstance();
270
271 private:
272 friend base::DefaultSingletonTraits<ArcMetricsServiceFactory>;
273 ArcMetricsServiceFactory() = default;
274 ~ArcMetricsServiceFactory() override = default;
275};
276
cnwan1ed447862016-03-21 08:00:10277} // namespace arc
278
lhchavezde2de962016-07-13 04:43:01279#endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_