Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / PageWidgetDelegate.cpp
blob364108b07198b658b72ab60c34320acd84c9b354
1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/PageWidgetDelegate.h"
34 #include "core/frame/FrameView.h"
35 #include "core/frame/LocalFrame.h"
36 #include "core/input/EventHandler.h"
37 #include "core/layout/LayoutView.h"
38 #include "core/layout/compositing/DeprecatedPaintLayerCompositor.h"
39 #include "core/page/AutoscrollController.h"
40 #include "core/page/Page.h"
41 #include "core/paint/TransformRecorder.h"
42 #include "platform/Logging.h"
43 #include "platform/graphics/GraphicsContext.h"
44 #include "platform/graphics/paint/ClipRecorder.h"
45 #include "platform/graphics/paint/DrawingRecorder.h"
46 #include "platform/graphics/paint/SkPictureBuilder.h"
47 #include "platform/transforms/AffineTransform.h"
48 #include "public/web/WebInputEvent.h"
49 #include "web/WebInputEventConversion.h"
50 #include "wtf/CurrentTime.h"
52 namespace blink {
54 void PageWidgetDelegate::animate(Page& page, double monotonicFrameBeginTime, LocalFrame& root)
56 RefPtrWillBeRawPtr<FrameView> view = root.view();
57 if (!view)
58 return;
59 page.autoscrollController().animate(monotonicFrameBeginTime);
60 page.animator().serviceScriptedAnimations(monotonicFrameBeginTime);
63 void PageWidgetDelegate::layout(Page& page, LocalFrame& root)
65 page.animator().updateLayoutAndStyleForPainting(&root);
68 static void paintInternal(Page& page, WebCanvas* canvas,
69 const WebRect& rect, LocalFrame& root, const GlobalPaintFlags globalPaintFlags)
71 if (rect.isEmpty())
72 return;
74 IntRect intRect(rect);
75 SkPictureBuilder pictureBuilder(intRect);
77 GraphicsContext& paintContext = pictureBuilder.context();
79 // FIXME: device scale factor settings are layering violations and should not
80 // be used within Blink paint code.
81 float scaleFactor = page.deviceScaleFactor();
82 paintContext.setDeviceScaleFactor(scaleFactor);
84 AffineTransform scale;
85 scale.scale(scaleFactor);
86 TransformRecorder scaleRecorder(paintContext, root, scale);
88 IntRect dirtyRect(rect);
89 FrameView* view = root.view();
90 if (view) {
91 ClipRecorder clipRecorder(paintContext, root, DisplayItem::PageWidgetDelegateClip, LayoutRect(dirtyRect));
93 view->paint(&paintContext, globalPaintFlags, dirtyRect);
94 } else if (!DrawingRecorder::useCachedDrawingIfPossible(paintContext, root, DisplayItem::PageWidgetDelegateBackgroundFallback)) {
95 DrawingRecorder drawingRecorder(paintContext, root, DisplayItem::PageWidgetDelegateBackgroundFallback, dirtyRect);
96 paintContext.fillRect(dirtyRect, Color::white);
99 pictureBuilder.endRecording()->playback(canvas);
102 void PageWidgetDelegate::paint(Page& page, WebCanvas* canvas,
103 const WebRect& rect, LocalFrame& root)
105 paintInternal(page, canvas, rect, root, GlobalPaintNormalPhase);
108 void PageWidgetDelegate::paintIgnoringCompositing(Page& page, WebCanvas* canvas,
109 const WebRect& rect, LocalFrame& root)
111 paintInternal(page, canvas, rect, root, GlobalPaintFlattenCompositingLayers);
114 bool PageWidgetDelegate::handleInputEvent(PageWidgetEventHandler& handler, const WebInputEvent& event, LocalFrame* root)
116 switch (event.type) {
118 // FIXME: WebKit seems to always return false on mouse events processing
119 // methods. For now we'll assume it has processed them (as we are only
120 // interested in whether keyboard events are processed).
121 // FIXME: Why do we return true when there is no root or the root is
122 // detached?
123 case WebInputEvent::MouseMove:
124 if (!root || !root->view())
125 return true;
126 handler.handleMouseMove(*root, static_cast<const WebMouseEvent&>(event));
127 return true;
128 case WebInputEvent::MouseLeave:
129 if (!root || !root->view())
130 return true;
131 handler.handleMouseLeave(*root, static_cast<const WebMouseEvent&>(event));
132 return true;
133 case WebInputEvent::MouseDown:
134 if (!root || !root->view())
135 return true;
136 handler.handleMouseDown(*root, static_cast<const WebMouseEvent&>(event));
137 return true;
138 case WebInputEvent::MouseUp:
139 if (!root || !root->view())
140 return true;
141 handler.handleMouseUp(*root, static_cast<const WebMouseEvent&>(event));
142 return true;
144 case WebInputEvent::MouseWheel:
145 if (!root || !root->view())
146 return false;
147 return handler.handleMouseWheel(*root, static_cast<const WebMouseWheelEvent&>(event));
149 case WebInputEvent::RawKeyDown:
150 case WebInputEvent::KeyDown:
151 case WebInputEvent::KeyUp:
152 return handler.handleKeyEvent(static_cast<const WebKeyboardEvent&>(event));
154 case WebInputEvent::Char:
155 return handler.handleCharEvent(static_cast<const WebKeyboardEvent&>(event));
156 case WebInputEvent::GestureScrollBegin:
157 case WebInputEvent::GestureScrollEnd:
158 case WebInputEvent::GestureScrollUpdate:
159 case WebInputEvent::GestureFlingStart:
160 case WebInputEvent::GestureFlingCancel:
161 case WebInputEvent::GestureTap:
162 case WebInputEvent::GestureTapUnconfirmed:
163 case WebInputEvent::GestureTapDown:
164 case WebInputEvent::GestureShowPress:
165 case WebInputEvent::GestureTapCancel:
166 case WebInputEvent::GestureDoubleTap:
167 case WebInputEvent::GestureTwoFingerTap:
168 case WebInputEvent::GestureLongPress:
169 case WebInputEvent::GestureLongTap:
170 return handler.handleGestureEvent(static_cast<const WebGestureEvent&>(event));
172 case WebInputEvent::TouchStart:
173 case WebInputEvent::TouchMove:
174 case WebInputEvent::TouchEnd:
175 case WebInputEvent::TouchCancel:
176 if (!root || !root->view())
177 return false;
178 return handler.handleTouchEvent(*root, static_cast<const WebTouchEvent&>(event));
179 case WebInputEvent::GesturePinchBegin:
180 case WebInputEvent::GesturePinchEnd:
181 case WebInputEvent::GesturePinchUpdate:
182 // Touchscreen pinch events are currently not handled in main thread. Once they are,
183 // these should be passed to |handleGestureEvent| similar to gesture scroll events.
184 return false;
185 default:
186 return false;
190 // ----------------------------------------------------------------
191 // Default handlers for PageWidgetEventHandler
193 void PageWidgetEventHandler::handleMouseMove(LocalFrame& mainFrame, const WebMouseEvent& event)
195 mainFrame.eventHandler().handleMouseMoveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
198 void PageWidgetEventHandler::handleMouseLeave(LocalFrame& mainFrame, const WebMouseEvent& event)
200 mainFrame.eventHandler().handleMouseLeaveEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
203 void PageWidgetEventHandler::handleMouseDown(LocalFrame& mainFrame, const WebMouseEvent& event)
205 mainFrame.eventHandler().handleMousePressEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
208 void PageWidgetEventHandler::handleMouseUp(LocalFrame& mainFrame, const WebMouseEvent& event)
210 mainFrame.eventHandler().handleMouseReleaseEvent(PlatformMouseEventBuilder(mainFrame.view(), event));
213 bool PageWidgetEventHandler::handleMouseWheel(LocalFrame& mainFrame, const WebMouseWheelEvent& event)
215 return mainFrame.eventHandler().handleWheelEvent(PlatformWheelEventBuilder(mainFrame.view(), event));
218 bool PageWidgetEventHandler::handleTouchEvent(LocalFrame& mainFrame, const WebTouchEvent& event)
220 return mainFrame.eventHandler().handleTouchEvent(PlatformTouchEventBuilder(mainFrame.view(), event));
223 } // namespace blink