All pastes #1586558 Raw Edit

Miscellany

public text v1 · immutable
#1586558 ·published 2009-09-30 19:48 UTC
rendered paste body
From 22a88b44c31c6a013b96590f9e097f161be70e0e Mon Sep 17 00:00:00 2001
From: Kenneth Rohde Christiansen <kenneth.christiansen@openbossa.org>
Date: Wed, 30 Sep 2009 16:39:52 -0300
Subject: [PATCH] Use the new error page extension from QWebPage for showing better error pages.

---
 src/webpage.cpp |  118 ++++++++++++++++++++++++++++++++----------------------
 src/webpage.h   |    5 ++
 2 files changed, 75 insertions(+), 48 deletions(-)

diff --git a/src/webpage.cpp b/src/webpage.cpp
index d619c7c..40b4b4b 100644
--- a/src/webpage.cpp
+++ b/src/webpage.cpp
@@ -114,6 +114,64 @@ WebPage::WebPage(QObject *parent)
     loadSettings();
 }
 
+bool WebPage::supportsExtension(Extension extension) const
+{
+#if defined(WEBKIT_TRUNK)
+    if (extension == ErrorPageExtension)
+        return true;
+#endif
+    return false;
+}
+
+bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
+{
+#if defined(WEBKIT_TRUNK)
+    if (extension == ErrorPageExtension)
+        return errorPageExtension(
+                static_cast<const ErrorPageExtensionOption*>(option),
+                static_cast<ErrorPageExtensionReturn*>(output));
+#endif
+
+    return false;
+}
+
+#if defined(WEBKIT_TRUNK)
+bool WebPage::errorPageExtension(const ErrorPageExtensionOption *option, ErrorPageExtensionReturn *output)
+{
+    // Disregard Policy Changes, which happens then unsupported content in encountered, as the
+    // policy changes from "use" to "download".
+    if (option->domain == WebKit && option->error == 102)
+        return false;
+
+    // Generate translated not found error page with an image
+    QFile notFoundErrorFile(QLatin1String(":/notfound.html"));
+    if (!notFoundErrorFile.open(QIODevice::ReadOnly))
+        return false;
+
+    QString title = tr("Error loading page: %1").arg(QString::fromUtf8(option->url.toEncoded()));
+    QString html = QLatin1String(notFoundErrorFile.readAll());
+    QPixmap pixmap = qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, view()).pixmap(QSize(32, 32));
+    QBuffer imageBuffer;
+    imageBuffer.open(QBuffer::ReadWrite);
+    if (pixmap.save(&imageBuffer, "PNG")) {
+        html.replace(QLatin1String("IMAGE_BINARY_DATA_HERE"),
+                     QLatin1String(imageBuffer.buffer().toBase64()));
+    }
+
+    html = html.arg(title, option->errorString,
+                    tr("When connecting to: %1.").arg(QString::fromUtf8(option->url.toEncoded())),
+                    tr("Check the address for errors such as <b>ww</b>.arora-browser.org instead of <b>www</b>.arora-browser.org"),
+                    tr("If the address is correct, try checking the network connection."),
+                    tr("If your computer or network is protected by a firewall or proxy, make sure that the browser is permitted to access the network."));
+
+    // Sets the right URL. FIXME: Remove hack when WebKit is fixed.
+    output->baseUrl = option->url;
+    output->content = html.toUtf8();
+
+    return true;
+}
+#endif
+
 WebPluginFactory *WebPage::webPluginFactory()
 {
     if (!s_webPluginFactory)
@@ -324,56 +382,20 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply)
     if (replyUrl.scheme() == QLatin1String("abp"))
         return;
 
-    switch (reply->error()) {
-    case QNetworkReply::NoError:
-        if (reply->header(QNetworkRequest::ContentTypeHeader).isValid()) {
-            BrowserApplication::downloadManager()->handleUnsupportedContent(reply);
-            return;
-        }
-        break;
-    case QNetworkReply::ProtocolUnknownError: {
-        QSettings settings;
-        settings.beginGroup(QLatin1String("WebView"));
-        QStringList externalSchemes = settings.value(QLatin1String("externalSchemes")).toStringList();
-        if (externalSchemes.contains(replyUrl.scheme())) {
-            QDesktopServices::openUrl(replyUrl);
-            return;
-        }
-        break;
-    }
-    default:
-        break;
-    }
-
-    // Find the frame that has the unsupported content
-    if (replyUrl.isEmpty() || replyUrl != m_requestedUrl)
-        return;
-
-    QWebFrame *notFoundFrame = mainFrame();
-    if (!notFoundFrame)
+    if (reply->header(QNetworkRequest::ContentTypeHeader).isValid()) {
+        BrowserApplication::downloadManager()->handleUnsupportedContent(reply);
         return;
+    }
 
-    // Generate translated not found error page with an image
-    QFile notFoundErrorFile(QLatin1String(":/notfound.html"));
-    if (!notFoundErrorFile.open(QIODevice::ReadOnly))
-        return;
-    QString title = tr("Error loading page: %1").arg(QString::fromUtf8(replyUrl.toEncoded()));
-    QString html = QLatin1String(notFoundErrorFile.readAll());
-    QPixmap pixmap = qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, view()).pixmap(QSize(32, 32));
-    QBuffer imageBuffer;
-    imageBuffer.open(QBuffer::ReadWrite);
-    if (pixmap.save(&imageBuffer, "PNG")) {
-        html.replace(QLatin1String("IMAGE_BINARY_DATA_HERE"),
-                     QLatin1String(imageBuffer.buffer().toBase64()));
+    QSettings settings;
+    settings.beginGroup(QLatin1String("WebView"));
+    QStringList externalSchemes = settings.value(QLatin1String("externalSchemes")).toStringList();
+    if (externalSchemes.contains(replyUrl.scheme())) {
+        if (QDesktopServices::openUrl(replyUrl));
+            return;
     }
-    html = html.arg(title,
-                    reply->errorString(),
-                    tr("When connecting to: %1.").arg(QString::fromUtf8(replyUrl.toEncoded())),
-                    tr("Check the address for errors such as <b>ww</b>.arora-browser.org instead of <b>www</b>.arora-browser.org"),
-                    tr("If the address is correct, try checking the network connection."),
-                    tr("If your computer or network is protected by a firewall or proxy, make sure that the browser is permitted to access the network."));
-    notFoundFrame->setHtml(html, replyUrl);
-    // Don't put error pages to the history.
-    BrowserApplication::instance()->historyManager()->removeHistoryEntry(replyUrl, notFoundFrame->title());
+    // FIXME: would be nice to be able to set an error on the reply instead.
+    // like QNetworkReply::UnknownContentError.
+    reply->abort();
 }
 
diff --git a/src/webpage.h b/src/webpage.h
index b9a80ad..626b176 100644
--- a/src/webpage.h
+++ b/src/webpage.h
@@ -78,8 +78,13 @@ public:
 
     static WebPluginFactory *webPluginFactory();
     QList<WebPageLinkedResource> linkedResources(const QString &relation = QString());
+    virtual bool supportsExtension(QWebPage::Extension extension) const;
+    virtual bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output);
 
 protected:
+#if defined(WEBKIT_TRUNK)
+    bool errorPageExtension(const ErrorPageExtensionOption *option, ErrorPageExtensionReturn *output);
+#endif
     QString userAgentForUrl(const QUrl &url) const;
     bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request,
                                  NavigationType type);
-- 
1.6.0.4