external_metrics.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/file_path.h"
11#include "base/gtest_prod_util.h"
12#include "base/hash_tables.h"
13#include "base/memory/ref_counted.h"
14
15namespace chromeos {
16
17// ExternalMetrics is a service that Chrome offers to Chrome OS to upload
18// metrics to the UMA server on its behalf.  Chrome periodically reads the
19// content of a well-know file, and parses it into name-value pairs, each
20// representing a Chrome OS metrics event. The events are logged using the
21// normal UMA mechanism. The file is then truncated to zero size. Chrome uses
22// flock() to synchronize accesses to the file.
23class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> {
24  FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, ParseExternalMetricsFile);
25  friend class base::RefCountedThreadSafe<ExternalMetrics>;
26
27 public:
28  ExternalMetrics();
29
30  // Begins the external data collection.  This service is started and stopped
31  // by the chrome metrics service.  Calls to RecordAction originate in the
32  // File thread but are executed in the UI thread.
33  void Start();
34
35 private:
36  // There is one function with this type for each action.
37  typedef void (*RecordFunctionType)();
38
39  typedef void (*RecorderType)(const char*, const char*);  // For testing only.
40
41  // The max length of a message (name-value pair, plus header)
42  static const int kMetricsMessageMaxLength = 1024;  // be generous
43
44  ~ExternalMetrics();
45
46  // Passes an action event to the UMA service on the UI thread.
47  void RecordActionUI(std::string action_string);
48
49  // Passes an action event to the UMA service.
50  void RecordAction(const char* action_name);
51
52  // Records an external crash of the given string description to
53  // UMA service on the UI thread.
54  void RecordCrashUI(const std::string& crash_kind);
55
56  // Records an external crash of the given string description.
57  void RecordCrash(const std::string& crash_kind);
58
59  // Passes an histogram event to the UMA service.  |histogram_data| is in the
60  // form <histogram-name> <sample> <min> <max> <buckets_count>.
61  void RecordHistogram(const char* histogram_data);
62
63  // Passes a linear histogram event to the UMA service.  |histogram_data| is
64  // in the form <histogram-name> <sample> <max>.
65  void RecordLinearHistogram(const char* histogram_data);
66
67  // Collects external events from metrics log file.  This is run at periodic
68  // intervals.
69  void CollectEvents();
70
71  // Calls CollectEvents and reschedules a future collection.
72  void CollectEventsAndReschedule();
73
74  // Schedules a metrics event collection in the future.
75  void ScheduleCollector();
76
77  // Maps histogram or action names to recorder structs.
78  base::hash_map<std::string, RecordFunctionType> action_recorders_;
79
80  // Set containing known user actions.
81  base::hash_set<std::string> valid_user_actions_;
82
83  // Used for testing only.
84  RecorderType test_recorder_;
85  FilePath test_path_;
86  DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
87};
88
89}  // namespace chromeos
90
91#endif  // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_
92