1 // Copyright (c) 2011 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 "webkit/plugins/ppapi/plugin_object.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "third_party/npapi/bindings/npapi.h"
13 #include "third_party/npapi/bindings/npruntime.h"
14 #include "ppapi/c/dev/ppb_var_deprecated.h"
15 #include "ppapi/c/dev/ppp_class_deprecated.h"
16 #include "ppapi/c/pp_resource.h"
17 #include "ppapi/c/pp_var.h"
18 #include "ppapi/shared_impl/ppapi_globals.h"
19 #include "ppapi/shared_impl/resource_tracker.h"
20 #include "ppapi/shared_impl/var.h"
21 #include "ppapi/shared_impl/var_tracker.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
23 #include "webkit/plugins/ppapi/npapi_glue.h"
24 #include "webkit/plugins/ppapi/plugin_module.h"
25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
26 #include "webkit/plugins/ppapi/string.h"
28 using ppapi::PpapiGlobals
;
29 using ppapi::StringVar
;
31 using WebKit::WebBindings
;
38 const char kInvalidValueException
[] = "Error: Invalid value";
40 // NPObject implementation in terms of PPP_Class_Deprecated --------------------
42 NPObject
* WrapperClass_Allocate(NPP npp
, NPClass
* unused
) {
43 return PluginObject::AllocateObjectWrapper();
46 void WrapperClass_Deallocate(NPObject
* np_object
) {
47 PluginObject
* plugin_object
= PluginObject::FromNPObject(np_object
);
50 plugin_object
->ppp_class()->Deallocate(plugin_object
->ppp_class_data());
54 void WrapperClass_Invalidate(NPObject
* object
) {
57 bool WrapperClass_HasMethod(NPObject
* object
, NPIdentifier method_name
) {
58 NPObjectAccessorWithIdentifier
accessor(object
, method_name
, false);
59 if (!accessor
.is_valid())
62 PPResultAndExceptionToNPResult
result_converter(
63 accessor
.object()->GetNPObject(), NULL
);
64 bool rv
= accessor
.object()->ppp_class()->HasMethod(
65 accessor
.object()->ppp_class_data(), accessor
.identifier(),
66 result_converter
.exception());
67 result_converter
.CheckExceptionForNoResult();
71 bool WrapperClass_Invoke(NPObject
* object
, NPIdentifier method_name
,
72 const NPVariant
* argv
, uint32_t argc
,
74 NPObjectAccessorWithIdentifier
accessor(object
, method_name
, false);
75 if (!accessor
.is_valid())
78 PPResultAndExceptionToNPResult
result_converter(
79 accessor
.object()->GetNPObject(), result
);
80 PPVarArrayFromNPVariantArray
args(accessor
.object()->instance(),
83 // For the OOP plugin case we need to grab a reference on the plugin module
84 // object to ensure that it is not destroyed courtsey an incoming
85 // ExecuteScript call which destroys the plugin module and in turn the
87 scoped_refptr
<webkit::ppapi::PluginModule
> ref(
88 accessor
.object()->instance()->module());
90 return result_converter
.SetResult(accessor
.object()->ppp_class()->Call(
91 accessor
.object()->ppp_class_data(), accessor
.identifier(),
92 argc
, args
.array(), result_converter
.exception()));
95 bool WrapperClass_InvokeDefault(NPObject
* np_object
, const NPVariant
* argv
,
96 uint32_t argc
, NPVariant
* result
) {
97 PluginObject
* obj
= PluginObject::FromNPObject(np_object
);
101 PPVarArrayFromNPVariantArray
args(obj
->instance(), argc
, argv
);
102 PPResultAndExceptionToNPResult
result_converter(obj
->GetNPObject(), result
);
104 // For the OOP plugin case we need to grab a reference on the plugin module
105 // object to ensure that it is not destroyed courtsey an incoming
106 // ExecuteScript call which destroys the plugin module and in turn the
108 scoped_refptr
<webkit::ppapi::PluginModule
> ref(
109 obj
->instance()->module());
111 result_converter
.SetResult(obj
->ppp_class()->Call(
112 obj
->ppp_class_data(), PP_MakeUndefined(), argc
, args
.array(),
113 result_converter
.exception()));
114 return result_converter
.success();
117 bool WrapperClass_HasProperty(NPObject
* object
, NPIdentifier property_name
) {
118 NPObjectAccessorWithIdentifier
accessor(object
, property_name
, true);
119 if (!accessor
.is_valid())
122 PPResultAndExceptionToNPResult
result_converter(
123 accessor
.object()->GetNPObject(), NULL
);
124 bool rv
= accessor
.object()->ppp_class()->HasProperty(
125 accessor
.object()->ppp_class_data(), accessor
.identifier(),
126 result_converter
.exception());
127 result_converter
.CheckExceptionForNoResult();
131 bool WrapperClass_GetProperty(NPObject
* object
, NPIdentifier property_name
,
133 NPObjectAccessorWithIdentifier
accessor(object
, property_name
, true);
134 if (!accessor
.is_valid())
137 PPResultAndExceptionToNPResult
result_converter(
138 accessor
.object()->GetNPObject(), result
);
139 return result_converter
.SetResult(accessor
.object()->ppp_class()->GetProperty(
140 accessor
.object()->ppp_class_data(), accessor
.identifier(),
141 result_converter
.exception()));
144 bool WrapperClass_SetProperty(NPObject
* object
, NPIdentifier property_name
,
145 const NPVariant
* value
) {
146 NPObjectAccessorWithIdentifier
accessor(object
, property_name
, true);
147 if (!accessor
.is_valid())
150 PPResultAndExceptionToNPResult
result_converter(
151 accessor
.object()->GetNPObject(), NULL
);
152 PP_Var value_var
= NPVariantToPPVar(accessor
.object()->instance(), value
);
153 accessor
.object()->ppp_class()->SetProperty(
154 accessor
.object()->ppp_class_data(), accessor
.identifier(), value_var
,
155 result_converter
.exception());
156 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(value_var
);
157 return result_converter
.CheckExceptionForNoResult();
160 bool WrapperClass_RemoveProperty(NPObject
* object
, NPIdentifier property_name
) {
161 NPObjectAccessorWithIdentifier
accessor(object
, property_name
, true);
162 if (!accessor
.is_valid())
165 PPResultAndExceptionToNPResult
result_converter(
166 accessor
.object()->GetNPObject(), NULL
);
167 accessor
.object()->ppp_class()->RemoveProperty(
168 accessor
.object()->ppp_class_data(), accessor
.identifier(),
169 result_converter
.exception());
170 return result_converter
.CheckExceptionForNoResult();
173 bool WrapperClass_Enumerate(NPObject
* object
, NPIdentifier
** values
,
177 PluginObject
* obj
= PluginObject::FromNPObject(object
);
181 uint32_t property_count
= 0;
182 PP_Var
* properties
= NULL
; // Must be freed!
183 PPResultAndExceptionToNPResult
result_converter(obj
->GetNPObject(), NULL
);
184 obj
->ppp_class()->GetAllPropertyNames(obj
->ppp_class_data(),
185 &property_count
, &properties
,
186 result_converter
.exception());
188 // Convert the array of PP_Var to an array of NPIdentifiers. If any
189 // conversions fail, we will set the exception.
190 if (!result_converter
.has_exception()) {
191 if (property_count
> 0) {
192 *values
= static_cast<NPIdentifier
*>(
193 malloc(sizeof(NPIdentifier
) * property_count
));
194 *count
= 0; // Will be the number of items successfully converted.
195 for (uint32_t i
= 0; i
< property_count
; ++i
) {
196 if (!((*values
)[i
] = PPVarToNPIdentifier(properties
[i
]))) {
197 // Throw an exception for the failed convertion.
198 *result_converter
.exception() =
199 StringVar::StringToPPVar(kInvalidValueException
);
205 if (result_converter
.has_exception()) {
206 // We don't actually have to free the identifiers we converted since
207 // all identifiers leak anyway :( .
215 // This will actually throw the exception, either from GetAllPropertyNames,
216 // or if anything was set during the conversion process.
217 result_converter
.CheckExceptionForNoResult();
219 // Release the PP_Var that the plugin allocated. On success, they will all
220 // be converted to NPVariants, and on failure, we want them to just go away.
221 ::ppapi::VarTracker
* var_tracker
= PpapiGlobals::Get()->GetVarTracker();
222 for (uint32_t i
= 0; i
< property_count
; ++i
)
223 var_tracker
->ReleaseVar(properties
[i
]);
225 return result_converter
.success();
228 bool WrapperClass_Construct(NPObject
* object
, const NPVariant
* argv
,
229 uint32_t argc
, NPVariant
* result
) {
230 PluginObject
* obj
= PluginObject::FromNPObject(object
);
234 PPVarArrayFromNPVariantArray
args(obj
->instance(), argc
, argv
);
235 PPResultAndExceptionToNPResult
result_converter(obj
->GetNPObject(), result
);
236 return result_converter
.SetResult(obj
->ppp_class()->Construct(
237 obj
->ppp_class_data(), argc
, args
.array(),
238 result_converter
.exception()));
241 const NPClass wrapper_class
= {
242 NP_CLASS_STRUCT_VERSION
,
243 WrapperClass_Allocate
,
244 WrapperClass_Deallocate
,
245 WrapperClass_Invalidate
,
246 WrapperClass_HasMethod
,
248 WrapperClass_InvokeDefault
,
249 WrapperClass_HasProperty
,
250 WrapperClass_GetProperty
,
251 WrapperClass_SetProperty
,
252 WrapperClass_RemoveProperty
,
253 WrapperClass_Enumerate
,
254 WrapperClass_Construct
259 // PluginObject ----------------------------------------------------------------
261 struct PluginObject::NPObjectWrapper
: public NPObject
{
262 // Points to the var object that owns this wrapper. This value may be NULL
263 // if there is no var owning this wrapper. This can happen if the plugin
264 // releases all references to the var, but a reference to the underlying
265 // NPObject is still held by script on the page.
269 PluginObject::PluginObject(PluginInstance
* instance
,
270 NPObjectWrapper
* object_wrapper
,
271 const PPP_Class_Deprecated
* ppp_class
,
272 void* ppp_class_data
)
273 : instance_(instance
),
274 object_wrapper_(object_wrapper
),
275 ppp_class_(ppp_class
),
276 ppp_class_data_(ppp_class_data
) {
277 // Make the object wrapper refer back to this class so our NPObject
278 // implementation can call back into the Pepper layer.
279 object_wrapper_
->obj
= this;
280 instance_
->AddPluginObject(this);
283 PluginObject::~PluginObject() {
284 // The wrapper we made for this NPObject may still have a reference to it
285 // from JavaScript, so we clear out its ObjectVar back pointer which will
286 // cause all calls "up" to the plugin to become NOPs. Our ObjectVar base
287 // class will release our reference to the object, which may or may not
288 // delete the NPObject.
289 DCHECK(object_wrapper_
->obj
== this);
290 object_wrapper_
->obj
= NULL
;
291 instance_
->RemovePluginObject(this);
294 PP_Var
PluginObject::Create(PluginInstance
* instance
,
295 const PPP_Class_Deprecated
* ppp_class
,
296 void* ppp_class_data
) {
297 // This will internally end up calling our AllocateObjectWrapper via the
298 // WrapperClass_Allocated function which will have created an object wrapper
299 // appropriate for this class (derived from NPObject).
300 NPObjectWrapper
* wrapper
= static_cast<NPObjectWrapper
*>(
301 WebBindings::createObject(NULL
, const_cast<NPClass
*>(&wrapper_class
)));
303 // This object will register itself both with the NPObject and with the
304 // PluginModule. The NPObject will normally handle its lifetime, and it
305 // will get deleted in the destroy method. It may also get deleted when the
306 // plugin module is deallocated.
307 new PluginObject(instance
, wrapper
, ppp_class
, ppp_class_data
);
309 // We can just use a normal ObjectVar to refer to this object from the
310 // plugin. It will hold a ref to the underlying NPObject which will in turn
311 // hold our pluginObject.
312 PP_Var
obj_var(NPObjectToPPVar(instance
, wrapper
));
314 // Note that the ObjectVar constructor incremented the reference count, and so
315 // did WebBindings::createObject above. Now that the PP_Var has taken
316 // ownership, we need to release to balance out the createObject reference
318 WebBindings::releaseObject(wrapper
);
322 NPObject
* PluginObject::GetNPObject() const {
323 return object_wrapper_
;
327 bool PluginObject::IsInstanceOf(NPObject
* np_object
,
328 const PPP_Class_Deprecated
* ppp_class
,
329 void** ppp_class_data
) {
330 // Validate that this object is implemented by our wrapper class before
331 // trying to get the PluginObject.
332 if (np_object
->_class
!= &wrapper_class
)
335 PluginObject
* plugin_object
= FromNPObject(np_object
);
337 return false; // Object is no longer alive.
339 if (plugin_object
->ppp_class() != ppp_class
)
342 *ppp_class_data
= plugin_object
->ppp_class_data();
347 PluginObject
* PluginObject::FromNPObject(NPObject
* object
) {
348 return static_cast<NPObjectWrapper
*>(object
)->obj
;
352 NPObject
* PluginObject::AllocateObjectWrapper() {
353 NPObjectWrapper
* wrapper
= new NPObjectWrapper
;
354 memset(wrapper
, 0, sizeof(NPObjectWrapper
));
359 } // namespace webkit