| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 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 * 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 | |
| 31 #include "config.h" | |
| 32 #include "bindings/core/dart/DartCustomElementLifecycleCallbacks.h" | |
| 33 | |
| 34 #include "bindings/core/dart/DartCustomElementWrapper.h" | |
| 35 #include "bindings/core/dart/DartDOMData.h" | |
| 36 #include "bindings/core/dart/DartDOMWrapper.h" | |
| 37 #include "bindings/core/dart/DartHTMLElement.h" | |
| 38 #include "bindings/core/dart/DartScriptState.h" | |
| 39 #include "bindings/core/dart/DartUtilities.h" | |
| 40 #include "bindings/core/v8/V8Binding.h" | |
| 41 #include "core/dom/Element.h" | |
| 42 #include "core/dom/ExecutionContext.h" | |
| 43 #include "wtf/PassOwnPtr.h" | |
| 44 | |
| 45 | |
| 46 namespace blink { | |
| 47 | |
| 48 PassRefPtr<DartCustomElementLifecycleCallbacks> DartCustomElementLifecycleCallba
cks::create(DartScriptState* scriptState) | |
| 49 { | |
| 50 return adoptRef(new DartCustomElementLifecycleCallbacks(scriptState)); | |
| 51 } | |
| 52 | |
| 53 static CustomElementLifecycleCallbacks::CallbackType flagSet() | |
| 54 { | |
| 55 int flags = CustomElementLifecycleCallbacks::Created | CustomElementLifecycl
eCallbacks::Attached | CustomElementLifecycleCallbacks::Detached | CustomElement
LifecycleCallbacks::AttributeChanged; | |
| 56 | |
| 57 return CustomElementLifecycleCallbacks::CallbackType(flags); | |
| 58 } | |
| 59 | |
| 60 DartCustomElementLifecycleCallbacks::DartCustomElementLifecycleCallbacks(DartScr
iptState* scriptState) | |
| 61 : CustomElementLifecycleCallbacks(flagSet()) | |
| 62 , m_scriptState(scriptState) | |
| 63 , m_owner(0) | |
| 64 { | |
| 65 } | |
| 66 | |
| 67 DartCustomElementLifecycleCallbacks::~DartCustomElementLifecycleCallbacks() | |
| 68 { | |
| 69 if (!m_owner || !Dart_CurrentIsolate()) | |
| 70 return; | |
| 71 | |
| 72 DartDOMData::current()->clearCustomElementBinding(m_owner); | |
| 73 } | |
| 74 | |
| 75 | |
| 76 bool DartCustomElementLifecycleCallbacks::setBinding(CustomElementDefinition* ow
ner, PassOwnPtr<DartCustomElementBinding> binding) | |
| 77 { | |
| 78 ASSERT(!m_owner); | |
| 79 m_owner = owner; | |
| 80 | |
| 81 DartDOMData::current()->addCustomElementBinding(owner, binding); | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 void DartCustomElementLifecycleCallbacks::created(Element* element) | |
| 86 { | |
| 87 DartIsolateScope isolateScope(m_scriptState->isolate()); | |
| 88 DartApiScope dartApiScope; | |
| 89 | |
| 90 element->setCustomElementState(Element::Upgraded); | |
| 91 | |
| 92 DartCustomElementWrapper<HTMLElement>::upgradeDartWrapper(reinterpret_cast<H
TMLElement*>(element), 0); | |
| 93 } | |
| 94 | |
| 95 void DartCustomElementLifecycleCallbacks::attached(Element* element) | |
| 96 { | |
| 97 // TODO(blois): cache this method name to avoid re-creating handles. | |
| 98 call("attached", element); | |
| 99 } | |
| 100 | |
| 101 void DartCustomElementLifecycleCallbacks::detached(Element* element) | |
| 102 { | |
| 103 // TODO(blois): cache this method name to avoid re-creating handles. | |
| 104 call("detached", element); | |
| 105 } | |
| 106 | |
| 107 void DartCustomElementLifecycleCallbacks::attributeChanged(Element* element, con
st AtomicString& name, const AtomicString& oldValue, const AtomicString& newValu
e) | |
| 108 { | |
| 109 DartIsolateScope isolateScope(m_scriptState->isolate()); | |
| 110 DartApiScope dartApiScope; | |
| 111 | |
| 112 DartDOMData* domData = DartDOMData::current(); | |
| 113 | |
| 114 Dart_WeakPersistentHandle wrapper = DartDOMWrapper::lookupWrapper<DartHTMLEl
ement>(domData, reinterpret_cast<HTMLElement*>(element)); | |
| 115 if (!wrapper) { | |
| 116 return; | |
| 117 } | |
| 118 Dart_Handle receiver = Dart_HandleFromWeakPersistent(wrapper); | |
| 119 | |
| 120 Dart_Handle args[3] = { | |
| 121 DartUtilities::stringToDartString(name), | |
| 122 oldValue.isNull() ? Dart_Null() : DartUtilities::stringToDartString(oldV
alue), | |
| 123 newValue.isNull() ? Dart_Null() : DartUtilities::stringToDartString(newV
alue) | |
| 124 }; | |
| 125 | |
| 126 // TODO(blois): cache this method name to avoid re-creating handles. | |
| 127 Dart_Handle result = Dart_Invoke(receiver, Dart_NewStringFromCString("attrib
uteChanged"), 3, args); | |
| 128 if (Dart_IsError(result)) { | |
| 129 DartUtilities::reportProblem(DartUtilities::scriptExecutionContext(), re
sult); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 void DartCustomElementLifecycleCallbacks::call(const char* methodName, Element*
element) | |
| 134 { | |
| 135 DartIsolateScope isolateScope(m_scriptState->isolate()); | |
| 136 DartApiScope dartApiScope; | |
| 137 | |
| 138 DartDOMData* domData = DartDOMData::current(); | |
| 139 | |
| 140 Dart_WeakPersistentHandle wrapper = DartDOMWrapper::lookupWrapper<DartHTMLEl
ement>(domData, reinterpret_cast<HTMLElement*>(element)); | |
| 141 if (!wrapper) { | |
| 142 return; | |
| 143 } | |
| 144 Dart_Handle receiver = Dart_HandleFromWeakPersistent(wrapper); | |
| 145 Dart_Handle result = Dart_Invoke(receiver, Dart_NewStringFromCString(methodN
ame), 0, 0); | |
| 146 if (Dart_IsError(result)) { | |
| 147 DartUtilities::reportProblem(DartUtilities::scriptExecutionContext(), re
sult); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 } // namespace blink | |
| OLD | NEW |