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