[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 5 | #include "content/public/browser/user_metrics.h" |
[email protected] | ca59d8a | 2010-12-23 01:22:04 | [diff] [blame] | 6 | |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 7 | #include <vector> |
8 | |||||
[email protected] | 8075105 | 2011-11-12 17:10:58 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 11 | #include "content/public/browser/browser_thread.h" |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 12 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 13 | namespace content { |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 14 | namespace { |
15 | |||||
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 16 | base::LazyInstance<std::vector<ActionCallback> > g_action_callbacks = |
17 | LAZY_INSTANCE_INITIALIZER; | ||||
18 | |||||
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 19 | // Forward declare because of circular dependency. |
20 | void CallRecordOnUI(const std::string& action); | ||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 21 | |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 22 | void Record(const char *action) { |
[email protected] | ca59d8a | 2010-12-23 01:22:04 | [diff] [blame] | 23 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 24 | BrowserThread::PostTask( |
25 | BrowserThread::UI, | ||||
26 | FROM_HERE, | ||||
27 | base::Bind(&CallRecordOnUI, action)); | ||||
[email protected] | ca59d8a | 2010-12-23 01:22:04 | [diff] [blame] | 28 | return; |
29 | } | ||||
30 | |||||
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 31 | for (size_t i = 0; i < g_action_callbacks.Get().size(); i++) |
32 | g_action_callbacks.Get()[i].Run(action); | ||||
[email protected] | 89622004 | 2010-03-23 18:14:28 | [diff] [blame] | 33 | } |
34 | |||||
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 35 | void CallRecordOnUI(const std::string& action) { |
[email protected] | ca59d8a | 2010-12-23 01:22:04 | [diff] [blame] | 36 | Record(action.c_str()); |
37 | } | ||||
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 38 | |
39 | } // namespace | ||||
40 | |||||
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 41 | void RecordAction(const UserMetricsAction& action) { |
42 | Record(action.str_); | ||||
43 | } | ||||
44 | |||||
45 | void RecordComputedAction(const std::string& action) { | ||||
46 | Record(action.c_str()); | ||||
47 | } | ||||
48 | |||||
[email protected] | dd98f39 | 2013-02-04 13:03:22 | [diff] [blame] | 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 | |||||
[email protected] | 7f6f44c | 2011-12-14 13:23:38 | [diff] [blame] | 62 | } // namespace content |