OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "extensions/common/async_event.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
Devlin
2013/05/15 00:21:16
logging is included in both the .h and .cc file.
Jeffrey Yasskin
2013/05/15 03:07:18
That's not a big problem, but sure, removed it fro
| |
10 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "base/task_runner.h" | |
12 | |
13 using base::TaskRunner; | |
14 | |
15 namespace extensions { | |
16 | |
17 struct AsyncEvent::TaskInfo { | |
18 TaskInfo() {} | |
19 TaskInfo(const tracked_objects::Location& from_here, | |
20 const scoped_refptr<TaskRunner>& runner, | |
21 const base::Closure& task) | |
22 : from_here(from_here), runner(runner), task(task) {} | |
23 tracked_objects::Location from_here; | |
24 scoped_refptr<TaskRunner> runner; | |
25 base::Closure task; | |
26 }; | |
27 | |
28 AsyncEvent::AsyncEvent() : happened_(false) {} | |
29 AsyncEvent::~AsyncEvent() {} | |
30 | |
31 void AsyncEvent::RunAfter(const tracked_objects::Location& from_here, | |
32 const base::Closure& task, | |
33 const scoped_refptr<TaskRunner>& in_runner) const { | |
34 DCHECK(thread_checker_.CalledOnValidThread()); | |
35 | |
36 scoped_refptr<TaskRunner> message_loop_proxy; | |
37 TaskRunner* runner = in_runner.get(); | |
38 if (runner == NULL) { | |
39 message_loop_proxy = base::MessageLoopProxy::current(); | |
40 runner = message_loop_proxy.get(); | |
41 } | |
42 | |
43 if (has_happened()) { | |
44 runner->PostTask(from_here, task); | |
45 } else { | |
46 tasks_.push_back(TaskInfo(from_here, runner, task)); | |
47 } | |
48 } | |
49 | |
50 void AsyncEvent::MarkHappened() { | |
51 DCHECK(thread_checker_.CalledOnValidThread()); | |
52 | |
53 CHECK(!happened_) << "Only call MarkHappened once."; | |
54 | |
55 happened_ = true; | |
56 // After this point, a call to RunAfter() from one of the queued | |
57 // tasks could proceed immediately, but the fact that this object is | |
58 // single-threaded prevents that from being relevant. | |
59 | |
60 // We could randomize tasks_ in debug mode in order to check that | |
61 // the order doesn't matter... | |
62 for (size_t i = 0; i < tasks_.size(); ++i) { | |
63 tasks_[i].runner->PostTask(tasks_[i].from_here, tasks_[i].task); | |
64 } | |
65 } | |
66 | |
67 } | |
Devlin
2013/05/15 00:21:16
// namespace extensions
Jeffrey Yasskin
2013/05/15 03:07:18
Done.
| |
OLD | NEW |