OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/pepper/plugin_object.h" | 5 #include "content/renderer/pepper/plugin_object.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "content/renderer/pepper/npapi_glue.h" | |
13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 14 #include "content/renderer/pepper/pepper_try_catch.h" |
14 #include "content/renderer/pepper/plugin_module.h" | 15 #include "content/renderer/pepper/plugin_module.h" |
| 16 #include "content/renderer/pepper/v8_var_converter.h" |
| 17 #include "gin/arguments.h" |
| 18 #include "gin/converter.h" |
| 19 #include "gin/function_template.h" |
| 20 #include "gin/handle.h" |
| 21 #include "gin/interceptor.h" |
| 22 #include "gin/object_template_builder.h" |
| 23 #include "gin/public/gin_embedders.h" |
15 #include "ppapi/c/dev/ppb_var_deprecated.h" | 24 #include "ppapi/c/dev/ppb_var_deprecated.h" |
16 #include "ppapi/c/dev/ppp_class_deprecated.h" | 25 #include "ppapi/c/dev/ppp_class_deprecated.h" |
17 #include "ppapi/c/pp_resource.h" | 26 #include "ppapi/c/pp_resource.h" |
18 #include "ppapi/c/pp_var.h" | 27 #include "ppapi/c/pp_var.h" |
19 #include "ppapi/shared_impl/ppapi_globals.h" | 28 #include "ppapi/shared_impl/ppapi_globals.h" |
20 #include "ppapi/shared_impl/resource_tracker.h" | 29 #include "ppapi/shared_impl/resource_tracker.h" |
21 #include "ppapi/shared_impl/var.h" | 30 #include "ppapi/shared_impl/var.h" |
22 #include "ppapi/shared_impl/var_tracker.h" | 31 #include "ppapi/shared_impl/var_tracker.h" |
23 #include "third_party/WebKit/public/web/WebBindings.h" | |
24 #include "third_party/npapi/bindings/npapi.h" | |
25 #include "third_party/npapi/bindings/npruntime.h" | |
26 | 32 |
27 using ppapi::PpapiGlobals; | 33 using ppapi::PpapiGlobals; |
| 34 using ppapi::ScopedPPVar; |
| 35 using ppapi::ScopedPPVarArray; |
28 using ppapi::StringVar; | 36 using ppapi::StringVar; |
29 using ppapi::Var; | 37 using ppapi::Var; |
30 using blink::WebBindings; | |
31 | 38 |
32 namespace content { | 39 namespace content { |
33 | 40 |
34 namespace { | 41 namespace { |
35 | 42 |
36 const char kInvalidValueException[] = "Error: Invalid value"; | 43 const char kInvalidValueException[] = "Error: Invalid value"; |
37 | 44 |
38 // NPObject implementation in terms of PPP_Class_Deprecated -------------------- | 45 } // namespace |
39 | 46 |
40 NPObject* WrapperClass_Allocate(NPP npp, NPClass* unused) { | 47 // PluginObject ---------------------------------------------------------------- |
41 return PluginObject::AllocateObjectWrapper(); | 48 |
42 } | 49 PluginObject::~PluginObject() { |
43 | 50 if (instance_) { |
44 void WrapperClass_Deallocate(NPObject* np_object) { | 51 ppp_class_->Deallocate(ppp_class_data_); |
45 PluginObject* plugin_object = PluginObject::FromNPObject(np_object); | 52 instance_->RemovePluginObject(this); |
46 if (plugin_object) { | 53 } |
47 plugin_object->ppp_class()->Deallocate(plugin_object->ppp_class_data()); | 54 } |
48 delete plugin_object; | 55 |
49 } | 56 // static |
50 delete np_object; | 57 gin::WrapperInfo PluginObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
51 } | 58 |
52 | 59 // static |
53 void WrapperClass_Invalidate(NPObject* object) {} | 60 PluginObject* PluginObject::FromV8Object(v8::Isolate* isolate, |
54 | 61 v8::Handle<v8::Object> v8_object) { |
55 bool WrapperClass_HasMethod(NPObject* object, NPIdentifier method_name) { | 62 PluginObject* plugin_object; |
56 NPObjectAccessorWithIdentifier accessor(object, method_name, false); | 63 if (!v8_object.IsEmpty() && |
57 if (!accessor.is_valid()) | 64 gin::ConvertFromV8(isolate, v8_object, &plugin_object)) { |
58 return false; | 65 return plugin_object; |
59 | 66 } |
60 PPResultAndExceptionToNPResult result_converter( | 67 return NULL; |
61 accessor.object()->GetNPObject(), NULL); | 68 } |
62 bool rv = accessor.object()->ppp_class()->HasMethod( | 69 |
63 accessor.object()->ppp_class_data(), | 70 // static |
64 accessor.identifier(), | 71 PP_Var PluginObject::Create(PepperPluginInstanceImpl* instance, |
65 result_converter.exception()); | 72 const PPP_Class_Deprecated* ppp_class, |
66 result_converter.CheckExceptionForNoResult(); | 73 void* ppp_class_data) { |
67 return rv; | 74 PepperTryCatchVar try_catch(instance, true, NULL); |
68 } | 75 gin::Handle<PluginObject> object = |
69 | 76 gin::CreateHandle(instance->GetIsolate(), |
70 bool WrapperClass_Invoke(NPObject* object, | 77 new PluginObject(instance, ppp_class, ppp_class_data)); |
71 NPIdentifier method_name, | 78 ScopedPPVar result = try_catch.FromV8(object.ToV8()); |
72 const NPVariant* argv, | 79 DCHECK(!try_catch.HasException()); |
73 uint32_t argc, | 80 return result.Release(); |
74 NPVariant* result) { | 81 } |
75 NPObjectAccessorWithIdentifier accessor(object, method_name, false); | 82 |
76 if (!accessor.is_valid()) | 83 v8::Local<v8::Value> PluginObject::GetNamedProperty( |
77 return false; | 84 v8::Isolate* isolate, |
78 | 85 const std::string& identifier) { |
79 PPResultAndExceptionToNPResult result_converter( | 86 ScopedPPVar identifier_var(ScopedPPVar::PassRef(), |
80 accessor.object()->GetNPObject(), result); | 87 StringVar::StringToPPVar(identifier)); |
81 PPVarArrayFromNPVariantArray args(accessor.object()->instance(), argc, argv); | 88 return GetPropertyOrMethod(instance_->GetIsolate(), identifier_var.get()); |
| 89 } |
| 90 |
| 91 void PluginObject::SetNamedProperty(v8::Isolate* isolate, |
| 92 const std::string& identifier, |
| 93 v8::Local<v8::Value> value) { |
| 94 ScopedPPVar identifier_var(ScopedPPVar::PassRef(), |
| 95 StringVar::StringToPPVar(identifier)); |
| 96 SetProperty(isolate, identifier_var.get(), value); |
| 97 } |
| 98 |
| 99 std::vector<std::string> PluginObject::EnumerateNamedProperties( |
| 100 v8::Isolate* isolate) { |
| 101 return EnumerateProperties(isolate).first; |
| 102 } |
| 103 |
| 104 v8::Local<v8::Value> PluginObject::GetIndexedProperty(v8::Isolate* isolate, |
| 105 uint32_t index) { |
| 106 return GetPropertyOrMethod(isolate, PP_MakeInt32(index)); |
| 107 } |
| 108 |
| 109 void PluginObject::SetIndexedProperty(v8::Isolate* isolate, |
| 110 uint32_t index, |
| 111 v8::Local<v8::Value> value) { |
| 112 SetProperty(isolate, PP_MakeInt32(index), value); |
| 113 } |
| 114 |
| 115 std::vector<uint32_t> PluginObject::EnumerateIndexedProperties( |
| 116 v8::Isolate* isolate) { |
| 117 return EnumerateProperties(isolate).second; |
| 118 } |
| 119 |
| 120 void PluginObject::InstanceDeleted() { |
| 121 instance_ = NULL; |
| 122 } |
| 123 |
| 124 PluginObject::PluginObject(PepperPluginInstanceImpl* instance, |
| 125 const PPP_Class_Deprecated* ppp_class, |
| 126 void* ppp_class_data) |
| 127 : gin::NamedPropertyInterceptor(instance->GetIsolate(), this), |
| 128 gin::IndexedPropertyInterceptor(instance->GetIsolate(), this), |
| 129 instance_(instance), |
| 130 ppp_class_(ppp_class), |
| 131 ppp_class_data_(ppp_class_data), |
| 132 weak_factory_(this) { |
| 133 DCHECK(instance_); |
| 134 instance_->AddPluginObject(this); |
| 135 } |
| 136 |
| 137 gin::ObjectTemplateBuilder PluginObject::GetObjectTemplateBuilder( |
| 138 v8::Isolate* isolate) { |
| 139 return Wrappable<PluginObject>::GetObjectTemplateBuilder(isolate) |
| 140 .AddNamedPropertyInterceptor() |
| 141 .AddIndexedPropertyInterceptor(); |
| 142 } |
| 143 |
| 144 v8::Local<v8::Value> PluginObject::GetPropertyOrMethod( |
| 145 v8::Isolate* isolate, |
| 146 PP_Var identifier_var) { |
| 147 if (!instance_) |
| 148 return v8::Local<v8::Value>(); |
| 149 |
| 150 PepperTryCatchV8 try_catch(instance_, true, isolate); |
| 151 bool has_property = |
| 152 ppp_class_->HasProperty(ppp_class_data_, identifier_var, |
| 153 try_catch.exception()); |
| 154 if (try_catch.ThrowException()) |
| 155 return v8::Local<v8::Value>(); |
| 156 |
| 157 if (has_property) { |
| 158 ScopedPPVar result_var(ScopedPPVar::PassRef(), |
| 159 ppp_class_->GetProperty(ppp_class_data_, identifier_var, |
| 160 try_catch.exception())); |
| 161 if (try_catch.ThrowException()) |
| 162 return v8::Local<v8::Value>(); |
| 163 |
| 164 v8::Handle<v8::Value> result = try_catch.ToV8(result_var.get()); |
| 165 if (try_catch.ThrowException()) |
| 166 return v8::Local<v8::Value>(); |
| 167 |
| 168 return result; |
| 169 } |
| 170 |
| 171 bool has_method = identifier_var.type == PP_VARTYPE_STRING && |
| 172 ppp_class_->HasMethod(ppp_class_data_, identifier_var, |
| 173 try_catch.exception()); |
| 174 if (try_catch.ThrowException()) |
| 175 return v8::Local<v8::Value>(); |
| 176 |
| 177 if (has_method) { |
| 178 const std::string& identifier = |
| 179 StringVar::FromPPVar(identifier_var)->value(); |
| 180 return gin::CreateFunctionTemplate(isolate, |
| 181 base::Bind(&PluginObject::Call, |
| 182 weak_factory_.GetWeakPtr(), |
| 183 identifier))->GetFunction(); |
| 184 } |
| 185 |
| 186 return v8::Local<v8::Value>(); |
| 187 } |
| 188 |
| 189 void PluginObject::SetProperty(v8::Isolate* isolate, |
| 190 PP_Var identifier_var, |
| 191 v8::Handle<v8::Value> value) { |
| 192 if (!instance_) |
| 193 return; |
| 194 |
| 195 PepperTryCatchV8 try_catch(instance_, true, isolate); |
| 196 if (value->IsUndefined()) { |
| 197 ppp_class_->RemoveProperty(ppp_class_data_, identifier_var, |
| 198 try_catch.exception()); |
| 199 try_catch.ThrowException(); |
| 200 } else { |
| 201 ScopedPPVar var = try_catch.FromV8(value); |
| 202 if (try_catch.ThrowException()) |
| 203 return; |
| 204 |
| 205 ppp_class_->SetProperty(ppp_class_data_, identifier_var, var.get(), |
| 206 try_catch.exception()); |
| 207 try_catch.ThrowException(); |
| 208 } |
| 209 } |
| 210 |
| 211 std::pair<std::vector<std::string>, std::vector<uint32_t> > |
| 212 PluginObject::EnumerateProperties(v8::Isolate* isolate) { |
| 213 std::pair<std::vector<std::string>, std::vector<uint32_t> > all_properties; |
| 214 if (!instance_) |
| 215 return all_properties; |
| 216 |
| 217 PepperTryCatchV8 try_catch(instance_, true, isolate); |
| 218 |
| 219 PP_Var* name_vars; |
| 220 uint32_t count; |
| 221 ppp_class_->GetAllPropertyNames(ppp_class_data_, &count, &name_vars, |
| 222 try_catch.exception()); |
| 223 ScopedPPVarArray scoped_name_vars( |
| 224 ScopedPPVarArray::PassPPBMemoryAllocatedRef(), name_vars, count); |
| 225 |
| 226 if (try_catch.ThrowException()) |
| 227 return all_properties; |
| 228 |
| 229 for (uint32_t i = 0; i < count; ++i) { |
| 230 StringVar* string_var = StringVar::FromPPVar(name_vars[i]); |
| 231 if (string_var) { |
| 232 all_properties.first.push_back(string_var->value()); |
| 233 } else if (name_vars[i].type == PP_VARTYPE_INT32) { |
| 234 all_properties.second.push_back(name_vars[i].value.as_int); |
| 235 } else { |
| 236 try_catch.ThrowException(kInvalidValueException); |
| 237 all_properties.first.clear(); |
| 238 all_properties.second.clear(); |
| 239 return all_properties; |
| 240 } |
| 241 } |
| 242 |
| 243 return all_properties; |
| 244 } |
| 245 |
| 246 void PluginObject::Call(const std::string& identifier, |
| 247 gin::Arguments* args) { |
| 248 if (!instance_) |
| 249 return; |
| 250 |
| 251 PepperTryCatchV8 try_catch(instance_, true, args->isolate()); |
| 252 ScopedPPVar identifier_var(ScopedPPVar::PassRef(), |
| 253 StringVar::StringToPPVar(identifier)); |
| 254 ScopedPPVarArray argument_vars(args->Length()); |
| 255 |
| 256 for (uint32_t i = 0; i < argument_vars.size(); ++i) { |
| 257 v8::Handle<v8::Value> arg; |
| 258 if (!args->GetNext(&arg)) { |
| 259 NOTREACHED(); |
| 260 } |
| 261 |
| 262 argument_vars.Set(i, try_catch.FromV8(arg).Release()); |
| 263 if (try_catch.ThrowException()) |
| 264 return; |
| 265 } |
82 | 266 |
83 // For the OOP plugin case we need to grab a reference on the plugin module | 267 // 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 | 268 // object to ensure that it is not destroyed courtsey an incoming |
85 // ExecuteScript call which destroys the plugin module and in turn the | 269 // ExecuteScript call which destroys the plugin module and in turn the |
86 // dispatcher. | 270 // dispatcher. |
87 scoped_refptr<PluginModule> ref(accessor.object()->instance()->module()); | 271 scoped_refptr<PluginModule> ref(instance_->module()); |
88 | 272 |
89 return result_converter.SetResult( | 273 ScopedPPVar result_var(ScopedPPVar::PassRef(), |
90 accessor.object()->ppp_class()->Call(accessor.object()->ppp_class_data(), | 274 ppp_class_->Call(ppp_class_data_, identifier_var.get(), |
91 accessor.identifier(), | 275 argument_vars.size(), argument_vars.get(), |
92 argc, | 276 try_catch.exception())); |
93 args.array(), | 277 if (try_catch.ThrowException()) |
94 result_converter.exception())); | 278 return; |
95 } | 279 |
96 | 280 v8::Handle<v8::Value> result = try_catch.ToV8(result_var.get()); |
97 bool WrapperClass_InvokeDefault(NPObject* np_object, | 281 if (try_catch.ThrowException()) |
98 const NPVariant* argv, | 282 return; |
99 uint32_t argc, | 283 |
100 NPVariant* result) { | 284 args->Return(result); |
101 PluginObject* obj = PluginObject::FromNPObject(np_object); | |
102 if (!obj) | |
103 return false; | |
104 | |
105 PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); | |
106 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); | |
107 | |
108 // For the OOP plugin case we need to grab a reference on the plugin module | |
109 // object to ensure that it is not destroyed courtsey an incoming | |
110 // ExecuteScript call which destroys the plugin module and in turn the | |
111 // dispatcher. | |
112 scoped_refptr<PluginModule> ref(obj->instance()->module()); | |
113 | |
114 result_converter.SetResult( | |
115 obj->ppp_class()->Call(obj->ppp_class_data(), | |
116 PP_MakeUndefined(), | |
117 argc, | |
118 args.array(), | |
119 result_converter.exception())); | |
120 return result_converter.success(); | |
121 } | |
122 | |
123 bool WrapperClass_HasProperty(NPObject* object, NPIdentifier property_name) { | |
124 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
125 if (!accessor.is_valid()) | |
126 return false; | |
127 | |
128 PPResultAndExceptionToNPResult result_converter( | |
129 accessor.object()->GetNPObject(), NULL); | |
130 bool rv = accessor.object()->ppp_class()->HasProperty( | |
131 accessor.object()->ppp_class_data(), | |
132 accessor.identifier(), | |
133 result_converter.exception()); | |
134 result_converter.CheckExceptionForNoResult(); | |
135 return rv; | |
136 } | |
137 | |
138 bool WrapperClass_GetProperty(NPObject* object, | |
139 NPIdentifier property_name, | |
140 NPVariant* result) { | |
141 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
142 if (!accessor.is_valid()) | |
143 return false; | |
144 | |
145 PPResultAndExceptionToNPResult result_converter( | |
146 accessor.object()->GetNPObject(), result); | |
147 return result_converter.SetResult(accessor.object()->ppp_class()->GetProperty( | |
148 accessor.object()->ppp_class_data(), | |
149 accessor.identifier(), | |
150 result_converter.exception())); | |
151 } | |
152 | |
153 bool WrapperClass_SetProperty(NPObject* object, | |
154 NPIdentifier property_name, | |
155 const NPVariant* value) { | |
156 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
157 if (!accessor.is_valid()) | |
158 return false; | |
159 | |
160 PPResultAndExceptionToNPResult result_converter( | |
161 accessor.object()->GetNPObject(), NULL); | |
162 PP_Var value_var = NPVariantToPPVar(accessor.object()->instance(), value); | |
163 accessor.object()->ppp_class()->SetProperty( | |
164 accessor.object()->ppp_class_data(), | |
165 accessor.identifier(), | |
166 value_var, | |
167 result_converter.exception()); | |
168 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(value_var); | |
169 return result_converter.CheckExceptionForNoResult(); | |
170 } | |
171 | |
172 bool WrapperClass_RemoveProperty(NPObject* object, NPIdentifier property_name) { | |
173 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
174 if (!accessor.is_valid()) | |
175 return false; | |
176 | |
177 PPResultAndExceptionToNPResult result_converter( | |
178 accessor.object()->GetNPObject(), NULL); | |
179 accessor.object()->ppp_class()->RemoveProperty( | |
180 accessor.object()->ppp_class_data(), | |
181 accessor.identifier(), | |
182 result_converter.exception()); | |
183 return result_converter.CheckExceptionForNoResult(); | |
184 } | |
185 | |
186 bool WrapperClass_Enumerate(NPObject* object, | |
187 NPIdentifier** values, | |
188 uint32_t* count) { | |
189 *values = NULL; | |
190 *count = 0; | |
191 PluginObject* obj = PluginObject::FromNPObject(object); | |
192 if (!obj) | |
193 return false; | |
194 | |
195 uint32_t property_count = 0; | |
196 PP_Var* properties = NULL; // Must be freed! | |
197 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), NULL); | |
198 obj->ppp_class()->GetAllPropertyNames(obj->ppp_class_data(), | |
199 &property_count, | |
200 &properties, | |
201 result_converter.exception()); | |
202 | |
203 // Convert the array of PP_Var to an array of NPIdentifiers. If any | |
204 // conversions fail, we will set the exception. | |
205 if (!result_converter.has_exception()) { | |
206 if (property_count > 0) { | |
207 *values = static_cast<NPIdentifier*>( | |
208 calloc(property_count, sizeof(NPIdentifier))); | |
209 *count = 0; // Will be the number of items successfully converted. | |
210 for (uint32_t i = 0; i < property_count; ++i) { | |
211 (*values)[i] = PPVarToNPIdentifier(properties[i]); | |
212 if (!(*values)[i]) { | |
213 // Throw an exception for the failed convertion. | |
214 *result_converter.exception() = | |
215 StringVar::StringToPPVar(kInvalidValueException); | |
216 break; | |
217 } | |
218 (*count)++; | |
219 } | |
220 | |
221 if (result_converter.has_exception()) { | |
222 // We don't actually have to free the identifiers we converted since | |
223 // all identifiers leak anyway :( . | |
224 free(*values); | |
225 *values = NULL; | |
226 *count = 0; | |
227 } | |
228 } | |
229 } | |
230 | |
231 // This will actually throw the exception, either from GetAllPropertyNames, | |
232 // or if anything was set during the conversion process. | |
233 result_converter.CheckExceptionForNoResult(); | |
234 | |
235 // Release the PP_Var that the plugin allocated. On success, they will all | |
236 // be converted to NPVariants, and on failure, we want them to just go away. | |
237 ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | |
238 for (uint32_t i = 0; i < property_count; ++i) | |
239 var_tracker->ReleaseVar(properties[i]); | |
240 free(properties); | |
241 return result_converter.success(); | |
242 } | |
243 | |
244 bool WrapperClass_Construct(NPObject* object, | |
245 const NPVariant* argv, | |
246 uint32_t argc, | |
247 NPVariant* result) { | |
248 PluginObject* obj = PluginObject::FromNPObject(object); | |
249 if (!obj) | |
250 return false; | |
251 | |
252 PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); | |
253 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); | |
254 return result_converter.SetResult(obj->ppp_class()->Construct( | |
255 obj->ppp_class_data(), argc, args.array(), result_converter.exception())); | |
256 } | |
257 | |
258 const NPClass wrapper_class = { | |
259 NP_CLASS_STRUCT_VERSION, WrapperClass_Allocate, | |
260 WrapperClass_Deallocate, WrapperClass_Invalidate, | |
261 WrapperClass_HasMethod, WrapperClass_Invoke, | |
262 WrapperClass_InvokeDefault, WrapperClass_HasProperty, | |
263 WrapperClass_GetProperty, WrapperClass_SetProperty, | |
264 WrapperClass_RemoveProperty, WrapperClass_Enumerate, | |
265 WrapperClass_Construct}; | |
266 | |
267 } // namespace | |
268 | |
269 // PluginObject ---------------------------------------------------------------- | |
270 | |
271 struct PluginObject::NPObjectWrapper : public NPObject { | |
272 // Points to the var object that owns this wrapper. This value may be NULL | |
273 // if there is no var owning this wrapper. This can happen if the plugin | |
274 // releases all references to the var, but a reference to the underlying | |
275 // NPObject is still held by script on the page. | |
276 PluginObject* obj; | |
277 }; | |
278 | |
279 PluginObject::PluginObject(PepperPluginInstanceImpl* instance, | |
280 NPObjectWrapper* object_wrapper, | |
281 const PPP_Class_Deprecated* ppp_class, | |
282 void* ppp_class_data) | |
283 : instance_(instance), | |
284 object_wrapper_(object_wrapper), | |
285 ppp_class_(ppp_class), | |
286 ppp_class_data_(ppp_class_data) { | |
287 // Make the object wrapper refer back to this class so our NPObject | |
288 // implementation can call back into the Pepper layer. | |
289 object_wrapper_->obj = this; | |
290 instance_->AddPluginObject(this); | |
291 } | |
292 | |
293 PluginObject::~PluginObject() { | |
294 // The wrapper we made for this NPObject may still have a reference to it | |
295 // from JavaScript, so we clear out its ObjectVar back pointer which will | |
296 // cause all calls "up" to the plugin to become NOPs. Our ObjectVar base | |
297 // class will release our reference to the object, which may or may not | |
298 // delete the NPObject. | |
299 DCHECK(object_wrapper_->obj == this); | |
300 object_wrapper_->obj = NULL; | |
301 instance_->RemovePluginObject(this); | |
302 } | |
303 | |
304 PP_Var PluginObject::Create(PepperPluginInstanceImpl* instance, | |
305 const PPP_Class_Deprecated* ppp_class, | |
306 void* ppp_class_data) { | |
307 // This will internally end up calling our AllocateObjectWrapper via the | |
308 // WrapperClass_Allocated function which will have created an object wrapper | |
309 // appropriate for this class (derived from NPObject). | |
310 NPObjectWrapper* wrapper = | |
311 static_cast<NPObjectWrapper*>(WebBindings::createObject( | |
312 instance->instanceNPP(), const_cast<NPClass*>(&wrapper_class))); | |
313 | |
314 // This object will register itself both with the NPObject and with the | |
315 // PluginModule. The NPObject will normally handle its lifetime, and it | |
316 // will get deleted in the destroy method. It may also get deleted when the | |
317 // plugin module is deallocated. | |
318 new PluginObject(instance, wrapper, ppp_class, ppp_class_data); | |
319 | |
320 // We can just use a normal ObjectVar to refer to this object from the | |
321 // plugin. It will hold a ref to the underlying NPObject which will in turn | |
322 // hold our pluginObject. | |
323 PP_Var obj_var(NPObjectToPPVar(instance, wrapper)); | |
324 | |
325 // Note that the ObjectVar constructor incremented the reference count, and so | |
326 // did WebBindings::createObject above. Now that the PP_Var has taken | |
327 // ownership, we need to release to balance out the createObject reference | |
328 // count bump. | |
329 WebBindings::releaseObject(wrapper); | |
330 return obj_var; | |
331 } | |
332 | |
333 NPObject* PluginObject::GetNPObject() const { return object_wrapper_; } | |
334 | |
335 // static | |
336 bool PluginObject::IsInstanceOf(NPObject* np_object, | |
337 const PPP_Class_Deprecated* ppp_class, | |
338 void** ppp_class_data) { | |
339 // Validate that this object is implemented by our wrapper class before | |
340 // trying to get the PluginObject. | |
341 if (np_object->_class != &wrapper_class) | |
342 return false; | |
343 | |
344 PluginObject* plugin_object = FromNPObject(np_object); | |
345 if (!plugin_object) | |
346 return false; // Object is no longer alive. | |
347 | |
348 if (plugin_object->ppp_class() != ppp_class) | |
349 return false; | |
350 if (ppp_class_data) | |
351 *ppp_class_data = plugin_object->ppp_class_data(); | |
352 return true; | |
353 } | |
354 | |
355 // static | |
356 PluginObject* PluginObject::FromNPObject(NPObject* object) { | |
357 return static_cast<NPObjectWrapper*>(object)->obj; | |
358 } | |
359 | |
360 // static | |
361 NPObject* PluginObject::AllocateObjectWrapper() { | |
362 NPObjectWrapper* wrapper = new NPObjectWrapper; | |
363 memset(wrapper, 0, sizeof(NPObjectWrapper)); | |
364 return wrapper; | |
365 } | 285 } |
366 | 286 |
367 } // namespace content | 287 } // namespace content |
OLD | NEW |