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

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

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