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.
5 #include "ppapi/proxy/ppp_class_proxy.h"
7 #include "ppapi/c/dev/ppb_var_deprecated.h"
8 #include "ppapi/c/dev/ppp_class_deprecated.h"
9 #include "ppapi/c/pp_var.h"
10 #include "ppapi/proxy/dispatcher.h"
11 #include "ppapi/proxy/plugin_globals.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/proxy/serialized_var.h"
14 #include "ppapi/shared_impl/proxy_lock.h"
15 #include "ppapi/shared_impl/api_id.h"
22 // PPP_Class in the browser implementation -------------------------------------
24 // Represents a plugin-implemented class in the browser process. This just
25 // stores the data necessary to call back the plugin.
27 ObjectProxy(Dispatcher
* d
, int64 p
, int64 ud
)
33 Dispatcher
* dispatcher
;
38 ObjectProxy
* ToObjectProxy(void* data
) {
39 ObjectProxy
* obj
= reinterpret_cast<ObjectProxy
*>(data
);
40 if (!obj
|| !obj
->dispatcher
)
42 if (!obj
->dispatcher
->permissions().HasPermission(PERMISSION_DEV
))
47 bool HasProperty(void* object
, PP_Var name
, PP_Var
* exception
) {
48 ObjectProxy
* obj
= ToObjectProxy(object
);
53 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
54 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_HasProperty(
55 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
56 SerializedVarSendInput(obj
->dispatcher
, name
), &se
, &result
));
60 bool HasMethod(void* object
, PP_Var name
, PP_Var
* exception
) {
61 ObjectProxy
* obj
= ToObjectProxy(object
);
66 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
67 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_HasMethod(
68 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
69 SerializedVarSendInput(obj
->dispatcher
, name
), &se
, &result
));
73 PP_Var
GetProperty(void* object
,
76 ObjectProxy
* obj
= ToObjectProxy(object
);
78 return PP_MakeUndefined();
80 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
81 ReceiveSerializedVarReturnValue result
;
82 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_GetProperty(
83 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
84 SerializedVarSendInput(obj
->dispatcher
, name
), &se
, &result
));
85 return result
.Return(obj
->dispatcher
);
88 void GetAllPropertyNames(void* object
,
89 uint32_t* property_count
,
93 // TODO(brettw) implement this.
96 void SetProperty(void* object
,
100 ObjectProxy
* obj
= ToObjectProxy(object
);
104 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
105 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_SetProperty(
106 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
107 SerializedVarSendInput(obj
->dispatcher
, name
),
108 SerializedVarSendInput(obj
->dispatcher
, value
), &se
));
111 void RemoveProperty(void* object
,
114 ObjectProxy
* obj
= ToObjectProxy(object
);
118 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
119 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_RemoveProperty(
120 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
121 SerializedVarSendInput(obj
->dispatcher
, name
), &se
));
124 PP_Var
Call(void* object
,
129 ObjectProxy
* obj
= ToObjectProxy(object
);
131 return PP_MakeUndefined();
133 ReceiveSerializedVarReturnValue result
;
134 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
135 std::vector
<SerializedVar
> argv_vect
;
136 SerializedVarSendInput::ConvertVector(obj
->dispatcher
, argv
, argc
,
139 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_Call(
140 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
,
141 SerializedVarSendInput(obj
->dispatcher
, method_name
), argv_vect
,
143 return result
.Return(obj
->dispatcher
);
146 PP_Var
Construct(void* object
,
150 ObjectProxy
* obj
= ToObjectProxy(object
);
152 return PP_MakeUndefined();
154 ReceiveSerializedVarReturnValue result
;
155 ReceiveSerializedException
se(obj
->dispatcher
, exception
);
156 std::vector
<SerializedVar
> argv_vect
;
157 SerializedVarSendInput::ConvertVector(obj
->dispatcher
, argv
, argc
,
160 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_Construct(
162 obj
->ppp_class
, obj
->user_data
, argv_vect
, &se
, &result
));
163 return result
.Return(obj
->dispatcher
);
166 void Deallocate(void* object
) {
167 ObjectProxy
* obj
= ToObjectProxy(object
);
171 obj
->dispatcher
->Send(new PpapiMsg_PPPClass_Deallocate(
172 API_ID_PPP_CLASS
, obj
->ppp_class
, obj
->user_data
));
176 const PPP_Class_Deprecated class_interface
= {
180 &GetAllPropertyNames
,
188 // Plugin helper functions -----------------------------------------------------
190 // Converts an int64 object from IPC to a PPP_Class* for calling into the
191 // plugin's implementation.
192 const PPP_Class_Deprecated
* ToPPPClass(int64 value
) {
193 return reinterpret_cast<const PPP_Class_Deprecated
*>(
194 static_cast<intptr_t>(value
));
197 // Converts an int64 object from IPC to a void* for calling into the plugin's
198 // implementation as the user data.
199 void* ToUserData(int64 value
) {
200 return reinterpret_cast<void*>(static_cast<intptr_t>(value
));
205 // PPP_Class_Proxy -------------------------------------------------------------
207 PPP_Class_Proxy::PPP_Class_Proxy(Dispatcher
* dispatcher
)
208 : InterfaceProxy(dispatcher
) {
211 PPP_Class_Proxy::~PPP_Class_Proxy() {
215 InterfaceProxy
* PPP_Class_Proxy::Create(Dispatcher
* dispatcher
) {
216 return new PPP_Class_Proxy(dispatcher
);
220 PP_Var
PPP_Class_Proxy::CreateProxiedObject(const PPB_Var_Deprecated
* var
,
221 Dispatcher
* dispatcher
,
222 PP_Instance instance_id
,
225 ObjectProxy
* object_proxy
= new ObjectProxy(dispatcher
,
226 ppp_class
, class_data
);
227 return var
->CreateObject(instance_id
, &class_interface
, object_proxy
);
231 PP_Bool
PPP_Class_Proxy::IsInstanceOf(const PPB_Var_Deprecated
* ppb_var_impl
,
234 int64
* ppp_class_data
) {
235 void* proxied_object
= NULL
;
236 if (ppb_var_impl
->IsInstanceOf(var
,
239 if (static_cast<ObjectProxy
*>(proxied_object
)->ppp_class
== ppp_class
) {
240 DCHECK(ppp_class_data
);
241 *ppp_class_data
= static_cast<ObjectProxy
*>(proxied_object
)->user_data
;
248 bool PPP_Class_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
249 if (!dispatcher()->IsPlugin())
250 return false; // These messages are only valid from host->plugin.
253 IPC_BEGIN_MESSAGE_MAP(PPP_Class_Proxy
, msg
)
254 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_HasProperty
,
256 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_HasMethod
,
258 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_GetProperty
,
260 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_EnumerateProperties
,
261 OnMsgEnumerateProperties
)
262 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_SetProperty
,
264 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_Call
,
266 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_Construct
,
268 IPC_MESSAGE_HANDLER(PpapiMsg_PPPClass_Deallocate
,
270 IPC_MESSAGE_UNHANDLED(handled
= false)
271 IPC_END_MESSAGE_MAP()
275 void PPP_Class_Proxy::OnMsgHasProperty(int64 ppp_class
, int64 object
,
276 SerializedVarReceiveInput property
,
277 SerializedVarOutParam exception
,
279 if (!ValidateUserData(ppp_class
, object
, &exception
))
281 *result
= CallWhileUnlocked(ToPPPClass(ppp_class
)->HasProperty
,
283 property
.Get(dispatcher()),
284 exception
.OutParam(dispatcher()));
287 void PPP_Class_Proxy::OnMsgHasMethod(int64 ppp_class
, int64 object
,
288 SerializedVarReceiveInput property
,
289 SerializedVarOutParam exception
,
291 if (!ValidateUserData(ppp_class
, object
, &exception
))
293 *result
= CallWhileUnlocked(ToPPPClass(ppp_class
)->HasMethod
,
295 property
.Get(dispatcher()),
296 exception
.OutParam(dispatcher()));
299 void PPP_Class_Proxy::OnMsgGetProperty(int64 ppp_class
, int64 object
,
300 SerializedVarReceiveInput property
,
301 SerializedVarOutParam exception
,
302 SerializedVarReturnValue result
) {
303 if (!ValidateUserData(ppp_class
, object
, &exception
))
305 result
.Return(dispatcher(), CallWhileUnlocked(
306 ToPPPClass(ppp_class
)->GetProperty
,
307 ToUserData(object
), property
.Get(dispatcher()),
308 exception
.OutParam(dispatcher())));
311 void PPP_Class_Proxy::OnMsgEnumerateProperties(
312 int64 ppp_class
, int64 object
,
313 std::vector
<SerializedVar
>* props
,
314 SerializedVarOutParam exception
) {
315 if (!ValidateUserData(ppp_class
, object
, &exception
))
318 // TODO(brettw) implement this.
321 void PPP_Class_Proxy::OnMsgSetProperty(int64 ppp_class
, int64 object
,
322 SerializedVarReceiveInput property
,
323 SerializedVarReceiveInput value
,
324 SerializedVarOutParam exception
) {
325 if (!ValidateUserData(ppp_class
, object
, &exception
))
327 CallWhileUnlocked(ToPPPClass(ppp_class
)->SetProperty
,
328 ToUserData(object
), property
.Get(dispatcher()), value
.Get(dispatcher()),
329 exception
.OutParam(dispatcher()));
332 void PPP_Class_Proxy::OnMsgRemoveProperty(int64 ppp_class
, int64 object
,
333 SerializedVarReceiveInput property
,
334 SerializedVarOutParam exception
) {
335 if (!ValidateUserData(ppp_class
, object
, &exception
))
337 CallWhileUnlocked(ToPPPClass(ppp_class
)->RemoveProperty
,
338 ToUserData(object
), property
.Get(dispatcher()),
339 exception
.OutParam(dispatcher()));
342 void PPP_Class_Proxy::OnMsgCall(
343 int64 ppp_class
, int64 object
,
344 SerializedVarReceiveInput method_name
,
345 SerializedVarVectorReceiveInput arg_vector
,
346 SerializedVarOutParam exception
,
347 SerializedVarReturnValue result
) {
348 if (!ValidateUserData(ppp_class
, object
, &exception
))
350 uint32_t arg_count
= 0;
351 PP_Var
* args
= arg_vector
.Get(dispatcher(), &arg_count
);
352 result
.Return(dispatcher(), CallWhileUnlocked(ToPPPClass(ppp_class
)->Call
,
353 ToUserData(object
), method_name
.Get(dispatcher()),
354 arg_count
, args
, exception
.OutParam(dispatcher())));
357 void PPP_Class_Proxy::OnMsgConstruct(
358 int64 ppp_class
, int64 object
,
359 SerializedVarVectorReceiveInput arg_vector
,
360 SerializedVarOutParam exception
,
361 SerializedVarReturnValue result
) {
362 if (!ValidateUserData(ppp_class
, object
, &exception
))
364 uint32_t arg_count
= 0;
365 PP_Var
* args
= arg_vector
.Get(dispatcher(), &arg_count
);
366 result
.Return(dispatcher(), CallWhileUnlocked(
367 ToPPPClass(ppp_class
)->Construct
,
368 ToUserData(object
), arg_count
, args
, exception
.OutParam(dispatcher())));
371 void PPP_Class_Proxy::OnMsgDeallocate(int64 ppp_class
, int64 object
) {
372 if (!ValidateUserData(ppp_class
, object
, NULL
))
374 PluginGlobals::Get()->plugin_var_tracker()->PluginImplementedObjectDestroyed(
376 CallWhileUnlocked(ToPPPClass(ppp_class
)->Deallocate
, ToUserData(object
));
379 bool PPP_Class_Proxy::ValidateUserData(int64 ppp_class
, int64 class_data
,
380 SerializedVarOutParam
* exception
) {
381 if (!PluginGlobals::Get()->plugin_var_tracker()->ValidatePluginObjectCall(
382 ToPPPClass(ppp_class
), ToUserData(class_data
))) {
383 // Set the exception. This is so the caller will know about the error and
384 // also that we won't assert that somebody forgot to call OutParam on the
385 // output parameter. Although this exception of "1" won't be very useful
386 // this shouldn't happen in normal usage, only when the renderer is being
389 *exception
->OutParam(dispatcher()) = PP_MakeInt32(1);