OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef METRICS_DAEMON_H_ | 5 #ifndef METRICS_DAEMON_H_ |
6 #define METRICS_DAEMON_H_ | 6 #define METRICS_DAEMON_H_ |
7 | 7 |
8 #include <dbus/dbus.h> | 8 #include <dbus/dbus.h> |
9 #include <glib.h> | 9 #include <glib.h> |
10 #include <time.h> | |
11 | 10 |
12 #include "metrics_library.h" | 11 #include "metrics_library.h" |
13 | 12 |
14 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 13 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 #include <base/time.h> |
15 | 15 |
16 class MetricsDaemon { | 16 class MetricsDaemon { |
17 | 17 |
18 public: | 18 public: |
19 MetricsDaemon() | 19 MetricsDaemon() |
20 : daily_use_record_file_(NULL), | 20 : daily_use_record_file_(NULL), |
21 network_state_(kUnknownNetworkState), | 21 network_state_(kUnknownNetworkState), |
22 network_state_last_(0), | |
23 power_state_(kUnknownPowerState), | 22 power_state_(kUnknownPowerState), |
24 screensaver_state_(kUnknownScreenSaverState), | 23 screensaver_state_(kUnknownScreenSaverState), |
25 session_state_(kUnknownSessionState), | 24 session_state_(kUnknownSessionState), |
26 user_active_(false), | 25 user_active_(false), |
27 user_active_last_(0), | |
28 daily_use_day_last_(0), | 26 daily_use_day_last_(0), |
29 usemon_interval_(0), | 27 usemon_interval_(0), |
30 usemon_source_(NULL) {} | 28 usemon_source_(NULL) {} |
31 ~MetricsDaemon() {} | 29 ~MetricsDaemon() {} |
32 | 30 |
33 // Initializes. | 31 // Initializes. |
34 void Init(bool testing, MetricsLibraryInterface* metrics_lib); | 32 void Init(bool testing, MetricsLibraryInterface* metrics_lib); |
35 | 33 |
36 // Does all the work. If |run_as_daemon| is true, daemonizes by | 34 // Does all the work. If |run_as_daemon| is true, daemonizes by |
37 // forking. | 35 // forking. |
(...skipping 11 matching lines...) Expand all Loading... |
49 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); | 47 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); |
50 FRIEND_TEST(MetricsDaemonTest, MessageFilter); | 48 FRIEND_TEST(MetricsDaemonTest, MessageFilter); |
51 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); | 49 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); |
52 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); | 50 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); |
53 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); | 51 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); |
54 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); | 52 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); |
55 FRIEND_TEST(MetricsDaemonTest, SendMetric); | 53 FRIEND_TEST(MetricsDaemonTest, SendMetric); |
56 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); | 54 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); |
57 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnLogin); | 55 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnLogin); |
58 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnMonitor); | 56 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnMonitor); |
| 57 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateTimeJump); |
59 | 58 |
60 // The network states (see network_states.h). | 59 // The network states (see network_states.h). |
61 enum NetworkState { | 60 enum NetworkState { |
62 kUnknownNetworkState = -1, // Initial/unknown network state. | 61 kUnknownNetworkState = -1, // Initial/unknown network state. |
63 #define STATE(name, capname) kNetworkState ## capname, | 62 #define STATE(name, capname) kNetworkState ## capname, |
64 #include "network_states.h" | 63 #include "network_states.h" |
65 kNumberNetworkStates | 64 kNumberNetworkStates |
66 }; | 65 }; |
67 | 66 |
68 // The power states (see power_states.h). | 67 // The power states (see power_states.h). |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 123 |
125 // Creates the event loop and enters it. | 124 // Creates the event loop and enters it. |
126 void Loop(); | 125 void Loop(); |
127 | 126 |
128 // D-Bus filter callback. | 127 // D-Bus filter callback. |
129 static DBusHandlerResult MessageFilter(DBusConnection* connection, | 128 static DBusHandlerResult MessageFilter(DBusConnection* connection, |
130 DBusMessage* message, | 129 DBusMessage* message, |
131 void* user_data); | 130 void* user_data); |
132 | 131 |
133 // Processes network state change. | 132 // Processes network state change. |
134 void NetStateChanged(const char* state_name, time_t now); | 133 void NetStateChanged(const char* state_name, base::TimeTicks ticks); |
135 | 134 |
136 // Given the state name, returns the state id. | 135 // Given the state name, returns the state id. |
137 NetworkState LookupNetworkState(const char* state_name); | 136 NetworkState LookupNetworkState(const char* state_name); |
138 | 137 |
139 // Processes power state change. | 138 // Processes power state change. |
140 void PowerStateChanged(const char* state_name, time_t now); | 139 void PowerStateChanged(const char* state_name, base::Time now); |
141 | 140 |
142 // Given the state name, returns the state id. | 141 // Given the state name, returns the state id. |
143 PowerState LookupPowerState(const char* state_name); | 142 PowerState LookupPowerState(const char* state_name); |
144 | 143 |
145 // Processes screen-saver state change. | 144 // Processes screen-saver state change. |
146 void ScreenSaverStateChanged(const char* state_name, time_t now); | 145 void ScreenSaverStateChanged(const char* state_name, base::Time now); |
147 | 146 |
148 // Given the state name, returns the state id. | 147 // Given the state name, returns the state id. |
149 ScreenSaverState LookupScreenSaverState(const char* state_name); | 148 ScreenSaverState LookupScreenSaverState(const char* state_name); |
150 | 149 |
151 // Processes user session state change. | 150 // Processes user session state change. |
152 void SessionStateChanged(const char* state_name, time_t now); | 151 void SessionStateChanged(const char* state_name, base::Time now); |
153 | 152 |
154 // Given the state name, returns the state id. | 153 // Given the state name, returns the state id. |
155 SessionState LookupSessionState(const char* state_name); | 154 SessionState LookupSessionState(const char* state_name); |
156 | 155 |
157 // Updates the user-active state to |active| and logs the usage data | 156 // Updates the user-active state to |active| and logs the usage data |
158 // since the last update. If the user has just become active, | 157 // since the last update. If the user has just become active, |
159 // reschedule the daily use monitor for more frequent updates -- | 158 // reschedule the daily use monitor for more frequent updates -- |
160 // this is followed by an exponential back-off (see UseMonitor). | 159 // this is followed by an exponential back-off (see UseMonitor). |
161 void SetUserActiveState(bool active, time_t now); | 160 // While in active use, this method should be called at intervals no |
| 161 // longer than kUseMonitorIntervalMax otherwise new use time will be |
| 162 // discarded. |
| 163 void SetUserActiveState(bool active, base::Time now); |
162 | 164 |
163 // Updates the daily usage file, if necessary, by adding |seconds| | 165 // Updates the daily usage file, if necessary, by adding |seconds| |
164 // of active use to the |day| since Epoch. If there's usage data for | 166 // of active use to the |day| since Epoch. If there's usage data for |
165 // day in the past in the usage file, that data is sent to UMA and | 167 // day in the past in the usage file, that data is sent to UMA and |
166 // removed from the file. If there's already usage data for |day| in | 168 // removed from the file. If there's already usage data for |day| in |
167 // the usage file, the |seconds| are accumulated. | 169 // the usage file, the |seconds| are accumulated. |
168 void LogDailyUseRecord(int day, int seconds); | 170 void LogDailyUseRecord(int day, int seconds); |
169 | 171 |
170 // Callbacks for the daily use monitor. The daily use monitor uses | 172 // Callbacks for the daily use monitor. The daily use monitor uses |
171 // LogDailyUseRecord to aggregate current usage data and send it to | 173 // LogDailyUseRecord to aggregate current usage data and send it to |
(...skipping 26 matching lines...) Expand all Loading... |
198 bool testing_; | 200 bool testing_; |
199 | 201 |
200 // The metrics library handle. | 202 // The metrics library handle. |
201 MetricsLibraryInterface* metrics_lib_; | 203 MetricsLibraryInterface* metrics_lib_; |
202 | 204 |
203 const char* daily_use_record_file_; | 205 const char* daily_use_record_file_; |
204 | 206 |
205 // Current network state. | 207 // Current network state. |
206 NetworkState network_state_; | 208 NetworkState network_state_; |
207 | 209 |
208 // Timestamps last network state update. | 210 // Timestamps last network state update. This timestamp is used to |
209 time_t network_state_last_; | 211 // sample the time from the network going online to going offline so |
| 212 // TimeTicks ensures a monotonically increasing TimeDelta. |
| 213 base::TimeTicks network_state_last_; |
210 | 214 |
211 // Current power state. | 215 // Current power state. |
212 PowerState power_state_; | 216 PowerState power_state_; |
213 | 217 |
214 // Current screen-saver state. | 218 // Current screen-saver state. |
215 ScreenSaverState screensaver_state_; | 219 ScreenSaverState screensaver_state_; |
216 | 220 |
217 // Current user session state. | 221 // Current user session state. |
218 SessionState session_state_; | 222 SessionState session_state_; |
219 | 223 |
220 // Is the user currently active: power is on, user session has | 224 // Is the user currently active: power is on, user session has |
221 // started, screen is not locked. | 225 // started, screen is not locked. |
222 bool user_active_; | 226 bool user_active_; |
223 | 227 |
224 // Timestamps last user active update. | 228 // Timestamps last user active update. Active use time is |
225 time_t user_active_last_; | 229 // aggregated each day before sending to UMA so using time since the |
| 230 // epoch as the timestamp. |
| 231 base::Time user_active_last_; |
226 | 232 |
227 // Last stored daily use day (since epoch). | 233 // Last stored daily use day (since the epoch). |
228 int daily_use_day_last_; | 234 int daily_use_day_last_; |
229 | 235 |
230 // Sleep period until the next daily usage aggregation performed by | 236 // Sleep period until the next daily usage aggregation performed by |
231 // the daily use monitor (see ScheduleUseMonitor). | 237 // the daily use monitor (see ScheduleUseMonitor). |
232 int usemon_interval_; | 238 int usemon_interval_; |
233 | 239 |
234 // Scheduled daily use monitor source (see ScheduleUseMonitor). | 240 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
235 GSource* usemon_source_; | 241 GSource* usemon_source_; |
236 }; | 242 }; |
237 | 243 |
238 #endif // METRICS_DAEMON_H_ | 244 #endif // METRICS_DAEMON_H_ |
OLD | NEW |