| 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 | 10 |
| 11 #include "metrics_library.h" | 11 #include "metrics_library.h" |
| 12 | 12 |
| 13 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 13 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 14 #include <base/time.h> | 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 power_state_(kUnknownPowerState), | 22 power_state_(kUnknownPowerState), |
| 23 screensaver_state_(kUnknownScreenSaverState), | |
| 24 session_state_(kUnknownSessionState), | 23 session_state_(kUnknownSessionState), |
| 25 user_active_(false), | 24 user_active_(false), |
| 26 daily_use_day_last_(0), | 25 daily_use_day_last_(0), |
| 27 usemon_interval_(0), | 26 usemon_interval_(0), |
| 28 usemon_source_(NULL) {} | 27 usemon_source_(NULL) {} |
| 29 ~MetricsDaemon() {} | 28 ~MetricsDaemon() {} |
| 30 | 29 |
| 31 // Initializes. | 30 // Initializes. |
| 32 void Init(bool testing, MetricsLibraryInterface* metrics_lib); | 31 void Init(bool testing, MetricsLibraryInterface* metrics_lib); |
| 33 | 32 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 }; | 64 }; |
| 66 | 65 |
| 67 // The power states (see power_states.h). | 66 // The power states (see power_states.h). |
| 68 enum PowerState { | 67 enum PowerState { |
| 69 kUnknownPowerState = -1, // Initial/unknown power state. | 68 kUnknownPowerState = -1, // Initial/unknown power state. |
| 70 #define STATE(name, capname) kPowerState ## capname, | 69 #define STATE(name, capname) kPowerState ## capname, |
| 71 #include "power_states.h" | 70 #include "power_states.h" |
| 72 kNumberPowerStates | 71 kNumberPowerStates |
| 73 }; | 72 }; |
| 74 | 73 |
| 75 // The screen-saver states (see screensaver_states.h). | |
| 76 enum ScreenSaverState { | |
| 77 kUnknownScreenSaverState = -1, // Initial/unknown screen-saver state. | |
| 78 #define STATE(name, capname) kScreenSaverState ## capname, | |
| 79 #include "screensaver_states.h" | |
| 80 kNumberScreenSaverStates | |
| 81 }; | |
| 82 | |
| 83 // The user session states (see session_states.h). | 74 // The user session states (see session_states.h). |
| 84 enum SessionState { | 75 enum SessionState { |
| 85 kUnknownSessionState = -1, // Initial/unknown user session state. | 76 kUnknownSessionState = -1, // Initial/unknown user session state. |
| 86 #define STATE(name, capname) kSessionState ## capname, | 77 #define STATE(name, capname) kSessionState ## capname, |
| 87 #include "session_states.h" | 78 #include "session_states.h" |
| 88 kNumberSessionStates | 79 kNumberSessionStates |
| 89 }; | 80 }; |
| 90 | 81 |
| 91 // Data record for aggregating daily usage. | 82 // Data record for aggregating daily usage. |
| 92 class UseRecord { | 83 class UseRecord { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 108 | 99 |
| 109 // D-Bus message match strings. | 100 // D-Bus message match strings. |
| 110 static const char* kDBusMatches_[]; | 101 static const char* kDBusMatches_[]; |
| 111 | 102 |
| 112 // Array of network states. | 103 // Array of network states. |
| 113 static const char* kNetworkStates_[kNumberNetworkStates]; | 104 static const char* kNetworkStates_[kNumberNetworkStates]; |
| 114 | 105 |
| 115 // Array of power states. | 106 // Array of power states. |
| 116 static const char* kPowerStates_[kNumberPowerStates]; | 107 static const char* kPowerStates_[kNumberPowerStates]; |
| 117 | 108 |
| 118 // Array of screen-saver states. | |
| 119 static const char* kScreenSaverStates_[kNumberScreenSaverStates]; | |
| 120 | |
| 121 // Array of user session states. | 109 // Array of user session states. |
| 122 static const char* kSessionStates_[kNumberSessionStates]; | 110 static const char* kSessionStates_[kNumberSessionStates]; |
| 123 | 111 |
| 124 // Creates the event loop and enters it. | 112 // Creates the event loop and enters it. |
| 125 void Loop(); | 113 void Loop(); |
| 126 | 114 |
| 127 // D-Bus filter callback. | 115 // D-Bus filter callback. |
| 128 static DBusHandlerResult MessageFilter(DBusConnection* connection, | 116 static DBusHandlerResult MessageFilter(DBusConnection* connection, |
| 129 DBusMessage* message, | 117 DBusMessage* message, |
| 130 void* user_data); | 118 void* user_data); |
| 131 | 119 |
| 132 // Processes network state change. | 120 // Processes network state change. |
| 133 void NetStateChanged(const char* state_name, base::TimeTicks ticks); | 121 void NetStateChanged(const char* state_name, base::TimeTicks ticks); |
| 134 | 122 |
| 135 // Given the state name, returns the state id. | 123 // Given the state name, returns the state id. |
| 136 NetworkState LookupNetworkState(const char* state_name); | 124 NetworkState LookupNetworkState(const char* state_name); |
| 137 | 125 |
| 138 // Processes power state change. | 126 // Processes power state change. |
| 139 void PowerStateChanged(const char* state_name, base::Time now); | 127 void PowerStateChanged(const char* state_name, base::Time now); |
| 140 | 128 |
| 141 // Given the state name, returns the state id. | 129 // Given the state name, returns the state id. |
| 142 PowerState LookupPowerState(const char* state_name); | 130 PowerState LookupPowerState(const char* state_name); |
| 143 | 131 |
| 144 // Processes screen-saver state change. | |
| 145 void ScreenSaverStateChanged(const char* state_name, base::Time now); | |
| 146 | |
| 147 // Given the state name, returns the state id. | |
| 148 ScreenSaverState LookupScreenSaverState(const char* state_name); | |
| 149 | |
| 150 // Processes user session state change. | 132 // Processes user session state change. |
| 151 void SessionStateChanged(const char* state_name, base::Time now); | 133 void SessionStateChanged(const char* state_name, base::Time now); |
| 152 | 134 |
| 153 // Given the state name, returns the state id. | 135 // Given the state name, returns the state id. |
| 154 SessionState LookupSessionState(const char* state_name); | 136 SessionState LookupSessionState(const char* state_name); |
| 155 | 137 |
| 156 // Updates the user-active state to |active| and logs the usage data | 138 // Updates the user-active state to |active| and logs the usage data |
| 157 // since the last update. If the user has just become active, | 139 // since the last update. If the user has just become active, |
| 158 // reschedule the daily use monitor for more frequent updates -- | 140 // reschedule the daily use monitor for more frequent updates -- |
| 159 // this is followed by an exponential back-off (see UseMonitor). | 141 // this is followed by an exponential back-off (see UseMonitor). |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 NetworkState network_state_; | 190 NetworkState network_state_; |
| 209 | 191 |
| 210 // Timestamps last network state update. This timestamp is used to | 192 // Timestamps last network state update. This timestamp is used to |
| 211 // sample the time from the network going online to going offline so | 193 // sample the time from the network going online to going offline so |
| 212 // TimeTicks ensures a monotonically increasing TimeDelta. | 194 // TimeTicks ensures a monotonically increasing TimeDelta. |
| 213 base::TimeTicks network_state_last_; | 195 base::TimeTicks network_state_last_; |
| 214 | 196 |
| 215 // Current power state. | 197 // Current power state. |
| 216 PowerState power_state_; | 198 PowerState power_state_; |
| 217 | 199 |
| 218 // Current screen-saver state. | |
| 219 ScreenSaverState screensaver_state_; | |
| 220 | |
| 221 // Current user session state. | 200 // Current user session state. |
| 222 SessionState session_state_; | 201 SessionState session_state_; |
| 223 | 202 |
| 224 // Is the user currently active: power is on, user session has | 203 // Is the user currently active: power is on, user session has |
| 225 // started, screen is not locked. | 204 // started, screen is not locked. |
| 226 bool user_active_; | 205 bool user_active_; |
| 227 | 206 |
| 228 // Timestamps last user active update. Active use time is | 207 // Timestamps last user active update. Active use time is |
| 229 // aggregated each day before sending to UMA so using time since the | 208 // aggregated each day before sending to UMA so using time since the |
| 230 // epoch as the timestamp. | 209 // epoch as the timestamp. |
| 231 base::Time user_active_last_; | 210 base::Time user_active_last_; |
| 232 | 211 |
| 233 // Last stored daily use day (since the epoch). | 212 // Last stored daily use day (since the epoch). |
| 234 int daily_use_day_last_; | 213 int daily_use_day_last_; |
| 235 | 214 |
| 236 // Sleep period until the next daily usage aggregation performed by | 215 // Sleep period until the next daily usage aggregation performed by |
| 237 // the daily use monitor (see ScheduleUseMonitor). | 216 // the daily use monitor (see ScheduleUseMonitor). |
| 238 int usemon_interval_; | 217 int usemon_interval_; |
| 239 | 218 |
| 240 // Scheduled daily use monitor source (see ScheduleUseMonitor). | 219 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
| 241 GSource* usemon_source_; | 220 GSource* usemon_source_; |
| 242 }; | 221 }; |
| 243 | 222 |
| 244 #endif // METRICS_DAEMON_H_ | 223 #endif // METRICS_DAEMON_H_ |
| OLD | NEW |