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