040ce1b911bc54a5d362aff5f9a7f144d1c390c1
[WebKit-https.git] / Source / WebKit2 / UIProcess / CoordinatedGraphics / WebView.cpp
1 /*
2  * Copyright (C) 2013 Intel Corporation. All rights reserved.
3  * Copyright (C) 2013 Samsung Electronics. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #if USE(COORDINATED_GRAPHICS)
29
30 #include "WebView.h"
31
32 #include "CoordinatedLayerTreeHostProxy.h"
33 #include "DrawingAreaProxyImpl.h"
34 #include "NotImplemented.h"
35 #include "WebContextMenuProxy.h"
36 #include "WebPageProxy.h"
37 #include <WebCore/CoordinatedGraphicsScene.h>
38
39 #if ENABLE(FULLSCREEN_API)
40 #include "WebFullScreenManagerProxy.h"
41 #endif
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 WebView::WebView(WebContext* context, WebPageGroup* pageGroup)
48     : m_focused(false)
49     , m_visible(false)
50     , m_contentScaleFactor(1.0)
51     , m_opacity(1.0)
52 {
53     // Need to call createWebPage after other data members, specifically m_visible, are initialized.
54     m_page = context->createWebPage(this, pageGroup);
55
56     m_page->pageGroup()->preferences()->setAcceleratedCompositingEnabled(true);
57     m_page->pageGroup()->preferences()->setForceCompositingMode(true);
58
59     char* debugVisualsEnvironment = getenv("WEBKIT_SHOW_COMPOSITING_DEBUG_VISUALS");
60     bool showDebugVisuals = debugVisualsEnvironment && !strcmp(debugVisualsEnvironment, "1");
61     m_page->pageGroup()->preferences()->setCompositingBordersVisible(showDebugVisuals);
62     m_page->pageGroup()->preferences()->setCompositingRepaintCountersVisible(showDebugVisuals);
63 }
64
65 WebView::~WebView()
66 {
67     if (m_page->isClosed())
68         return;
69
70     m_page->close();
71 }
72
73 void WebView::initialize()
74 {
75     m_page->initializeWebPage();
76     if (CoordinatedGraphicsScene* scene = coordinatedGraphicsScene())
77         scene->setActive(true);
78 }
79
80 void WebView::setSize(const WebCore::IntSize& size)
81 {
82     if (m_size == size)
83         return;
84
85     m_size = size;
86
87     updateViewportSize();
88 }
89
90 void WebView::setFocused(bool focused)
91 {
92     if (m_focused == focused)
93         return;
94
95     m_focused = focused;
96     m_page->viewStateDidChange(WebPageProxy::ViewIsFocused | WebPageProxy::ViewWindowIsActive);
97 }
98
99 void WebView::setVisible(bool visible)
100 {
101     if (m_visible == visible)
102         return;
103
104     m_visible = visible;
105     m_page->viewStateDidChange(WebPageProxy::ViewIsVisible);
106 }
107
108 void WebView::setUserViewportTranslation(double tx, double ty)
109 {
110     m_userViewportTransform = TransformationMatrix().translate(tx, ty);
111 }
112
113 IntPoint WebView::userViewportToContents(const IntPoint& point) const
114 {
115     return transformFromScene().mapPoint(point);
116 }
117
118 IntPoint WebView::userViewportToScene(const WebCore::IntPoint& point) const
119 {
120     return m_userViewportTransform.mapPoint(point);
121 }
122
123 IntPoint WebView::contentsToUserViewport(const IntPoint& point) const
124 {
125     return transformToScene().mapPoint(point);
126 }
127
128 void WebView::paintToCurrentGLContext()
129 {
130     CoordinatedGraphicsScene* scene = coordinatedGraphicsScene();
131     if (!scene)
132         return;
133
134     // FIXME: We need to clean up this code as it is split over CoordGfx and Page.
135     scene->setDrawsBackground(m_page->drawsBackground());
136     const FloatRect& viewport = m_userViewportTransform.mapRect(IntRect(IntPoint(), m_size));
137
138     scene->paintToCurrentGLContext(transformToScene().toTransformationMatrix(), m_opacity, viewport);
139 }
140
141 void WebView::setDrawsBackground(bool drawsBackground)
142 {
143     m_page->setDrawsBackground(drawsBackground);
144 }
145
146 bool WebView::drawsBackground() const
147 {
148     return m_page->drawsBackground();
149 }
150
151 void WebView::setDrawsTransparentBackground(bool transparentBackground)
152 {
153     m_page->setDrawsTransparentBackground(transparentBackground);
154 }
155
156 bool WebView::drawsTransparentBackground() const
157 {
158     return m_page->drawsTransparentBackground();
159 }
160
161 void WebView::suspendActiveDOMObjectsAndAnimations()
162 {
163     m_page->suspendActiveDOMObjectsAndAnimations();
164 }
165
166 void WebView::resumeActiveDOMObjectsAndAnimations()
167 {
168     m_page->resumeActiveDOMObjectsAndAnimations();
169 }
170
171 void WebView::setShowsAsSource(bool showsAsSource)
172 {
173     m_page->setMainFrameInViewSourceMode(showsAsSource);
174 }
175
176 bool WebView::showsAsSource() const
177 {
178     return m_page->mainFrameInViewSourceMode();
179 }
180
181 #if ENABLE(FULLSCREEN_API)
182 bool WebView::exitFullScreen()
183 {
184 #if PLATFORM(EFL)
185     // FIXME: Implement this for other platforms.
186     if (!m_page->fullScreenManager()->isFullScreen())
187         return false;
188 #endif
189     m_page->fullScreenManager()->requestExitFullScreen();
190     return true;
191 }
192 #endif
193
194 void WebView::initializeClient(const WKViewClient* client)
195 {
196     m_client.initialize(client);
197 }
198
199 void WebView::didChangeContentsSize(const WebCore::IntSize& size)
200 {
201     if (m_contentsSize == size)
202         return;
203
204     m_contentsSize = size;
205     m_client.didChangeContentsSize(this, size);
206
207     updateViewportSize();
208 }
209
210 AffineTransform WebView::transformFromScene() const
211 {
212     return transformToScene().inverse();
213 }
214
215 AffineTransform WebView::transformToScene() const
216 {
217     FloatPoint position = -m_contentPosition;
218     float effectiveScale = m_contentScaleFactor * m_page->deviceScaleFactor();
219     position.scale(effectiveScale, effectiveScale);
220
221     TransformationMatrix transform = m_userViewportTransform;
222     transform.translate(position.x(), position.y());
223     transform.scale(effectiveScale);
224
225     return transform.toAffineTransform();
226 }
227
228 CoordinatedGraphicsScene* WebView::coordinatedGraphicsScene()
229 {
230     DrawingAreaProxy* drawingArea = m_page->drawingArea();
231     if (!drawingArea)
232         return 0;
233
234     WebKit::CoordinatedLayerTreeHostProxy* layerTreeHostProxy = drawingArea->coordinatedLayerTreeHostProxy();
235     if (!layerTreeHostProxy)
236         return 0;
237
238     return layerTreeHostProxy->coordinatedGraphicsScene();
239 }
240
241 void WebView::updateViewportSize()
242 {
243     if (DrawingAreaProxy* drawingArea = page()->drawingArea()) {
244         // Web Process expects sizes in UI units, and not raw device units.
245         drawingArea->setSize(roundedIntSize(dipSize()), IntSize(), IntSize());
246         FloatRect visibleContentsRect(contentPosition(), visibleContentsSize());
247         visibleContentsRect.intersect(FloatRect(FloatPoint(), contentsSize()));
248         drawingArea->setVisibleContentsRect(visibleContentsRect, FloatPoint());
249     }
250 }
251
252 inline WebCore::FloatSize WebView::dipSize() const
253 {
254     FloatSize dipSize(size());
255     dipSize.scale(1 / m_page->deviceScaleFactor());
256
257     return dipSize;
258 }
259
260 WebCore::FloatSize WebView::visibleContentsSize() const
261 {
262     FloatSize visibleContentsSize(dipSize());
263     visibleContentsSize.scale(1 / m_contentScaleFactor);
264
265     return visibleContentsSize;
266 }
267
268 // Page Client
269
270 PassOwnPtr<DrawingAreaProxy> WebView::createDrawingAreaProxy()
271 {
272     OwnPtr<DrawingAreaProxy> drawingArea = DrawingAreaProxyImpl::create(page());
273     return drawingArea.release();
274 }
275
276 void WebView::setViewNeedsDisplay(const WebCore::IntRect& area)
277 {
278     m_client.viewNeedsDisplay(this, area);
279 }
280
281 void WebView::displayView()
282 {
283     notImplemented();
284 }
285
286 void WebView::scrollView(const WebCore::IntRect& scrollRect, const WebCore::IntSize&)
287 {
288     setViewNeedsDisplay(scrollRect);
289 }
290
291 WebCore::IntSize WebView::viewSize()
292 {
293     return roundedIntSize(dipSize());
294 }
295
296 bool WebView::isViewWindowActive()
297 {
298     notImplemented();
299     return true;
300 }
301
302 bool WebView::isViewFocused()
303 {
304     return isFocused();
305 }
306
307 bool WebView::isViewVisible()
308 {
309     return isVisible();
310 }
311
312 bool WebView::isViewInWindow()
313 {
314     notImplemented();
315     return true;
316 }
317
318 void WebView::processDidCrash()
319 {
320     m_client.webProcessCrashed(this, m_page->urlAtProcessExit());
321 }
322
323 void WebView::didRelaunchProcess()
324 {
325     m_client.webProcessDidRelaunch(this);
326 }
327
328 void WebView::pageClosed()
329 {
330     notImplemented();
331 }
332
333 void WebView::preferencesDidChange()
334 {
335     notImplemented();
336 }
337
338 void WebView::toolTipChanged(const String&, const String& newToolTip)
339 {
340     m_client.didChangeTooltip(this, newToolTip);
341 }
342
343 void WebView::setCursor(const WebCore::Cursor&)
344 {
345     notImplemented();
346 }
347
348 void WebView::setCursorHiddenUntilMouseMoves(bool)
349 {
350     notImplemented();
351 }
352
353 void WebView::registerEditCommand(PassRefPtr<WebEditCommandProxy> command, WebPageProxy::UndoOrRedo undoOrRedo)
354 {
355     m_undoController.registerEditCommand(command, undoOrRedo);
356 }
357
358 void WebView::clearAllEditCommands()
359 {
360     m_undoController.clearAllEditCommands();
361 }
362
363 bool WebView::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
364 {
365     return m_undoController.canUndoRedo(undoOrRedo);
366 }
367
368 void WebView::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
369 {
370     m_undoController.executeUndoRedo(undoOrRedo);
371 }
372
373 IntPoint WebView::screenToWindow(const IntPoint& point)
374 {
375     notImplemented();
376     return point;
377 }
378
379 IntRect WebView::windowToScreen(const IntRect&)
380 {
381     notImplemented();
382     return IntRect();
383 }
384
385 void WebView::doneWithKeyEvent(const NativeWebKeyboardEvent&, bool)
386 {
387     notImplemented();
388 }
389
390 #if ENABLE(TOUCH_EVENTS)
391 void WebView::doneWithTouchEvent(const NativeWebTouchEvent&, bool /*wasEventHandled*/)
392 {
393     notImplemented();
394 }
395 #endif
396
397 PassRefPtr<WebPopupMenuProxy> WebView::createPopupMenuProxy(WebPageProxy*)
398 {
399     notImplemented();
400     return 0;
401 }
402
403 PassRefPtr<WebContextMenuProxy> WebView::createContextMenuProxy(WebPageProxy*)
404 {
405     notImplemented();
406     return 0;
407 }
408
409 #if ENABLE(INPUT_TYPE_COLOR)
410 PassRefPtr<WebColorChooserProxy> WebView::createColorChooserProxy(WebPageProxy*, const WebCore::Color&, const WebCore::IntRect&)
411 {
412     notImplemented();
413     return 0;
414 }
415 #endif
416
417 void WebView::setFindIndicator(PassRefPtr<FindIndicator>, bool, bool)
418 {
419     notImplemented();
420 }
421
422 void WebView::enterAcceleratedCompositingMode(const LayerTreeContext&)
423 {
424     if (CoordinatedGraphicsScene* scene = coordinatedGraphicsScene())
425         scene->setActive(true);
426 }
427
428 void WebView::exitAcceleratedCompositingMode()
429 {
430     if (CoordinatedGraphicsScene* scene = coordinatedGraphicsScene())
431         scene->setActive(false);
432 }
433
434 void WebView::updateAcceleratedCompositingMode(const LayerTreeContext&)
435 {
436     notImplemented();
437 }
438
439 void WebView::flashBackingStoreUpdates(const Vector<IntRect>&)
440 {
441     notImplemented();
442 }
443
444 void WebView::updateTextInputState()
445 {
446     notImplemented();
447 }
448
449 void WebView::handleDownloadRequest(DownloadProxy*)
450 {
451     notImplemented();
452 }
453
454 FloatRect WebView::convertToDeviceSpace(const FloatRect& userRect)
455 {
456     if (m_page->useFixedLayout()) {
457         FloatRect result = userRect;
458         result.scale(m_page->deviceScaleFactor());
459         return result;
460     }
461     // Legacy mode.
462     notImplemented();
463     return userRect;
464 }
465
466 FloatRect WebView::convertToUserSpace(const FloatRect& deviceRect)
467 {
468     if (m_page->useFixedLayout()) {
469         FloatRect result = deviceRect;
470         result.scale(1 / m_page->deviceScaleFactor());
471         return result;
472     }
473     // Legacy mode.
474     notImplemented();
475     return deviceRect;
476 }
477
478 void WebView::didChangeViewportProperties(const WebCore::ViewportAttributes& attr)
479 {
480     m_client.didChangeViewportAttributes(this, attr);
481 }
482
483 void WebView::pageDidRequestScroll(const IntPoint& position)
484 {
485     FloatPoint uiPosition(position);
486     uiPosition.scale(contentScaleFactor(), contentScaleFactor());
487     setContentPosition(uiPosition);
488
489     m_client.didChangeContentsPosition(this, position);
490 }
491
492 void WebView::didRenderFrame(const WebCore::IntSize& contentsSize, const WebCore::IntRect& coveredRect)
493 {
494     m_client.didRenderFrame(this, contentsSize, coveredRect);
495 }
496
497 void WebView::pageTransitionViewportReady()
498 {
499     m_client.didCompletePageTransition(this);
500 }
501
502 } // namespace WebKit
503
504 #endif // USE(COORDINATED_GRAPHICS)
505