OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/public/browser/user_metrics.h" | 5 #include "content/public/browser/user_metrics.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" |
8 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
9 #include "content/public/browser/notification_service.h" | |
10 #include "content/public/browser/notification_types.h" | |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 namespace { | 14 namespace { |
14 | 15 |
| 16 base::LazyInstance<std::vector<ActionCallback> > g_action_callbacks = |
| 17 LAZY_INSTANCE_INITIALIZER; |
| 18 |
15 // Forward declare because of circular dependency. | 19 // Forward declare because of circular dependency. |
16 void CallRecordOnUI(const std::string& action); | 20 void CallRecordOnUI(const std::string& action); |
17 | 21 |
18 void Record(const char *action) { | 22 void Record(const char *action) { |
19 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 23 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
20 BrowserThread::PostTask( | 24 BrowserThread::PostTask( |
21 BrowserThread::UI, | 25 BrowserThread::UI, |
22 FROM_HERE, | 26 FROM_HERE, |
23 base::Bind(&CallRecordOnUI, action)); | 27 base::Bind(&CallRecordOnUI, action)); |
24 return; | 28 return; |
25 } | 29 } |
26 | 30 |
27 NotificationService::current()->Notify( | 31 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) |
28 NOTIFICATION_USER_ACTION, | 32 g_action_callbacks.Get()[i].Run(action); |
29 NotificationService::AllSources(), | |
30 Details<const char*>(&action)); | |
31 } | 33 } |
32 | 34 |
33 void CallRecordOnUI(const std::string& action) { | 35 void CallRecordOnUI(const std::string& action) { |
34 Record(action.c_str()); | 36 Record(action.c_str()); |
35 } | 37 } |
36 | 38 |
37 } // namespace | 39 } // namespace |
38 | 40 |
39 void RecordAction(const UserMetricsAction& action) { | 41 void RecordAction(const UserMetricsAction& action) { |
40 Record(action.str_); | 42 Record(action.str_); |
41 } | 43 } |
42 | 44 |
43 void RecordComputedAction(const std::string& action) { | 45 void RecordComputedAction(const std::string& action) { |
44 Record(action.c_str()); | 46 Record(action.c_str()); |
45 } | 47 } |
46 | 48 |
| 49 void AddActionCallback(const ActionCallback& callback) { |
| 50 g_action_callbacks.Get().push_back(callback); |
| 51 } |
| 52 |
| 53 void RemoveActionCallback(const ActionCallback& callback) { |
| 54 for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) { |
| 55 if (g_action_callbacks.Get()[i].Equals(callback)) { |
| 56 g_action_callbacks.Get().erase(g_action_callbacks.Get().begin() + i); |
| 57 return; |
| 58 } |
| 59 } |
| 60 } |
| 61 |
47 } // namespace content | 62 } // namespace content |
OLD | NEW |