Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / PageWidgetDelegate.cpp
1 /*
2  * Copyright (C) 2012 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "web/PageWidgetDelegate.h"
33
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/page/AutoscrollController.h"
37 #include "core/page/EventHandler.h"
38 #include "core/page/Page.h"
39 #include "core/rendering/RenderView.h"
40 #include "core/rendering/compositing/RenderLayerCompositor.h"
41 #include "platform/graphics/GraphicsContext.h"
42 #include "public/web/WebInputEvent.h"
43 #include "web/PageOverlayList.h"
44 #include "web/WebInputEventConversion.h"
45 #include "wtf/CurrentTime.h"
46
47 namespace blink {
48
49 static inline FrameView* mainFrameView(Page* page)
50 {
51     if (!page)
52         return 0;
53     // FIXME: Can we remove this check?
54     if (!page->mainFrame())
55         return 0;
56     if (!page->mainFrame()->isLocalFrame())
57         return 0;
58     return page->deprecatedLocalMainFrame()->view();
59 }
60
61 void PageWidgetDelegate::animate(Page* page, double monotonicFrameBeginTime)
62 {
63     RefPtr<FrameView> view = mainFrameView(page);
64     if (!view)
65         return;
66     page->autoscrollController().animate(monotonicFrameBeginTime);
67     page->animator().serviceScriptedAnimations(monotonicFrameBeginTime);
68 }
69
70 void PageWidgetDelegate::layout(Page* page, LocalFrame* rootFrame)
71 {
72     if (!page)
73         return;
74
75     if (!rootFrame) {
76         if (!page->mainFrame() || !page->mainFrame()->isLocalFrame())
77             return;
78         rootFrame = toLocalFrame(page->mainFrame());
79     }
80
81     page->animator().updateLayoutAndStyleForPainting(rootFrame);
82 }
83
84 void PageWidgetDelegate::paint(Page* page, PageOverlayList* overlays, WebCanvas* canvas, const WebRect& rect, CanvasBackground background)
85 {
86     if (rect.isEmpty())
87         return;
88     GraphicsContext gc(canvas);
89     gc.setCertainlyOpaque(background == Opaque);
90     gc.applyDeviceScaleFactor(page->deviceScaleFactor());
91     gc.setDeviceScaleFactor(page->deviceScaleFactor());
92     IntRect dirtyRect(rect);
93     gc.save(); // Needed to save the canvas, not the GraphicsContext.
94     FrameView* view = mainFrameView(page);
95     // FIXME: Can we remove the mainFrame()->document() check?
96     if (view && page->deprecatedLocalMainFrame()->document()) {
97         gc.clip(dirtyRect);
98         view->paint(&gc, dirtyRect);
99         if (overlays)
100             overlays->paintWebFrame(gc);
101     } else {
102         gc.fillRect(dirtyRect, Color::white);
103     }
104     gc.restore();
105 }
106
107 bool PageWidgetDelegate::handleInputEvent(Page* page, PageWidgetEventHandler& handler, const WebInputEvent& event)
108 {
109     LocalFrame* frame = page && page->mainFrame()->isLocalFrame() ? page->deprecatedLocalMainFrame() : 0;
110     switch (event.type) {
111
112     // FIXME: WebKit seems to always return false on mouse events processing
113     // methods. For now we'll assume it has processed them (as we are only
114     // interested in whether keyboard events are processed).
115     case WebInputEvent::MouseMove:
116         if (!frame || !frame->view())
117             return true;
118         handler.handleMouseMove(*frame, static_cast<const WebMouseEvent&>(event));
119         return true;
120     case WebInputEvent::MouseLeave:
121         if (!frame || !frame->view())
122             return true;
123         handler.handleMouseLeave(*frame, static_cast<const WebMouseEvent&>(event));
124         return true;
125     case WebInputEvent::MouseDown:
126         if (!frame || !frame->view())
127             return true;
128         handler.handleMouseDown(*frame, static_cast<const WebMouseEvent&>(event));
129         return true;
130     case WebInputEvent::MouseUp:
131         if (!frame || !frame->view())
132             return true;
133         handler.handleMouseUp(*frame, static_cast<const WebMouseEvent&>(event));
134         return true;
135
136     case WebInputEvent::MouseWheel:
137         if (!frame || !frame->view())
138             return false;
139         return handler.handleMouseWheel(*frame, static_cast<const WebMouseWheelEvent&>(event));
140
141     case WebInputEvent::RawKeyDown:
142     case WebInputEvent::KeyDown:
143     case WebInputEvent::KeyUp:
144         return handler.handleKeyEvent(static_cast<const WebKeyboardEvent&>(event));
145
146     case WebInputEvent::Char:
147         return handler.handleCharEvent(static_cast<const WebKeyboardEvent&>(event));
148     case WebInputEvent::GestureScrollBegin:
149     case WebInputEvent::GestureScrollEnd:
150     case WebInputEvent::GestureScrollUpdate:
151     case WebInputEvent::GestureScrollUpdateWithoutPropagation:
152     case WebInputEvent::GestureFlingStart:
153     case WebInputEvent::GestureFlingCancel:
154     case WebInputEvent::GestureTap:
155     case WebInputEvent::GestureTapUnconfirmed:
156     case WebInputEvent::GestureTapDown:
157     case WebInputEvent::GestureShowPress:
158     case WebInputEvent::GestureTapCancel:
159     case WebInputEvent::GestureDoubleTap:
160     case WebInputEvent::GestureTwoFingerTap:
161     case WebInputEvent::GestureLongPress:
162     case WebInputEvent::GestureLongTap:
163         return handler.handleGestureEvent(static_cast<const WebGestureEvent&>(event));
164
165     case WebInputEvent::TouchStart:
166     case WebInputEvent::TouchMove:
167     case WebInputEvent::TouchEnd:
168     case WebInputEvent::TouchCancel:
169         if (!frame || !frame->view())
170             return false;
171         return handler.handleTouchEvent(*frame, static_cast<const WebTouchEvent&>(event));
172
173     case WebInputEvent::GesturePinchBegin:
174     case WebInputEvent::GesturePinchEnd:
175     case WebInputEvent::GesturePinchUpdate:
176         // FIXME: Once PlatformGestureEvent is updated to support pinch, this
177         // should call handleGestureEvent, just like it currently does for
178         // gesture scroll.
179         return false;
180
181     default:
182         return false;
183     }
184 }
185
186 // ----------------------------------------------------------------
187 // Default handlers for PageWidgetEventHandler
188
189 void PageWidgetEventHandler::handleMouseMove(LocalFrame& mainFrame, const WebMouseEvent& event)
190 {
191     mainFrame.eventHandler().handleMouseMoveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
192 }
193
194 void PageWidgetEventHandler::handleMouseLeave(LocalFrame& mainFrame, const WebMouseEvent& event)
195 {
196     mainFrame.eventHandler().handleMouseLeaveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
197 }
198
199 void PageWidgetEventHandler::handleMouseDown(LocalFrame& mainFrame, const WebMouseEvent& event)
200 {
201     mainFrame.eventHandler().handleMousePressEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
202 }
203
204 void PageWidgetEventHandler::handleMouseUp(LocalFrame& mainFrame, const WebMouseEvent& event)
205 {
206     mainFrame.eventHandler().handleMouseReleaseEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
207 }
208
209 bool PageWidgetEventHandler::handleMouseWheel(LocalFrame& mainFrame, const WebMouseWheelEvent& event)
210 {
211     return mainFrame.eventHandler().handleWheelEvent(PlatformWheelEventBuilder(mainFrame.view(), event));
212 }
213
214 bool PageWidgetEventHandler::handleTouchEvent(LocalFrame& mainFrame, const WebTouchEvent& event)
215 {
216     return mainFrame.eventHandler().handleTouchEvent(PlatformTouchEventBuilder(mainFrame.view(), event));
217 }
218
219 } // namespace blink