Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 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 Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 32 | |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 33 | #include "bindings/core/v8/V8NPObject.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 34 | |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 35 | #include "bindings/core/v8/NPV8Object.h" |
| 36 | #include "bindings/core/v8/V8Binding.h" |
Torne (Richard Coles) | f6b7aed | 2014-06-09 12:01:17 +0100 | [diff] [blame] | 37 | #include "bindings/core/v8/V8HTMLAppletElement.h" |
| 38 | #include "bindings/core/v8/V8HTMLEmbedElement.h" |
| 39 | #include "bindings/core/v8/V8HTMLObjectElement.h" |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 40 | #include "bindings/core/v8/V8NPUtils.h" |
| 41 | #include "bindings/core/v8/V8ObjectConstructor.h" |
| 42 | #include "bindings/core/v8/V8PersistentValueMap.h" |
| 43 | #include "bindings/core/v8/npruntime_impl.h" |
| 44 | #include "bindings/core/v8/npruntime_priv.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 45 | #include "core/html/HTMLPlugInElement.h" |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 46 | #include "v8-util.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 47 | #include "wtf/OwnPtr.h" |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 48 | |
| 49 | namespace WebCore { |
| 50 | |
| 51 | enum InvokeFunctionType { |
| 52 | InvokeMethod = 1, |
| 53 | InvokeConstruct = 2, |
| 54 | InvokeDefault = 3 |
| 55 | }; |
| 56 | |
| 57 | struct 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) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 70 | static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& info, InvokeFunctionType functionId) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 71 | { |
| 72 | NPObject* npObject; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 73 | v8::Isolate* isolate = info.GetIsolate(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 74 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 75 | // These three types are subtypes of HTMLPlugInElement. |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 76 | 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) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 84 | if (RefPtr<SharedPersistent<v8::Object> > wrapper = element->pluginWrapper()) { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 85 | v8::HandleScope handleScope(isolate); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 86 | npObject = v8ObjectToNPObject(wrapper->newLocal(isolate)); |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 87 | } else { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 88 | npObject = 0; |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 89 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 90 | } else { |
| 91 | // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three |
| 92 | // internal fields. |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 93 | if (info.Holder()->InternalFieldCount() != npObjectInternalFieldCount) { |
| 94 | throwError(v8ReferenceError, "NPMethod called on non-NPObject", info.GetIsolate()); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 95 | return; |
| 96 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 97 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 98 | npObject = v8ObjectToNPObject(info.Holder()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Verify that our wrapper wasn't using a NPObject which has already been deleted. |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 102 | if (!npObject || !_NPN_IsAlive(npObject)) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 103 | throwError(v8ReferenceError, "NPObject deleted", isolate); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 104 | return; |
| 105 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 106 | |
| 107 | // Wrap up parameters. |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 108 | int numArgs = info.Length(); |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 109 | OwnPtr<NPVariant[]> npArgs = adoptArrayPtr(new NPVariant[numArgs]); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 110 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 111 | for (int i = 0; i < numArgs; i++) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 112 | convertV8ObjectToNPVariant(info[i], npObject, &npArgs[i], isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 113 | |
| 114 | NPVariant result; |
| 115 | VOID_TO_NPVARIANT(result); |
| 116 | |
| 117 | bool retval = true; |
| 118 | switch (functionId) { |
| 119 | case InvokeMethod: |
| 120 | if (npObject->_class->invoke) { |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 121 | v8::Handle<v8::String> functionName = v8::Handle<v8::String>::Cast(info.Data()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 122 | NPIdentifier identifier = getStringIdentifier(functionName); |
| 123 | retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result); |
| 124 | } |
| 125 | break; |
| 126 | case InvokeConstruct: |
| 127 | if (npObject->_class->construct) |
| 128 | retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result); |
| 129 | break; |
| 130 | case InvokeDefault: |
| 131 | if (npObject->_class->invokeDefault) |
| 132 | retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result); |
| 133 | break; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (!retval) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 139 | throwError(v8GeneralError, "Error calling method on NPObject.", isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 140 | |
| 141 | for (int i = 0; i < numArgs; i++) |
| 142 | _NPN_ReleaseVariantValue(&npArgs[i]); |
| 143 | |
| 144 | // Unwrap return values. |
| 145 | v8::Handle<v8::Value> returnValue; |
| 146 | if (_NPN_IsAlive(npObject)) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 147 | returnValue = convertNPVariantToV8Object(&result, npObject, isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 148 | _NPN_ReleaseVariantValue(&result); |
| 149 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 150 | v8SetReturnValue(info, returnValue); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 154 | void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 155 | { |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 156 | return npObjectInvokeImpl(info, InvokeMethod); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 160 | void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 161 | { |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 162 | if (info.IsConstructCall()) { |
| 163 | npObjectInvokeImpl(info, InvokeConstruct); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 164 | return; |
| 165 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 166 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 167 | npObjectInvokeImpl(info, InvokeDefault); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 170 | class V8TemplateMapTraits : public V8PersistentValueMapTraits<PrivateIdentifier*, v8::FunctionTemplate, true> { |
| 171 | public: |
| 172 | typedef v8::PersistentValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8TemplateMapTraits> MapType; |
| 173 | typedef PrivateIdentifier WeakCallbackDataType; |
| 174 | |
| 175 | static WeakCallbackDataType* WeakCallbackParameter(MapType* map, PrivateIdentifier* key, const v8::Local<v8::FunctionTemplate>& value) |
| 176 | { |
| 177 | return key; |
| 178 | } |
| 179 | |
| 180 | static void DisposeCallbackData(WeakCallbackDataType* callbackData) { } |
| 181 | |
| 182 | static MapType* MapFromWeakCallbackData( |
| 183 | const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>&); |
| 184 | |
| 185 | static PrivateIdentifier* KeyFromWeakCallbackData( |
| 186 | const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>& data) |
| 187 | { |
| 188 | return data.GetParameter(); |
| 189 | } |
| 190 | |
| 191 | // Dispose traits: |
| 192 | static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::FunctionTemplate> value, PrivateIdentifier* key) { } |
| 193 | }; |
| 194 | |
| 195 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 196 | class V8NPTemplateMap { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 197 | public: |
| 198 | // NPIdentifier is PrivateIdentifier*. |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 199 | typedef v8::PersistentValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8TemplateMapTraits> MapType; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 200 | |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 201 | v8::Local<v8::FunctionTemplate> get(PrivateIdentifier* key) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 202 | { |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 203 | return m_map.Get(key); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 206 | void set(PrivateIdentifier* key, v8::Handle<v8::FunctionTemplate> handle) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 207 | { |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 208 | ASSERT(!m_map.Contains(key)); |
| 209 | m_map.Set(key, handle); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate) |
| 213 | { |
| 214 | DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate)); |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 215 | ASSERT(isolate == map.m_map.GetIsolate()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 216 | return map; |
| 217 | } |
| 218 | |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 219 | friend class V8TemplateMapTraits; |
| 220 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 221 | private: |
| 222 | explicit V8NPTemplateMap(v8::Isolate* isolate) |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 223 | : m_map(isolate) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 224 | { |
| 225 | } |
| 226 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 227 | MapType m_map; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 230 | V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackData(const v8::WeakCallbackData<v8::FunctionTemplate, WeakCallbackDataType>& data) |
| 231 | { |
| 232 | return &V8NPTemplateMap::sharedInstance(data.GetIsolate()).m_map; |
| 233 | } |
| 234 | |
| 235 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 236 | static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key, v8::Isolate* isolate) |
| 237 | { |
| 238 | NPObject* npObject = v8ObjectToNPObject(self); |
| 239 | |
| 240 | // Verify that our wrapper wasn't using a NPObject which |
| 241 | // has already been deleted. |
| 242 | if (!npObject || !_NPN_IsAlive(npObject)) |
| 243 | return throwError(v8ReferenceError, "NPObject deleted", isolate); |
| 244 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 245 | |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 246 | if (npObject->_class->hasProperty && npObject->_class->getProperty && npObject->_class->hasProperty(npObject, identifier)) { |
| 247 | if (!_NPN_IsAlive(npObject)) |
| 248 | return throwError(v8ReferenceError, "NPObject deleted", isolate); |
| 249 | |
| 250 | NPVariant result; |
| 251 | VOID_TO_NPVARIANT(result); |
| 252 | if (!npObject->_class->getProperty(npObject, identifier, &result)) |
| 253 | return v8Undefined(); |
| 254 | |
| 255 | v8::Handle<v8::Value> returnValue; |
| 256 | if (_NPN_IsAlive(npObject)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 257 | returnValue = convertNPVariantToV8Object(&result, npObject, isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 258 | _NPN_ReleaseVariantValue(&result); |
| 259 | return returnValue; |
| 260 | |
| 261 | } |
| 262 | |
| 263 | if (!_NPN_IsAlive(npObject)) |
| 264 | return throwError(v8ReferenceError, "NPObject deleted", isolate); |
| 265 | |
| 266 | if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) { |
| 267 | if (!_NPN_IsAlive(npObject)) |
| 268 | return throwError(v8ReferenceError, "NPObject deleted", isolate); |
| 269 | |
| 270 | PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier); |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 271 | v8::Local<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::sharedInstance(isolate).get(id); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 272 | // Cache templates using identifier as the key. |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 273 | if (functionTemplate.IsEmpty()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 274 | // Create a new template. |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 275 | functionTemplate = v8::FunctionTemplate::New(isolate); |
| 276 | functionTemplate->SetCallHandler(npObjectMethodHandler, key); |
| 277 | V8NPTemplateMap::sharedInstance(isolate).set(id, functionTemplate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 278 | } |
Ben Murdoch | 6f543c7 | 2014-04-16 11:17:22 +0100 | [diff] [blame] | 279 | v8::Local<v8::Function> v8Function = functionTemplate->GetFunction(); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 280 | v8Function->SetName(v8::Handle<v8::String>::Cast(key)); |
| 281 | return v8Function; |
| 282 | } |
| 283 | |
| 284 | return v8Undefined(); |
| 285 | } |
| 286 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 287 | void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 288 | { |
| 289 | NPIdentifier identifier = getStringIdentifier(name); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 290 | v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 293 | void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 294 | { |
| 295 | NPIdentifier identifier = _NPN_GetIntIdentifier(index); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 296 | v8SetReturnValue(info, npObjectGetProperty(info.Holder(), identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 299 | void npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 300 | { |
| 301 | NPIdentifier identifier = getStringIdentifier(name); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 302 | v8SetReturnValue(info, npObjectGetProperty(self, identifier, name, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 305 | void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 306 | { |
| 307 | NPIdentifier identifier = _NPN_GetIntIdentifier(index); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 308 | v8SetReturnValue(info, npObjectGetProperty(self, identifier, v8::Number::New(info.GetIsolate(), index), info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 311 | void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbackInfo<v8::Integer>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 312 | { |
| 313 | NPIdentifier identifier = getStringIdentifier(name); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 314 | if (npObjectGetProperty(info.Holder(), identifier, name, info.GetIsolate()).IsEmpty()) |
| 315 | return; |
| 316 | v8SetReturnValueInt(info, 0); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate) |
| 320 | { |
| 321 | NPObject* npObject = v8ObjectToNPObject(self); |
| 322 | |
| 323 | // Verify that our wrapper wasn't using a NPObject which has already been deleted. |
| 324 | if (!npObject || !_NPN_IsAlive(npObject)) { |
| 325 | throwError(v8ReferenceError, "NPObject deleted", isolate); |
Ben Murdoch | 197021e | 2014-07-20 18:26:29 -0700 | [diff] [blame^] | 326 | return value; // Intercepted, but an exception was thrown. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (npObject->_class->hasProperty && npObject->_class->setProperty && npObject->_class->hasProperty(npObject, identifier)) { |
| 330 | if (!_NPN_IsAlive(npObject)) |
| 331 | return throwError(v8ReferenceError, "NPObject deleted", isolate); |
| 332 | |
| 333 | NPVariant npValue; |
| 334 | VOID_TO_NPVARIANT(npValue); |
Torne (Richard Coles) | 9bbd2f5 | 2013-09-19 22:37:05 +0100 | [diff] [blame] | 335 | convertV8ObjectToNPVariant(value, npObject, &npValue, isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 336 | bool success = npObject->_class->setProperty(npObject, identifier, &npValue); |
| 337 | _NPN_ReleaseVariantValue(&npValue); |
| 338 | if (success) |
| 339 | return value; // Intercept the call. |
| 340 | } |
| 341 | return v8Undefined(); |
| 342 | } |
| 343 | |
| 344 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 345 | void npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 346 | { |
| 347 | NPIdentifier identifier = getStringIdentifier(name); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 348 | v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 352 | void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 353 | { |
| 354 | NPIdentifier identifier = _NPN_GetIntIdentifier(index); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 355 | v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 358 | void 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) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 359 | { |
| 360 | NPIdentifier identifier = getStringIdentifier(name); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 361 | v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 364 | void npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 365 | { |
| 366 | NPIdentifier identifier = _NPN_GetIntIdentifier(index); |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 367 | v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.GetIsolate())); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 370 | void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info, bool namedProperty) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 371 | { |
| 372 | NPObject* npObject = v8ObjectToNPObject(info.Holder()); |
| 373 | |
| 374 | // Verify that our wrapper wasn't using a NPObject which |
| 375 | // has already been deleted. |
| 376 | if (!npObject || !_NPN_IsAlive(npObject)) |
| 377 | throwError(v8ReferenceError, "NPObject deleted", info.GetIsolate()); |
| 378 | |
| 379 | if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) { |
| 380 | uint32_t count; |
| 381 | NPIdentifier* identifiers; |
| 382 | if (npObject->_class->enumerate(npObject, &identifiers, &count)) { |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 383 | uint32_t propertiesCount = 0; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 384 | for (uint32_t i = 0; i < count; ++i) { |
| 385 | IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 386 | if (namedProperty == identifier->m_isString) |
| 387 | ++propertiesCount; |
| 388 | } |
| 389 | v8::Handle<v8::Array> properties = v8::Array::New(info.GetIsolate(), propertiesCount); |
| 390 | for (uint32_t i = 0, propertyIndex = 0; i < count; ++i) { |
| 391 | IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]); |
| 392 | if (namedProperty == identifier->m_isString) { |
| 393 | ASSERT(propertyIndex < propertiesCount); |
| 394 | if (namedProperty) |
| 395 | properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8AtomicString(info.GetIsolate(), identifier->string())); |
| 396 | else |
| 397 | properties->Set(v8::Integer::New(info.GetIsolate(), propertyIndex++), v8::Integer::New(info.GetIsolate(), identifier->number())); |
| 398 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 401 | v8SetReturnValue(info, properties); |
| 402 | return; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 403 | } |
| 404 | } |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 407 | void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 408 | { |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 409 | npObjectPropertyEnumerator(info, true); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 412 | void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 413 | { |
Torne (Richard Coles) | 521d96e | 2013-06-19 11:58:24 +0100 | [diff] [blame] | 414 | npObjectPropertyEnumerator(info, false); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static DOMWrapperMap<NPObject>& staticNPObjectMap() |
| 418 | { |
| 419 | DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetCurrent())); |
| 420 | return npObjectMap; |
| 421 | } |
| 422 | |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 423 | template <> |
| 424 | inline void DOMWrapperMap<NPObject>::PersistentValueMapTraits::Dispose( |
| 425 | v8::Isolate* isolate, |
| 426 | v8::UniquePersistent<v8::Object> value, |
| 427 | NPObject* npObject) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 428 | { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 429 | ASSERT(npObject); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 430 | if (_NPN_IsAlive(npObject)) |
| 431 | _NPN_ReleaseObject(npObject); |
| 432 | } |
| 433 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 434 | v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root, v8::Isolate* isolate) |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 435 | { |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 436 | static v8::Eternal<v8::FunctionTemplate> npObjectDesc; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 437 | |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 438 | ASSERT(isolate->InContext()); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 439 | |
| 440 | // If this is a v8 object, just return it. |
Ben Murdoch | 591b958 | 2013-07-10 11:41:44 +0100 | [diff] [blame] | 441 | V8NPObject* v8NPObject = npObjectToV8NPObject(object); |
| 442 | if (v8NPObject) |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 443 | return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 444 | |
| 445 | // If we've already wrapped this object, just return it. |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 446 | v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 447 | if (!wrapper.IsEmpty()) |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 448 | return wrapper; |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 449 | |
| 450 | // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped |
| 451 | // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type |
| 452 | // can be used by DOM bindings. |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 453 | if (npObjectDesc.IsEmpty()) { |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 454 | v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolate); |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 455 | templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount); |
| 456 | templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedPropertyEnumerator); |
| 457 | templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator); |
| 458 | templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 459 | npObjectDesc.Set(isolate, templ); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // FIXME: Move staticNPObjectMap() to DOMDataStore. |
| 463 | // Use V8DOMWrapper::createWrapper() and |
| 464 | // V8DOMWrapper::associateObjectWithWrapper() |
| 465 | // to create a wrapper object. |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 466 | v8::Handle<v8::Function> v8Function = npObjectDesc.Get(isolate)->GetFunction(); |
Ben Murdoch | a9984bf | 2014-04-10 11:22:39 +0100 | [diff] [blame] | 467 | v8::Local<v8::Object> value = V8ObjectConstructor::newInstance(isolate, v8Function); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 468 | if (value.IsEmpty()) |
| 469 | return value; |
| 470 | |
| 471 | V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), object); |
| 472 | |
| 473 | // KJS retains the object as part of its wrapper (see Bindings::CInstance). |
| 474 | _NPN_RetainObject(object); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 475 | _NPN_RegisterObject(object, root); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 476 | |
| 477 | WrapperConfiguration configuration = buildWrapperConfiguration(object, WrapperConfiguration::Dependent); |
| 478 | staticNPObjectMap().set(object, value, configuration); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 479 | ASSERT(V8DOMWrapper::isDOMWrapper(value)); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 480 | return value; |
| 481 | } |
| 482 | |
| 483 | void forgetV8ObjectForNPObject(NPObject* object) |
| 484 | { |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 485 | v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 486 | v8::HandleScope scope(isolate); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 487 | v8::Handle<v8::Object> wrapper = staticNPObjectMap().newLocal(object, isolate); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 488 | if (!wrapper.IsEmpty()) { |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 489 | V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo()); |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 490 | staticNPObjectMap().removeAndDispose(object); |
Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame] | 491 | _NPN_ReleaseObject(object); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | } // namespace WebCore |