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

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

Issue 8802010: Dart bindings for WebKit (Closed) Base URL: http://svn.webkit.org/repository/webkit/trunk
Patch Set: Created 9 years 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
OLDNEW
(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 "DartScriptValueDeserializer.h"
34 #include "DartUtilities.h"
35 #include "EventListener.h"
36 #include "EventTarget.h"
37 #include "ExceptionCode.h"
38 #include "MediaQueryListListener.h"
39 #include "OptionsObject.h"
40 #include "Range.h"
41 #include "ScriptValue.h"
42 #include "SerializedScriptValue.h"
43 #if ENABLE(SVG)
44 #include "SVGPropertyTearOff.h"
45 #endif
46 #include "VoidCallback.h"
47 #include "npruntime_impl.h"
48
49 #include <bindings/npruntime.h>
50 #include <dart_api.h>
51 #include <wtf/PassRefPtr.h>
52 #include <wtf/RefPtr.h>
53 #include <wtf/text/AtomicString.h>
54 #include <wtf/text/WTFString.h>
55
56 namespace WebCore {
57
58 #if ENABLE(FILE_SYSTEM)
59 class Entry;
60 class EntrySync;
61 #endif
62
63 #if ENABLE(INDEXED_DATABASE)
64 class IDBAny;
65 class IDBKey;
66 #endif
67
68 #if ENABLE(SVG)
69 class SVGPathSeg;
70 #endif
71
72 enum ConversionFlag {
73 ConvertNone = 0,
74 ConvertDefaultToNull = 1
75 };
76
77 // The only purpose of this template declaration is to prohibit implicit
78 // conversions of toDartValue parameter.
79 template <typename Type>
80 Dart_Handle toDartValue(Type value);
81
82 template <typename Type>
83 Dart_Handle toDartValue(Type value, ConversionFlag);
84
85 template <typename Type>
86 inline Dart_Handle toDartValue(PassRefPtr<Type> value)
87 {
88 return toDartValue(value.get());
89 }
90
91 inline Dart_Handle toDartValue(const AtomicString& value, ConversionFlag flag = ConvertNone)
92 {
93 if ((flag & ConvertDefaultToNull) && value.isNull())
94 return 0;
95 return DartUtilities::stringToDartString(value);
96 }
97
98 inline Dart_Handle toDartValue(const String& value, ConversionFlag flag = Conver tNone)
99 {
100 if ((flag & ConvertDefaultToNull) && value.isNull())
101 return 0;
102 return DartUtilities::stringToDartString(value);
103 }
104
105 inline Dart_Handle toDartValue(bool value)
106 {
107 return Dart_NewBoolean(value);
108 }
109
110 inline Dart_Handle toDartValue(unsigned long long value)
111 {
112 // FIXME: WebIDL unsigned long long is guaranteed to fit into 64-bit unsigne d,
113 // so we need a dart API for constructing an integer from uint64_t.
114 ASSERT(value <= 0x7fffffffffffffffLL);
115 return Dart_NewInteger(static_cast<int64_t>(value));
116 }
117
118 inline Dart_Handle toDartValue(int64_t value)
119 {
120 return Dart_NewInteger(value);
121 }
122
123 inline Dart_Handle toDartValue(intptr_t value)
124 {
125 return Dart_NewInteger(value);
126 }
127
128 inline Dart_Handle toDartValue(float value)
129 {
130 return Dart_NewDouble(value);
131 }
132
133 inline Dart_Handle toDartValue(double value)
134 {
135 return Dart_NewDouble(value);
136 }
137
138 inline Dart_Handle toDartValue(const KURL& value, ConversionFlag flag = ConvertN one)
139 {
140 return toDartValue(value.string(), flag);
141 }
142
143 inline Dart_Handle toDartValue(const ScriptValue& value)
144 {
145 // FIXME: dart specific implementation of ScriptValue and
146 // proper conversion.
147 DART_UNIMPLEMENTED();
148 return 0;
149 }
150
151 inline Dart_Handle toDartValue(SerializedScriptValue* value)
152 {
153 // FIXME: better error handling.
154 DartScriptValueDeserializer deserializer;
155 if (!value->deserialize(&deserializer))
156 return 0;
157 return deserializer.result();
158 }
159
160 Dart_Handle toDartValue(NPObject* object);
161
162 class ContainerNode;
163 Dart_Handle toDartValue(ContainerNode*);
164
165 class CSSMutableStyleDeclaration;
166 Dart_Handle toDartValue(CSSMutableStyleDeclaration*);
167
168 class HTMLFormControlElement;
169 Dart_Handle toDartValue(HTMLFormControlElement*);
170
171 class DartDOMWrapper {
172 public:
173 template <class WebkitClass>
174 static Dart_Handle newWrapper(const char* className, WebkitClass* domObject)
175 {
176 Dart_Handle wrapper = instantiateWrapper(className);
177 installNativePointers(domObject, wrapper);
178 return wrapper;
179 }
180
181 template <class BindingsClass>
182 static Dart_Handle toDart(typename BindingsClass::NativeType* instance)
183 {
184 return toDart(instance, BindingsClass::dartImplementationClassName);
185 }
186
187 template <class WebkitClass>
188 static Dart_Handle toDart(WebkitClass* instance, const char* className)
189 {
190 if (!instance)
191 return 0;
192
193 // FIXME: custom toDartValue implementations should check the cache firs t.
194 // FIXME: separate maps for nodes, dom objects and active dom objects.
195 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate();
196 Dart_Handle wrapper = domMap->get(instance);
197 if (wrapper)
198 return wrapper;
199
200 wrapper = instantiateWrapper(className);
201 bindDOMObjectToDartWrapper(instance, wrapper);
202 return wrapper;
203 }
204
205 template <class WebkitClass>
206 static void bindDOMObjectToDartWrapper(WebkitClass* domObject, Dart_Handle w rapper)
207 {
208 ASSERT(domObject);
209 domObject->ref();
210 installNativePointers(domObject, wrapper);
211
212 // FIXME: make persistent handle weak and deref domObject in weak callba ck.
213 Dart_Handle persistentWrapperHandle = Dart_NewPersistentHandle(wrapper);
214
215 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate();
216 ASSERT(!domMap->contains(domObject));
217 domMap->set(domObject, persistentWrapperHandle);
218 }
219
220 template <class BindingsClass>
221 static typename BindingsClass::NativeType* unwrapDartWrapper(Dart_Handle wra pper, Dart_Handle& exception)
222 {
223 // FIXME: support cross-domain wrappers.
224 if (!instanceOf(BindingsClass::dartImplementationClassName, wrapper, exc eption))
225 return 0;
226 ASSERT(!exception);
227 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn dex);
228 return reinterpret_cast<typename BindingsClass::NativeType*>(nativePoint er);
229 }
230
231 static void derefDOMObject(Dart_Handle wrapper, void* domObject);
232
233 template <class WebkitClass>
234 static WebkitClass* receiver(Dart_NativeArguments args)
235 {
236 // Type of receiver is ensured by Dart VM runtime, so bypass additional checks.
237 void* nativePointer = readNativePointer(Dart_GetNativeArgument(args, 0), kNativeImplementationIndex);
238 WebkitClass* const recv = static_cast<WebkitClass*>(nativePointer);
239 ASSERT(recv); // Never should return 0.
240 return recv;
241 }
242
243 template <typename Type>
244 static void returnValue(Dart_NativeArguments args, const Type& value)
245 {
246 Dart_Handle result = toDartValue(value);
247 if (result)
248 Dart_SetReturnValue(args, result);
249 }
250
251 static int wrapperNativeFieldCount() { return kNativeFieldCount; }
252
253 template <typename Type>
254 static void returnValue(Dart_NativeArguments args, const Type& value, Conver sionFlag flag)
255 {
256 Dart_Handle result = toDartValue(value, flag);
257 if (result)
258 Dart_SetReturnValue(args, result);
259 }
260
261 static Dart_Handle exceptionCodeToDartException(ExceptionCode);
262
263 private:
264 enum NativeFieldIndices {
265 kNativeImplementationIndex = 0,
266 kDerefObjectFunctionIndex,
267 kNativeFieldCount
268 };
269
270 static Dart_Handle instantiateWrapper(const char* className);
271 static bool instanceOf(const char* dartImplementationClassName, Dart_Handle wrapper, Dart_Handle& exception);
272
273 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer )
274 {
275 DartApiScope scope;
276 Dart_Handle result = Dart_SetNativeInstanceField(wrapper, index, reinter pret_cast<intptr_t>(pointer));
277 UNUSED_PARAM(result);
278 ASSERT(!Dart_IsError(result));
279 }
280
281 static void* readNativePointer(Dart_Handle wrapper, int index)
282 {
283 // FIXME: Try to remove this scope from the hot path.
284 DartApiScope scope;
285 intptr_t value;
286 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value) ;
287 ASSERT(!Dart_IsError(result));
288 UNUSED_PARAM(result);
289 return reinterpret_cast<void*>(value);
290 }
291
292 template <class WebkitClass>
293 static void installNativePointers(WebkitClass* domObject, Dart_Handle wrappe r)
294 {
295 ASSERT(domObject);
296 DerefObjectFunction derefObjectFunction = &DartDOMWrapper::derefObject<W ebkitClass>;
297 writeNativePointer(wrapper, kNativeImplementationIndex, domObject);
298 writeNativePointer(wrapper, kDerefObjectFunctionIndex, reinterpret_cast< void*>(derefObjectFunction));
299 }
300
301 typedef void (*DerefObjectFunction)(void*);
302
303 template<typename T>
304 static void derefObject(void* pointer)
305 {
306 static_cast<T*>(pointer)->deref();
307 }
308 };
309
310 template<>
311 inline void DartDOMWrapper::derefObject<NPObject>(void*)
312 {
313 // FIXME: proper deref.
314 }
315
316 // ParameterAdapter.
317
318 template <typename Value>
319 class ParameterAdapterBase {
320 public:
321 bool conversionSuccessful() const { return !m_exception; }
322
323 Dart_Handle exception() const
324 {
325 ASSERT(!conversionSuccessful());
326 return m_exception;
327 }
328
329 protected:
330
331 ParameterAdapterBase()
332 : m_value()
333 , m_exception(0) { }
334
335 void setException(Dart_Handle exception)
336 {
337 ASSERT(exception);
338 m_exception = exception;
339 ASSERT(!conversionSuccessful());
340 }
341
342 const Value& value() const
343 {
344 ASSERT(conversionSuccessful());
345 return m_value;
346 }
347
348 void setValue(Value value)
349 {
350 m_value = value;
351 }
352
353 protected:
354 Value m_value;
355
356 private:
357 Dart_Handle m_exception;
358 };
359
360
361 template <class NativeValue, class DartBindingsClass = void>
362 class ParameterAdapter : public ParameterAdapterBase<NativeValue*> {
363 public:
364 explicit ParameterAdapter(Dart_Handle handle)
365 {
366 Dart_Handle exception = 0;
367 this->setValue(DartBindingsClass::toNative(handle, exception).get());
368 if (exception)
369 this->setException(exception);
370 }
371
372 operator PassRefPtr<NativeValue>() const { return this->value(); }
373 operator NativeValue*() const { return this->value(); }
374 };
375
376 template <>
377 class ParameterAdapter< RefPtr<EventListener> > : public ParameterAdapterBase< R efPtr<EventListener> > {
378 public:
379 explicit ParameterAdapter(Dart_Handle handle)
380 {
381 // FIXME: abstract away this common pattern of exception management in c onversion.
382 Dart_Handle exception = 0;
383 this->setValue(DartUtilities::toEventListener(handle, exception));
384 if (exception)
385 this->setException(exception);
386 }
387 operator EventListener*() const { return this->value().get(); }
388 operator PassRefPtr<EventListener>() const { return this->value(); }
389 };
390
391 template <>
392 class ParameterAdapter< RefPtr<EventTarget> > : public ParameterAdapterBase< Ref Ptr<EventTarget> > {
393 public:
394 explicit ParameterAdapter(Dart_Handle handle)
395 {
396 // FIXME: abstract away this common pattern of exception management in c onversion.
397 Dart_Handle exception = 0;
398 this->setValue(DartUtilities::toEventTarget(handle, exception));
399 if (exception)
400 this->setException(exception);
401 }
402 operator EventTarget*() const { return this->value().get(); }
403 operator PassRefPtr<EventTarget>() const { return this->value(); }
404 };
405
406 template <>
407 class ParameterAdapter< RefPtr<MediaQueryListListener> > : public ParameterAdapt erBase<MediaQueryListListener*> {
408 public:
409 explicit ParameterAdapter(Dart_Handle handle)
410 {
411 setException(Dart_NewString("Not supported yet"));
412 }
413 operator MediaQueryListListener*() const { return this->value(); }
414 operator PassRefPtr<MediaQueryListListener>() const { return this->value(); }
415 };
416
417 template <>
418 class ParameterAdapter< RefPtr<SerializedScriptValue> > : public ParameterAdapte rBase< RefPtr<SerializedScriptValue> > {
419 public:
420 explicit ParameterAdapter(Dart_Handle handle)
421 {
422 // FIXME: abstract away this common pattern of exception management in c onversion.
423 Dart_Handle exception = 0;
424 this->setValue(DartUtilities::toSerializedScriptValue(handle, exception) );
425 if (exception)
426 this->setException(exception);
427 }
428 operator SerializedScriptValue*() const { return this->value().get(); }
429 operator PassRefPtr<SerializedScriptValue>() const { return this->value(); }
430 PassRefPtr<SerializedScriptValue> release() { return m_value->release(); }
431 };
432
433 template <>
434 class ParameterAdapter< RefPtr<VoidCallback> > : public ParameterAdapterBase<Voi dCallback*> {
435 public:
436 explicit ParameterAdapter(Dart_Handle handle)
437 {
438 setException(Dart_NewString("Not supported yet"));
439 }
440 operator VoidCallback*() const { return this->value(); }
441 operator PassRefPtr<VoidCallback>() const { return this->value(); }
442 };
443
444 #if ENABLE(SVG)
445 template <class NativeValue, class DartBindingsClass>
446 class ParameterAdapter< RefPtr<SVGPropertyTearOff<NativeValue> >, DartBindingsCl ass> : public ParameterAdapterBase< RefPtr<SVGPropertyTearOff<NativeValue> > > {
447 public:
448 explicit ParameterAdapter(Dart_Handle handle)
449 {
450 Dart_Handle exception = 0;
451 this->setValue(DartBindingsClass::toNative(handle, exception).get());
452 if (exception)
453 this->setException(exception);
454 }
455 operator const NativeValue&() const { return this->value()->propertyReferenc e(); }
456 operator NativeValue&() { return this->value()->propertyReference(); }
457 operator PassRefPtr<SVGPropertyTearOff<NativeValue> >() const { return this- >value(); }
458 operator SVGPropertyTearOff<NativeValue>*() const { return this->value().get (); }
459 };
460 #endif // ENABLE(SVG)
461
462 template <class NativeValue, class DartBindingsClass>
463 class ParameterAdapter< RefPtr<NativeValue>, DartBindingsClass > : public Parame terAdapterBase< RefPtr<NativeValue> > {
464 public:
465 explicit ParameterAdapter(Dart_Handle handle)
466 {
467 Dart_Handle exception = 0;
468 this->setValue(DartBindingsClass::toNative(handle, exception));
469 if (exception)
470 this->setException(exception);
471 }
472
473 operator PassRefPtr<NativeValue>() const { return this->value(); }
474 operator RefPtr<NativeValue>() const { return this->value(); }
475 operator NativeValue*() const { return this->value().get(); }
476 };
477
478 class DoubleParameterAdapter : public ParameterAdapterBase<double> {
479 protected:
480 explicit DoubleParameterAdapter(Dart_Handle handle)
481 {
482 Dart_Handle exception = 0;
483 setValue(DartUtilities::toDouble(handle, exception));
484 if (exception)
485 setException(exception);
486 }
487 };
488
489 template <>
490 class ParameterAdapter<double> : public DoubleParameterAdapter {
491 public:
492 explicit ParameterAdapter(Dart_Handle handle)
493 : DoubleParameterAdapter(handle) { }
494 operator double() const { return this->value(); }
495 };
496
497 template <>
498 class ParameterAdapter<float> : public DoubleParameterAdapter {
499 public:
500 explicit ParameterAdapter(Dart_Handle handle)
501 : DoubleParameterAdapter(handle) { }
502 operator float() const { return this->value(); }
503 };
504
505 class IntParameterAdapter : public ParameterAdapterBase<int64_t> {
506 protected:
507 explicit IntParameterAdapter(Dart_Handle handle)
508 {
509 Dart_Handle exception = 0;
510 setValue(DartUtilities::toInteger(handle, exception));
511 if (exception)
512 setException(exception);
513 }
514 };
515
516 template <>
517 class ParameterAdapter<int> : public IntParameterAdapter {
518 public:
519 explicit ParameterAdapter(Dart_Handle handle)
520 : IntParameterAdapter(handle)
521 {
522 if (conversionSuccessful() && (value() < INT_MIN || value() > INT_MAX))
523 setException(Dart_NewString("value out of range"));
524 }
525 operator int() const { return this->value(); }
526 };
527
528 template <>
529 class ParameterAdapter<long long> : public IntParameterAdapter {
530 public:
531 explicit ParameterAdapter(Dart_Handle handle)
532 : IntParameterAdapter(handle) { }
533 // FIXME: proper processing of longer integer values.
534 operator long long() const { return this->value(); }
535 };
536
537 template <>
538 class ParameterAdapter<unsigned int> : public IntParameterAdapter {
539 public:
540 explicit ParameterAdapter(Dart_Handle handle)
541 : IntParameterAdapter(handle)
542 {
543 if (conversionSuccessful() && (value() < 0 || value() > UINT_MAX))
544 setException(Dart_NewString("value out of range"));
545 }
546 operator unsigned int() const { return this->value(); }
547 };
548
549 template <>
550 class ParameterAdapter<unsigned long long> : public IntParameterAdapter {
551 public:
552 explicit ParameterAdapter(Dart_Handle handle)
553 : IntParameterAdapter(handle)
554 {
555 if (conversionSuccessful() && value() < 0)
556 setException(Dart_NewString("value out of range"));
557 }
558 // FIXME: proper processing of longer integer values.
559 operator unsigned long long() const { return this->value(); }
560 };
561
562 // FIXME: [performance] need better treatment of String vs. AtomicString, cf. v8 implementation.
563 template <>
564 class ParameterAdapter<String> : public ParameterAdapterBase< RefPtr<StringImpl> > {
565 public:
566 explicit ParameterAdapter(Dart_Handle handle, DartUtilities::ConversionFlag flag = DartUtilities::ConvertNone)
567 {
568 Dart_Handle exception = 0;
569 setValue(DartUtilities::toStringImpl(handle, flag, exception));
570 if (exception)
571 setException(exception);
572 }
573 operator String() const { return String(this->value()); }
574 operator AtomicString() const { return AtomicString(this->value()); }
575 };
576
577 template <>
578 class ParameterAdapter<bool> : public ParameterAdapterBase<bool> {
579 public:
580 explicit ParameterAdapter(Dart_Handle handle)
581 {
582 Dart_Handle exception = 0;
583 setValue(DartUtilities::toBool(handle, exception));
584 if (exception)
585 setException(exception);
586 }
587 operator bool() const { return this->value(); }
588 };
589
590 template <>
591 class ParameterAdapter<ScriptValue> : public ParameterAdapterBase<ScriptValue> {
592 public:
593 explicit ParameterAdapter(Dart_Handle handle)
594 {
595 setException(Dart_NewString("Not supported yet"));
596 }
597 operator ScriptValue() const { return this->value(); }
598 };
599
600 template <>
601 class ParameterAdapter<OptionsObject> : public ParameterAdapterBase<OptionsObjec t> {
602 public:
603 explicit ParameterAdapter(Dart_Handle handle)
604 {
605 setException(Dart_NewString("Not supported yet"));
606 }
607 operator OptionsObject() const { return this->value(); }
608 };
609
610 template <>
611 class ParameterAdapter<Range::CompareHow> : public ParameterAdapterBase<Range::C ompareHow> {
612 public:
613 explicit ParameterAdapter(Dart_Handle handle)
614 {
615 setException(Dart_NewString("Not supported yet"));
616 }
617 operator Range::CompareHow() const { return this->value(); }
618 };
619
620 }
621
622 #endif // DartDOMWrapper_h
OLDNEW
« no previous file with comments | « Source/WebCore/bindings/dart/DartController.cpp ('k') | Source/WebCore/bindings/dart/DartDOMWrapper.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698