OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2010 Julien Chaffraix <jchaffraix@webkit.org> All right reserv
ed. |
| 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 "core/xmlhttprequest/XMLHttpRequestProgressEventThrottle.h" |
| 28 |
| 29 #include "core/EventTypeNames.h" |
| 30 #include "core/events/ProgressEvent.h" |
| 31 #include "core/inspector/InspectorInstrumentation.h" |
| 32 #include "core/inspector/InspectorTraceEvents.h" |
| 33 #include "core/xmlhttprequest/XMLHttpRequest.h" |
| 34 #include "wtf/Assertions.h" |
| 35 #include "wtf/text/AtomicString.h" |
| 36 |
| 37 namespace blink { |
| 38 |
| 39 XMLHttpRequestProgressEventThrottle::DeferredEvent::DeferredEvent() |
| 40 { |
| 41 clear(); |
| 42 } |
| 43 |
| 44 void XMLHttpRequestProgressEventThrottle::DeferredEvent::set(bool lengthComputab
le, unsigned long long loaded, unsigned long long total) |
| 45 { |
| 46 m_isSet = true; |
| 47 |
| 48 m_lengthComputable = lengthComputable; |
| 49 m_loaded = loaded; |
| 50 m_total = total; |
| 51 } |
| 52 |
| 53 void XMLHttpRequestProgressEventThrottle::DeferredEvent::clear() |
| 54 { |
| 55 m_isSet = false; |
| 56 |
| 57 m_lengthComputable = false; |
| 58 m_loaded = 0; |
| 59 m_total = 0; |
| 60 } |
| 61 |
| 62 PassRefPtrWillBeRawPtr<Event> XMLHttpRequestProgressEventThrottle::DeferredEvent
::take() |
| 63 { |
| 64 ASSERT(m_isSet); |
| 65 |
| 66 RefPtrWillBeRawPtr<Event> event = ProgressEvent::create(EventTypeNames::prog
ress, m_lengthComputable, m_loaded, m_total); |
| 67 clear(); |
| 68 return event.release(); |
| 69 } |
| 70 |
| 71 const double XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchin
gIntervalInSeconds = .05; // 50 ms per specification. |
| 72 |
| 73 XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(XMLHttp
Request* target) |
| 74 : m_target(target) |
| 75 , m_hasDispatchedProgressProgressEvent(false) |
| 76 { |
| 77 ASSERT(target); |
| 78 } |
| 79 |
| 80 XMLHttpRequestProgressEventThrottle::~XMLHttpRequestProgressEventThrottle() |
| 81 { |
| 82 } |
| 83 |
| 84 void XMLHttpRequestProgressEventThrottle::dispatchProgressEvent(const AtomicStri
ng& type, bool lengthComputable, unsigned long long loaded, unsigned long long t
otal) |
| 85 { |
| 86 // Given that ResourceDispatcher doesn't deliver an event when suspended, |
| 87 // we don't have to worry about event dispatching while suspended. |
| 88 if (type != EventTypeNames::progress) { |
| 89 m_target->dispatchEvent(ProgressEvent::create(type, lengthComputable, lo
aded, total)); |
| 90 return; |
| 91 } |
| 92 |
| 93 if (isActive()) { |
| 94 m_deferred.set(lengthComputable, loaded, total); |
| 95 } else { |
| 96 dispatchProgressProgressEvent(ProgressEvent::create(EventTypeNames::prog
ress, lengthComputable, loaded, total)); |
| 97 startOneShot(minimumProgressEventDispatchingIntervalInSeconds, BLINK_FRO
M_HERE); |
| 98 } |
| 99 } |
| 100 |
| 101 void XMLHttpRequestProgressEventThrottle::dispatchReadyStateChangeEvent(PassRefP
trWillBeRawPtr<Event> event, DeferredEventAction action) |
| 102 { |
| 103 XMLHttpRequest::State state = m_target->readyState(); |
| 104 // Given that ResourceDispatcher doesn't deliver an event when suspended, |
| 105 // we don't have to worry about event dispatching while suspended. |
| 106 if (action == Flush) { |
| 107 if (m_deferred.isSet()) |
| 108 dispatchProgressProgressEvent(m_deferred.take()); |
| 109 |
| 110 stop(); |
| 111 } else if (action == Clear) { |
| 112 m_deferred.clear(); |
| 113 stop(); |
| 114 } |
| 115 |
| 116 m_hasDispatchedProgressProgressEvent = false; |
| 117 if (state == m_target->readyState()) { |
| 118 // We don't dispatch the event when an event handler associated with |
| 119 // the previously dispatched event changes the readyState (e.g. when |
| 120 // the event handler calls xhr.abort()). In such cases a |
| 121 // readystatechange should have been already dispatched if necessary. |
| 122 m_target->dispatchEvent(event); |
| 123 } |
| 124 } |
| 125 |
| 126 void XMLHttpRequestProgressEventThrottle::dispatchProgressProgressEvent(PassRefP
trWillBeRawPtr<Event> progressEvent) |
| 127 { |
| 128 XMLHttpRequest::State state = m_target->readyState(); |
| 129 if (m_target->readyState() == XMLHttpRequest::LOADING && m_hasDispatchedProg
ressProgressEvent) { |
| 130 TRACE_EVENT1("devtools.timeline", "XHRReadyStateChange", "data", Inspect
orXhrReadyStateChangeEvent::data(m_target->executionContext(), m_target)); |
| 131 m_target->dispatchEvent(Event::create(EventTypeNames::readystatechange))
; |
| 132 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Up
dateCounters", TRACE_EVENT_SCOPE_THREAD, "data", InspectorUpdateCountersEvent::d
ata()); |
| 133 } |
| 134 |
| 135 if (m_target->readyState() != state) |
| 136 return; |
| 137 |
| 138 m_hasDispatchedProgressProgressEvent = true; |
| 139 m_target->dispatchEvent(progressEvent); |
| 140 } |
| 141 |
| 142 void XMLHttpRequestProgressEventThrottle::fired() |
| 143 { |
| 144 if (!m_deferred.isSet()) { |
| 145 // No "progress" event was queued since the previous dispatch, we can |
| 146 // safely stop the timer. |
| 147 return; |
| 148 } |
| 149 |
| 150 dispatchProgressProgressEvent(m_deferred.take()); |
| 151 |
| 152 // Watch if another "progress" ProgressEvent arrives in the next 50ms. |
| 153 startOneShot(minimumProgressEventDispatchingIntervalInSeconds, BLINK_FROM_HE
RE); |
| 154 } |
| 155 |
| 156 void XMLHttpRequestProgressEventThrottle::suspend() |
| 157 { |
| 158 stop(); |
| 159 } |
| 160 |
| 161 void XMLHttpRequestProgressEventThrottle::resume() |
| 162 { |
| 163 if (!m_deferred.isSet()) |
| 164 return; |
| 165 |
| 166 // Do not dispatch events inline here, since ExecutionContext is iterating |
| 167 // over the list of active DOM objects to resume them, and any activated JS |
| 168 // event-handler could insert new active DOM objects to the list. |
| 169 startOneShot(0, BLINK_FROM_HERE); |
| 170 } |
| 171 |
| 172 DEFINE_TRACE(XMLHttpRequestProgressEventThrottle) |
| 173 { |
| 174 visitor->trace(m_target); |
| 175 } |
| 176 |
| 177 } // namespace blink |
OLD | NEW |