blob: 7dc8caa9f4c047f93cf7366c2e373ad1eebdd0fb [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// 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.
4
5#include "base/message_loop/message_loop_proxy_impl.h"
6
7#include "base/location.h"
8#include "base/threading/thread_restrictions.h"
9
10namespace base {
11
12MessageLoopProxyImpl::~MessageLoopProxyImpl() {
13}
14
15bool 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);
20}
21
22bool 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);
27}
28
29bool 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
32 // function.
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_));
38}
39
40// MessageLoop::DestructionObserver implementation
41void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() {
42 AutoLock lock(message_loop_lock_);
43 target_message_loop_ = NULL;
44}
45
46void 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
49 // function.
50 // http://crbug.com/63678
51 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
52 bool delete_later = false;
53 {
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);
58 delete_later = true;
59 }
60 }
61 if (!delete_later)
62 delete this;
63}
64
65MessageLoopProxyImpl::MessageLoopProxyImpl()
66 : target_message_loop_(MessageLoop::current()) {
67}
68
69bool 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_) {
74 if (nestable) {
75 target_message_loop_->PostDelayedTask(from_here, task, delay);
76 } else {
77 target_message_loop_->PostNonNestableDelayedTask(from_here, task, delay);
78 }
79 return true;
80 }
81 return false;
82}
83
84scoped_refptr<MessageLoopProxy>
85MessageLoopProxy::current() {
86 MessageLoop* cur_loop = MessageLoop::current();
87 if (!cur_loop)
88 return NULL;
89 return cur_loop->message_loop_proxy();
90}
91
92} // namespace base