Speculative fix for the white/stale tab issue on Windows.
[chromium-blink-merge.git] / base / message_loop_proxy_impl.cc
blobb4ca2101af5a514944d040b108e1bde30824bde5
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"
10 namespace base {
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
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_));
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
49 // function.
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);
58 delete_later = true;
61 if (!delete_later)
62 delete 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_) {
74 if (nestable) {
75 target_message_loop_->PostDelayedTask(from_here, task, delay);
76 } else {
77 target_message_loop_->PostNonNestableDelayedTask(from_here, task, delay);
79 return true;
81 return false;
84 scoped_refptr<MessageLoopProxy>
85 MessageLoopProxy::current() {
86 MessageLoop* cur_loop = MessageLoop::current();
87 if (!cur_loop)
88 return NULL;
89 return cur_loop->message_loop_proxy();
92 } // namespace base