blob: a90f3e61a9944518756b9d6d9228c03ed872a109 [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
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -07005#ifndef METRICS_METRICS_LIBRARY_H_
6#define METRICS_METRICS_LIBRARY_H_
Darin Petkov65b01462010-04-14 13:32:20 -07007
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/compiler_specific.h>
Ben Chan652d6972014-09-03 17:23:46 -070013#include <base/macros.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;
Bertrand SIMONNETe4fa61e2015-02-18 09:38:55 -080022 virtual bool AreMetricsEnabled() = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070023 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 Semenzatoa7ebeb32013-03-19 15:02:42 -070026 virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080027 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070028 virtual ~MetricsLibraryInterface() {}
29};
30
31// Library used to send metrics to both Autotest and Chrome/UMA.
32class MetricsLibrary : public MetricsLibraryInterface {
33 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070034 MetricsLibrary();
Daniel Eratfd158292014-03-09 21:39:08 -070035 virtual ~MetricsLibrary();
Darin Petkov11b8eb32010-05-18 11:00:59 -070036
Darin Petkovfc91b422010-05-12 13:05:45 -070037 // Initializes the library.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070038 void Init() override;
Darin Petkovfc91b422010-05-12 13:05:45 -070039
Ken Mixtereafbbdf2010-10-01 15:38:42 -070040 // Returns whether or not the machine is running in guest mode.
41 bool IsGuestMode();
42
Ken Mixter4c5daa42010-08-26 18:35:06 -070043 // Returns whether or not metrics collection is enabled.
Bertrand SIMONNETe4fa61e2015-02-18 09:38:55 -080044 bool AreMetricsEnabled() override;
Ken Mixter4c5daa42010-08-26 18:35:06 -070045
Darin Petkovc2526a12010-04-21 14:24:04 -070046 // 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 Petkovc2bf95f2010-06-21 16:27:52 -070057 //
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 Petkovfc91b422010-05-12 13:05:45 -070062 bool SendToUMA(const std::string& name, int sample,
Yunlian Jiang5a6ac9c2015-01-28 13:12:38 -080063 int min, int max, int nbuckets) override;
Darin Petkovfc91b422010-05-12 13:05:45 -070064
Darin Petkov5b7dce12010-04-21 15:45:10 -070065 // 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 Petkovc2bf95f2010-06-21 16:27:52 -070074 //
Daniel Eratfd158292014-03-09 21:39:08 -070075 // An enumeration histogram requires |max| + 1 number of
Darin Petkovc2bf95f2010-06-21 16:27:52 -070076 // 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 Vakulenkoe8a8e302014-08-14 12:56:21 -070080 bool SendEnumToUMA(const std::string& name, int sample, int max) override;
Darin Petkovfc91b422010-05-12 13:05:45 -070081
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070082 // 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 Vakulenkoe8a8e302014-08-14 12:56:21 -070086 bool SendSparseToUMA(const std::string& name, int sample) override;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070087
Darin Petkoved824852011-01-06 10:51:47 -080088 // 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 Erat6c35d7c2011-01-21 11:25:45 -080090 // 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 Petkoved824852011-01-06 10:51:47 -080096 //
97 // |action| is the user-generated event (e.g., "MuteKeyPressed").
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070098 bool SendUserActionToUMA(const std::string& action) override;
Darin Petkoved824852011-01-06 10:51:47 -080099
Ken Mixterbe2e13b2011-01-22 06:15:56 -0800100 // 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 Semenzato32684222013-03-13 10:53:55 -0700104 // 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 Petkov4fcb2ac2010-04-15 16:40:23 -0700112 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -0700113 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700114
115 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700116 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700117 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700118 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700119 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
120 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700121 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700122 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
123 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
124
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700125 // 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 Mixterb2f17092011-07-22 14:59:51 -0700133 // This function is used by tests only to mock the device policies.
134 void SetPolicyProvider(policy::PolicyProvider* provider);
135
Ken Mixter4c5daa42010-08-26 18:35:06 -0700136 // 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 Semenzato41c54502014-05-13 15:16:24 -0700142 std::string uma_events_file_;
143 std::string consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700144
145 scoped_ptr<policy::PolicyProvider> policy_provider_;
Daniel Eratfd158292014-03-09 21:39:08 -0700146
147 DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
Darin Petkov65b01462010-04-14 13:32:20 -0700148};
149
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700150#endif // METRICS_METRICS_LIBRARY_H_