Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef METRICS_DAEMON_H_ |
| 6 | #define METRICS_DAEMON_H_ |
| 7 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 8 | #include <dbus/dbus.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 9 | #include <time.h> |
| 10 | |
| 11 | class MetricsDaemon { |
| 12 | |
| 13 | public: |
| 14 | MetricsDaemon() |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 15 | : testing_(false), |
| 16 | network_state_(kUnknownNetworkState), |
| 17 | network_state_changed_(0), |
| 18 | power_state_(kUnknownPowerState) {} |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 19 | ~MetricsDaemon() {} |
| 20 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 21 | // Does all the work. If |run_as_daemon| is true, daemonizes by |
| 22 | // forking. If |testing| is true, logs the stats instead of sending |
| 23 | // them to Chrome. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 24 | void Run(bool run_as_daemon, bool testing); |
| 25 | |
| 26 | private: |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 27 | // The network states (see network_states.h). |
| 28 | enum NetworkState { |
| 29 | kUnknownNetworkState = -1, // Initial/unknown network state. |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 30 | #define STATE(name, capname) kNetworkState ## capname, |
| 31 | #include "network_states.h" |
| 32 | kNumberNetworkStates |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 33 | }; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 34 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 35 | // The power states (see power_states.h). |
| 36 | enum PowerState { |
| 37 | kUnknownPowerState = -1, // Initial/unknown power state. |
| 38 | #define STATE(name, capname) kPowerState ## capname, |
| 39 | #include "power_states.h" |
| 40 | kNumberPowerStates |
| 41 | }; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 42 | |
| 43 | // Initializes. |
| 44 | void Init(bool testing); |
| 45 | |
| 46 | // Creates the event loop and enters it. |
| 47 | void Loop(); |
| 48 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 49 | // D-Bus filter callback. |
| 50 | static DBusHandlerResult MessageFilter(DBusConnection* connection, |
| 51 | DBusMessage* message, |
| 52 | void* user_data); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 53 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 54 | // Processes network state change. |
| 55 | void NetStateChanged(const char* state_name); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 56 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 57 | // Given the state name, returns the state id. |
| 58 | NetworkState LookupNetworkState(const char* state_name); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 59 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 60 | // Processes power state change. |
| 61 | void PowerStateChanged(const char* state_name); |
| 62 | |
| 63 | // Given the state name, returns the state id. |
| 64 | PowerState LookupPowerState(const char* state_name); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 65 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 66 | // Sends a stat to Chrome for transport to UMA (or prints it for |
| 67 | // testing). See MetricsLibrary::SendToChrome in metrics_library.h |
| 68 | // for a description of the arguments. |
| 69 | void PublishMetric(const char* name, int sample, |
| 70 | int min, int max, int nbuckets); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 71 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 72 | // D-Bus message match strings. |
| 73 | static const char* dbus_matches_[]; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 74 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 75 | // Array of network states. |
| 76 | static const char* network_states_[kNumberNetworkStates]; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 77 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 78 | // Array of power states. |
| 79 | static const char* power_states_[kNumberPowerStates]; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 80 | |
Darin Petkov | 703ec97 | 2010-04-27 11:02:18 -0700 | [diff] [blame^] | 81 | bool testing_; // just testing |
| 82 | NetworkState network_state_; // current network state |
| 83 | time_t network_state_changed_; // timestamp last net state change |
| 84 | PowerState power_state_; // current power state |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | #endif // METRICS_DAEMON_H_ |