1/*
2 * Copyright (C) 2011, 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef EventDispatcher_h
27#define EventDispatcher_h
28
29#include "Connection.h"
30
31#include <WebCore/WheelEventDeltaTracker.h>
32#include <WebEvent.h>
33#include <memory>
34#include <wtf/HashMap.h>
35#include <wtf/Noncopyable.h>
36#include <wtf/RefPtr.h>
37#include <wtf/TCSpinLock.h>
38#include <wtf/ThreadingPrimitives.h>
39
40namespace WebCore {
41class ThreadedScrollingTree;
42}
43
44namespace WebKit {
45
46class WebEvent;
47class WebPage;
48class WebWheelEvent;
49
50class EventDispatcher : public IPC::Connection::WorkQueueMessageReceiver {
51public:
52    static PassRefPtr<EventDispatcher> create();
53    ~EventDispatcher();
54
55#if ENABLE(ASYNC_SCROLLING)
56    void addScrollingTreeForPage(WebPage*);
57    void removeScrollingTreeForPage(WebPage*);
58#endif
59
60#if ENABLE(IOS_TOUCH_EVENTS)
61    typedef Vector<WebTouchEvent, 1> TouchEventQueue;
62
63    void clearQueuedTouchEventsForPage(const WebPage&);
64    void getQueuedTouchEventsForPage(const WebPage&, TouchEventQueue&);
65#endif
66
67    void initializeConnection(IPC::Connection*);
68
69private:
70    EventDispatcher();
71
72    // IPC::Connection::WorkQueueMessageReceiver.
73    virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
74
75    // Message handlers
76    void wheelEvent(uint64_t pageID, const WebWheelEvent&, bool canRubberBandAtLeft, bool canRubberBandAtRight, bool canRubberBandAtTop, bool canRubberBandAtBottom);
77#if ENABLE(IOS_TOUCH_EVENTS)
78    void touchEvent(uint64_t pageID, const WebTouchEvent&);
79#endif
80
81
82    // This is called on the main thread.
83    void dispatchWheelEvent(uint64_t pageID, const WebWheelEvent&);
84#if ENABLE(IOS_TOUCH_EVENTS)
85    void dispatchTouchEvents();
86#endif
87
88#if ENABLE(ASYNC_SCROLLING)
89    void sendDidReceiveEvent(uint64_t pageID, const WebEvent&, bool didHandleEvent);
90#endif
91
92    RefPtr<WorkQueue> m_queue;
93
94#if ENABLE(ASYNC_SCROLLING)
95    Mutex m_scrollingTreesMutex;
96    HashMap<uint64_t, RefPtr<WebCore::ThreadedScrollingTree>> m_scrollingTrees;
97#endif
98    std::unique_ptr<WebCore::WheelEventDeltaTracker> m_recentWheelEventDeltaTracker;
99#if ENABLE(IOS_TOUCH_EVENTS)
100    SpinLock m_touchEventsLock;
101    HashMap<uint64_t, TouchEventQueue> m_touchEvents;
102#endif
103};
104
105} // namespace WebKit
106
107#endif // EventDispatcher_h
108