blob: a3f8ab7566c8219ff8ac88292599c05ee6b53164 [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"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010039#include "bindings/v8/UnsafePersistent.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040#include "bindings/v8/V8Binding.h"
41#include "bindings/v8/V8NPUtils.h"
42#include "bindings/v8/V8ObjectConstructor.h"
43#include "bindings/v8/npruntime_impl.h"
44#include "bindings/v8/npruntime_priv.h"
45#include "core/html/HTMLPlugInElement.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010046#include "wtf/OwnPtr.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047
48namespace WebCore {
49
50enum InvokeFunctionType {
51 InvokeMethod = 1,
52 InvokeConstruct = 2,
53 InvokeDefault = 3
54};
55
56struct IdentifierRep {
57 int number() const { return m_isString ? 0 : m_value.m_number; }
58 const char* string() const { return m_isString ? m_value.m_string : 0; }
59
60 union {
61 const char* m_string;
62 int m_number;
63 } m_value;
64 bool m_isString;
65};
66
67// FIXME: need comments.
68// Params: holder could be HTMLEmbedElement or NPObject
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000069static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& info, InvokeFunctionType functionId)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010070{
71 NPObject* npObject;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000072 v8::Isolate* isolate = info.GetIsolate();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010073
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010074 // These three types are subtypes of HTMLPlugInElement.
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000075 HTMLPlugInElement* element = V8HTMLAppletElement::toNativeWithTypeCheck(isolate, info.Holder());
76 if (!element) {
77 element = V8HTMLEmbedElement::toNativeWithTypeCheck(isolate, info.Holder());
78 if (!element) {
79 element = V8HTMLObjectElement::toNativeWithTypeCheck(isolate, info.Holder());
80 }
81 }
82 if (element) {
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010083 if (RefPtr<SharedPersistent<v8::Object> > wrapper = element->pluginWrapper()) {
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010084 v8::HandleScope handleScope(isolate);
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010085 npObject = v8ObjectToNPObject(wrapper->newLocal(isolate));
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010086 } else
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010087 npObject = 0;
88 } else {
89 // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three
90 // internal fields.
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000091 if (info.Holder()->InternalFieldCount() != npObjectInternalFieldCount) {
92 throwError(v8ReferenceError, "NPMethod called on non-NPObject", info.GetIsolate());
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010093 return;
94 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010095
Torne (Richard Coles)19cde672013-11-06 12:28:04 +000096 npObject = v8ObjectToNPObject(info.Holder());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010097 }
98
99 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100100 if (!npObject || !_NPN_IsAlive(npObject)) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000101 throwError(v8ReferenceError, "NPObject deleted", isolate);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100102 return;
103 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100104
105 // Wrap up parameters.
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000106 int numArgs = info.Length();
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100107 OwnPtr<NPVariant[]> npArgs = adoptArrayPtr(new NPVariant[numArgs]);
Ben Murdoche69819b2013-07-17 14:56:49 +0100108
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100109 for (int i = 0; i < numArgs; i++)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000110 convertV8ObjectToNPVariant(info[i], npObject, &npArgs[i], isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111
112 NPVariant result;
113 VOID_TO_NPVARIANT(result);
114
115 bool retval = true;
116 switch (functionId) {
117 case InvokeMethod:
118 if (npObject->_class->invoke) {
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000119 v8::Handle<v8::String> functionName = v8::Handle<v8::String>::Cast(info.Data());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100120 NPIdentifier identifier = getStringIdentifier(functionName);
121 retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
122 }
123 break;
124 case InvokeConstruct:
125 if (npObject->_class->construct)
126 retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
127 break;
128 case InvokeDefault:
129 if (npObject->_class->invokeDefault)
130 retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
131 break;
132 default:
133 break;
134 }
135
136 if (!retval)
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000137 throwError(v8GeneralError, "Error calling method on NPObject.", isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100138
139 for (int i = 0; i < numArgs; i++)
140 _NPN_ReleaseVariantValue(&npArgs[i]);
141
142 // Unwrap return values.
143 v8::Handle<v8::Value> returnValue;
144 if (_NPN_IsAlive(npObject))
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000145 returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100146 _NPN_ReleaseVariantValue(&result);
147
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000148 v8SetReturnValue(info, returnValue);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100149}
150
151
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000152void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100153{
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000154 return npObjectInvokeImpl(info, InvokeMethod);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100155}
156
157
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000158void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100159{
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000160 if (info.IsConstructCall()) {
161 npObjectInvokeImpl(info, InvokeConstruct);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100162 return;
163 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100164
Torne (Richard Coles)19cde672013-11-06 12:28:04 +0000165 npObjectInvokeImpl(info, InvokeDefault);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100166}
167
168class V8NPTemplateMap {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100169public:
170 // NPIdentifier is PrivateIdentifier*.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100171 typedef HashMap<PrivateIdentifier*, UnsafePersistent<v8::FunctionTemplate> > MapType;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100172
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100173 UnsafePersistent<v8::FunctionTemplate> get(PrivateIdentifier* key)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100174 {
175 return m_map.get(key);
176 }
177
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100178 void set(PrivateIdentifier* key, v8::Handle<v8::FunctionTemplate> handle)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100179 {
180 ASSERT(!m_map.contains(key));
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100181 v8::Persistent<v8::FunctionTemplate> wrapper(m_isolate, handle);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000182 wrapper.SetWeak(key, &setWeakCallback);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100183 m_map.set(key, UnsafePersistent<v8::FunctionTemplate>(wrapper));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100184 }
185
186 static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate)
187 {
188 DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate));
189 ASSERT(isolate == map.m_isolate);
190 return map;
191 }
192
193private:
194 explicit V8NPTemplateMap(v8::Isolate* isolate)
195 : m_isolate(isolate)
196 {
197 }
198
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000199 void clear(PrivateIdentifier* key)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100200 {
201 MapType::iterator it = m_map.find(key);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000202 ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100203 it->value.dispose();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100204 m_map.remove(it);
205 }
206
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000207 static void setWeakCallback(const v8::WeakCallbackData<v8::FunctionTemplate, PrivateIdentifier>& data)
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100208 {
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000209 V8NPTemplateMap::sharedInstance(data.GetIsolate()).clear(data.GetParameter());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100210 }
211
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100212 MapType m_map;
213 v8::Isolate* m_isolate;
214};
215
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100216static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key, v8::Isolate* isolate)
217{
218 NPObject* npObject = v8ObjectToNPObject(self);
219
220 // Verify that our wrapper wasn't using a NPObject which
221 // has already been deleted.
222 if (!npObject || !_NPN_IsAlive(npObject))
223 return throwError(v8ReferenceError, "NPObject deleted", isolate);
224
Ben Murdoche69819b2013-07-17 14:56:49 +0100225
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100226 if (npObject->_class->hasProperty && npObject->_class->getProperty && npObject->_class->hasProperty(npObject, identifier)) {
227 if (!_NPN_IsAlive(npObject))
228 return throwError(v8ReferenceError, "NPObject deleted", isolate);
229
230 NPVariant result;
231 VOID_TO_NPVARIANT(result);
232 if (!npObject->_class->getProperty(npObject, identifier, &result))
233 return v8Undefined();
234
235 v8::Handle<v8::Value> returnValue;
236 if (_NPN_IsAlive(npObject))
Ben Murdoche69819b2013-07-17 14:56:49 +0100237 returnValue = convertNPVariantToV8Object(&result, npObject, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100238 _NPN_ReleaseVariantValue(&result);
239 return returnValue;
240
241 }
242
243 if (!_NPN_IsAlive(npObject))
244 return throwError(v8ReferenceError, "NPObject deleted", isolate);
245
246 if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
247 if (!_NPN_IsAlive(npObject))
248 return throwError(v8ReferenceError, "NPObject deleted", isolate);
249
250 PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100251 UnsafePersistent<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::sharedInstance(isolate).get(id);
252 // FunctionTemplate caches function for each context.
253 v8::Local<v8::Function> v8Function;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100254 // Cache templates using identifier as the key.
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100255 if (functionTemplate.isEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100256 // Create a new template.
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000257 v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100258 temp->SetCallHandler(npObjectMethodHandler, key);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100259 V8NPTemplateMap::sharedInstance(isolate).set(id, temp);
260 v8Function = temp->GetFunction();
261 } else {
262 v8Function = functionTemplate.newLocal(isolate)->GetFunction();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100263 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100264 v8Function->SetName(v8::Handle<v8::String>::Cast(key));
265 return v8Function;
266 }
267
268 return v8Undefined();
269}
270
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100271void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100272{
273 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100274 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100275}
276
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100277void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100278{
279 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100280 v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100281}
282
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100283void 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 +0100284{
285 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100286 v8SetReturnValue(info, npObjectGetProperty(self, identifier, name, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100287}
288
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100289void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100290{
291 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100292 v8SetReturnValue(info, npObjectGetProperty(self, identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100293}
294
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100295void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Integer>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100296{
297 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100298 if (npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()).IsEmpty())
299 return;
300 v8SetReturnValueInt(info, 0);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100301}
302
303static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate)
304{
305 NPObject* npObject = v8ObjectToNPObject(self);
306
307 // Verify that our wrapper wasn't using a NPObject which has already been deleted.
308 if (!npObject || !_NPN_IsAlive(npObject)) {
309 throwError(v8ReferenceError, "NPObject deleted", isolate);
310 return value; // Intercepted, but an exception was thrown.
311 }
312
313 if (npObject->_class->hasProperty && npObject->_class->setProperty && npObject->_class->hasProperty(npObject, identifier)) {
314 if (!_NPN_IsAlive(npObject))
315 return throwError(v8ReferenceError, "NPObject deleted", isolate);
316
317 NPVariant npValue;
318 VOID_TO_NPVARIANT(npValue);
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100319 convertV8ObjectToNPVariant(value, npObject, &npValue, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100320 bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
321 _NPN_ReleaseVariantValue(&npValue);
322 if (success)
323 return value; // Intercept the call.
324 }
325 return v8Undefined();
326}
327
328
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100329void 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 +0100330{
331 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100332 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100333}
334
335
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100336void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100337{
338 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100339 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100340}
341
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100342void 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 +0100343{
344 NPIdentifier identifier = getStringIdentifier(name);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100345 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100346}
347
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100348void 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 +0100349{
350 NPIdentifier identifier = _NPN_GetIntIdentifier(index);
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100351 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100352}
353
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100354void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info, bool namedProperty)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100355{
356 NPObject* npObject = v8ObjectToNPObject(info.Holder());
357
358 // Verify that our wrapper wasn't using a NPObject which
359 // has already been deleted.
360 if (!npObject || !_NPN_IsAlive(npObject))
361 throwError(v8ReferenceError, "NPObject deleted", info.GetIsolate());
362
363 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) {
364 uint32_t count;
365 NPIdentifier* identifiers;
366 if (npObject->_class->enumerate(npObject, &identifiers, &count)) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000367 uint32_t propertiesCount = 0;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100368 for (uint32_t i = 0; i < count; ++i) {
369 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000370 if (namedProperty == identifier->m_isString)
371 ++propertiesCount;
372 }
373 v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertiesCount);
374 for (uint32_t i = 0, propertyIndex = 0; i < count; ++i) {
375 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
376 if (namedProperty == identifier->m_isString) {
377 ASSERT(propertyIndex < propertiesCount);
378 if (namedProperty)
379 properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8AtomicString(info.GetIsolate(), identifier->string()));
380 else
381 properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8::Integer::New(info.GetIsolate(), identifier->number()));
382 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100383 }
384
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100385 v8SetReturnValue(info, properties);
386 return;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100387 }
388 }
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100389}
390
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100391void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100392{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100393 npObjectPropertyEnumerator(info, true);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100394}
395
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100396void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100397{
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100398 npObjectPropertyEnumerator(info, false);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100399}
400
401static DOMWrapperMap<NPObject>& staticNPObjectMap()
402{
403 DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetCurrent()));
404 return npObjectMap;
405}
406
407template<>
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000408inline void DOMWrapperMap<NPObject>::setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<NPObject> >& data)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100409{
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000410 NPObject* npObject = static_cast<NPObject*>(toNative(data.GetValue()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100411
412 ASSERT(npObject);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000413 ASSERT(staticNPObjectMap().containsKeyAndValue(npObject, data.GetValue()));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100414
415 // Must remove from our map before calling _NPN_ReleaseObject(). _NPN_ReleaseObject can
416 // call forgetV8ObjectForNPObject, which uses the table as well.
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100417 staticNPObjectMap().removeAndDispose(npObject);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100418
419 if (_NPN_IsAlive(npObject))
420 _NPN_ReleaseObject(npObject);
421}
422
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100423v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root, v8::Isolate* isolate)
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100424{
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100425 static v8::Eternal<v8::FunctionTemplate> npObjectDesc;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100426
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000427 ASSERT(isolate->InContext());
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100428
429 // If this is a v8 object, just return it.
Ben Murdoch591b9582013-07-10 11:41:44 +0100430 V8NPObject* v8NPObject = npObjectToV8NPObject(object);
431 if (v8NPObject)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100432 return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100433
434 // If we've already wrapped this object, just return it.
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100435 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100436 if (!wrapper.IsEmpty())
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100437 return wrapper;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100438
439 // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
440 // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
441 // can be used by DOM bindings.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100442 if (npObjectDesc.IsEmpty()) {
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000443 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100444 templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
445 templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator);
446 templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
447 templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100448 npObjectDesc.Set(isolate, templ);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100449 }
450
451 // FIXME: Move staticNPObjectMap() to DOMDataStore.
452 // Use V8DOMWrapper::createWrapper() and
453 // V8DOMWrapper::associateObjectWithWrapper()
454 // to create a wrapper object.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100455 v8::Handle<v8::Function> v8Function = npObjectDesc.Get(isolate)->GetFunction();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100456 v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(v8Function);
457 if (value.IsEmpty())
458 return value;
459
460 V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object);
461
462 // KJS retains the object as part of its wrapper (see Bindings::CInstance).
463 _NPN_RetainObject(object);
Ben Murdoche69819b2013-07-17 14:56:49 +0100464 _NPN_RegisterObject(object, root);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100465
466 WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent);
467 staticNPObjectMap().set(object, value, configuration);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000468 ASSERT(V8DOMWrapper::isDOMWrapper(value));
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100469 return value;
470}
471
472void forgetV8ObjectForNPObject(NPObject* object)
473{
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100474 v8::Isolate* isolate = v8::Isolate::GetCurrent();
475 v8::HandleScope scope(isolate);
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100476 v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100477 if (!wrapper.IsEmpty()) {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100478 V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo());
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +0100479 staticNPObjectMap().removeAndDispose(object);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100480 _NPN_ReleaseObject(object);
481 }
482}
483
484} // namespace WebCore