| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * Copyright (C) 2013 Google Inc. All rights reserved. | 2  * Copyright (C) 2013 Google Inc. 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 10 matching lines...) Expand all  Loading... | 
| 21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| 22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 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 | 30 | 
| 31 #include "bindings/core/v8/V8CustomElementLifecycleCallbacks.h" | 31 #include "bindings/core/v8/V8V0CustomElementLifecycleCallbacks.h" | 
| 32 | 32 | 
| 33 #include "bindings/core/v8/CustomElementBinding.h" |  | 
| 34 #include "bindings/core/v8/DOMDataStore.h" | 33 #include "bindings/core/v8/DOMDataStore.h" | 
| 35 #include "bindings/core/v8/ScriptController.h" | 34 #include "bindings/core/v8/ScriptController.h" | 
|  | 35 #include "bindings/core/v8/V0CustomElementBinding.h" | 
| 36 #include "bindings/core/v8/V8Binding.h" | 36 #include "bindings/core/v8/V8Binding.h" | 
| 37 #include "bindings/core/v8/V8Element.h" | 37 #include "bindings/core/v8/V8Element.h" | 
| 38 #include "bindings/core/v8/V8HiddenValue.h" | 38 #include "bindings/core/v8/V8HiddenValue.h" | 
| 39 #include "bindings/core/v8/V8PerContextData.h" | 39 #include "bindings/core/v8/V8PerContextData.h" | 
| 40 #include "core/dom/ExecutionContext.h" | 40 #include "core/dom/ExecutionContext.h" | 
| 41 #include "wtf/PassOwnPtr.h" | 41 #include "wtf/PassOwnPtr.h" | 
| 42 | 42 | 
| 43 namespace blink { | 43 namespace blink { | 
| 44 | 44 | 
| 45 #define CALLBACK_LIST(V)                    \ | 45 #define CALLBACK_LIST(V)                    \ | 
| 46     V(created, CreatedCallback)             \ | 46     V(created, CreatedCallback)             \ | 
| 47     V(attached, AttachedCallback)           \ | 47     V(attached, AttachedCallback)           \ | 
| 48     V(detached, DetachedCallback)           \ | 48     V(detached, DetachedCallback)           \ | 
| 49     V(attributeChanged, AttributeChangedCallback) | 49     V(attributeChanged, AttributeChangedCallback) | 
| 50 | 50 | 
| 51 V8CustomElementLifecycleCallbacks* V8CustomElementLifecycleCallbacks::create(Scr
     iptState* scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::Funct
     ion> created, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Function
     > detached, v8::MaybeLocal<v8::Function> attributeChanged) | 51 V8V0CustomElementLifecycleCallbacks* V8V0CustomElementLifecycleCallbacks::create
     (ScriptState* scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::F
     unction> created, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Func
     tion> detached, v8::MaybeLocal<v8::Function> attributeChanged) | 
| 52 { | 52 { | 
| 53     v8::Isolate* isolate = scriptState->isolate(); | 53     v8::Isolate* isolate = scriptState->isolate(); | 
| 54     // A given object can only be used as a Custom Element prototype | 54     // A given object can only be used as a Custom Element prototype | 
| 55     // once; see customElementIsInterfacePrototypeObject | 55     // once; see customElementIsInterfacePrototypeObject | 
| 56 #define SET_HIDDEN_VALUE(Value, Name) \ | 56 #define SET_HIDDEN_VALUE(Value, Name) \ | 
| 57     ASSERT(V8HiddenValue::getHiddenValue(scriptState, prototype, V8HiddenValue::
     customElement##Name(isolate)).IsEmpty()); \ | 57     ASSERT(V8HiddenValue::getHiddenValue(scriptState, prototype, V8HiddenValue::
     customElement##Name(isolate)).IsEmpty()); \ | 
| 58     if (!Value.IsEmpty()) \ | 58     if (!Value.IsEmpty()) \ | 
| 59         V8HiddenValue::setHiddenValue(scriptState, prototype, V8HiddenValue::cus
     tomElement##Name(isolate), Value.ToLocalChecked()); | 59         V8HiddenValue::setHiddenValue(scriptState, prototype, V8HiddenValue::cus
     tomElement##Name(isolate), Value.ToLocalChecked()); | 
| 60 | 60 | 
| 61     CALLBACK_LIST(SET_HIDDEN_VALUE) | 61     CALLBACK_LIST(SET_HIDDEN_VALUE) | 
| 62 #undef SET_HIDDEN_VALUE | 62 #undef SET_HIDDEN_VALUE | 
| 63 | 63 | 
| 64     return new V8CustomElementLifecycleCallbacks(scriptState, prototype, created
     , attached, detached, attributeChanged); | 64     return new V8V0CustomElementLifecycleCallbacks(scriptState, prototype, creat
     ed, attached, detached, attributeChanged); | 
| 65 } | 65 } | 
| 66 | 66 | 
| 67 static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::MaybeLocal<v8::
     Function> attached, v8::MaybeLocal<v8::Function> detached, v8::MaybeLocal<v8::Fu
     nction> attributeChanged) | 67 static V0CustomElementLifecycleCallbacks::CallbackType flagSet(v8::MaybeLocal<v8
     ::Function> attached, v8::MaybeLocal<v8::Function> detached, v8::MaybeLocal<v8::
     Function> attributeChanged) | 
| 68 { | 68 { | 
| 69     // V8 Custom Elements always run created to swizzle prototypes. | 69     // V8 Custom Elements always run created to swizzle prototypes. | 
| 70     int flags = CustomElementLifecycleCallbacks::CreatedCallback; | 70     int flags = V0CustomElementLifecycleCallbacks::CreatedCallback; | 
| 71 | 71 | 
| 72     if (!attached.IsEmpty()) | 72     if (!attached.IsEmpty()) | 
| 73         flags |= CustomElementLifecycleCallbacks::AttachedCallback; | 73         flags |= V0CustomElementLifecycleCallbacks::AttachedCallback; | 
| 74 | 74 | 
| 75     if (!detached.IsEmpty()) | 75     if (!detached.IsEmpty()) | 
| 76         flags |= CustomElementLifecycleCallbacks::DetachedCallback; | 76         flags |= V0CustomElementLifecycleCallbacks::DetachedCallback; | 
| 77 | 77 | 
| 78     if (!attributeChanged.IsEmpty()) | 78     if (!attributeChanged.IsEmpty()) | 
| 79         flags |= CustomElementLifecycleCallbacks::AttributeChangedCallback; | 79         flags |= V0CustomElementLifecycleCallbacks::AttributeChangedCallback; | 
| 80 | 80 | 
| 81     return CustomElementLifecycleCallbacks::CallbackType(flags); | 81     return V0CustomElementLifecycleCallbacks::CallbackType(flags); | 
| 82 } | 82 } | 
| 83 | 83 | 
| 84 template <typename T> | 84 template <typename T> | 
| 85 static void weakCallback(const v8::WeakCallbackInfo<ScopedPersistent<T>>& data) | 85 static void weakCallback(const v8::WeakCallbackInfo<ScopedPersistent<T>>& data) | 
| 86 { | 86 { | 
| 87     data.GetParameter()->clear(); | 87     data.GetParameter()->clear(); | 
| 88 } | 88 } | 
| 89 | 89 | 
| 90 V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ScriptState
     * scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::Function> cre
     ated, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Function> detach
     ed, v8::MaybeLocal<v8::Function> attributeChanged) | 90 V8V0CustomElementLifecycleCallbacks::V8V0CustomElementLifecycleCallbacks(ScriptS
     tate* scriptState, v8::Local<v8::Object> prototype, v8::MaybeLocal<v8::Function>
      created, v8::MaybeLocal<v8::Function> attached, v8::MaybeLocal<v8::Function> de
     tached, v8::MaybeLocal<v8::Function> attributeChanged) | 
| 91     : CustomElementLifecycleCallbacks(flagSet(attached, detached, attributeChang
     ed)) | 91     : V0CustomElementLifecycleCallbacks(flagSet(attached, detached, attributeCha
     nged)) | 
| 92     , ContextLifecycleObserver(scriptState->getExecutionContext()) | 92     , ContextLifecycleObserver(scriptState->getExecutionContext()) | 
| 93     , m_scriptState(scriptState) | 93     , m_scriptState(scriptState) | 
| 94     , m_prototype(scriptState->isolate(), prototype) | 94     , m_prototype(scriptState->isolate(), prototype) | 
| 95     , m_created(scriptState->isolate(), created) | 95     , m_created(scriptState->isolate(), created) | 
| 96     , m_attached(scriptState->isolate(), attached) | 96     , m_attached(scriptState->isolate(), attached) | 
| 97     , m_detached(scriptState->isolate(), detached) | 97     , m_detached(scriptState->isolate(), detached) | 
| 98     , m_attributeChanged(scriptState->isolate(), attributeChanged) | 98     , m_attributeChanged(scriptState->isolate(), attributeChanged) | 
| 99 { | 99 { | 
| 100     m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>); | 100     m_prototype.setWeak(&m_prototype, weakCallback<v8::Object>); | 
| 101 | 101 | 
| 102 #define MAKE_WEAK(Var, _) \ | 102 #define MAKE_WEAK(Var, _) \ | 
| 103     if (!m_##Var.isEmpty()) \ | 103     if (!m_##Var.isEmpty()) \ | 
| 104         m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>); | 104         m_##Var.setWeak(&m_##Var, weakCallback<v8::Function>); | 
| 105 | 105 | 
| 106     CALLBACK_LIST(MAKE_WEAK) | 106     CALLBACK_LIST(MAKE_WEAK) | 
| 107 #undef MAKE_WEAK | 107 #undef MAKE_WEAK | 
| 108 } | 108 } | 
| 109 | 109 | 
| 110 V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData() | 110 V8PerContextData* V8V0CustomElementLifecycleCallbacks::creationContextData() | 
| 111 { | 111 { | 
| 112     if (!getExecutionContext()) | 112     if (!getExecutionContext()) | 
| 113         return 0; | 113         return 0; | 
| 114 | 114 | 
| 115     v8::Local<v8::Context> context = m_scriptState->context(); | 115     v8::Local<v8::Context> context = m_scriptState->context(); | 
| 116     if (context.IsEmpty()) | 116     if (context.IsEmpty()) | 
| 117         return 0; | 117         return 0; | 
| 118 | 118 | 
| 119     return V8PerContextData::from(context); | 119     return V8PerContextData::from(context); | 
| 120 } | 120 } | 
| 121 | 121 | 
| 122 V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks() | 122 V8V0CustomElementLifecycleCallbacks::~V8V0CustomElementLifecycleCallbacks() | 
| 123 { | 123 { | 
| 124 } | 124 } | 
| 125 | 125 | 
| 126 bool V8CustomElementLifecycleCallbacks::setBinding(PassOwnPtr<CustomElementBindi
     ng> binding) | 126 bool V8V0CustomElementLifecycleCallbacks::setBinding(PassOwnPtr<V0CustomElementB
     inding> binding) | 
| 127 { | 127 { | 
| 128     V8PerContextData* perContextData = creationContextData(); | 128     V8PerContextData* perContextData = creationContextData(); | 
| 129     if (!perContextData) | 129     if (!perContextData) | 
| 130         return false; | 130         return false; | 
| 131 | 131 | 
| 132     // The context is responsible for keeping the prototype | 132     // The context is responsible for keeping the prototype | 
| 133     // alive. This in turn keeps callbacks alive through hidden | 133     // alive. This in turn keeps callbacks alive through hidden | 
| 134     // references; see CALLBACK_LIST(SET_HIDDEN_VALUE). | 134     // references; see CALLBACK_LIST(SET_HIDDEN_VALUE). | 
| 135     perContextData->addCustomElementBinding(std::move(binding)); | 135     perContextData->addCustomElementBinding(std::move(binding)); | 
| 136     return true; | 136     return true; | 
| 137 } | 137 } | 
| 138 | 138 | 
| 139 void V8CustomElementLifecycleCallbacks::created(Element* element) | 139 void V8V0CustomElementLifecycleCallbacks::created(Element* element) | 
| 140 { | 140 { | 
| 141     // FIXME: callbacks while paused should be queued up for execution to | 141     // FIXME: callbacks while paused should be queued up for execution to | 
| 142     // continue then be delivered in order rather than delivered immediately. | 142     // continue then be delivered in order rather than delivered immediately. | 
| 143     // Bug 329665 tracks similar behavior for other synchronous events. | 143     // Bug 329665 tracks similar behavior for other synchronous events. | 
| 144     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 144     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 
| 145         return; | 145         return; | 
| 146 | 146 | 
| 147     if (!m_scriptState->contextIsValid()) | 147     if (!m_scriptState->contextIsValid()) | 
| 148         return; | 148         return; | 
| 149 | 149 | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 166 | 166 | 
| 167     v8::Local<v8::Function> callback = m_created.newLocal(isolate); | 167     v8::Local<v8::Function> callback = m_created.newLocal(isolate); | 
| 168     if (callback.IsEmpty()) | 168     if (callback.IsEmpty()) | 
| 169         return; | 169         return; | 
| 170 | 170 | 
| 171     v8::TryCatch exceptionCatcher(isolate); | 171     v8::TryCatch exceptionCatcher(isolate); | 
| 172     exceptionCatcher.SetVerbose(true); | 172     exceptionCatcher.SetVerbose(true); | 
| 173     ScriptController::callFunction(getExecutionContext(), callback, receiver, 0,
      0, isolate); | 173     ScriptController::callFunction(getExecutionContext(), callback, receiver, 0,
      0, isolate); | 
| 174 } | 174 } | 
| 175 | 175 | 
| 176 void V8CustomElementLifecycleCallbacks::attached(Element* element) | 176 void V8V0CustomElementLifecycleCallbacks::attached(Element* element) | 
| 177 { | 177 { | 
| 178     call(m_attached, element); | 178     call(m_attached, element); | 
| 179 } | 179 } | 
| 180 | 180 | 
| 181 void V8CustomElementLifecycleCallbacks::detached(Element* element) | 181 void V8V0CustomElementLifecycleCallbacks::detached(Element* element) | 
| 182 { | 182 { | 
| 183     call(m_detached, element); | 183     call(m_detached, element); | 
| 184 } | 184 } | 
| 185 | 185 | 
| 186 void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const
      AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue) | 186 void V8V0CustomElementLifecycleCallbacks::attributeChanged(Element* element, con
     st AtomicString& name, const AtomicString& oldValue, const AtomicString& newValu
     e) | 
| 187 { | 187 { | 
| 188     // FIXME: callbacks while paused should be queued up for execution to | 188     // FIXME: callbacks while paused should be queued up for execution to | 
| 189     // continue then be delivered in order rather than delivered immediately. | 189     // continue then be delivered in order rather than delivered immediately. | 
| 190     // Bug 329665 tracks similar behavior for other synchronous events. | 190     // Bug 329665 tracks similar behavior for other synchronous events. | 
| 191     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 191     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 
| 192         return; | 192         return; | 
| 193 | 193 | 
| 194     if (!m_scriptState->contextIsValid()) | 194     if (!m_scriptState->contextIsValid()) | 
| 195         return; | 195         return; | 
| 196     ScriptState::Scope scope(m_scriptState.get()); | 196     ScriptState::Scope scope(m_scriptState.get()); | 
| (...skipping 11 matching lines...) Expand all  Loading... | 
| 208         v8String(isolate, name), | 208         v8String(isolate, name), | 
| 209         oldValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local<
     v8::Value>(v8String(isolate, oldValue)), | 209         oldValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local<
     v8::Value>(v8String(isolate, oldValue)), | 
| 210         newValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local<
     v8::Value>(v8String(isolate, newValue)) | 210         newValue.isNull() ? v8::Local<v8::Value>(v8::Null(isolate)) : v8::Local<
     v8::Value>(v8String(isolate, newValue)) | 
| 211     }; | 211     }; | 
| 212 | 212 | 
| 213     v8::TryCatch exceptionCatcher(isolate); | 213     v8::TryCatch exceptionCatcher(isolate); | 
| 214     exceptionCatcher.SetVerbose(true); | 214     exceptionCatcher.SetVerbose(true); | 
| 215     ScriptController::callFunction(getExecutionContext(), callback, receiver, WT
     F_ARRAY_LENGTH(argv), argv, isolate); | 215     ScriptController::callFunction(getExecutionContext(), callback, receiver, WT
     F_ARRAY_LENGTH(argv), argv, isolate); | 
| 216 } | 216 } | 
| 217 | 217 | 
| 218 void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function
     >& weakCallback, Element* element) | 218 void V8V0CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Functi
     on>& weakCallback, Element* element) | 
