2 * Copyright (C) 2010 Julien Chaffraix <jchaffraix@webkit.org> All right reserved.
3 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "core/xmlhttprequest/XMLHttpRequestProgressEventThrottle.h"
30 #include "core/EventTypeNames.h"
31 #include "core/xmlhttprequest/XMLHttpRequest.h"
32 #include "core/xmlhttprequest/XMLHttpRequestProgressEvent.h"
33 #include "wtf/Assertions.h"
34 #include "wtf/text/AtomicString.h"
38 class XMLHttpRequestProgressEventThrottle::DeferredEvent
{
40 DeferredEvent() { clear(); }
41 void set(bool lengthComputable
, unsigned long long loaded
, unsigned long long total
)
44 m_lengthComputable
= lengthComputable
;
51 m_lengthComputable
= false;
55 bool isDeferred() const { return m_isDeferred
; }
56 bool lengthComputable() const { return m_lengthComputable
; }
57 unsigned long long loaded() const { return m_loaded
; }
58 unsigned long long total() const { return m_total
; }
61 unsigned long long m_loaded
;
62 unsigned long long m_total
;
64 bool m_lengthComputable
;
67 const double XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchingIntervalInSeconds
= .05; // 50 ms per specification.
69 XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(XMLHttpRequest
* target
)
71 , m_deferred(adoptPtr(new DeferredEvent
))
76 XMLHttpRequestProgressEventThrottle::~XMLHttpRequestProgressEventThrottle()
80 void XMLHttpRequestProgressEventThrottle::dispatchProgressEvent(const AtomicString
& type
, bool lengthComputable
, unsigned long long loaded
, unsigned long long total
)
82 // Given that ResourceDispatcher doesn't deliver an event when suspended,
83 // we don't have to worry about event dispatching while suspended.
84 if (type
!= EventTypeNames::progress
) {
85 m_target
->dispatchEvent(XMLHttpRequestProgressEvent::create(type
, lengthComputable
, loaded
, total
));
90 m_deferred
->set(lengthComputable
, loaded
, total
);
92 m_target
->dispatchEvent(XMLHttpRequestProgressEvent::create(type
, lengthComputable
, loaded
, total
));
93 startOneShot(minimumProgressEventDispatchingIntervalInSeconds
, FROM_HERE
);
97 void XMLHttpRequestProgressEventThrottle::dispatchReadyStateChangeEvent(PassRefPtrWillBeRawPtr
<Event
> event
, DeferredEventAction action
)
99 XMLHttpRequest::State state
= m_target
->readyState();
100 // Given that ResourceDispatcher doesn't deliver an event when suspended,
101 // we don't have to worry about event dispatching while suspended.
102 if (action
== Flush
) {
103 dispatchDeferredEvent();
104 // |m_target| is protected by the caller.
106 } else if (action
== Clear
) {
111 if (state
== m_target
->readyState()) {
112 // We don't dispatch the event when an event handler associated with
113 // the previously dispatched event changes the readyState (e.g. when
114 // the event handler calls xhr.abort()). In such cases a
115 // readystatechange should have been already dispatched if necessary.
116 m_target
->dispatchEvent(event
);
120 void XMLHttpRequestProgressEventThrottle::dispatchDeferredEvent()
122 if (m_deferred
->isDeferred()) {
123 m_target
->dispatchEvent(XMLHttpRequestProgressEvent::create(EventTypeNames::progress
, m_deferred
->lengthComputable(), m_deferred
->loaded(), m_deferred
->total()));
128 void XMLHttpRequestProgressEventThrottle::fired()
130 if (!m_deferred
->isDeferred()) {
131 // No "progress" event was queued since the previous dispatch, we can
132 // safely stop the timer.
136 dispatchDeferredEvent();
138 // Watch if another "progress" ProgressEvent arrives in the next 50ms.
139 startOneShot(minimumProgressEventDispatchingIntervalInSeconds
, FROM_HERE
);
142 void XMLHttpRequestProgressEventThrottle::suspend()
147 void XMLHttpRequestProgressEventThrottle::resume()
149 if (!m_deferred
->isDeferred())
152 // Do not dispatch events inline here, since ExecutionContext is iterating
153 // over the list of active DOM objects to resume them, and any activated JS
154 // event-handler could insert new active DOM objects to the list.
155 startOneShot(0, FROM_HERE
);
158 DEFINE_TRACE(XMLHttpRequestProgressEventThrottle
)
160 visitor
->trace(m_target
);