blob: 38ab47fdbbf785f7bf163327099d4ed98bec4c29 [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;
22 virtual bool SendToUMA(const std::string& name, int sample,
23 int min, int max, int nbuckets) = 0;
24 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070025 virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080026 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070027 virtual ~MetricsLibraryInterface() {}
28};
29
30// Library used to send metrics to both Autotest and Chrome/UMA.
31class MetricsLibrary : public MetricsLibraryInterface {
32 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070033 MetricsLibrary();
Daniel Eratfd158292014-03-09 21:39:08 -070034 virtual ~MetricsLibrary();
Darin Petkov11b8eb32010-05-18 11:00:59 -070035
Darin Petkovfc91b422010-05-12 13:05:45 -070036 // Initializes the library.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070037 void Init() override;
Darin Petkovfc91b422010-05-12 13:05:45 -070038
Ken Mixtereafbbdf2010-10-01 15:38:42 -070039 // Returns whether or not the machine is running in guest mode.
40 bool IsGuestMode();
41
Ken Mixter4c5daa42010-08-26 18:35:06 -070042 // Returns whether or not metrics collection is enabled.
43 bool AreMetricsEnabled();
44
Darin Petkovc2526a12010-04-21 14:24:04 -070045 // Sends histogram data to Chrome for transport to UMA and returns
46 // true on success. This method results in the equivalent of an
47 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
48 // inside Chrome (see base/histogram.h).
49 //
50 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
51 // |min| is the minimum value of the histogram samples (|min| > 0).
52 // |max| is the maximum value of the histogram samples.
53 // |nbuckets| is the number of histogram buckets.
54 // [0,min) is the implicit underflow bucket.
55 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070056 //
57 // Note that the memory allocated in Chrome for each histogram is
58 // proportional to the number of buckets. Therefore, it is strongly
59 // recommended to keep this number low (e.g., 50 is normal, while
60 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070061 bool SendToUMA(const std::string& name, int sample,
62 int min, int max, int nbuckets);
63
Darin Petkov5b7dce12010-04-21 15:45:10 -070064 // Sends linear histogram data to Chrome for transport to UMA and
65 // returns true on success. This method results in the equivalent of
66 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
67 // inside Chrome (see base/histogram.h).
68 //
69 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
70 // |max| is the maximum value of the histogram samples.
71 // 0 is the implicit underflow bucket.
72 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070073 //
Daniel Eratfd158292014-03-09 21:39:08 -070074 // An enumeration histogram requires |max| + 1 number of
Darin Petkovc2bf95f2010-06-21 16:27:52 -070075 // buckets. Note that the memory allocated in Chrome for each
76 // histogram is proportional to the number of buckets. Therefore, it
77 // is strongly recommended to keep this number low (e.g., 50 is
78 // normal, while 100 is high).
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070079 bool SendEnumToUMA(const std::string& name, int sample, int max) override;
Darin Petkovfc91b422010-05-12 13:05:45 -070080
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070081 // Sends sparse histogram sample to Chrome for transport to UMA. Returns
82 // true on success.
83 //
84 // |sample| is the 32-bit integer value to be recorded.
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070085 bool SendSparseToUMA(const std::string& name, int sample) override;
Luigi Semenzatoa7ebeb32013-03-19 15:02:42 -070086
Darin Petkoved824852011-01-06 10:51:47 -080087 // Sends a user action to Chrome for transport to UMA and returns true on
88 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080089 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
90 // added to chrome/tools/extract_actions.py in the Chromium repository, which
91 // should then be run to generate a hash for the new action.
92 //
93 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
94 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080095 //
96 // |action| is the user-generated event (e.g., "MuteKeyPressed").
Alex Vakulenkoe8a8e302014-08-14 12:56:21 -070097 bool SendUserActionToUMA(const std::string& action) override;
Darin Petkoved824852011-01-06 10:51:47 -080098
Ken Mixterbe2e13b2011-01-22 06:15:56 -080099 // Sends a signal to UMA that a crash of the given |crash_kind|
100 // has occurred. Used by UMA to generate stability statistics.
101 bool SendCrashToUMA(const char *crash_kind);
102
Luigi Semenzato32684222013-03-13 10:53:55 -0700103 // Sends a "generic Chrome OS event" to UMA. This is an event name
104 // that is translated into an enumerated histogram entry. Event names
105 // are added to metrics_library.cc. Optionally, they can be added
106 // to histograms.xml---but part of the reason for this is to simplify
107 // the addition of events (at the cost of having to look them up by
108 // number in the histograms dashboard).
109 bool SendCrosEventToUMA(const std::string& event);
110
Darin Petkov4fcb2ac2010-04-15 16:40:23 -0700111 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -0700112 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700113
114 private:
Sam Leffler10b301d2010-06-17 14:22:43 -0700115 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -0700116 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700117 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700118 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
119 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700120 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700121 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
122 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
123
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700124 // Sets |*result| to whether or not the |mounts_file| indicates that
125 // the |device_name| is currently mounted. Uses |buffer| of
126 // |buffer_size| to read the file. Returns false if any error.
127 bool IsDeviceMounted(const char* device_name,
128 const char* mounts_file,
129 char* buffer, int buffer_size,
130 bool* result);
131
Ken Mixterb2f17092011-07-22 14:59:51 -0700132 // This function is used by tests only to mock the device policies.
133 void SetPolicyProvider(policy::PolicyProvider* provider);
134
Ken Mixter4c5daa42010-08-26 18:35:06 -0700135 // Time at which we last checked if metrics were enabled.
136 static time_t cached_enabled_time_;
137
138 // Cached state of whether or not metrics were enabled.
139 static bool cached_enabled_;
140
Luigi Semenzato41c54502014-05-13 15:16:24 -0700141 std::string uma_events_file_;
142 std::string consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700143
144 scoped_ptr<policy::PolicyProvider> policy_provider_;
Daniel Eratfd158292014-03-09 21:39:08 -0700145
146 DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
Darin Petkov65b01462010-04-14 13:32:20 -0700147};
148
Bertrand SIMONNETd83ca802014-07-09 16:34:29 -0700149#endif // METRICS_METRICS_LIBRARY_H_