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