OLD | NEW |
(Empty) | |
| 1 // Copyright 2011, Google Inc. |
| 2 // 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 #ifndef DartDOMWrapper_h |
| 31 #define DartDOMWrapper_h |
| 32 |
| 33 #include "bindings/core/v8/Dictionary.h" |
| 34 #include "bindings/core/dart/DartDOMData.h" |
| 35 #include "bindings/core/dart/DartUtilities.h" |
| 36 #include "bindings/core/v8/SerializedScriptValue.h" |
| 37 #include "bindings/dart/DartWebkitClassIds.h" |
| 38 #include "core/dom/ExceptionCode.h" |
| 39 |
| 40 #include "wtf/PassRefPtr.h" |
| 41 #include "wtf/RefPtr.h" |
| 42 #include "wtf/text/AtomicString.h" |
| 43 #include "wtf/text/WTFString.h" |
| 44 #include <dart_api.h> |
| 45 |
| 46 namespace blink { |
| 47 |
| 48 template<typename HTMLElement> |
| 49 class DartCustomElementWrapper; |
| 50 class HTMLElement; |
| 51 |
| 52 template<class BindingsClass> |
| 53 struct DartDOMWrapperTraits; |
| 54 |
| 55 class DartDOMWrapper { |
| 56 public: |
| 57 enum NativeFieldIndices { |
| 58 NativeImplementationIndex = 0, |
| 59 NativeTypeIndex = 1, |
| 60 NativeFieldCount |
| 61 }; |
| 62 |
| 63 template <class BindingsClass> |
| 64 static Dart_WeakPersistentHandle lookupWrapper(DartDOMData* domData, typenam
e BindingsClass::NativeType* domObject) |
| 65 { |
| 66 typedef DartDOMWrapperTraits<BindingsClass> Traits; |
| 67 ASSERT(domObject); |
| 68 ASSERT(domData); |
| 69 return Traits::MapTraits::domMap(domData)->get(domObject); |
| 70 } |
| 71 |
| 72 template <class BindingsClass> |
| 73 static Dart_Handle createWrapper(DartDOMData* domData, typename BindingsClas
s::NativeType* domObject) |
| 74 { |
| 75 return createWrapper<BindingsClass>( |
| 76 domData, |
| 77 domObject, |
| 78 BindingsClass::dartClassId); |
| 79 } |
| 80 |
| 81 template <class BindingsClass> |
| 82 static Dart_Handle createWrapper( |
| 83 DartDOMData* domData, typename BindingsClass::NativeType* domObject, |
| 84 intptr_t cid) |
| 85 { |
| 86 ASSERT(domData); |
| 87 ASSERT(domObject); |
| 88 Dart_PersistentHandle type = dartClass(domData, cid); |
| 89 ASSERT(!Dart_IsError(type)); |
| 90 intptr_t nativeFields[NativeFieldCount]; |
| 91 nativeFields[NativeImplementationIndex] = reinterpret_cast<intptr_t>(dom
Object); |
| 92 nativeFields[NativeTypeIndex] = cid; |
| 93 Dart_Handle wrapper = Dart_AllocateWithNativeFields(type, NativeFieldCou
nt, nativeFields); |
| 94 if (Dart_IsError(wrapper)) { |
| 95 return wrapper; |
| 96 } |
| 97 associateWrapper<BindingsClass>(domData, domObject, wrapper); |
| 98 return wrapper; |
| 99 } |
| 100 |
| 101 template<class BindingsClass> |
| 102 static Dart_Handle vectorToDart(const Vector< RefPtr<typename BindingsClass:
:NativeType> >& vector) |
| 103 { |
| 104 return DartUtilities::vectorToDart<RefPtr<typename BindingsClass::Native
Type>, PassRefPtr<typename BindingsClass::NativeType>, BindingsClass::toDart>(ve
ctor); |
| 105 } |
| 106 |
| 107 template<class BindingsClass> |
| 108 static Dart_Handle vectorToDart(const HeapVector< Member<typename BindingsCl
ass::NativeType> >& vector) |
| 109 { |
| 110 return DartUtilities::vectorToDart<typename BindingsClass::NativeType, B
indingsClass::toDart>(vector); |
| 111 } |
| 112 |
| 113 static Dart_Handle vectorToDart(const Vector<float>& vector) |
| 114 { |
| 115 // If this is hot, consider using a Float32List instead. |
| 116 return DartUtilities::vectorToDart<float, double, DartUtilities::doubleT
oDart>(vector); |
| 117 } |
| 118 |
| 119 static Dart_Handle vectorToDart(const Vector<double>& vector) |
| 120 { |
| 121 // If this is hot, consider using a Float32List instead. |
| 122 return DartUtilities::vectorToDart<double, double, DartUtilities::double
ToDart>(vector); |
| 123 } |
| 124 |
| 125 static Dart_Handle vectorToDart(const Vector<unsigned>& vector) |
| 126 { |
| 127 // If this is hot, consider using a typed array instead. |
| 128 return DartUtilities::vectorToDart<unsigned, unsigned, DartUtilities::un
signedToDart>(vector); |
| 129 } |
| 130 |
| 131 static Dart_Handle vectorToDart(const Vector<String>& vector) |
| 132 { |
| 133 return DartUtilities::vectorToDart<String, const String&, DartUtilities:
:stringToDart>(vector); |
| 134 } |
| 135 |
| 136 template<class T> |
| 137 static Dart_Handle vectorToDartNullable(Nullable<T> vector) |
| 138 { |
| 139 if (vector.isNull()) |
| 140 return Dart_Null(); |
| 141 return vectorToDart(vector.get()); |
| 142 } |
| 143 |
| 144 template<class BindingsClass> |
| 145 static Dart_Handle vectorToDartNullable(Nullable< Vector< RefPtr<typename Bi
ndingsClass::NativeType> > > vector) |
| 146 { |
| 147 if (vector.isNull()) |
| 148 return Dart_Null(); |
| 149 return vectorToDart<BindingsClass>(vector.get()); |
| 150 } |
| 151 |
| 152 template <class BindingsClass> |
| 153 static typename BindingsClass::NativeType* unwrapDartWrapper( |
| 154 DartDOMData* domData, Dart_Handle wrapper, Dart_Handle& exception) |
| 155 { |
| 156 ASSERT(!exception); |
| 157 |
| 158 if (Dart_IsNull(wrapper)) |
| 159 return 0; |
| 160 |
| 161 if (subtypeOf(wrapper, BindingsClass::dartClassId)) { |
| 162 void* nativePointer = readNativePointer(wrapper, NativeImplementatio
nIndex); |
| 163 return static_cast<typename BindingsClass::NativeType*>(nativePointe
r); |
| 164 } |
| 165 const char* className = DartWebkitClassInfo[BindingsClass::dartClassId].
jsName; |
| 166 String message = String("Invalid class: expected instance of ") + |
| 167 className; |
| 168 exception = DartUtilities::stringToDartString(message); |
| 169 return 0; |
| 170 } |
| 171 |
| 172 template <class BindingsClass> |
| 173 static typename BindingsClass::NativeType* unwrapDartWrapper( |
| 174 Dart_NativeArguments args, int index, Dart_Handle& exception) |
| 175 { |
| 176 ASSERT(!exception); |
| 177 intptr_t fieldValues[NativeFieldCount]; |
| 178 Dart_Handle result = Dart_GetNativeFieldsOfArgument(args, index, NativeF
ieldCount, fieldValues); |
| 179 if (!Dart_IsError(result)) { |
| 180 void* wrapper = reinterpret_cast<void*>(fieldValues[NativeImplementa
tionIndex]); |
| 181 if (!wrapper) { |
| 182 return 0; |
| 183 } |
| 184 intptr_t cid = fieldValues[NativeTypeIndex]; |
| 185 if (subtypeOf(cid, BindingsClass::dartClassId)) { |
| 186 // FIXME(vsm): This is not safe if BindingsClass::NativeType is |
| 187 // not the primary parent. |
| 188 return static_cast<typename BindingsClass::NativeType*>(wrapper)
; |
| 189 } |
| 190 } |
| 191 const char* className = DartWebkitClassInfo[BindingsClass::dartClassId].
jsName; |
| 192 String message = String("Invalid class: expected instance of ") + classN
ame; |
| 193 exception = DartUtilities::stringToDartString(message); |
| 194 return 0; |
| 195 } |
| 196 |
| 197 static bool subtypeOf(Dart_Handle wrapper, intptr_t basecid) |
| 198 { |
| 199 intptr_t cid = reinterpret_cast<intptr_t>(readNativePointer(wrapper, Nat
iveTypeIndex)); |
| 200 return subtypeOf(cid, basecid); |
| 201 } |
| 202 |
| 203 static bool subtypeOf(intptr_t cid, intptr_t basecid) |
| 204 { |
| 205 while (cid != -1) { |
| 206 if (cid == basecid) { |
| 207 return true; |
| 208 } |
| 209 ASSERT(cid < NumWebkitClassIds); |
| 210 cid = DartWebkitClassInfo[cid].base_class_id; |
| 211 } |
| 212 return false; |
| 213 } |
| 214 |
| 215 template <class BindingsClass> |
| 216 static bool instanceOf(DartDOMData* domData, Dart_Handle wrapper) |
| 217 { |
| 218 Dart_PersistentHandle type = dartClass(domData, BindingsClass::dartClass
Id); |
| 219 |
| 220 bool isInstanceOf = false; |
| 221 Dart_Handle ALLOW_UNUSED result = Dart_ObjectIsType(wrapper, type, &isIn
stanceOf); |
| 222 ASSERT(!Dart_IsError(result)); |
| 223 return isInstanceOf; |
| 224 } |
| 225 |
| 226 template <class WebKitClass> |
| 227 static WebKitClass* receiver(Dart_NativeArguments args) |
| 228 { |
| 229 // Type of receiver is ensured by Dart VM runtime, so bypass additional
checks. |
| 230 intptr_t value = 0; |
| 231 ASSERT(!NativeImplementationIndex); |
| 232 Dart_Handle ALLOW_UNUSED result = Dart_GetNativeReceiver(args, &value); |
| 233 ASSERT(!Dart_IsError(result)); |
| 234 WebKitClass* const recv = reinterpret_cast<WebKitClass*>(value); |
| 235 if (recv == 0) { |
| 236 Dart_ThrowException(DartUtilities::stringToDartString(String("Not a
valid object"))); |
| 237 } |
| 238 return recv; |
| 239 } |
| 240 |
| 241 template <class BindingsClass> |
| 242 static void returnToDart(Dart_NativeArguments args, typename BindingsClass::
NativeType* domObject) |
| 243 { |
| 244 if (domObject) { |
| 245 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsola
teData(args)); |
| 246 Dart_WeakPersistentHandle result = lookupWrapper<BindingsClass>(domD
ata, domObject); |
| 247 if (result) |
| 248 Dart_SetWeakHandleReturnValue(args, result); |
| 249 else |
| 250 Dart_SetReturnValue(args, createWrapper<BindingsClass>(domData,
domObject, BindingsClass::dartClassId)); |
| 251 } |
| 252 } |
| 253 |
| 254 template <class BindingsClass> |
| 255 static void returnToDart(Dart_NativeArguments args, PassRefPtr<typename Bind
ingsClass::NativeType> domObject) |
| 256 { |
| 257 returnToDart<BindingsClass>(args, domObject.get()); |
| 258 } |
| 259 |
| 260 static int wrapperNativeFieldCount() { return NativeFieldCount; } |
| 261 |
| 262 private: |
| 263 static Dart_PersistentHandle dartClass(DartDOMData*, intptr_t classIndex); |
| 264 |
| 265 static void writeNativePointer(Dart_Handle wrapper, void* pointer, intptr_t
cid) |
| 266 { |
| 267 Dart_Handle ALLOW_UNUSED result = Dart_SetNativeInstanceField( |
| 268 wrapper, NativeImplementationIndex, reinterpret_cast<intptr_t>(point
er)); |
| 269 ASSERT(!Dart_IsError(result)); |
| 270 result = Dart_SetNativeInstanceField(wrapper, NativeTypeIndex, cid); |
| 271 ASSERT(!Dart_IsError(result)); |
| 272 } |
| 273 |
| 274 static void* readNativePointer(Dart_Handle wrapper, int index) |
| 275 { |
| 276 intptr_t value; |
| 277 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value)
; |
| 278 // FIXME: the fact that we return 0 on error rather than asserting is |
| 279 // somewhat of a hack. We currently make this method return 0 because |
| 280 // we reuse this method to verify that objects are actually native |
| 281 // Node objects rather than objects that implement the Node interface. |
| 282 if (Dart_IsError(result)) |
| 283 return 0; |
| 284 return reinterpret_cast<void*>(value); |
| 285 } |
| 286 |
| 287 template <class BindingsClass> |
| 288 static void wrapperWeakCallback(void* isolateCallbackData, Dart_WeakPersiste
ntHandle wrapper, void* blinkHandle) |
| 289 { |
| 290 typedef DartDOMWrapperTraits<BindingsClass> Traits; |
| 291 DartDOMData* domData = reinterpret_cast<DartDOMData*>(isolateCallbackDat
a); |
| 292 typename BindingsClass::NativeType* domObject = Traits::GCTraits::read(b
linkHandle); |
| 293 |
| 294 Dart_WeakPersistentHandle currentWrapper = 0; |
| 295 currentWrapper = Traits::MapTraits::domMap(domData)->get(domObject); |
| 296 |
| 297 // This could be an old wrapper which has been replaced with a custom el
ement. |
| 298 if (currentWrapper != wrapper) { |
| 299 #ifdef DEBUG |
| 300 DartApiScope scope; |
| 301 ASSERT(!Dart_IdentityEquals(Dart_HandleFromWeakPersistent(currentWra
pper), Dart_HandleFromWeakPersistent(wrapper))); |
| 302 #endif |
| 303 return; |
| 304 } |
| 305 |
| 306 if (currentWrapper) { |
| 307 Traits::MapTraits::domMap(domData)->remove(domObject); |
| 308 } |
| 309 Traits::GCTraits::deref(blinkHandle); |
| 310 } |
| 311 |
| 312 template <class BindingsClass> |
| 313 static void associateWrapper( |
| 314 DartDOMData* domData, typename BindingsClass::NativeType* domObject, Dar
t_Handle newInstance) |
| 315 { |
| 316 typedef DartDOMWrapperTraits<BindingsClass> Traits; |
| 317 void* blinkHandle = Traits::GCTraits::ref(domObject); |
| 318 // This is only used to inform the Dart garbage collector on how much ex
ternal memory |
| 319 // is kept alive. |
| 320 intptr_t externalAllocationSize = sizeof(*domObject); |
| 321 |
| 322 Dart_WeakPersistentHandle wrapper = Dart_NewPrologueWeakPersistentHandle
( |
| 323 newInstance, blinkHandle, externalAllocationSize, &wrapperWeakCallba
ck<BindingsClass>); |
| 324 Traits::MapTraits::domMap(domData)->set(domObject, wrapper); |
| 325 } |
| 326 |
| 327 template <class BindingsClass> |
| 328 static void disassociateWrapper( |
| 329 DartDOMData* domData, typename BindingsClass::NativeType* domObject, Dar
t_Handle oldInstance) |
| 330 { |
| 331 typedef DartDOMWrapperTraits<BindingsClass> Traits; |
| 332 |
| 333 #ifdef DEBUG |
| 334 Dart_WeakPersistentHandle wrapper = Traits::MapTraits::domMap(domData)->
get(domObject); |
| 335 ASSERT(Dart_IdentityEquals(Dart_HandleFromWeakPersistent(wrapper), oldIn
stance)); |
| 336 #endif |
| 337 Traits::MapTraits::domMap(domData)->remove(domObject); |
| 338 } |
| 339 |
| 340 friend class DartCustomElementConstructorBuilder; |
| 341 friend class DartCustomElementWrapper<HTMLElement>; |
| 342 friend class DartUtilities; |
| 343 }; |
| 344 |
| 345 struct DartDOMWrapperMapTraits { |
| 346 static DartDOMObjectMap* domMap(DartDOMData* domData) { return domData->obje
ctMap(); } |
| 347 }; |
| 348 |
| 349 template<class BindingsClass, bool isGarbageCollected> |
| 350 struct DartDOMWrapperGarbageCollectedTraits { }; |
| 351 |
| 352 template<class BindingsClass> |
| 353 struct DartDOMWrapperGarbageCollectedTraits<BindingsClass, false> { |
| 354 static void* ref(typename BindingsClass::NativeType* domObject) |
| 355 { |
| 356 domObject->ref(); |
| 357 return domObject; |
| 358 } |
| 359 |
| 360 static void deref(void* blinkHandle) |
| 361 { |
| 362 typename BindingsClass::NativeType* domObject = read(blinkHandle); |
| 363 domObject->deref(); |
| 364 } |
| 365 |
| 366 static typename BindingsClass::NativeType* read(void* blinkHandle) |
| 367 { |
| 368 return static_cast<typename BindingsClass::NativeType*>(blinkHandle); |
| 369 } |
| 370 }; |
| 371 |
| 372 template<class BindingsClass> |
| 373 struct DartDOMWrapperGarbageCollectedTraits<BindingsClass, true> { |
| 374 static void* ref(typename BindingsClass::NativeType* domObject) |
| 375 { |
| 376 return new Persistent<typename BindingsClass::NativeType>(domObject); |
| 377 } |
| 378 |
| 379 static void deref(void* blinkHandle) |
| 380 { |
| 381 Persistent<typename BindingsClass::NativeType>* handle = static_cast<Per
sistent<typename BindingsClass::NativeType>*>(blinkHandle); |
| 382 delete handle; |
| 383 } |
| 384 |
| 385 static typename BindingsClass::NativeType* read(void* blinkHandle) |
| 386 { |
| 387 Persistent<typename BindingsClass::NativeType>* handle = static_cast<Per
sistent<typename BindingsClass::NativeType>*>(blinkHandle); |
| 388 return handle->get(); |
| 389 } |
| 390 }; |
| 391 |
| 392 template<class BindingsClass> |
| 393 struct DartDOMWrapperTraits { |
| 394 typedef DartDOMWrapperMapTraits MapTraits; |
| 395 typedef DartDOMWrapperGarbageCollectedTraits<BindingsClass, BindingsClass::i
sGarbageCollected> GCTraits; |
| 396 }; |
| 397 |
| 398 struct DartMessagePort; |
| 399 |
| 400 template<> |
| 401 struct DartDOMWrapperTraits<DartMessagePort> { |
| 402 struct MessagePortMapTraits { |
| 403 static DartMessagePortMap* domMap(DartDOMData* domData) { return domData
->messagePortMap(); } |
| 404 }; |
| 405 typedef MessagePortMapTraits MapTraits; |
| 406 typedef DartDOMWrapperGarbageCollectedTraits<DartMessagePort, false> GCTrait
s; |
| 407 }; |
| 408 |
| 409 } |
| 410 |
| 411 #endif // DartDOMWrapper_h |
OLD | NEW |