Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: Source/WebCore/bindings/dart/DartDOMWrapper.h

Issue 9837116: DOM wrappers that are not retained from Dart should be collected. (Closed) Base URL: svn://svn.chromium.org/multivm/trunk/webkit
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011, Google Inc. 1 // Copyright 2011, Google Inc.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 // Redistribution and use in source and binary forms, with or without 4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are 5 // modification, are permitted provided that the following conditions are
6 // met: 6 // met:
7 // 7 //
8 // * Redistributions of source code must retain the above copyright 8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer. 9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above 10 // * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 29
30 #ifndef DartDOMWrapper_h 30 #ifndef DartDOMWrapper_h
31 #define DartDOMWrapper_h 31 #define DartDOMWrapper_h
32 32
33 #include "DOMStringList.h" 33 #include "DOMStringList.h"
34 #include "DartIsolate.h"
34 #include "DartUtilities.h" 35 #include "DartUtilities.h"
35 #include "EventListener.h" 36 #include "EventListener.h"
36 #include "EventTarget.h" 37 #include "EventTarget.h"
37 #include "ExceptionCode.h" 38 #include "ExceptionCode.h"
38 #include "MediaQueryListListener.h" 39 #include "MediaQueryListListener.h"
39 #include "Range.h" 40 #include "Range.h"
40 #include "ScriptValue.h" 41 #include "ScriptValue.h"
41 #include "SerializedScriptValue.h" 42 #include "SerializedScriptValue.h"
42 #if ENABLE(SVG) 43 #if ENABLE(SVG)
43 #include "SVGPropertyTearOff.h" 44 #include "SVGPropertyTearOff.h"
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 if (!DartUtilities::checkResult(element, exception)) 306 if (!DartUtilities::checkResult(element, exception))
306 return; 307 return;
307 array[i] = toWebGLArrayElement<Element>(element, exception); 308 array[i] = toWebGLArrayElement<Element>(element, exception);
308 if (exception) 309 if (exception)
309 return; 310 return;
310 } 311 }
311 } 312 }
312 313
313 class DartDOMWrapper { 314 class DartDOMWrapper {
314 public: 315 public:
315 template <class WebkitClass> 316 template <class WebKitClass>
316 static Dart_Handle newWrapper(const char* className, WebkitClass* domObject) 317 static Dart_Handle newWrapper(const char* className, WebKitClass* domObject)
317 { 318 {
318 Dart_Handle wrapper = instantiateWrapper(className); 319 Dart_Handle wrapper = instantiateWrapper(className);
319 installNativePointers(domObject, wrapper); 320 installNativePointers(domObject, wrapper);
320 return wrapper; 321 return wrapper;
321 } 322 }
322 323
323 template <class BindingsClass> 324 template <class BindingsClass>
324 static Dart_Handle toDart(typename BindingsClass::NativeType* instance) 325 static Dart_Handle toDart(typename BindingsClass::NativeType* instance)
325 { 326 {
326 return toDart(instance, BindingsClass::dartImplementationClassName); 327 return toDart(instance, BindingsClass::dartImplementationClassName);
327 } 328 }
328 329
329 template <class WebkitClass> 330 template <class WebKitClass>
330 static Dart_Handle toDart(WebkitClass* instance, const char* className) 331 static Dart_Handle toDart(WebKitClass* instance, const char* className)
331 { 332 {
332 if (!instance) 333 if (!instance)
333 return 0; 334 return 0;
334 335
335 // FIXME: custom toDartValue implementations should check the cache firs t. 336 // FIXME: custom toDartValue implementations should check the cache firs t.
336 // FIXME: separate maps for nodes, dom objects and active dom objects. 337 // FIXME: separate maps for nodes, dom objects and active dom objects.
337 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate(); 338 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate();
338 Dart_Handle wrapper = domMap->get(instance); 339 Dart_Handle wrapper = domMap->get(instance);
339 if (wrapper) 340 if (wrapper)
340 return wrapper; 341 return wrapper;
341 342
342 wrapper = instantiateWrapper(className); 343 wrapper = instantiateWrapper(className);
343 bindDOMObjectToDartWrapper(instance, wrapper); 344 bindDOMObjectToDartWrapper(instance, wrapper);
344 return wrapper; 345 return wrapper;
345 } 346 }
346 347
347 template <class WebkitClass> 348 template <class WebKitClass>
348 static void bindDOMObjectToDartWrapper(WebkitClass* domObject, Dart_Handle w rapper) 349 static void bindDOMObjectToDartWrapper(WebKitClass* domObject, Dart_Handle w rapper)
349 { 350 {
350 ASSERT(domObject); 351 ASSERT(domObject);
351 domObject->ref(); 352 domObject->ref();
352 installNativePointers(domObject, wrapper); 353 installNativePointers(domObject, wrapper);
353 354
354 // FIXME: make persistent handle weak and deref domObject in weak callba ck. 355 RefPtr<DartIsolate> isolate = DartIsolate::current();
355 Dart_Handle persistentWrapperHandle = Dart_NewPersistentHandle(wrapper); 356 Dart_Handle persistentWrapperHandle = isolate->createWeakPersistentHandl e(wrapper, domObject, &wrapperWeakCallback<WebKitClass>);
356 357
357 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate(); 358 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate();
358 ASSERT(!domMap->contains(domObject)); 359 ASSERT(!domMap->contains(domObject));
359 domMap->set(domObject, persistentWrapperHandle); 360 domMap->set(domObject, persistentWrapperHandle);
360 } 361 }
361 362
362 template <class BindingsClass> 363 template <class BindingsClass>
363 static typename BindingsClass::NativeType* unwrapDartWrapper(Dart_Handle wra pper, Dart_Handle& exception) 364 static typename BindingsClass::NativeType* unwrapDartWrapper(Dart_Handle wra pper, Dart_Handle& exception)
364 { 365 {
365 // FIXME: support cross-domain wrappers. 366 // FIXME: support cross-domain wrappers.
366 if (!BindingsClass::instanceOf(wrapper)) { 367 if (!BindingsClass::instanceOf(wrapper)) {
367 String message = String("Invalid class: expected instance of ") + Bi ndingsClass::dartImplementationClassName; 368 String message = String("Invalid class: expected instance of ") + Bi ndingsClass::dartImplementationClassName;
368 exception = DartUtilities::stringToDartString(message); 369 exception = DartUtilities::stringToDartString(message);
369 return 0; 370 return 0;
370 } 371 }
371 ASSERT(!exception); 372 ASSERT(!exception);
372 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn dex); 373 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn dex);
373 return reinterpret_cast<typename BindingsClass::NativeType*>(nativePoint er); 374 return reinterpret_cast<typename BindingsClass::NativeType*>(nativePoint er);
374 } 375 }
375 376
376 static void derefDOMObject(Dart_Handle wrapper, void* domObject); 377 template <class WebKitClass>
377 378 static WebKitClass* receiver(Dart_NativeArguments args)
378 template <class WebkitClass>
379 static WebkitClass* receiver(Dart_NativeArguments args)
380 { 379 {
381 // Type of receiver is ensured by Dart VM runtime, so bypass additional checks. 380 // Type of receiver is ensured by Dart VM runtime, so bypass additional checks.
382 void* nativePointer = readNativePointer(Dart_GetNativeArgument(args, 0), kNativeImplementationIndex); 381 void* nativePointer = readNativePointer(Dart_GetNativeArgument(args, 0), kNativeImplementationIndex);
383 WebkitClass* const recv = static_cast<WebkitClass*>(nativePointer); 382 WebKitClass* const recv = static_cast<WebKitClass*>(nativePointer);
384 ASSERT(recv); // Never should return 0. 383 ASSERT(recv); // Never should return 0.
385 return recv; 384 return recv;
386 } 385 }
387 386
388 template <typename Type> 387 template <typename Type>
389 static void returnValue(Dart_NativeArguments args, const Type& value) 388 static void returnValue(Dart_NativeArguments args, const Type& value)
390 { 389 {
391 Dart_Handle result = toDartValue(value); 390 Dart_Handle result = toDartValue(value);
392 if (result) 391 if (result)
393 Dart_SetReturnValue(args, result); 392 Dart_SetReturnValue(args, result);
(...skipping 10 matching lines...) Expand all
404 } 403 }
405 404
406 static Dart_Handle exceptionCodeToDartException(ExceptionCode); 405 static Dart_Handle exceptionCodeToDartException(ExceptionCode);
407 406
408 // DO NOT USE: Use BindingsClass:instanceOf(handle) instead. 407 // DO NOT USE: Use BindingsClass:instanceOf(handle) instead.
409 static bool instanceOf(const char* dartImplementationClassName, Dart_Handle wrapper); 408 static bool instanceOf(const char* dartImplementationClassName, Dart_Handle wrapper);
410 409
411 private: 410 private:
412 enum NativeFieldIndices { 411 enum NativeFieldIndices {
413 kNativeImplementationIndex = 0, 412 kNativeImplementationIndex = 0,
414 kDerefObjectFunctionIndex,
415 kNativeFieldCount 413 kNativeFieldCount
416 }; 414 };
417 415
418 static Dart_Handle instantiateWrapper(const char* className); 416 static Dart_Handle instantiateWrapper(const char* className);
419 417
420 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer ) 418 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer )
421 { 419 {
422 DartApiScope scope; 420 DartApiScope scope;
423 Dart_Handle result = Dart_SetNativeInstanceField(wrapper, index, reinter pret_cast<intptr_t>(pointer)); 421 Dart_Handle result = Dart_SetNativeInstanceField(wrapper, index, reinter pret_cast<intptr_t>(pointer));
424 UNUSED_PARAM(result); 422 UNUSED_PARAM(result);
425 ASSERT(!Dart_IsError(result)); 423 ASSERT(!Dart_IsError(result));
426 } 424 }
427 425
428 static void* readNativePointer(Dart_Handle wrapper, int index) 426 static void* readNativePointer(Dart_Handle wrapper, int index)
429 { 427 {
430 // FIXME: Try to remove this scope from the hot path. 428 // FIXME: Try to remove this scope from the hot path.
431 DartApiScope scope; 429 DartApiScope scope;
432 intptr_t value; 430 intptr_t value;
433 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value) ; 431 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value) ;
434 ASSERT(!Dart_IsError(result)); 432 ASSERT(!Dart_IsError(result));
435 UNUSED_PARAM(result); 433 UNUSED_PARAM(result);
436 return reinterpret_cast<void*>(value); 434 return reinterpret_cast<void*>(value);
437 } 435 }
438 436
439 template <class WebkitClass> 437 template <class WebKitClass>
440 static void installNativePointers(WebkitClass* domObject, Dart_Handle wrappe r) 438 static void installNativePointers(WebKitClass* domObject, Dart_Handle wrappe r)
441 { 439 {
442 ASSERT(domObject); 440 ASSERT(domObject);
443 DerefObjectFunction derefObjectFunction = &DartDOMWrapper::derefObject<W ebkitClass>;
444 writeNativePointer(wrapper, kNativeImplementationIndex, domObject); 441 writeNativePointer(wrapper, kNativeImplementationIndex, domObject);
445 writeNativePointer(wrapper, kDerefObjectFunctionIndex, reinterpret_cast< void*>(derefObjectFunction));
446 } 442 }
447 443
448 typedef void (*DerefObjectFunction)(void*);
449
450 template<typename T> 444 template<typename T>
451 static void derefObject(void* pointer) 445 static void wrapperWeakCallback(Dart_Handle, void* domObject)
452 { 446 {
453 static_cast<T*>(pointer)->deref(); 447 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate();
448 ASSERT(domMap->contains(domObject));
449 domMap->remove(domObject);
450 static_cast<T*>(domObject)->deref();
454 } 451 }
455 }; 452 };
456 453
457 template<>
458 inline void DartDOMWrapper::derefObject<NPObject>(void*)
459 {
460 // FIXME: proper deref.
461 }
462
463 // ParameterAdapter. 454 // ParameterAdapter.
464 455
465 template <typename Value> 456 template <typename Value>
466 class ParameterAdapterBase { 457 class ParameterAdapterBase {
467 public: 458 public:
468 bool conversionSuccessful() const { return !m_exception; } 459 bool conversionSuccessful() const { return !m_exception; }
469 460
470 Dart_Handle exception() const 461 Dart_Handle exception() const
471 { 462 {
472 ASSERT(!conversionSuccessful()); 463 ASSERT(!conversionSuccessful());
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 template <> 759 template <>
769 class ParameterAdapter<ScriptValue> : public ParameterAdapterBase<ScriptValue> { 760 class ParameterAdapter<ScriptValue> : public ParameterAdapterBase<ScriptValue> {
770 public: 761 public:
771 explicit ParameterAdapter(Dart_Handle handle) { this->unsupported(); } 762 explicit ParameterAdapter(Dart_Handle handle) { this->unsupported(); }
772 operator ScriptValue() const { return this->value(); } 763 operator ScriptValue() const { return this->value(); }
773 }; 764 };
774 765
775 } 766 }
776 767
777 #endif // DartDOMWrapper_h 768 #endif // DartDOMWrapper_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698