[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 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 | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 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" | ||||
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 3c1ec41 | 2010-06-02 15:22:56 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 12 | #include "base/hash_tables.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 14 | |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 15 | namespace 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. | ||||
23 | class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> { | ||||
[email protected] | 3c1ec41 | 2010-06-02 15:22:56 | [diff] [blame] | 24 | FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, ParseExternalMetricsFile); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 25 | friend class base::RefCountedThreadSafe<ExternalMetrics>; |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 26 | |
27 | public: | ||||
[email protected] | 196ff47 | 2011-01-21 19:25:27 | [diff] [blame] | 28 | ExternalMetrics(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 29 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 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(); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 34 | |
35 | private: | ||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 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. | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 40 | |
41 | // The max length of a message (name-value pair, plus header) | ||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 42 | static const int kMetricsMessageMaxLength = 1024; // be generous |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 43 | |
[email protected] | 41baad0 | 2011-05-15 20:37:46 | [diff] [blame] | 44 | ~ExternalMetrics(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 45 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 46 | // Passes an action event to the UMA service on the UI thread. |
47 | void RecordActionUI(std::string action_string); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 48 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 49 | // Passes an action event to the UMA service. |
50 | void RecordAction(const char* action_name); | ||||
51 | |||||
[email protected] | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 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 | |||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 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); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 66 | |
[email protected] | 85bbf09 | 2013-05-07 08:23:40 | [diff] [blame^] | 67 | // 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] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 71 | // 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] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 81 | // Maps histogram or action names to recorder structs. |
82 | base::hash_map<std::string, RecordFunctionType> action_recorders_; | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 83 | |
[email protected] | 196ff47 | 2011-01-21 19:25:27 | [diff] [blame] | 84 | // Set containing known user actions. |
85 | base::hash_set<std::string> valid_user_actions_; | ||||
86 | |||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 87 | // Used for testing only. |
88 | RecorderType test_recorder_; | ||||
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 89 | base::FilePath test_path_; |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 90 | DISALLOW_COPY_AND_ASSIGN(ExternalMetrics); |
91 | }; | ||||
92 | |||||
93 | } // namespace chromeos | ||||
94 | |||||
95 | #endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_ |