2010-04-14 22:32:20 +02:00
|
|
|
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-07-10 01:34:29 +02:00
|
|
|
#ifndef METRICS_METRICS_LIBRARY_H_
|
|
|
|
#define METRICS_METRICS_LIBRARY_H_
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
#include <sys/types.h>
|
2010-04-14 22:32:20 +02:00
|
|
|
#include <string>
|
2012-05-02 23:24:04 +02:00
|
|
|
#include <unistd.h>
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2014-03-10 05:39:08 +01:00
|
|
|
#include <base/compiler_specific.h>
|
2014-09-04 02:23:46 +02:00
|
|
|
#include <base/macros.h>
|
2012-02-29 20:52:29 +01:00
|
|
|
#include <base/memory/scoped_ptr.h>
|
2010-05-18 20:00:59 +02:00
|
|
|
#include <gtest/gtest_prod.h> // for FRIEND_TEST
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2011-07-22 23:59:51 +02:00
|
|
|
#include "policy/libpolicy.h"
|
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
class MetricsLibraryInterface {
|
2010-04-14 22:32:20 +02:00
|
|
|
public:
|
2010-05-12 22:05:45 +02:00
|
|
|
virtual void Init() = 0;
|
2015-02-18 18:38:55 +01:00
|
|
|
virtual bool AreMetricsEnabled() = 0;
|
2010-05-12 22:05:45 +02:00
|
|
|
virtual bool SendToUMA(const std::string& name, int sample,
|
|
|
|
int min, int max, int nbuckets) = 0;
|
|
|
|
virtual bool SendEnumToUMA(const std::string& name, int sample, int max) = 0;
|
2013-03-19 23:02:42 +01:00
|
|
|
virtual bool SendSparseToUMA(const std::string& name, int sample) = 0;
|
2011-01-07 00:46:00 +01:00
|
|
|
virtual bool SendUserActionToUMA(const std::string& action) = 0;
|
2010-05-12 22:05:45 +02:00
|
|
|
virtual ~MetricsLibraryInterface() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Library used to send metrics to both Autotest and Chrome/UMA.
|
|
|
|
class MetricsLibrary : public MetricsLibraryInterface {
|
|
|
|
public:
|
2010-05-18 20:00:59 +02:00
|
|
|
MetricsLibrary();
|
2014-03-10 05:39:08 +01:00
|
|
|
virtual ~MetricsLibrary();
|
2010-05-18 20:00:59 +02:00
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
// Initializes the library.
|
2014-08-14 21:56:21 +02:00
|
|
|
void Init() override;
|
2010-05-12 22:05:45 +02:00
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
// Returns whether or not the machine is running in guest mode.
|
|
|
|
bool IsGuestMode();
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
// Returns whether or not metrics collection is enabled.
|
2015-02-18 18:38:55 +01:00
|
|
|
bool AreMetricsEnabled() override;
|
2010-08-27 03:35:06 +02:00
|
|
|
|
2010-04-21 23:24:04 +02:00
|
|
|
// Sends histogram data to Chrome for transport to UMA and returns
|
|
|
|
// true on success. This method results in the equivalent of an
|
|
|
|
// asynchronous non-blocking RPC to UMA_HISTOGRAM_CUSTOM_COUNTS
|
|
|
|
// inside Chrome (see base/histogram.h).
|
|
|
|
//
|
|
|
|
// |sample| is the sample value to be recorded (|min| <= |sample| < |max|).
|
|
|
|
// |min| is the minimum value of the histogram samples (|min| > 0).
|
|
|
|
// |max| is the maximum value of the histogram samples.
|
|
|
|
// |nbuckets| is the number of histogram buckets.
|
|
|
|
// [0,min) is the implicit underflow bucket.
|
|
|
|
// [|max|,infinity) is the implicit overflow bucket.
|
2010-06-22 01:27:52 +02:00
|
|
|
//
|
|
|
|
// Note that the memory allocated in Chrome for each histogram is
|
|
|
|
// proportional to the number of buckets. Therefore, it is strongly
|
|
|
|
// recommended to keep this number low (e.g., 50 is normal, while
|
|
|
|
// 100 is high).
|
2010-05-12 22:05:45 +02:00
|
|
|
bool SendToUMA(const std::string& name, int sample,
|
2015-01-28 22:12:38 +01:00
|
|
|
int min, int max, int nbuckets) override;
|
2010-05-12 22:05:45 +02:00
|
|
|
|
2010-04-22 00:45:10 +02:00
|
|
|
// Sends linear histogram data to Chrome for transport to UMA and
|
|
|
|
// returns true on success. This method results in the equivalent of
|
|
|
|
// an asynchronous non-blocking RPC to UMA_HISTOGRAM_ENUMERATION
|
|
|
|
// inside Chrome (see base/histogram.h).
|
|
|
|
//
|
|
|
|
// |sample| is the sample value to be recorded (1 <= |sample| < |max|).
|
|
|
|
// |max| is the maximum value of the histogram samples.
|
|
|
|
// 0 is the implicit underflow bucket.
|
|
|
|
// [|max|,infinity) is the implicit overflow bucket.
|
2010-06-22 01:27:52 +02:00
|
|
|
//
|
2014-03-10 05:39:08 +01:00
|
|
|
// An enumeration histogram requires |max| + 1 number of
|
2010-06-22 01:27:52 +02:00
|
|
|
// buckets. Note that the memory allocated in Chrome for each
|
|
|
|
// histogram is proportional to the number of buckets. Therefore, it
|
|
|
|
// is strongly recommended to keep this number low (e.g., 50 is
|
|
|
|
// normal, while 100 is high).
|
2014-08-14 21:56:21 +02:00
|
|
|
bool SendEnumToUMA(const std::string& name, int sample, int max) override;
|
2010-05-12 22:05:45 +02:00
|
|
|
|
2013-03-19 23:02:42 +01:00
|
|
|
// Sends sparse histogram sample to Chrome for transport to UMA. Returns
|
|
|
|
// true on success.
|
|
|
|
//
|
|
|
|
// |sample| is the 32-bit integer value to be recorded.
|
2014-08-14 21:56:21 +02:00
|
|
|
bool SendSparseToUMA(const std::string& name, int sample) override;
|
2013-03-19 23:02:42 +01:00
|
|
|
|
2011-01-06 19:51:47 +01:00
|
|
|
// Sends a user action to Chrome for transport to UMA and returns true on
|
|
|
|
// success. This method results in the equivalent of an asynchronous
|
2011-01-21 20:25:45 +01:00
|
|
|
// non-blocking RPC to UserMetrics::RecordAction. The new metric must be
|
|
|
|
// added to chrome/tools/extract_actions.py in the Chromium repository, which
|
|
|
|
// should then be run to generate a hash for the new action.
|
|
|
|
//
|
|
|
|
// Until http://crosbug.com/11125 is fixed, the metric must also be added to
|
|
|
|
// chrome/browser/chromeos/external_metrics.cc.
|
2011-01-06 19:51:47 +01:00
|
|
|
//
|
|
|
|
// |action| is the user-generated event (e.g., "MuteKeyPressed").
|
2014-08-14 21:56:21 +02:00
|
|
|
bool SendUserActionToUMA(const std::string& action) override;
|
2011-01-06 19:51:47 +01:00
|
|
|
|
2011-01-22 15:15:56 +01:00
|
|
|
// Sends a signal to UMA that a crash of the given |crash_kind|
|
|
|
|
// has occurred. Used by UMA to generate stability statistics.
|
|
|
|
bool SendCrashToUMA(const char *crash_kind);
|
|
|
|
|
2013-03-13 18:53:55 +01:00
|
|
|
// Sends a "generic Chrome OS event" to UMA. This is an event name
|
|
|
|
// that is translated into an enumerated histogram entry. Event names
|
|
|
|
// are added to metrics_library.cc. Optionally, they can be added
|
|
|
|
// to histograms.xml---but part of the reason for this is to simplify
|
|
|
|
// the addition of events (at the cost of having to look them up by
|
|
|
|
// number in the histograms dashboard).
|
|
|
|
bool SendCrosEventToUMA(const std::string& event);
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Sends to Autotest and returns true on success.
|
2010-04-21 23:24:04 +02:00
|
|
|
static bool SendToAutotest(const std::string& name, int value);
|
2010-05-18 20:00:59 +02:00
|
|
|
|
|
|
|
private:
|
2010-06-17 23:22:43 +02:00
|
|
|
friend class CMetricsLibraryTest;
|
2010-05-18 20:00:59 +02:00
|
|
|
friend class MetricsLibraryTest;
|
2010-08-27 03:35:06 +02:00
|
|
|
FRIEND_TEST(MetricsLibraryTest, AreMetricsEnabled);
|
2010-05-18 20:00:59 +02:00
|
|
|
FRIEND_TEST(MetricsLibraryTest, FormatChromeMessage);
|
|
|
|
FRIEND_TEST(MetricsLibraryTest, FormatChromeMessageTooLong);
|
2010-10-02 00:38:42 +02:00
|
|
|
FRIEND_TEST(MetricsLibraryTest, IsDeviceMounted);
|
2010-05-18 20:00:59 +02:00
|
|
|
FRIEND_TEST(MetricsLibraryTest, SendMessageToChrome);
|
|
|
|
FRIEND_TEST(MetricsLibraryTest, SendMessageToChromeUMAEventsBadFileLocation);
|
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
// Sets |*result| to whether or not the |mounts_file| indicates that
|
|
|
|
// the |device_name| is currently mounted. Uses |buffer| of
|
|
|
|
// |buffer_size| to read the file. Returns false if any error.
|
|
|
|
bool IsDeviceMounted(const char* device_name,
|
|
|
|
const char* mounts_file,
|
|
|
|
char* buffer, int buffer_size,
|
|
|
|
bool* result);
|
|
|
|
|
2011-07-22 23:59:51 +02:00
|
|
|
// This function is used by tests only to mock the device policies.
|
|
|
|
void SetPolicyProvider(policy::PolicyProvider* provider);
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
// Time at which we last checked if metrics were enabled.
|
|
|
|
static time_t cached_enabled_time_;
|
|
|
|
|
|
|
|
// Cached state of whether or not metrics were enabled.
|
|
|
|
static bool cached_enabled_;
|
|
|
|
|
2014-05-14 00:16:24 +02:00
|
|
|
std::string uma_events_file_;
|
|
|
|
std::string consent_file_;
|
2011-07-22 23:59:51 +02:00
|
|
|
|
|
|
|
scoped_ptr<policy::PolicyProvider> policy_provider_;
|
2014-03-10 05:39:08 +01:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(MetricsLibrary);
|
2010-04-14 22:32:20 +02:00
|
|
|
};
|
|
|
|
|
2014-07-10 01:34:29 +02:00
|
|
|
#endif // METRICS_METRICS_LIBRARY_H_
|