Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / Tools / QtTestBrowser / webpage.cpp
1 /*
2  * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
3  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
4  * Copyright (C) 2006 George Staikos <staikos@kde.org>
5  * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
7  * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "webpage.h"
34
35 #include "launcherwindow.h"
36
37 #include <QAuthenticator>
38 #include <QDesktopServices>
39 #include <QtGui>
40 #include <QtNetwork/QNetworkReply>
41 #include <QtNetwork/QNetworkRequest>
42 #include <QtNetwork/QNetworkProxy>
43
44 WebPage::WebPage(QObject* parent)
45     : QWebPage(parent)
46     , m_userAgent()
47     , m_interruptingJavaScriptEnabled(false)
48 {
49     applyProxy();
50
51     connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
52     connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)));
53 }
54
55 void WebPage::applyProxy()
56 {
57     QUrl proxyUrl(qgetenv("http_proxy"));
58
59     if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
60         int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
61         networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
62     }
63 }
64
65 bool WebPage::supportsExtension(QWebPage::Extension extension) const
66 {
67     if (extension == QWebPage::ErrorPageExtension)
68         return true;
69     return false;
70 }
71
72 bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
73 {
74     const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
75     QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
76
77     errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
78         .arg(info->errorString).toUtf8();
79
80     return true;
81 }
82
83 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
84 {
85     QObject* view = parent();
86
87     QVariant value = view->property("keyboardModifiers");
88
89     if (!value.isNull()) {
90         Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
91
92         if (modifiers & Qt::ShiftModifier) {
93             QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
94             page->mainFrame()->load(request);
95             return false;
96         }
97
98         if (modifiers & Qt::AltModifier) {
99             openUrlInDefaultBrowser(request.url());
100             return false;
101         }
102     }
103
104     return QWebPage::acceptNavigationRequest(frame, request, type);
105 }
106
107 void WebPage::openUrlInDefaultBrowser(const QUrl& url)
108 {
109     if (QAction* action = qobject_cast<QAction*>(sender()))
110         QDesktopServices::openUrl(action->data().toUrl());
111     else
112         QDesktopServices::openUrl(url);
113 }
114
115 QString WebPage::userAgentForUrl(const QUrl& url) const
116 {
117     if (!m_userAgent.isEmpty())
118         return m_userAgent;
119     return QWebPage::userAgentForUrl(url);
120 }
121
122 void WebPage::setQnamThreaded(bool threaded)
123 {
124     bool alreadyThreaded = networkAccessManager()->thread() != thread();
125     if (threaded == alreadyThreaded)
126         return;
127
128     if (threaded) {
129         m_qnamThread.reset(new QtNAMThread);
130         m_qnamThread->start();
131         setNetworkAccessManager(m_qnamThread->networkAccessManager());
132     } else {
133         setNetworkAccessManager(0);
134         m_qnamThread.reset();
135     }
136
137     Qt::ConnectionType connectionType = threaded ? Qt::BlockingQueuedConnection : Qt::DirectConnection;
138     connect(networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
139             this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)),
140             connectionType);
141     applyProxy();
142 }
143
144 bool WebPage::shouldInterruptJavaScript()
145 {
146     if (!m_interruptingJavaScriptEnabled)
147         return false;
148     return QWebPage::shouldInterruptJavaScript();
149 }
150
151 void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
152 {
153     QDialog* dialog = new QDialog(QApplication::activeWindow());
154     dialog->setWindowTitle("HTTP Authentication");
155
156     QGridLayout* layout = new QGridLayout(dialog);
157     dialog->setLayout(layout);
158
159     QLabel* messageLabel = new QLabel(dialog);
160     messageLabel->setWordWrap(true);
161     QString messageStr = QString("Enter with username and password for: %1");
162     messageLabel->setText(messageStr.arg(reply->url().toString()));
163     layout->addWidget(messageLabel, 0, 1);
164
165     QLabel* userLabel = new QLabel("Username:", dialog);
166     layout->addWidget(userLabel, 1, 0);
167     QLineEdit* userInput = new QLineEdit(dialog);
168     layout->addWidget(userInput, 1, 1);
169
170     QLabel* passLabel = new QLabel("Password:", dialog);
171     layout->addWidget(passLabel, 2, 0);
172     QLineEdit* passInput = new QLineEdit(dialog);
173     passInput->setEchoMode(QLineEdit::Password);
174     layout->addWidget(passInput, 2, 1);
175
176     QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
177             | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
178     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
179     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
180     layout->addWidget(buttonBox, 3, 1);
181
182     if (dialog->exec() == QDialog::Accepted) {
183         authenticator->setUser(userInput->text());
184         authenticator->setPassword(passInput->text());
185     }
186
187     delete dialog;
188 }
189
190 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
191 {
192     setFeaturePermission(frame, feature, PermissionGrantedByUser);
193 }
194
195 void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
196 {
197 }
198
199 QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
200 {
201     LauncherWindow* mw = new LauncherWindow;
202     if (type == WebModalDialog)
203         mw->setWindowModality(Qt::ApplicationModal);
204     mw->show();
205     return mw->page();
206 }
207
208 QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
209 {
210     if (classId == "alien_QLabel") {
211         QLabel* l = new QLabel;
212         l->winId();
213         return l;
214     }
215
216 #ifndef QT_NO_UITOOLS
217     QUiLoader loader;
218     return loader.createWidget(classId, view());
219 #else
220     Q_UNUSED(classId);
221     return 0;
222 #endif
223 }
224