blob: 2539f02e5e348d057a9d1eec35f7feedaa6b4d09 [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>
Yunlian Jiang564c69f2012-05-02 14:24:04 -070010#include <unistd.h>
Darin Petkov65b01462010-04-14 13:32:20 -070011
Eric Shienbrood11353552012-02-29 14:52:29 -050012#include <base/memory/scoped_ptr.h>
Darin Petkov11b8eb32010-05-18 11:00:59 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkov65b01462010-04-14 13:32:20 -070014
Ken Mixterb2f17092011-07-22 14:59:51 -070015#include "policy/libpolicy.h"
16
Darin Petkovfc91b422010-05-12 13:05:45 -070017class MetricsLibraryInterface {
Darin Petkov65b01462010-04-14 13:32:20 -070018 public:
Darin Petkovfc91b422010-05-12 13:05:45 -070019 virtual void Init() = 0;
20 virtual bool SendToUMA(const std::string& name, int sample,
21 int min, int max, int nbuckets) = 0;
22 virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
Daniel Eratca90d8b2011-01-06 15:46:00 -080023 virtual bool SendUserActionToUMA(const std::string& action) = 0;
Darin Petkovfc91b422010-05-12 13:05:45 -070024 virtual ~MetricsLibraryInterface() {}
25};
26
27// Library used to send metrics to both Autotest and Chrome/UMA.
28class MetricsLibrary : public MetricsLibraryInterface {
29 public:
Darin Petkov11b8eb32010-05-18 11:00:59 -070030 MetricsLibrary();
31
Darin Petkovfc91b422010-05-12 13:05:45 -070032 // Initializes the library.
33 void Init();
34
Ken Mixtereafbbdf2010-10-01 15:38:42 -070035 // Returns whether or not the machine is running in guest mode.
36 bool IsGuestMode();
37
Ken Mixter4c5daa42010-08-26 18:35:06 -070038 // Returns whether or not metrics collection is enabled.
39 bool AreMetricsEnabled();
40
Darin Petkovc2526a12010-04-21 14:24:04 -070041 // Sends histogram data to Chrome for transport to UMA and returns
42 // true on success. This method results in the equivalent of an
43 // asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
44 // inside Chrome (see base/histogram.h).
45 //
46 // |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
47 // |min| is the minimum value of the histogram samples (|min| > 0).
48 // |max| is the maximum value of the histogram samples.
49 // |nbuckets| is the number of histogram buckets.
50 // [0,min) is the implicit underflow bucket.
51 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070052 //
53 // Note that the memory allocated in Chrome for each histogram is
54 // proportional to the number of buckets. Therefore, it is strongly
55 // recommended to keep this number low (e.g., 50 is normal, while
56 // 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070057 bool SendToUMA(const std::string& name, int sample,
58 int min, int max, int nbuckets);
59
Darin Petkov5b7dce12010-04-21 15:45:10 -070060 // Sends linear histogram data to Chrome for transport to UMA and
61 // returns true on success. This method results in the equivalent of
62 // an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
63 // inside Chrome (see base/histogram.h).
64 //
65 // |sample| is the sample value to be recorded (1 <= |sample| < |max|).
66 // |max| is the maximum value of the histogram samples.
67 // 0 is the implicit underflow bucket.
68 // [|max|,infinity) is the implicit overflow bucket.
Darin Petkovc2bf95f2010-06-21 16:27:52 -070069 //
70 // An enumaration histogram requires |max| + 1 number of
71 // buckets. Note that the memory allocated in Chrome for each
72 // histogram is proportional to the number of buckets. Therefore, it
73 // is strongly recommended to keep this number low (e.g., 50 is
74 // normal, while 100 is high).
Darin Petkovfc91b422010-05-12 13:05:45 -070075 bool SendEnumToUMA(const std::string& name, int sample, int max);
76
Darin Petkoved824852011-01-06 10:51:47 -080077 // Sends a user action to Chrome for transport to UMA and returns true on
78 // success. This method results in the equivalent of an asynchronous
Daniel Erat6c35d7c2011-01-21 11:25:45 -080079 // non-blocking RPC to UserMetrics::RecordAction. The new metric must be
80 // added to chrome/tools/extract_actions.py in the Chromium repository, which
81 // should then be run to generate a hash for the new action.
82 //
83 // Until http://crosbug.com/11125 is fixed, the metric must also be added to
84 // chrome/browser/chromeos/external_metrics.cc.
Darin Petkoved824852011-01-06 10:51:47 -080085 //
86 // |action| is the user-generated event (e.g., "MuteKeyPressed").
87 bool SendUserActionToUMA(const std::string& action);
88
Ken Mixterbe2e13b2011-01-22 06:15:56 -080089 // Sends a signal to UMA that a crash of the given |crash_kind|
90 // has occurred. Used by UMA to generate stability statistics.
91 bool SendCrashToUMA(const char *crash_kind);
92
Darin Petkov4fcb2ac2010-04-15 16:40:23 -070093 // Sends to Autotest and returns true on success.
Darin Petkovc2526a12010-04-21 14:24:04 -070094 static bool SendToAutotest(const std::string& name, int value);
Darin Petkov11b8eb32010-05-18 11:00:59 -070095
96 private:
Sam Leffler10b301d2010-06-17 14:22:43 -070097 friend class CMetricsLibraryTest;
Darin Petkov11b8eb32010-05-18 11:00:59 -070098 friend class MetricsLibraryTest;
Ken Mixter4c5daa42010-08-26 18:35:06 -070099 FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700100 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
101 FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700102 FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700103 FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
104 FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
105
Ken Mixtereafbbdf2010-10-01 15:38:42 -0700106 // Sets |*result| to whether or not the |mounts_file| indicates that
107 // the |device_name| is currently mounted. Uses |buffer| of
108 // |buffer_size| to read the file. Returns false if any error.
109 bool IsDeviceMounted(const char* device_name,
110 const char* mounts_file,
111 char* buffer, int buffer_size,
112 bool* result);
113
Darin Petkov11b8eb32010-05-18 11:00:59 -0700114 // Sends message of size |length| to Chrome for transport to UMA and
115 // returns true on success.
116 bool SendMessageToChrome(int32_t length, const char* message);
117
118 // Formats a name/value message for Chrome in |buffer| and returns the
119 // length of the message or a negative value on error.
120 //
121 // Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
122 //
123 // The arbitrary |format| argument covers the non-LENGTH portion of the
124 // message. The caller is responsible to store the \0 character
125 // between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
126 int32_t FormatChromeMessage(int32_t buffer_size, char* buffer,
David James3b3add52010-06-04 15:01:19 -0700127 const char* format, ...);
Darin Petkov11b8eb32010-05-18 11:00:59 -0700128
Ken Mixterb2f17092011-07-22 14:59:51 -0700129 // This function is used by tests only to mock the device policies.
130 void SetPolicyProvider(policy::PolicyProvider* provider);
131
Ken Mixter4c5daa42010-08-26 18:35:06 -0700132 // Time at which we last checked if metrics were enabled.
133 static time_t cached_enabled_time_;
134
135 // Cached state of whether or not metrics were enabled.
136 static bool cached_enabled_;
137
Darin Petkov11b8eb32010-05-18 11:00:59 -0700138 const char* uma_events_file_;
Ken Mixter4c5daa42010-08-26 18:35:06 -0700139 const char* consent_file_;
Ken Mixterb2f17092011-07-22 14:59:51 -0700140
141 scoped_ptr<policy::PolicyProvider> policy_provider_;
Darin Petkov65b01462010-04-14 13:32:20 -0700142};
143
David James3b3add52010-06-04 15:01:19 -0700144#endif // METRICS_LIBRARY_H_