1// Copyright (c) 2010 The Chromium 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
5#ifndef CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
6#define CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/containers/hash_tables.h"
12#include "base/gtest_prod_util.h"
13#include "base/memory/ref_counted.h"
14
15namespace metrics {
16class MetricSample;
17}  // namespace metrics
18
19namespace chromeos {
20
21// ExternalMetrics is a service that Chrome offers to Chrome OS to upload
22// metrics to the UMA server on its behalf.  Chrome periodically reads the
23// content of a well-know file, and parses it into name-value pairs, each
24// representing a Chrome OS metrics event. The events are logged using the
25// normal UMA mechanism. The file is then truncated to zero size. Chrome uses
26// flock() to synchronize accesses to the file.
27class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> {
28 public:
29  ExternalMetrics();
30
31  // Begins the external data collection.  This service is started and stopped
32  // by the chrome metrics service.  Calls to RecordAction originate in the
33  // File thread but are executed in the UI thread.
34  void Start();
35
36  // Creates an ExternalMetrics instance reading from |filename| for testing
37  // purpose.
38  static scoped_refptr<ExternalMetrics> CreateForTesting(
39      const std::string& filename);
40
41 private:
42  friend class base::RefCountedThreadSafe<ExternalMetrics>;
43  friend class ExternalMetricsTest;
44
45  FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CanReceiveHistogram);
46  FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, HandleMissingFile);
47  FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest,
48                           IncorrectHistogramsAreDiscarded);
49
50  // The max length of a message (name-value pair, plus header)
51  static const int kMetricsMessageMaxLength = 1024;  // be generous
52
53  ~ExternalMetrics();
54
55  // Passes an action event to the UMA service on the UI thread.
56  void RecordActionUI(std::string action_string);
57
58  // Passes an action event to the UMA service.
59  void RecordAction(const std::string& action_name);
60
61  // Records an external crash of the given string description to
62  // UMA service on the UI thread.
63  void RecordCrashUI(const std::string& crash_kind);
64
65  // Records an external crash of the given string description.
66  void RecordCrash(const std::string& crash_kind);
67
68  // Records an histogram. |sample| is expected to be an histogram.
69  void RecordHistogram(const metrics::MetricSample& sample);
70
71  // Records a sparse histogram. |sample| is expected to be a sparse histogram.
72  void RecordSparseHistogram(const metrics::MetricSample& sample);
73
74  // Records a linear histogram. |sample| is expected to be a linear histogram.
75  void RecordLinearHistogram(const metrics::MetricSample& sample);
76
77  // Collects external events from metrics log file.  This is run at periodic
78  // intervals.
79  //
80  // Returns the number of events collected.
81  int CollectEvents();
82
83  // Calls CollectEvents and reschedules a future collection.
84  void CollectEventsAndReschedule();
85
86  // Schedules a metrics event collection in the future.
87  void ScheduleCollector();
88
89  // Calls setup methods for Chrome OS field trials that need to be initialized
90  // based on data from the file system.  They are setup here so that we can
91  // make absolutely sure that they are setup before we gather UMA statistics
92  // from ChromeOS.
93  void SetupFieldTrialsOnFileThread();
94
95  // Set containing known user actions.
96  base::hash_set<std::string> valid_user_actions_;
97
98  // File used by libmetrics to send metrics to Chrome.
99  std::string uma_events_file_;
100
101  DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
102};
103
104}  // namespace chromeos
105
106#endif  // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
107