OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 |
7 namespace base { | 9 namespace base { |
8 | 10 |
9 MessageLoopProxy::MessageLoopProxy() { | 11 MessageLoopProxy::MessageLoopProxy() { |
10 } | 12 } |
11 | 13 |
12 MessageLoopProxy::~MessageLoopProxy() { | 14 MessageLoopProxy::~MessageLoopProxy() { |
13 } | 15 } |
14 | 16 |
| 17 bool MessageLoopProxy::PostTaskAndReply( |
| 18 const tracked_objects::Location& from_here, |
| 19 const Closure& task, |
| 20 const Closure& reply) { |
| 21 internal::PostTaskAndReplyRelay* relay = |
| 22 new internal::PostTaskAndReplyRelay(from_here, task, reply); |
| 23 if (!PostTask(from_here, Bind(&internal::PostTaskAndReplyRelay::Run, |
| 24 Unretained(relay)))) { |
| 25 delete relay; |
| 26 return false; |
| 27 } |
| 28 |
| 29 return true; |
| 30 } |
| 31 |
15 void MessageLoopProxy::OnDestruct() const { | 32 void MessageLoopProxy::OnDestruct() const { |
16 delete this; | 33 delete this; |
17 } | 34 } |
18 | 35 |
| 36 namespace internal { |
| 37 |
| 38 PostTaskAndReplyRelay::PostTaskAndReplyRelay( |
| 39 const tracked_objects::Location& from_here, |
| 40 const Closure& task, |
| 41 const Closure& reply) |
| 42 : from_here_(from_here), |
| 43 origin_loop_(MessageLoopProxy::current()) { |
| 44 task_ = task; |
| 45 reply_ = reply; |
| 46 } |
| 47 |
| 48 PostTaskAndReplyRelay::~PostTaskAndReplyRelay() { |
| 49 DCHECK(origin_loop_->BelongsToCurrentThread()); |
| 50 task_.Reset(); |
| 51 reply_.Reset(); |
| 52 } |
| 53 |
| 54 void PostTaskAndReplyRelay::Run() { |
| 55 task_.Run(); |
| 56 origin_loop_->PostTask( |
| 57 from_here_, |
| 58 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, |
| 59 base::Unretained(this))); |
| 60 } |
| 61 |
| 62 void PostTaskAndReplyRelay::RunReplyAndSelfDestruct() { |
| 63 DCHECK(origin_loop_->BelongsToCurrentThread()); |
| 64 |
| 65 // Force |task_| to be released before |reply_| is to ensure that no one |
| 66 // accidentally depends on |task_| keeping one of its arguments alive while |
| 67 // |reply_| is executing. |
| 68 task_.Reset(); |
| 69 |
| 70 reply_.Run(); |
| 71 |
| 72 // Cue mission impossible theme. |
| 73 delete this; |
| 74 } |
| 75 |
| 76 } // namespace internal |
| 77 |
19 } // namespace base | 78 } // namespace base |
OLD | NEW |