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
3 // found in the LICENSE file.
5 #include "base/message_loop_proxy_impl.h"
7 #include "base/location.h"
8 #include "base/threading/thread_restrictions.h"
12 MessageLoopProxyImpl::~MessageLoopProxyImpl() {
15 bool MessageLoopProxyImpl::PostDelayedTask(
16 const tracked_objects::Location
& from_here
,
17 const base::Closure
& task
,
18 base::TimeDelta delay
) {
19 return PostTaskHelper(from_here
, task
, delay
, true);
22 bool MessageLoopProxyImpl::PostNonNestableDelayedTask(
23 const tracked_objects::Location
& from_here
,
24 const base::Closure
& task
,
25 base::TimeDelta delay
) {
26 return PostTaskHelper(from_here
, task
, delay
, false);
29 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const {
30 // We shouldn't use MessageLoop::current() since it uses LazyInstance which
31 // may be deleted by ~AtExitManager when a WorkerPool thread calls this
33 // http://crbug.com/63678
34 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton
;
35 AutoLock
lock(message_loop_lock_
);
36 return (target_message_loop_
&&
37 (MessageLoop::current() == target_message_loop_
));
40 // MessageLoop::DestructionObserver implementation
41 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() {
42 AutoLock
lock(message_loop_lock_
);
43 target_message_loop_
= NULL
;
46 void MessageLoopProxyImpl::OnDestruct() const {
47 // We shouldn't use MessageLoop::current() since it uses LazyInstance which
48 // may be deleted by ~AtExitManager when a WorkerPool thread calls this
50 // http://crbug.com/63678
51 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton
;
52 bool delete_later
= false;
54 AutoLock
lock(message_loop_lock_
);
55 if (target_message_loop_
&&
56 (MessageLoop::current() != target_message_loop_
)) {
57 target_message_loop_
->DeleteSoon(FROM_HERE
, this);
65 MessageLoopProxyImpl::MessageLoopProxyImpl()
66 : target_message_loop_(MessageLoop::current()) {
69 bool MessageLoopProxyImpl::PostTaskHelper(
70 const tracked_objects::Location
& from_here
, const base::Closure
& task
,
71 base::TimeDelta delay
, bool nestable
) {
72 AutoLock
lock(message_loop_lock_
);
73 if (target_message_loop_
) {
75 target_message_loop_
->PostDelayedTask(from_here
, task
, delay
);
77 target_message_loop_
->PostNonNestableDelayedTask(from_here
, task
, delay
);
84 scoped_refptr
<MessageLoopProxy
>
85 MessageLoopProxy::current() {
86 MessageLoop
* cur_loop
= MessageLoop::current();
89 return cur_loop
->message_loop_proxy();