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