OLD | NEW |
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 12 matching lines...) Expand all Loading... |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
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 "DartWebkitClassIds.h" |
| 34 |
33 #include "bindings/dart/DartDOMData.h" | 35 #include "bindings/dart/DartDOMData.h" |
34 #include "bindings/dart/DartExceptionState.h" | 36 #include "bindings/dart/DartExceptionState.h" |
35 #include "bindings/dart/DartUtilities.h" | 37 #include "bindings/dart/DartUtilities.h" |
36 #include "bindings/v8/Dictionary.h" | 38 #include "bindings/v8/Dictionary.h" |
37 #include "bindings/v8/ScriptValue.h" | 39 #include "bindings/v8/ScriptValue.h" |
38 #include "bindings/v8/SerializedScriptValue.h" | 40 #include "bindings/v8/SerializedScriptValue.h" |
39 #include "core/dom/ExceptionCode.h" | 41 #include "core/dom/ExceptionCode.h" |
40 | 42 |
41 #include <dart_api.h> | 43 #include <dart_api.h> |
42 #include <wtf/PassRefPtr.h> | 44 #include <wtf/PassRefPtr.h> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 96 |
95 template <class BindingsClass> | 97 template <class BindingsClass> |
96 static Dart_Handle createWrapperWithClassName( | 98 static Dart_Handle createWrapperWithClassName( |
97 DartDOMData* domData, typename BindingsClass::NativeType* domObject, | 99 DartDOMData* domData, typename BindingsClass::NativeType* domObject, |
98 const char* className, intptr_t libraryId, intptr_t cid) | 100 const char* className, intptr_t libraryId, intptr_t cid) |
99 { | 101 { |
100 ASSERT(domObject); | 102 ASSERT(domObject); |
101 Dart_PersistentHandle type = dartClass(domData, className, libraryId, ci
d); | 103 Dart_PersistentHandle type = dartClass(domData, className, libraryId, ci
d); |
102 ASSERT(!Dart_IsError(type)); | 104 ASSERT(!Dart_IsError(type)); |
103 Dart_Handle wrapper = Dart_Allocate(type); | 105 Dart_Handle wrapper = Dart_Allocate(type); |
104 writeNativePointer(wrapper, kNativeImplementationIndex, domObject); | 106 writeNativePointer(wrapper, domObject, cid); |
105 associateWrapper<BindingsClass>(domData, domObject, wrapper); | 107 associateWrapper<BindingsClass>(domData, domObject, wrapper); |
106 return wrapper; | 108 return wrapper; |
107 } | 109 } |
108 | 110 |
109 // FIXME: move vectorToDart into DartUtilities or even into a separate class
. | 111 // FIXME: move vectorToDart into DartUtilities or even into a separate class
. |
110 template<class BindingsClass> | 112 template<class BindingsClass> |
111 static Dart_Handle vectorToDart(const Vector< RefPtr<typename BindingsClass:
:NativeType> >& vector) | 113 static Dart_Handle vectorToDart(const Vector< RefPtr<typename BindingsClass:
:NativeType> >& vector) |
112 { | 114 { |
113 Dart_Handle list = Dart_NewList(vector.size()); | 115 Dart_Handle list = Dart_NewList(vector.size()); |
114 if (Dart_IsError(list)) | 116 if (Dart_IsError(list)) |
(...skipping 20 matching lines...) Expand all Loading... |
135 | 137 |
136 template <class BindingsClass> | 138 template <class BindingsClass> |
137 static typename BindingsClass::NativeType* unwrapDartWrapper( | 139 static typename BindingsClass::NativeType* unwrapDartWrapper( |
138 DartDOMData* domData, Dart_Handle wrapper, Dart_Handle& exception) | 140 DartDOMData* domData, Dart_Handle wrapper, Dart_Handle& exception) |
139 { | 141 { |
140 ASSERT(!exception); | 142 ASSERT(!exception); |
141 | 143 |
142 if (Dart_IsNull(wrapper)) | 144 if (Dart_IsNull(wrapper)) |
143 return 0; | 145 return 0; |
144 | 146 |
145 // FIXME: support cross-domain wrappers. | 147 if (subtypeOf(wrapper, BindingsClass::dartClassId)) { |
146 if (!instanceOf<BindingsClass>(domData, wrapper)) { | 148 void* nativePointer = readNativePointer(wrapper, kNativeImplementati
onIndex); |
147 String message = String("Invalid class: expected instance of ") + Bi
ndingsClass::dartImplementationClassName; | 149 return static_cast<typename BindingsClass::NativeType*>(nativePointe
r); |
148 exception = DartUtilities::stringToDartString(message); | |
149 return 0; | |
150 } | 150 } |
151 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn
dex); | 151 String message = String("Invalid class: expected instance of ") + |
152 return static_cast<typename BindingsClass::NativeType*>(nativePointer); | 152 BindingsClass::dartImplementationClassName; |
| 153 exception = DartUtilities::stringToDartString(message); |
| 154 return 0; |
153 } | 155 } |
154 | 156 |
155 template <class BindingsClass> | 157 template <class BindingsClass> |
156 static typename BindingsClass::NativeType* unwrapDartWrapper( | 158 static typename BindingsClass::NativeType* unwrapDartWrapper( |
157 Dart_NativeArguments args, int index, Dart_Handle& exception) | 159 Dart_NativeArguments args, int index, Dart_Handle& exception) |
158 { | 160 { |
159 ASSERT(!exception); | 161 ASSERT(!exception); |
160 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); | 162 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
161 Dart_Handle wrapper = Dart_GetNativeArgument(args, index); | 163 Dart_Handle wrapper = Dart_GetNativeArgument(args, index); |
162 return unwrapDartWrapper<BindingsClass>(domData, wrapper, exception); | 164 return unwrapDartWrapper<BindingsClass>(domData, wrapper, exception); |
163 } | 165 } |
164 | 166 |
| 167 static bool subtypeOf(Dart_Handle wrapper, intptr_t basecid) |
| 168 { |
| 169 intptr_t cid = reinterpret_cast<intptr_t>(readNativePointer(wrapper, kNa
tiveTypeIndex)); |
| 170 while (cid != -1) { |
| 171 if (cid == basecid) { |
| 172 return true; |
| 173 } |
| 174 ASSERT(cid < NumWebkitClassIds); |
| 175 cid = DartWebkitClassInfo[cid].base_class_id; |
| 176 } |
| 177 return false; |
| 178 } |
| 179 |
165 template <class BindingsClass> | 180 template <class BindingsClass> |
166 static bool instanceOf(DartDOMData* domData, Dart_Handle wrapper) | 181 static bool instanceOf(DartDOMData* domData, Dart_Handle wrapper) |
167 { | 182 { |
168 Dart_PersistentHandle type = dartClass<BindingsClass>(domData); | 183 Dart_PersistentHandle type = dartClass<BindingsClass>(domData); |
169 | 184 |
170 bool isInstanceOf = false; | 185 bool isInstanceOf = false; |
171 Dart_Handle result = Dart_ObjectIsType(wrapper, type, &isInstanceOf); | 186 Dart_Handle result = Dart_ObjectIsType(wrapper, type, &isInstanceOf); |
172 return isInstanceOf; | 187 return isInstanceOf; |
173 } | 188 } |
174 | 189 |
(...skipping 30 matching lines...) Expand all Loading... |
205 } | 220 } |
206 | 221 |
207 static int wrapperNativeFieldCount() { return kNativeFieldCount; } | 222 static int wrapperNativeFieldCount() { return kNativeFieldCount; } |
208 | 223 |
209 static Dart_Handle exceptionCodeToDartException(ExceptionCode); | 224 static Dart_Handle exceptionCodeToDartException(ExceptionCode); |
210 static Dart_Handle exceptionCodeToDartException(DartExceptionState&); | 225 static Dart_Handle exceptionCodeToDartException(DartExceptionState&); |
211 | 226 |
212 private: | 227 private: |
213 enum NativeFieldIndices { | 228 enum NativeFieldIndices { |
214 kNativeImplementationIndex = 0, | 229 kNativeImplementationIndex = 0, |
| 230 kNativeTypeIndex = 1, |
215 kNativeFieldCount | 231 kNativeFieldCount |
216 }; | 232 }; |
217 | 233 |
218 static Dart_PersistentHandle dartClass( | 234 static Dart_PersistentHandle dartClass( |
219 DartDOMData*, | 235 DartDOMData*, |
220 const char* dartImplementationClassName, intptr_t dartImplementationLibr
aryId, | 236 const char* dartImplementationClassName, intptr_t dartImplementationLibr
aryId, |
221 intptr_t classIndex); | 237 intptr_t classIndex); |
222 | 238 |
223 template <class BindingsClass> | 239 template <class BindingsClass> |
224 static Dart_PersistentHandle dartClass(DartDOMData* domData) | 240 static Dart_PersistentHandle dartClass(DartDOMData* domData) |
225 { | 241 { |
226 return dartClass( | 242 return dartClass( |
227 domData, | 243 domData, |
228 BindingsClass::dartImplementationClassName, BindingsClass::dartImple
mentationLibraryId, | 244 BindingsClass::dartImplementationClassName, BindingsClass::dartImple
mentationLibraryId, |
229 BindingsClass::dartClassId); | 245 BindingsClass::dartClassId); |
230 } | 246 } |
231 | 247 |
232 static Dart_PersistentHandle dartClass(const char* dartImplementationClassNa
me, | 248 static Dart_PersistentHandle dartClass(const char* dartImplementationClassNa
me, |
233 const char* dartImplementationLibraryName, intptr_t classIndex); | 249 const char* dartImplementationLibraryName, intptr_t classIndex); |
234 | 250 |
235 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer
) | 251 static void writeNativePointer(Dart_Handle wrapper, void* pointer, intptr_t
cid) |
236 { | 252 { |
237 Dart_Handle result = Dart_SetNativeInstanceField( | 253 Dart_Handle result = Dart_SetNativeInstanceField( |
238 wrapper, index, reinterpret_cast<intptr_t>(pointer)); | 254 wrapper, kNativeImplementationIndex, reinterpret_cast<intptr_t>(poin
ter)); |
239 UNUSED_PARAM(result); | 255 UNUSED_PARAM(result); |
240 ASSERT(!Dart_IsError(result)); | 256 ASSERT(!Dart_IsError(result)); |
| 257 result = Dart_SetNativeInstanceField(wrapper, kNativeTypeIndex, cid); |
| 258 ASSERT(!Dart_IsError(result)); |
241 } | 259 } |
242 | 260 |
243 static void* readNativePointer(Dart_Handle wrapper, int index) | 261 static void* readNativePointer(Dart_Handle wrapper, int index) |
244 { | 262 { |
245 intptr_t value; | 263 intptr_t value; |
246 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value)
; | 264 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value)
; |
247 // FIXME: the fact that we return 0 on error rather than asserting is | 265 // FIXME: the fact that we return 0 on error rather than asserting is |
248 // somewhat of a hack. We currently make this method return 0 because | 266 // somewhat of a hack. We currently make this method return 0 because |
249 // we reuse this method to verify that objects are actually native | 267 // we reuse this method to verify that objects are actually native |
250 // Node objects rather than objects that implement the Node interface. | 268 // Node objects rather than objects that implement the Node interface. |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 static DartMessagePortMap* domMap(DartDOMData* domData) { return domData
->messagePortMap(); } | 390 static DartMessagePortMap* domMap(DartDOMData* domData) { return domData
->messagePortMap(); } |
373 }; | 391 }; |
374 typedef MessagePortMapTraits MapTraits; | 392 typedef MessagePortMapTraits MapTraits; |
375 typedef DartDOMWrapperActiveTraits<DartMessagePort, false> ActiveTraits; | 393 typedef DartDOMWrapperActiveTraits<DartMessagePort, false> ActiveTraits; |
376 typedef DartDOMWrapperEventTargetTraits<DartMessagePort, true> EventTargetTr
aits; | 394 typedef DartDOMWrapperEventTargetTraits<DartMessagePort, true> EventTargetTr
aits; |
377 }; | 395 }; |
378 | 396 |
379 } | 397 } |
380 | 398 |
381 #endif // DartDOMWrapper_h | 399 #endif // DartDOMWrapper_h |
OLD | NEW |