| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All Rights Reserved. | 2 * Copyright (C) 2010 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 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 * | 24 * |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #include "core/workers/WorkerEventQueue.h" | 27 #include "core/workers/WorkerEventQueue.h" |
| 28 | 28 |
| 29 #include "core/dom/ExecutionContext.h" | 29 #include "core/dom/ExecutionContext.h" |
| 30 #include "core/dom/ExecutionContextTask.h" | 30 #include "core/dom/ExecutionContextTask.h" |
| 31 #include "core/events/Event.h" | 31 #include "core/events/Event.h" |
| 32 #include "core/inspector/InspectorInstrumentation.h" | 32 #include "core/inspector/InspectorInstrumentation.h" |
| 33 #include "wtf/PtrUtil.h" | |
| 34 | 33 |
| 35 namespace blink { | 34 namespace blink { |
| 36 | 35 |
| 37 WorkerEventQueue* WorkerEventQueue::create(ExecutionContext* context) | 36 WorkerEventQueue* WorkerEventQueue::create(ExecutionContext* context) |
| 38 { | 37 { |
| 39 return new WorkerEventQueue(context); | 38 return new WorkerEventQueue(context); |
| 40 } | 39 } |
| 41 | 40 |
| 42 WorkerEventQueue::WorkerEventQueue(ExecutionContext* context) | 41 WorkerEventQueue::WorkerEventQueue(ExecutionContext* context) |
| 43 : m_executionContext(context) | 42 : m_executionContext(context) |
| 44 , m_isClosed(false) | 43 , m_isClosed(false) |
| 45 { | 44 { |
| 46 } | 45 } |
| 47 | 46 |
| 48 WorkerEventQueue::~WorkerEventQueue() | 47 WorkerEventQueue::~WorkerEventQueue() |
| 49 { | 48 { |
| 50 DCHECK(m_eventTaskMap.isEmpty()); | 49 DCHECK(m_pendingEvents.isEmpty()); |
| 51 } | 50 } |
| 52 | 51 |
| 53 DEFINE_TRACE(WorkerEventQueue) | 52 DEFINE_TRACE(WorkerEventQueue) |
| 54 { | 53 { |
| 55 visitor->trace(m_executionContext); | 54 visitor->trace(m_executionContext); |
| 56 visitor->trace(m_eventTaskMap); | 55 visitor->trace(m_pendingEvents); |
| 57 EventQueue::trace(visitor); | 56 EventQueue::trace(visitor); |
| 58 } | 57 } |
| 59 | 58 |
| 60 class WorkerEventQueue::EventDispatcherTask : public ExecutionContextTask { | |
| 61 public: | |
| 62 static std::unique_ptr<EventDispatcherTask> create(Event* event, WorkerEvent
Queue* eventQueue) | |
| 63 { | |
| 64 return wrapUnique(new EventDispatcherTask(event, eventQueue)); | |
| 65 } | |
| 66 | |
| 67 ~EventDispatcherTask() override | |
| 68 { | |
| 69 if (m_event) { | |
| 70 InspectorInstrumentation::asyncTaskCanceled(m_event->target()->getEx
ecutionContext(), m_event); | |
| 71 m_eventQueue->m_eventTaskMap.remove(m_event); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void dispatchEvent(ExecutionContext* context, Event* event) | |
| 76 { | |
| 77 InspectorInstrumentation::AsyncTask asyncTask(context, event); | |
| 78 event->target()->dispatchEvent(event); | |
| 79 } | |
| 80 | |
| 81 virtual void performTask(ExecutionContext* context) | |
| 82 { | |
| 83 if (m_isCancelled) | |
| 84 return; | |
| 85 | |
| 86 m_eventQueue->m_eventTaskMap.remove(m_event.get()); | |
| 87 dispatchEvent(context, m_event); | |
| 88 m_event.clear(); | |
| 89 } | |
| 90 | |
| 91 void cancel() | |
| 92 { | |
| 93 m_isCancelled = true; | |
| 94 m_event.clear(); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 EventDispatcherTask(Event* event, WorkerEventQueue* eventQueue) | |
| 99 : m_event(event) | |
| 100 , m_eventQueue(eventQueue) | |
| 101 , m_isCancelled(false) | |
| 102 { | |
| 103 } | |
| 104 | |
| 105 Persistent<Event> m_event; | |
| 106 Persistent<WorkerEventQueue> m_eventQueue; | |
| 107 bool m_isCancelled; | |
| 108 }; | |
| 109 | |
| 110 bool WorkerEventQueue::enqueueEvent(Event* event) | 59 bool WorkerEventQueue::enqueueEvent(Event* event) |
| 111 { | 60 { |
| 112 if (m_isClosed) | 61 if (m_isClosed) |
| 113 return false; | 62 return false; |
| 114 InspectorInstrumentation::asyncTaskScheduled(event->target()->getExecutionCo
ntext(), event->type(), event); | 63 InspectorInstrumentation::asyncTaskScheduled(event->target()->getExecutionCo
ntext(), event->type(), event); |
| 115 std::unique_ptr<EventDispatcherTask> task = EventDispatcherTask::create(even
t, this); | 64 m_pendingEvents.add(event); |
| 116 m_eventTaskMap.add(event, task.get()); | 65 m_executionContext->postTask(BLINK_FROM_HERE, createSameThreadTask(&WorkerEv
entQueue::dispatchEvent, wrapPersistent(this), wrapWeakPersistent(event))); |
| 117 m_executionContext->postTask(BLINK_FROM_HERE, std::move(task)); | |
| 118 return true; | 66 return true; |
| 119 } | 67 } |
| 120 | 68 |
| 121 bool WorkerEventQueue::cancelEvent(Event* event) | 69 bool WorkerEventQueue::cancelEvent(Event* event) |
| 122 { | 70 { |
| 123 EventDispatcherTask* task = m_eventTaskMap.get(event); | 71 if (!removeEvent(event)) |
| 124 if (!task) | |
| 125 return false; | 72 return false; |
| 126 task->cancel(); | |
| 127 InspectorInstrumentation::asyncTaskCanceled(event->target()->getExecutionCon
text(), event); | 73 InspectorInstrumentation::asyncTaskCanceled(event->target()->getExecutionCon
text(), event); |
| 128 m_eventTaskMap.remove(event); | |
| 129 return true; | 74 return true; |
| 130 } | 75 } |
| 131 | 76 |
| 132 void WorkerEventQueue::close() | 77 void WorkerEventQueue::close() |
| 133 { | 78 { |
| 134 m_isClosed = true; | 79 m_isClosed = true; |
| 135 for (const auto& entry : m_eventTaskMap) { | 80 for (const auto& event : m_pendingEvents) |
| 136 Event* event = entry.key.get(); | |
| 137 EventDispatcherTask* task = entry.value; | |
| 138 InspectorInstrumentation::asyncTaskCanceled(event->target()->getExecutio
nContext(), event); | 81 InspectorInstrumentation::asyncTaskCanceled(event->target()->getExecutio
nContext(), event); |
| 139 task->cancel(); | 82 m_pendingEvents.clear(); |
| 140 } | 83 } |
| 141 m_eventTaskMap.clear(); | 84 |
| 85 bool WorkerEventQueue::removeEvent(Event* event) |
| 86 { |
| 87 auto found = m_pendingEvents.find(event); |
| 88 if (found == m_pendingEvents.end()) |
| 89 return false; |
| 90 m_pendingEvents.remove(found); |
| 91 return true; |
| 92 } |
| 93 |
| 94 void WorkerEventQueue::dispatchEvent(Event* event, ExecutionContext* executionCo
ntext) |
| 95 { |
| 96 if (!event || !removeEvent(event)) |
| 97 return; |
| 98 |
| 99 InspectorInstrumentation::AsyncTask asyncTask(executionContext, event); |
| 100 event->target()->dispatchEvent(event); |
| 142 } | 101 } |
| 143 | 102 |
| 144 } // namespace blink | 103 } // namespace blink |
| OLD | NEW |