Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 1 | // 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 Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 5 | #ifndef METRICS_LIBRARY_H_ |
| 6 | #define METRICS_LIBRARY_H_ |
| 7 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 8 | #include <sys/types.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 9 | #include <string> |
Yunlian Jiang | 564c69f | 2012-05-02 14:24:04 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 11 | |
Eric Shienbrood | 1135355 | 2012-02-29 14:52:29 -0500 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 14 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 15 | #include "policy/libpolicy.h" |
| 16 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 17 | class MetricsLibraryInterface { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 18 | public: |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 19 | 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 Erat | ca90d8b | 2011-01-06 15:46:00 -0800 | [diff] [blame] | 23 | virtual bool SendUserActionToUMA(const std::string& action) = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 24 | virtual ~MetricsLibraryInterface() {} |
| 25 | }; |
| 26 | |
| 27 | // Library used to send metrics to both Autotest and Chrome/UMA. |
| 28 | class MetricsLibrary : public MetricsLibraryInterface { |
| 29 | public: |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 30 | MetricsLibrary(); |
| 31 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 32 | // Initializes the library. |
| 33 | void Init(); |
| 34 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 35 | // Returns whether or not the machine is running in guest mode. |
| 36 | bool IsGuestMode(); |
| 37 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 38 | // Returns whether or not metrics collection is enabled. |
| 39 | bool AreMetricsEnabled(); |
| 40 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 41 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 52 | // |
| 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 57 | bool SendToUMA(const std::string& name, int sample, |
| 58 | int min, int max, int nbuckets); |
| 59 | |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 60 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 69 | // |
| 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 75 | bool SendEnumToUMA(const std::string& name, int sample, int max); |
| 76 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 77 | // 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 Erat | 6c35d7c | 2011-01-21 11:25:45 -0800 | [diff] [blame] | 79 | // 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 Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 85 | // |
| 86 | // |action| is the user-generated event (e.g., "MuteKeyPressed"). |
| 87 | bool SendUserActionToUMA(const std::string& action); |
| 88 | |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 89 | // 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 Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame^] | 93 | // 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 Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 101 | // Sends to Autotest and returns true on success. |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 102 | static bool SendToAutotest(const std::string& name, int value); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 105 | friend class CMetricsLibraryTest; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 106 | friend class MetricsLibraryTest; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 107 | FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 108 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
| 109 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 110 | FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 111 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
| 112 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
| 113 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 114 | // 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 Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 122 | // 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 James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 135 | const char* format, ...); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 136 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 137 | // This function is used by tests only to mock the device policies. |
| 138 | void SetPolicyProvider(policy::PolicyProvider* provider); |
| 139 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 140 | // 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 Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 146 | const char* uma_events_file_; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 147 | const char* consent_file_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 148 | |
| 149 | scoped_ptr<policy::PolicyProvider> policy_provider_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 152 | #endif // METRICS_LIBRARY_H_ |