XMLHttpRequestProgressEventThrottle.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef XMLHttpRequestProgressEventThrottle_h
  27. #define XMLHttpRequestProgressEventThrottle_h
  28. #include "Timer.h"
  29. #include "wtf/PassRefPtr.h"
  30. #include "wtf/Vector.h"
  31. namespace WebCore {
  32. class Event;
  33. class EventTarget;
  34. enum ProgressEventAction {
  35. DoNotFlushProgressEvent,
  36. FlushProgressEvent
  37. };
  38. // This implements the XHR2 progress event dispatching: "dispatch a progress event called progress
  39. // about every 50ms or for every byte received, whichever is least frequent".
  40. class XMLHttpRequestProgressEventThrottle : public TimerBase {
  41. public:
  42. explicit XMLHttpRequestProgressEventThrottle(EventTarget*);
  43. virtual ~XMLHttpRequestProgressEventThrottle();
  44. void dispatchProgressEvent(bool lengthComputable, unsigned long long loaded, unsigned long long total);
  45. void dispatchReadyStateChangeEvent(PassRefPtr<Event>, ProgressEventAction = DoNotFlushProgressEvent);
  46. void dispatchEvent(PassRefPtr<Event>);
  47. void dispatchEventAndLoadEnd(PassRefPtr<Event>);
  48. void suspend();
  49. void resume();
  50. private:
  51. static const double minimumProgressEventDispatchingIntervalInSeconds;
  52. virtual void fired();
  53. void dispatchDeferredEvents(Timer<XMLHttpRequestProgressEventThrottle>*);
  54. void flushProgressEvent();
  55. bool hasEventToDispatch() const;
  56. // Weak pointer to our XMLHttpRequest object as it is the one holding us.
  57. EventTarget* m_target;
  58. bool m_lengthComputable;
  59. unsigned long long m_loaded;
  60. unsigned long long m_total;
  61. bool m_deferEvents;
  62. RefPtr<Event> m_deferredProgressEvent;
  63. Vector<RefPtr<Event> > m_deferredEvents;
  64. Timer<XMLHttpRequestProgressEventThrottle> m_dispatchDeferredEventsTimer;
  65. };
  66. } // namespace WebCore
  67. #endif // XMLHttpRequestProgressEventThrottle_h