cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 1 | // 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 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 5 | #ifndef COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |
| 6 | #define COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 7 | |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 8 | #include <memory> |
Yusuke Sato | b4885ff | 2019-03-26 17:03:36 | [diff] [blame] | 9 | #include <string> |
Willie Koomson | 07b723fd | 2021-02-17 21:35:30 | [diff] [blame] | 10 | #include <utility> |
lhchavez | db94ad7 | 2016-11-12 04:08:47 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 13 | #include "base/macros.h" |
| 14 | #include "base/memory/weak_ptr.h" |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 15 | #include "base/observer_list.h" |
| 16 | #include "base/observer_list_types.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 17 | #include "base/threading/thread_checker.h" |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 18 | #include "base/time/time.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 19 | #include "base/timer/timer.h" |
Yusuke Sato | d58d302f | 2021-05-07 04:37:26 | [diff] [blame] | 20 | #include "components/arc/arc_browser_context_keyed_service_factory_base.h" |
Yusuke Sato | 6954fb8d | 2021-05-05 19:21:40 | [diff] [blame] | 21 | #include "components/arc/metrics/arc_metrics_constants.h" |
Miyoung Shin | 314259e | 2019-08-05 06:41:48 | [diff] [blame] | 22 | #include "components/arc/mojom/metrics.mojom.h" |
| 23 | #include "components/arc/mojom/process.mojom.h" |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 24 | #include "components/arc/session/arc_bridge_service.h" |
Yusuke Sato | d41a2c9 | 2019-03-26 00:55:14 | [diff] [blame] | 25 | #include "components/arc/session/connection_observer.h" |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 26 | #include "components/guest_os/guest_os_engagement_metrics.h" |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 27 | #include "components/keyed_service/core/keyed_service.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 28 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 29 | #include "ui/events/ozone/gamepad/gamepad_observer.h" |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 30 | #include "ui/wm/public/activation_change_observer.h" |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 31 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 32 | class BrowserContextKeyedServiceFactory; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 33 | |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 34 | namespace aura { |
| 35 | class Window; |
| 36 | } // namespace aura |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 37 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 38 | namespace content { |
| 39 | class BrowserContext; |
| 40 | } // namespace content |
| 41 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 42 | namespace arc { |
| 43 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 44 | namespace mojom { |
| 45 | class AppInstance; |
| 46 | class IntentHelperInstance; |
| 47 | } // namespace mojom |
yusukes | 883612b | 2016-10-13 18:07:05 | [diff] [blame] | 48 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 49 | // Collects information from other ArcServices and send UMA metrics. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 50 | class ArcMetricsService : public KeyedService, |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 51 | public wm::ActivationChangeObserver, |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 52 | public mojom::MetricsHost, |
| 53 | public ui::GamepadObserver { |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 54 | public: |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 55 | using HistogramNamer = |
| 56 | base::RepeatingCallback<std::string(const std::string& base_name)>; |
| 57 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 58 | 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 Sato | 0bc79f6 | 2021-05-05 23:46:45 | [diff] [blame] | 65 | class UserInteractionObserver : public base::CheckedObserver { |
| 66 | public: |
| 67 | virtual void OnUserInteraction(UserInteractionType type) = 0; |
| 68 | }; |
| 69 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 70 | // 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); |
yusukes | c77383a | 2017-12-11 17:05:00 | [diff] [blame] | 74 | static ArcMetricsService* GetForBrowserContextForTesting( |
| 75 | content::BrowserContext* context); |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 76 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 77 | // Returns factory instance for this class. |
| 78 | static BrowserContextKeyedServiceFactory* GetFactory(); |
| 79 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 80 | ArcMetricsService(content::BrowserContext* context, |
| 81 | ArcBridgeService* bridge_service); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 82 | ~ArcMetricsService() override; |
| 83 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 84 | // KeyedService overrides. |
| 85 | void Shutdown() override; |
| 86 | |
Yusuke Sato | 6954fb8d | 2021-05-05 19:21:40 | [diff] [blame] | 87 | // Records one of Arc.UserInteraction UMA stats. |context| cannot be null. |
| 88 | static void RecordArcUserInteraction(content::BrowserContext* context, |
| 89 | UserInteractionType type); |
| 90 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 91 | // Sets the histogram namer. Required to not have a dependency on browser |
| 92 | // codebase. |
| 93 | void SetHistogramNamer(HistogramNamer histogram_namer); |
| 94 | |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 95 | // Implementations for ConnectionObserver<mojom::ProcessInstance>. |
| 96 | void OnProcessConnectionReady(); |
| 97 | void OnProcessConnectionClosed(); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 98 | |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 99 | // MetricsHost overrides. |
yusukes | d6ea165b | 2017-05-26 23:26:04 | [diff] [blame] | 100 | void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events, |
| 101 | mojom::BootType boot_type) override; |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame] | 102 | void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override; |
Shengsong Tan | c76913a | 2020-04-23 02:46:51 | [diff] [blame] | 103 | void ReportCompanionLibApiUsage(mojom::CompanionLibApiId api_id) override; |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 104 | void ReportAppKill(mojom::AppKillPtr app_kill) override; |
Vaibhav Raheja | 61ecbac2 | 2020-11-03 23:27:21 | [diff] [blame] | 105 | 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 Ohkubo | 971a915e | 2021-02-22 07:26:52 | [diff] [blame] | 110 | void ReportClipboardDragDropEvent( |
| 111 | mojom::ArcClipboardDragDropEvent event_type) override; |
Yury Khmel | 35a0bf8 | 2021-05-10 23:45:49 | [diff] [blame] | 112 | void ReportAnr(mojom::AnrPtr anr) override; |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame] | 113 | |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 114 | // wm::ActivationChangeObserver overrides. |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 115 | // Records to UMA when a user has interacted with an ARC app window. |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 116 | void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason, |
| 117 | aura::Window* gained_active, |
| 118 | aura::Window* lost_active) override; |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 119 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 120 | // ui::GamepadObserver overrides. |
| 121 | void OnGamepadEvent(const ui::GamepadEvent& event) override; |
| 122 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 123 | // 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 Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 131 | void AddAppKillObserver(AppKillObserver* obs); |
| 132 | void RemoveAppKillObserver(AppKillObserver* obs); |
| 133 | |
Yusuke Sato | 0bc79f6 | 2021-05-05 23:46:45 | [diff] [blame] | 134 | void AddUserInteractionObserver(UserInteractionObserver* obs); |
| 135 | void RemoveUserInteractionObserver(UserInteractionObserver* obs); |
| 136 | |
Willie Koomson | 07b723fd | 2021-02-17 21:35:30 | [diff] [blame] | 137 | // 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 Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 139 | // found, absl::nullopt is returned. This function is public for testing |
Willie Koomson | 07b723fd | 2021-02-17 21:35:30 | [diff] [blame] | 140 | // purposes. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 141 | absl::optional<base::TimeTicks> GetArcStartTimeFromEvents( |
Willie Koomson | 07b723fd | 2021-02-17 21:35:30 | [diff] [blame] | 142 | std::vector<mojom::BootProgressEventPtr>& events); |
| 143 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 144 | private: |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 145 | // Adapter to be able to also observe ProcessInstance events. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 146 | class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> { |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 147 | public: |
| 148 | explicit ProcessObserver(ArcMetricsService* arc_metrics_service); |
| 149 | ~ProcessObserver() override; |
| 150 | |
| 151 | private: |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 152 | // ConnectionObserver<mojom::ProcessInstance> overrides. |
| 153 | void OnConnectionReady() override; |
| 154 | void OnConnectionClosed() override; |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 155 | |
| 156 | ArcMetricsService* arc_metrics_service_; |
yusukes | dbfd395 | 2017-01-16 02:48:56 | [diff] [blame] | 157 | |
| 158 | DISALLOW_COPY_AND_ASSIGN(ProcessObserver); |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 159 | }; |
| 160 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 161 | 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 Sato | 6954fb8d | 2021-05-05 19:21:40 | [diff] [blame] | 211 | void RecordArcUserInteraction(UserInteractionType type); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 212 | void RequestProcessList(); |
| 213 | void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes); |
| 214 | |
| 215 | // DBus callbacks. |
Hidehiko Abe | d0c8e46 | 2017-11-22 06:02:17 | [diff] [blame] | 216 | void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events, |
| 217 | mojom::BootType boot_type, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 218 | absl::optional<base::TimeTicks> arc_start_time); |
Vaibhav Raheja | 61ecbac2 | 2020-11-03 23:27:21 | [diff] [blame] | 219 | void OnArcStartTimeForPriAbiMigration( |
| 220 | base::TimeTicks durationTicks, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 221 | absl::optional<base::TimeTicks> arc_start_time); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 222 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 223 | // Notify AppKillObservers. |
| 224 | void NotifyLowMemoryKill(); |
| 225 | void NotifyOOMKillCount(unsigned long count); |
| 226 | |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 227 | THREAD_CHECKER(thread_checker_); |
| 228 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 229 | ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager. |
Timothy Loh | 888a10d4 | 2019-08-12 06:41:24 | [diff] [blame] | 230 | |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 231 | // Helper class for tracking engagement metrics. |
| 232 | guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_; |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 233 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 234 | // 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 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 238 | ProcessObserver process_observer_; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 239 | base::RepeatingTimer request_process_list_timer_; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 240 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 241 | ArcBridgeServiceObserver arc_bridge_service_observer_; |
| 242 | IntentHelperObserver intent_helper_observer_; |
| 243 | AppLauncherObserver app_launcher_observer_; |
| 244 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 245 | bool was_arc_window_active_ = false; |
| 246 | std::vector<int32_t> task_ids_; |
| 247 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 248 | bool gamepad_interaction_recorded_ = false; |
| 249 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 250 | base::ObserverList<AppKillObserver> app_kill_observers_; |
Yusuke Sato | 0bc79f6 | 2021-05-05 23:46:45 | [diff] [blame] | 251 | base::ObserverList<UserInteractionObserver> user_interaction_observers_; |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 252 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 253 | // Always keep this the last member of this class to make sure it's the |
| 254 | // first thing to be destructed. |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 255 | base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this}; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 256 | |
| 257 | DISALLOW_COPY_AND_ASSIGN(ArcMetricsService); |
| 258 | }; |
| 259 | |
Yusuke Sato | d58d302f | 2021-05-07 04:37:26 | [diff] [blame] | 260 | // Singleton factory for ArcMetricsService. |
| 261 | class 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 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 277 | } // namespace arc |
| 278 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 279 | #endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |