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> |
lhchavez | db94ad7 | 2016-11-12 04:08:47 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/weak_ptr.h" |
Hidehiko Abe | 3c446ed | 2017-10-11 10:31:25 | [diff] [blame] | 14 | #include "base/optional.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 15 | #include "base/threading/thread_checker.h" |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 16 | #include "base/time/time.h" |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 17 | #include "base/timer/timer.h" |
Miyoung Shin | 314259e | 2019-08-05 06:41:48 | [diff] [blame] | 18 | #include "components/arc/mojom/metrics.mojom.h" |
| 19 | #include "components/arc/mojom/process.mojom.h" |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 20 | #include "components/arc/session/arc_bridge_service.h" |
Yusuke Sato | d41a2c9 | 2019-03-26 00:55:14 | [diff] [blame] | 21 | #include "components/arc/session/connection_observer.h" |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 22 | #include "components/guest_os/guest_os_engagement_metrics.h" |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 23 | #include "components/keyed_service/core/keyed_service.h" |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 24 | #include "ui/events/ozone/gamepad/gamepad_observer.h" |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 25 | #include "ui/wm/public/activation_change_observer.h" |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 26 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 27 | class BrowserContextKeyedServiceFactory; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 28 | |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 29 | namespace aura { |
| 30 | class Window; |
| 31 | } // namespace aura |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 32 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 33 | namespace content { |
| 34 | class BrowserContext; |
| 35 | } // namespace content |
| 36 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 37 | namespace arc { |
| 38 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 39 | namespace mojom { |
| 40 | class AppInstance; |
| 41 | class IntentHelperInstance; |
| 42 | } // namespace mojom |
yusukes | 883612b | 2016-10-13 18:07:05 | [diff] [blame] | 43 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 44 | // Collects information from other ArcServices and send UMA metrics. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 45 | class ArcMetricsService : public KeyedService, |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 46 | public wm::ActivationChangeObserver, |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 47 | public mojom::MetricsHost, |
| 48 | public ui::GamepadObserver { |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 49 | public: |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 50 | using HistogramNamer = |
| 51 | base::RepeatingCallback<std::string(const std::string& base_name)>; |
| 52 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 53 | // 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); |
yusukes | c77383a | 2017-12-11 17:05:00 | [diff] [blame] | 57 | static ArcMetricsService* GetForBrowserContextForTesting( |
| 58 | content::BrowserContext* context); |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 59 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 60 | // Returns factory instance for this class. |
| 61 | static BrowserContextKeyedServiceFactory* GetFactory(); |
| 62 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 63 | ArcMetricsService(content::BrowserContext* context, |
| 64 | ArcBridgeService* bridge_service); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 65 | ~ArcMetricsService() override; |
| 66 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 67 | // Sets the histogram namer. Required to not have a dependency on browser |
| 68 | // codebase. |
| 69 | void SetHistogramNamer(HistogramNamer histogram_namer); |
| 70 | |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 71 | // Implementations for ConnectionObserver<mojom::ProcessInstance>. |
| 72 | void OnProcessConnectionReady(); |
| 73 | void OnProcessConnectionClosed(); |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 74 | |
cywang | 020b77ac | 2016-04-27 06:13:09 | [diff] [blame] | 75 | // MetricsHost overrides. |
yusukes | d6ea165b | 2017-05-26 23:26:04 | [diff] [blame] | 76 | void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events, |
| 77 | mojom::BootType boot_type) override; |
Lev Rumyantsev | deae325 | 2017-12-06 04:57:05 | [diff] [blame] | 78 | void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override; |
| 79 | |
Shao-Chuan Lee | 2fa2380 | 2018-09-21 07:29:46 | [diff] [blame] | 80 | // wm::ActivationChangeObserver overrides. |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 81 | // 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] | 82 | void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason, |
| 83 | aura::Window* gained_active, |
| 84 | aura::Window* lost_active) override; |
Maajid | e30800a | 2018-07-27 15:36:53 | [diff] [blame] | 85 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 86 | // ui::GamepadObserver overrides. |
| 87 | void OnGamepadEvent(const ui::GamepadEvent& event) override; |
| 88 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 89 | // 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 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 97 | private: |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 98 | // Adapter to be able to also observe ProcessInstance events. |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 99 | class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> { |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 100 | public: |
| 101 | explicit ProcessObserver(ArcMetricsService* arc_metrics_service); |
| 102 | ~ProcessObserver() override; |
| 103 | |
| 104 | private: |
Hidehiko Abe | ac2e551 | 2017-11-21 09:54:46 | [diff] [blame] | 105 | // ConnectionObserver<mojom::ProcessInstance> overrides. |
| 106 | void OnConnectionReady() override; |
| 107 | void OnConnectionClosed() override; |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 108 | |
| 109 | ArcMetricsService* arc_metrics_service_; |
yusukes | dbfd395 | 2017-01-16 02:48:56 | [diff] [blame] | 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(ProcessObserver); |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 112 | }; |
| 113 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 114 | 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 Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 164 | void RequestProcessList(); |
| 165 | void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes); |
| 166 | |
| 167 | // DBus callbacks. |
Hidehiko Abe | d0c8e46 | 2017-11-22 06:02:17 | [diff] [blame] | 168 | void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events, |
| 169 | mojom::BootType boot_type, |
| 170 | base::Optional<base::TimeTicks> arc_start_time); |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 171 | |
Hidehiko Abe | a0cfefae | 2017-06-22 19:30:11 | [diff] [blame] | 172 | THREAD_CHECKER(thread_checker_); |
| 173 | |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 174 | ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager. |
Timothy Loh | 888a10d4 | 2019-08-12 06:41:24 | [diff] [blame] | 175 | |
Timothy Loh | f2c1851 | 2019-09-18 04:07:37 | [diff] [blame] | 176 | // Helper class for tracking engagement metrics. |
| 177 | guest_os::GuestOsEngagementMetrics guest_os_engagement_metrics_; |
Hidehiko Abe | f7f59ab6 | 2017-07-16 12:56:58 | [diff] [blame] | 178 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 179 | // 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 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 183 | ProcessObserver process_observer_; |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 184 | base::RepeatingTimer request_process_list_timer_; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 185 | |
Michael Matena | a92cfe68 | 2019-11-22 16:23:54 | [diff] [blame^] | 186 | ArcBridgeServiceObserver arc_bridge_service_observer_; |
| 187 | IntentHelperObserver intent_helper_observer_; |
| 188 | AppLauncherObserver app_launcher_observer_; |
| 189 | |
Shao-Chuan Lee | 678d55df | 2018-11-22 07:27:51 | [diff] [blame] | 190 | bool was_arc_window_active_ = false; |
| 191 | std::vector<int32_t> task_ids_; |
| 192 | |
Tetsui Ohkubo | f06ccfc | 2019-05-14 21:42:27 | [diff] [blame] | 193 | bool gamepad_interaction_recorded_ = false; |
| 194 | |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 195 | // Always keep this the last member of this class to make sure it's the |
| 196 | // first thing to be destructed. |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 197 | base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_{this}; |
cnwan | 1ed44786 | 2016-03-21 08:00:10 | [diff] [blame] | 198 | |
| 199 | DISALLOW_COPY_AND_ASSIGN(ArcMetricsService); |
| 200 | }; |
| 201 | |
| 202 | } // namespace arc |
| 203 | |
lhchavez | de2de96 | 2016-07-13 04:43:01 | [diff] [blame] | 204 | #endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_ |