blob: 424c0bbaff95293cbe02ee383b2df8efb8fe30ed [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2* Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7*
8* * Redistributions of source code must retain the above copyright
9* notice, this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above
11* copyright notice, this list of conditions and the following disclaimer
12* in the documentation and/or other materials provided with the
13* distribution.
14* * Neither the name of Google Inc. nor the names of its
15* contributors may be used to endorse or promote products derived from
16* this software without specific prior written permission.
17*
18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*/
30
31#include "config.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010032
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033#include "bindings/v8/V8NPObject.h"
34
35#include "V8HTMLAppletElement.h"
36#include "V8HTMLEmbedElement.h"
37#include "V8HTMLObjectElement.h"
38#include "bindings/v8/NPV8Object.h"
39#include "bindings/v8/V8Binding.h"
40#include "bindings/v8/V8NPUtils.h"
41#include "bindings/v8/V8ObjectConstructor.h"
Ben Murdoch6f543c72014-04-16 11:17:22 +010042#include "bindings/v8/V8PersistentValueMap.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010043#include "bindings/v8/npruntime_impl.h"
44#include "bindings/v8/npruntime_priv.h"
45#include "core/html/HTMLPlugInElement.h"
Ben Murdoch6f543c72014-04-16 11:17:22 +010046#include "v8-util.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010047#include "wtf/OwnPtr.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010048
49namespace WebCore {
50
51enum InvokeFunctionType {
52 InvokeMethod = 1,
53 InvokeConstruct = 2,
54 InvokeDefault = 3
55};
56
57struct IdentifierRep {
58 int number() const { return m_isString ? 0 : m_value.m_number; }
59 const char* string() const { return m_isString ? m_value.m_string : 0; }
60
61 union {
62 const char* m_string;
63 int m_number;
64 } m_value;
65 bool m_isString;
66};
67
68// FIXME: need comments.
69// Params: holder could be HTMLEmbedElement or NPObject
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000070static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& info, InvokeFunctionType functionId)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071{
72 NPObject* npObject;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000073 v8::Isolate* isolate = info.GetIsolate();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010074
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010075 // These three types are subtypes of HTMLPlugInElement.
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000076 HTMLPlugInElement* element = V8HTMLAppletElement::toNativeWithTypeCheck(isolate, info.Holder());
77 if (!element) {
78 element = V8HTMLEmbedElement::toNativeWithTypeCheck(isolate, info.Holder());
79 if (!element) {
80 element = V8HTMLObjectElement::toNativeWithTypeCheck(isolate, info.Holder());
81 }
82 }
83 if (element) {
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010084 if (RefPtr<SharedPersistent<v8::Object> > wrapper = element->pluginWrapper()) {
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010085 v8::HandleScope handleScope(isolate);
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010086 npObject = v8ObjectToNPObject(wrapper->newLocal(isolate));
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010087 } else
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010088 npObject = 0;
89 } else {
90 // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three
91 // internal fields.
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000092 if (info.Holder()->InternalFieldCount() != npObjectInternalFieldCount) {
93 throwError(v8ReferenceError, "NPMethod called on non-NPObject", info.GetIsolate());
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010094 return;
95 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010096
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000097 npObject = v8ObjectToNPObject(info.Holder());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010098 }
99
100 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100101 if (!npObject || !_NPN_IsAlive(npObject)) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000102 throwError(v8ReferenceError, "NPObject deleted", isolate);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100103 return;
104 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100105
106 // Wrap up parameters.
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000107 int numArgs = info.Length();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100108 OwnPtr<NPVariant[]> npArgs = adoptArrayPtr(new NPVariant[numArgs]);
Ben Murdoche69819b2013-07-17 14:56:49 +0100109
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100110 for (int i = 0; i < numArgs; i++)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000111 convertV8ObjectToNPVariant(info[i], npObject, &npArgs[i], isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100112
113 NPVariant result;
114 VOID_TO_NPVARIANT(result);
115
116 bool retval = true;
117 switch (functionId) {
118 case InvokeMethod:
119 if (npObject->_class->invoke) {
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000120 v8::Handle<v8::String> functionName = v8::Handle<v8::String>::Cast(info.Data());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100121 NPIdentifier identifier = getStringIdentifier(functionName);
122 retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
123 }
124 break;
125 case InvokeConstruct:
126 if (npObject->_class->construct)
127 retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
128 break;
129 case InvokeDefault:
130 if (npObject->_class->invokeDefault)
131 retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
132 break;
133 default:
134 break;
135 }
136
137 if (!retval)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000138 throwError(v8GeneralError, "Error calling method on NPObject.", isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100139
140 for (int i = 0; i < numArgs; i++)
141 _NPN_ReleaseVariantValue(&npArgs[i]);
142
143 // Unwrap return values.
144 v8::Handle<v8::Value> returnValue;
145 if (_NPN_IsAlive(npObject))
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000146 returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100147 _NPN_ReleaseVariantValue(&result);
148
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000149 v8SetReturnValue(info, returnValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100150}
151
152
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000153void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100154{
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000155 return npObjectInvokeImpl(info, InvokeMethod);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100156}
157
158
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000159void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100160{
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000161 if (info.IsConstructCall()) {
162 npObjectInvokeImpl(info, InvokeConstruct);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100163 return;
164 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100165
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000166 npObjectInvokeImpl(info, InvokeDefault);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100167}
168
Ben Murdoch6f543c72014-04-16 11:17:22 +0100169class V8TemplateMapTraits : public V8PersistentValueMapTraits<PrivateIdentifier*, v8::FunctionTemplate, true> {
170public:
171 typedef v8::PersistentValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8TemplateMapTraits> MapType;
172 typedef PrivateIdentifier WeakCallbackDataType;
173
174 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, PrivateIdentifier* key, const v8::Local<v8::FunctionTemplate>& value)
175 {
176 return key;
177 }
178
179 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
180
181 static MapType* MapFromWeakCallbackData(
182 const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>&);
183
184 static PrivateIdentifier* KeyFromWeakCallbackData(
185 const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>& data)
186 {
187 return data.GetParameter();
188 }
189
190 // Dispose traits:
191 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::FunctionTemplate> value, PrivateIdentifier* key) { }
192};
193
194
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100195class V8NPTemplateMap {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100196public:
197 // NPIdentifier is PrivateIdentifier*.
Ben Murdoch6f543c72014-04-16 11:17:22 +0100198 typedef v8::PersistentValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8TemplateMapTraits> MapType;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100199
Ben Murdoch6f543c72014-04-16 11:17:22 +0100200 v8::Local<v8::FunctionTemplate> get(PrivateIdentifier* key)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100201 {
Ben Murdoch6f543c72014-04-16 11:17:22 +0100202 return m_map.Get(key);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100203 }
204
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100205 void set(PrivateIdentifier* key, v8::Handle<v8::FunctionTemplate> handle)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100206 {
Ben Murdoch6f543c72014-04-16 11:17:22 +0100207 ASSERT(!m_map.Contains(key));
208 m_map.Set(key, handle);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100209 }
210
211 static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate)
212 {
213 DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate));
Ben Murdoch6f543c72014-04-16 11:17:22 +0100214 ASSERT(isolate == map.m_map.GetIsolate());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100215 return map;
216 }
217
Ben Murdoch6f543c72014-04-16 11:17:22 +0100218 friend class V8TemplateMapTraits;
219
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100220private:
221 explicit V8NPTemplateMap(v8::Isolate* isolate)
Ben Murdoch6f543c72014-04-16 11:17:22 +0100222 : m_map(isolate)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100223 {
224 }
225
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100226 MapType m_map;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100227};
228
Ben Murdoch6f543c72014-04-16 11:17:22 +0100229V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackData(const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>& data)
230{
231 return &V8NPTemplateMap::sharedInstance(data.GetIsolate()).m_map;
232}
233
234
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100235static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key, v8::Isolate* isolate)
236{
237 NPObject* npObject = v8ObjectToNPObject(self);
238
239 // Verify that our wrapper wasn't using a NPObject which
240 // has already been deleted.
241 if (!npObject || !_NPN_IsAlive(npObject))
242 return throwError(v8ReferenceError, "NPObject deleted", isolate);
243
Ben Murdoche69819b2013-07-17 14:56:49 +0100244
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100245 if (npObject->_class->hasProperty && npObject->_class->getProperty && npObject->_class->hasProperty(npObject, identifier)) {
246 if (!_NPN_IsAlive(npObject))
247 return throwError(v8ReferenceError, "NPObject deleted", isolate);
248
249 NPVariant result;
250 VOID_TO_NPVARIANT(result);
251 if (!npObject->_class->getProperty(npObject, identifier, &result))
252 return v8Undefined();
253
254 v8::Handle<v8::Value> returnValue;
255 if (_NPN_IsAlive(npObject))
Ben Murdoche69819b2013-07-17 14:56:49 +0100256 returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100257 _NPN_ReleaseVariantValue(&result);
258 return returnValue;
259
260 }
261
262 if (!_NPN_IsAlive(npObject))
263 return throwError(v8ReferenceError, "NPObject deleted", isolate);
264
265 if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
266 if (!_NPN_IsAlive(npObject))
267 return throwError(v8ReferenceError, "NPObject deleted", isolate);
268
269 PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
Ben Murdoch6f543c72014-04-16 11:17:22 +0100270 v8::Local<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::sharedInstance(isolate).get(id);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100271 // Cache templates using identifier as the key.
Ben Murdoch6f543c72014-04-16 11:17:22 +0100272 if (functionTemplate.IsEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100273 // Create a new template.
Ben Murdoch6f543c72014-04-16 11:17:22 +0100274 functionTemplate = v8::FunctionTemplate::New(isolate);
275 functionTemplate->SetCallHandler(npObjectMethodHandler, key);
276 V8NPTemplateMap::sharedInstance(isolate).set(id, functionTemplate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100277 }
Ben Murdoch6f543c72014-04-16 11:17:22 +0100278 v8::Local<v8::Function> v8Function = functionTemplate->GetFunction();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100279 v8Function->SetName(v8::Handle<v8::String>::Cast(key));
280 return v8Function;
281 }
282
283 return v8Undefined();
284}
285
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100286void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100287{
288 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100289 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100290}
291
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100292void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100293{
294 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100295 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100296}
297
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100298void npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100299{
300 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100301 v8SetReturnValue(info, npObjectGetProperty(self, identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100302}
303
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100304void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100305{
306 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100307 v8SetReturnValue(info, npObjectGetProperty(self, identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100308}
309
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100310void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Integer>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100311{
312 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100313 if (npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()).IsEmpty())
314 return;
315 v8SetReturnValueInt(info, 0);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100316}
317
318static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate)
319{
320 NPObject* npObject = v8ObjectToNPObject(self);
321
322 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
323 if (!npObject || !_NPN_IsAlive(npObject)) {
324 throwError(v8ReferenceError, "NPObject deleted", isolate);
325 return value; // Intercepted, but an exception was thrown.
326 }
327
328 if (npObject->_class->hasProperty && npObject->_class->setProperty && npObject->_class->hasProperty(npObject, identifier)) {
329 if (!_NPN_IsAlive(npObject))
330 return throwError(v8ReferenceError, "NPObject deleted", isolate);
331
332 NPVariant npValue;
333 VOID_TO_NPVARIANT(npValue);
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100334 convertV8ObjectToNPVariant(value, npObject, &npValue, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100335 bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
336 _NPN_ReleaseVariantValue(&npValue);
337 if (success)
338 return value; // Intercept the call.
339 }
340 return v8Undefined();
341}
342
343
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100344void npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100345{
346 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100347 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100348}
349
350
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100351void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100352{
353 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100354 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100355}
356
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100357void npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100358{
359 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100360 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100361}
362
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100363void npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100364{
365 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100366 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100367}
368
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100369void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info, bool namedProperty)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100370{
371 NPObject* npObject = v8ObjectToNPObject(info.Holder());
372
373 // Verify that our wrapper wasn't using a NPObject which
374 // has already been deleted.
375 if (!npObject || !_NPN_IsAlive(npObject))
376 throwError(v8ReferenceError, "NPObject deleted", info.GetIsolate());
377
378 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) {
379 uint32_t count;
380 NPIdentifier* identifiers;
381 if (npObject->_class->enumerate(npObject, &identifiers, &count)) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000382 uint32_t propertiesCount = 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100383 for (uint32_t i = 0; i < count; ++i) {
384 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000385 if (namedProperty == identifier->m_isString)
386 ++propertiesCount;
387 }
388 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertiesCount);
389 for (uint32_t i = 0, propertyIndex = 0; i < count; ++i) {
390 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
391 if (namedProperty == identifier->m_isString) {
392 ASSERT(propertyIndex < propertiesCount);
393 if (namedProperty)
394 properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8AtomicString(info.GetIsolate(), identifier->string()));
395 else
396 properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8::Integer::New(info.GetIsolate(), identifier->number()));
397 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100398 }
399
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100400 v8SetReturnValue(info, properties);
401 return;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100402 }
403 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100404}
405
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100406void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100407{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100408 npObjectPropertyEnumerator(info, true);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100409}
410
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100411void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100412{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100413 npObjectPropertyEnumerator(info, false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100414}
415
416static DOMWrapperMap<NPObject>& staticNPObjectMap()
417{
418 DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetCurrent()));
419 return npObjectMap;
420}
421
Ben Murdocha9984bf2014-04-10 11:22:39 +0100422template <>
423inline void DOMWrapperMap<NPObject>::PersistentValueMapTraits::Dispose(
424 v8::Isolate* isolate,
425 v8::UniquePersistent<v8::Object> value,
426 NPObject* npObject)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100427{
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100428 ASSERT(npObject);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100429 if (_NPN_IsAlive(npObject))
430 _NPN_ReleaseObject(npObject);
431}
432
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100433v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root, v8::Isolate* isolate)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100434{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100435 static v8::Eternal<v8::FunctionTemplate> npObjectDesc;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100436
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000437 ASSERT(isolate->InContext());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100438
439 // If this is a v8 object, just return it.
Ben Murdoch591b9582013-07-10 11:41:44 +0100440 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
441 if (v8NPObject)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100442 return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100443
444 // If we've already wrapped this object, just return it.
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100445 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100446 if (!wrapper.IsEmpty())
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100447 return wrapper;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100448
449 // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
450 // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
451 // can be used by DOM bindings.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100452 if (npObjectDesc.IsEmpty()) {
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000453 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100454 templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
455 templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator);
456 templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
457 templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100458 npObjectDesc.Set(isolate, templ);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100459 }
460
461 // FIXME: Move staticNPObjectMap() to DOMDataStore.
462 // Use V8DOMWrapper::createWrapper() and
463 // V8DOMWrapper::associateObjectWithWrapper()
464 // to create a wrapper object.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100465 v8::Handle<v8::Function> v8Function = npObjectDesc.Get(isolate)->GetFunction();
Ben Murdocha9984bf2014-04-10 11:22:39 +0100466 v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(isolate, v8Function);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100467 if (value.IsEmpty())
468 return value;
469
470 V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object);
471
472 // KJS retains the object as part of its wrapper (see Bindings::CInstance).
473 _NPN_RetainObject(object);
Ben Murdoche69819b2013-07-17 14:56:49 +0100474 _NPN_RegisterObject(object, root);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100475
476 WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent);
477 staticNPObjectMap().set(object, value, configuration);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000478 ASSERT(V8DOMWrapper::isDOMWrapper(value));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100479 return value;
480}
481
482void forgetV8ObjectForNPObject(NPObject* object)
483{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100484 v8::Isolate* isolate = v8::Isolate::GetCurrent();
485 v8::HandleScope scope(isolate);
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100486 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100487 if (!wrapper.IsEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100488 V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100489 staticNPObjectMap().removeAndDispose(object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100490 _NPN_ReleaseObject(object);
491 }
492}
493
494} // namespace WebCore