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