Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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/v8/V8CustomElementLifecycleCallbacks.h" |
| 33 | |
| 34 | #include "V8Element.h" |
| 35 | #include "bindings/v8/DOMDataStore.h" |
| 36 | #include "bindings/v8/ScriptController.h" |
| 37 | #include "bindings/v8/V8Binding.h" |
| 38 | #include "bindings/v8/V8HiddenPropertyName.h" |
| 39 | #include "core/dom/ScriptExecutionContext.h" |
| 40 | #include "wtf/PassRefPtr.h" |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | #define CALLBACK_LIST(V) \ |
| 45 | V(created, Created) \ |
| 46 | V(enteredDocument, EnteredDocument) \ |
| 47 | V(leftDocument, LeftDocument) \ |
| 48 | V(attributeChanged, AttributeChanged) |
| 49 | |
| 50 | PassRefPtr<V8CustomElementLifecycleCallbacks> V8CustomElementLifecycleCallbacks::create(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredDocument, v8::Handle<v8::Function> leftDocument, v8::Handle<v8::Function> attributeChanged) |
| 51 | { |
| 52 | // A given object can only be used as a Custom Element prototype |
| 53 | // once; see customElementIsInterfacePrototypeObject |
| 54 | #define SET_HIDDEN_PROPERTY(Value, Name) \ |
| 55 | ASSERT(prototype->GetHiddenValue(V8HiddenPropertyName::customElement##Name()).IsEmpty()); \ |
| 56 | if (!Value.IsEmpty()) \ |
| 57 | prototype->SetHiddenValue(V8HiddenPropertyName::customElement##Name(), Value); |
| 58 | |
| 59 | CALLBACK_LIST(SET_HIDDEN_PROPERTY) |
| 60 | #undef SET_HIDDEN_PROPERTY |
| 61 | |
| 62 | return adoptRef(new V8CustomElementLifecycleCallbacks(scriptExecutionContext, prototype, created, enteredDocument, leftDocument, attributeChanged)); |
| 63 | } |
| 64 | |
| 65 | static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> enteredDocument, v8::Handle<v8::Function> leftDocument, v8::Handle<v8::Function> attributeChanged) |
| 66 | { |
| 67 | // V8 Custom Elements always run created to swizzle prototypes. |
| 68 | int flags = CustomElementLifecycleCallbacks::Created; |
| 69 | |
| 70 | if (!enteredDocument.IsEmpty()) |
| 71 | flags |= CustomElementLifecycleCallbacks::EnteredDocument; |
| 72 | |
| 73 | if (!leftDocument.IsEmpty()) |
| 74 | flags |= CustomElementLifecycleCallbacks::LeftDocument; |
| 75 | |
| 76 | if (!attributeChanged.IsEmpty()) |
| 77 | flags |= CustomElementLifecycleCallbacks::AttributeChanged; |
| 78 | |
| 79 | return CustomElementLifecycleCallbacks::CallbackType(flags); |
| 80 | } |
| 81 | |
| 82 | template <typename T> |
| 83 | static void weakCallback(v8::Isolate*, v8::Persistent<T>*, ScopedPersistent<T>* handle) |
| 84 | { |
| 85 | handle->clear(); |
| 86 | } |
| 87 | |
| 88 | V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredDocument, v8::Handle<v8::Function> leftDocument, v8::Handle<v8::Function> attributeChanged) |
| 89 | : CustomElementLifecycleCallbacks(flagSet(enteredDocument, leftDocument, attributeChanged)) |
| 90 | , ActiveDOMCallback(scriptExecutionContext) |
| 91 | , m_world(DOMWrapperWorld::current()) |
| 92 | , m_prototype(prototype) |
| 93 | , m_created(created) |
| 94 | , m_enteredDocument(enteredDocument) |
| 95 | , m_leftDocument(leftDocument) |
| 96 | , m_attributeChanged(attributeChanged) |
| 97 | { |
| 98 | m_prototype.makeWeak(&m_prototype, weakCallback<v8::Object>); |
| 99 | |
| 100 | #define MAKE_WEAK(Var, _) \ |
| 101 | if (!m_##Var.isEmpty()) \ |
| 102 | m_##Var.makeWeak(&m_##Var, weakCallback<v8::Function>); |
| 103 | |
| 104 | CALLBACK_LIST(MAKE_WEAK) |
| 105 | #undef MAKE_WEAK |
| 106 | } |
| 107 | |
| 108 | void V8CustomElementLifecycleCallbacks::created(Element* element) |
| 109 | { |
| 110 | if (!canInvokeCallback()) |
| 111 | return; |
| 112 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 113 | element->setCustomElementState(Element::Upgraded); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 114 | |
| 115 | v8::HandleScope handleScope; |
| 116 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 117 | if (context.IsEmpty()) |
| 118 | return; |
| 119 | |
| 120 | v8::Context::Scope scope(context); |
| 121 | v8::Isolate* isolate = context->GetIsolate(); |
| 122 | |
| 123 | v8::Handle<v8::Object> receiver = DOMDataStore::current(isolate)->get<V8Element>(element); |
| 124 | if (!receiver.IsEmpty()) { |
| 125 | // Swizzle the prototype of the existing wrapper. We don't need to |
| 126 | // worry about non-existent wrappers; they will get the right |
| 127 | // prototype when wrapped. |
| 128 | v8::Handle<v8::Object> prototype = m_prototype.newLocal(isolate); |
| 129 | if (prototype.IsEmpty()) |
| 130 | return; |
| 131 | receiver->SetPrototype(prototype); |
| 132 | } |
| 133 | |
| 134 | v8::Handle<v8::Function> callback = m_created.newLocal(isolate); |
| 135 | if (callback.IsEmpty()) |
| 136 | return; |
| 137 | |
| 138 | if (receiver.IsEmpty()) |
| 139 | receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 140 | |
| 141 | ASSERT(!receiver.IsEmpty()); |
| 142 | |
| 143 | v8::TryCatch exceptionCatcher; |
| 144 | exceptionCatcher.SetVerbose(true); |
| 145 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0); |
| 146 | } |
| 147 | |
| 148 | void V8CustomElementLifecycleCallbacks::enteredDocument(Element* element) |
| 149 | { |
| 150 | call(m_enteredDocument, element); |
| 151 | } |
| 152 | |
| 153 | void V8CustomElementLifecycleCallbacks::leftDocument(Element* element) |
| 154 | { |
| 155 | call(m_leftDocument, element); |
| 156 | } |
| 157 | |
| 158 | void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue) |
| 159 | { |
| 160 | if (!canInvokeCallback()) |
| 161 | return; |
| 162 | |
| 163 | v8::HandleScope handleScope; |
| 164 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 165 | if (context.IsEmpty()) |
| 166 | return; |
| 167 | |
| 168 | v8::Context::Scope scope(context); |
| 169 | v8::Isolate* isolate = context->GetIsolate(); |
| 170 | |
| 171 | v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 172 | ASSERT(!receiver.IsEmpty()); |
| 173 | |
| 174 | v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(isolate); |
| 175 | if (callback.IsEmpty()) |
| 176 | return; |
| 177 | |
| 178 | v8::Handle<v8::Value> argv[] = { |
| 179 | v8String(name, isolate), |
| 180 | oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(oldValue, isolate)), |
| 181 | newValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(newValue, isolate)) |
| 182 | }; |
| 183 | |
| 184 | v8::TryCatch exceptionCatcher; |
| 185 | exceptionCatcher.SetVerbose(true); |
| 186 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv); |
| 187 | } |
| 188 | |
| 189 | void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element) |
| 190 | { |
| 191 | if (!canInvokeCallback()) |
| 192 | return; |
| 193 | |
| 194 | v8::HandleScope handleScope; |
| 195 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 196 | if (context.IsEmpty()) |
| 197 | return; |
| 198 | |
| 199 | v8::Context::Scope scope(context); |
| 200 | v8::Isolate* isolate = context->GetIsolate(); |
| 201 | |
| 202 | v8::Handle<v8::Function> callback = weakCallback.newLocal(isolate); |
| 203 | if (callback.IsEmpty()) |
| 204 | return; |
| 205 | |
| 206 | v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 207 | ASSERT(!receiver.IsEmpty()); |
| 208 | |
| 209 | v8::TryCatch exceptionCatcher; |
| 210 | exceptionCatcher.SetVerbose(true); |
| 211 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0); |
| 212 | } |
| 213 | |
| 214 | } // namespace WebCore |