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