blob: 4bba96dce44d55160aa8ad19e40615530ca6607e [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>
Yunlian Jiang564c69f2012-05-02 14:24:04 -070010#include <unistd.h>
Darin Petkov65b01462010-04-14 13:32:20 -070011
Eric Shienbrood11353552012-02-29 14:52:29 -050012#include <base/memory/scoped_ptr.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070014
Ken Mixterb2f17092011-07-22 14:59:51 -070015#include "policy/libpolicy.h"
16
Darin Petkovfc91b422010-05-12 13:05:45 -070017class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070018 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070019 virtual void Init() = 0;
20 virtual bool SendToUMA(const std::string& name, int sample,
21 int min, int max, int nbuckets) = 0;
22 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070023 virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080024 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070025 virtual ~MetricsLibraryInterface() {}
26};
27
28// Library used to send metrics to both Autotest and Chrome/UMA.
29class MetricsLibrary : public MetricsLibraryInterface {
30 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070031 MetricsLibrary();
32
Darin Petkovfc91b422010-05-12 13:05:45 -070033 // Initializes the library.
34 void Init();
35
Ken Mixtereafbbdf2010-10-01 15:38:42 -070036 // Returns whether or not the machine is running in guest mode.
37 bool IsGuestMode();
38
Ken Mixter4c5daa42010-08-26 18:35:06 -070039 // Returns whether or not metrics collection is enabled.
40 bool AreMetricsEnabled();
41
Darin Petkovc2526a12010-04-21 14:24:04 -070042 // Sends histogram data to Chrome for transport to UMA and returns
43 // true on success. This method results in the equivalent of an
44 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
45 // inside Chrome (see base/histogram.h).
46 //
47 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
48 // |min| is the minimum value of the histogram samples (|min| > 0).
49 // |max| is the maximum value of the histogram samples.
50 // |nbuckets| is the number of histogram buckets.
51 // [0,min) is the implicit underflow bucket.
52 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070053 //
54 // Note that the memory allocated in Chrome for each histogram is
55 // proportional to the number of buckets. Therefore, it is strongly
56 // recommended to keep this number low (e.g., 50 is normal, while
57 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070058 bool SendToUMA(const std::string& name, int sample,
59 int min, int max, int nbuckets);
60
Darin Petkov5b7dce12010-04-21 15:45:10 -070061 // Sends linear histogram data to Chrome for transport to UMA and
62 // returns true on success. This method results in the equivalent of
63 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
64 // inside Chrome (see base/histogram.h).
65 //
66 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
67 // |max| is the maximum value of the histogram samples.
68 // 0 is the implicit underflow bucket.
69 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070070 //
71 // An enumaration histogram requires |max| + 1 number of
72 // buckets. Note that the memory allocated in Chrome for each
73 // histogram is proportional to the number of buckets. Therefore, it
74 // is strongly recommended to keep this number low (e.g., 50 is
75 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070076 bool SendEnumToUMA(const std::string& name, int sample, int max);
77
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070078 // Sends sparse histogram sample to Chrome for transport to UMA. Returns
79 // true on success.
80 //
81 // |sample| is the 32-bit integer value to be recorded.
82 bool SendSparseToUMA(const std::string& name, int sample);
83
Darin Petkoved824852011-01-06 10:51:47 -080084 // Sends a user action to Chrome for transport to UMA and returns true on
85 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080086 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
87 // added to chrome/tools/extract_actions.py in the Chromium repository, which
88 // should then be run to generate a hash for the new action.
89 //
90 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
91 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080092 //
93 // |action| is the user-generated event (e.g., "MuteKeyPressed").
94 bool SendUserActionToUMA(const std::string& action);
95
Ken Mixterbe2e13b2011-01-22 06:15:56 -080096 // Sends a signal to UMA that a crash of the given |crash_kind|
97 // has occurred. Used by UMA to generate stability statistics.
98 bool SendCrashToUMA(const char *crash_kind);
99
Luigi Semenzato32684222013-03-13 10:53:55 -0700100 // Sends a "generic Chrome OS event" to UMA. This is an event name
101 // that is translated into an enumerated histogram entry. Event names
102 // are added to metrics_library.cc. Optionally, they can be added
103 // to histograms.xml---but part of the reason for this is to simplify
104 // the addition of events (at the cost of having to look them up by
105 // number in the histograms dashboard).
106 bool SendCrosEventToUMA(const std::string& event);
107
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700108 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -0700109 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700110
111 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700112 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700113 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700114 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700115 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
116 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700117 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700118 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
119 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
120
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700121 // Sets |*result| to whether or not the |mounts_file| indicates that
122 // the |device_name| is currently mounted. Uses |buffer| of
123 // |buffer_size| to read the file. Returns false if any error.
124 bool IsDeviceMounted(const char* device_name,
125 const char* mounts_file,
126 char* buffer, int buffer_size,
127 bool* result);
128
Darin Petkov11b8eb32010-05-18 11:00:59 -0700129 // Sends message of size |length| to Chrome for transport to UMA and
130 // returns true on success.
131 bool SendMessageToChrome(int32_t length, const char* message);
132
133 // Formats a name/value message for Chrome in |buffer| and returns the
134 // length of the message or a negative value on error.
135 //
136 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
137 //
138 // The arbitrary |format| argument covers the non-LENGTH portion of the
139 // message. The caller is responsible to store the \0 character
140 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
Mike Frysinger754dc922013-12-19 01:37:49 -0500141 //
142 // Ideally we'd use "3, 4" here instead of "4, 5", but it seems clang/gcc
143 // have an implicit first arg ("this"). http://crbug.com/329356
144 __attribute__((__format__(__printf__, 4, 5)))
Darin Petkov11b8eb32010-05-18 11:00:59 -0700145 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700146 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700147
Ken Mixterb2f17092011-07-22 14:59:51 -0700148 // This function is used by tests only to mock the device policies.
149 void SetPolicyProvider(policy::PolicyProvider* provider);
150
Ken Mixter4c5daa42010-08-26 18:35:06 -0700151 // Time at which we last checked if metrics were enabled.
152 static time_t cached_enabled_time_;
153
154 // Cached state of whether or not metrics were enabled.
155 static bool cached_enabled_;
156
Darin Petkov11b8eb32010-05-18 11:00:59 -0700157 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700158 const char* consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700159
160 scoped_ptr<policy::PolicyProvider> policy_provider_;
Darin Petkov65b01462010-04-14 13:32:20 -0700161};
162
David James3b3add52010-06-04 15:01:19 -0700163#endif // METRICS_LIBRARY_H_