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" |
Hidehiko Abe | 3c446ed | 2017-10-11 10:31:25 | [diff] [blame] | 17 | #include "base/optional.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 19 | #include "base/time/time.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 20 | #include "base/timer/timer.h" |
Miyoung Shin | 314259e | 2019-08-05 06:41:48 | [diff] [blame] | 21 | #include "components/arc/mojom/metrics.mojom.h" |
| 22 | #include "components/arc/mojom/process.mojom.h" |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 23 | #include "components/arc/session/arc_bridge_service.h" |
Yusuke Sato | d41a2c9 | 2019-03-26 00:55:14 | [diff] [blame] | 24 | #include "components/arc/session/connection_observer.h" |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 25 | #include "components/guest_os/guest_os_engagement_metrics.h" |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 26 | #include "components/keyed_service/core/keyed_service.h" |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 27 | #include "ui/events/ozone/gamepad/gamepad_observer.h" |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 28 | #include "ui/wm/public/activation_change_observer.h" |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 29 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 30 | class BrowserContextKeyedServiceFactory; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 31 | |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 32 | namespace aura { |
| 33 | class Window; |
| 34 | } // namespace aura |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 35 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 36 | namespace content { |
| 37 | class BrowserContext; |
| 38 | } // namespace content |
| 39 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 40 | namespace arc { |
| 41 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 42 | namespace mojom { |
| 43 | class AppInstance; |
| 44 | class IntentHelperInstance; |
| 45 | } // namespace mojom |
yusukes | 883612b | 2016-10-13 18:07:05 | [diff] [blame] | 46 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 47 | // Collects information from other ArcServices and send UMA metrics. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 48 | class ArcMetricsService : public KeyedService, |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 49 | public wm::ActivationChangeObserver, |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 50 | public mojom::MetricsHost, |
| 51 | public ui::GamepadObserver { |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 52 | public: |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 53 | using HistogramNamer = |
| 54 | base::RepeatingCallback<std::string(const std::string& base_name)>; |
| 55 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 56 | 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 Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 63 | // 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); |
yusukes | c77383a | 2017-12-11 17:05:00 | [diff] [blame] | 67 | static ArcMetricsService* GetForBrowserContextForTesting( |
| 68 | content::BrowserContext* context); |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 69 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 70 | // Returns factory instance for this class. |
| 71 | static BrowserContextKeyedServiceFactory* GetFactory(); |
| 72 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 73 | ArcMetricsService(content::BrowserContext* context, |
| 74 | ArcBridgeService* bridge_service); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 75 | ~ArcMetricsService() override; |
| 76 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 77 | // KeyedService overrides. |
| 78 | void Shutdown() override; |
| 79 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 80 | // Sets the histogram namer. Required to not have a dependency on browser |
| 81 | // codebase. |
| 82 | void SetHistogramNamer(HistogramNamer histogram_namer); |
| 83 | |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 84 | // Implementations for ConnectionObserver<mojom::ProcessInstance>. |
| 85 | void OnProcessConnectionReady(); |
| 86 | void OnProcessConnectionClosed(); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 87 | |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 88 | // MetricsHost overrides. |
yusukes | d6ea165b | 2017-05-26 23:26:04 | [diff] [blame] | 89 | void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events, |
| 90 | mojom::BootType boot_type) override; |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame] | 91 | void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override; |
Shengsong Tan | c76913a | 2020-04-23 02:46:51 | [diff] [blame] | 92 | void ReportCompanionLibApiUsage(mojom::CompanionLibApiId api_id) override; |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 93 | void ReportAppKill(mojom::AppKillPtr app_kill) override; |
Vaibhav Raheja | 61ecbac2 | 2020-11-03 23:27:21 | [diff] [blame] | 94 | 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 Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame] | 99 | |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 100 | // wm::ActivationChangeObserver overrides. |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 101 | // 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] | 102 | void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason, |
| 103 | aura::Window* gained_active, |
| 104 | aura::Window* lost_active) override; |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 105 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 106 | // ui::GamepadObserver overrides. |
| 107 | void OnGamepadEvent(const ui::GamepadEvent& event) override; |
| 108 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 109 | // 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 Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 117 | void AddAppKillObserver(AppKillObserver* obs); |
| 118 | void RemoveAppKillObserver(AppKillObserver* obs); |
| 119 | |
Willie Koomson | 07b723fd | 2021-02-17 21:35:30 | [diff] [blame^] | 120 | // 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 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 127 | private: |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 128 | // Adapter to be able to also observe ProcessInstance events. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 129 | class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> { |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 130 | public: |
| 131 | explicit ProcessObserver(ArcMetricsService* arc_metrics_service); |
| 132 | ~ProcessObserver() override; |
| 133 | |
| 134 | private: |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 135 | // ConnectionObserver<mojom::ProcessInstance> overrides. |
| 136 | void OnConnectionReady() override; |
| 137 | void OnConnectionClosed() override; |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 138 | |
| 139 | ArcMetricsService* arc_metrics_service_; |
yusukes | dbfd395 | 2017-01-16 02:48:56 | [diff] [blame] | 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(ProcessObserver); |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 142 | }; |
| 143 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 144 | 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 Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 194 | void RequestProcessList(); |
| 195 | void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes); |
| 196 | |
| 197 | // DBus callbacks. |
Hidehiko Abe | d0c8e46 | 2017-11-22 06:02:17 | [diff] [blame] | 198 | void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events, |
| 199 | mojom::BootType boot_type, |
| 200 | base::Optional<base::TimeTicks> arc_start_time); |
Vaibhav Raheja | 61ecbac2 | 2020-11-03 23:27:21 | [diff] [blame] | 201 | void OnArcStartTimeForPriAbiMigration( |
| 202 | base::TimeTicks durationTicks, |
| 203 | base::Optional<base::TimeTicks> arc_start_time); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 204 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 205 | // Notify AppKillObservers. |
| 206 | void NotifyLowMemoryKill(); |
| 207 | void NotifyOOMKillCount(unsigned long count); |
| 208 | |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 209 | THREAD_CHECKER(thread_checker_); |
| 210 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 211 | ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager. |
Timothy Loh | 888a10d4 | 2019-08-12 06:41:24 | [diff] [blame] | 212 | |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 213 | // Helper class for tracking engagement metrics. |
| 214 | guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_; |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 215 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 216 | // 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 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 220 | ProcessObserver process_observer_; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 221 | base::RepeatingTimer request_process_list_timer_; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 222 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame] | 223 | ArcBridgeServiceObserver arc_bridge_service_observer_; |
| 224 | IntentHelperObserver intent_helper_observer_; |
| 225 | AppLauncherObserver app_launcher_observer_; |
| 226 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 227 | bool was_arc_window_active_ = false; |
| 228 | std::vector<int32_t> task_ids_; |
| 229 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 230 | bool gamepad_interaction_recorded_ = false; |
| 231 | |
Willie Koomson | 8223faa | 2020-10-20 22:02:41 | [diff] [blame] | 232 | base::ObserverList<AppKillObserver> app_kill_observers_; |
| 233 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 234 | // Always keep this the last member of this class to make sure it's the |
| 235 | // first thing to be destructed. |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 236 | base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this}; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 237 | |
| 238 | DISALLOW_COPY_AND_ASSIGN(ArcMetricsService); |
| 239 | }; |
| 240 | |
| 241 | } // namespace arc |
| 242 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 243 | #endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |