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 #include "chrome/browser/chromeos/external_metrics.h" | 5 #include "chrome/browser/chromeos/external_metrics.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 #include <sys/file.h> | 12 #include <sys/file.h> |
13 #include <sys/stat.h> | 13 #include <sys/stat.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/eintr_wrapper.h" | 17 #include "base/eintr_wrapper.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/perftimer.h" | 19 #include "base/perftimer.h" |
20 #include "base/time.h" | 20 #include "base/time.h" |
21 #include "chrome/browser/browser_thread.h" | 21 #include "chrome/browser/browser_thread.h" |
22 #include "chrome/browser/metrics/user_metrics.h" | 22 #include "chrome/browser/metrics/user_metrics.h" |
23 | 23 |
24 // Steps to add an action. | |
25 // | |
26 // 1. Enter a helper function that calls UserMetrics::RecordAction. | |
27 // | |
28 // 2. Add a line for that function in InitializeUserActions. | |
29 // | |
30 // 3. Enjoy the recompilation. | |
31 // | |
32 // TODO(semenzato): should see if it is possible to avoid recompiling code | |
33 // every time a new user action is added, and register it in some other way. | |
34 | |
35 namespace chromeos { | 24 namespace chromeos { |
36 | 25 |
37 // The interval between external metrics collections, in milliseconds. | 26 // The interval between external metrics collections, in milliseconds. |
38 static const int kExternalMetricsCollectionIntervalMs = 30 * 1000; | 27 static const int kExternalMetricsCollectionIntervalMs = 30 * 1000; |
39 | 28 |
40 // There is one of the following functions for every user action as we have to | 29 ExternalMetrics::ExternalMetrics() |
41 // call RecordAction in a way that gets picked up by the processing scripts. | 30 : test_recorder_(NULL) { |
42 static void RecordTabOverviewKeystroke() { | |
43 UserMetrics::RecordAction(UserMetricsAction("TabOverview_Keystroke")); | |
44 } | |
45 | |
46 static void RecordTabOverviewExitMouse() { | |
47 UserMetrics::RecordAction(UserMetricsAction("TabOverview_ExitMouse")); | |
48 } | 31 } |
49 | 32 |
50 void ExternalMetrics::Start() { | 33 void ExternalMetrics::Start() { |
51 InitializeUserActions(); | 34 // Register user actions external to the browser. |
35 // chrome/tools/extract_actions.py won't understand these lines, so all of | |
36 // these are explicitly added in that script. | |
37 // TODO(derat): We shouldn't need to verify actions before reporting them; | |
petkov
2011/01/21 18:03:42
Or we can modify the regular expression in extract
| |
38 // remove all of this once http://crosbug.com/11125 is fixed. | |
39 valid_user_actions_.insert("Accel_NextWindow_Tab"); | |
40 valid_user_actions_.insert("Accel_PrevWindow_Tab"); | |
41 valid_user_actions_.insert("Accel_NextWindow_F5"); | |
42 valid_user_actions_.insert("Accel_PrevWindow_F5"); | |
43 valid_user_actions_.insert("Accel_BrightnessDown_F6"); | |
44 valid_user_actions_.insert("Accel_BrightnessUp_F7"); | |
45 | |
52 ScheduleCollector(); | 46 ScheduleCollector(); |
53 } | 47 } |
54 | 48 |
55 void ExternalMetrics::DefineUserAction(const std::string& name, | |
56 RecordFunctionType f) { | |
57 DCHECK(action_recorders_.find(name) == action_recorders_.end()); | |
58 action_recorders_[name] = f; | |
59 } | |
60 | |
61 void ExternalMetrics::InitializeUserActions() { | |
62 DefineUserAction("TabOverviewExitMouse", RecordTabOverviewExitMouse); | |
63 DefineUserAction("TabOverviewKeystroke", RecordTabOverviewKeystroke); | |
64 } | |
65 | |
66 void ExternalMetrics::RecordActionUI(std::string action_string) { | 49 void ExternalMetrics::RecordActionUI(std::string action_string) { |
67 base::hash_map<std::string, RecordFunctionType>::const_iterator iterator; | 50 if (valid_user_actions_.count(action_string)) { |
68 iterator = action_recorders_.find(action_string); | 51 UserMetrics::RecordComputedAction(action_string); |
69 if (iterator == action_recorders_.end()) { | 52 } else { |
70 LOG(ERROR) << "undefined UMA action: " << action_string; | 53 LOG(ERROR) << "undefined UMA action: " << action_string; |
71 } else { | |
72 iterator->second(); | |
73 } | 54 } |
74 } | 55 } |
75 | 56 |
76 void ExternalMetrics::RecordAction(const char* action) { | 57 void ExternalMetrics::RecordAction(const char* action) { |
77 std::string action_string(action); | 58 std::string action_string(action); |
78 BrowserThread::PostTask( | 59 BrowserThread::PostTask( |
79 BrowserThread::UI, FROM_HERE, | 60 BrowserThread::UI, FROM_HERE, |
80 NewRunnableMethod(this, &ExternalMetrics::RecordActionUI, action)); | 61 NewRunnableMethod(this, &ExternalMetrics::RecordActionUI, action_string)); |
81 } | 62 } |
82 | 63 |
83 void ExternalMetrics::RecordHistogram(const char* histogram_data) { | 64 void ExternalMetrics::RecordHistogram(const char* histogram_data) { |
84 int sample, min, max, nbuckets; | 65 int sample, min, max, nbuckets; |
85 char name[128]; // length must be consistent with sscanf format below. | 66 char name[128]; // length must be consistent with sscanf format below. |
86 int n = sscanf(histogram_data, "%127s %d %d %d %d", | 67 int n = sscanf(histogram_data, "%127s %d %d %d %d", |
87 name, &sample, &min, &max, &nbuckets); | 68 name, &sample, &min, &max, &nbuckets); |
88 if (n != 5) { | 69 if (n != 5) { |
89 LOG(ERROR) << "bad histogram request: " << histogram_data; | 70 LOG(ERROR) << "bad histogram request: " << histogram_data; |
90 return; | 71 return; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 void ExternalMetrics::ScheduleCollector() { | 211 void ExternalMetrics::ScheduleCollector() { |
231 bool result; | 212 bool result; |
232 result = BrowserThread::PostDelayedTask( | 213 result = BrowserThread::PostDelayedTask( |
233 BrowserThread::FILE, FROM_HERE, NewRunnableMethod( | 214 BrowserThread::FILE, FROM_HERE, NewRunnableMethod( |
234 this, &chromeos::ExternalMetrics::CollectEventsAndReschedule), | 215 this, &chromeos::ExternalMetrics::CollectEventsAndReschedule), |
235 kExternalMetricsCollectionIntervalMs); | 216 kExternalMetricsCollectionIntervalMs); |
236 DCHECK(result); | 217 DCHECK(result); |
237 } | 218 } |
238 | 219 |
239 } // namespace chromeos | 220 } // namespace chromeos |
OLD | NEW |