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> |
| 10 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 12 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 13 | class MetricsLibraryInterface { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 14 | public: |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 15 | 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 Erat | ca90d8b | 2011-01-06 15:46:00 -0800 | [diff] [blame^] | 19 | virtual bool SendUserActionToUMA(const std::string& action) = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 20 | virtual ~MetricsLibraryInterface() {} |
| 21 | }; |
| 22 | |
| 23 | // Library used to send metrics to both Autotest and Chrome/UMA. |
| 24 | class MetricsLibrary : public MetricsLibraryInterface { |
| 25 | public: |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 26 | MetricsLibrary(); |
| 27 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 28 | // Initializes the library. |
| 29 | void Init(); |
| 30 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 31 | // Returns whether or not the machine is running in guest mode. |
| 32 | bool IsGuestMode(); |
| 33 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 34 | // Returns whether or not metrics collection is enabled. |
| 35 | bool AreMetricsEnabled(); |
| 36 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 37 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 48 | // |
| 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 53 | bool SendToUMA(const std::string& name, int sample, |
| 54 | int min, int max, int nbuckets); |
| 55 | |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 56 | // 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 Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 65 | // |
| 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 Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 71 | bool SendEnumToUMA(const std::string& name, int sample, int max); |
| 72 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 73 | // 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 |
| 75 | // non-blocking RPC to UserMetrics::RecordAction (see the comments in |
| 76 | // chrome/browser/chromeos/external_metrics.cc and |
| 77 | // chrome/browser/metrics/user_metrics.h on how to register new user actions). |
| 78 | // |
| 79 | // |action| is the user-generated event (e.g., "MuteKeyPressed"). |
| 80 | bool SendUserActionToUMA(const std::string& action); |
| 81 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 82 | // Sends to Autotest and returns true on success. |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 83 | static bool SendToAutotest(const std::string& name, int value); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 86 | friend class CMetricsLibraryTest; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 87 | friend class MetricsLibraryTest; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 88 | FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 89 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
| 90 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 91 | FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 92 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
| 93 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
| 94 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 95 | // Sets |*result| to whether or not the |mounts_file| indicates that |
| 96 | // the |device_name| is currently mounted. Uses |buffer| of |
| 97 | // |buffer_size| to read the file. Returns false if any error. |
| 98 | bool IsDeviceMounted(const char* device_name, |
| 99 | const char* mounts_file, |
| 100 | char* buffer, int buffer_size, |
| 101 | bool* result); |
| 102 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 103 | // Sends message of size |length| to Chrome for transport to UMA and |
| 104 | // returns true on success. |
| 105 | bool SendMessageToChrome(int32_t length, const char* message); |
| 106 | |
| 107 | // Formats a name/value message for Chrome in |buffer| and returns the |
| 108 | // length of the message or a negative value on error. |
| 109 | // |
| 110 | // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 | |
| 111 | // |
| 112 | // The arbitrary |format| argument covers the non-LENGTH portion of the |
| 113 | // message. The caller is responsible to store the \0 character |
| 114 | // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value). |
| 115 | int32_t FormatChromeMessage(int32_t buffer_size, char* buffer, |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 116 | const char* format, ...); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 117 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 118 | // Time at which we last checked if metrics were enabled. |
| 119 | static time_t cached_enabled_time_; |
| 120 | |
| 121 | // Cached state of whether or not metrics were enabled. |
| 122 | static bool cached_enabled_; |
| 123 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 124 | const char* uma_events_file_; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 125 | const char* consent_file_; |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
David James | 3b3add5 | 2010-06-04 15:01:19 -0700 | [diff] [blame] | 128 | #endif // METRICS_LIBRARY_H_ |