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"
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
> {
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
,
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
,
33 virtual bool PostNonNestableDelayedTask(
34 const tracked_objects::Location
& from_here
,
37 // A method which checks if the caller is currently running in the thread that
38 // this proxy represents.
39 virtual bool BelongsToCurrentThread() = 0;
42 bool DeleteSoon(const tracked_objects::Location
& from_here
,
44 return PostNonNestableTask(from_here
, new DeleteTask
<T
>(object
));
47 bool ReleaseSoon(const tracked_objects::Location
& from_here
,
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();
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() {
65 struct MessageLoopProxyTraits
{
66 static void Destruct(MessageLoopProxy
* proxy
) {
73 #endif // BASE_MESSAGE_LOOP_PROXY_H_