[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_ |
| 6 | #define PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_ |
| 7 | |
| 8 | #include "base/basictypes.h" |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
| 12 | #include "base/message_loop.h" |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 13 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame^] | 14 | #include "ppapi/proxy/ppapi_proxy_export.h" |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 15 | #include "ppapi/shared_impl/ppb_message_loop_shared.h" |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 16 | #include "ppapi/thunk/ppb_message_loop_api.h" |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 17 | |
[email protected] | d4ab9471 | 2012-11-15 21:01:23 | [diff] [blame] | 18 | struct PPB_MessageLoop_1_0; |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 19 | |
| 20 | namespace ppapi { |
| 21 | namespace proxy { |
| 22 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame^] | 23 | class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared { |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 24 | public: |
| 25 | explicit MessageLoopResource(PP_Instance instance); |
| 26 | // Construct the one MessageLoopResource for the main thread. This must be |
| 27 | // invoked on the main thread. |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 28 | explicit MessageLoopResource(ForMainThread); |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 29 | virtual ~MessageLoopResource(); |
| 30 | |
| 31 | // Resource overrides. |
| 32 | virtual thunk::PPB_MessageLoop_API* AsPPB_MessageLoop_API() OVERRIDE; |
| 33 | |
| 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; |
| 40 | |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 41 | static MessageLoopResource* GetCurrent(); |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 42 | void DetachFromThread(); |
| 43 | bool is_main_thread_loop() const { |
| 44 | return is_main_thread_loop_; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | struct TaskInfo { |
| 49 | tracked_objects::Location from_here; |
| 50 | base::Closure closure; |
| 51 | int64 delay_ms; |
| 52 | }; |
| 53 | |
| 54 | // Returns true if the object is associated with the current thread. |
| 55 | bool IsCurrent() const; |
| 56 | |
| 57 | // Handles posting to the message loop if there is one, or the pending queue |
| 58 | // if there isn't. |
| 59 | // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock. |
| 60 | // This only makes sense for user code and completely thread-safe |
| 61 | // proxy operations (e.g., MessageLoop::QuitClosure). |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 62 | virtual void PostClosure(const tracked_objects::Location& from_here, |
| 63 | const base::Closure& closure, |
| 64 | int64 delay_ms) OVERRIDE; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 65 | |
| 66 | // TLS destructor function. |
| 67 | static void ReleaseMessageLoop(void* value); |
| 68 | |
| 69 | // Created when we attach to the current thread, since MessageLoop assumes |
| 70 | // that it's created on the thread it will run on. NULL for the main thread |
| 71 | // loop, since that's owned by somebody else. This is needed for Run and Quit. |
| 72 | // Any time we post tasks, we should post them using loop_proxy_. |
| 73 | scoped_ptr<MessageLoop> loop_; |
| 74 | scoped_refptr<base::MessageLoopProxy> loop_proxy_; |
| 75 | |
| 76 | // Number of invocations of Run currently on the stack. |
| 77 | int nested_invocations_; |
| 78 | |
| 79 | // Set to true when the message loop is destroyed to prevent forther |
| 80 | // posting of work. |
| 81 | bool destroyed_; |
| 82 | |
| 83 | // Set to true if all message loop invocations should exit and that the |
| 84 | // loop should be destroyed once it reaches the outermost Run invocation. |
| 85 | bool should_destroy_; |
| 86 | |
| 87 | bool is_main_thread_loop_; |
| 88 | |
| 89 | // Since we allow tasks to be posted before the message loop is actually |
| 90 | // created (when it's associated with a thread), we keep tasks posted here |
| 91 | // until that happens. Once the loop_ is created, this is unused. |
| 92 | std::vector<TaskInfo> pending_tasks_; |
| 93 | |
| 94 | DISALLOW_COPY_AND_ASSIGN(MessageLoopResource); |
| 95 | }; |
| 96 | |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 97 | class PPB_MessageLoop_Proxy : public InterfaceProxy { |
| 98 | public: |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 99 | explicit PPB_MessageLoop_Proxy(Dispatcher* dispatcher); |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 100 | virtual ~PPB_MessageLoop_Proxy(); |
| 101 | |
[email protected] | d4ab9471 | 2012-11-15 21:01:23 | [diff] [blame] | 102 | static const PPB_MessageLoop_1_0* GetInterface(); |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | DISALLOW_COPY_AND_ASSIGN(PPB_MessageLoop_Proxy); |
| 106 | }; |
| 107 | |
| 108 | } // namespace proxy |
| 109 | } // namespace ppapi |
| 110 | |
| 111 | #endif // PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_ |