blob: 48873bf38fd5c4e8b9cfddae02fe124f2bfec599 [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>
lhchavezdb94ad72016-11-12 04:08:4710#include <vector>
11
cnwan1ed447862016-03-21 08:00:1012#include "base/macros.h"
13#include "base/memory/weak_ptr.h"
Hidehiko Abe3c446ed2017-10-11 10:31:2514#include "base/optional.h"
cnwan1ed447862016-03-21 08:00:1015#include "base/threading/thread_checker.h"
Shao-Chuan Lee678d55df2018-11-22 07:27:5116#include "base/time/time.h"
cnwan1ed447862016-03-21 08:00:1017#include "base/timer/timer.h"
Steven Bennetts3330b9f2019-03-15 20:24:1318#include "chromeos/dbus/power/power_manager_client.h"
Miyoung Shin314259e2019-08-05 06:41:4819#include "components/arc/mojom/metrics.mojom.h"
20#include "components/arc/mojom/process.mojom.h"
Yusuke Satod41a2c92019-03-26 00:55:1421#include "components/arc/session/connection_observer.h"
Hidehiko Abef7f59ab62017-07-16 12:56:5822#include "components/keyed_service/core/keyed_service.h"
Shao-Chuan Lee678d55df2018-11-22 07:27:5123#include "components/session_manager/core/session_manager_observer.h"
Tetsui Ohkubof06ccfc2019-05-14 21:42:2724#include "ui/events/ozone/gamepad/gamepad_observer.h"
Shao-Chuan Lee2fa23802018-09-21 07:29:4625#include "ui/wm/public/activation_change_observer.h"
Maajide30800a2018-07-27 15:36:5326
Shao-Chuan Lee678d55df2018-11-22 07:27:5127class BrowserContextKeyedServiceFactory;
28class PrefService;
29
Maajide30800a2018-07-27 15:36:5330namespace aura {
31class Window;
32} // namespace aura
cnwan1ed447862016-03-21 08:00:1033
Shao-Chuan Lee678d55df2018-11-22 07:27:5134namespace base {
35class Clock;
36class TickClock;
37} // namespace base
38
Hidehiko Abef7f59ab62017-07-16 12:56:5839namespace content {
40class BrowserContext;
41} // namespace content
42
cnwan1ed447862016-03-21 08:00:1043namespace arc {
44
yusukes883612b2016-10-13 18:07:0545class ArcBridgeService;
46
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,
Shao-Chuan Lee678d55df2018-11-22 07:27:5150 public session_manager::SessionManagerObserver,
51 public chromeos::PowerManagerClient::Observer,
Tetsui Ohkubof06ccfc2019-05-14 21:42:2752 public mojom::MetricsHost,
53 public ui::GamepadObserver {
cnwan1ed447862016-03-21 08:00:1054 public:
Timothy Loh888a10d42019-08-12 06:41:2455 using WindowMatcher = base::RepeatingCallback<bool(const aura::Window*)>;
Maajide30800a2018-07-27 15:36:5356
Timothy Loh888a10d42019-08-12 06:41:2457 // Sets the fake WindowMatcher for testing.
58 void SetWindowMatcherForTesting(WindowMatcher window_matcher);
Maajide30800a2018-07-27 15:36:5359
Shao-Chuan Lee678d55df2018-11-22 07:27:5160 // Sets Clock for testing.
61 void SetClockForTesting(base::Clock* clock);
62
63 // Sets TickClock for testing.
64 void SetTickClockForTesting(base::TickClock* tick_clock);
65
Hidehiko Abef7f59ab62017-07-16 12:56:5866 // Returns singleton instance for the given BrowserContext,
67 // or nullptr if the browser |context| is not allowed to use ARC.
68 static ArcMetricsService* GetForBrowserContext(
69 content::BrowserContext* context);
yusukesc77383a2017-12-11 17:05:0070 static ArcMetricsService* GetForBrowserContextForTesting(
71 content::BrowserContext* context);
Hidehiko Abef7f59ab62017-07-16 12:56:5872
Shao-Chuan Lee678d55df2018-11-22 07:27:5173 // Returns factory instance for this class.
74 static BrowserContextKeyedServiceFactory* GetFactory();
75
Hidehiko Abef7f59ab62017-07-16 12:56:5876 ArcMetricsService(content::BrowserContext* context,
77 ArcBridgeService* bridge_service);
cnwan1ed447862016-03-21 08:00:1078 ~ArcMetricsService() override;
79
Hidehiko Abeac2e5512017-11-21 09:54:4680 // Implementations for ConnectionObserver<mojom::ProcessInstance>.
81 void OnProcessConnectionReady();
82 void OnProcessConnectionClosed();
cnwan1ed447862016-03-21 08:00:1083
cywang020b77ac2016-04-27 06:13:0984 // MetricsHost overrides.
yusukesd6ea165b2017-05-26 23:26:0485 void ReportBootProgress(std::vector<mojom::BootProgressEventPtr> events,
86 mojom::BootType boot_type) override;
Lev Rumyantsevdeae3252017-12-06 04:57:0587 void ReportNativeBridge(mojom::NativeBridgeType native_bridge_type) override;
88
Shao-Chuan Lee2fa23802018-09-21 07:29:4689 // wm::ActivationChangeObserver overrides.
Maajide30800a2018-07-27 15:36:5390 // Records to UMA when a user has interacted with an ARC app window.
Shao-Chuan Lee2fa23802018-09-21 07:29:4691 void OnWindowActivated(wm::ActivationChangeObserver::ActivationReason reason,
92 aura::Window* gained_active,
93 aura::Window* lost_active) override;
Maajide30800a2018-07-27 15:36:5394
Shao-Chuan Lee678d55df2018-11-22 07:27:5195 // session_manager::SessionManagerObserver overrides.
96 void OnSessionStateChanged() override;
97
98 // chromeos::PowerManagerClient::Observer overrides.
99 void ScreenIdleStateChanged(
100 const power_manager::ScreenIdleState& proto) override;
101
Tetsui Ohkubof06ccfc2019-05-14 21:42:27102 // ui::GamepadObserver overrides.
103 void OnGamepadEvent(const ui::GamepadEvent& event) override;
104
Shao-Chuan Lee678d55df2018-11-22 07:27:51105 // ArcAppListPrefs::Observer callbacks which are called through
106 // ArcMetricsServiceProxy.
107 void OnTaskCreated(int32_t task_id,
108 const std::string& package_name,
109 const std::string& activity,
110 const std::string& intent);
111 void OnTaskDestroyed(int32_t task_id);
112
cnwan1ed447862016-03-21 08:00:10113 private:
lhchavezde2de962016-07-13 04:43:01114 // Adapter to be able to also observe ProcessInstance events.
Hidehiko Abeac2e5512017-11-21 09:54:46115 class ProcessObserver : public ConnectionObserver<mojom::ProcessInstance> {
lhchavezde2de962016-07-13 04:43:01116 public:
117 explicit ProcessObserver(ArcMetricsService* arc_metrics_service);
118 ~ProcessObserver() override;
119
120 private:
Hidehiko Abeac2e5512017-11-21 09:54:46121 // ConnectionObserver<mojom::ProcessInstance> overrides.
122 void OnConnectionReady() override;
123 void OnConnectionClosed() override;
lhchavezde2de962016-07-13 04:43:01124
125 ArcMetricsService* arc_metrics_service_;
yusukesdbfd3952017-01-16 02:48:56126
127 DISALLOW_COPY_AND_ASSIGN(ProcessObserver);
lhchavezde2de962016-07-13 04:43:01128 };
129
Hidehiko Abea0cfefae2017-06-22 19:30:11130 void RequestProcessList();
131 void ParseProcessList(std::vector<mojom::RunningAppProcessInfoPtr> processes);
132
133 // DBus callbacks.
Hidehiko Abed0c8e462017-11-22 06:02:17134 void OnArcStartTimeRetrieved(std::vector<mojom::BootProgressEventPtr> events,
135 mojom::BootType boot_type,
136 base::Optional<base::TimeTicks> arc_start_time);
Hidehiko Abea0cfefae2017-06-22 19:30:11137
Shao-Chuan Lee678d55df2018-11-22 07:27:51138 // Restores accumulated ARC++ engagement time in previous sessions from
139 // profile preferences.
140 void RestoreEngagementTimeFromPrefs();
141
142 // Called periodically to save accumulated results to profile preferences.
143 void SaveEngagementTimeToPrefs();
144
145 // Called whenever engagement state is changed. Time spent in last state is
146 // accumulated to corresponding metrics.
147 void UpdateEngagementTime();
148
149 // Records accumulated engagement time metrics to UMA if necessary (i.e. day
150 // has changed).
151 void RecordEngagementTimeToUmaIfNeeded();
152
153 // Resets accumulated engagement times to zero, and updates both OS version
154 // and day ID.
155 void ResetEngagementTimePrefs();
156
157 bool ShouldAccumulateEngagementTotalTime() const;
158 bool ShouldAccumulateEngagementForegroundTime() const;
159 bool ShouldAccumulateEngagementBackgroundTime() const;
160 bool ShouldRecordEngagementTimeToUma() const;
161
Hidehiko Abea0cfefae2017-06-22 19:30:11162 THREAD_CHECKER(thread_checker_);
163
Hidehiko Abef7f59ab62017-07-16 12:56:58164 ArcBridgeService* const arc_bridge_service_; // Owned by ArcServiceManager.
Timothy Loh888a10d42019-08-12 06:41:24165
166 // A function to determine if a window is an ARC window, which can be
167 // replaced in tests.
168 WindowMatcher window_matcher_;
Hidehiko Abef7f59ab62017-07-16 12:56:58169
lhchavezde2de962016-07-13 04:43:01170 ProcessObserver process_observer_;
Shao-Chuan Lee678d55df2018-11-22 07:27:51171 base::RepeatingTimer request_process_list_timer_;
cnwan1ed447862016-03-21 08:00:10172
Shao-Chuan Lee678d55df2018-11-22 07:27:51173 PrefService* const pref_service_;
174 const base::Clock* clock_;
175 const base::TickClock* tick_clock_;
176 base::RepeatingTimer update_engagement_time_timer_;
177 base::RepeatingTimer save_engagement_time_to_prefs_timer_;
178 base::TimeTicks last_update_ticks_;
179
180 // States for determining which engagement metrics should we accumulate to.
181 bool was_session_active_ = false;
182 bool was_screen_dimmed_ = false;
183 bool was_arc_window_active_ = false;
184 std::vector<int32_t> task_ids_;
185
186 // Accumulated results and associated state which are saved to profile
187 // preferences at fixed interval.
188 int day_id_ = 0;
189 base::TimeDelta engagement_time_total_;
190 base::TimeDelta engagement_time_foreground_;
191 base::TimeDelta engagement_time_background_;
192
Tetsui Ohkubof06ccfc2019-05-14 21:42:27193 bool gamepad_interaction_recorded_ = false;
194
cnwan1ed447862016-03-21 08:00:10195 // Always keep this the last member of this class to make sure it's the
196 // first thing to be destructed.
197 base::WeakPtrFactory<ArcMetricsService> weak_ptr_factory_;
198
199 DISALLOW_COPY_AND_ASSIGN(ArcMetricsService);
200};
201
202} // namespace arc
203
lhchavezde2de962016-07-13 04:43:01204#endif // COMPONENTS_ARC_METRICS_ARC_METRICS_SERVICE_H_