Complete SyncMessageFilter initialization after SyncChannel initialization
[chromium-blink-merge.git] / ppapi / proxy / ppb_message_loop_proxy.h
blob25e69b31ba93a459efaec43654c114616d08dad4
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"
9 #include "base/bind.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 "base/single_thread_task_runner.h"
14 #include "ppapi/proxy/interface_proxy.h"
15 #include "ppapi/proxy/ppapi_proxy_export.h"
16 #include "ppapi/shared_impl/ppb_message_loop_shared.h"
17 #include "ppapi/thunk/ppb_message_loop_api.h"
19 struct PPB_MessageLoop_1_0;
21 namespace ppapi {
22 namespace proxy {
24 class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared {
25 public:
26 explicit MessageLoopResource(PP_Instance instance);
27 // Construct the one MessageLoopResource for the main thread. This must be
28 // invoked on the main thread.
29 explicit MessageLoopResource(ForMainThread);
30 ~MessageLoopResource() override;
32 // Resource overrides.
33 thunk::PPB_MessageLoop_API* AsPPB_MessageLoop_API() override;
35 // PPB_MessageLoop_API implementation.
36 int32_t AttachToCurrentThread() override;
37 int32_t Run() override;
38 int32_t PostWork(PP_CompletionCallback callback, int64_t delay_ms) override;
39 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::SingleThreadTaskRunner>& task_runner() {
48 return task_runner_;
51 void set_currently_handling_blocking_message(bool handling_blocking_message) {
52 currently_handling_blocking_message_ = handling_blocking_message;
55 private:
56 struct TaskInfo {
57 tracked_objects::Location from_here;
58 base::Closure closure;
59 int64 delay_ms;
62 // Returns true if the object is associated with the current thread.
63 bool IsCurrent() const;
65 // MessageLoopShared implementation.
67 // Handles posting to the message loop if there is one, or the pending queue
68 // if there isn't.
69 // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock.
70 // This only makes sense for user code and completely thread-safe
71 // proxy operations (e.g., MessageLoop::QuitClosure).
72 void PostClosure(const tracked_objects::Location& from_here,
73 const base::Closure& closure,
74 int64 delay_ms) override;
75 base::SingleThreadTaskRunner* GetTaskRunner() override;
76 bool CurrentlyHandlingBlockingMessage() override;
78 // TLS destructor function.
79 static void ReleaseMessageLoop(void* value);
81 // Created when we attach to the current thread, since MessageLoop assumes
82 // that it's created on the thread it will run on. NULL for the main thread
83 // loop, since that's owned by somebody else. This is needed for Run and Quit.
84 // Any time we post tasks, we should post them using task_runner_.
85 scoped_ptr<base::MessageLoop> loop_;
86 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
88 // Number of invocations of Run currently on the stack.
89 int nested_invocations_;
91 // Set to true when the message loop is destroyed to prevent forther
92 // posting of work.
93 bool destroyed_;
95 // Set to true if all message loop invocations should exit and that the
96 // loop should be destroyed once it reaches the outermost Run invocation.
97 bool should_destroy_;
99 bool is_main_thread_loop_;
101 bool currently_handling_blocking_message_;
103 // Since we allow tasks to be posted before the message loop is actually
104 // created (when it's associated with a thread), we keep tasks posted here
105 // until that happens. Once the loop_ is created, this is unused.
106 std::vector<TaskInfo> pending_tasks_;
108 DISALLOW_COPY_AND_ASSIGN(MessageLoopResource);
111 class PPB_MessageLoop_Proxy : public InterfaceProxy {
112 public:
113 explicit PPB_MessageLoop_Proxy(Dispatcher* dispatcher);
114 virtual ~PPB_MessageLoop_Proxy();
116 static const PPB_MessageLoop_1_0* GetInterface();
118 private:
119 DISALLOW_COPY_AND_ASSIGN(PPB_MessageLoop_Proxy);
122 } // namespace proxy
123 } // namespace ppapi
125 #endif // PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_