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