OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/common/one_shot_event.h" | 5 #include "extensions/common/one_shot_event.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 | 14 |
14 using base::TaskRunner; | 15 using base::SingleThreadTaskRunner; |
15 | 16 |
16 namespace extensions { | 17 namespace extensions { |
17 | 18 |
18 struct OneShotEvent::TaskInfo { | 19 struct OneShotEvent::TaskInfo { |
19 TaskInfo() {} | 20 TaskInfo() {} |
20 TaskInfo(const tracked_objects::Location& from_here, | 21 TaskInfo(const tracked_objects::Location& from_here, |
21 const scoped_refptr<TaskRunner>& runner, | 22 const scoped_refptr<SingleThreadTaskRunner>& runner, |
22 const base::Closure& task, | 23 const base::Closure& task, |
23 const base::TimeDelta& delay) | 24 const base::TimeDelta& delay) |
24 : from_here(from_here), runner(runner), task(task), delay(delay) { | 25 : from_here(from_here), runner(runner), task(task), delay(delay) { |
25 CHECK(runner.get()); // Detect mistakes with a decent stack frame. | 26 CHECK(runner.get()); // Detect mistakes with a decent stack frame. |
26 } | 27 } |
27 tracked_objects::Location from_here; | 28 tracked_objects::Location from_here; |
28 scoped_refptr<TaskRunner> runner; | 29 scoped_refptr<SingleThreadTaskRunner> runner; |
29 base::Closure task; | 30 base::Closure task; |
30 base::TimeDelta delay; | 31 base::TimeDelta delay; |
31 }; | 32 }; |
32 | 33 |
33 OneShotEvent::OneShotEvent() : signaled_(false) { | 34 OneShotEvent::OneShotEvent() : signaled_(false) { |
34 // It's acceptable to construct the OneShotEvent on one thread, but | 35 // It's acceptable to construct the OneShotEvent on one thread, but |
35 // immediately move it to another thread. | 36 // immediately move it to another thread. |
36 thread_checker_.DetachFromThread(); | 37 thread_checker_.DetachFromThread(); |
37 } | 38 } |
38 OneShotEvent::OneShotEvent(bool signaled) : signaled_(signaled) { | 39 OneShotEvent::OneShotEvent(bool signaled) : signaled_(signaled) { |
39 thread_checker_.DetachFromThread(); | 40 thread_checker_.DetachFromThread(); |
40 } | 41 } |
41 OneShotEvent::~OneShotEvent() {} | 42 OneShotEvent::~OneShotEvent() {} |
42 | 43 |
43 void OneShotEvent::Post(const tracked_objects::Location& from_here, | 44 void OneShotEvent::Post(const tracked_objects::Location& from_here, |
44 const base::Closure& task) const { | 45 const base::Closure& task) const { |
45 PostImpl( | 46 PostImpl(from_here, task, base::ThreadTaskRunnerHandle::Get(), |
46 from_here, task, base::MessageLoopProxy::current(), base::TimeDelta()); | 47 base::TimeDelta()); |
47 } | 48 } |
48 | 49 |
49 void OneShotEvent::Post(const tracked_objects::Location& from_here, | 50 void OneShotEvent::Post( |
50 const base::Closure& task, | 51 const tracked_objects::Location& from_here, |
51 const scoped_refptr<TaskRunner>& runner) const { | 52 const base::Closure& task, |
| 53 const scoped_refptr<SingleThreadTaskRunner>& runner) const { |
52 PostImpl(from_here, task, runner, base::TimeDelta()); | 54 PostImpl(from_here, task, runner, base::TimeDelta()); |
53 } | 55 } |
54 | 56 |
55 void OneShotEvent::PostDelayed(const tracked_objects::Location& from_here, | 57 void OneShotEvent::PostDelayed(const tracked_objects::Location& from_here, |
56 const base::Closure& task, | 58 const base::Closure& task, |
57 const base::TimeDelta& delay) const { | 59 const base::TimeDelta& delay) const { |
58 PostImpl(from_here, task, base::MessageLoopProxy::current(), delay); | 60 PostImpl(from_here, task, base::ThreadTaskRunnerHandle::Get(), delay); |
59 } | 61 } |
60 | 62 |
61 void OneShotEvent::Signal() { | 63 void OneShotEvent::Signal() { |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
63 | 65 |
64 CHECK(!signaled_) << "Only call Signal once."; | 66 CHECK(!signaled_) << "Only call Signal once."; |
65 | 67 |
66 signaled_ = true; | 68 signaled_ = true; |
67 // After this point, a call to Post() from one of the queued tasks | 69 // After this point, a call to Post() from one of the queued tasks |
68 // could proceed immediately, but the fact that this object is | 70 // could proceed immediately, but the fact that this object is |
69 // single-threaded prevents that from being relevant. | 71 // single-threaded prevents that from being relevant. |
70 | 72 |
71 // We could randomize tasks_ in debug mode in order to check that | 73 // We could randomize tasks_ in debug mode in order to check that |
72 // the order doesn't matter... | 74 // the order doesn't matter... |
73 for (size_t i = 0; i < tasks_.size(); ++i) { | 75 for (size_t i = 0; i < tasks_.size(); ++i) { |
74 const TaskInfo& task = tasks_[i]; | 76 const TaskInfo& task = tasks_[i]; |
75 if (task.delay != base::TimeDelta()) | 77 if (task.delay != base::TimeDelta()) |
76 task.runner->PostDelayedTask(task.from_here, task.task, task.delay); | 78 task.runner->PostDelayedTask(task.from_here, task.task, task.delay); |
77 else | 79 else |
78 task.runner->PostTask(task.from_here, task.task); | 80 task.runner->PostTask(task.from_here, task.task); |
79 } | 81 } |
80 } | 82 } |
81 | 83 |
82 void OneShotEvent::PostImpl(const tracked_objects::Location& from_here, | 84 void OneShotEvent::PostImpl(const tracked_objects::Location& from_here, |
83 const base::Closure& task, | 85 const base::Closure& task, |
84 const scoped_refptr<TaskRunner>& runner, | 86 const scoped_refptr<SingleThreadTaskRunner>& runner, |
85 const base::TimeDelta& delay) const { | 87 const base::TimeDelta& delay) const { |
86 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
87 | 89 |
88 if (is_signaled()) { | 90 if (is_signaled()) { |
89 if (delay != base::TimeDelta()) | 91 if (delay != base::TimeDelta()) |
90 runner->PostDelayedTask(from_here, task, delay); | 92 runner->PostDelayedTask(from_here, task, delay); |
91 else | 93 else |
92 runner->PostTask(from_here, task); | 94 runner->PostTask(from_here, task); |
93 } else { | 95 } else { |
94 tasks_.push_back(TaskInfo(from_here, runner, task, delay)); | 96 tasks_.push_back(TaskInfo(from_here, runner, task, delay)); |
95 } | 97 } |
96 } | 98 } |
97 | 99 |
98 } // namespace extensions | 100 } // namespace extensions |
OLD | NEW |