Merge "Fixed paste issue after selecting word in inputbox" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebPage / EventDispatcher.cpp
1 /*
2  * Copyright (C) 2011 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 #include "config.h"
27 #include "EventDispatcher.h"
28
29 #include "WebEvent.h"
30 #include "WebEventConversion.h"
31 #include "WebPage.h"
32 #include "WebPageProxyMessages.h"
33 #include "WebProcess.h"
34 #include <WebCore/Page.h>
35 #include <WebCore/RunLoop.h>
36 #include <wtf/MainThread.h>
37
38 #if ENABLE(THREADED_SCROLLING)
39 #include <WebCore/ScrollingCoordinator.h>
40 #include <WebCore/ScrollingThread.h>
41 #include <WebCore/ScrollingTree.h>
42 #endif
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 EventDispatcher::EventDispatcher()
49 {
50 }
51
52 EventDispatcher::~EventDispatcher()
53 {
54 }
55
56 #if ENABLE(THREADED_SCROLLING)
57 void EventDispatcher::addScrollingTreeForPage(WebPage* webPage)
58 {
59     MutexLocker locker(m_scrollingTreesMutex);
60
61     ASSERT(webPage->corePage()->scrollingCoordinator());
62     ASSERT(!m_scrollingTrees.contains(webPage->pageID()));
63     m_scrollingTrees.set(webPage->pageID(), webPage->corePage()->scrollingCoordinator()->scrollingTree());
64 }
65
66 void EventDispatcher::removeScrollingTreeForPage(WebPage* webPage)
67 {
68     MutexLocker locker(m_scrollingTreesMutex);
69     ASSERT(m_scrollingTrees.contains(webPage->pageID()));
70
71     m_scrollingTrees.remove(webPage->pageID());
72 }
73 #endif
74
75 void EventDispatcher::didReceiveMessageOnConnectionWorkQueue(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* arguments, bool& didHandleMessage)
76 {
77     if (messageID.is<CoreIPC::MessageClassEventDispatcher>()) {
78         didReceiveEventDispatcherMessageOnConnectionWorkQueue(connection, messageID, arguments, didHandleMessage);
79         return;
80     }
81 }
82
83 void EventDispatcher::wheelEvent(CoreIPC::Connection*, uint64_t pageID, const WebWheelEvent& wheelEvent, bool canGoBack, bool canGoForward)
84 {
85 #if ENABLE(THREADED_SCROLLING)
86     MutexLocker locker(m_scrollingTreesMutex);
87     if (ScrollingTree* scrollingTree = m_scrollingTrees.get(pageID).get()) {
88         PlatformWheelEvent platformWheelEvent = platform(wheelEvent);
89
90         // FIXME: It's pretty horrible that we're updating the back/forward state here.
91         // WebCore should always know the current state and know when it changes so the
92         // scrolling tree can be notified.
93         // We only need to do this at the beginning of the gesture.
94         if (platformWheelEvent.phase() == PlatformWheelEventPhaseBegan)
95             ScrollingThread::dispatch(bind(&ScrollingTree::updateBackForwardState, scrollingTree, canGoBack, canGoForward));
96
97         ScrollingTree::EventResult result = scrollingTree->tryToHandleWheelEvent(platformWheelEvent);
98         if (result == ScrollingTree::DidHandleEvent || result == ScrollingTree::DidNotHandleEvent) {
99             sendDidReceiveEvent(pageID, wheelEvent, result == ScrollingTree::DidHandleEvent);
100             return;
101         }
102     }
103 #else
104     UNUSED_PARAM(canGoBack);
105     UNUSED_PARAM(canGoForward);
106 #endif
107
108     RunLoop::main()->dispatch(bind(&EventDispatcher::dispatchWheelEvent, this, pageID, wheelEvent));
109 }
110
111 #if ENABLE(GESTURE_EVENTS)
112 void EventDispatcher::gestureEvent(CoreIPC::Connection*, uint64_t pageID, const WebGestureEvent& gestureEvent)
113 {
114     RunLoop::main()->dispatch(bind(&EventDispatcher::dispatchGestureEvent, this, pageID, gestureEvent));
115 }
116 #endif
117
118 void EventDispatcher::dispatchWheelEvent(uint64_t pageID, const WebWheelEvent& wheelEvent)
119 {
120     ASSERT(isMainThread());
121
122     WebPage* webPage = WebProcess::shared().webPage(pageID);
123     if (!webPage)
124         return;
125
126     webPage->wheelEvent(wheelEvent);
127 }
128
129 #if ENABLE(GESTURE_EVENTS)
130 void EventDispatcher::dispatchGestureEvent(uint64_t pageID, const WebGestureEvent& gestureEvent)
131 {
132     ASSERT(isMainThread());
133
134     WebPage* webPage = WebProcess::shared().webPage(pageID);
135     if (!webPage)
136         return;
137
138     webPage->gestureEvent(gestureEvent);
139 }
140 #endif
141
142 #if ENABLE(THREADED_SCROLLING)
143 void EventDispatcher::sendDidReceiveEvent(uint64_t pageID, const WebEvent& event, bool didHandleEvent)
144 {
145     WebProcess::shared().connection()->send(Messages::WebPageProxy::DidReceiveEvent(static_cast<uint32_t>(event.type()), didHandleEvent), pageID);
146 }
147 #endif
148
149 } // namespace WebKit