| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2012 Google Inc. 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 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ |
| 31 |
| 32 #include "config.h" |
| 33 #include "bindings/core/dart/DartCustomElementWrapper.h" |
| 34 |
| 35 #include "bindings/core/dart/DartCustomElementLifecycleCallbacks.h" |
| 36 #include "bindings/core/dart/DartHTMLElement.h" |
| 37 #include "bindings/core/dart/DartSVGElement.h" |
| 38 #include "core/DartHTMLElementWrapperFactory.h" |
| 39 #include "core/DartSVGElementWrapperFactory.h" |
| 40 #include "core/dom/custom/CustomElement.h" |
| 41 #include "core/html/HTMLElement.h" |
| 42 #include "core/html/HTMLUnknownElement.h" |
| 43 #include "core/svg/SVGElement.h" |
| 44 |
| 45 namespace blink { |
| 46 |
| 47 template<typename ElementType> |
| 48 Dart_Handle createDartDirectWrapper(DartDOMData*, ElementType*); |
| 49 |
| 50 template<> |
| 51 Dart_Handle createDartDirectWrapper<HTMLElement>(DartDOMData* domData, HTMLEleme
nt* element) |
| 52 { |
| 53 return DartDOMWrapper::createWrapper<DartHTMLElement>(domData, element); |
| 54 } |
| 55 |
| 56 template<> |
| 57 Dart_Handle createDartDirectWrapper<SVGElement>(DartDOMData* domData, SVGElement
* element) |
| 58 { |
| 59 return DartDOMWrapper::createWrapper<DartSVGElement>(domData, element); |
| 60 } |
| 61 |
| 62 template<typename ElementType> |
| 63 Dart_Handle createDartFallbackWrapper(DartDOMData*, ElementType*); |
| 64 |
| 65 template<> |
| 66 Dart_Handle createDartFallbackWrapper<HTMLElement>(DartDOMData* domData, HTMLEle
ment* element) |
| 67 { |
| 68 return createDartHTMLFallbackWrapper(domData, toHTMLUnknownElement(element))
; |
| 69 } |
| 70 |
| 71 template<> |
| 72 Dart_Handle createDartFallbackWrapper<SVGElement>(DartDOMData* domData, SVGEleme
nt* element) |
| 73 { |
| 74 return createDartSVGFallbackWrapper(domData, element); |
| 75 } |
| 76 |
| 77 template<typename ElementType> |
| 78 Dart_Handle createUpgradeCandidateWrapper(ElementType* element, Dart_Handle (*cr
eateSpecificWrapper)(DartDOMData*, ElementType* element)) |
| 79 { |
| 80 DartDOMData* domData = DartDOMData::current(); |
| 81 if (CustomElement::isValidName(element->localName())) |
| 82 return createDartDirectWrapper(domData, element); |
| 83 if (createSpecificWrapper) |
| 84 return createSpecificWrapper(domData, element); |
| 85 return createDartFallbackWrapper(domData, element); |
| 86 } |
| 87 |
| 88 template<typename ElementType> |
| 89 Dart_Handle DartCustomElementWrapper<ElementType>::wrap(PassRefPtr<ElementType>
element, Dart_Handle (*createSpecificWrapper)(DartDOMData* domData, ElementType*
)) |
| 90 { |
| 91 if (!element->isUpgradedCustomElement()) |
| 92 return createUpgradeCandidateWrapper(element.get(), createSpecificWrappe
r); |
| 93 |
| 94 return upgradeDartWrapper(element.get(), createSpecificWrapper); |
| 95 } |
| 96 |
| 97 template<> |
| 98 Dart_Handle DartCustomElementWrapper<HTMLElement>::swapElementWrapper(DartDOMDat
a* domData, HTMLElement* element, Dart_Handle wrapperType, intptr_t nativeClassI
d) |
| 99 { |
| 100 Dart_WeakPersistentHandle oldInstance = DartDOMWrapper::lookupWrapper<DartHT
MLElement>( |
| 101 domData, reinterpret_cast<HTMLElement*>(element)); |
| 102 Dart_Handle oldWrapper = 0; |
| 103 if (oldInstance) { |
| 104 oldWrapper = Dart_HandleFromWeakPersistent(oldInstance); |
| 105 DartDOMWrapper::disassociateWrapper<DartHTMLElement>( |
| 106 domData, |
| 107 reinterpret_cast<HTMLElement*>(element), |
| 108 oldWrapper); |
| 109 } |
| 110 |
| 111 Dart_Handle newWrapper = Dart_Allocate(wrapperType); |
| 112 ASSERT(!Dart_IsError(newWrapper)); |
| 113 DartDOMWrapper::writeNativePointer(newWrapper, element, nativeClassId); |
| 114 |
| 115 Dart_Handle result = Dart_InvokeConstructor(newWrapper, Dart_NewStringFromCS
tring("created"), 0, 0); |
| 116 |
| 117 if (Dart_IsError(result)) { |
| 118 DartUtilities::reportProblem(domData->scriptExecutionContext(), result); |
| 119 |
| 120 // Fall back to the old wrapper if possible. |
| 121 if (oldWrapper) { |
| 122 DartDOMWrapper::associateWrapper<DartHTMLElement>(domData, element,
oldWrapper); |
| 123 return oldWrapper; |
| 124 } |
| 125 return result; |
| 126 } |
| 127 return newWrapper; |
| 128 } |
| 129 |
| 130 template<> |
| 131 Dart_Handle DartCustomElementWrapper<SVGElement>::swapElementWrapper(DartDOMData
* domData, SVGElement* element, Dart_Handle wrapperType, intptr_t nativeClassId) |
| 132 { |
| 133 // TODO: support SVG elements. |
| 134 ASSERT(FALSE); |
| 135 return Dart_Handle(); |
| 136 } |
| 137 |
| 138 template<> |
| 139 Dart_Handle DartCustomElementWrapper<HTMLElement>::upgradeDartWrapper(HTMLElemen
t* element, Dart_Handle (*createSpecificWrapper)(DartDOMData*, HTMLElement*)) |
| 140 { |
| 141 DartDOMData* domData = DartDOMData::current(); |
| 142 DartCustomElementBinding* binding = domData->customElementBinding(element->c
ustomElementDefinition()); |
| 143 if (!binding) |
| 144 return createUpgradeCandidateWrapper(element, createSpecificWrapper); |
| 145 |
| 146 Dart_PersistentHandle customType = binding->customType(); |
| 147 ASSERT(!Dart_IsError(customType)); |
| 148 |
| 149 Dart_Handle newWrapper = swapElementWrapper(domData, element, customType, bi
nding->nativeClassId()); |
| 150 if (Dart_IsError(newWrapper)) { |
| 151 // When the upgrade fails the failed wrapper may have been associated, |
| 152 // so we need to create a new one and re-associate it. |
| 153 Dart_Handle fallbackWrapper = createUpgradeCandidateWrapper(element, cre
ateSpecificWrapper); |
| 154 if (Dart_IsError(fallbackWrapper)) { |
| 155 // FIXME: We can reach here on a stack overflow. We should cascade t
he exception from here, but that will |
| 156 // require additional plumbing. We already reported the problem in s
wapElementWrapper so just return null |
| 157 // here. |
| 158 return Dart_Null(); |
| 159 } |
| 160 |
| 161 DartDOMWrapper::associateWrapper<DartHTMLElement>(domData, element, fall
backWrapper); |
| 162 DartDOMWrapper::writeNativePointer(fallbackWrapper, element, binding->na
tiveClassId()); |
| 163 return fallbackWrapper; |
| 164 } |
| 165 return newWrapper; |
| 166 } |
| 167 |
| 168 template<> |
| 169 Dart_Handle DartCustomElementWrapper<HTMLElement>::changeElementWrapper(Dart_Han
dle element, Dart_Handle wrapperType) |
| 170 { |
| 171 DartDOMData* domData = DartDOMData::current(); |
| 172 |
| 173 intptr_t nativeClassId = reinterpret_cast<intptr_t>(DartDOMWrapper::readNati
vePointer(element, DartDOMWrapper::NativeTypeIndex)); |
| 174 Dart_Handle exception = 0; |
| 175 HTMLElement* nativeElement = DartDOMWrapper::unwrapDartWrapper<DartHTMLEleme
nt>(domData, element, exception); |
| 176 if (exception) { |
| 177 return exception; |
| 178 } |
| 179 return swapElementWrapper(domData, nativeElement, wrapperType, nativeClassId
); |
| 180 } |
| 181 |
| 182 template<> |
| 183 Dart_Handle DartCustomElementWrapper<SVGElement>::changeElementWrapper(Dart_Hand
le element, Dart_Handle wrapperType) |
| 184 { |
| 185 // TODO: support SVG elements. |
| 186 ASSERT(FALSE); |
| 187 return Dart_Handle(); |
| 188 } |
| 189 |
| 190 template<> |
| 191 Dart_Handle DartCustomElementWrapper<SVGElement>::upgradeDartWrapper(SVGElement*
element, Dart_Handle (*createSpecificWrapper)(DartDOMData*, SVGElement*)) |
| 192 { |
| 193 // TODO: support SVG elements. |
| 194 ASSERT(FALSE); |
| 195 return Dart_Handle(); |
| 196 } |
| 197 |
| 198 template<> |
| 199 void DartCustomElementWrapper<HTMLElement>::initializeCustomElement(Dart_Handle
wrapper, Dart_Handle& exception) |
| 200 { |
| 201 DartDOMData* domData = DartDOMData::current(); |
| 202 HTMLElement* element = DartDOMWrapper::unwrapDartWrapper<DartHTMLElement>(do
mData, wrapper, exception); |
| 203 if (exception) { |
| 204 return; |
| 205 } |
| 206 if (!element) { |
| 207 exception = Dart_NewStringFromCString("created called outside of custom
element creation."); |
| 208 return; |
| 209 } |
| 210 |
| 211 DartDOMWrapper::associateWrapper<DartHTMLElement>(domData, element, wrapper)
; |
| 212 } |
| 213 |
| 214 template<> |
| 215 void DartCustomElementWrapper<SVGElement>::initializeCustomElement(Dart_Handle w
rapper, Dart_Handle& exception) |
| 216 { |
| 217 // TODO: support SVG elements. |
| 218 ASSERT(FALSE); |
| 219 } |
| 220 |
| 221 template |
| 222 class DartCustomElementWrapper<HTMLElement>; |
| 223 |
| 224 template |
| 225 class DartCustomElementWrapper<SVGElement>; |
| 226 |
| 227 } // namespace blink |
| OLD | NEW |