[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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 12 | #include "base/bind.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 15 | #include "base/message_loop/message_loop.h" |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 16 | #include "base/run_loop.h" |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 18 | #include "ppapi/proxy/interface_proxy.h" |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 19 | #include "ppapi/proxy/ppapi_proxy_export.h" |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 20 | #include "ppapi/shared_impl/ppb_message_loop_shared.h" |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 21 | #include "ppapi/thunk/ppb_message_loop_api.h" |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 22 | |
[email protected] | d4ab9471 | 2012-11-15 21:01:23 | [diff] [blame] | 23 | struct PPB_MessageLoop_1_0; |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 24 | |
| 25 | namespace ppapi { |
| 26 | namespace proxy { |
| 27 | |
[email protected] | 7ef6b79b | 2013-01-17 02:38:24 | [diff] [blame] | 28 | class PPAPI_PROXY_EXPORT MessageLoopResource : public MessageLoopShared { |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 29 | public: |
| 30 | explicit MessageLoopResource(PP_Instance instance); |
| 31 | // Construct the one MessageLoopResource for the main thread. This must be |
| 32 | // invoked on the main thread. |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 33 | explicit MessageLoopResource(ForMainThread); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 34 | ~MessageLoopResource() override; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 35 | |
| 36 | // Resource overrides. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 37 | thunk::PPB_MessageLoop_API* AsPPB_MessageLoop_API() override; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 38 | |
| 39 | // PPB_MessageLoop_API implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 40 | int32_t AttachToCurrentThread() override; |
| 41 | int32_t Run() override; |
| 42 | int32_t PostWork(PP_CompletionCallback callback, int64_t delay_ms) override; |
| 43 | int32_t PostQuit(PP_Bool should_destroy) override; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 44 | |
[email protected] | 77c3417 | 2012-11-08 18:55:16 | [diff] [blame] | 45 | static MessageLoopResource* GetCurrent(); |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 46 | void DetachFromThread(); |
| 47 | bool is_main_thread_loop() const { |
| 48 | return is_main_thread_loop_; |
| 49 | } |
| 50 | |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 51 | const scoped_refptr<base::SingleThreadTaskRunner>& task_runner() { |
| 52 | return task_runner_; |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 53 | } |
| 54 | |
dmichael | 44b96754 | 2014-09-24 15:41:14 | [diff] [blame] | 55 | void set_currently_handling_blocking_message(bool handling_blocking_message) { |
| 56 | currently_handling_blocking_message_ = handling_blocking_message; |
| 57 | } |
| 58 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 59 | private: |
| 60 | struct TaskInfo { |
| 61 | tracked_objects::Location from_here; |
| 62 | base::Closure closure; |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 63 | int64_t delay_ms; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Returns true if the object is associated with the current thread. |
| 67 | bool IsCurrent() const; |
| 68 | |
dmichael | 44b96754 | 2014-09-24 15:41:14 | [diff] [blame] | 69 | // MessageLoopShared implementation. |
| 70 | // |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 71 | // Handles posting to the message loop if there is one, or the pending queue |
| 72 | // if there isn't. |
| 73 | // NOTE: The given closure will be run *WITHOUT* acquiring the Proxy lock. |
| 74 | // This only makes sense for user code and completely thread-safe |
| 75 | // proxy operations (e.g., MessageLoop::QuitClosure). |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 76 | void PostClosure(const tracked_objects::Location& from_here, |
| 77 | const base::Closure& closure, |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 78 | int64_t delay_ms) override; |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 79 | base::SingleThreadTaskRunner* GetTaskRunner() override; |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 80 | bool CurrentlyHandlingBlockingMessage() override; |
[email protected] | 511c58e | 2013-12-12 12:25:33 | [diff] [blame] | 81 | |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 82 | // Quits |run_loop_|. Must be called from the thread that runs the RunLoop. |
| 83 | void QuitRunLoopWhenIdle(); |
| 84 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 85 | // TLS destructor function. |
| 86 | static void ReleaseMessageLoop(void* value); |
| 87 | |
| 88 | // Created when we attach to the current thread, since MessageLoop assumes |
| 89 | // that it's created on the thread it will run on. NULL for the main thread |
| 90 | // loop, since that's owned by somebody else. This is needed for Run and Quit. |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 91 | // Any time we post tasks, we should post them using task_runner_. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 92 | std::unique_ptr<base::MessageLoop> loop_; |
skyostil | 23490d4 | 2015-06-12 12:54:26 | [diff] [blame] | 93 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 94 | |
fdoray | 2df4a9e | 2016-07-18 23:47:16 | [diff] [blame] | 95 | // RunLoop currently on the stack. |
| 96 | base::RunLoop* run_loop_ = nullptr; |
| 97 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 98 | // Number of invocations of Run currently on the stack. |
| 99 | int nested_invocations_; |
| 100 | |
| 101 | // Set to true when the message loop is destroyed to prevent forther |
| 102 | // posting of work. |
| 103 | bool destroyed_; |
| 104 | |
| 105 | // Set to true if all message loop invocations should exit and that the |
| 106 | // loop should be destroyed once it reaches the outermost Run invocation. |
| 107 | bool should_destroy_; |
| 108 | |
| 109 | bool is_main_thread_loop_; |
| 110 | |
dmichael | 44b96754 | 2014-09-24 15:41:14 | [diff] [blame] | 111 | bool currently_handling_blocking_message_; |
| 112 | |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 113 | // Since we allow tasks to be posted before the message loop is actually |
| 114 | // created (when it's associated with a thread), we keep tasks posted here |
| 115 | // until that happens. Once the loop_ is created, this is unused. |
| 116 | std::vector<TaskInfo> pending_tasks_; |
| 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(MessageLoopResource); |
| 119 | }; |
| 120 | |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 121 | class PPB_MessageLoop_Proxy : public InterfaceProxy { |
| 122 | public: |
[email protected] | 6de743a | 2012-08-30 20:03:22 | [diff] [blame] | 123 | explicit PPB_MessageLoop_Proxy(Dispatcher* dispatcher); |
Lei Zhang | 94f57fe3 | 2017-08-30 23:58:24 | [diff] [blame^] | 124 | ~PPB_MessageLoop_Proxy() override; |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 125 | |
[email protected] | d4ab9471 | 2012-11-15 21:01:23 | [diff] [blame] | 126 | static const PPB_MessageLoop_1_0* GetInterface(); |
[email protected] | 15b1224 | 2012-01-26 19:28:47 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | DISALLOW_COPY_AND_ASSIGN(PPB_MessageLoop_Proxy); |
| 130 | }; |
| 131 | |
| 132 | } // namespace proxy |
| 133 | } // namespace ppapi |
| 134 | |
| 135 | #endif // PPAPI_PROXY_PPB_MESSAGE_LOOP_PROXY_H_ |