Chromium Code Reviews| 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 (std::vector<ActionCallback>::iterator i = | |
|
jam
2013/01/31 18:02:29
nit: size_t here too
Paweł Hajdan Jr.
2013/02/01 10:43:57
Done.
| |
| 55 g_action_callbacks.Get().begin(); | |
| 56 i != g_action_callbacks.Get().end(); | |
| 57 ++i) { | |
| 58 if ((*i).Equals(callback)) { | |
| 59 i = g_action_callbacks.Get().erase(i); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 47 } // namespace content | 64 } // namespace content |
| OLD | NEW |