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/OwnPtr.h"
33#include "wtf/PassRefPtr.h"
34#include "wtf/Vector.h"
35#include "wtf/text/AtomicString.h"
36
37namespace blink {
38
39class Event;
40class EventTarget;
41class XMLHttpRequestProgressEvent;
42
43// This class implements the XHR2 ProgressEvent dispatching:
44//   "dispatch a progress event named progress about every 50ms or for every
45//   byte received, whichever is least frequent".
46//
47// readystatechange events also go through this class since ProgressEvents and
48// readystatechange events affect each other.
49//
50// In the comments for this class:
51// - "progress" event means an event named "progress"
52// - ProgressEvent means an event using the ProgressEvent interface defined in
53//   the spec.
54class XMLHttpRequestProgressEventThrottle FINAL : public TimerBase {
55    DISALLOW_ALLOCATION();
56public:
57    enum DeferredEventAction {
58        Ignore,
59        Clear,
60        Flush,
61    };
62
63    explicit XMLHttpRequestProgressEventThrottle(EventTarget*);
64    virtual ~XMLHttpRequestProgressEventThrottle();
65
66    // Dispatches a ProgressEvent.
67    //
68    // Special treatment for events named "progress" is implemented to dispatch
69    // them at the required frequency. If this object is suspended, the given
70    // ProgressEvent overwrites the existing. I.e. only the latest one gets
71    // queued. If the timer is running, this method just updates
72    // m_lengthComputable, m_loaded and m_total. They'll be used on next
73    // fired() call.
74    void dispatchProgressEvent(const AtomicString&, bool lengthComputable, unsigned long long loaded, unsigned long long total);
75    // Dispatches the given event after operation about the "progress" event
76    // depending on the value of the ProgressEventAction argument.
77    void dispatchReadyStateChangeEvent(PassRefPtrWillBeRawPtr<Event>, DeferredEventAction);
78
79    void suspend();
80    void resume();
81
82    void trace(Visitor*);
83
84private:
85    // The main purpose of this class is to throttle the "progress"
86    // ProgressEvent dispatching. This class represents such a deferred
87    // "progress" ProgressEvent.
88    class DeferredEvent;
89    static const double minimumProgressEventDispatchingIntervalInSeconds;
90
91    virtual void fired() OVERRIDE;
92    void dispatchDeferredEvent();
93
94    // Non-Oilpan, keep a weak pointer to our XMLHttpRequest object as it is
95    // the one holding us. With Oilpan, a simple strong Member can be used -
96    // this XMLHttpRequestProgressEventThrottle (part) object dies together
97    // with the XMLHttpRequest object.
98    RawPtrWillBeMember<EventTarget> m_target;
99
100    // A slot for the deferred "progress" ProgressEvent. When multiple events
101    // arrive, only the last one is stored and others are discarded.
102    const OwnPtr<DeferredEvent> m_deferred;
103};
104
105} // namespace blink
106
107#endif // XMLHttpRequestProgressEventThrottle_h
108