[email protected] | eccf8031 | 2012-07-14 15:43:42 | [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_PLUGIN_RESOURCE_H_ |
| 6 | #define PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| 7 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 8 | #include <map> |
| 9 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
| 11 | #include "ipc/ipc_sender.h" |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 12 | #include "ppapi/proxy/connection.h" |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 13 | #include "ppapi/proxy/plugin_resource_callback.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 14 | #include "ppapi/proxy/ppapi_proxy_export.h" |
| 15 | #include "ppapi/shared_impl/resource.h" |
| 16 | |
| 17 | namespace IPC { |
| 18 | class Message; |
| 19 | } |
| 20 | |
| 21 | namespace ppapi { |
| 22 | namespace proxy { |
| 23 | |
| 24 | class PluginDispatcher; |
| 25 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 26 | class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 27 | public: |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 28 | PluginResource(Connection connection, PP_Instance instance); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 29 | virtual ~PluginResource(); |
| 30 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 31 | // Returns true if we've previously sent a create message to the browser |
| 32 | // or renderer. Generally resources will use these to tell if they should |
| 33 | // lazily send create messages. |
| 34 | bool sent_create_to_browser() const { return sent_create_to_browser_; } |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 35 | bool sent_create_to_renderer() const { return sent_create_to_renderer_; } |
| 36 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 37 | // This handles a reply to a resource call. It works by looking up the |
| 38 | // callback that was registered when CallBrowser/CallRenderer was called |
| 39 | // and calling it with |params| and |msg|. |
| 40 | virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params, |
| 41 | const IPC::Message& msg) OVERRIDE; |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 42 | protected: |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 43 | // Sends a create message to the browser or renderer for the current resource. |
| 44 | void SendCreateToBrowser(const IPC::Message& msg); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 45 | void SendCreateToRenderer(const IPC::Message& msg); |
| 46 | |
| 47 | // Sends the given IPC message as a resource request to the host |
| 48 | // corresponding to this resource object and does not expect a reply. |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 49 | void PostToBrowser(const IPC::Message& msg); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 50 | void PostToRenderer(const IPC::Message& msg); |
| 51 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 52 | // Like PostToBrowser/Renderer but expects a response. |callback| is |
| 53 | // a |base::Callback| that will be run when a reply message with a sequence |
| 54 | // number matching that of the call is received. |ReplyMsgClass| is the type |
| 55 | // of the reply message that is expected. An example of usage: |
| 56 | // |
| 57 | // CallBrowser<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| 58 | // PpapiHostMsg_MyResourceType_MyRequestMessage(), |
| 59 | // base::Bind(&MyPluginResource::ReplyHandler, this)); |
| 60 | // |
| 61 | // If a reply message to this call is received whose type does not match |
| 62 | // |ReplyMsgClass| (for example, in the case of an error), the callback will |
| 63 | // still be invoked but with the default values of the message parameters. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 64 | // |
| 65 | // Returns the new request's sequence number which can be used to identify |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 66 | // the callback. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 67 | // |
| 68 | // Note that all integers (including 0 and -1) are valid request IDs. |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 69 | template<typename ReplyMsgClass, typename CallbackType> |
| 70 | int32_t CallBrowser(const IPC::Message& msg, const CallbackType& callback); |
| 71 | template<typename ReplyMsgClass, typename CallbackType> |
| 72 | int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 73 | |
[email protected] | ff44fc1 | 2012-10-03 00:52:16 | [diff] [blame] | 74 | // Call the browser/renderer with sync messages. The pepper error code from |
| 75 | // the call is returned and the reply message is stored in |reply_msg|. |
| 76 | int32_t CallBrowserSync(const IPC::Message& msg, IPC::Message* reply_msg); |
| 77 | int32_t CallRendererSync(const IPC::Message& msg, IPC::Message* reply_msg); |
| 78 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 79 | private: |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 80 | // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender |
| 81 | // with |nested_msg| and |call_params|. |
| 82 | bool SendResourceCall(IPC::Sender* sender, |
| 83 | const ResourceMessageCallParams& call_params, |
| 84 | const IPC::Message& nested_msg); |
| 85 | |
| 86 | // Helper function to make a Resource Call to a host with a callback. |
| 87 | template<typename ReplyMsgClass, typename CallbackType> |
| 88 | int32_t CallHost(IPC::Sender* sender, |
| 89 | const IPC::Message& msg, |
| 90 | const CallbackType& callback); |
| 91 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 92 | Connection connection_; |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 93 | |
| 94 | int32_t next_sequence_number_; |
| 95 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 96 | bool sent_create_to_browser_; |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 97 | bool sent_create_to_renderer_; |
| 98 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 99 | typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
| 100 | CallbackMap; |
| 101 | CallbackMap callbacks_; |
| 102 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 103 | DISALLOW_COPY_AND_ASSIGN(PluginResource); |
| 104 | }; |
| 105 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame^] | 106 | template<typename ReplyMsgClass, typename CallbackType> |
| 107 | int32_t PluginResource::CallBrowser(const IPC::Message& msg, |
| 108 | const CallbackType& callback) { |
| 109 | return CallHost<ReplyMsgClass, CallbackType>( |
| 110 | connection_.browser_sender, msg, callback); |
| 111 | } |
| 112 | |
| 113 | template<typename ReplyMsgClass, typename CallbackType> |
| 114 | int32_t PluginResource::CallRenderer(const IPC::Message& msg, |
| 115 | const CallbackType& callback) { |
| 116 | return CallHost<ReplyMsgClass, CallbackType>( |
| 117 | connection_.renderer_sender, msg, callback); |
| 118 | } |
| 119 | |
| 120 | template<typename ReplyMsgClass, typename CallbackType> |
| 121 | int32_t PluginResource::CallHost(IPC::Sender* sender, |
| 122 | const IPC::Message& msg, |
| 123 | const CallbackType& callback) { |
| 124 | ResourceMessageCallParams params(pp_resource(), |
| 125 | next_sequence_number_++); |
| 126 | // Stash the |callback| in |callbacks_| identified by the sequence number of |
| 127 | // the call. |
| 128 | scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
| 129 | new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); |
| 130 | callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
| 131 | params.set_has_callback(); |
| 132 | SendResourceCall(sender, params, msg); |
| 133 | return params.sequence(); |
| 134 | } |
| 135 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 136 | } // namespace proxy |
| 137 | } // namespace ppapi |
| 138 | |
| 139 | #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ |