[Qt] Replace use of QApplication with QGuiApplication
[WebKit-https.git] / Source / WebKit2 / UIProcess / qt / QtWebPageProxy.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "QtWebPageProxy.h"
23
24 #include "QtWebError.h"
25 #include "qwebdownloaditem.h"
26 #include "qwebdownloaditem_p.h"
27 #include "qwebpreferences.h"
28 #include "qwebpreferences_p.h"
29
30 #include "ClientImpl.h"
31 #include "DownloadProxy.h"
32 #include "DrawingAreaProxyImpl.h"
33 #include "qwkhistory.h"
34 #include "qwkhistory_p.h"
35 #include "FindIndicator.h"
36 #include "LocalizedStrings.h"
37 #include "MutableArray.h"
38 #include "NativeWebKeyboardEvent.h"
39 #include "NativeWebMouseEvent.h"
40 #include "NativeWebWheelEvent.h"
41 #include "NotImplemented.h"
42 #include "QtPolicyInterface.h"
43 #include "QtViewInterface.h"
44 #include "QtViewportInteractionEngine.h"
45 #include "QtWebUndoCommand.h"
46 #include "WebBackForwardList.h"
47 #include "WebContext.h"
48 #include "WebContextMenuProxyQt.h"
49 #include "WebEditCommandProxy.h"
50 #include "WebEventFactoryQt.h"
51 #include "WebPopupMenuProxyQt.h"
52 #include "WKStringQt.h"
53 #include "WKURLQt.h"
54 #include <QGuiApplication>
55 #include <QGraphicsSceneMouseEvent>
56 #include <QJSEngine>
57 #include <QMimeData>
58 #include <QStyleHints>
59 #include <QTouchEvent>
60 #include <QUndoStack>
61 #include <QtDebug>
62 #include <WebCore/Cursor.h>
63 #include <WebCore/DragData.h>
64 #include <WebCore/FloatRect.h>
65 #include <WebCore/NotImplemented.h>
66 #include <WebCore/Region.h>
67 #include <WebKit2/WKFrame.h>
68 #include <WebKit2/WKPageGroup.h>
69 #include <WebKit2/WKRetainPtr.h>
70
71 using namespace WebKit;
72 using namespace WebCore;
73
74 RefPtr<WebContext> QtWebPageProxy::s_defaultContext;
75 RefPtr<QtDownloadManager> QtWebPageProxy::s_downloadManager;
76
77 unsigned QtWebPageProxy::s_defaultPageProxyCount = 0;
78
79 PassRefPtr<WebContext> QtWebPageProxy::defaultWKContext()
80 {
81     if (!s_defaultContext) {
82         s_defaultContext = WebContext::create(String());
83         setupContextInjectedBundleClient(toAPI(s_defaultContext.get()));
84         s_downloadManager = QtDownloadManager::create(s_defaultContext.get());
85     }
86     return s_defaultContext;
87 }
88
89 static inline Qt::DropAction dragOperationToDropAction(unsigned dragOperation)
90 {
91     Qt::DropAction result = Qt::IgnoreAction;
92     if (dragOperation & DragOperationCopy)
93         result = Qt::CopyAction;
94     else if (dragOperation & DragOperationMove)
95         result = Qt::MoveAction;
96     else if (dragOperation & DragOperationGeneric)
97         result = Qt::MoveAction;
98     else if (dragOperation & DragOperationLink)
99         result = Qt::LinkAction;
100     return result;
101 }
102
103 static inline Qt::DropActions dragOperationToDropActions(unsigned dragOperations)
104 {
105     Qt::DropActions result = Qt::IgnoreAction;
106     if (dragOperations & DragOperationCopy)
107         result |= Qt::CopyAction;
108     if (dragOperations & DragOperationMove)
109         result |= Qt::MoveAction;
110     if (dragOperations & DragOperationGeneric)
111         result |= Qt::MoveAction;
112     if (dragOperations & DragOperationLink)
113         result |= Qt::LinkAction;
114     return result;
115 }
116
117 WebCore::DragOperation dropActionToDragOperation(Qt::DropActions actions)
118 {
119     unsigned result = 0;
120     if (actions & Qt::CopyAction)
121         result |= DragOperationCopy;
122     if (actions & Qt::MoveAction)
123         result |= (DragOperationMove | DragOperationGeneric);
124     if (actions & Qt::LinkAction)
125         result |= DragOperationLink;
126     if (result == (DragOperationCopy | DragOperationMove | DragOperationGeneric | DragOperationLink))
127         result = DragOperationEvery;
128     return (DragOperation)result;
129 }
130
131 QtWebPageProxy::QtWebPageProxy(QtViewInterface* viewInterface, QtViewportInteractionEngine* viewportInteractionEngine, QtPolicyInterface* policyInterface, WKContextRef contextRef, WKPageGroupRef pageGroupRef)
132     : m_viewInterface(viewInterface)
133     , m_interactionEngine(viewportInteractionEngine)
134     , m_panGestureRecognizer(viewportInteractionEngine)
135     , m_pinchGestureRecognizer(viewportInteractionEngine)
136     , m_policyInterface(policyInterface)
137     , m_context(contextRef ? toImpl(contextRef) : defaultWKContext())
138     , m_undoStack(adoptPtr(new QUndoStack(this)))
139     , m_loadProgress(0)
140     , m_navigatorQtObjectEnabled(false)
141 {
142     ASSERT(viewInterface);
143     m_webPageProxy = m_context->createWebPage(this, toImpl(pageGroupRef));
144     m_history = QWKHistoryPrivate::createHistory(this, m_webPageProxy->backForwardList());
145     if (!contextRef)
146         s_defaultPageProxyCount++;
147 }
148
149 void QtWebPageProxy::init()
150 {
151     m_webPageProxy->initializeWebPage();
152
153     setupPageLoaderClient(this, m_webPageProxy.get());
154     setupPageUiClient(this, m_webPageProxy.get());
155
156     if (m_policyInterface)
157         setupPagePolicyClient(m_policyInterface, m_webPageProxy.get());
158 }
159
160 QtWebPageProxy::~QtWebPageProxy()
161 {
162     m_webPageProxy->close();
163     // The context is the default one and we're deleting the last QtWebPageProxy.
164     if (m_context == s_defaultContext) {
165         ASSERT(s_defaultPageProxyCount > 0);
166         s_defaultPageProxyCount--;
167         if (!s_defaultPageProxyCount) {
168             s_defaultContext.clear();
169             s_downloadManager.clear();
170         }
171     }
172     delete m_history;
173 }
174
175 bool QtWebPageProxy::handleEvent(QEvent* ev)
176 {
177     switch (ev->type()) {
178     case QEvent::MouseMove:
179         return handleMouseMoveEvent(reinterpret_cast<QMouseEvent*>(ev));
180     case QEvent::MouseButtonPress:
181         return handleMousePressEvent(reinterpret_cast<QMouseEvent*>(ev));
182     case QEvent::MouseButtonRelease:
183         return handleMouseReleaseEvent(reinterpret_cast<QMouseEvent*>(ev));
184     case QEvent::MouseButtonDblClick:
185         return handleMouseDoubleClickEvent(reinterpret_cast<QMouseEvent*>(ev));
186     case QEvent::Wheel:
187         return handleWheelEvent(reinterpret_cast<QWheelEvent*>(ev));
188     case QEvent::HoverLeave:
189         return handleHoverLeaveEvent(reinterpret_cast<QHoverEvent*>(ev));
190     case QEvent::HoverEnter: // Fall-through, for WebKit the distinction doesn't matter.
191     case QEvent::HoverMove:
192         return handleHoverMoveEvent(reinterpret_cast<QHoverEvent*>(ev));
193     case QEvent::DragEnter:
194         return handleDragEnterEvent(reinterpret_cast<QDragEnterEvent*>(ev));
195     case QEvent::DragLeave:
196         return handleDragLeaveEvent(reinterpret_cast<QDragLeaveEvent*>(ev));
197     case QEvent::DragMove:
198         return handleDragMoveEvent(reinterpret_cast<QDragMoveEvent*>(ev));
199     case QEvent::Drop:
200         return handleDropEvent(reinterpret_cast<QDropEvent*>(ev));
201     case QEvent::KeyPress:
202         return handleKeyPressEvent(reinterpret_cast<QKeyEvent*>(ev));
203     case QEvent::KeyRelease:
204         return handleKeyReleaseEvent(reinterpret_cast<QKeyEvent*>(ev));
205     case QEvent::FocusIn:
206         return handleFocusInEvent(reinterpret_cast<QFocusEvent*>(ev));
207     case QEvent::FocusOut:
208         return handleFocusOutEvent(reinterpret_cast<QFocusEvent*>(ev));
209     case QEvent::TouchBegin:
210     case QEvent::TouchEnd:
211     case QEvent::TouchUpdate:
212         touchEvent(static_cast<QTouchEvent*>(ev));
213         return true;
214     }
215
216     // FIXME: Move all common event handling here.
217     return false;
218 }
219
220 bool QtWebPageProxy::handleMouseMoveEvent(QMouseEvent* ev)
221 {
222     // For some reason mouse press results in mouse hover (which is
223     // converted to mouse move for WebKit). We ignore these hover
224     // events by comparing lastPos with newPos.
225     // NOTE: lastPos from the event always comes empty, so we work
226     // around that here.
227     static QPointF lastPos = QPointF();
228     if (lastPos == ev->pos())
229         return ev->isAccepted();
230     lastPos = ev->pos();
231
232     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
233
234     return ev->isAccepted();
235 }
236
237 bool QtWebPageProxy::handleMousePressEvent(QMouseEvent* ev)
238 {
239     if (m_tripleClickTimer.isActive() && (ev->pos() - m_tripleClick).manhattanLength() < qApp->styleHints()->startDragDistance()) {
240         m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/3));
241         return ev->isAccepted();
242     }
243
244     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/1));
245     return ev->isAccepted();
246 }
247
248 bool QtWebPageProxy::handleMouseReleaseEvent(QMouseEvent* ev)
249 {
250     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
251     return ev->isAccepted();
252 }
253
254 bool QtWebPageProxy::handleMouseDoubleClickEvent(QMouseEvent* ev)
255 {
256     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/2));
257
258     m_tripleClickTimer.start(qApp->styleHints()->mouseDoubleClickInterval(), this);
259     m_tripleClick = ev->localPos().toPoint();
260     return ev->isAccepted();
261 }
262
263 bool QtWebPageProxy::handleWheelEvent(QWheelEvent* ev)
264 {
265     m_webPageProxy->handleWheelEvent(NativeWebWheelEvent(ev));
266     return ev->isAccepted();
267 }
268
269 bool QtWebPageProxy::handleHoverLeaveEvent(QHoverEvent* ev)
270 {
271     // To get the correct behavior of mouseout, we need to turn the Leave event of our webview into a mouse move
272     // to a very far region.
273     QHoverEvent fakeEvent(QEvent::HoverMove, QPoint(INT_MIN, INT_MIN), ev->oldPos());
274     return handleHoverMoveEvent(&fakeEvent);
275 }
276
277 bool QtWebPageProxy::handleHoverMoveEvent(QHoverEvent* ev)
278 {
279     QMouseEvent me(QEvent::MouseMove, ev->pos(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
280     me.setAccepted(ev->isAccepted());
281
282     return handleMouseMoveEvent(&me);
283 }
284
285 bool QtWebPageProxy::handleDragEnterEvent(QDragEnterEvent* ev)
286 {
287     m_webPageProxy->resetDragOperation();
288     // FIXME: Should not use QCursor::pos()
289     DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
290     m_webPageProxy->dragEntered(&dragData);
291     ev->acceptProposedAction();
292     return true;
293 }
294
295 bool QtWebPageProxy::handleDragLeaveEvent(QDragLeaveEvent* ev)
296 {
297     bool accepted = ev->isAccepted();
298
299     // FIXME: Should not use QCursor::pos()
300     DragData dragData(0, IntPoint(), QCursor::pos(), DragOperationNone);
301     m_webPageProxy->dragExited(&dragData);
302     m_webPageProxy->resetDragOperation();
303
304     ev->setAccepted(accepted);
305     return accepted;
306 }
307
308 bool QtWebPageProxy::handleDragMoveEvent(QDragMoveEvent* ev)
309 {
310     bool accepted = ev->isAccepted();
311
312     // FIXME: Should not use QCursor::pos()
313     DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
314     m_webPageProxy->dragUpdated(&dragData);
315     ev->setDropAction(dragOperationToDropAction(m_webPageProxy->dragSession().operation));
316     if (m_webPageProxy->dragSession().operation != DragOperationNone)
317         ev->accept();
318
319     ev->setAccepted(accepted);
320     return accepted;
321 }
322
323 bool QtWebPageProxy::handleDropEvent(QDropEvent* ev)
324 {
325     bool accepted = ev->isAccepted();
326
327     // FIXME: Should not use QCursor::pos()
328     DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
329     SandboxExtension::Handle handle;
330     m_webPageProxy->performDrag(&dragData, String(), handle);
331     ev->setDropAction(dragOperationToDropAction(m_webPageProxy->dragSession().operation));
332     ev->accept();
333
334     ev->setAccepted(accepted);
335     return accepted;
336 }
337
338 void QtWebPageProxy::timerEvent(QTimerEvent* ev)
339 {
340     int timerId = ev->timerId();
341     if (timerId == m_tripleClickTimer.timerId())
342         m_tripleClickTimer.stop();
343     else
344         QObject::timerEvent(ev);
345 }
346
347 bool QtWebPageProxy::handleKeyPressEvent(QKeyEvent* ev)
348 {
349     m_webPageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(ev));
350     return true;
351 }
352
353 bool QtWebPageProxy::handleKeyReleaseEvent(QKeyEvent* ev)
354 {
355     m_webPageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(ev));
356     return true;
357 }
358
359 bool QtWebPageProxy::handleFocusInEvent(QFocusEvent*)
360 {
361     m_webPageProxy->viewStateDidChange(WebPageProxy::ViewIsFocused | WebPageProxy::ViewWindowIsActive);
362     return true;
363 }
364
365 bool QtWebPageProxy::handleFocusOutEvent(QFocusEvent*)
366 {
367     m_webPageProxy->viewStateDidChange(WebPageProxy::ViewIsFocused | WebPageProxy::ViewWindowIsActive);
368     return true;
369 }
370
371 void QtWebPageProxy::setCursor(const WebCore::Cursor& cursor)
372 {
373     m_viewInterface->didChangeCursor(*cursor.platformCursor());
374 }
375
376 void QtWebPageProxy::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
377 {
378     notImplemented();
379 }
380
381 void QtWebPageProxy::setViewNeedsDisplay(const WebCore::IntRect& rect)
382 {
383     m_viewInterface->setViewNeedsDisplay(QRect(rect));
384 }
385
386 void QtWebPageProxy::displayView()
387 {
388     // FIXME: Implement.
389 }
390
391 void QtWebPageProxy::scrollView(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollOffset)
392 {
393     // FIXME: Implement.
394 }
395
396 WebCore::IntSize QtWebPageProxy::viewSize()
397 {
398     return WebCore::IntSize(m_viewInterface->drawingAreaSize());
399 }
400
401 bool QtWebPageProxy::isViewWindowActive()
402 {
403     return m_viewInterface->isActive();
404 }
405
406 bool QtWebPageProxy::isViewFocused()
407 {
408     return m_viewInterface->hasFocus();
409 }
410
411 bool QtWebPageProxy::isViewVisible()
412 {
413     return m_viewInterface->isVisible();
414 }
415
416 bool QtWebPageProxy::isViewInWindow()
417 {
418     // FIXME: Implement.
419     return true;
420 }
421
422 void QtWebPageProxy::enterAcceleratedCompositingMode(const LayerTreeContext&)
423 {
424     // FIXME: Implement.
425 }
426
427 void QtWebPageProxy::exitAcceleratedCompositingMode()
428 {
429     // FIXME: Implement.
430 }
431
432 void QtWebPageProxy::pageDidRequestScroll(const IntPoint& pos)
433 {
434     m_viewInterface->scrollPositionRequested(pos);
435 }
436
437 void QtWebPageProxy::didChangeContentsSize(const IntSize& newSize)
438 {
439     m_viewInterface->contentSizeChanged(QSize(newSize));
440 }
441
442 void QtWebPageProxy::toolTipChanged(const String&, const String& newTooltip)
443 {
444     m_viewInterface->didChangeToolTip(QString(newTooltip));
445 }
446
447 void QtWebPageProxy::registerEditCommand(PassRefPtr<WebEditCommandProxy> command, WebPageProxy::UndoOrRedo undoOrRedo)
448 {
449     if (undoOrRedo == WebPageProxy::Undo) {
450         const QtWebUndoCommand* webUndoCommand = static_cast<const QtWebUndoCommand*>(m_undoStack->command(m_undoStack->index()));
451         if (webUndoCommand && webUndoCommand->inUndoRedo())
452             return;
453         m_undoStack->push(new QtWebUndoCommand(command));
454     }
455 }
456
457 void QtWebPageProxy::clearAllEditCommands()
458 {
459     m_undoStack->clear();
460 }
461
462 bool QtWebPageProxy::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
463 {
464     if (undoOrRedo == WebPageProxy::Undo)
465         return m_undoStack->canUndo();
466     return m_undoStack->canRedo();
467 }
468
469 void QtWebPageProxy::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
470 {
471     if (undoOrRedo == WebPageProxy::Undo)
472         m_undoStack->undo();
473     else
474         m_undoStack->redo();
475 }
476
477 FloatRect QtWebPageProxy::convertToDeviceSpace(const FloatRect& rect)
478 {
479     return rect;
480 }
481
482 IntPoint QtWebPageProxy::screenToWindow(const IntPoint& point)
483 {
484     return point;
485 }
486
487 IntRect QtWebPageProxy::windowToScreen(const IntRect& rect)
488 {
489     return rect;
490 }
491
492 FloatRect QtWebPageProxy::convertToUserSpace(const FloatRect& rect)
493 {
494     return rect;
495 }
496
497 void QtWebPageProxy::selectionChanged(bool, bool, bool, bool)
498 {
499 }
500
501 void QtWebPageProxy::doneWithKeyEvent(const NativeWebKeyboardEvent&, bool)
502 {
503 }
504
505 PassRefPtr<WebPopupMenuProxy> QtWebPageProxy::createPopupMenuProxy(WebPageProxy*)
506 {
507     return WebPopupMenuProxyQt::create();
508 }
509
510 PassRefPtr<WebContextMenuProxy> QtWebPageProxy::createContextMenuProxy(WebPageProxy*)
511 {
512     return WebContextMenuProxyQt::create(m_webPageProxy.get(), m_viewInterface);
513 }
514
515 void QtWebPageProxy::setFindIndicator(PassRefPtr<FindIndicator>, bool fadeOut)
516 {
517 }
518
519 void QtWebPageProxy::didCommitLoadForMainFrame(bool useCustomRepresentation)
520 {
521 }
522
523 void QtWebPageProxy::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference&)
524 {
525 }
526
527 void QtWebPageProxy::flashBackingStoreUpdates(const Vector<IntRect>&)
528 {
529     notImplemented();
530 }
531
532 WKPageRef QtWebPageProxy::pageRef() const
533 {
534     return toAPI(m_webPageProxy.get());;
535 }
536
537 void QtWebPageProxy::didFindZoomableArea(const IntPoint& target, const IntRect& area)
538 {
539     m_viewInterface->didFindZoomableArea(QPoint(target), QRect(area));
540 }
541
542 void QtWebPageProxy::didReceiveMessageFromNavigatorQtObject(const String& message)
543 {
544     QVariantMap variantMap;
545     variantMap.insert(QLatin1String("data"), QString(message));
546     variantMap.insert(QLatin1String("origin"), url());
547     emit receivedMessageFromNavigatorQtObject(variantMap);
548 }
549
550 void QtWebPageProxy::didChangeUrl(const QUrl& url)
551 {
552     m_viewInterface->didChangeUrl(url);
553 }
554
555 void QtWebPageProxy::didChangeTitle(const QString& newTitle)
556 {
557     m_viewInterface->didChangeTitle(newTitle);
558 }
559
560 void QtWebPageProxy::loadDidBegin()
561 {
562     m_viewInterface->loadDidBegin();
563 }
564
565 void QtWebPageProxy::loadDidCommit()
566 {
567     m_viewInterface->loadDidCommit();
568 }
569
570 void QtWebPageProxy::loadDidSucceed()
571 {
572     m_viewInterface->loadDidSucceed();
573 }
574
575 void QtWebPageProxy::loadDidFail(const QtWebError& error)
576 {
577     m_viewInterface->loadDidFail(error);
578 }
579
580 void QtWebPageProxy::didChangeLoadProgress(int newLoadProgress)
581 {
582     m_loadProgress = newLoadProgress;
583     m_viewInterface->didChangeLoadProgress(newLoadProgress);
584 }
585
586 void QtWebPageProxy::paint(QPainter* painter, const QRect& area)
587 {
588     if (m_webPageProxy->isValid())
589         paintContent(painter, area);
590     else
591         painter->fillRect(area, Qt::white);
592 }
593
594 bool QtWebPageProxy::canGoBack() const
595 {
596     return m_webPageProxy->canGoBack();
597 }
598
599 void QtWebPageProxy::goBack()
600 {
601     m_webPageProxy->goBack();
602 }
603
604 bool QtWebPageProxy::canGoForward() const
605 {
606     return m_webPageProxy->canGoForward();
607 }
608
609 void QtWebPageProxy::goForward()
610 {
611     m_webPageProxy->goForward();
612 }
613
614 bool QtWebPageProxy::canStop() const
615 {
616     RefPtr<WebKit::WebFrameProxy> mainFrame = m_webPageProxy->mainFrame();
617     return mainFrame && !(WebFrameProxy::LoadStateFinished == mainFrame->loadState());
618 }
619
620 void QtWebPageProxy::stop()
621 {
622     m_webPageProxy->stopLoading();
623 }
624
625 bool QtWebPageProxy::canReload() const
626 {
627     RefPtr<WebKit::WebFrameProxy> mainFrame = m_webPageProxy->mainFrame();
628     if (mainFrame)
629         return (WebFrameProxy::LoadStateFinished == mainFrame->loadState());
630     return m_webPageProxy->backForwardList()->currentItem();
631 }
632
633 void QtWebPageProxy::reload()
634 {
635     m_webPageProxy->reload(/* reloadFromOrigin */ true);
636 }
637
638 void QtWebPageProxy::navigationStateChanged()
639 {
640     emit updateNavigationState();
641 }
642
643 void QtWebPageProxy::didRelaunchProcess()
644 {
645     updateNavigationState();
646     m_viewInterface->didRelaunchProcess();
647     setDrawingAreaSize(m_viewInterface->drawingAreaSize());
648 }
649
650 void QtWebPageProxy::processDidCrash()
651 {
652     updateNavigationState();
653     m_panGestureRecognizer.reset();
654     m_pinchGestureRecognizer.reset();
655     m_viewInterface->processDidCrash();
656 }
657
658 QWebPreferences* QtWebPageProxy::preferences() const
659 {
660     if (!m_preferences)
661         m_preferences = adoptPtr(QWebPreferencesPrivate::createPreferences(const_cast<QtWebPageProxy*>(this)));
662     return m_preferences.get();
663 }
664
665 void QtWebPageProxy::setCustomUserAgent(const QString& userAgent)
666 {
667     WKRetainPtr<WKStringRef> wkUserAgent(WKStringCreateWithQString(userAgent));
668     WKPageSetCustomUserAgent(pageRef(), wkUserAgent.get());
669 }
670
671 QString QtWebPageProxy::customUserAgent() const
672 {
673     return WKStringCopyQString(WKPageCopyCustomUserAgent(pageRef()));
674 }
675
676 void QtWebPageProxy::setNavigatorQtObjectEnabled(bool enabled)
677 {
678     static String messageName("SetNavigatorQtObjectEnabled");
679
680     ASSERT(enabled != m_navigatorQtObjectEnabled);
681     // FIXME: Currently we have to keep this information in both processes and the setting is asynchronous.
682     m_navigatorQtObjectEnabled = enabled;
683     RefPtr<MutableArray> body = MutableArray::create();
684     body->append(m_webPageProxy.get());
685     RefPtr<WebBoolean> webEnabled = WebBoolean::create(enabled);
686     body->append(webEnabled.get());
687     m_context->postMessageToInjectedBundle(messageName, body.get());
688 }
689
690 void QtWebPageProxy::postMessageToNavigatorQtObject(const QString& message)
691 {
692     static String messageName("MessageToNavigatorQtObject");
693
694     RefPtr<MutableArray> body = MutableArray::create();
695     body->append(m_webPageProxy.get());
696     RefPtr<WebString> contents = WebString::create(String(message));
697     body->append(contents.get());
698     m_context->postMessageToInjectedBundle(messageName, body.get());
699 }
700
701 void QtWebPageProxy::load(const QUrl& url)
702 {
703     WKRetainPtr<WKURLRef> wkurl(WKURLCreateWithQUrl(url));
704     WKPageLoadURL(pageRef(), wkurl.get());
705 }
706
707 QUrl QtWebPageProxy::url() const
708 {
709     WKRetainPtr<WKFrameRef> frame = WKPageGetMainFrame(pageRef());
710     if (!frame)
711         return QUrl();
712     return WKURLCopyQUrl(WKFrameCopyURL(frame.get()));
713 }
714
715 QString QtWebPageProxy::title() const
716 {
717     return WKStringCopyQString(WKPageCopyTitle(toAPI(m_webPageProxy.get())));
718 }
719
720 void QtWebPageProxy::setDrawingAreaSize(const QSize& size)
721 {
722     if (!m_webPageProxy->drawingArea())
723         return;
724     m_webPageProxy->drawingArea()->setSize(IntSize(size), IntSize());
725 }
726
727 qreal QtWebPageProxy::textZoomFactor() const
728 {
729     return WKPageGetTextZoomFactor(pageRef());
730 }
731
732 void QtWebPageProxy::setTextZoomFactor(qreal zoomFactor)
733 {
734     WKPageSetTextZoomFactor(pageRef(), zoomFactor);
735 }
736
737 qreal QtWebPageProxy::pageZoomFactor() const
738 {
739     return WKPageGetPageZoomFactor(pageRef());
740 }
741
742 void QtWebPageProxy::setPageZoomFactor(qreal zoomFactor)
743 {
744     WKPageSetPageZoomFactor(pageRef(), zoomFactor);
745 }
746
747 void QtWebPageProxy::setPageAndTextZoomFactors(qreal pageZoomFactor, qreal textZoomFactor)
748 {
749     WKPageSetPageAndTextZoomFactors(pageRef(), pageZoomFactor, textZoomFactor);
750 }
751
752 QWKHistory* QtWebPageProxy::history() const
753 {
754     return m_history;
755 }
756
757 void QtWebPageProxy::startDrag(const WebCore::DragData& dragData, PassRefPtr<ShareableBitmap> dragImage)
758 {
759     QImage dragQImage;
760     if (dragImage)
761         dragQImage = dragImage->createQImage();
762     else if (dragData.platformData() && dragData.platformData()->hasImage())
763         dragQImage = qvariant_cast<QImage>(dragData.platformData()->imageData());
764
765
766     DragOperation dragOperationMask = dragData.draggingSourceOperationMask();
767     QMimeData* mimeData = const_cast<QMimeData*>(dragData.platformData());
768     Qt::DropActions supportedDropActions = dragOperationToDropActions(dragOperationMask);
769
770     QPoint clientPosition;
771     QPoint globalPosition;
772     Qt::DropAction actualDropAction;
773
774     m_viewInterface->startDrag(supportedDropActions, dragQImage, mimeData,
775                                &clientPosition, &globalPosition, &actualDropAction);
776
777     m_webPageProxy->dragEnded(clientPosition, globalPosition, dropActionToDragOperation(actualDropAction));
778 }
779
780 void QtWebPageProxy::didChangeViewportProperties(const WebCore::ViewportArguments& args)
781 {
782     m_viewInterface->didChangeViewportProperties(args);
783 }
784
785 void QtWebPageProxy::handleDownloadRequest(DownloadProxy* download)
786 {
787     // This function is responsible for hooking up a DownloadProxy to our API layer
788     // by creating a QWebDownloadItem. It will then wait for the QWebDownloadItem to be
789     // ready (filled with the ResourceResponse information) so we can pass it through to
790     // our WebViews.
791     QWebDownloadItem* downloadItem = new QWebDownloadItem();
792     downloadItem->d->downloadProxy = download;
793
794     connect(downloadItem->d, SIGNAL(receivedResponse(QWebDownloadItem*)), this, SLOT(didReceiveDownloadResponse(QWebDownloadItem*)));
795     s_downloadManager->addDownload(download, downloadItem);
796 }
797
798 void QtWebPageProxy::didReceiveDownloadResponse(QWebDownloadItem* download)
799 {
800     // Now that our downloadItem has everything we need we can emit downloadRequested.
801     m_viewInterface->downloadRequested(download);
802 }
803
804 void QtWebPageProxy::paintContent(QPainter* painter, const QRect& area)
805 {
806     // FIXME: Do something with the unpainted region?
807     WebCore::Region unpaintedRegion;
808     static_cast<DrawingAreaProxyImpl*>(m_webPageProxy->drawingArea())->paint(painter, area, unpaintedRegion);
809 }
810
811 PassOwnPtr<DrawingAreaProxy> QtWebPageProxy::createDrawingAreaProxy()
812 {
813     return DrawingAreaProxyImpl::create(m_webPageProxy.get());
814 }
815
816 void QtWebPageProxy::renderToCurrentGLContext(const TransformationMatrix& transform, float opacity)
817 {
818     DrawingAreaProxy* drawingArea = m_webPageProxy->drawingArea();
819     if (drawingArea)
820         drawingArea->paintToCurrentGLContext(transform, opacity);
821 }
822
823 #if ENABLE(TOUCH_EVENTS)
824 void QtWebPageProxy::doneWithTouchEvent(const NativeWebTouchEvent& event, bool wasEventHandled)
825 {
826     if (!m_interactionEngine)
827         return;
828     if (wasEventHandled || event.type() == WebEvent::TouchCancel) {
829         m_panGestureRecognizer.reset();
830         m_pinchGestureRecognizer.reset();
831         return;
832     }
833
834     const QTouchEvent* ev = event.nativeEvent();
835
836     switch (ev->type()) {
837     case QEvent::TouchBegin:
838         ASSERT(!m_interactionEngine->panGestureActive());
839         ASSERT(!m_interactionEngine->pinchGestureActive());
840
841         // The interaction engine might still be animating kinetic scrolling or a scale animation
842         // such as double-tap to zoom or the bounce back effect. A touch stops the kinetic scrolling
843         // where as it does not stop the scale animation.
844         if (m_interactionEngine->scrollAnimationActive())
845             m_interactionEngine->interruptScrollAnimation();
846         break;
847     case QEvent::TouchUpdate:
848         // The scale animation can only be interrupted by a pinch gesture, which will then take over.
849         if (m_interactionEngine->scaleAnimationActive() && m_pinchGestureRecognizer.isRecognized())
850             m_interactionEngine->interruptScaleAnimation();
851         break;
852     default:
853         break;
854     }
855
856     // If the scale animation is active we don't pass the event to the recognizers. In the future
857     // we would want to queue the event here and repost then when the animation ends.
858     if (m_interactionEngine->scaleAnimationActive())
859         return;
860
861     // Convert the event timestamp from second to millisecond.
862     qint64 eventTimestampMillis = static_cast<qint64>(event.timestamp() * 1000);
863     m_panGestureRecognizer.recognize(ev, eventTimestampMillis);
864     m_pinchGestureRecognizer.recognize(ev);
865 }
866 #endif
867
868 void QtWebPageProxy::setVisibleContentRectAndScale(const QRectF& visibleContentRect, float scale)
869 {
870     QRect alignedVisibleContentRect = visibleContentRect.toAlignedRect();
871     m_webPageProxy->drawingArea()->setVisibleContentsRectAndScale(alignedVisibleContentRect, scale);
872
873     // FIXME: Once we support suspend and resume, this should be delayed until the page is active if the page is suspended.
874     m_webPageProxy->setFixedVisibleContentRect(alignedVisibleContentRect);
875 }
876
877 void QtWebPageProxy::setVisibleContentRectTrajectoryVector(const QPointF& trajectoryVector)
878 {
879     m_webPageProxy->drawingArea()->setVisibleContentRectTrajectoryVector(trajectoryVector);
880 }
881
882 void QtWebPageProxy::touchEvent(QTouchEvent* event)
883 {
884 #if ENABLE(TOUCH_EVENTS)
885     m_webPageProxy->handleTouchEvent(NativeWebTouchEvent(event));
886     event->accept();
887 #else
888     ASSERT_NOT_REACHED();
889     ev->ignore();
890 #endif
891 }
892
893 void QtWebPageProxy::findZoomableAreaForPoint(const QPoint& point)
894 {
895     m_webPageProxy->findZoomableAreaForPoint(point);
896 }
897
898 #include "moc_QtWebPageProxy.cpp"