| 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 <gtest/gtest_prod.h> // for FRIEND_TEST |
| 13 |
| 12 class MetricsDaemon { | 14 class MetricsDaemon { |
| 13 | 15 |
| 14 public: | 16 public: |
| 15 MetricsDaemon() | 17 MetricsDaemon() |
| 16 : network_state_(kUnknownNetworkState), | 18 : daily_use_record_file_(NULL), |
| 19 network_state_(kUnknownNetworkState), |
| 17 network_state_last_(0), | 20 network_state_last_(0), |
| 18 power_state_(kUnknownPowerState), | 21 power_state_(kUnknownPowerState), |
| 19 screensaver_state_(kUnknownScreenSaverState), | 22 screensaver_state_(kUnknownScreenSaverState), |
| 20 session_state_(kUnknownSessionState), | 23 session_state_(kUnknownSessionState), |
| 21 user_active_(false), | 24 user_active_(false), |
| 22 user_active_last_(0), | 25 user_active_last_(0), |
| 23 daily_use_day_last_(0), | 26 daily_use_day_last_(0), |
| 24 usemon_interval_(0), | 27 usemon_interval_(0), |
| 25 usemon_source_(NULL) {} | 28 usemon_source_(NULL) {} |
| 26 ~MetricsDaemon() {} | 29 ~MetricsDaemon() {} |
| 27 | 30 |
| 28 // Does all the work. If |run_as_daemon| is true, daemonizes by | 31 // Does all the work. If |run_as_daemon| is true, daemonizes by |
| 29 // forking. If |testing| is true, logs the stats instead of sending | 32 // forking. |
| 30 // them to Chrome. | 33 void Run(bool run_as_daemon); |
| 31 void Run(bool run_as_daemon, bool testing); | |
| 32 | 34 |
| 33 private: | 35 private: |
| 36 friend class MetricsDaemonTest; |
| 37 FRIEND_TEST(MetricsDaemonTest, LogDailyUseRecord); |
| 38 FRIEND_TEST(MetricsDaemonTest, LookupNetworkState); |
| 39 FRIEND_TEST(MetricsDaemonTest, LookupPowerState); |
| 40 FRIEND_TEST(MetricsDaemonTest, LookupScreenSaverState); |
| 41 FRIEND_TEST(MetricsDaemonTest, LookupSessionState); |
| 42 FRIEND_TEST(MetricsDaemonTest, NetStateChanged); |
| 43 FRIEND_TEST(MetricsDaemonTest, PowerStateChanged); |
| 44 FRIEND_TEST(MetricsDaemonTest, PublishMetric); |
| 45 FRIEND_TEST(MetricsDaemonTest, ScreenSaverStateChanged); |
| 46 FRIEND_TEST(MetricsDaemonTest, SessionStateChanged); |
| 47 FRIEND_TEST(MetricsDaemonTest, SetUserActiveState); |
| 48 |
| 34 // The network states (see network_states.h). | 49 // The network states (see network_states.h). |
| 35 enum NetworkState { | 50 enum NetworkState { |
| 36 kUnknownNetworkState = -1, // Initial/unknown network state. | 51 kUnknownNetworkState = -1, // Initial/unknown network state. |
| 37 #define STATE(name, capname) kNetworkState ## capname, | 52 #define STATE(name, capname) kNetworkState ## capname, |
| 38 #include "network_states.h" | 53 #include "network_states.h" |
| 39 kNumberNetworkStates | 54 kNumberNetworkStates |
| 40 }; | 55 }; |
| 41 | 56 |
| 42 // The power states (see power_states.h). | 57 // The power states (see power_states.h). |
| 43 enum PowerState { | 58 enum PowerState { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 64 }; | 79 }; |
| 65 | 80 |
| 66 // Data record for aggregating daily usage. | 81 // Data record for aggregating daily usage. |
| 67 class UseRecord { | 82 class UseRecord { |
| 68 public: | 83 public: |
| 69 UseRecord() : day_(0), seconds_(0) {} | 84 UseRecord() : day_(0), seconds_(0) {} |
| 70 int day_; | 85 int day_; |
| 71 int seconds_; | 86 int seconds_; |
| 72 }; | 87 }; |
| 73 | 88 |
| 89 // Metric parameters. |
| 90 static const char kMetricDailyUseTimeName[]; |
| 91 static const int kMetricDailyUseTimeMin; |
| 92 static const int kMetricDailyUseTimeMax; |
| 93 static const int kMetricDailyUseTimeBuckets; |
| 94 static const char kMetricTimeToNetworkDropName[]; |
| 95 static const int kMetricTimeToNetworkDropMin; |
| 96 static const int kMetricTimeToNetworkDropMax; |
| 97 static const int kMetricTimeToNetworkDropBuckets; |
| 98 |
| 74 // D-Bus message match strings. | 99 // D-Bus message match strings. |
| 75 static const char* kDBusMatches_[]; | 100 static const char* kDBusMatches_[]; |
| 76 | 101 |
| 77 // Array of network states. | 102 // Array of network states. |
| 78 static const char* kNetworkStates_[kNumberNetworkStates]; | 103 static const char* kNetworkStates_[kNumberNetworkStates]; |
| 79 | 104 |
| 80 // Array of power states. | 105 // Array of power states. |
| 81 static const char* kPowerStates_[kNumberPowerStates]; | 106 static const char* kPowerStates_[kNumberPowerStates]; |
| 82 | 107 |
| 83 // Array of screen-saver states. | 108 // Array of screen-saver states. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 179 |
| 155 // Unschedules a scheduled use monitor, if any. | 180 // Unschedules a scheduled use monitor, if any. |
| 156 void UnscheduleUseMonitor(); | 181 void UnscheduleUseMonitor(); |
| 157 | 182 |
| 158 // Sends a stat to Chrome for transport to UMA (or prints it for | 183 // Sends a stat to Chrome for transport to UMA (or prints it for |
| 159 // testing). See MetricsLibrary::SendToChrome in metrics_library.h | 184 // testing). See MetricsLibrary::SendToChrome in metrics_library.h |
| 160 // for a description of the arguments. | 185 // for a description of the arguments. |
| 161 void PublishMetric(const char* name, int sample, | 186 void PublishMetric(const char* name, int sample, |
| 162 int min, int max, int nbuckets); | 187 int min, int max, int nbuckets); |
| 163 | 188 |
| 164 // Testing mode. | 189 // Test mode. |
| 165 bool testing_; | 190 bool testing_; |
| 166 | 191 |
| 192 const char* daily_use_record_file_; |
| 193 |
| 167 // Current network state. | 194 // Current network state. |
| 168 NetworkState network_state_; | 195 NetworkState network_state_; |
| 169 | 196 |
| 170 // Timestamps last network state update. | 197 // Timestamps last network state update. |
| 171 time_t network_state_last_; | 198 time_t network_state_last_; |
| 172 | 199 |
| 173 // Current power state. | 200 // Current power state. |
| 174 PowerState power_state_; | 201 PowerState power_state_; |
| 175 | 202 |
| 176 // Current screen-saver state. | 203 // Current screen-saver state. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 191 | 218 |
| 192 // Sleep period until the next daily usage aggregation performed by | 219 // Sleep period until the next daily usage aggregation performed by |
| 193 // the daily use monitor (see ScheduleUseMonitor). | 220 // the daily use monitor (see ScheduleUseMonitor). |
| 194 int usemon_interval_; | 221 int usemon_interval_; |
| 195 | 222 |
| 196 // Scheduled daily use monitor source (see ScheduleUseMonitor). | 223 // Scheduled daily use monitor source (see ScheduleUseMonitor). |
| 197 GSource* usemon_source_; | 224 GSource* usemon_source_; |
| 198 }; | 225 }; |
| 199 | 226 |
| 200 #endif // METRICS_DAEMON_H_ | 227 #endif // METRICS_DAEMON_H_ |
| OLD | NEW |