message_loop_proxy.h revision 731df977c0511bca2206b5f333555b1205ff1f43
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
3// found in the LICENSE file.
4
5#ifndef BASE_MESSAGE_LOOP_PROXY_H_
6#define BASE_MESSAGE_LOOP_PROXY_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/ref_counted.h"
11#include "base/task.h"
12
13namespace base {
14
15struct MessageLoopProxyTraits;
16
17// This class provides a thread-safe refcounted interface to the Post* methods
18// of a message loop. This class can outlive the target message loop. You can
19// obtain a MessageLoopProxy via Thread::message_loop_proxy().
20class MessageLoopProxy
21    : public base::RefCountedThreadSafe<MessageLoopProxy,
22                                        MessageLoopProxyTraits> {
23 public:
24  // These methods are the same as in message_loop.h, but are guaranteed to
25  // either post the Task to the MessageLoop (if it's still alive), or to
26  // delete the Task otherwise.
27  // They return true iff the thread existed and the task was posted.  Note that
28  // even if the task is posted, there's no guarantee that it will run, since
29  // the target thread may already have a Quit message in its queue.
30  virtual bool PostTask(const tracked_objects::Location& from_here,
31                        Task* task) = 0;
32  virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
33                               Task* task, int64 delay_ms) = 0;
34  virtual bool PostNonNestableTask(const tracked_objects::Location& from_here,
35                                   Task* task) = 0;
36  virtual bool PostNonNestableDelayedTask(
37      const tracked_objects::Location& from_here,
38      Task* task,
39      int64 delay_ms) = 0;
40  // A method which checks if the caller is currently running in the thread that
41  // this proxy represents.
42  virtual bool BelongsToCurrentThread() = 0;
43
44  template <class T>
45  bool DeleteSoon(const tracked_objects::Location& from_here,
46                  T* object) {
47    return PostNonNestableTask(from_here, new DeleteTask<T>(object));
48  }
49  template <class T>
50  bool ReleaseSoon(const tracked_objects::Location& from_here,
51                   T* object) {
52    return PostNonNestableTask(from_here, new ReleaseTask<T>(object));
53  }
54
55  // Factory method for creating an implementation of MessageLoopProxy
56  // for the current thread.
57  static scoped_refptr<MessageLoopProxy> CreateForCurrentThread();
58
59 protected:
60  friend struct MessageLoopProxyTraits;
61
62  MessageLoopProxy();
63  virtual ~MessageLoopProxy();
64
65  // Called when the proxy is about to be deleted. Subclasses can override this
66  // to provide deletion on specific threads.
67  virtual void OnDestruct();
68};
69
70struct MessageLoopProxyTraits {
71  static void Destruct(MessageLoopProxy* proxy) {
72    proxy->OnDestruct();
73  }
74};
75
76}  // namespace base
77
78#endif  // BASE_MESSAGE_LOOP_PROXY_H_
79