OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" | 7 #include "base/location.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/threading/thread_restrictions.h" |
8 | 10 |
9 namespace base { | 11 namespace base { |
10 | 12 |
11 MessageLoopProxy::MessageLoopProxy() { | 13 scoped_refptr<MessageLoopProxy> |
| 14 MessageLoopProxy::current() { |
| 15 MessageLoop* cur_loop = MessageLoop::current(); |
| 16 if (!cur_loop) |
| 17 return NULL; |
| 18 return cur_loop->message_loop_proxy(); |
| 19 } |
| 20 |
| 21 // This function will be removed later in the fixing of CR Bug #108171. |
| 22 bool MessageLoopProxy::PostDelayedTask( |
| 23 const tracked_objects::Location& from_here, |
| 24 const base::Closure& task, |
| 25 int64 delay_ms) { |
| 26 return PostDelayedTask( |
| 27 from_here, task, base::TimeDelta::FromMilliseconds(delay_ms)); |
| 28 } |
| 29 |
| 30 bool MessageLoopProxy::PostDelayedTask( |
| 31 const tracked_objects::Location& from_here, |
| 32 const base::Closure& task, |
| 33 base::TimeDelta delay) { |
| 34 return PostTaskHelper(from_here, task, delay, true); |
| 35 } |
| 36 |
| 37 bool MessageLoopProxy::RunsTasksOnCurrentThread() const { |
| 38 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| 39 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
| 40 // function. |
| 41 // http://crbug.com/63678 |
| 42 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
| 43 AutoLock lock(message_loop_lock_); |
| 44 return (target_message_loop_ && |
| 45 (MessageLoop::current() == target_message_loop_)); |
| 46 } |
| 47 |
| 48 // This function will be removed later in the fixing of CR Bug #108171. |
| 49 bool MessageLoopProxy::PostNonNestableDelayedTask( |
| 50 const tracked_objects::Location& from_here, |
| 51 const base::Closure& task, |
| 52 int64 delay_ms) { |
| 53 return PostNonNestableDelayedTask( |
| 54 from_here, task, base::TimeDelta::FromMilliseconds(delay_ms)); |
| 55 } |
| 56 |
| 57 bool MessageLoopProxy::PostNonNestableDelayedTask( |
| 58 const tracked_objects::Location& from_here, |
| 59 const base::Closure& task, |
| 60 base::TimeDelta delay) { |
| 61 return PostTaskHelper(from_here, task, delay, false); |
| 62 } |
| 63 |
| 64 void MessageLoopProxy::OnDestruct() const { |
| 65 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| 66 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
| 67 // function. |
| 68 // http://crbug.com/63678 |
| 69 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
| 70 bool delete_later = false; |
| 71 { |
| 72 AutoLock lock(message_loop_lock_); |
| 73 if (target_message_loop_ && |
| 74 (MessageLoop::current() != target_message_loop_)) { |
| 75 target_message_loop_->DeleteSoon(FROM_HERE, this); |
| 76 delete_later = true; |
| 77 } |
| 78 } |
| 79 if (!delete_later) |
| 80 delete this; |
| 81 } |
| 82 |
| 83 MessageLoopProxy::MessageLoopProxy() |
| 84 : target_message_loop_(MessageLoop::current()) { |
12 } | 85 } |
13 | 86 |
14 MessageLoopProxy::~MessageLoopProxy() { | 87 MessageLoopProxy::~MessageLoopProxy() { |
15 } | 88 } |
16 | 89 |
| 90 void MessageLoopProxy::WillDestroyCurrentMessageLoop() { |
| 91 AutoLock lock(message_loop_lock_); |
| 92 target_message_loop_ = NULL; |
| 93 } |
| 94 |
| 95 bool MessageLoopProxy::PostTaskHelper( |
| 96 const tracked_objects::Location& from_here, const base::Closure& task, |
| 97 base::TimeDelta delay, bool nestable) { |
| 98 AutoLock lock(message_loop_lock_); |
| 99 if (target_message_loop_) { |
| 100 if (nestable) { |
| 101 target_message_loop_->PostDelayedTask(from_here, task, delay); |
| 102 } else { |
| 103 target_message_loop_->PostNonNestableDelayedTask(from_here, task, delay); |
| 104 } |
| 105 return true; |
| 106 } |
| 107 return false; |
| 108 } |
| 109 |
17 } // namespace base | 110 } // namespace base |
OLD | NEW |