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> | 10 #include <time.h> |
11 | 11 |
| 12 #include "metrics_library.h" |
| 13 |
12 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 14 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
13 | 15 |
14 class MetricsDaemon { | 16 class MetricsDaemon { |
15 | 17 |
16 public: | 18 public: |
17 MetricsDaemon() | 19 MetricsDaemon() |
18 : daily_use_record_file_(NULL), | 20 : daily_use_record_file_(NULL), |
19 network_state_(kUnknownNetworkState), | 21 network_state_(kUnknownNetworkState), |
20 network_state_last_(0), | 22 network_state_last_(0), |
21 power_state_(kUnknownPowerState), | 23 power_state_(kUnknownPowerState), |
22 screensaver_state_(kUnknownScreenSaverState), | 24 screensaver_state_(kUnknownScreenSaverState), |
23 session_state_(kUnknownSessionState), | 25 session_state_(kUnknownSessionState), |
24 user_active_(false), | 26 user_active_(false), |
25 user_active_last_(0), | 27 user_active_last_(0), |
26 daily_use_day_last_(0), | 28 daily_use_day_last_(0), |
27 usemon_interval_(0), | 29 usemon_interval_(0), |
28 usemon_source_(NULL) {} | 30 usemon_source_(NULL) {} |
29 ~MetricsDaemon() {} | 31 ~MetricsDaemon() {} |
30 | 32 |
31 // Does all the work. If |run_as_daemon| is true, daemonizes by | 33 // Does all the work. If |run_as_daemon| is true, daemonizes by |
32 // forking. | 34 // forking. |
33 void Run(bool run_as_daemon); | 35 void Run(bool run_as_daemon); |
34 | 36 |
35 private: | 37 private: |
36 friend class MetricsDaemonTest; | 38 friend class MetricsDaemonTest; |
37 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecord); | 39 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordBadFileLocation); |
| 40 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordOnLogin); |
| 41 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundDown); |
| 42 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecordRoundUp); |
38 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); | 43 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); |
39 FRIEND_TEST(MetricsDaemonTest, LookupPowerState); | 44 FRIEND_TEST(MetricsDaemonTest, LookupPowerState); |
40 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); | 45 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); |
41 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); | 46 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); |
42 FRIEND_TEST(MetricsDaemonTest, MessageFilter); | 47 FRIEND_TEST(MetricsDaemonTest, MessageFilter); |
43 FRIEND_TEST(MetricsDaemonTest, NetStateChanged); | 48 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSimpleDrop); |
| 49 FRIEND_TEST(MetricsDaemonTest, NetStateChangedSuspend); |
44 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); | 50 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); |
45 FRIEND_TEST(MetricsDaemonTest, PublishMetric); | 51 FRIEND_TEST(MetricsDaemonTest, PublishMetric); |
46 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); | 52 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); |
47 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); | 53 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); |
48 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState); | 54 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnLogin); |
| 55 FRIEND_TEST(MetricsDaemonTest, SetUserActiveStateSendOnMonitor); |
49 | 56 |
50 // The network states (see network_states.h). | 57 // The network states (see network_states.h). |
51 enum NetworkState { | 58 enum NetworkState { |
52 kUnknownNetworkState = -1, // Initial/unknown network state. | 59 kUnknownNetworkState = -1, // Initial/unknown network state. |
53 #define STATE(name, capname) kNetworkState ## capname, | 60 #define STATE(name, capname) kNetworkState ## capname, |
54 #include "network_states.h" | 61 #include "network_states.h" |
55 kNumberNetworkStates | 62 kNumberNetworkStates |
56 }; | 63 }; |
57 | 64 |
58 // The power states (see power_states.h). | 65 // The power states (see power_states.h). |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 // Array of power states. | 113 // Array of power states. |
107 static const char* kPowerStates_[kNumberPowerStates]; | 114 static const char* kPowerStates_[kNumberPowerStates]; |
108 | 115 |
109 // Array of screen-saver states. | 116 // Array of screen-saver states. |
110 static const char* kScreenSaverStates_[kNumberScreenSaverStates]; | 117 static const char* kScreenSaverStates_[kNumberScreenSaverStates]; |
111 | 118 |
112 // Array of user session states. | 119 // Array of user session states. |
113 static const char* kSessionStates_[kNumberSessionStates]; | 120 static const char* kSessionStates_[kNumberSessionStates]; |
114 | 121 |
115 // Initializes. | 122 // Initializes. |
116 void Init(bool testing); | 123 void Init(bool testing, MetricsLibraryInterface* metrics_lib); |
117 | 124 |
118 // Creates the event loop and enters it. | 125 // Creates the event loop and enters it. |
119 void Loop(); | 126 void Loop(); |
120 | 127 |
121 // D-Bus filter callback. | 128 // D-Bus filter callback. |
122 static DBusHandlerResult MessageFilter(DBusConnection* connection, | 129 static DBusHandlerResult MessageFilter(DBusConnection* connection, |
123 DBusMessage* message, | 130 DBusMessage* message, |
124 void* user_data); | 131 void* user_data); |
125 | 132 |
126 // Processes network state change. | 133 // Processes network state change. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 190 |
184 // Sends a stat to Chrome for transport to UMA (or prints it for | 191 // Sends a stat to Chrome for transport to UMA (or prints it for |
185 // testing). See MetricsLibrary::SendToChrome in metrics_library.h | 192 // testing). See MetricsLibrary::SendToChrome in metrics_library.h |
186 // for a description of the arguments. | 193 // for a description of the arguments. |
187 void PublishMetric(const char* name, int sample, | 194 void PublishMetric(const char* name, int sample, |
188 int min, int max, int nbuckets); | 195 int min, int max, int nbuckets); |
189 | 196 |
190 // Test mode. | 197 // Test mode. |
191 bool testing_; | 198 bool testing_; |
192 | 199 |
| 200 // The metrics library handle. |
| 201 MetricsLibraryInterface* metrics_lib_; |
| 202 |
193 const char* daily_use_record_file_; | 203 const char* daily_use_record_file_; |
194 | 204 |
195 // Current network state. | 205 // Current network state. |
196 NetworkState network_state_; | 206 NetworkState network_state_; |
197 | 207 |
198 // Timestamps last network state update. | 208 // Timestamps last network state update. |
199 time_t network_state_last_; | 209 time_t network_state_last_; |
200 | 210 |
201 // Current power state. | 211 // Current power state. |
202 PowerState power_state_; | 212 PowerState power_state_; |
(...skipping 16 matching lines...) Expand all Loading... |
219 | 229 |
220 // Sleep period until the next daily usage aggregation performed by | 230 // Sleep period until the next daily usage aggregation performed by |
221 // the daily use monitor (see ScheduleUseMonitor). | 231 // the daily use monitor (see ScheduleUseMonitor). |
222 int usemon_interval_; | 232 int usemon_interval_; |
223 | 233 |
224 // Scheduled daily use monitor source (see ScheduleUseMonitor). | 234 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
225 GSource* usemon_source_; | 235 GSource* usemon_source_; |
226 }; | 236 }; |
227 | 237 |
228 #endif // METRICS_DAEMON_H_ | 238 #endif // METRICS_DAEMON_H_ |
OLD | NEW |