1/*
2 * Copyright (C) 2013 University of Washington. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights 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 *
9 * 1.  Redistributions of source code must retain the above copyright
10 *     notice, this list of conditions and the following disclaimer.
11 * 2.  Redistributions in binary form must reproduce the above copyright
12 *     notice, this list of conditions and the following disclaimer in the
13 *     documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef EventLoopInputDispatcher_h
29#define EventLoopInputDispatcher_h
30
31#if ENABLE(WEB_REPLAY)
32
33#include "EventLoopInput.h"
34#include "Timer.h"
35#include <wtf/Noncopyable.h>
36#include <wtf/Vector.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41class Page;
42class ReplayingInputCursor;
43
44enum class DispatchSpeed {
45    RealTime,
46    FastForward,
47};
48
49class EventLoopInputDispatcherClient {
50public:
51    EventLoopInputDispatcherClient() { }
52    virtual ~EventLoopInputDispatcherClient() { }
53
54    virtual void willDispatchInput(const EventLoopInputBase&) =0;
55    virtual void didDispatchInput(const EventLoopInputBase&) =0;
56    virtual void didDispatchFinalInput() =0;
57};
58
59class EventLoopInputDispatcher {
60    WTF_MAKE_NONCOPYABLE(EventLoopInputDispatcher);
61public:
62    EventLoopInputDispatcher(Page&, ReplayingInputCursor&, EventLoopInputDispatcherClient*);
63
64    void run();
65    void pause();
66
67    void setDispatchSpeed(DispatchSpeed speed) { m_speed = speed; }
68    DispatchSpeed dispatchSpeed() const { return m_speed; }
69
70    bool isRunning() const { return m_running; }
71    bool isDispatching() const { return m_dispatching; }
72private:
73    void dispatchInputSoon();
74    void dispatchInput();
75    void timerFired(Timer<EventLoopInputDispatcher>*);
76
77    Page& m_page;
78    EventLoopInputDispatcherClient* m_client;
79    ReplayingInputCursor& m_cursor;
80    Timer<EventLoopInputDispatcher> m_timer;
81
82    // This pointer is valid when an event loop input is presently dispatching.
83    EventLoopInputBase* m_runningInput;
84    // Whether the dispatcher is currently calling out to an inputs' dispatch() method.
85    bool m_dispatching;
86    // Whether the dispatcher is waiting to dispatch or actively dispatching inputs.
87    bool m_running;
88
89    DispatchSpeed m_speed;
90    // The time at which the last input dispatch() method was called.
91    double m_previousDispatchStartTime;
92    // The timestamp specified by the last dispatched input.
93    double m_previousInputTimestamp;
94};
95
96} // namespace WebCore
97
98#endif // ENABLE(WEB_REPLAY)
99
100#endif // EventLoopInputDispatcher_h
101