[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] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 12 | #include "ipc/ipc_message.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 13 | #include "ipc/ipc_sender.h" |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 14 | #include "ppapi/c/pp_errors.h" |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 15 | #include "ppapi/proxy/connection.h" |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 16 | #include "ppapi/proxy/plugin_resource_callback.h" |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 17 | #include "ppapi/proxy/ppapi_message_utils.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 18 | #include "ppapi/proxy/ppapi_proxy_export.h" |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 19 | #include "ppapi/proxy/resource_message_params.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 20 | #include "ppapi/shared_impl/resource.h" |
| 21 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 22 | namespace ppapi { |
| 23 | namespace proxy { |
| 24 | |
| 25 | class PluginDispatcher; |
| 26 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 27 | class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 28 | public: |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame^] | 29 | enum Destination { |
| 30 | RENDERER = 0, |
| 31 | BROWSER = 1 |
| 32 | }; |
| 33 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 34 | PluginResource(Connection connection, PP_Instance instance); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 35 | virtual ~PluginResource(); |
| 36 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 37 | // Returns true if we've previously sent a create message to the browser |
| 38 | // or renderer. Generally resources will use these to tell if they should |
| 39 | // lazily send create messages. |
| 40 | bool sent_create_to_browser() const { return sent_create_to_browser_; } |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 41 | bool sent_create_to_renderer() const { return sent_create_to_renderer_; } |
| 42 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 43 | // This handles a reply to a resource call. It works by looking up the |
| 44 | // callback that was registered when CallBrowser/CallRenderer was called |
| 45 | // and calling it with |params| and |msg|. |
| 46 | virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params, |
| 47 | const IPC::Message& msg) OVERRIDE; |
[email protected] | 28df6a0 | 2012-11-08 07:29:45 | [diff] [blame] | 48 | |
| 49 | // Resource overrides. |
| 50 | // Note: Subclasses shouldn't override these methods directly. Instead, they |
| 51 | // should implement LastPluginRefWasDeleted() or InstanceWasDeleted() to get |
| 52 | // notified. |
| 53 | virtual void NotifyLastPluginRefWasDeleted() OVERRIDE; |
| 54 | virtual void NotifyInstanceWasDeleted() OVERRIDE; |
| 55 | |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 56 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 57 | // Sends a create message to the browser or renderer for the current resource. |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 58 | void SendCreate(Destination dest, const IPC::Message& msg); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 59 | |
[email protected] | db70c13 | 2012-12-05 00:41:20 | [diff] [blame] | 60 | // When the host returnes a resource to the plugin, it will create a pending |
| 61 | // ResourceHost and send an ID back to the plugin that identifies the pending |
| 62 | // object. The plugin uses this function to connect the plugin resource with |
| 63 | // the pending host resource. See also PpapiHostMsg_AttachToPendingHost. This |
| 64 | // is in lieu of sending a create message. |
| 65 | void AttachToPendingHost(Destination dest, int pending_host_id); |
| 66 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 67 | // Sends the given IPC message as a resource request to the host |
| 68 | // corresponding to this resource object and does not expect a reply. |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 69 | void Post(Destination dest, const IPC::Message& msg); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 70 | |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 71 | // Like Post() but expects a response. |callback| is a |base::Callback| that |
| 72 | // will be run when a reply message with a sequence number matching that of |
| 73 | // the call is received. |ReplyMsgClass| is the type of the reply message that |
| 74 | // is expected. An example of usage: |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 75 | // |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 76 | // Call<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| 77 | // BROWSER, |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 78 | // PpapiHostMsg_MyResourceType_MyRequestMessage(), |
[email protected] | 28df6a0 | 2012-11-08 07:29:45 | [diff] [blame] | 79 | // base::Bind(&MyPluginResource::ReplyHandler, base::Unretained(this))); |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 80 | // |
| 81 | // If a reply message to this call is received whose type does not match |
| 82 | // |ReplyMsgClass| (for example, in the case of an error), the callback will |
| 83 | // still be invoked but with the default values of the message parameters. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 84 | // |
| 85 | // Returns the new request's sequence number which can be used to identify |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 86 | // the callback. This value will never be 0, which you can use to identify |
| 87 | // an invalid callback. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 88 | // |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 89 | // Note: 1) When all plugin references to this resource are gone or the |
[email protected] | 28df6a0 | 2012-11-08 07:29:45 | [diff] [blame] | 90 | // corresponding plugin instance is deleted, all pending callbacks |
| 91 | // are abandoned. |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 92 | // 2) It is *not* recommended to let |callback| hold any reference to |
[email protected] | 28df6a0 | 2012-11-08 07:29:45 | [diff] [blame] | 93 | // |this|, in which it will be stored. Otherwise, this object will |
| 94 | // live forever if we fail to clean up the callback. It is safe to |
| 95 | // use base::Unretained(this) or a weak pointer, because this object |
| 96 | // will outlive the callback. |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 97 | template<typename ReplyMsgClass, typename CallbackType> |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 98 | int32_t Call(Destination dest, |
| 99 | const IPC::Message& msg, |
| 100 | const CallbackType& callback); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 101 | |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 102 | // Calls the browser/renderer with sync messages. Returns the pepper error |
| 103 | // code from the call. |
| 104 | // |ReplyMsgClass| is the type of the reply message that is expected. If it |
| 105 | // carries x parameters, then the method with x out parameters should be used. |
| 106 | // An example of usage: |
| 107 | // |
| 108 | // // Assuming the reply message carries a string and an integer. |
| 109 | // std::string param_1; |
| 110 | // int param_2 = 0; |
| 111 | // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| 112 | // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(), |
| 113 | // ¶m_1, ¶m_2); |
| 114 | template <class ReplyMsgClass> |
| 115 | int32_t SyncCall(Destination dest, const IPC::Message& msg); |
| 116 | template <class ReplyMsgClass, class A> |
| 117 | int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a); |
| 118 | template <class ReplyMsgClass, class A, class B> |
| 119 | int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b); |
| 120 | template <class ReplyMsgClass, class A, class B, class C> |
| 121 | int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c); |
| 122 | template <class ReplyMsgClass, class A, class B, class C, class D> |
| 123 | int32_t SyncCall( |
| 124 | Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d); |
| 125 | template <class ReplyMsgClass, class A, class B, class C, class D, class E> |
| 126 | int32_t SyncCall( |
| 127 | Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e); |
[email protected] | ff44fc1 | 2012-10-03 00:52:16 | [diff] [blame] | 128 | |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 129 | int32_t GenericSyncCall(Destination dest, |
| 130 | const IPC::Message& msg, |
| 131 | IPC::Message* reply_msg, |
| 132 | ResourceMessageReplyParams* reply_params); |
| 133 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 134 | private: |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame^] | 135 | IPC::Sender* GetSender(Destination dest) { |
| 136 | return dest == RENDERER ? connection_.renderer_sender : |
| 137 | connection_.browser_sender; |
| 138 | } |
| 139 | |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 140 | // Helper function to send a |PpapiHostMsg_ResourceCall| to the given |
| 141 | // destination with |nested_msg| and |call_params|. |
| 142 | bool SendResourceCall(Destination dest, |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 143 | const ResourceMessageCallParams& call_params, |
| 144 | const IPC::Message& nested_msg); |
| 145 | |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 146 | int32_t GetNextSequence(); |
| 147 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 148 | Connection connection_; |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 149 | |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 150 | // Use GetNextSequence to retrieve the next value. |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 151 | int32_t next_sequence_number_; |
| 152 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 153 | bool sent_create_to_browser_; |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 154 | bool sent_create_to_renderer_; |
| 155 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 156 | typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
| 157 | CallbackMap; |
| 158 | CallbackMap callbacks_; |
| 159 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 160 | DISALLOW_COPY_AND_ASSIGN(PluginResource); |
| 161 | }; |
| 162 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 163 | template<typename ReplyMsgClass, typename CallbackType> |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 164 | int32_t PluginResource::Call(Destination dest, |
| 165 | const IPC::Message& msg, |
| 166 | const CallbackType& callback) { |
| 167 | ResourceMessageCallParams params(pp_resource(), next_sequence_number_++); |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 168 | // Stash the |callback| in |callbacks_| identified by the sequence number of |
| 169 | // the call. |
| 170 | scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
| 171 | new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback)); |
| 172 | callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
| 173 | params.set_has_callback(); |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 174 | SendResourceCall(dest, params, msg); |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 175 | return params.sequence(); |
| 176 | } |
| 177 | |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 178 | template <class ReplyMsgClass> |
| 179 | int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) { |
| 180 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 181 | ResourceMessageReplyParams reply_params; |
| 182 | return GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | template <class ReplyMsgClass, class A> |
| 186 | int32_t PluginResource::SyncCall( |
| 187 | Destination dest, const IPC::Message& msg, A* a) { |
| 188 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 189 | ResourceMessageReplyParams reply_params; |
| 190 | int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 191 | |
| 192 | if (UnpackMessage<ReplyMsgClass>(reply, a)) |
| 193 | return result; |
| 194 | return PP_ERROR_FAILED; |
| 195 | } |
| 196 | |
| 197 | template <class ReplyMsgClass, class A, class B> |
| 198 | int32_t PluginResource::SyncCall( |
| 199 | Destination dest, const IPC::Message& msg, A* a, B* b) { |
| 200 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 201 | ResourceMessageReplyParams reply_params; |
| 202 | int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 203 | |
| 204 | if (UnpackMessage<ReplyMsgClass>(reply, a, b)) |
| 205 | return result; |
| 206 | return PP_ERROR_FAILED; |
| 207 | } |
| 208 | |
| 209 | template <class ReplyMsgClass, class A, class B, class C> |
| 210 | int32_t PluginResource::SyncCall( |
| 211 | Destination dest, const IPC::Message& msg, A* a, B* b, C* c) { |
| 212 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 213 | ResourceMessageReplyParams reply_params; |
| 214 | int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 215 | |
| 216 | if (UnpackMessage<ReplyMsgClass>(reply, a, b, c)) |
| 217 | return result; |
| 218 | return PP_ERROR_FAILED; |
| 219 | } |
| 220 | |
| 221 | template <class ReplyMsgClass, class A, class B, class C, class D> |
| 222 | int32_t PluginResource::SyncCall( |
| 223 | Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d) { |
| 224 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 225 | ResourceMessageReplyParams reply_params; |
| 226 | int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 227 | |
| 228 | if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d)) |
| 229 | return result; |
| 230 | return PP_ERROR_FAILED; |
| 231 | } |
| 232 | |
| 233 | template <class ReplyMsgClass, class A, class B, class C, class D, class E> |
| 234 | int32_t PluginResource::SyncCall( |
| 235 | Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) { |
| 236 | IPC::Message reply; |
[email protected] | 2432c058 | 2012-12-04 23:32:50 | [diff] [blame] | 237 | ResourceMessageReplyParams reply_params; |
| 238 | int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 239 | |
| 240 | if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e)) |
| 241 | return result; |
| 242 | return PP_ERROR_FAILED; |
| 243 | } |
| 244 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 245 | } // namespace proxy |
| 246 | } // namespace ppapi |
| 247 | |
| 248 | #endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_ |