blob: 4ea0663ba5093469781a44e724141d7d79d786be [file] [log] [blame]
[email protected]29cf16772010-04-21 15:13:471// 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
[email protected]5ccaa412009-11-13 22:00:163// 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"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]3c1ec412010-06-02 15:22:5611#include "base/gtest_prod_util.h"
[email protected]29cf16772010-04-21 15:13:4712#include "base/hash_tables.h"
[email protected]c6944272012-01-06 22:12:2813#include "base/memory/ref_counted.h"
[email protected]5ccaa412009-11-13 22:00:1614
[email protected]5ccaa412009-11-13 22:00:1615namespace 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> {
[email protected]3c1ec412010-06-02 15:22:5624 FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, ParseExternalMetricsFile);
[email protected]5ccaa412009-11-13 22:00:1625 friend class base::RefCountedThreadSafe<ExternalMetrics>;
[email protected]5ccaa412009-11-13 22:00:1626
27 public:
[email protected]196ff472011-01-21 19:25:2728 ExternalMetrics();
[email protected]5ccaa412009-11-13 22:00:1629
[email protected]29cf16772010-04-21 15:13:4730 // 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();
[email protected]5ccaa412009-11-13 22:00:1634
35 private:
[email protected]29cf16772010-04-21 15:13:4736 // 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.
[email protected]5ccaa412009-11-13 22:00:1640
41 // The max length of a message (name-value pair, plus header)
[email protected]29cf16772010-04-21 15:13:4742 static const int kMetricsMessageMaxLength = 1024; // be generous
[email protected]5ccaa412009-11-13 22:00:1643
[email protected]41baad02011-05-15 20:37:4644 ~ExternalMetrics();
[email protected]5ccaa412009-11-13 22:00:1645
[email protected]29cf16772010-04-21 15:13:4746 // Passes an action event to the UMA service on the UI thread.
47 void RecordActionUI(std::string action_string);
[email protected]5ccaa412009-11-13 22:00:1648
[email protected]29cf16772010-04-21 15:13:4749 // Passes an action event to the UMA service.
50 void RecordAction(const char* action_name);
51
[email protected]c1834a92011-01-21 18:21:0352 // 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
[email protected]29cf16772010-04-21 15:13:4759 // 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);
[email protected]5ccaa412009-11-13 22:00:1666
[email protected]85bbf092013-05-07 08:23:4067 // Passes a sparse histogram event to the UMA service. |histogram_data| is
68 // in the form <histogram-name> <sample>.
69 void RecordSparseHistogram(const char* histogram_data);
70
[email protected]5ccaa412009-11-13 22:00:1671 // Collects external events from metrics log file. This is run at periodic
72 // intervals.
73 void CollectEvents();
74
75 // Calls CollectEvents and reschedules a future collection.
76 void CollectEventsAndReschedule();
77
78 // Schedules a metrics event collection in the future.
79 void ScheduleCollector();
80
[email protected]29cf16772010-04-21 15:13:4781 // Maps histogram or action names to recorder structs.
82 base::hash_map<std::string, RecordFunctionType> action_recorders_;
[email protected]5ccaa412009-11-13 22:00:1683
[email protected]196ff472011-01-21 19:25:2784 // Set containing known user actions.
85 base::hash_set<std::string> valid_user_actions_;
86
[email protected]29cf16772010-04-21 15:13:4787 // Used for testing only.
88 RecorderType test_recorder_;
[email protected]650b2d52013-02-10 03:41:4589 base::FilePath test_path_;
[email protected]5ccaa412009-11-13 22:00:1690 DISALLOW_COPY_AND_ASSIGN(ExternalMetrics);
91};
92
93} // namespace chromeos
94
95#endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_