summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Rossi <pierre.rossi@digia.com>2013-09-05 15:55:00 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-17 15:06:19 +0200
commit7642fabf5f62fbc583dc6bd9f349dbe1e45e805a (patch)
treef493be28f91e06b46534061bcd251d259105da58
parentd9276b51c64c2b0b1cd97dcd02c0a83e78018f81 (diff)
Add javascript dialogs support
This is just the basic core part and widgets plumbing and default implementations of the virtual functions in QWebEnginePage. QtQuick implementation is still yet to be done Change-Id: I7cf8d6e5ec0bf747d45e9914db57bd0e4ef95b7f Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
-rw-r--r--lib/javascript_dialog_manager_qt.cpp78
-rw-r--r--lib/javascript_dialog_manager_qt.h71
-rw-r--r--lib/lib.pro2
-rw-r--r--lib/quick/qquickwebengineview_p_p.h1
-rw-r--r--lib/type_conversion.h7
-rw-r--r--lib/web_contents_adapter_client.h8
-rw-r--r--lib/web_contents_delegate_qt.cpp5
-rw-r--r--lib/web_contents_delegate_qt.h6
-rw-r--r--lib/web_contents_view_qt.h3
-rw-r--r--lib/widgets/Api/qwebenginepage.cpp38
-rw-r--r--lib/widgets/Api/qwebenginepage.h6
-rw-r--r--lib/widgets/Api/qwebenginepage_p.h1
12 files changed, 221 insertions, 5 deletions
diff --git a/lib/javascript_dialog_manager_qt.cpp b/lib/javascript_dialog_manager_qt.cpp
new file mode 100644
index 000000000..b2f6d86bc
--- /dev/null
+++ b/lib/javascript_dialog_manager_qt.cpp
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebEngine module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "javascript_dialog_manager_qt.h"
+
+#include "web_contents_adapter_client.h"
+#include "web_contents_view_qt.h"
+#include "type_conversion.h"
+
+#include "base/memory/singleton.h"
+
+Q_STATIC_ASSERT_X(static_cast<int>(content::JAVASCRIPT_MESSAGE_TYPE_PROMPT) == static_cast<int>(WebContentsAdapterClient::PromptDialog), "These enums should be in sync.");
+
+JavaScriptDialogManagerQt *JavaScriptDialogManagerQt::GetInstance()
+{
+ return Singleton<JavaScriptDialogManagerQt>::get();
+}
+
+void JavaScriptDialogManagerQt::RunJavaScriptDialog(content::WebContents *webContents, const GURL &originUrl, const std::string &acceptLang, content::JavaScriptMessageType javascriptMessageType, const base::string16 &messageText, const base::string16 &defaultPromptText, const content::JavaScriptDialogManager::DialogClosedCallback &callback, bool *didSuppressMessage)
+{
+ Q_UNUSED(originUrl);
+ Q_UNUSED(acceptLang);
+
+ WebContentsAdapterClient *client = WebContentsViewQt::from(webContents->GetView())->client();
+ if (!client) {
+ *didSuppressMessage = true;
+ return;
+ }
+
+ QString promptInput;
+ WebContentsAdapterClient::JavascriptDialogType dialogType = static_cast<WebContentsAdapterClient::JavascriptDialogType>(javascriptMessageType);
+ bool res = client->javascriptDialog(dialogType, toQt(messageText).toHtmlEscaped(), toQt(defaultPromptText).toHtmlEscaped(), &promptInput);
+ callback.Run(res, toString16(promptInput));
+}
+
+bool JavaScriptDialogManagerQt::HandleJavaScriptDialog(content::WebContents *, bool accept, const base::string16 *promptOverride)
+{
+ // FIXME: We might need to keep a queue of modal dialogs in there and unqueue them...
+ return false;
+}
diff --git a/lib/javascript_dialog_manager_qt.h b/lib/javascript_dialog_manager_qt.h
new file mode 100644
index 000000000..1a2a01db2
--- /dev/null
+++ b/lib/javascript_dialog_manager_qt.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtWebEngine module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#ifndef JAVASCRIPT_DIALOG_MANAGER_QT_H
+#define JAVASCRIPT_DIALOG_MANAGER_QT_H
+
+#include "content/public/browser/javascript_dialog_manager.h"
+#include "content/public/common/javascript_message_type.h"
+
+#include "qglobal.h"
+
+namespace content {
+class WebContents;
+}
+
+class JavaScriptDialogManagerQt : public content::JavaScriptDialogManager
+{
+public:
+ // For use with the Singleton helper class from chromium
+ static JavaScriptDialogManagerQt *GetInstance();
+
+ virtual void RunJavaScriptDialog(content::WebContents *, const GURL &, const std::string &acceptLang, content::JavaScriptMessageType javascriptMessageType,
+ const base::string16 &messageText, const base::string16 &defaultPromptText,
+ const content::JavaScriptDialogManager::DialogClosedCallback &callback, bool *didSuppressMessage);
+ virtual void RunBeforeUnloadDialog(content::WebContents *, const base::string16 &messageText, bool isReload,
+ const content::JavaScriptDialogManager::DialogClosedCallback &callback) { Q_UNUSED(messageText); Q_UNUSED(isReload); Q_UNUSED(callback); }
+ virtual bool HandleJavaScriptDialog(content::WebContents *, bool accept, const base::string16 *promptOverride);
+ virtual void CancelActiveAndPendingDialogs(content::WebContents *) {}
+ virtual void WebContentsDestroyed(content::WebContents *) {}
+
+};
+
+#endif // JAVASCRIPT_DIALOG_MANAGER_QT_H
+
diff --git a/lib/lib.pro b/lib/lib.pro
index eda03287d..cbbb84a4a 100644
--- a/lib/lib.pro
+++ b/lib/lib.pro
@@ -34,6 +34,7 @@ SOURCES = \
backing_store_qt.cpp \
content_browser_client_qt.cpp \
download_manager_delegate_qt.cpp \
+ javascript_dialog_manager_qt.cpp \
render_widget_host_view_qt.cpp \
render_widget_host_view_qt_delegate.cpp \
resource_context_qt.cpp \
@@ -49,6 +50,7 @@ HEADERS = \
browser_context_qt.h \
content_browser_client_qt.h \
download_manager_delegate_qt.h \
+ javascript_dialog_manager_qt.h \
render_widget_host_view_qt.h \
render_widget_host_view_qt_delegate.h \
resource_context_qt.h \
diff --git a/lib/quick/qquickwebengineview_p_p.h b/lib/quick/qquickwebengineview_p_p.h
index 54bc21628..bd127c02b 100644
--- a/lib/quick/qquickwebengineview_p_p.h
+++ b/lib/quick/qquickwebengineview_p_p.h
@@ -69,6 +69,7 @@ public:
virtual void focusContainer() Q_DECL_OVERRIDE;
virtual void adoptNewWindow(WebContentsAdapter *newWebContents, WindowOpenDisposition disposition) Q_DECL_OVERRIDE;
virtual bool contextMenuRequested(const WebEngineContextMenuData &) Q_DECL_OVERRIDE { return false;}
+ virtual bool javascriptDialog(JavascriptDialogType type, const QString &message, const QString &defaultValue = QString(), QString *result = 0) Q_DECL_OVERRIDE { return false; }
QExplicitlySharedDataPointer<WebContentsAdapter> adapter;
QUrl icon;
diff --git a/lib/type_conversion.h b/lib/type_conversion.h
index 2b7dee25b..6e2f55957 100644
--- a/lib/type_conversion.h
+++ b/lib/type_conversion.h
@@ -51,11 +51,16 @@
#include "ui/gfx/rect.h"
#include "url/gurl.h"
-inline QString toQt(const string16 &string)
+inline QString toQt(const base::string16 &string)
{
return QString::fromUtf16(string.data());
}
+inline base::string16 toString16(const QString &qString)
+{
+ return base::string16(qString.utf16());
+}
+
inline QUrl toQt(const GURL &url)
{
return QUrl(QString::fromStdString(url.spec()));
diff --git a/lib/web_contents_adapter_client.h b/lib/web_contents_adapter_client.h
index d85c55173..799836c6b 100644
--- a/lib/web_contents_adapter_client.h
+++ b/lib/web_contents_adapter_client.h
@@ -92,6 +92,13 @@ public:
IgnoreActionDisposition = 10,
};
+ // Must match the values in javascript_message_type.h.
+ enum JavascriptDialogType {
+ AlertDialog,
+ ConfirmDialog,
+ PromptDialog
+ };
+
virtual ~WebContentsAdapterClient() { }
virtual RenderWidgetHostViewQtDelegate* CreateRenderWidgetHostViewQtDelegate(CompositingMode mode) = 0;
@@ -105,6 +112,7 @@ public:
virtual void focusContainer() = 0;
virtual void adoptNewWindow(WebContentsAdapter *newWebContents, WindowOpenDisposition disposition) = 0;
virtual bool contextMenuRequested(const WebEngineContextMenuData&) = 0;
+ virtual bool javascriptDialog(JavascriptDialogType type, const QString &message, const QString &defaultValue = QString(), QString *result = 0) = 0;
};
#endif // WEB_CONTENTS_ADAPTER_CLIENT_H
diff --git a/lib/web_contents_delegate_qt.cpp b/lib/web_contents_delegate_qt.cpp
index 877d1cafc..c2840b608 100644
--- a/lib/web_contents_delegate_qt.cpp
+++ b/lib/web_contents_delegate_qt.cpp
@@ -113,3 +113,8 @@ void WebContentsDelegateQt::DidUpdateFaviconURL(int32 page_id, const std::vector
}
}
}
+
+content::JavaScriptDialogManager *WebContentsDelegateQt::GetJavaScriptDialogManager()
+{
+ return JavaScriptDialogManagerQt::GetInstance();
+}
diff --git a/lib/web_contents_delegate_qt.h b/lib/web_contents_delegate_qt.h
index b9720eac2..42ec5c15c 100644
--- a/lib/web_contents_delegate_qt.h
+++ b/lib/web_contents_delegate_qt.h
@@ -44,12 +44,15 @@
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
-#include "content/public/browser/web_contents.h"
+
+#include "javascript_dialog_manager_qt.h"
namespace content {
class BrowserContext;
class SiteInstance;
class RenderViewHost;
+ class JavaScriptDialogManager;
+ class WebContents;
}
class WebContentsAdapterClient;
@@ -66,6 +69,7 @@ public:
virtual void DidFailLoad(int64 frame_id, const GURL &validated_url, bool is_main_frame, int error_code, const string16 &error_description, content::RenderViewHost *render_view_host);
virtual void DidFinishLoad(int64 frame_id, const GURL &validated_url, bool is_main_frame, content::RenderViewHost *render_view_host);
virtual void DidUpdateFaviconURL(int32 page_id, const std::vector<content::FaviconURL>& candidates);
+ virtual content::JavaScriptDialogManager *GetJavaScriptDialogManager();
private:
WebContentsAdapterClient *m_viewClient;
diff --git a/lib/web_contents_view_qt.h b/lib/web_contents_view_qt.h
index d71369762..bd4f00a7e 100644
--- a/lib/web_contents_view_qt.h
+++ b/lib/web_contents_view_qt.h
@@ -58,6 +58,8 @@ class WebContentsViewQt
, public content::RenderViewHostDelegateView
{
public:
+ static inline WebContentsViewQt *from(WebContentsView *view) { return static_cast<WebContentsViewQt*>(view); }
+
WebContentsViewQt(content::WebContents* webContents)
: m_webContents(webContents)
, m_client(0)
@@ -65,6 +67,7 @@ public:
{ }
void initialize(WebContentsAdapterClient* client);
+ WebContentsAdapterClient *client() { return m_client; }
virtual content::RenderWidgetHostView *CreateViewForWidget(content::RenderWidgetHost* render_widget_host);
diff --git a/lib/widgets/Api/qwebenginepage.cpp b/lib/widgets/Api/qwebenginepage.cpp
index b956a8785..a11272a0d 100644
--- a/lib/widgets/Api/qwebenginepage.cpp
+++ b/lib/widgets/Api/qwebenginepage.cpp
@@ -34,8 +34,10 @@
#include <QApplication>
#include <QClipboard>
#include <QIcon>
+#include <QInputDialog>
#include <QLayout>
#include <QMenu>
+#include <QMessageBox>
#include <QUrl>
QT_BEGIN_NAMESPACE
@@ -306,6 +308,22 @@ bool QWebEnginePagePrivate::contextMenuRequested(const WebEngineContextMenuData
return true;
}
+bool QWebEnginePagePrivate::javascriptDialog(JavascriptDialogType type, const QString &message, const QString &defaultValue, QString *result)
+{
+ Q_Q(QWebEnginePage);
+ switch (type) {
+ case AlertDialog:
+ q->javaScriptAlert(0, message);
+ return true;
+ case ConfirmDialog:
+ return q->javaScriptConfirm(0, message);
+ case PromptDialog:
+ return q->javaScriptPrompt(0, message, defaultValue, result);
+ }
+ Q_UNREACHABLE();
+ return false;
+}
+
QMenu *QWebEnginePage::createStandardContextMenu()
{
Q_D(QWebEnginePage);
@@ -391,6 +409,26 @@ QWebEnginePage *QWebEnginePage::createWindow(WebWindowType type)
return 0;
}
+void QWebEnginePage::javaScriptAlert(QWebEngineFrame *originatingFrame, const QString &msg)
+{
+ Q_UNUSED(originatingFrame);
+ QMessageBox::information(view(), QStringLiteral("Javascript Alert - %1").arg(url().toString()), msg);
+}
+
+bool QWebEnginePage::javaScriptConfirm(QWebEngineFrame *originatingFrame, const QString &msg)
+{
+ Q_UNUSED(originatingFrame);
+ return (QMessageBox::information(view(), QStringLiteral("Javascript Confirm - %1").arg(url().toString()), msg, QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok);
+}
+
+bool QWebEnginePage::javaScriptPrompt(QWebEngineFrame *originatingFrame, const QString &msg, const QString &defaultValue, QString *result)
+{
+ Q_UNUSED(originatingFrame);
+ bool ret = false;
+ if (result)
+ *result = QInputDialog::getText(view(), QStringLiteral("Javascript Prompt - %1").arg(url().toString()), msg, QLineEdit::Normal, defaultValue, &ret);
+ return ret;
+}
QT_END_NAMESPACE
#include "moc_qwebenginepage.cpp"
diff --git a/lib/widgets/Api/qwebenginepage.h b/lib/widgets/Api/qwebenginepage.h
index 2ac0b9dc6..e426f8da6 100644
--- a/lib/widgets/Api/qwebenginepage.h
+++ b/lib/widgets/Api/qwebenginepage.h
@@ -539,9 +539,9 @@ protected:
virtual bool acceptNavigationRequest(QWebEngineFrame *frame, const QNetworkRequest &request, NavigationType type) { Q_UNUSED(frame); Q_UNUSED(request); Q_UNUSED(type); Q_UNREACHABLE(); return false; }
virtual QString chooseFile(QWebEngineFrame *originatingFrame, const QString& oldFile) { Q_UNUSED(originatingFrame); Q_UNUSED(oldFile); Q_UNREACHABLE(); return QString(); }
- virtual void javaScriptAlert(QWebEngineFrame *originatingFrame, const QString& msg) { Q_UNUSED(originatingFrame); Q_UNUSED(msg); Q_UNREACHABLE(); }
- virtual bool javaScriptConfirm(QWebEngineFrame *originatingFrame, const QString& msg) { Q_UNUSED(originatingFrame); Q_UNUSED(msg); Q_UNREACHABLE(); return false; }
- virtual bool javaScriptPrompt(QWebEngineFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result) { Q_UNUSED(originatingFrame); Q_UNUSED(msg); Q_UNUSED(defaultValue); Q_UNUSED(result); Q_UNREACHABLE(); return false; }
+ virtual void javaScriptAlert(QWebEngineFrame *originatingFrame, const QString& msg);
+ virtual bool javaScriptConfirm(QWebEngineFrame *originatingFrame, const QString& msg);
+ virtual bool javaScriptPrompt(QWebEngineFrame *originatingFrame, const QString& msg, const QString& defaultValue, QString* result);
virtual void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString& sourceID) { Q_UNUSED(message); Q_UNUSED(lineNumber); Q_UNUSED(sourceID); Q_UNREACHABLE(); }
virtual QString userAgentForUrl(const QUrl& url) const { Q_UNUSED(url); Q_UNREACHABLE(); return QString(); }
diff --git a/lib/widgets/Api/qwebenginepage_p.h b/lib/widgets/Api/qwebenginepage_p.h
index 56080c977..584b5abeb 100644
--- a/lib/widgets/Api/qwebenginepage_p.h
+++ b/lib/widgets/Api/qwebenginepage_p.h
@@ -75,6 +75,7 @@ public:
virtual void focusContainer() Q_DECL_OVERRIDE;
virtual void adoptNewWindow(WebContentsAdapter *newWebContents, WindowOpenDisposition disposition) Q_DECL_OVERRIDE;
virtual bool contextMenuRequested(const WebEngineContextMenuData &data) Q_DECL_OVERRIDE;
+ virtual bool javascriptDialog(JavascriptDialogType type, const QString &message, const QString &defaultValue = QString(), QString *result = 0) Q_DECL_OVERRIDE;
void updateAction(QWebEnginePage::WebAction) const;
void updateNavigationActions();