OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2010 Julien Chaffraix <jchaffraix@webkit.org> |
| 3 * All right reserved. |
| 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 #ifndef XMLHttpRequestProgressEventThrottle_h |
| 28 #define XMLHttpRequestProgressEventThrottle_h |
| 29 |
| 30 #include "platform/Timer.h" |
| 31 #include "platform/heap/Handle.h" |
| 32 #include "wtf/Forward.h" |
| 33 #include "wtf/PassRefPtr.h" |
| 34 |
| 35 namespace blink { |
| 36 |
| 37 class Event; |
| 38 class XMLHttpRequest; |
| 39 |
| 40 // This class implements the XHR2 ProgressEvent dispatching: |
| 41 // "dispatch a progress event named progress about every 50ms or for every |
| 42 // byte received, whichever is least frequent". |
| 43 // |
| 44 // readystatechange events also go through this class since ProgressEvents and |
| 45 // readystatechange events affect each other. |
| 46 // |
| 47 // In the comments for this class: |
| 48 // - "progress" event means an event named "progress" |
| 49 // - ProgressEvent means an event using the ProgressEvent interface defined in |
| 50 // the spec. |
| 51 class XMLHttpRequestProgressEventThrottle final : public GarbageCollectedFinaliz
ed<XMLHttpRequestProgressEventThrottle>, public TimerBase { |
| 52 public: |
| 53 static XMLHttpRequestProgressEventThrottle* create(XMLHttpRequest* eventTarg
et) |
| 54 { |
| 55 return new XMLHttpRequestProgressEventThrottle(eventTarget); |
| 56 } |
| 57 ~XMLHttpRequestProgressEventThrottle() override; |
| 58 |
| 59 enum DeferredEventAction { |
| 60 Ignore, |
| 61 Clear, |
| 62 Flush, |
| 63 }; |
| 64 |
| 65 // Dispatches a ProgressEvent. |
| 66 // |
| 67 // Special treatment for events named "progress" is implemented to dispatch |
| 68 // them at the required frequency. If this object is suspended, the given |
| 69 // ProgressEvent overwrites the existing. I.e. only the latest one gets |
| 70 // queued. If the timer is running, this method just updates |
| 71 // m_lengthComputable, m_loaded and m_total. They'll be used on next |
| 72 // fired() call. |
| 73 // For an event named "progress", a readyStateChange will be dispatched |
| 74 // as well. |
| 75 void dispatchProgressEvent(const AtomicString&, bool lengthComputable, unsig
ned long long loaded, unsigned long long total); |
| 76 // Dispatches the given event after operation about the "progress" event |
| 77 // depending on the value of the ProgressEventAction argument. |
| 78 void dispatchReadyStateChangeEvent(PassRefPtrWillBeRawPtr<Event>, DeferredEv
entAction); |
| 79 |
| 80 void suspend(); |
| 81 void resume(); |
| 82 |
| 83 // Need to promptly stop this timer when it is deemed finalizable. |
| 84 EAGERLY_FINALIZE(); |
| 85 DECLARE_TRACE(); |
| 86 |
| 87 private: |
| 88 explicit XMLHttpRequestProgressEventThrottle(XMLHttpRequest*); |
| 89 |
| 90 // Dispatches a "progress" progress event and usually a readyStateChange |
| 91 // event as well. |
| 92 void dispatchProgressProgressEvent(PassRefPtrWillBeRawPtr<Event>); |
| 93 |
| 94 // The main purpose of this class is to throttle the "progress" |
| 95 // ProgressEvent dispatching. This class represents such a deferred |
| 96 // "progress" ProgressEvent. |
| 97 class DeferredEvent { |
| 98 public: |
| 99 DeferredEvent(); |
| 100 void set(bool lengthComputable, unsigned long long loaded, unsigned long
long total); |
| 101 void clear(); |
| 102 bool isSet() const { return m_isSet; } |
| 103 PassRefPtrWillBeRawPtr<Event> take(); |
| 104 |
| 105 private: |
| 106 unsigned long long m_loaded; |
| 107 unsigned long long m_total; |
| 108 bool m_lengthComputable; |
| 109 |
| 110 bool m_isSet; |
| 111 }; |
| 112 |
| 113 static const double minimumProgressEventDispatchingIntervalInSeconds; |
| 114 |
| 115 void fired() override; |
| 116 |
| 117 Member<XMLHttpRequest> m_target; |
| 118 |
| 119 // A slot for the deferred "progress" ProgressEvent. When multiple events |
| 120 // arrive, only the last one is stored and others are discarded. |
| 121 DeferredEvent m_deferred; |
| 122 |
| 123 // True if any "progress" progress event has been dispatched since |
| 124 // |m_target|'s readyState changed. |
| 125 bool m_hasDispatchedProgressProgressEvent; |
| 126 }; |
| 127 |
| 128 } // namespace blink |
| 129 |
| 130 #endif // XMLHttpRequestProgressEventThrottle_h |
OLD | NEW |