Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / xmlhttprequest / XMLHttpRequestProgressEventThrottle.cpp
blobf3d619673fd4845ec63c13e1302507978faad4de
1 /*
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
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.
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.
27 #include "config.h"
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"
36 namespace blink {
38 class XMLHttpRequestProgressEventThrottle::DeferredEvent {
39 public:
40 DeferredEvent() { clear(); }
41 void set(bool lengthComputable, unsigned long long loaded, unsigned long long total)
43 m_isDeferred = true;
44 m_lengthComputable = lengthComputable;
45 m_loaded = loaded;
46 m_total = total;
48 void clear()
50 m_isDeferred = false;
51 m_lengthComputable = false;
52 m_loaded = 0;
53 m_total = 0;
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; }
60 private:
61 unsigned long long m_loaded;
62 unsigned long long m_total;
63 bool m_isDeferred;
64 bool m_lengthComputable;
67 const double XMLHttpRequestProgressEventThrottle::minimumProgressEventDispatchingIntervalInSeconds = .05; // 50 ms per specification.
69 XMLHttpRequestProgressEventThrottle::XMLHttpRequestProgressEventThrottle(XMLHttpRequest* target)
70 : m_target(target)
71 , m_deferred(adoptPtr(new DeferredEvent))
73 ASSERT(target);
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));
86 return;
89 if (isActive()) {
90 m_deferred->set(lengthComputable, loaded, total);
91 } else {
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.
105 stop();
106 } else if (action == Clear) {
107 m_deferred->clear();
108 stop();
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()));
124 m_deferred->clear();
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.
133 return;
136 dispatchDeferredEvent();
138 // Watch if another "progress" ProgressEvent arrives in the next 50ms.
139 startOneShot(minimumProgressEventDispatchingIntervalInSeconds, FROM_HERE);
142 void XMLHttpRequestProgressEventThrottle::suspend()
144 stop();
147 void XMLHttpRequestProgressEventThrottle::resume()
149 if (!m_deferred->isDeferred())
150 return;
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);
163 } // namespace blink