OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2012 Apple 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 |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 #ifndef EventSender_h |
| 27 #define EventSender_h |
| 28 |
| 29 #include "platform/Timer.h" |
| 30 #include "platform/heap/Handle.h" |
| 31 #include "platform/wtf/Vector.h" |
| 32 #include "platform/wtf/text/AtomicString.h" |
| 33 |
| 34 namespace blink { |
| 35 |
| 36 template <typename T> |
| 37 class EventSender final : public GarbageCollectedFinalized<EventSender<T>> { |
| 38 WTF_MAKE_NONCOPYABLE(EventSender); |
| 39 |
| 40 public: |
| 41 static EventSender* Create(const AtomicString& event_type) { |
| 42 return new EventSender(event_type); |
| 43 } |
| 44 |
| 45 const AtomicString& EventType() const { return event_type_; } |
| 46 void DispatchEventSoon(T*); |
| 47 void CancelEvent(T*); |
| 48 void DispatchPendingEvents(); |
| 49 |
| 50 #if DCHECK_IS_ON() |
| 51 bool HasPendingEvents(T* sender) const { |
| 52 return dispatch_soon_list_.Find(sender) != kNotFound || |
| 53 dispatching_list_.Find(sender) != kNotFound; |
| 54 } |
| 55 #endif |
| 56 |
| 57 DEFINE_INLINE_TRACE() { |
| 58 visitor->Trace(dispatch_soon_list_); |
| 59 visitor->Trace(dispatching_list_); |
| 60 } |
| 61 |
| 62 private: |
| 63 explicit EventSender(const AtomicString& event_type); |
| 64 |
| 65 void TimerFired(TimerBase*) { DispatchPendingEvents(); } |
| 66 |
| 67 AtomicString event_type_; |
| 68 Timer<EventSender<T>> timer_; |
| 69 HeapVector<Member<T>> dispatch_soon_list_; |
| 70 HeapVector<Member<T>> dispatching_list_; |
| 71 }; |
| 72 |
| 73 template <typename T> |
| 74 EventSender<T>::EventSender(const AtomicString& event_type) |
| 75 : event_type_(event_type), timer_(this, &EventSender::TimerFired) {} |
| 76 |
| 77 template <typename T> |
| 78 void EventSender<T>::DispatchEventSoon(T* sender) { |
| 79 dispatch_soon_list_.push_back(sender); |
| 80 if (!timer_.IsActive()) |
| 81 timer_.StartOneShot(0, BLINK_FROM_HERE); |
| 82 } |
| 83 |
| 84 template <typename T> |
| 85 void EventSender<T>::CancelEvent(T* sender) { |
| 86 // Remove instances of this sender from both lists. |
| 87 // Use loops because we allow multiple instances to get into the lists. |
| 88 for (auto& sender_in_list : dispatch_soon_list_) { |
| 89 if (sender_in_list == sender) |
| 90 sender_in_list = nullptr; |
| 91 } |
| 92 for (auto& sender_in_list : dispatching_list_) { |
| 93 if (sender_in_list == sender) |
| 94 sender_in_list = nullptr; |
| 95 } |
| 96 } |
| 97 |
| 98 template <typename T> |
| 99 void EventSender<T>::DispatchPendingEvents() { |
| 100 // Need to avoid re-entering this function; if new dispatches are |
| 101 // scheduled before the parent finishes processing the list, they |
| 102 // will set a timer and eventually be processed. |
| 103 if (!dispatching_list_.IsEmpty()) |
| 104 return; |
| 105 |
| 106 timer_.Stop(); |
| 107 |
| 108 dispatching_list_.swap(dispatch_soon_list_); |
| 109 for (auto& sender_in_list : dispatching_list_) { |
| 110 if (T* sender = sender_in_list) { |
| 111 sender_in_list = nullptr; |
| 112 sender->DispatchPendingEvent(this); |
| 113 } |
| 114 } |
| 115 dispatching_list_.clear(); |
| 116 } |
| 117 |
| 118 } // namespace blink |
| 119 |
| 120 #endif // EventSender_h |
OLD | NEW |