blob: 2ac1ea0f8c99142ff32682c0b15517c1c7818dcf [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
8#include <dbus/dbus-glib.h>
9#include <sys/time.h>
10#include <time.h>
11
12class MetricsDaemon {
13
14 public:
15 MetricsDaemon()
16 : network_state_id_(kUnknownNetworkStateId) {
17 }
18 ~MetricsDaemon() {}
19
20 // Does all the work. If |run_as_daemon| is true, daemonize by forking. If
21 // |testing| is true, log the stats instead of sending them to Chrome.
22 void Run(bool run_as_daemon, bool testing);
23
24 private:
25 // Shared with Chrome for transport.
26 static const char* kMetricsFilePath;
27 static const int kMetricsMessageMaxLength = 4096;
28
29 // The network states. See network_states.h.
30 typedef enum {
31 // Initial/unknown network state id.
32 kUnknownNetworkStateId = -1,
33#define STATE(name, capname) kNetworkState ## capname,
34#include "network_states.h"
35 kNumberNetworkStates
36 } NetworkStateId;
37
38 typedef struct {
39 const char* name;
40 const char* stat_name;
41 } NetworkState;
42
43 // Initializes.
44 void Init(bool testing);
45
46 // Creates the event loop and enters it.
47 void Loop();
48
49 // Static callback for network events on DBus.
50 static void StaticNetSignalHandler(::DBusGProxy* proxy, const char* property,
51 const ::GValue* value, void* data);
52
53 // Callback for network events on DBus.
54 void NetSignalHandler(::DBusGProxy* proxy, const char* property,
55 const ::GValue* value);
56
57 // This is called at each network state change. The new state is identified
58 // by the string @newstate. As a side effect, this method ships to Chrome
59 // (or prints to stdout when testing) the name and duration of the state
60 // that has ended.
61 void LogNetworkStateChange(const char* newstate);
62
63 // Given a string with the name of a state, returns the id for the state.
64 NetworkStateId GetNetworkStateId(const char* state_name);
65
66 // Sends a stat to Chrome for transport to UMA.
67 void ChromePublishMetric(const char* name, const char* value);
68
69 // Prints a stat for testing.
70 void TestPublishMetric(const char* name, const char* value);
71
72#if 0
73 // Fetches a name-value hash table from DBus.
74 bool GetProperties(::DBusGProxy* proxy, ::GHashTable** table);
75
76 // The type descriptor for a glib hash table.
77 GType hashtable_gtype;
78#endif
79
80 // Array of network states of interest.
81 static NetworkState network_states_[kNumberNetworkStates];
82
83 bool testing_; // just testing
84 NetworkStateId network_state_id_; // id of current state
85 struct timeval network_state_start_; // when current state was entered
86};
87
88#endif // METRICS_DAEMON_H_