| 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 | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 13 matching lines...) Expand all Loading... |
| 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 "config.h" | 31 #include "config.h" |
| 32 #include "core/dom/CustomElementCallbackDispatcher.h" | 32 #include "core/dom/CustomElementCallbackDispatcher.h" |
| 33 | 33 |
| 34 #include "core/dom/CustomElementCallback.h" | |
| 35 #include "wtf/PassOwnPtr.h" | 34 #include "wtf/PassOwnPtr.h" |
| 36 | 35 |
| 37 namespace WebCore { | 36 namespace WebCore { |
| 38 | 37 |
| 38 CustomElementCallbackDispatcher::Invocation::Invocation(CustomElementCallback::C
allbackType type, PassRefPtr<CustomElementCallback> callback, PassRefPtr<Element
> element) |
| 39 : m_type(type) |
| 40 , m_callback(callback) |
| 41 , m_element(element) |
| 42 { |
| 43 } |
| 44 |
| 45 void CustomElementCallbackDispatcher::Invocation::invoke() |
| 46 { |
| 47 switch (m_type) { |
| 48 case CustomElementCallback::Ready: |
| 49 m_callback->ready(m_element.get()); |
| 50 break; |
| 51 |
| 52 case CustomElementCallback::Inserted: |
| 53 m_callback->inserted(m_element.get()); |
| 54 break; |
| 55 |
| 56 case CustomElementCallback::Removed: |
| 57 m_callback->removed(m_element.get()); |
| 58 break; |
| 59 |
| 60 default: |
| 61 ASSERT_NOT_REACHED(); |
| 62 } |
| 63 } |
| 64 |
| 39 CustomElementCallbackDispatcher& CustomElementCallbackDispatcher::instance() | 65 CustomElementCallbackDispatcher& CustomElementCallbackDispatcher::instance() |
| 40 { | 66 { |
| 41 DEFINE_STATIC_LOCAL(CustomElementCallbackDispatcher, instance, ()); | 67 DEFINE_STATIC_LOCAL(CustomElementCallbackDispatcher, instance, ()); |
| 42 return instance; | 68 return instance; |
| 43 } | 69 } |
| 44 | 70 |
| 45 CustomElementCallbackDispatcher::ReadyInvocation::ReadyInvocation(PassRefPtr<Cus
tomElementCallback> callback, PassRefPtr<Element> element) | |
| 46 : m_callback(callback) | |
| 47 , m_element(element) | |
| 48 { | |
| 49 } | |
| 50 | |
| 51 bool CustomElementCallbackDispatcher::dispatch() | 71 bool CustomElementCallbackDispatcher::dispatch() |
| 52 { | 72 { |
| 53 if (m_invocations.isEmpty()) | 73 if (m_invocations.isEmpty()) |
| 54 return false; | 74 return false; |
| 55 | 75 |
| 56 do { | 76 do { |
| 57 InvocationList invocations; | 77 InvocationList invocations; |
| 58 m_invocations.swap(invocations); | 78 m_invocations.swap(invocations); |
| 59 | 79 |
| 60 for (InvocationList::iterator it = invocations.begin(); it != invocation
s.end(); ++it) | 80 for (InvocationList::iterator it = invocations.begin(); it != invocation
s.end(); ++it) |
| 61 it->get()->invoke(); | 81 it->get()->invoke(); |
| 62 } while (!m_invocations.isEmpty()); | 82 } while (!m_invocations.isEmpty()); |
| 63 | 83 |
| 64 return true; | 84 return true; |
| 65 } | 85 } |
| 66 | 86 |
| 67 void CustomElementCallbackDispatcher::dispatchReadyCallback(Element* element) | 87 void CustomElementCallbackDispatcher::dispatchReadyCallback(Element* element) |
| 68 { | 88 { |
| 69 for (size_t i = 0; i < m_invocations.size(); ++i) { | 89 for (size_t i = 0; i < m_invocations.size(); ++i) { |
| 70 if (m_invocations[i].get()->element() == element) { | 90 Invocation* invocation = m_invocations[i].get(); |
| 71 OwnPtr<ReadyInvocation> invocation = m_invocations[i].release(); | 91 if (invocation->element() == element && invocation->type() == CustomElem
entCallback::Ready) { |
| 92 OwnPtr<Invocation> protect(m_invocations[i].release()); |
| 72 m_invocations.remove(i); | 93 m_invocations.remove(i); |
| 73 invocation->invoke(); | 94 invocation->invoke(); |
| 74 return; | 95 return; |
| 75 } | 96 } |
| 76 } | 97 } |
| 77 // The element is unresolved, or it has no ready callback. | 98 // The element is unresolved, or it has no ready callback. |
| 78 } | 99 } |
| 79 | 100 |
| 80 void CustomElementCallbackDispatcher::enqueueReadyCallback(CustomElementCallback
* callback, Element* element) | 101 void CustomElementCallbackDispatcher::enqueueCreationCallbacks(CustomElementCall
back* callback, Element* element, bool createdByParser) |
| 81 { | 102 { |
| 82 if (!callback->hasReady()) | 103 if (callback->hasReady()) { |
| 104 OwnPtr<Invocation> readyInvocation = adoptPtr(new Invocation(CustomEleme
ntCallback::Ready, callback, element)); |
| 105 if (createdByParser) |
| 106 m_invocations.prepend(readyInvocation.release()); |
| 107 else |
| 108 m_invocations.append(readyInvocation.release()); |
| 109 } |
| 110 |
| 111 if (element->inDocument()) |
| 112 enqueueInsertedCallback(callback, element); |
| 113 } |
| 114 |
| 115 void CustomElementCallbackDispatcher::enqueueInsertedCallback(CustomElementCallb
ack* callback, Element* element) |
| 116 { |
| 117 if (!callback->hasInserted()) |
| 83 return; | 118 return; |
| 84 | 119 |
| 85 m_invocations.append(adoptPtr(new ReadyInvocation(callback, element))); | 120 m_invocations.append(adoptPtr(new Invocation(CustomElementCallback::Inserted
, callback, element))); |
| 121 } |
| 122 |
| 123 void CustomElementCallbackDispatcher::enqueueRemovedCallback(CustomElementCallba
ck* callback, Element* element) |
| 124 { |
| 125 if (!callback->hasRemoved()) |
| 126 return; |
| 127 |
| 128 m_invocations.append(adoptPtr(new Invocation(CustomElementCallback::Removed,
callback, element))); |
| 86 } | 129 } |
| 87 | 130 |
| 88 } // namespace WebCore | 131 } // namespace WebCore |
| OLD | NEW |