blob: e9c6f4be4d872c726e6d7db5cdacc58f45a32866 [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
Daniel Eratfd158292014-03-09 21:39:08 -070012#include <base/basictypes.h>
13#include <base/compiler_specific.h>
Eric Shienbrood11353552012-02-29 14:52:29 -050014#include <base/memory/scoped_ptr.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070016
Ken Mixterb2f17092011-07-22 14:59:51 -070017#include "policy/libpolicy.h"
18
Darin Petkovfc91b422010-05-12 13:05:45 -070019class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070020 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070021 virtual void Init() = 0;
22 virtual bool SendToUMA(const std::string& name, int sample,
23 int min, int max, int nbuckets) = 0;
24 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070025 virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080026 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070027 virtual ~MetricsLibraryInterface() {}
28};
29
30// Library used to send metrics to both Autotest and Chrome/UMA.
31class MetricsLibrary : public MetricsLibraryInterface {
32 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070033 MetricsLibrary();
Daniel Eratfd158292014-03-09 21:39:08 -070034 virtual ~MetricsLibrary();
Darin Petkov11b8eb32010-05-18 11:00:59 -070035
Darin Petkovfc91b422010-05-12 13:05:45 -070036 // Initializes the library.
Daniel Eratfd158292014-03-09 21:39:08 -070037 virtual void Init() OVERRIDE;
Darin Petkovfc91b422010-05-12 13:05:45 -070038
Ken Mixtereafbbdf2010-10-01 15:38:42 -070039 // Returns whether or not the machine is running in guest mode.
40 bool IsGuestMode();
41
Ken Mixter4c5daa42010-08-26 18:35:06 -070042 // Returns whether or not metrics collection is enabled.
43 bool AreMetricsEnabled();
44
Darin Petkovc2526a12010-04-21 14:24:04 -070045 // Sends histogram data to Chrome for transport to UMA and returns
46 // true on success. This method results in the equivalent of an
47 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
48 // inside Chrome (see base/histogram.h).
49 //
50 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
51 // |min| is the minimum value of the histogram samples (|min| > 0).
52 // |max| is the maximum value of the histogram samples.
53 // |nbuckets| is the number of histogram buckets.
54 // [0,min) is the implicit underflow bucket.
55 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070056 //
57 // Note that the memory allocated in Chrome for each histogram is
58 // proportional to the number of buckets. Therefore, it is strongly
59 // recommended to keep this number low (e.g., 50 is normal, while
60 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070061 bool SendToUMA(const std::string& name, int sample,
62 int min, int max, int nbuckets);
63
Darin Petkov5b7dce12010-04-21 15:45:10 -070064 // Sends linear histogram data to Chrome for transport to UMA and
65 // returns true on success. This method results in the equivalent of
66 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
67 // inside Chrome (see base/histogram.h).
68 //
69 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
70 // |max| is the maximum value of the histogram samples.
71 // 0 is the implicit underflow bucket.
72 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070073 //
Daniel Eratfd158292014-03-09 21:39:08 -070074 // An enumeration histogram requires |max| + 1 number of
Darin Petkovc2bf95f2010-06-21 16:27:52 -070075 // buckets. Note that the memory allocated in Chrome for each
76 // histogram is proportional to the number of buckets. Therefore, it
77 // is strongly recommended to keep this number low (e.g., 50 is
78 // normal, while 100 is high).
Daniel Eratfd158292014-03-09 21:39:08 -070079 virtual bool SendEnumToUMA(const std::string& name,
80 int sample,
81 int max) OVERRIDE;
Darin Petkovfc91b422010-05-12 13:05:45 -070082
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070083 // Sends sparse histogram sample to Chrome for transport to UMA. Returns
84 // true on success.
85 //
86 // |sample| is the 32-bit integer value to be recorded.
Daniel Eratfd158292014-03-09 21:39:08 -070087 virtual bool SendSparseToUMA(const std::string& name, int sample) OVERRIDE;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070088
Darin Petkoved824852011-01-06 10:51:47 -080089 // Sends a user action to Chrome for transport to UMA and returns true on
90 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080091 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
92 // added to chrome/tools/extract_actions.py in the Chromium repository, which
93 // should then be run to generate a hash for the new action.
94 //
95 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
96 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080097 //
98 // |action| is the user-generated event (e.g., "MuteKeyPressed").
Daniel Eratfd158292014-03-09 21:39:08 -070099 virtual bool SendUserActionToUMA(const std::string& action) OVERRIDE;
Darin Petkoved824852011-01-06 10:51:47 -0800100
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800101 // Sends a signal to UMA that a crash of the given |crash_kind|
102 // has occurred. Used by UMA to generate stability statistics.
103 bool SendCrashToUMA(const char *crash_kind);
104
Luigi Semenzato32684222013-03-13 10:53:55 -0700105 // Sends a "generic Chrome OS event" to UMA. This is an event name
106 // that is translated into an enumerated histogram entry. Event names
107 // are added to metrics_library.cc. Optionally, they can be added
108 // to histograms.xml---but part of the reason for this is to simplify
109 // the addition of events (at the cost of having to look them up by
110 // number in the histograms dashboard).
111 bool SendCrosEventToUMA(const std::string& event);
112
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700113 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -0700114 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700115
116 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700117 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700118 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700119 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700120 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
121 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700122 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700123 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
124 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
125
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700126 // Sets |*result| to whether or not the |mounts_file| indicates that
127 // the |device_name| is currently mounted. Uses |buffer| of
128 // |buffer_size| to read the file. Returns false if any error.
129 bool IsDeviceMounted(const char* device_name,
130 const char* mounts_file,
131 char* buffer, int buffer_size,
132 bool* result);
133
Darin Petkov11b8eb32010-05-18 11:00:59 -0700134 // Sends message of size |length| to Chrome for transport to UMA and
135 // returns true on success.
136 bool SendMessageToChrome(int32_t length, const char* message);
137
138 // Formats a name/value message for Chrome in |buffer| and returns the
139 // length of the message or a negative value on error.
140 //
141 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
142 //
143 // The arbitrary |format| argument covers the non-LENGTH portion of the
144 // message. The caller is responsible to store the \0 character
145 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
Mike Frysinger754dc922013-12-19 01:37:49 -0500146 //
147 // Ideally we'd use "3, 4" here instead of "4, 5", but it seems clang/gcc
148 // have an implicit first arg ("this"). http://crbug.com/329356
149 __attribute__((__format__(__printf__, 4, 5)))
Darin Petkov11b8eb32010-05-18 11:00:59 -0700150 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700151 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700152
Ken Mixterb2f17092011-07-22 14:59:51 -0700153 // This function is used by tests only to mock the device policies.
154 void SetPolicyProvider(policy::PolicyProvider* provider);
155
Ken Mixter4c5daa42010-08-26 18:35:06 -0700156 // Time at which we last checked if metrics were enabled.
157 static time_t cached_enabled_time_;
158
159 // Cached state of whether or not metrics were enabled.
160 static bool cached_enabled_;
161
Darin Petkov11b8eb32010-05-18 11:00:59 -0700162 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700163 const char* consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700164
165 scoped_ptr<policy::PolicyProvider> policy_provider_;
Daniel Eratfd158292014-03-09 21:39:08 -0700166
167 DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
Darin Petkov65b01462010-04-14 13:32:20 -0700168};
169
David James3b3add52010-06-04 15:01:19 -0700170#endif // METRICS_LIBRARY_H_