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