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 "tonic/dart_converter.h" |
| 12 #include "tonic/dart_error.h" |
| 13 #include "tonic/dart_state.h" |
| 14 #include "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 void AssociateWithDartWrapper(Dart_NativeArguments args); |
| 44 Dart_WeakPersistentHandle dart_wrapper() const { return dart_wrapper_; } |
| 45 |
| 46 protected: |
| 47 virtual ~DartWrappable(); |
| 48 |
| 49 private: |
| 50 static void FinalizeDartWrapper(void* isolate_callback_data, |
| 51 Dart_WeakPersistentHandle wrapper, |
| 52 void* peer); |
| 53 |
| 54 Dart_WeakPersistentHandle dart_wrapper_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(DartWrappable); |
| 57 }; |
| 58 |
| 59 #define DEFINE_WRAPPERTYPEINFO() \ |
| 60 public: \ |
| 61 const DartWrapperInfo& GetDartWrapperInfo() const override { \ |
| 62 return dart_wrapper_info_; \ |
| 63 } \ |
| 64 private: \ |
| 65 static const DartWrapperInfo& dart_wrapper_info_ |
| 66 |
| 67 struct DartConverterWrappable { |
| 68 static DartWrappable* FromDart(Dart_Handle handle); |
| 69 static DartWrappable* FromArguments(Dart_NativeArguments args, |
| 70 int index, |
| 71 Dart_Handle& exception); |
| 72 static DartWrappable* FromArgumentsWithNullCheck(Dart_NativeArguments args, |
| 73 int index, |
| 74 Dart_Handle& exception); |
| 75 }; |
| 76 |
| 77 template<typename T> |
| 78 struct DartConverter< |
| 79 T*, |
| 80 typename base::enable_if< |
| 81 base::is_convertible<T*, DartWrappable*>::value>::type> { |
| 82 static Dart_Handle ToDart(DartWrappable* val) { |
| 83 if (!val) |
| 84 return Dart_Null(); |
| 85 if (Dart_WeakPersistentHandle wrapper = val->dart_wrapper()) |
| 86 return Dart_HandleFromWeakPersistent(wrapper); |
| 87 return val->CreateDartWrapper(DartState::Current()); |
| 88 } |
| 89 |
| 90 static void SetReturnValue(Dart_NativeArguments args, |
| 91 DartWrappable* val, |
| 92 bool auto_scope = true) { |
| 93 if (!val) |
| 94 Dart_SetReturnValue(args, Dart_Null()); |
| 95 else if (Dart_WeakPersistentHandle wrapper = val->dart_wrapper()) |
| 96 Dart_SetWeakHandleReturnValue(args, wrapper); |
| 97 else |
| 98 Dart_SetReturnValue(args, val->CreateDartWrapper(DartState::Current())); |
| 99 } |
| 100 |
| 101 static T* FromDart(Dart_Handle handle) { |
| 102 // TODO(abarth): We're missing a type check. |
| 103 return static_cast<T*>(DartConverterWrappable::FromDart(handle)); |
| 104 } |
| 105 |
| 106 static T* FromArguments(Dart_NativeArguments args, |
| 107 int index, |
| 108 Dart_Handle& exception, |
| 109 bool auto_scope = true) { |
| 110 // TODO(abarth): We're missing a type check. |
| 111 return static_cast<T*>(DartConverterWrappable::FromArguments( |
| 112 args, index, exception)); |
| 113 } |
| 114 |
| 115 static T* FromArgumentsWithNullCheck(Dart_NativeArguments args, |
| 116 int index, |
| 117 Dart_Handle& exception, |
| 118 bool auto_scope = true) { |
| 119 // TODO(abarth): We're missing a type check. |
| 120 return static_cast<T*>(DartConverterWrappable::FromArgumentsWithNullCheck( |
| 121 args, index, exception)); |
| 122 } |
| 123 }; |
| 124 |
| 125 template<typename T> |
| 126 struct DartConverter<RefPtr<T>> { |
| 127 static Dart_Handle ToDart(RefPtr<T> val) { |
| 128 return DartConverter<T*>::ToDart(val.get()); |
| 129 } |
| 130 |
| 131 static RefPtr<T> FromDart(Dart_Handle handle) { |
| 132 return DartConverter<T*>::FromDart(handle); |
| 133 } |
| 134 }; |
| 135 |
| 136 template<typename T> |
| 137 inline T* GetReceiver(Dart_NativeArguments args) { |
| 138 intptr_t receiver; |
| 139 Dart_Handle result = Dart_GetNativeReceiver(args, &receiver); |
| 140 DCHECK(!Dart_IsError(result)); |
| 141 return static_cast<T*>(reinterpret_cast<DartWrappable*>(receiver)); |
| 142 } |
| 143 |
| 144 } // namespace blink |
| 145 |
| 146 #endif // SKY_ENGINE_TONIC_DART_WRAPPABLE_H_ |
OLD | NEW |