Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/metrics/user_metrics.h" | 5 #include "base/metrics/user_metrics.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/location.h" | |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 14 | 16 |
| 15 namespace base { | 17 namespace base { |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // A helper class for tracking callbacks and ensuring thread-safety. | 20 base::LazyInstance<std::vector<ActionCallback>> g_callbacks = |
| 19 class Callbacks { | 21 LAZY_INSTANCE_INITIALIZER; |
| 20 public: | 22 base::LazyInstance<scoped_refptr<base::SingleThreadTaskRunner>> g_task_runner = |
| 21 Callbacks() {} | 23 LAZY_INSTANCE_INITIALIZER; |
| 22 | |
| 23 // Records the |action|. | |
| 24 void Record(const std::string& action) { | |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 26 for (size_t i = 0; i < callbacks_.size(); ++i) { | |
| 27 callbacks_[i].Run(action); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 // Adds |callback| to the list of |callbacks_|. | |
| 32 void AddCallback(const ActionCallback& callback) { | |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 34 callbacks_.push_back(callback); | |
| 35 } | |
| 36 | |
| 37 // Removes the first instance of |callback| from the list of |callbacks_|, if | |
| 38 // there is one. | |
| 39 void RemoveCallback(const ActionCallback& callback) { | |
| 40 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 41 for (size_t i = 0; i < callbacks_.size(); ++i) { | |
| 42 if (callbacks_[i].Equals(callback)) { | |
| 43 callbacks_.erase(callbacks_.begin() + i); | |
| 44 return; | |
| 45 } | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 base::ThreadChecker thread_checker_; | |
| 51 std::vector<ActionCallback> callbacks_; | |
| 52 | |
| 53 DISALLOW_COPY_AND_ASSIGN(Callbacks); | |
| 54 }; | |
| 55 | |
| 56 base::LazyInstance<Callbacks> g_callbacks = LAZY_INSTANCE_INITIALIZER; | |
| 57 | 24 |
| 58 } // namespace | 25 } // namespace |
| 59 | 26 |
| 60 void RecordAction(const UserMetricsAction& action) { | 27 void RecordAction(const UserMetricsAction& action) { |
| 61 g_callbacks.Get().Record(action.str_); | 28 RecordComputedAction(action.str_); |
| 62 } | 29 } |
| 63 | 30 |
| 64 void RecordComputedAction(const std::string& action) { | 31 void RecordComputedAction(const std::string& action) { |
| 65 g_callbacks.Get().Record(action); | 32 // Many tests do not register a task runner. This call shouldn't fail. |
| 33 if(!g_task_runner.Get()) | |
| 34 return; | |
|
Ilya Sherman
2016/04/07 18:21:53
Please add a DCHECK within this if-stmt to verify
Ilya Sherman
2016/04/07 18:21:53
nit: Please leave a blank line after this if-stmt.
beaudoin
2016/04/07 21:12:58
Done.
beaudoin
2016/04/07 21:12:58
Done.
| |
| 35 if (!g_task_runner.Get()->BelongsToCurrentThread()) { | |
| 36 g_task_runner.Get()->PostTask(FROM_HERE, | |
| 37 base::Bind(&RecordComputedAction, action)); | |
| 38 return; | |
| 39 } | |
|
Ilya Sherman
2016/04/07 18:21:53
nit: Please leave a blank line after this if-stmt.
beaudoin
2016/04/07 21:12:58
Done.
| |
| 40 const std::vector<ActionCallback>& callbacks = g_callbacks.Get(); | |
| 41 for (size_t i = 0; i < callbacks.size(); ++i) { | |
| 42 callbacks[i].Run(action); | |
| 43 } | |
| 66 } | 44 } |
| 67 | 45 |
| 68 void AddActionCallback(const ActionCallback& callback) { | 46 void AddActionCallback(const ActionCallback& callback) { |
| 69 g_callbacks.Get().AddCallback(callback); | 47 DCHECK(g_task_runner.Get() && g_task_runner.Get()->BelongsToCurrentThread()); |
|
Ilya Sherman
2016/04/07 18:21:53
nit: Please write this as two DCHECKs, so that if
beaudoin
2016/04/07 21:12:58
Done.
| |
| 48 g_callbacks.Get().push_back(callback); | |
| 70 } | 49 } |
| 71 | 50 |
| 72 void RemoveActionCallback(const ActionCallback& callback) { | 51 void RemoveActionCallback(const ActionCallback& callback) { |
| 73 g_callbacks.Get().RemoveCallback(callback); | 52 DCHECK(g_task_runner.Get() && g_task_runner.Get()->BelongsToCurrentThread()); |
|
Ilya Sherman
2016/04/07 18:21:53
nit: Ditto here
beaudoin
2016/04/07 21:12:58
Done.
| |
| 53 std::vector<ActionCallback>& callbacks = g_callbacks.Get(); | |
| 54 for (size_t i = 0; i < callbacks.size(); ++i) { | |
| 55 if (callbacks[i].Equals(callback)) { | |
| 56 callbacks.erase(callbacks.begin() + i); | |
| 57 return; | |
| 58 } | |
| 59 } | |
| 60 } | |
| 74 | 61 |
| 62 void SetRecordActionTaskRunner( | |
| 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 64 DCHECK(!g_task_runner.Get()); | |
| 65 g_task_runner.Get() = task_runner; | |
| 75 } | 66 } |
| 76 | 67 |
| 77 } // namespace base | 68 } // namespace base |
| OLD | NEW |