XMLHttpRequestProgressEventThrottle.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #include "config.h"
  27. #include "XMLHttpRequestProgressEventThrottle.h"
  28. #include "EventTarget.h"
  29. #include "XMLHttpRequestProgressEvent.h"
  30. namespace WebCore {
  31. const double XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchingIntervalInSeconds = .05; // 50 ms per specification.
  32. XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(EventTarget* target)
  33. : m_target(target)
  34. , m_loaded(0)
  35. , m_total(0)
  36. , m_deferEvents(false)
  37. , m_dispatchDeferredEventsTimer(this, &XMLHttpRequestProgressEventThrottle::dispatchDeferredEvents)
  38. {
  39. ASSERT(target);
  40. }
  41. XMLHttpRequestProgressEventThrottle::~XMLHttpRequestProgressEventThrottle()
  42. {
  43. }
  44. void XMLHttpRequestProgressEventThrottle::dispatchProgressEvent(bool lengthComputable, unsigned long long loaded, unsigned long long total)
  45. {
  46. if (m_deferEvents) {
  47. // Only store the latest progress event while suspended.
  48. m_deferredProgressEvent = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, lengthComputable, loaded, total);
  49. return;
  50. }
  51. if (!isActive()) {
  52. // The timer is not active so the least frequent event for now is every byte.
  53. // Just go ahead and dispatch the event.
  54. // We should not have any pending loaded & total information from a previous run.
  55. ASSERT(!m_loaded);
  56. ASSERT(!m_total);
  57. dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, lengthComputable, loaded, total));
  58. startRepeating(minimumProgressEventDispatchingIntervalInSeconds);
  59. return;
  60. }
  61. // The timer is already active so minimumProgressEventDispatchingIntervalInSeconds is the least frequent event.
  62. m_lengthComputable = lengthComputable;
  63. m_loaded = loaded;
  64. m_total = total;
  65. }
  66. void XMLHttpRequestProgressEventThrottle::dispatchReadyStateChangeEvent(PassRefPtr<Event> event, ProgressEventAction progressEventAction)
  67. {
  68. if (progressEventAction == FlushProgressEvent)
  69. flushProgressEvent();
  70. dispatchEvent(event);
  71. }
  72. void XMLHttpRequestProgressEventThrottle::dispatchEvent(PassRefPtr<Event> event)
  73. {
  74. ASSERT(event);
  75. if (m_deferEvents) {
  76. if (m_deferredEvents.size() > 1 && event->type() == eventNames().readystatechangeEvent && event->type() == m_deferredEvents.last()->type()) {
  77. // Readystatechange events are state-less so avoid repeating two identical events in a row on resume.
  78. return;
  79. }
  80. m_deferredEvents.append(event);
  81. } else
  82. m_target->dispatchEvent(event);
  83. }
  84. void XMLHttpRequestProgressEventThrottle::dispatchEventAndLoadEnd(PassRefPtr<Event> event)
  85. {
  86. ASSERT(event->type() == eventNames().loadEvent || event->type() == eventNames().abortEvent || event->type() == eventNames().errorEvent || event->type() == eventNames().timeoutEvent);
  87. dispatchEvent(event);
  88. dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().loadendEvent));
  89. }
  90. void XMLHttpRequestProgressEventThrottle::flushProgressEvent()
  91. {
  92. if (m_deferEvents && m_deferredProgressEvent) {
  93. // Move the progress event to the queue, to get it in the right order on resume.
  94. m_deferredEvents.append(m_deferredProgressEvent);
  95. m_deferredProgressEvent = 0;
  96. return;
  97. }
  98. if (!hasEventToDispatch())
  99. return;
  100. PassRefPtr<Event> event = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total);
  101. m_loaded = 0;
  102. m_total = 0;
  103. // We stop the timer as this is called when no more events are supposed to occur.
  104. stop();
  105. dispatchEvent(event);
  106. }
  107. void XMLHttpRequestProgressEventThrottle::dispatchDeferredEvents(Timer<XMLHttpRequestProgressEventThrottle>* timer)
  108. {
  109. ASSERT_UNUSED(timer, timer == &m_dispatchDeferredEventsTimer);
  110. ASSERT(m_deferEvents);
  111. m_deferEvents = false;
  112. // Take over the deferred events before dispatching them which can potentially add more.
  113. Vector<RefPtr<Event> > deferredEvents;
  114. m_deferredEvents.swap(deferredEvents);
  115. RefPtr<Event> deferredProgressEvent = m_deferredProgressEvent;
  116. m_deferredProgressEvent = 0;
  117. Vector<RefPtr<Event> >::const_iterator it = deferredEvents.begin();
  118. const Vector<RefPtr<Event> >::const_iterator end = deferredEvents.end();
  119. for (; it != end; ++it)
  120. dispatchEvent(*it);
  121. // The progress event will be in the m_deferredEvents vector if the load was finished while suspended.
  122. // If not, just send the most up-to-date progress on resume.
  123. if (deferredProgressEvent)
  124. dispatchEvent(deferredProgressEvent);
  125. }
  126. void XMLHttpRequestProgressEventThrottle::fired()
  127. {
  128. ASSERT(isActive());
  129. if (!hasEventToDispatch()) {
  130. // No progress event was queued since the previous dispatch, we can safely stop the timer.
  131. stop();
  132. return;
  133. }
  134. dispatchEvent(XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total));
  135. m_total = 0;
  136. m_loaded = 0;
  137. }
  138. bool XMLHttpRequestProgressEventThrottle::hasEventToDispatch() const
  139. {
  140. return (m_total || m_loaded) && isActive();
  141. }
  142. void XMLHttpRequestProgressEventThrottle::suspend()
  143. {
  144. // If re-suspended before deferred events have been dispatched, just stop the dispatch
  145. // and continue the last suspend.
  146. if (m_dispatchDeferredEventsTimer.isActive()) {
  147. ASSERT(m_deferEvents);
  148. m_dispatchDeferredEventsTimer.stop();
  149. return;
  150. }
  151. ASSERT(!m_deferredProgressEvent);
  152. ASSERT(m_deferredEvents.isEmpty());
  153. ASSERT(!m_deferEvents);
  154. m_deferEvents = true;
  155. // If we have a progress event waiting to be dispatched,
  156. // just defer it.
  157. if (hasEventToDispatch()) {
  158. m_deferredProgressEvent = XMLHttpRequestProgressEvent::create(eventNames().progressEvent, m_lengthComputable, m_loaded, m_total);
  159. m_total = 0;
  160. m_loaded = 0;
  161. }
  162. stop();
  163. }
  164. void XMLHttpRequestProgressEventThrottle::resume()
  165. {
  166. ASSERT(!m_loaded);
  167. ASSERT(!m_total);
  168. if (m_deferredEvents.isEmpty() && !m_deferredProgressEvent) {
  169. m_deferEvents = false;
  170. return;
  171. }
  172. // Do not dispatch events inline here, since ScriptExecutionContext is iterating over
  173. // the list of active DOM objects to resume them, and any activated JS event-handler
  174. // could insert new active DOM objects to the list.
  175. // m_deferEvents is kept true until all deferred events have been dispatched.
  176. m_dispatchDeferredEventsTimer.startOneShot(0);
  177. }
  178. } // namespace WebCore