[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 1 | // Copyright 2014 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_MESSAGE_HANDLER_H_ |
| 6 | #define PPAPI_PROXY_MESSAGE_HANDLER_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] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 13 | #include "ppapi/c/pp_resource.h" |
| 14 | #include "ppapi/c/ppp_message_handler.h" |
| 15 | #include "ppapi/proxy/ppapi_proxy_export.h" |
| 16 | |
| 17 | namespace IPC { |
| 18 | class Message; |
| 19 | } |
| 20 | |
| 21 | namespace ppapi { |
| 22 | |
| 23 | class ScopedPPVar; |
| 24 | |
| 25 | namespace proxy { |
| 26 | |
| 27 | class MessageLoopResource; |
| 28 | |
| 29 | // MessageHandler wraps a PPP_MessageHandler to encapsulate calling methods |
| 30 | // on the right thread and calling the Destroy function when this |
| 31 | // MessageHandler is destroyed. |
| 32 | class PPAPI_PROXY_EXPORT MessageHandler { |
| 33 | public: |
| 34 | // Create a MessageHandler. If any parameters are invalid, it will return a |
| 35 | // null scoped_ptr and set |*error| appropriately. |
| 36 | // |handler_if| is the struct of function pointers we will invoke. All of |
| 37 | // the function pointers within must be valid, or we fail |
| 38 | // with PP_ERROR_BADARGUMENT. |
| 39 | // |user_data| is a pointer provided by the plugin that we pass back when we |
| 40 | // call functions in |handler_if|. |
| 41 | // |message_loop| is the message loop where we will invoke functions in |
| 42 | // |handler_if|. Must not be the main thread message loop, |
| 43 | // to try to force the plugin to not over-subscribe the main |
| 44 | // thread. If it's the main thread loop, |error| will be set |
| 45 | // to PP_ERROR_WRONGTHREAD. |
| 46 | // |error| is an out-param that will be set on failure. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 47 | static std::unique_ptr<MessageHandler> Create( |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 48 | PP_Instance instance, |
Dave Michael | 970fa0cf | 2014-09-17 20:56:10 | [diff] [blame] | 49 | const PPP_MessageHandler_0_2* handler_if, |
| 50 | void* user_data, |
| 51 | PP_Resource message_loop, |
| 52 | int32_t* error); |
Peter Boström | 3d5b3cb | 2021-09-23 21:35:45 | [diff] [blame] | 53 | |
| 54 | MessageHandler(const MessageHandler&) = delete; |
| 55 | MessageHandler& operator=(const MessageHandler&) = delete; |
| 56 | |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 57 | ~MessageHandler(); |
| 58 | |
| 59 | bool LoopIsValid() const; |
| 60 | |
| 61 | void HandleMessage(ScopedPPVar var); |
| 62 | void HandleBlockingMessage(ScopedPPVar var, |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 63 | std::unique_ptr<IPC::Message> reply_msg); |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | MessageHandler(PP_Instance instance, |
Dave Michael | 970fa0cf | 2014-09-17 20:56:10 | [diff] [blame] | 67 | const PPP_MessageHandler_0_2* handler_if, |
| 68 | void* user_data, |
| 69 | scoped_refptr<MessageLoopResource> message_loop); |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 70 | PP_Instance instance_; |
Dave Michael | 970fa0cf | 2014-09-17 20:56:10 | [diff] [blame] | 71 | const PPP_MessageHandler_0_2* handler_if_; |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 72 | void* user_data_; |
| 73 | scoped_refptr<MessageLoopResource> message_loop_; |
[email protected] | e87640bd | 2014-06-18 16:44:00 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | } // namespace proxy |
| 77 | } // namespace ppapi |
| 78 | |
| 79 | #endif // PPAPI_PROXY_MESSAGE_HANDLER_H_ |