blob: 99f76d087198f3e653893d8f42d72fb4ac83db80 [file] [log] [blame]
Darin Petkov65b01462010-04-14 13:32:20 -07001// 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 Petkov703ec972010-04-27 11:02:18 -07008#include <dbus/dbus.h>
Darin Petkov41e06232010-05-03 16:45:37 -07009#include <glib.h>
Darin Petkov65b01462010-04-14 13:32:20 -070010#include <time.h>
11
12class MetricsDaemon {
13
14 public:
15 MetricsDaemon()
Darin Petkov41e06232010-05-03 16:45:37 -070016 : network_state_(kUnknownNetworkState),
17 network_state_last_(0),
18 power_state_(kUnknownPowerState),
19 screensaver_state_(kUnknownScreenSaverState),
20 session_state_(kUnknownSessionState),
21 user_active_(false),
22 user_active_last_(0),
23 daily_use_day_last_(0),
24 usemon_interval_(0),
25 usemon_source_(NULL) {}
Darin Petkov65b01462010-04-14 13:32:20 -070026 ~MetricsDaemon() {}
27
Darin Petkov703ec972010-04-27 11:02:18 -070028 // Does all the work. If |run_as_daemon| is true, daemonizes by
29 // forking. If |testing| is true, logs the stats instead of sending
30 // them to Chrome.
Darin Petkov65b01462010-04-14 13:32:20 -070031 void Run(bool run_as_daemon, bool testing);
32
33 private:
Darin Petkov703ec972010-04-27 11:02:18 -070034 // The network states (see network_states.h).
35 enum NetworkState {
36 kUnknownNetworkState = -1, // Initial/unknown network state.
Darin Petkov65b01462010-04-14 13:32:20 -070037#define STATE(name, capname) kNetworkState ## capname,
38#include "network_states.h"
39 kNumberNetworkStates
Darin Petkov703ec972010-04-27 11:02:18 -070040 };
Darin Petkov65b01462010-04-14 13:32:20 -070041
Darin Petkov703ec972010-04-27 11:02:18 -070042 // The power states (see power_states.h).
43 enum PowerState {
44 kUnknownPowerState = -1, // Initial/unknown power state.
45#define STATE(name, capname) kPowerState ## capname,
46#include "power_states.h"
47 kNumberPowerStates
48 };
Darin Petkov65b01462010-04-14 13:32:20 -070049
Darin Petkov41e06232010-05-03 16:45:37 -070050 // The screen-saver states (see screensaver_states.h).
51 enum ScreenSaverState {
52 kUnknownScreenSaverState = -1, // Initial/unknown screen-saver state.
53#define STATE(name, capname) kScreenSaverState ## capname,
54#include "screensaver_states.h"
55 kNumberScreenSaverStates
56 };
57
58 // The user session states (see session_states.h).
59 enum SessionState {
60 kUnknownSessionState = -1, // Initial/unknown user session state.
61#define STATE(name, capname) kSessionState ## capname,
62#include "session_states.h"
63 kNumberSessionStates
64 };
65
66 // Data record for aggregating daily usage.
67 class UseRecord {
68 public:
69 UseRecord() : day_(0), seconds_(0) {}
70 int day_;
71 int seconds_;
72 };
73
74 // D-Bus message match strings.
75 static const char* kDBusMatches_[];
76
77 // Array of network states.
78 static const char* kNetworkStates_[kNumberNetworkStates];
79
80 // Array of power states.
81 static const char* kPowerStates_[kNumberPowerStates];
82
83 // Array of screen-saver states.
84 static const char* kScreenSaverStates_[kNumberScreenSaverStates];
85
86 // Array of user session states.
87 static const char* kSessionStates_[kNumberSessionStates];
88
Darin Petkov65b01462010-04-14 13:32:20 -070089 // Initializes.
90 void Init(bool testing);
91
92 // Creates the event loop and enters it.
93 void Loop();
94
Darin Petkov703ec972010-04-27 11:02:18 -070095 // D-Bus filter callback.
96 static DBusHandlerResult MessageFilter(DBusConnection* connection,
97 DBusMessage* message,
98 void* user_data);
Darin Petkov65b01462010-04-14 13:32:20 -070099
Darin Petkov703ec972010-04-27 11:02:18 -0700100 // Processes network state change.
Darin Petkov41e06232010-05-03 16:45:37 -0700101 void NetStateChanged(const char* state_name, time_t now);
Darin Petkov65b01462010-04-14 13:32:20 -0700102
Darin Petkov703ec972010-04-27 11:02:18 -0700103 // Given the state name, returns the state id.
104 NetworkState LookupNetworkState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700105
Darin Petkov703ec972010-04-27 11:02:18 -0700106 // Processes power state change.
Darin Petkov41e06232010-05-03 16:45:37 -0700107 void PowerStateChanged(const char* state_name, time_t now);
Darin Petkov703ec972010-04-27 11:02:18 -0700108
109 // Given the state name, returns the state id.
110 PowerState LookupPowerState(const char* state_name);
Darin Petkov65b01462010-04-14 13:32:20 -0700111
Darin Petkov41e06232010-05-03 16:45:37 -0700112 // Processes screen-saver state change.
113 void ScreenSaverStateChanged(const char* state_name, time_t now);
114
115 // Given the state name, returns the state id.
116 ScreenSaverState LookupScreenSaverState(const char* state_name);
117
118 // Processes user session state change.
119 void SessionStateChanged(const char* state_name, time_t now);
120
121 // Given the state name, returns the state id.
122 SessionState LookupSessionState(const char* state_name);
123
124 // Updates the user-active state to |active| and logs the usage data
125 // since the last update. If the user has just become active,
126 // reschedule the daily use monitor for more frequent updates --
127 // this is followed by an exponential back-off (see UseMonitor).
128 void SetUserActiveState(bool active, time_t now);
129
130 // Updates the daily usage file, if necessary, by adding |seconds|
131 // of active use to the |day| since Epoch. If there's usage data for
132 // day in the past in the usage file, that data is sent to UMA and
133 // removed from the file. If there's already usage data for |day| in
134 // the usage file, the |seconds| are accumulated.
135 void LogDailyUseRecord(int day, int seconds);
136
137 // Callbacks for the daily use monitor. The daily use monitor uses
138 // LogDailyUseRecord to aggregate current usage data and send it to
139 // UMA, if necessary. It also reschedules itself using an
140 // exponentially bigger interval (up to a certain maximum) -- so
141 // usage is monitored less frequently with longer active use.
142 static gboolean UseMonitorStatic(gpointer data);
143 bool UseMonitor();
144
145 // Schedules or reschedules a daily use monitor for |interval|
146 // seconds from now. |backoff| mode is used by the use monitor to
147 // reschedule itself. If there's a monitor scheduled already and
148 // |backoff| is false, unschedules it first. Doesn't schedule a
149 // monitor for more than kUseMonitorIntervalMax seconds in the
150 // future (see metrics_daemon.cc). Returns true if a new use monitor
151 // was scheduled, false otherwise (note that if |backoff| is false a
152 // new use monitor will always be scheduled).
153 bool ScheduleUseMonitor(int interval, bool backoff);
154
155 // Unschedules a scheduled use monitor, if any.
156 void UnscheduleUseMonitor();
157
Darin Petkovc2526a12010-04-21 14:24:04 -0700158 // Sends a stat to Chrome for transport to UMA (or prints it for
159 // testing). See MetricsLibrary::SendToChrome in metrics_library.h
160 // for a description of the arguments.
161 void PublishMetric(const char* name, int sample,
162 int min, int max, int nbuckets);
Darin Petkov65b01462010-04-14 13:32:20 -0700163
Darin Petkov41e06232010-05-03 16:45:37 -0700164 // Testing mode.
165 bool testing_;
Darin Petkov65b01462010-04-14 13:32:20 -0700166
Darin Petkov41e06232010-05-03 16:45:37 -0700167 // Current network state.
168 NetworkState network_state_;
Darin Petkov65b01462010-04-14 13:32:20 -0700169
Darin Petkov41e06232010-05-03 16:45:37 -0700170 // Timestamps last network state update.
171 time_t network_state_last_;
Darin Petkov65b01462010-04-14 13:32:20 -0700172
Darin Petkov41e06232010-05-03 16:45:37 -0700173 // Current power state.
174 PowerState power_state_;
175
176 // Current screen-saver state.
177 ScreenSaverState screensaver_state_;
178
179 // Current user session state.
180 SessionState session_state_;
181
182 // Is the user currently active: power is on, user session has
183 // started, screen is not locked.
184 bool user_active_;
185
186 // Timestamps last user active update.
187 time_t user_active_last_;
188
189 // Last stored daily use day (since epoch).
190 int daily_use_day_last_;
191
192 // Sleep period until the next daily usage aggregation performed by
193 // the daily use monitor (see ScheduleUseMonitor).
194 int usemon_interval_;
195
196 // Scheduled daily use monitor source (see ScheduleUseMonitor).
197 GSource* usemon_source_;
Darin Petkov65b01462010-04-14 13:32:20 -0700198};
199
200#endif // METRICS_DAEMON_H_