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