1 // Copyright (c) 2012 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 PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
6 #define PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_
8 #include "base/basictypes.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "ppapi/proxy/interface_proxy.h"
14 #include "ppapi/proxy/ppapi_proxy_export.h"
15 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
16 #include "ppapi/thunk/ppb_message_loop_api.h"
18 struct PPB_MessageLoop_1_0
;
23 class PPAPI_PROXY_EXPORT MessageLoopResource
: public MessageLoopShared
{
25 explicit MessageLoopResource(PP_Instance instance
);
26 // Construct the one MessageLoopResource for the main thread. This must be
27 // invoked on the main thread.
28 explicit MessageLoopResource(ForMainThread
);
29 virtual ~MessageLoopResource();
31 // Resource overrides.
32 virtual thunk::PPB_MessageLoop_API
* AsPPB_MessageLoop_API() OVERRIDE
;
34 // PPB_MessageLoop_API implementation.
35 virtual int32_t AttachToCurrentThread() OVERRIDE
;
36 virtual int32_t Run() OVERRIDE
;
37 virtual int32_t PostWork(PP_CompletionCallback callback
,
38 int64_t delay_ms
) OVERRIDE
;
39 virtual int32_t PostQuit(PP_Bool should_destroy
) OVERRIDE
;
41 static MessageLoopResource
* GetCurrent();
42 void DetachFromThread();
43 bool is_main_thread_loop() const {
44 return is_main_thread_loop_
;
47 const scoped_refptr
<base::MessageLoopProxy
>& message_loop_proxy() {
53 tracked_objects::Location from_here
;
54 base::Closure closure
;
58 // Returns true if the object is associated with the current thread.
59 bool IsCurrent() const;
61 // Handles posting to the message loop if there is one, or the pending queue
63 // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock.
64 // This only makes sense for user code and completely thread-safe
65 // proxy operations (e.g., MessageLoop::QuitClosure).
66 virtual void PostClosure(const tracked_objects::Location
& from_here
,
67 const base::Closure
& closure
,
68 int64 delay_ms
) OVERRIDE
;
70 virtual base::MessageLoopProxy
* GetMessageLoopProxy() OVERRIDE
;
72 // TLS destructor function.
73 static void ReleaseMessageLoop(void* value
);
75 // Created when we attach to the current thread, since MessageLoop assumes
76 // that it's created on the thread it will run on. NULL for the main thread
77 // loop, since that's owned by somebody else. This is needed for Run and Quit.
78 // Any time we post tasks, we should post them using loop_proxy_.
79 scoped_ptr
<base::MessageLoop
> loop_
;
80 scoped_refptr
<base::MessageLoopProxy
> loop_proxy_
;
82 // Number of invocations of Run currently on the stack.
83 int nested_invocations_
;
85 // Set to true when the message loop is destroyed to prevent forther
89 // Set to true if all message loop invocations should exit and that the
90 // loop should be destroyed once it reaches the outermost Run invocation.
93 bool is_main_thread_loop_
;
95 // Since we allow tasks to be posted before the message loop is actually
96 // created (when it's associated with a thread), we keep tasks posted here
97 // until that happens. Once the loop_ is created, this is unused.
98 std::vector
<TaskInfo
> pending_tasks_
;
100 DISALLOW_COPY_AND_ASSIGN(MessageLoopResource
);
103 class PPB_MessageLoop_Proxy
: public InterfaceProxy
{
105 explicit PPB_MessageLoop_Proxy(Dispatcher
* dispatcher
);
106 virtual ~PPB_MessageLoop_Proxy();
108 static const PPB_MessageLoop_1_0
* GetInterface();
111 DISALLOW_COPY_AND_ASSIGN(PPB_MessageLoop_Proxy
);
117 #endif // PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_