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" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 35 | #include "bindings/v8/CustomElementBinding.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 36 | #include "bindings/v8/DOMDataStore.h" |
| 37 | #include "bindings/v8/ScriptController.h" |
| 38 | #include "bindings/v8/V8Binding.h" |
| 39 | #include "bindings/v8/V8HiddenPropertyName.h" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 40 | #include "bindings/v8/V8PerContextData.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 41 | #include "core/dom/ScriptExecutionContext.h" |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 42 | #include "wtf/PassOwnPtr.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 43 | |
| 44 | namespace WebCore { |
| 45 | |
| 46 | #define CALLBACK_LIST(V) \ |
| 47 | V(created, Created) \ |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 48 | V(enteredView, EnteredView) \ |
| 49 | V(leftView, LeftView) \ |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 50 | V(attributeChanged, AttributeChanged) |
| 51 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 52 | PassRefPtr<V8CustomElementLifecycleCallbacks> V8CustomElementLifecycleCallbacks::create(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 53 | { |
| 54 | // A given object can only be used as a Custom Element prototype |
| 55 | // once; see customElementIsInterfacePrototypeObject |
| 56 | #define SET_HIDDEN_PROPERTY(Value, Name) \ |
| 57 | ASSERT(prototype->GetHiddenValue(V8HiddenPropertyName::customElement##Name()).IsEmpty()); \ |
| 58 | if (!Value.IsEmpty()) \ |
| 59 | prototype->SetHiddenValue(V8HiddenPropertyName::customElement##Name(), Value); |
| 60 | |
| 61 | CALLBACK_LIST(SET_HIDDEN_PROPERTY) |
| 62 | #undef SET_HIDDEN_PROPERTY |
| 63 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 64 | return adoptRef(new V8CustomElementLifecycleCallbacks(scriptExecutionContext, prototype, created, enteredView, leftView, attributeChanged)); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 67 | static CustomElementLifecycleCallbacks::CallbackType flagSet(v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 68 | { |
| 69 | // V8 Custom Elements always run created to swizzle prototypes. |
| 70 | int flags = CustomElementLifecycleCallbacks::Created; |
| 71 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 72 | if (!enteredView.IsEmpty()) |
| 73 | flags |= CustomElementLifecycleCallbacks::EnteredView; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 74 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 75 | if (!leftView.IsEmpty()) |
| 76 | flags |= CustomElementLifecycleCallbacks::LeftView; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 77 | |
| 78 | if (!attributeChanged.IsEmpty()) |
| 79 | flags |= CustomElementLifecycleCallbacks::AttributeChanged; |
| 80 | |
| 81 | return CustomElementLifecycleCallbacks::CallbackType(flags); |
| 82 | } |
| 83 | |
| 84 | template <typename T> |
| 85 | static void weakCallback(v8::Isolate*, v8::Persistent<T>*, ScopedPersistent<T>* handle) |
| 86 | { |
| 87 | handle->clear(); |
| 88 | } |
| 89 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 90 | V8CustomElementLifecycleCallbacks::V8CustomElementLifecycleCallbacks(ScriptExecutionContext* scriptExecutionContext, v8::Handle<v8::Object> prototype, v8::Handle<v8::Function> created, v8::Handle<v8::Function> enteredView, v8::Handle<v8::Function> leftView, v8::Handle<v8::Function> attributeChanged) |
| 91 | : CustomElementLifecycleCallbacks(flagSet(enteredView, leftView, attributeChanged)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 92 | , ActiveDOMCallback(scriptExecutionContext) |
| 93 | , m_world(DOMWrapperWorld::current()) |
| 94 | , m_prototype(prototype) |
| 95 | , m_created(created) |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 96 | , m_enteredView(enteredView) |
| 97 | , m_leftView(leftView) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 98 | , m_attributeChanged(attributeChanged) |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 99 | , m_owner(0) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 100 | { |
| 101 | m_prototype.makeWeak(&m_prototype, weakCallback<v8::Object>); |
| 102 | |
| 103 | #define MAKE_WEAK(Var, _) \ |
| 104 | if (!m_##Var.isEmpty()) \ |
| 105 | m_##Var.makeWeak(&m_##Var, weakCallback<v8::Function>); |
| 106 | |
| 107 | CALLBACK_LIST(MAKE_WEAK) |
| 108 | #undef MAKE_WEAK |
| 109 | } |
| 110 | |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 111 | V8PerContextData* V8CustomElementLifecycleCallbacks::creationContextData() |
| 112 | { |
| 113 | if (!scriptExecutionContext()) |
| 114 | return 0; |
| 115 | |
| 116 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 117 | if (context.IsEmpty()) |
| 118 | return 0; |
| 119 | |
| 120 | return V8PerContextData::from(context); |
| 121 | } |
| 122 | |
| 123 | V8CustomElementLifecycleCallbacks::~V8CustomElementLifecycleCallbacks() |
| 124 | { |
| 125 | if (!m_owner) |
| 126 | return; |
| 127 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 128 | v8::HandleScope handleScope(getIsolateFromScriptExecutionContext(scriptExecutionContext())); |
Ben Murdoch | 02772c6 | 2013-07-26 10:21:05 +0100 | [diff] [blame] | 129 | if (V8PerContextData* perContextData = creationContextData()) |
| 130 | perContextData->clearCustomElementBinding(m_owner); |
| 131 | } |
| 132 | |
| 133 | bool V8CustomElementLifecycleCallbacks::setBinding(CustomElementDefinition* owner, PassOwnPtr<CustomElementBinding> binding) |
| 134 | { |
| 135 | ASSERT(!m_owner); |
| 136 | |
| 137 | V8PerContextData* perContextData = creationContextData(); |
| 138 | if (!perContextData) |
| 139 | return false; |
| 140 | |
| 141 | m_owner = owner; |
| 142 | |
| 143 | // Bindings retrieve the prototype when needed from per-context data. |
| 144 | perContextData->addCustomElementBinding(owner, binding); |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 149 | void V8CustomElementLifecycleCallbacks::created(Element* element) |
| 150 | { |
| 151 | if (!canInvokeCallback()) |
| 152 | return; |
| 153 | |
Ben Murdoch | 7757ec2 | 2013-07-23 11:17:36 +0100 | [diff] [blame] | 154 | element->setCustomElementState(Element::Upgraded); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 155 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 156 | v8::Isolate* isolate = getIsolateFromScriptExecutionContext(scriptExecutionContext()); |
| 157 | v8::HandleScope handleScope(isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 158 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 159 | if (context.IsEmpty()) |
| 160 | return; |
| 161 | |
| 162 | v8::Context::Scope scope(context); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 163 | |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 164 | v8::Handle<v8::Object> receiver = DOMDataStore::current(isolate)->get<V8Element>(element, isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 165 | if (!receiver.IsEmpty()) { |
| 166 | // Swizzle the prototype of the existing wrapper. We don't need to |
| 167 | // worry about non-existent wrappers; they will get the right |
| 168 | // prototype when wrapped. |
| 169 | v8::Handle<v8::Object> prototype = m_prototype.newLocal(isolate); |
| 170 | if (prototype.IsEmpty()) |
| 171 | return; |
| 172 | receiver->SetPrototype(prototype); |
| 173 | } |
| 174 | |
| 175 | v8::Handle<v8::Function> callback = m_created.newLocal(isolate); |
| 176 | if (callback.IsEmpty()) |
| 177 | return; |
| 178 | |
| 179 | if (receiver.IsEmpty()) |
| 180 | receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 181 | |
| 182 | ASSERT(!receiver.IsEmpty()); |
| 183 | |
| 184 | v8::TryCatch exceptionCatcher; |
| 185 | exceptionCatcher.SetVerbose(true); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 186 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0, isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 189 | void V8CustomElementLifecycleCallbacks::enteredView(Element* element) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 190 | { |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 191 | call(m_enteredView, element); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 194 | void V8CustomElementLifecycleCallbacks::leftView(Element* element) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 195 | { |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 196 | call(m_leftView, element); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void V8CustomElementLifecycleCallbacks::attributeChanged(Element* element, const AtomicString& name, const AtomicString& oldValue, const AtomicString& newValue) |
| 200 | { |
| 201 | if (!canInvokeCallback()) |
| 202 | return; |
| 203 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 204 | v8::Isolate* isolate = getIsolateFromScriptExecutionContext(scriptExecutionContext()); |
| 205 | v8::HandleScope handleScope(isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 206 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 207 | if (context.IsEmpty()) |
| 208 | return; |
| 209 | |
| 210 | v8::Context::Scope scope(context); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 211 | |
| 212 | v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 213 | ASSERT(!receiver.IsEmpty()); |
| 214 | |
| 215 | v8::Handle<v8::Function> callback = m_attributeChanged.newLocal(isolate); |
| 216 | if (callback.IsEmpty()) |
| 217 | return; |
| 218 | |
| 219 | v8::Handle<v8::Value> argv[] = { |
| 220 | v8String(name, isolate), |
| 221 | oldValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(oldValue, isolate)), |
| 222 | newValue.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(newValue, isolate)) |
| 223 | }; |
| 224 | |
| 225 | v8::TryCatch exceptionCatcher; |
| 226 | exceptionCatcher.SetVerbose(true); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 227 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, WTF_ARRAY_LENGTH(argv), argv, isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void V8CustomElementLifecycleCallbacks::call(const ScopedPersistent<v8::Function>& weakCallback, Element* element) |
| 231 | { |
| 232 | if (!canInvokeCallback()) |
| 233 | return; |
| 234 | |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 235 | v8::HandleScope handleScope(getIsolateFromScriptExecutionContext(scriptExecutionContext())); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 236 | v8::Handle<v8::Context> context = toV8Context(scriptExecutionContext(), m_world.get()); |
| 237 | if (context.IsEmpty()) |
| 238 | return; |
| 239 | |
| 240 | v8::Context::Scope scope(context); |
| 241 | v8::Isolate* isolate = context->GetIsolate(); |
| 242 | |
| 243 | v8::Handle<v8::Function> callback = weakCallback.newLocal(isolate); |
| 244 | if (callback.IsEmpty()) |
| 245 | return; |
| 246 | |
| 247 | v8::Handle<v8::Object> receiver = toV8(element, context->Global(), isolate).As<v8::Object>(); |
| 248 | ASSERT(!receiver.IsEmpty()); |
| 249 | |
| 250 | v8::TryCatch exceptionCatcher; |
| 251 | exceptionCatcher.SetVerbose(true); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame^] | 252 | ScriptController::callFunctionWithInstrumentation(scriptExecutionContext(), callback, receiver, 0, 0, isolate); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } // namespace WebCore |