1 | /* |
2 | * Copyright (C) 2010 Julien Chaffraix <jchaffraix@webkit.org> All right reserved. |
3 | * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies) |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
15 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
17 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
18 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
19 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
20 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #include "config.h" |
28 | #include "XMLHttpRequestProgressEventThrottle.h" |
29 | |
30 | #include "EventNames.h" |
31 | #include "EventTarget.h" |
32 | #include "XMLHttpRequestProgressEvent.h" |
33 | |
34 | namespace WebCore { |
35 | |
36 | const Seconds XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchingInterval { 50_ms }; // 50 ms per specification. |
37 | |
38 | XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(EventTarget* target) |
39 | : m_target(target) |
40 | , m_dispatchDeferredEventsTimer(*this, &XMLHttpRequestProgressEventThrottle::dispatchDeferredEvents) |
41 | { |
42 | ASSERT(target); |
43 | } |
44 | |
45 | XMLHttpRequestProgressEventThrottle::~XMLHttpRequestProgressEventThrottle() = default; |
46 | |
47 | void XMLHttpRequestProgressEventThrottle::dispatchThrottledProgressEvent(bool lengthComputable, unsigned long long loaded, unsigned long long total) |
48 | { |
49 | m_lengthComputable = lengthComputable; |
50 | m_loaded = loaded; |
51 | m_total = total; |
52 | |
53 | if (!m_target->hasEventListeners(eventNames().progressEvent)) |
54 | return; |
55 | |
56 | if (m_deferEvents) { |
57 | // Only store the latest progress event while suspended. |
58 | m_deferredProgressEvent = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, lengthComputable, loaded, total); |
59 | return; |
60 | } |
61 | |
62 | if (!isActive()) { |
63 | // The timer is not active so the least frequent event for now is every byte. Just dispatch the event. |
64 | |
65 | // We should not have any throttled progress event. |
66 | ASSERT(!m_hasThrottledProgressEvent); |
67 | |
68 | dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, lengthComputable, loaded, total)); |
69 | startRepeating(minimumProgressEventDispatchingInterval); |
70 | m_hasThrottledProgressEvent = false; |
71 | return; |
72 | } |
73 | |
74 | // The timer is already active so minimumProgressEventDispatchingInterval is the least frequent event. |
75 | m_hasThrottledProgressEvent = true; |
76 | } |
77 | |
78 | void XMLHttpRequestProgressEventThrottle::dispatchReadyStateChangeEvent(Event& event, ProgressEventAction progressEventAction) |
79 | { |
80 | if (progressEventAction == FlushProgressEvent) |
81 | flushProgressEvent(); |
82 | |
83 | dispatchEvent(event); |
84 | } |
85 | |
86 | void XMLHttpRequestProgressEventThrottle::dispatchEvent(Event& event) |
87 | { |
88 | if (m_deferEvents) { |
89 | if (m_deferredEvents.size() > 1 && event.type() == eventNames().readystatechangeEvent && event.type() == m_deferredEvents.last()->type()) { |
90 | // Readystatechange events are state-less so avoid repeating two identical events in a row on resume. |
91 | return; |
92 | } |
93 | m_deferredEvents.append(event); |
94 | } else |
95 | m_target->dispatchEvent(event); |
96 | } |
97 | |
98 | void XMLHttpRequestProgressEventThrottle::dispatchProgressEvent(const AtomicString& type) |
99 | { |
100 | ASSERT(type == eventNames().loadstartEvent || type == eventNames().progressEvent || type == eventNames().loadEvent || type == eventNames().loadendEvent || type == eventNames().abortEvent || type == eventNames().errorEvent || type == eventNames().timeoutEvent); |
101 | |
102 | if (type == eventNames().loadstartEvent) { |
103 | m_lengthComputable = false; |
104 | m_loaded = 0; |
105 | m_total = 0; |
106 | } |
107 | |
108 | if (m_target->hasEventListeners(type)) |
109 | dispatchEvent(XMLHttpRequestProgressEvent::create(type, m_lengthComputable, m_loaded, m_total)); |
110 | } |
111 | |
112 | void XMLHttpRequestProgressEventThrottle::flushProgressEvent() |
113 | { |
114 | if (m_deferEvents && m_deferredProgressEvent) { |
115 | // Move the progress event to the queue, to get it in the right order on resume. |
116 | m_deferredEvents.append(m_deferredProgressEvent.releaseNonNull()); |
117 | return; |
118 | } |
119 | |
120 | if (!hasEventToDispatch()) |
121 | return; |
122 | Ref<Event> event = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total); |
123 | m_hasThrottledProgressEvent = false; |
124 | |
125 | // We stop the timer as this is called when no more events are supposed to occur. |
126 | stop(); |
127 | |
128 | dispatchEvent(WTFMove(event)); |
129 | } |
130 | |
131 | void XMLHttpRequestProgressEventThrottle::dispatchDeferredEvents() |
132 | { |
133 | ASSERT(m_deferEvents); |
134 | m_deferEvents = false; |
135 | |
136 | // Take over the deferred events before dispatching them which can potentially add more. |
137 | auto deferredEvents = WTFMove(m_deferredEvents); |
138 | |
139 | RefPtr<Event> deferredProgressEvent = WTFMove(m_deferredProgressEvent); |
140 | |
141 | for (auto& deferredEvent : deferredEvents) |
142 | dispatchEvent(deferredEvent); |
143 | |
144 | // The progress event will be in the m_deferredEvents vector if the load was finished while suspended. |
145 | // If not, just send the most up-to-date progress on resume. |
146 | if (deferredProgressEvent) |
147 | dispatchEvent(*deferredProgressEvent); |
148 | } |
149 | |
150 | void XMLHttpRequestProgressEventThrottle::fired() |
151 | { |
152 | ASSERT(isActive()); |
153 | if (!hasEventToDispatch()) { |
154 | // No progress event was queued since the previous dispatch, we can safely stop the timer. |
155 | stop(); |
156 | return; |
157 | } |
158 | |
159 | dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total)); |
160 | m_hasThrottledProgressEvent = false; |
161 | } |
162 | |
163 | bool XMLHttpRequestProgressEventThrottle::hasEventToDispatch() const |
164 | { |
165 | return m_hasThrottledProgressEvent && isActive(); |
166 | } |
167 | |
168 | void XMLHttpRequestProgressEventThrottle::suspend() |
169 | { |
170 | // If re-suspended before deferred events have been dispatched, just stop the dispatch |
171 | // and continue the last suspend. |
172 | if (m_dispatchDeferredEventsTimer.isActive()) { |
173 | ASSERT(m_deferEvents); |
174 | m_dispatchDeferredEventsTimer.stop(); |
175 | return; |
176 | } |
177 | ASSERT(!m_deferredProgressEvent); |
178 | ASSERT(m_deferredEvents.isEmpty()); |
179 | ASSERT(!m_deferEvents); |
180 | |
181 | m_deferEvents = true; |
182 | // If we have a progress event waiting to be dispatched, |
183 | // just defer it. |
184 | if (hasEventToDispatch()) { |
185 | m_deferredProgressEvent = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total); |
186 | m_hasThrottledProgressEvent = false; |
187 | } |
188 | stop(); |
189 | } |
190 | |
191 | void XMLHttpRequestProgressEventThrottle::resume() |
192 | { |
193 | ASSERT(!m_hasThrottledProgressEvent); |
194 | |
195 | if (m_deferredEvents.isEmpty() && !m_deferredProgressEvent) { |
196 | m_deferEvents = false; |
197 | return; |
198 | } |
199 | |
200 | // Do not dispatch events inline here, since ScriptExecutionContext is iterating over |
201 | // the list of active DOM objects to resume them, and any activated JS event-handler |
202 | // could insert new active DOM objects to the list. |
203 | // m_deferEvents is kept true until all deferred events have been dispatched. |
204 | m_dispatchDeferredEventsTimer.startOneShot(0_s); |
205 | } |
206 | |
207 | } // namespace WebCore |
208 | |