blob: ed8386202b9280ca097179483900754a22d3e41d [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
Darin Petkov65b01462010-04-14 13:32:20 -07005#ifndef METRICS_LIBRARY_H_
6#define METRICS_LIBRARY_H_
7
Darin Petkov11b8eb32010-05-18 11:00:59 -07008#include <sys/types.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009#include <string>
10
Darin Petkov11b8eb32010-05-18 11:00:59 -070011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070012
Darin Petkovfc91b422010-05-12 13:05:45 -070013class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070014 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070015 virtual void Init() = 0;
16 virtual bool SendToUMA(const std::string& name, int sample,
17 int min, int max, int nbuckets) = 0;
18 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080019 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070020 virtual ~MetricsLibraryInterface() {}
21};
22
23// Library used to send metrics to both Autotest and Chrome/UMA.
24class MetricsLibrary : public MetricsLibraryInterface {
25 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070026 MetricsLibrary();
27
Darin Petkovfc91b422010-05-12 13:05:45 -070028 // Initializes the library.
29 void Init();
30
Ken Mixtereafbbdf2010-10-01 15:38:42 -070031 // Returns whether or not the machine is running in guest mode.
32 bool IsGuestMode();
33
Ken Mixter4c5daa42010-08-26 18:35:06 -070034 // Returns whether or not metrics collection is enabled.
35 bool AreMetricsEnabled();
36
Darin Petkovc2526a12010-04-21 14:24:04 -070037 // Sends histogram data to Chrome for transport to UMA and returns
38 // true on success. This method results in the equivalent of an
39 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
40 // inside Chrome (see base/histogram.h).
41 //
42 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
43 // |min| is the minimum value of the histogram samples (|min| > 0).
44 // |max| is the maximum value of the histogram samples.
45 // |nbuckets| is the number of histogram buckets.
46 // [0,min) is the implicit underflow bucket.
47 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070048 //
49 // Note that the memory allocated in Chrome for each histogram is
50 // proportional to the number of buckets. Therefore, it is strongly
51 // recommended to keep this number low (e.g., 50 is normal, while
52 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070053 bool SendToUMA(const std::string& name, int sample,
54 int min, int max, int nbuckets);
55
Darin Petkov5b7dce12010-04-21 15:45:10 -070056 // Sends linear histogram data to Chrome for transport to UMA and
57 // returns true on success. This method results in the equivalent of
58 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
59 // inside Chrome (see base/histogram.h).
60 //
61 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
62 // |max| is the maximum value of the histogram samples.
63 // 0 is the implicit underflow bucket.
64 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070065 //
66 // An enumaration histogram requires |max| + 1 number of
67 // buckets. Note that the memory allocated in Chrome for each
68 // histogram is proportional to the number of buckets. Therefore, it
69 // is strongly recommended to keep this number low (e.g., 50 is
70 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070071 bool SendEnumToUMA(const std::string& name, int sample, int max);
72
Darin Petkoved824852011-01-06 10:51:47 -080073 // Sends a user action to Chrome for transport to UMA and returns true on
74 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080075 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
76 // added to chrome/tools/extract_actions.py in the Chromium repository, which
77 // should then be run to generate a hash for the new action.
78 //
79 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
80 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080081 //
82 // |action| is the user-generated event (e.g., "MuteKeyPressed").
83 bool SendUserActionToUMA(const std::string& action);
84
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070085 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -070086 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -070087
88 private:
Sam Leffler10b301d2010-06-17 14:22:43 -070089 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -070090 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -070091 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -070092 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
93 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -070094 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -070095 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
96 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
97
Ken Mixtereafbbdf2010-10-01 15:38:42 -070098 // Sets |*result| to whether or not the |mounts_file| indicates that
99 // the |device_name| is currently mounted. Uses |buffer| of
100 // |buffer_size| to read the file. Returns false if any error.
101 bool IsDeviceMounted(const char* device_name,
102 const char* mounts_file,
103 char* buffer, int buffer_size,
104 bool* result);
105
Darin Petkov11b8eb32010-05-18 11:00:59 -0700106 // Sends message of size |length| to Chrome for transport to UMA and
107 // returns true on success.
108 bool SendMessageToChrome(int32_t length, const char* message);
109
110 // Formats a name/value message for Chrome in |buffer| and returns the
111 // length of the message or a negative value on error.
112 //
113 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
114 //
115 // The arbitrary |format| argument covers the non-LENGTH portion of the
116 // message. The caller is responsible to store the \0 character
117 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
118 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700119 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700120
Ken Mixter4c5daa42010-08-26 18:35:06 -0700121 // Time at which we last checked if metrics were enabled.
122 static time_t cached_enabled_time_;
123
124 // Cached state of whether or not metrics were enabled.
125 static bool cached_enabled_;
126
Darin Petkov11b8eb32010-05-18 11:00:59 -0700127 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700128 const char* consent_file_;
Darin Petkov65b01462010-04-14 13:32:20 -0700129};
130
David James3b3add52010-06-04 15:01:19 -0700131#endif // METRICS_LIBRARY_H_