blob: 0fabab20b3df639dcb4987cc838643a51f003439 [file] [log] [blame]
[email protected]eccf80312012-07-14 15:43:421// 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
avie029c4132015-12-23 06:45:228#include <stdint.h>
9
[email protected]e1f5c9b2012-10-04 00:07:4410#include <map>
11
[email protected]eccf80312012-07-14 15:43:4212#include "base/compiler_specific.h"
avie029c4132015-12-23 06:45:2213#include "base/macros.h"
[email protected]511c58e2013-12-12 12:25:3314#include "base/memory/ref_counted.h"
[email protected]58786932012-10-13 10:16:0815#include "ipc/ipc_message.h"
[email protected]eccf80312012-07-14 15:43:4216#include "ipc/ipc_sender.h"
[email protected]58786932012-10-13 10:16:0817#include "ppapi/c/pp_errors.h"
[email protected]93df81e2012-08-10 22:22:4618#include "ppapi/proxy/connection.h"
[email protected]e1f5c9b2012-10-04 00:07:4419#include "ppapi/proxy/plugin_resource_callback.h"
[email protected]58786932012-10-13 10:16:0820#include "ppapi/proxy/ppapi_message_utils.h"
[email protected]eccf80312012-07-14 15:43:4221#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]9164da32012-10-16 03:40:5722#include "ppapi/proxy/resource_message_params.h"
[email protected]511c58e2013-12-12 12:25:3323#include "ppapi/proxy/resource_reply_thread_registrar.h"
[email protected]eccf80312012-07-14 15:43:4224#include "ppapi/shared_impl/resource.h"
[email protected]511c58e2013-12-12 12:25:3325#include "ppapi/shared_impl/tracked_callback.h"
[email protected]eccf80312012-07-14 15:43:4226namespace ppapi {
27namespace proxy {
28
29class PluginDispatcher;
30
[email protected]93df81e2012-08-10 22:22:4631class PPAPI_PROXY_EXPORT PluginResource : public Resource {
[email protected]eccf80312012-07-14 15:43:4232 public:
[email protected]4f01c762012-12-05 02:44:1833 enum Destination {
34 RENDERER = 0,
35 BROWSER = 1
36 };
37
[email protected]93df81e2012-08-10 22:22:4638 PluginResource(Connection connection, PP_Instance instance);
nicke4784432015-04-23 14:01:4839 ~PluginResource() override;
[email protected]eccf80312012-07-14 15:43:4240
[email protected]93df81e2012-08-10 22:22:4641 // Returns true if we've previously sent a create message to the browser
42 // or renderer. Generally resources will use these to tell if they should
43 // lazily send create messages.
44 bool sent_create_to_browser() const { return sent_create_to_browser_; }
[email protected]eccf80312012-07-14 15:43:4245 bool sent_create_to_renderer() const { return sent_create_to_renderer_; }
46
[email protected]e1f5c9b2012-10-04 00:07:4447 // This handles a reply to a resource call. It works by looking up the
48 // callback that was registered when CallBrowser/CallRenderer was called
49 // and calling it with |params| and |msg|.
nicke4784432015-04-23 14:01:4850 void OnReplyReceived(const proxy::ResourceMessageReplyParams& params,
51 const IPC::Message& msg) override;
[email protected]28df6a02012-11-08 07:29:4552
53 // Resource overrides.
54 // Note: Subclasses shouldn't override these methods directly. Instead, they
55 // should implement LastPluginRefWasDeleted() or InstanceWasDeleted() to get
56 // notified.
nicke4784432015-04-23 14:01:4857 void NotifyLastPluginRefWasDeleted() override;
58 void NotifyInstanceWasDeleted() override;
[email protected]58786932012-10-13 10:16:0859
[email protected]93df81e2012-08-10 22:22:4660 // Sends a create message to the browser or renderer for the current resource.
[email protected]9164da32012-10-16 03:40:5761 void SendCreate(Destination dest, const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4262
[email protected]db70c132012-12-05 00:41:2063 // When the host returnes a resource to the plugin, it will create a pending
64 // ResourceHost and send an ID back to the plugin that identifies the pending
65 // object. The plugin uses this function to connect the plugin resource with
66 // the pending host resource. See also PpapiHostMsg_AttachToPendingHost. This
67 // is in lieu of sending a create message.
68 void AttachToPendingHost(Destination dest, int pending_host_id);
69
[email protected]eccf80312012-07-14 15:43:4270 // Sends the given IPC message as a resource request to the host
71 // corresponding to this resource object and does not expect a reply.
[email protected]9164da32012-10-16 03:40:5772 void Post(Destination dest, const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4273
[email protected]9164da32012-10-16 03:40:5774 // Like Post() but expects a response. |callback| is a |base::Callback| that
75 // will be run when a reply message with a sequence number matching that of
76 // the call is received. |ReplyMsgClass| is the type of the reply message that
77 // is expected. An example of usage:
[email protected]e1f5c9b2012-10-04 00:07:4478 //
[email protected]9164da32012-10-16 03:40:5779 // Call<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
80 // BROWSER,
[email protected]e1f5c9b2012-10-04 00:07:4481 // PpapiHostMsg_MyResourceType_MyRequestMessage(),
[email protected]28df6a02012-11-08 07:29:4582 // base::Bind(&MyPluginResource::ReplyHandler, base::Unretained(this)));
[email protected]e1f5c9b2012-10-04 00:07:4483 //
84 // If a reply message to this call is received whose type does not match
85 // |ReplyMsgClass| (for example, in the case of an error), the callback will
86 // still be invoked but with the default values of the message parameters.
[email protected]eccf80312012-07-14 15:43:4287 //
88 // Returns the new request's sequence number which can be used to identify
[email protected]d84e9992012-11-08 22:13:2889 // the callback. This value will never be 0, which you can use to identify
90 // an invalid callback.
[email protected]eccf80312012-07-14 15:43:4291 //
[email protected]d84e9992012-11-08 22:13:2892 // Note: 1) When all plugin references to this resource are gone or the
[email protected]28df6a02012-11-08 07:29:4593 // corresponding plugin instance is deleted, all pending callbacks
94 // are abandoned.
[email protected]d84e9992012-11-08 22:13:2895 // 2) It is *not* recommended to let |callback| hold any reference to
[email protected]28df6a02012-11-08 07:29:4596 // |this|, in which it will be stored. Otherwise, this object will
97 // live forever if we fail to clean up the callback. It is safe to
98 // use base::Unretained(this) or a weak pointer, because this object
99 // will outlive the callback.
[email protected]e1f5c9b2012-10-04 00:07:44100 template<typename ReplyMsgClass, typename CallbackType>
[email protected]9164da32012-10-16 03:40:57101 int32_t Call(Destination dest,
102 const IPC::Message& msg,
103 const CallbackType& callback);
[email protected]eccf80312012-07-14 15:43:42104
[email protected]511c58e2013-12-12 12:25:33105 // Comparing with the previous Call() method, this method takes
106 // |reply_thread_hint| as a hint to determine which thread to handle the reply
107 // message.
108 //
109 // If |reply_thread_hint| is non-blocking, the reply message will be handled
110 // on the target thread of the callback; otherwise, it will be handled on the
111 // main thread.
112 //
113 // If handling a reply message will cause a TrackedCallback to be run, it is
114 // recommended to use this version of Call(). It eliminates unnecessary
115 // thread switching and therefore has better performance.
116 template<typename ReplyMsgClass, typename CallbackType>
117 int32_t Call(Destination dest,
118 const IPC::Message& msg,
119 const CallbackType& callback,
120 scoped_refptr<TrackedCallback> reply_thread_hint);
121
[email protected]58786932012-10-13 10:16:08122 // Calls the browser/renderer with sync messages. Returns the pepper error
123 // code from the call.
124 // |ReplyMsgClass| is the type of the reply message that is expected. If it
125 // carries x parameters, then the method with x out parameters should be used.
126 // An example of usage:
127 //
128 // // Assuming the reply message carries a string and an integer.
129 // std::string param_1;
130 // int param_2 = 0;
131 // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
132 // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(),
133 // &param_1, &param_2);
134 template <class ReplyMsgClass>
135 int32_t SyncCall(Destination dest, const IPC::Message& msg);
136 template <class ReplyMsgClass, class A>
137 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a);
138 template <class ReplyMsgClass, class A, class B>
139 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b);
140 template <class ReplyMsgClass, class A, class B, class C>
141 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c);
142 template <class ReplyMsgClass, class A, class B, class C, class D>
143 int32_t SyncCall(
144 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d);
145 template <class ReplyMsgClass, class A, class B, class C, class D, class E>
146 int32_t SyncCall(
147 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e);
[email protected]ff44fc12012-10-03 00:52:16148
[email protected]0c92b0d2012-12-08 00:46:23149 int32_t GenericSyncCall(Destination dest,
150 const IPC::Message& msg,
151 IPC::Message* reply_msg,
152 ResourceMessageReplyParams* reply_params);
153
[email protected]47cb253f2013-05-16 01:50:40154 const Connection& connection() { return connection_; }
155
[email protected]eccf80312012-07-14 15:43:42156 private:
[email protected]4f01c762012-12-05 02:44:18157 IPC::Sender* GetSender(Destination dest) {
158 return dest == RENDERER ? connection_.renderer_sender :
159 connection_.browser_sender;
160 }
161
[email protected]9164da32012-10-16 03:40:57162 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given
163 // destination with |nested_msg| and |call_params|.
164 bool SendResourceCall(Destination dest,
[email protected]e1f5c9b2012-10-04 00:07:44165 const ResourceMessageCallParams& call_params,
166 const IPC::Message& nested_msg);
167
[email protected]d84e9992012-11-08 22:13:28168 int32_t GetNextSequence();
169
[email protected]93df81e2012-08-10 22:22:46170 Connection connection_;
[email protected]eccf80312012-07-14 15:43:42171
[email protected]d84e9992012-11-08 22:13:28172 // Use GetNextSequence to retrieve the next value.
[email protected]eccf80312012-07-14 15:43:42173 int32_t next_sequence_number_;
174
[email protected]93df81e2012-08-10 22:22:46175 bool sent_create_to_browser_;
[email protected]eccf80312012-07-14 15:43:42176 bool sent_create_to_renderer_;
177
[email protected]e1f5c9b2012-10-04 00:07:44178 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> >
179 CallbackMap;
180 CallbackMap callbacks_;
181
[email protected]511c58e2013-12-12 12:25:33182 scoped_refptr<ResourceReplyThreadRegistrar> resource_reply_thread_registrar_;
183
[email protected]eccf80312012-07-14 15:43:42184 DISALLOW_COPY_AND_ASSIGN(PluginResource);
185};
186
[email protected]e1f5c9b2012-10-04 00:07:44187template<typename ReplyMsgClass, typename CallbackType>
[email protected]9164da32012-10-16 03:40:57188int32_t PluginResource::Call(Destination dest,
189 const IPC::Message& msg,
190 const CallbackType& callback) {
[email protected]511c58e2013-12-12 12:25:33191 return Call<ReplyMsgClass>(dest, msg, callback, NULL);
192}
193
194template<typename ReplyMsgClass, typename CallbackType>
195int32_t PluginResource::Call(
196 Destination dest,
197 const IPC::Message& msg,
198 const CallbackType& callback,
199 scoped_refptr<TrackedCallback> reply_thread_hint) {
[email protected]278f5eb92013-03-27 00:26:18200 TRACE_EVENT2("ppapi proxy", "PluginResource::Call",
201 "Class", IPC_MESSAGE_ID_CLASS(msg.type()),
202 "Line", IPC_MESSAGE_ID_LINE(msg.type()));
[email protected]9164da32012-10-16 03:40:57203 ResourceMessageCallParams params(pp_resource(), next_sequence_number_++);
[email protected]e1f5c9b2012-10-04 00:07:44204 // Stash the |callback| in |callbacks_| identified by the sequence number of
205 // the call.
206 scoped_refptr<PluginResourceCallbackBase> plugin_callback(
207 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback));
208 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback));
209 params.set_has_callback();
[email protected]511c58e2013-12-12 12:25:33210
Daniel Cheng6d3ae972014-08-26 00:27:38211 if (resource_reply_thread_registrar_.get()) {
[email protected]511c58e2013-12-12 12:25:33212 resource_reply_thread_registrar_->Register(
213 pp_resource(), params.sequence(), reply_thread_hint);
214 }
[email protected]9164da32012-10-16 03:40:57215 SendResourceCall(dest, params, msg);
[email protected]e1f5c9b2012-10-04 00:07:44216 return params.sequence();
217}
218
[email protected]58786932012-10-13 10:16:08219template <class ReplyMsgClass>
220int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) {
221 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23222 ResourceMessageReplyParams reply_params;
223 return GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08224}
225
226template <class ReplyMsgClass, class A>
227int32_t PluginResource::SyncCall(
228 Destination dest, const IPC::Message& msg, A* a) {
229 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23230 ResourceMessageReplyParams reply_params;
231 int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08232
233 if (UnpackMessage<ReplyMsgClass>(reply, a))
234 return result;
235 return PP_ERROR_FAILED;
236}
237
238template <class ReplyMsgClass, class A, class B>
239int32_t PluginResource::SyncCall(
240 Destination dest, const IPC::Message& msg, A* a, B* b) {
241 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23242 ResourceMessageReplyParams reply_params;
243 int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08244
245 if (UnpackMessage<ReplyMsgClass>(reply, a, b))
246 return result;
247 return PP_ERROR_FAILED;
248}
249
250template <class ReplyMsgClass, class A, class B, class C>
251int32_t PluginResource::SyncCall(
252 Destination dest, const IPC::Message& msg, A* a, B* b, C* c) {
253 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23254 ResourceMessageReplyParams reply_params;
255 int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08256
257 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c))
258 return result;
259 return PP_ERROR_FAILED;
260}
261
262template <class ReplyMsgClass, class A, class B, class C, class D>
263int32_t PluginResource::SyncCall(
264 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d) {
265 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23266 ResourceMessageReplyParams reply_params;
267 int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08268
269 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d))
270 return result;
271 return PP_ERROR_FAILED;
272}
273
274template <class ReplyMsgClass, class A, class B, class C, class D, class E>
275int32_t PluginResource::SyncCall(
276 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) {
277 IPC::Message reply;
[email protected]0c92b0d2012-12-08 00:46:23278 ResourceMessageReplyParams reply_params;
279 int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
[email protected]58786932012-10-13 10:16:08280
281 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e))
282 return result;
283 return PP_ERROR_FAILED;
284}
285
[email protected]eccf80312012-07-14 15:43:42286} // namespace proxy
287} // namespace ppapi
288
289#endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_