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 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 5 | #ifndef METRICS_METRICS_LIBRARY_H_ |
| 6 | #define METRICS_METRICS_LIBRARY_H_ |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 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 | |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 12 | #include <base/compiler_specific.h> |
Ben Chan | 652d697 | 2014-09-03 17:23:46 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Eric Shienbrood | 1135355 | 2012-02-29 14:52:29 -0500 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 17 | #include "policy/libpolicy.h" |
| 18 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 19 | class MetricsLibraryInterface { |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 20 | public: |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 21 | virtual void Init() = 0; |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 22 | virtual bool AreMetricsEnabled() = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 23 | virtual bool SendToUMA(const std::string& name, int sample, |
| 24 | int min, int max, int nbuckets) = 0; |
| 25 | virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0; |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 26 | virtual bool SendSparseToUMA(const std::string& name, int sample) = 0; |
Daniel Erat | ca90d8b | 2011-01-06 15:46:00 -0800 | [diff] [blame] | 27 | virtual bool SendUserActionToUMA(const std::string& action) = 0; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 28 | virtual ~MetricsLibraryInterface() {} |
| 29 | }; |
| 30 | |
| 31 | // Library used to send metrics to both Autotest and Chrome/UMA. |
| 32 | class MetricsLibrary : public MetricsLibraryInterface { |
| 33 | public: |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 34 | MetricsLibrary(); |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 35 | virtual ~MetricsLibrary(); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 36 | |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 37 | // Initializes the library. |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 38 | void Init() override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 39 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 40 | // Returns whether or not the machine is running in guest mode. |
| 41 | bool IsGuestMode(); |
| 42 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 43 | // Returns whether or not metrics collection is enabled. |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 44 | bool AreMetricsEnabled() override; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 45 | |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 46 | // Sends histogram data to Chrome for transport to UMA and returns |
| 47 | // true on success. This method results in the equivalent of an |
| 48 | // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS |
| 49 | // inside Chrome (see base/histogram.h). |
| 50 | // |
| 51 | // |sample| is the sample value to be recorded (|min| <= |sample| < |max|). |
| 52 | // |min| is the minimum value of the histogram samples (|min| > 0). |
| 53 | // |max| is the maximum value of the histogram samples. |
| 54 | // |nbuckets| is the number of histogram buckets. |
| 55 | // [0,min) is the implicit underflow bucket. |
| 56 | // [|max|,infinity) is the implicit overflow bucket. |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 57 | // |
| 58 | // Note that the memory allocated in Chrome for each histogram is |
| 59 | // proportional to the number of buckets. Therefore, it is strongly |
| 60 | // recommended to keep this number low (e.g., 50 is normal, while |
| 61 | // 100 is high). |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 62 | bool SendToUMA(const std::string& name, int sample, |
Yunlian Jiang | 5a6ac9c | 2015-01-28 13:12:38 -0800 | [diff] [blame] | 63 | int min, int max, int nbuckets) override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 64 | |
Darin Petkov | 5b7dce1 | 2010-04-21 15:45:10 -0700 | [diff] [blame] | 65 | // Sends linear histogram data to Chrome for transport to UMA and |
| 66 | // returns true on success. This method results in the equivalent of |
| 67 | // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION |
| 68 | // inside Chrome (see base/histogram.h). |
| 69 | // |
| 70 | // |sample| is the sample value to be recorded (1 <= |sample| < |max|). |
| 71 | // |max| is the maximum value of the histogram samples. |
| 72 | // 0 is the implicit underflow bucket. |
| 73 | // [|max|,infinity) is the implicit overflow bucket. |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 74 | // |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 75 | // An enumeration histogram requires |max| + 1 number of |
Darin Petkov | c2bf95f | 2010-06-21 16:27:52 -0700 | [diff] [blame] | 76 | // buckets. Note that the memory allocated in Chrome for each |
| 77 | // histogram is proportional to the number of buckets. Therefore, it |
| 78 | // is strongly recommended to keep this number low (e.g., 50 is |
| 79 | // normal, while 100 is high). |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 80 | bool SendEnumToUMA(const std::string& name, int sample, int max) override; |
Darin Petkov | fc91b42 | 2010-05-12 13:05:45 -0700 | [diff] [blame] | 81 | |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 82 | // Sends sparse histogram sample to Chrome for transport to UMA. Returns |
| 83 | // true on success. |
| 84 | // |
| 85 | // |sample| is the 32-bit integer value to be recorded. |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 86 | bool SendSparseToUMA(const std::string& name, int sample) override; |
Luigi Semenzato | a7ebeb3 | 2013-03-19 15:02:42 -0700 | [diff] [blame] | 87 | |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 88 | // Sends a user action to Chrome for transport to UMA and returns true on |
| 89 | // success. This method results in the equivalent of an asynchronous |
Daniel Erat | 6c35d7c | 2011-01-21 11:25:45 -0800 | [diff] [blame] | 90 | // non-blocking RPC to UserMetrics::RecordAction. The new metric must be |
| 91 | // added to chrome/tools/extract_actions.py in the Chromium repository, which |
| 92 | // should then be run to generate a hash for the new action. |
| 93 | // |
| 94 | // Until http://crosbug.com/11125 is fixed, the metric must also be added to |
| 95 | // chrome/browser/chromeos/external_metrics.cc. |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 96 | // |
| 97 | // |action| is the user-generated event (e.g., "MuteKeyPressed"). |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 98 | bool SendUserActionToUMA(const std::string& action) override; |
Darin Petkov | ed82485 | 2011-01-06 10:51:47 -0800 | [diff] [blame] | 99 | |
Ken Mixter | be2e13b | 2011-01-22 06:15:56 -0800 | [diff] [blame] | 100 | // Sends a signal to UMA that a crash of the given |crash_kind| |
| 101 | // has occurred. Used by UMA to generate stability statistics. |
| 102 | bool SendCrashToUMA(const char *crash_kind); |
| 103 | |
Luigi Semenzato | 3268422 | 2013-03-13 10:53:55 -0700 | [diff] [blame] | 104 | // Sends a "generic Chrome OS event" to UMA. This is an event name |
| 105 | // that is translated into an enumerated histogram entry. Event names |
| 106 | // are added to metrics_library.cc. Optionally, they can be added |
| 107 | // to histograms.xml---but part of the reason for this is to simplify |
| 108 | // the addition of events (at the cost of having to look them up by |
| 109 | // number in the histograms dashboard). |
| 110 | bool SendCrosEventToUMA(const std::string& event); |
| 111 | |
Darin Petkov | 4fcb2ac | 2010-04-15 16:40:23 -0700 | [diff] [blame] | 112 | // Sends to Autotest and returns true on success. |
Darin Petkov | c2526a1 | 2010-04-21 14:24:04 -0700 | [diff] [blame] | 113 | static bool SendToAutotest(const std::string& name, int value); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
Sam Leffler | 10b301d | 2010-06-17 14:22:43 -0700 | [diff] [blame] | 116 | friend class CMetricsLibraryTest; |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 117 | friend class MetricsLibraryTest; |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 118 | FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 119 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage); |
| 120 | FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong); |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 121 | FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted); |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 122 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome); |
| 123 | FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation); |
| 124 | |
Ken Mixter | eafbbdf | 2010-10-01 15:38:42 -0700 | [diff] [blame] | 125 | // Sets |*result| to whether or not the |mounts_file| indicates that |
| 126 | // the |device_name| is currently mounted. Uses |buffer| of |
| 127 | // |buffer_size| to read the file. Returns false if any error. |
| 128 | bool IsDeviceMounted(const char* device_name, |
| 129 | const char* mounts_file, |
| 130 | char* buffer, int buffer_size, |
| 131 | bool* result); |
| 132 | |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 133 | // This function is used by tests only to mock the device policies. |
| 134 | void SetPolicyProvider(policy::PolicyProvider* provider); |
| 135 | |
Ken Mixter | 4c5daa4 | 2010-08-26 18:35:06 -0700 | [diff] [blame] | 136 | // Time at which we last checked if metrics were enabled. |
| 137 | static time_t cached_enabled_time_; |
| 138 | |
| 139 | // Cached state of whether or not metrics were enabled. |
| 140 | static bool cached_enabled_; |
| 141 | |
Luigi Semenzato | 41c5450 | 2014-05-13 15:16:24 -0700 | [diff] [blame] | 142 | std::string uma_events_file_; |
| 143 | std::string consent_file_; |
Ken Mixter | b2f1709 | 2011-07-22 14:59:51 -0700 | [diff] [blame] | 144 | |
| 145 | scoped_ptr<policy::PolicyProvider> policy_provider_; |
Daniel Erat | fd15829 | 2014-03-09 21:39:08 -0700 | [diff] [blame] | 146 | |
| 147 | DISALLOW_COPY_AND_ASSIGN(MetricsLibrary); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Bertrand SIMONNET | d83ca80 | 2014-07-09 16:34:29 -0700 | [diff] [blame] | 150 | #endif // METRICS_METRICS_LIBRARY_H_ |