Upstream version 7.36.149.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/rendering/RenderView.h"
39 #include "core/rendering/compositing/RenderLayerCompositor.h"
40 #include "platform/graphics/GraphicsContext.h"
41 #include "public/web/WebInputEvent.h"
42 #include "web/PageOverlayList.h"
43 #include "web/WebInputEventConversion.h"
44 #include "wtf/CurrentTime.h"
45
46 using namespace WebCore;
47
48 namespace blink {
49
50 static inline FrameView* mainFrameView(Page* page)
51 {
52     if (!page)
53         return 0;
54     // FIXME: Can we remove this check?
55     if (!page->mainFrame())
56         return 0;
57     return page->mainFrame()->view();
58 }
59
60 void PageWidgetDelegate::animate(Page* page, double monotonicFrameBeginTime)
61 {
62     RefPtr<FrameView> view = mainFrameView(page);
63     if (!view)
64         return;
65     page->autoscrollController().animate(monotonicFrameBeginTime);
66     page->animator().serviceScriptedAnimations(monotonicFrameBeginTime);
67 }
68
69 void PageWidgetDelegate::layout(Page* page)
70 {
71     if (!page || !page->mainFrame())
72         return;
73     page->animator().updateLayoutAndStyleForPainting();
74 }
75
76 void PageWidgetDelegate::paint(Page* page, PageOverlayList* overlays, WebCanvas* canvas, const WebRect& rect, CanvasBackground background)
77 {
78     if (rect.isEmpty())
79         return;
80     GraphicsContext gc(canvas);
81     gc.setCertainlyOpaque(background == Opaque);
82     gc.applyDeviceScaleFactor(page->deviceScaleFactor());
83     gc.setUseHighResMarkers(page->deviceScaleFactor() > 1.5f);
84     IntRect dirtyRect(rect);
85     gc.save(); // Needed to save the canvas, not the GraphicsContext.
86     FrameView* view = mainFrameView(page);
87     // FIXME: Can we remove the mainFrame()->document() check?
88     if (view && page->mainFrame()->document()) {
89         gc.clip(dirtyRect);
90         view->paint(&gc, dirtyRect);
91         if (overlays)
92             overlays->paintWebFrame(gc);
93     } else {
94         gc.fillRect(dirtyRect, Color::white);
95     }
96     gc.restore();
97 }
98
99 bool PageWidgetDelegate::handleInputEvent(Page* page, PageWidgetEventHandler& handler, const WebInputEvent& event)
100 {
101     LocalFrame* frame = page ? page->mainFrame() : 0;
102     switch (event.type) {
103
104     // FIXME: WebKit seems to always return false on mouse events processing
105     // methods. For now we'll assume it has processed them (as we are only
106     // interested in whether keyboard events are processed).
107     case WebInputEvent::MouseMove:
108         if (!frame || !frame->view())
109             return true;
110         handler.handleMouseMove(*frame, *static_cast<const WebMouseEvent*>(&event));
111         return true;
112     case WebInputEvent::MouseLeave:
113         if (!frame || !frame->view())
114             return true;
115         handler.handleMouseLeave(*frame, *static_cast<const WebMouseEvent*>(&event));
116         return true;
117     case WebInputEvent::MouseDown:
118         if (!frame || !frame->view())
119             return true;
120         handler.handleMouseDown(*frame, *static_cast<const WebMouseEvent*>(&event));
121         return true;
122     case WebInputEvent::MouseUp:
123         if (!frame || !frame->view())
124             return true;
125         handler.handleMouseUp(*frame, *static_cast<const WebMouseEvent*>(&event));
126         return true;
127
128     case WebInputEvent::MouseWheel:
129         if (!frame || !frame->view())
130             return false;
131         return handler.handleMouseWheel(*frame, *static_cast<const WebMouseWheelEvent*>(&event));
132
133     case WebInputEvent::RawKeyDown:
134     case WebInputEvent::KeyDown:
135     case WebInputEvent::KeyUp:
136         return handler.handleKeyEvent(*static_cast<const WebKeyboardEvent*>(&event));
137
138     case WebInputEvent::Char:
139         return handler.handleCharEvent(*static_cast<const WebKeyboardEvent*>(&event));
140     case WebInputEvent::GestureScrollBegin:
141     case WebInputEvent::GestureScrollEnd:
142     case WebInputEvent::GestureScrollUpdate:
143     case WebInputEvent::GestureScrollUpdateWithoutPropagation:
144     case WebInputEvent::GestureFlingStart:
145     case WebInputEvent::GestureFlingCancel:
146     case WebInputEvent::GestureTap:
147     case WebInputEvent::GestureTapUnconfirmed:
148     case WebInputEvent::GestureTapDown:
149     case WebInputEvent::GestureShowPress:
150     case WebInputEvent::GestureTapCancel:
151     case WebInputEvent::GestureDoubleTap:
152     case WebInputEvent::GestureTwoFingerTap:
153     case WebInputEvent::GestureLongPress:
154     case WebInputEvent::GestureLongTap:
155         return handler.handleGestureEvent(*static_cast<const WebGestureEvent*>(&event));
156
157     case WebInputEvent::TouchStart:
158     case WebInputEvent::TouchMove:
159     case WebInputEvent::TouchEnd:
160     case WebInputEvent::TouchCancel:
161         if (!frame || !frame->view())
162             return false;
163         return handler.handleTouchEvent(*frame, *static_cast<const WebTouchEvent*>(&event));
164
165     case WebInputEvent::GesturePinchBegin:
166     case WebInputEvent::GesturePinchEnd:
167     case WebInputEvent::GesturePinchUpdate:
168         // FIXME: Once PlatformGestureEvent is updated to support pinch, this
169         // should call handleGestureEvent, just like it currently does for
170         // gesture scroll.
171         return false;
172
173     default:
174         return false;
175     }
176 }
177
178 // ----------------------------------------------------------------
179 // Default handlers for PageWidgetEventHandler
180
181 void PageWidgetEventHandler::handleMouseMove(LocalFrame& mainFrame, const WebMouseEvent& event)
182 {
183     mainFrame.eventHandler().handleMouseMoveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
184 }
185
186 void PageWidgetEventHandler::handleMouseLeave(LocalFrame& mainFrame, const WebMouseEvent& event)
187 {
188     mainFrame.eventHandler().handleMouseLeaveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
189 }
190
191 void PageWidgetEventHandler::handleMouseDown(LocalFrame& mainFrame, const WebMouseEvent& event)
192 {
193     mainFrame.eventHandler().handleMousePressEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
194 }
195
196 void PageWidgetEventHandler::handleMouseUp(LocalFrame& mainFrame, const WebMouseEvent& event)
197 {
198     mainFrame.eventHandler().handleMouseReleaseEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
199 }
200
201 bool PageWidgetEventHandler::handleMouseWheel(LocalFrame& mainFrame, const WebMouseWheelEvent& event)
202 {
203     return mainFrame.eventHandler().handleWheelEvent(PlatformWheelEventBuilder(mainFrame.view(), event));
204 }
205
206 bool PageWidgetEventHandler::handleTouchEvent(LocalFrame& mainFrame, const WebTouchEvent& event)
207 {
208     return mainFrame.eventHandler().handleTouchEvent(PlatformTouchEventBuilder(mainFrame.view(), event));
209 }
210
211 }