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