| 219 { | 219 { | 
| 220     // FIXME: callbacks while paused should be queued up for execution to | 220     // FIXME: callbacks while paused should be queued up for execution to | 
| 221     // continue then be delivered in order rather than delivered immediately. | 221     // continue then be delivered in order rather than delivered immediately. | 
| 222     // Bug 329665 tracks similar behavior for other synchronous events. | 222     // Bug 329665 tracks similar behavior for other synchronous events. | 
| 223     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 223     if (!getExecutionContext() || getExecutionContext()->activeDOMObjectsAreStop
     ped()) | 
| 224         return; | 224         return; | 
| 225 | 225 | 
| 226     if (!m_scriptState->contextIsValid()) | 226     if (!m_scriptState->contextIsValid()) | 
| 227         return; | 227         return; | 
| 228     ScriptState::Scope scope(m_scriptState.get()); | 228     ScriptState::Scope scope(m_scriptState.get()); | 
| 229     v8::Isolate* isolate = m_scriptState->isolate(); | 229     v8::Isolate* isolate = m_scriptState->isolate(); | 
| 230     v8::Local<v8::Context> context = m_scriptState->context(); | 230     v8::Local<v8::Context> context = m_scriptState->context(); | 
| 231     v8::Local<v8::Function> callback = weakCallback.newLocal(isolate); | 231     v8::Local<v8::Function> callback = weakCallback.newLocal(isolate); | 
| 232     if (callback.IsEmpty()) | 232     if (callback.IsEmpty()) | 
| 233         return; | 233         return; | 
| 234 | 234 | 
| 235     v8::Local<v8::Value> receiver = toV8(element, context->Global(), isolate); | 235     v8::Local<v8::Value> receiver = toV8(element, context->Global(), isolate); | 
| 236     if (receiver.IsEmpty()) | 236     if (receiver.IsEmpty()) | 
| 237         return; | 237         return; | 
| 238 | 238 | 
| 239     v8::TryCatch exceptionCatcher(isolate); | 239     v8::TryCatch exceptionCatcher(isolate); | 
| 240     exceptionCatcher.SetVerbose(true); | 240     exceptionCatcher.SetVerbose(true); | 
| 241     ScriptController::callFunction(getExecutionContext(), callback, receiver, 0,
      0, isolate); | 241     ScriptController::callFunction(getExecutionContext(), callback, receiver, 0,
      0, isolate); | 
| 242 } | 242 } | 
| 243 | 243 | 
| 244 DEFINE_TRACE(V8CustomElementLifecycleCallbacks) | 244 DEFINE_TRACE(V8V0CustomElementLifecycleCallbacks) | 
| 245 { | 245 { | 
| 246     CustomElementLifecycleCallbacks::trace(visitor); | 246     V0CustomElementLifecycleCallbacks::trace(visitor); | 
| 247     ContextLifecycleObserver::trace(visitor); | 247     ContextLifecycleObserver::trace(visitor); | 
| 248 } | 248 } | 
| 249 | 249 | 
| 250 } // namespace blink | 250 } // namespace blink | 
| OLD | NEW | 
|---|