| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef SKY_ENGINE_TONIC_DART_WRAPPABLE_H_ |
| 6 #define SKY_ENGINE_TONIC_DART_WRAPPABLE_H_ |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "base/template_util.h" |
| 10 #include "dart/runtime/include/dart_api.h" |
| 11 #include "sky/engine/tonic/dart_converter.h" |
| 12 #include "sky/engine/tonic/dart_error.h" |
| 13 #include "sky/engine/tonic/dart_state.h" |
| 14 #include "sky/engine/tonic/dart_wrapper_info.h" |
| 15 |
| 16 namespace blink { |
| 17 class DartGCVisitor; |
| 18 struct DartWrapperInfo; |
| 19 |
| 20 // DartWrappable is a base class that you can inherit from in order to be |
| 21 // exposed to Dart code as an interface. |
| 22 class DartWrappable { |
| 23 public: |
| 24 enum DartNativeFields { |
| 25 kPeerIndex, // Must be first to work with Dart_GetNativeReceiver. |
| 26 kWrapperInfoIndex, |
| 27 kNumberOfNativeFields, |
| 28 }; |
| 29 |
| 30 DartWrappable() : dart_wrapper_(nullptr) {} |
| 31 |
| 32 // Subclasses that wish to expose a new interface must override this function |
| 33 // and provide information about their wrapper. There is no need to call your |
| 34 // base class's implementation of this function. |
| 35 virtual const DartWrapperInfo& GetDartWrapperInfo() const = 0; |
| 36 |
| 37 // Subclasses that wish to integrate with the Dart garbage collector should |
| 38 // override this function. Please call your base class's AcceptDartGCVisitor |
| 39 // at the end of your override. |
| 40 virtual void AcceptDartGCVisitor(DartGCVisitor& visitor) const; |
| 41 |
| 42 Dart_Handle CreateDartWrapper(DartState* dart_state); |
| 43 Dart_WeakPersistentHandle dart_wrapper() const { return dart_wrapper_; } |
| 44 |
| 45 protected: |
| 46 virtual ~DartWrappable(); |
| 47 |
| 48 private: |
| 49 static void FinalizeDartWrapper(void* isolate_callback_data, |
| 50 Dart_WeakPersistentHandle wrapper, |
| 51 void* peer); |
| 52 |
| 53 Dart_WeakPersistentHandle dart_wrapper_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(DartWrappable); |
| 56 }; |
| 57 |
| 58 #define DEFINE_WRAPPERTYPEINFO() \ |
| 59 public: \ |
| 60 const DartWrapperInfo& GetDartWrapperInfo() const override { \ |
| 61 return dart_wrapper_info_; \ |
| 62 } \ |
| 63 private: \ |
| 64 static const DartWrapperInfo& dart_wrapper_info_ |
| 65 |
| 66 struct DartConverterWrappable { |
| 67 static DartWrappable* FromDart(Dart_Handle handle); |
| 68 static DartWrappable* FromArguments(Dart_NativeArguments args, |
| 69 int index, |
| 70 Dart_Handle& exception); |
| 71 static DartWrappable* FromArgumentsWithNullCheck(Dart_NativeArguments args, |
| 72 int index, |
| 73 Dart_Handle& exception); |
| 74 }; |
| 75 |
| 76 template<typename T> |
| 77 struct DartConverter< |
| 78 T*, |
| 79 typename base::enable_if< |
| 80 base::is_convertible<T*, DartWrappable*>::value>::type> { |
| 81 static Dart_Handle ToDart(DartWrappable* val) { |
| 82 if (!val) |
| 83 return Dart_Null(); |
| 84 if (Dart_WeakPersistentHandle wrapper = val->dart_wrapper()) |
| 85 return Dart_HandleFromWeakPersistent(wrapper); |
| 86 return val->CreateDartWrapper(DartState::Current()); |
| 87 } |
| 88 |
| 89 static void SetReturnValue(Dart_NativeArguments args, |
| 90 DartWrappable* val, |
| 91 bool auto_scope = true) { |
| 92 if (!val) |
| 93 Dart_SetReturnValue(args, Dart_Null()); |
| 94 else if (Dart_WeakPersistentHandle wrapper = val->dart_wrapper()) |
| 95 Dart_SetWeakHandleReturnValue(args, wrapper); |
| 96 else |
| 97 Dart_SetReturnValue(args, val->CreateDartWrapper(DartState::Current())); |
| 98 } |
| 99 |
| 100 static T* FromDart(Dart_Handle handle) { |
| 101 // TODO(abarth): We're missing a type check. |
| 102 return static_cast<T*>(DartConverterWrappable::FromDart(handle)); |
| 103 } |
| 104 |
| 105 static T* FromArguments(Dart_NativeArguments args, |
| 106 int index, |
| 107 Dart_Handle& exception, |
| 108 bool auto_scope = true) { |
| 109 // TODO(abarth): We're missing a type check. |
| 110 return static_cast<T*>(DartConverterWrappable::FromArguments( |
| 111 args, index, exception)); |
| 112 } |
| 113 |
| 114 static T* FromArgumentsWithNullCheck(Dart_NativeArguments args, |
| 115 int index, |
| 116 Dart_Handle& exception, |
| 117 bool auto_scope = true) { |
| 118 // TODO(abarth): We're missing a type check. |
| 119 return static_cast<T*>(DartConverterWrappable::FromArgumentsWithNullCheck( |
| 120 args, index, exception)); |
| 121 } |
| 122 }; |
| 123 |
| 124 template<typename T> |
| 125 struct DartConverter<RefPtr<T>> { |
| 126 static Dart_Handle ToDart(RefPtr<T> val) { |
| 127 return DartConverter<T*>::ToDart(val.get()); |
| 128 } |
| 129 }; |
| 130 |
| 131 template<typename T> |
| 132 static T* GetReceiver(Dart_NativeArguments args) { |
| 133 intptr_t receiver; |
| 134 Dart_Handle result = Dart_GetNativeReceiver(args, &receiver); |
| 135 DCHECK(!Dart_IsError(result)); |
| 136 return static_cast<T*>(reinterpret_cast<DartWrappable*>(receiver)); |
| 137 } |
| 138 |
| 139 } // namespace blink |
| 140 |
| 141 #endif // SKY_ENGINE_TONIC_DART_WRAPPABLE_H_ |
| OLD | NEW |