2 * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
22 #include "QtWebPageProxy.h"
24 #include "QtWebError.h"
25 #include "qwebpreferences_p.h"
27 #include "ClientImpl.h"
28 #include "qwkhistory.h"
29 #include "qwkhistory_p.h"
30 #include "FindIndicator.h"
31 #include "LocalizedStrings.h"
32 #include "NativeWebKeyboardEvent.h"
33 #include "NotImplemented.h"
34 #include "QtPolicyInterface.h"
35 #include "QtViewInterface.h"
36 #include "QtWebUndoCommand.h"
37 #include "WebBackForwardList.h"
38 #include "WebContext.h"
39 #include "WebContextMenuProxyQt.h"
40 #include "WebEditCommandProxy.h"
41 #include "WebEventFactoryQt.h"
42 #include "WebPopupMenuProxyQt.h"
43 #include "WKStringQt.h"
45 #include <QApplication>
46 #include <QGraphicsSceneMouseEvent>
49 #include <QTouchEvent>
52 #include <WebCore/Cursor.h>
53 #include <WebCore/DragData.h>
54 #include <WebCore/FloatRect.h>
55 #include <WebCore/NotImplemented.h>
56 #include <WebKit2/WKFrame.h>
57 #include <WebKit2/WKPageGroup.h>
58 #include <WebKit2/WKRetainPtr.h>
60 using namespace WebKit;
61 using namespace WebCore;
64 RefPtr<WebContext> QtWebPageProxy::s_defaultContext;
66 unsigned QtWebPageProxy::s_defaultPageProxyCount = 0;
68 PassRefPtr<WebContext> QtWebPageProxy::defaultWKContext()
70 if (!s_defaultContext)
71 s_defaultContext = WebContext::create(String());
72 return s_defaultContext;
75 static inline Qt::DropActions dragOperationToDropActions(unsigned dragOperations)
77 Qt::DropActions result = Qt::IgnoreAction;
78 if (dragOperations & DragOperationCopy)
79 result |= Qt::CopyAction;
80 if (dragOperations & DragOperationMove)
81 result |= Qt::MoveAction;
82 if (dragOperations & DragOperationGeneric)
83 result |= Qt::MoveAction;
84 if (dragOperations & DragOperationLink)
85 result |= Qt::LinkAction;
89 WebCore::DragOperation dropActionToDragOperation(Qt::DropActions actions)
92 if (actions & Qt::CopyAction)
93 result |= DragOperationCopy;
94 if (actions & Qt::MoveAction)
95 result |= (DragOperationMove | DragOperationGeneric);
96 if (actions & Qt::LinkAction)
97 result |= DragOperationLink;
98 if (result == (DragOperationCopy | DragOperationMove | DragOperationGeneric | DragOperationLink))
99 result = DragOperationEvery;
100 return (DragOperation)result;
103 QtWebPageProxy::QtWebPageProxy(QtViewInterface* viewInterface, QtPolicyInterface* policyInterface, WKContextRef contextRef, WKPageGroupRef pageGroupRef)
104 : m_viewInterface(viewInterface)
105 , m_policyInterface(policyInterface)
106 , m_context(contextRef ? toImpl(contextRef) : defaultWKContext())
108 , m_undoStack(adoptPtr(new QUndoStack(this)))
111 ASSERT(viewInterface);
112 m_webPageProxy = m_context->createWebPage(this, toImpl(pageGroupRef));
113 m_history = QWKHistoryPrivate::createHistory(this, m_webPageProxy->backForwardList());
115 s_defaultPageProxyCount++;
118 void QtWebPageProxy::init()
120 m_webPageProxy->initializeWebPage();
122 setupPageLoaderClient(this, m_webPageProxy.get());
123 setupPageUiClient(this, m_webPageProxy.get());
125 if (m_policyInterface)
126 setupPagePolicyClient(m_policyInterface, m_webPageProxy.get());
129 QtWebPageProxy::~QtWebPageProxy()
131 m_webPageProxy->close();
132 // The context is the default one and we're deleting the last QtWebPageProxy.
133 if (m_context == s_defaultContext) {
134 ASSERT(s_defaultPageProxyCount > 0);
135 s_defaultPageProxyCount--;
136 if (!s_defaultPageProxyCount)
137 s_defaultContext.clear();
142 bool QtWebPageProxy::handleEvent(QEvent* ev)
144 switch (ev->type()) {
145 case QEvent::KeyPress:
146 return handleKeyPressEvent(reinterpret_cast<QKeyEvent*>(ev));
147 case QEvent::KeyRelease:
148 return handleKeyReleaseEvent(reinterpret_cast<QKeyEvent*>(ev));
149 case QEvent::FocusIn:
150 return handleFocusInEvent(reinterpret_cast<QFocusEvent*>(ev));
151 case QEvent::FocusOut:
152 return handleFocusOutEvent(reinterpret_cast<QFocusEvent*>(ev));
155 // FIXME: Move all common event handling here.
159 bool QtWebPageProxy::handleKeyPressEvent(QKeyEvent* ev)
161 m_webPageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(ev));
165 bool QtWebPageProxy::handleKeyReleaseEvent(QKeyEvent* ev)
167 m_webPageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(ev));
171 bool QtWebPageProxy::handleFocusInEvent(QFocusEvent*)
173 m_webPageProxy->viewStateDidChange(WebPageProxy::ViewIsFocused | WebPageProxy::ViewWindowIsActive);
177 bool QtWebPageProxy::handleFocusOutEvent(QFocusEvent*)
179 m_webPageProxy->viewStateDidChange(WebPageProxy::ViewIsFocused | WebPageProxy::ViewWindowIsActive);
183 void QtWebPageProxy::setCursor(const WebCore::Cursor& cursor)
185 m_viewInterface->didChangeCursor(*cursor.platformCursor());
188 void QtWebPageProxy::setCursorHiddenUntilMouseMoves(bool hiddenUntilMouseMoves)
193 void QtWebPageProxy::setViewNeedsDisplay(const WebCore::IntRect& rect)
195 m_viewInterface->setViewNeedsDisplay(QRect(rect));
198 void QtWebPageProxy::displayView()
203 void QtWebPageProxy::scrollView(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollOffset)
208 WebCore::IntSize QtWebPageProxy::viewSize()
210 return WebCore::IntSize(m_viewInterface->drawingAreaSize());
213 bool QtWebPageProxy::isViewWindowActive()
215 return m_viewInterface->isActive();
218 bool QtWebPageProxy::isViewFocused()
220 return m_viewInterface->hasFocus();
223 bool QtWebPageProxy::isViewVisible()
225 return m_viewInterface->isVisible();
228 bool QtWebPageProxy::isViewInWindow()
234 void QtWebPageProxy::enterAcceleratedCompositingMode(const LayerTreeContext&)
239 void QtWebPageProxy::exitAcceleratedCompositingMode()
244 void QtWebPageProxy::pageDidRequestScroll(const IntPoint& point)
246 emit scrollRequested(point.x(), point.y());
249 void QtWebPageProxy::didChangeContentsSize(const IntSize& newSize)
251 m_viewInterface->contentSizeChanged(QSize(newSize));
254 void QtWebPageProxy::toolTipChanged(const String&, const String& newTooltip)
256 m_viewInterface->didChangeToolTip(QString(newTooltip));
259 void QtWebPageProxy::registerEditCommand(PassRefPtr<WebEditCommandProxy> command, WebPageProxy::UndoOrRedo undoOrRedo)
261 if (undoOrRedo == WebPageProxy::Undo) {
262 const QtWebUndoCommand* webUndoCommand = static_cast<const QtWebUndoCommand*>(m_undoStack->command(m_undoStack->index()));
263 if (webUndoCommand && webUndoCommand->inUndoRedo())
265 m_undoStack->push(new QtWebUndoCommand(command));
269 void QtWebPageProxy::clearAllEditCommands()
271 m_undoStack->clear();
274 bool QtWebPageProxy::canUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
276 if (undoOrRedo == WebPageProxy::Undo)
277 return m_undoStack->canUndo();
278 return m_undoStack->canRedo();
281 void QtWebPageProxy::executeUndoRedo(WebPageProxy::UndoOrRedo undoOrRedo)
283 if (undoOrRedo == WebPageProxy::Undo)
289 FloatRect QtWebPageProxy::convertToDeviceSpace(const FloatRect& rect)
294 IntPoint QtWebPageProxy::screenToWindow(const IntPoint& point)
299 IntRect QtWebPageProxy::windowToScreen(const IntRect& rect)
304 FloatRect QtWebPageProxy::convertToUserSpace(const FloatRect& rect)
309 void QtWebPageProxy::selectionChanged(bool, bool, bool, bool)
313 void QtWebPageProxy::doneWithKeyEvent(const NativeWebKeyboardEvent&, bool)
317 PassRefPtr<WebPopupMenuProxy> QtWebPageProxy::createPopupMenuProxy(WebPageProxy*)
319 return WebPopupMenuProxyQt::create();
322 PassRefPtr<WebContextMenuProxy> QtWebPageProxy::createContextMenuProxy(WebPageProxy*)
324 return WebContextMenuProxyQt::create(m_webPageProxy.get(), m_viewInterface);
327 void QtWebPageProxy::setFindIndicator(PassRefPtr<FindIndicator>, bool fadeOut)
331 void QtWebPageProxy::didCommitLoadForMainFrame(bool useCustomRepresentation)
335 void QtWebPageProxy::didFinishLoadingDataForCustomRepresentation(const String& suggestedFilename, const CoreIPC::DataReference&)
339 void QtWebPageProxy::flashBackingStoreUpdates(const Vector<IntRect>&)
344 WKPageRef QtWebPageProxy::pageRef() const
346 return toAPI(m_webPageProxy.get());;
349 void QtWebPageProxy::didFindZoomableArea(const IntPoint& target, const IntRect& area)
351 m_viewInterface->didFindZoomableArea(QPoint(target), QRect(area));
354 void QtWebPageProxy::didChangeUrl(const QUrl& url)
356 m_viewInterface->didChangeUrl(url);
359 void QtWebPageProxy::didChangeTitle(const QString& newTitle)
361 m_viewInterface->didChangeTitle(newTitle);
364 void QtWebPageProxy::loadDidBegin()
366 m_viewInterface->loadDidBegin();
369 void QtWebPageProxy::loadDidCommit()
371 m_viewInterface->loadDidCommit();
374 void QtWebPageProxy::loadDidSucceed()
376 m_viewInterface->loadDidSucceed();
379 void QtWebPageProxy::loadDidFail(const QtWebError& error)
381 m_viewInterface->loadDidFail(error);
384 void QtWebPageProxy::didChangeLoadProgress(int newLoadProgress)
386 m_loadProgress = newLoadProgress;
387 m_viewInterface->didChangeLoadProgress(newLoadProgress);
390 void QtWebPageProxy::paint(QPainter* painter, const QRect& area)
392 if (m_webPageProxy->isValid())
393 paintContent(painter, area);
395 painter->fillRect(area, Qt::white);
398 bool QtWebPageProxy::canGoBack() const
400 return m_webPageProxy->canGoBack();
403 void QtWebPageProxy::goBack()
405 m_webPageProxy->goBack();
408 bool QtWebPageProxy::canGoForward() const
410 return m_webPageProxy->canGoForward();
413 void QtWebPageProxy::goForward()
415 m_webPageProxy->goForward();
418 bool QtWebPageProxy::canStop() const
420 RefPtr<WebKit::WebFrameProxy> mainFrame = m_webPageProxy->mainFrame();
421 return mainFrame && !(WebFrameProxy::LoadStateFinished == mainFrame->loadState());
424 void QtWebPageProxy::stop()
426 m_webPageProxy->stopLoading();
429 bool QtWebPageProxy::canReload() const
431 RefPtr<WebKit::WebFrameProxy> mainFrame = m_webPageProxy->mainFrame();
433 return (WebFrameProxy::LoadStateFinished == mainFrame->loadState());
434 return m_webPageProxy->backForwardList()->currentItem();
437 void QtWebPageProxy::reload()
439 m_webPageProxy->reload(/* reloadFromOrigin */ true);
442 void QtWebPageProxy::navigationStateChanged()
444 emit updateNavigationState();
447 void QtWebPageProxy::didRelaunchProcess()
449 updateNavigationState();
450 m_viewInterface->didRelaunchProcess();
451 setDrawingAreaSize(m_viewInterface->drawingAreaSize());
454 void QtWebPageProxy::processDidCrash()
456 updateNavigationState();
457 m_viewInterface->processDidCrash();
460 QWebPreferences* QtWebPageProxy::preferences() const
462 if (!m_preferences) {
463 WKPageGroupRef pageGroupRef = WKPageGetPageGroup(pageRef());
464 m_preferences = QWebPreferencesPrivate::createPreferences(pageGroupRef);
467 return m_preferences;
470 void QtWebPageProxy::setCustomUserAgent(const QString& userAgent)
472 WKRetainPtr<WKStringRef> wkUserAgent(WKStringCreateWithQString(userAgent));
473 WKPageSetCustomUserAgent(pageRef(), wkUserAgent.get());
476 QString QtWebPageProxy::customUserAgent() const
478 return WKStringCopyQString(WKPageCopyCustomUserAgent(pageRef()));
481 void QtWebPageProxy::load(const QUrl& url)
483 WKRetainPtr<WKURLRef> wkurl(WKURLCreateWithQUrl(url));
484 WKPageLoadURL(pageRef(), wkurl.get());
487 QUrl QtWebPageProxy::url() const
489 WKRetainPtr<WKFrameRef> frame = WKPageGetMainFrame(pageRef());
492 return WKURLCopyQUrl(WKFrameCopyURL(frame.get()));
495 QString QtWebPageProxy::title() const
497 return WKStringCopyQString(WKPageCopyTitle(toAPI(m_webPageProxy.get())));
500 void QtWebPageProxy::setDrawingAreaSize(const QSize& size)
502 if (!m_webPageProxy->drawingArea())
504 m_webPageProxy->drawingArea()->setSize(IntSize(size), IntSize());
507 qreal QtWebPageProxy::textZoomFactor() const
509 return WKPageGetTextZoomFactor(pageRef());
512 void QtWebPageProxy::setTextZoomFactor(qreal zoomFactor)
514 WKPageSetTextZoomFactor(pageRef(), zoomFactor);
517 qreal QtWebPageProxy::pageZoomFactor() const
519 return WKPageGetPageZoomFactor(pageRef());
522 void QtWebPageProxy::setPageZoomFactor(qreal zoomFactor)
524 WKPageSetPageZoomFactor(pageRef(), zoomFactor);
527 void QtWebPageProxy::setPageAndTextZoomFactors(qreal pageZoomFactor, qreal textZoomFactor)
529 WKPageSetPageAndTextZoomFactors(pageRef(), pageZoomFactor, textZoomFactor);
532 QWKHistory* QtWebPageProxy::history() const
537 void QtWebPageProxy::startDrag(const WebCore::DragData& dragData, PassRefPtr<ShareableBitmap> dragImage)
541 dragQImage = dragImage->createQImage();
542 else if (dragData.platformData() && dragData.platformData()->hasImage())
543 dragQImage = qvariant_cast<QImage>(dragData.platformData()->imageData());
546 DragOperation dragOperationMask = dragData.draggingSourceOperationMask();
547 QMimeData* mimeData = const_cast<QMimeData*>(dragData.platformData());
548 Qt::DropActions supportedDropActions = dragOperationToDropActions(dragOperationMask);
550 QPoint clientPosition;
551 QPoint globalPosition;
552 Qt::DropAction actualDropAction;
554 m_viewInterface->startDrag(supportedDropActions, dragQImage, mimeData,
555 &clientPosition, &globalPosition, &actualDropAction);
557 m_webPageProxy->dragEnded(clientPosition, globalPosition, dropActionToDragOperation(actualDropAction));
560 void QtWebPageProxy::didChangeViewportProperties(const WebCore::ViewportArguments& args)
562 m_viewInterface->didChangeViewportProperties(args);
565 #include "moc_QtWebPageProxy.cpp"