blob: ce6985f6cbfcaa5e0d1915c8e6f542b33df67917 [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;
Daniel Eratca90d8b2011-01-06 15:46:00 -080023 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070024 virtual ~MetricsLibraryInterface() {}
25};
26
27// Library used to send metrics to both Autotest and Chrome/UMA.
28class MetricsLibrary : public MetricsLibraryInterface {
29 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070030 MetricsLibrary();
31
Darin Petkovfc91b422010-05-12 13:05:45 -070032 // Initializes the library.
33 void Init();
34
Ken Mixtereafbbdf2010-10-01 15:38:42 -070035 // Returns whether or not the machine is running in guest mode.
36 bool IsGuestMode();
37
Ken Mixter4c5daa42010-08-26 18:35:06 -070038 // Returns whether or not metrics collection is enabled.
39 bool AreMetricsEnabled();
40
Darin Petkovc2526a12010-04-21 14:24:04 -070041 // Sends histogram data to Chrome for transport to UMA and returns
42 // true on success. This method results in the equivalent of an
43 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
44 // inside Chrome (see base/histogram.h).
45 //
46 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
47 // |min| is the minimum value of the histogram samples (|min| > 0).
48 // |max| is the maximum value of the histogram samples.
49 // |nbuckets| is the number of histogram buckets.
50 // [0,min) is the implicit underflow bucket.
51 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070052 //
53 // Note that the memory allocated in Chrome for each histogram is
54 // proportional to the number of buckets. Therefore, it is strongly
55 // recommended to keep this number low (e.g., 50 is normal, while
56 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070057 bool SendToUMA(const std::string& name, int sample,
58 int min, int max, int nbuckets);
59
Darin Petkov5b7dce12010-04-21 15:45:10 -070060 // Sends linear histogram data to Chrome for transport to UMA and
61 // returns true on success. This method results in the equivalent of
62 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
63 // inside Chrome (see base/histogram.h).
64 //
65 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
66 // |max| is the maximum value of the histogram samples.
67 // 0 is the implicit underflow bucket.
68 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070069 //
70 // An enumaration histogram requires |max| + 1 number of
71 // buckets. Note that the memory allocated in Chrome for each
72 // histogram is proportional to the number of buckets. Therefore, it
73 // is strongly recommended to keep this number low (e.g., 50 is
74 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070075 bool SendEnumToUMA(const std::string& name, int sample, int max);
76
Darin Petkoved824852011-01-06 10:51:47 -080077 // Sends a user action to Chrome for transport to UMA and returns true on
78 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080079 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
80 // added to chrome/tools/extract_actions.py in the Chromium repository, which
81 // should then be run to generate a hash for the new action.
82 //
83 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
84 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080085 //
86 // |action| is the user-generated event (e.g., "MuteKeyPressed").
87 bool SendUserActionToUMA(const std::string& action);
88
Ken Mixterbe2e13b2011-01-22 06:15:56 -080089 // Sends a signal to UMA that a crash of the given |crash_kind|
90 // has occurred. Used by UMA to generate stability statistics.
91 bool SendCrashToUMA(const char *crash_kind);
92
Luigi Semenzato32684222013-03-13 10:53:55 -070093 // Sends a "generic Chrome OS event" to UMA. This is an event name
94 // that is translated into an enumerated histogram entry. Event names
95 // are added to metrics_library.cc. Optionally, they can be added
96 // to histograms.xml---but part of the reason for this is to simplify
97 // the addition of events (at the cost of having to look them up by
98 // number in the histograms dashboard).
99 bool SendCrosEventToUMA(const std::string& event);
100
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700101 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -0700102 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700103
104 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700105 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700106 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700107 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700108 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
109 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700110 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700111 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
112 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
113
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700114 // Sets |*result| to whether or not the |mounts_file| indicates that
115 // the |device_name| is currently mounted. Uses |buffer| of
116 // |buffer_size| to read the file. Returns false if any error.
117 bool IsDeviceMounted(const char* device_name,
118 const char* mounts_file,
119 char* buffer, int buffer_size,
120 bool* result);
121
Darin Petkov11b8eb32010-05-18 11:00:59 -0700122 // Sends message of size |length| to Chrome for transport to UMA and
123 // returns true on success.
124 bool SendMessageToChrome(int32_t length, const char* message);
125
126 // Formats a name/value message for Chrome in |buffer| and returns the
127 // length of the message or a negative value on error.
128 //
129 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
130 //
131 // The arbitrary |format| argument covers the non-LENGTH portion of the
132 // message. The caller is responsible to store the \0 character
133 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
134 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700135 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700136
Ken Mixterb2f17092011-07-22 14:59:51 -0700137 // This function is used by tests only to mock the device policies.
138 void SetPolicyProvider(policy::PolicyProvider* provider);
139
Ken Mixter4c5daa42010-08-26 18:35:06 -0700140 // Time at which we last checked if metrics were enabled.
141 static time_t cached_enabled_time_;
142
143 // Cached state of whether or not metrics were enabled.
144 static bool cached_enabled_;
145
Darin Petkov11b8eb32010-05-18 11:00:59 -0700146 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700147 const char* consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700148
149 scoped_ptr<policy::PolicyProvider> policy_provider_;
Darin Petkov65b01462010-04-14 13:32:20 -0700150};
151
David James3b3add52010-06-04 15:01:19 -0700152#endif // METRICS_LIBRARY_H_