blob: f8ce6c74786273893d32abe157044fdfc89505ae [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
Darin Petkov65b01462010-04-14 13:32:20 -07005#ifndef METRICS_LIBRARY_H_
6#define METRICS_LIBRARY_H_
7
Darin Petkov11b8eb32010-05-18 11:00:59 -07008#include <sys/types.h>
Darin Petkov65b01462010-04-14 13:32:20 -07009#include <string>
10
Eric Shienbrood11353552012-02-29 14:52:29 -050011#include <base/memory/scoped_ptr.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070013
Ken Mixterb2f17092011-07-22 14:59:51 -070014#include "policy/libpolicy.h"
15
Darin Petkovfc91b422010-05-12 13:05:45 -070016class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070017 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070018 virtual void Init() = 0;
19 virtual bool SendToUMA(const std::string& name, int sample,
20 int min, int max, int nbuckets) = 0;
21 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080022 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070023 virtual ~MetricsLibraryInterface() {}
24};
25
26// Library used to send metrics to both Autotest and Chrome/UMA.
27class MetricsLibrary : public MetricsLibraryInterface {
28 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070029 MetricsLibrary();
30
Darin Petkovfc91b422010-05-12 13:05:45 -070031 // Initializes the library.
32 void Init();
33
Ken Mixtereafbbdf2010-10-01 15:38:42 -070034 // Returns whether or not the machine is running in guest mode.
35 bool IsGuestMode();
36
Ken Mixter4c5daa42010-08-26 18:35:06 -070037 // Returns whether or not metrics collection is enabled.
38 bool AreMetricsEnabled();
39
Darin Petkovc2526a12010-04-21 14:24:04 -070040 // Sends histogram data to Chrome for transport to UMA and returns
41 // true on success. This method results in the equivalent of an
42 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
43 // inside Chrome (see base/histogram.h).
44 //
45 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
46 // |min| is the minimum value of the histogram samples (|min| > 0).
47 // |max| is the maximum value of the histogram samples.
48 // |nbuckets| is the number of histogram buckets.
49 // [0,min) is the implicit underflow bucket.
50 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070051 //
52 // Note that the memory allocated in Chrome for each histogram is
53 // proportional to the number of buckets. Therefore, it is strongly
54 // recommended to keep this number low (e.g., 50 is normal, while
55 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070056 bool SendToUMA(const std::string& name, int sample,
57 int min, int max, int nbuckets);
58
Darin Petkov5b7dce12010-04-21 15:45:10 -070059 // Sends linear histogram data to Chrome for transport to UMA and
60 // returns true on success. This method results in the equivalent of
61 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
62 // inside Chrome (see base/histogram.h).
63 //
64 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
65 // |max| is the maximum value of the histogram samples.
66 // 0 is the implicit underflow bucket.
67 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070068 //
69 // An enumaration histogram requires |max| + 1 number of
70 // buckets. Note that the memory allocated in Chrome for each
71 // histogram is proportional to the number of buckets. Therefore, it
72 // is strongly recommended to keep this number low (e.g., 50 is
73 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070074 bool SendEnumToUMA(const std::string& name, int sample, int max);
75
Darin Petkoved824852011-01-06 10:51:47 -080076 // Sends a user action to Chrome for transport to UMA and returns true on
77 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080078 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
79 // added to chrome/tools/extract_actions.py in the Chromium repository, which
80 // should then be run to generate a hash for the new action.
81 //
82 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
83 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080084 //
85 // |action| is the user-generated event (e.g., "MuteKeyPressed").
86 bool SendUserActionToUMA(const std::string& action);
87
Ken Mixterbe2e13b2011-01-22 06:15:56 -080088 // Sends a signal to UMA that a crash of the given |crash_kind|
89 // has occurred. Used by UMA to generate stability statistics.
90 bool SendCrashToUMA(const char *crash_kind);
91
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070092 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -070093 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -070094
95 private:
Sam Leffler10b301d2010-06-17 14:22:43 -070096 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -070097 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -070098 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -070099 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
100 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700101 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700102 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
103 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
104
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700105 // Sets |*result| to whether or not the |mounts_file| indicates that
106 // the |device_name| is currently mounted. Uses |buffer| of
107 // |buffer_size| to read the file. Returns false if any error.
108 bool IsDeviceMounted(const char* device_name,
109 const char* mounts_file,
110 char* buffer, int buffer_size,
111 bool* result);
112
Darin Petkov11b8eb32010-05-18 11:00:59 -0700113 // Sends message of size |length| to Chrome for transport to UMA and
114 // returns true on success.
115 bool SendMessageToChrome(int32_t length, const char* message);
116
117 // Formats a name/value message for Chrome in |buffer| and returns the
118 // length of the message or a negative value on error.
119 //
120 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
121 //
122 // The arbitrary |format| argument covers the non-LENGTH portion of the
123 // message. The caller is responsible to store the \0 character
124 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
125 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700126 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700127
Ken Mixterb2f17092011-07-22 14:59:51 -0700128 // This function is used by tests only to mock the device policies.
129 void SetPolicyProvider(policy::PolicyProvider* provider);
130
Ken Mixter4c5daa42010-08-26 18:35:06 -0700131 // Time at which we last checked if metrics were enabled.
132 static time_t cached_enabled_time_;
133
134 // Cached state of whether or not metrics were enabled.
135 static bool cached_enabled_;
136
Darin Petkov11b8eb32010-05-18 11:00:59 -0700137 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700138 const char* consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700139
140 scoped_ptr<policy::PolicyProvider> policy_provider_;
Darin Petkov65b01462010-04-14 13:32:20 -0700141};
142
David James3b3add52010-06-04 15:01:19 -0700143#endif // METRICS_LIBRARY_H_