From f4c03d68cff2b107fe19dfe210e0207215c04f2d Mon Sep 17 00:00:00 2001 From: Adam Kallai Date: Wed, 4 Mar 2015 01:58:53 -0800 Subject: Refactor FilePickerController Move FilePickerController classes to the QtWebEngine core to providing common functionality of files selecetion for WebEngine and WebEngineWidgtes. Change-Id: I6ab407095460ef5b63b454f7d62b98215383fc21 Reviewed-by: Pierre Rossi --- src/core/core_gyp_generator.pro | 2 + src/core/file_picker_controller.cpp | 118 ++++++++++++++++++++++++++++ src/core/file_picker_controller.h | 77 ++++++++++++++++++ src/core/web_contents_adapter.cpp | 31 -------- src/core/web_contents_adapter.h | 1 - src/core/web_contents_adapter_client.h | 11 +-- src/core/web_contents_delegate_qt.cpp | 11 ++- src/webengine/api/qquickwebengineview.cpp | 5 +- src/webengine/api/qquickwebengineview_p_p.h | 2 +- src/webengine/ui_delegates_manager.cpp | 60 +++----------- src/webengine/ui_delegates_manager.h | 4 +- src/webenginewidgets/api/qwebenginepage.cpp | 28 ++++--- src/webenginewidgets/api/qwebenginepage.h | 1 - src/webenginewidgets/api/qwebenginepage_p.h | 2 +- 14 files changed, 239 insertions(+), 114 deletions(-) create mode 100644 src/core/file_picker_controller.cpp create mode 100644 src/core/file_picker_controller.h diff --git a/src/core/core_gyp_generator.pro b/src/core/core_gyp_generator.pro index b5722d2d3..6f58ce032 100644 --- a/src/core/core_gyp_generator.pro +++ b/src/core/core_gyp_generator.pro @@ -48,6 +48,7 @@ SOURCES = \ desktop_screen_qt.cpp \ dev_tools_http_handler_delegate_qt.cpp \ download_manager_delegate_qt.cpp \ + file_picker_controller.cpp \ gl_context_qt.cpp \ gl_surface_qt.cpp \ javascript_dialog_controller.cpp \ @@ -110,6 +111,7 @@ HEADERS = \ dev_tools_http_handler_delegate_qt.h \ download_manager_delegate_qt.h \ chromium_gpu_helper.h \ + file_picker_controller.h \ gl_context_qt.h \ gl_surface_qt.h \ javascript_dialog_controller_p.h \ diff --git a/src/core/file_picker_controller.cpp b/src/core/file_picker_controller.cpp new file mode 100644 index 000000000..df09c2101 --- /dev/null +++ b/src/core/file_picker_controller.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "file_picker_controller.h" +#include "type_conversion.h" +#include "content/browser/renderer_host/render_view_host_impl.h" +#include "content/browser/web_contents/web_contents_impl.h" + +#include +#include +#include +#include + +FilePickerController::FilePickerController(FileChooserMode mode, content::WebContents *contents, const QString &defaultFileName, const QStringList &acceptedMimeTypes, QObject *parent) + : QObject(parent) + , m_acceptedMimeTypes(acceptedMimeTypes) + , m_contents(contents) + , m_defaultFileName(defaultFileName) + , m_mode(mode) +{ +} + +void FilePickerController::accepted(const QStringList &files) +{ + FilePickerController::filesSelectedInChooser(files, m_contents); +} + +void FilePickerController::accepted(const QVariant &files) +{ + QStringList stringList; + Q_FOREACH (const QUrl &url, files.value >()) + stringList.append(url.toLocalFile()); + + FilePickerController::filesSelectedInChooser(stringList, m_contents); +} + +void FilePickerController::rejected() +{ + FilePickerController::filesSelectedInChooser(QStringList(), m_contents); +} + +static QStringList listRecursively(const QDir &dir) +{ + QStringList ret; + QFileInfoList infoList(dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden)); + Q_FOREACH (const QFileInfo &fileInfo, infoList) { + if (fileInfo.isDir()) { + ret.append(fileInfo.absolutePath() + QStringLiteral("/.")); // Match chromium's behavior. See chrome/browser/file_select_helper.cc + ret.append(listRecursively(QDir(fileInfo.absoluteFilePath()))); + } else + ret.append(fileInfo.absoluteFilePath()); + } + return ret; +} + +ASSERT_ENUMS_MATCH(FilePickerController::Open, content::FileChooserParams::Open) +ASSERT_ENUMS_MATCH(FilePickerController::OpenMultiple, content::FileChooserParams::OpenMultiple) +ASSERT_ENUMS_MATCH(FilePickerController::UploadFolder, content::FileChooserParams::UploadFolder) +ASSERT_ENUMS_MATCH(FilePickerController::Save, content::FileChooserParams::Save) + +void FilePickerController::filesSelectedInChooser(const QStringList &filesList, content::WebContents *contents) +{ + content::RenderViewHost *rvh = contents->GetRenderViewHost(); + Q_ASSERT(rvh); + QStringList files(filesList); + if (this->m_mode == UploadFolder && !filesList.isEmpty() + && QFileInfo(filesList.first()).isDir()) // Enumerate the directory + files = listRecursively(QDir(filesList.first())); + rvh->FilesSelectedInChooser(toVector(files), static_cast(this->m_mode)); +} + +QStringList FilePickerController::acceptedMimeTypes() +{ + return m_acceptedMimeTypes; +} + +FilePickerController::FileChooserMode FilePickerController::mode() +{ + return m_mode; +} + +QString FilePickerController::defaultFileName() +{ + return m_defaultFileName; +} diff --git a/src/core/file_picker_controller.h b/src/core/file_picker_controller.h new file mode 100644 index 000000000..045134b30 --- /dev/null +++ b/src/core/file_picker_controller.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef FILE_PICKER_CONTROLLER_H +#define FILE_PICKER_CONTROLLER_H + +namespace content { + class WebContents; +} + +#include "qtwebenginecoreglobal.h" +#include + +class QWEBENGINE_EXPORT FilePickerController : public QObject { + Q_OBJECT +public: + enum FileChooserMode { + Open, + OpenMultiple, + UploadFolder, + Save + }; + + FilePickerController(FileChooserMode mode, content::WebContents *contents, const QString &defaultFileName, const QStringList &acceptedMimeTypes, QObject * = 0); + +public Q_SLOTS: + void accepted(const QStringList &files); + void accepted(const QVariant &files); + void rejected(); + + QStringList acceptedMimeTypes(); + QString defaultFileName(); + FileChooserMode mode(); + void filesSelectedInChooser(const QStringList &filesList, content::WebContents *contents); + +private: + QString m_defaultFileName; + QStringList m_acceptedMimeTypes; + content::WebContents *m_contents; + FileChooserMode m_mode; + +}; + +#endif // FILE_PICKER_CONTROLLER_H diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 848e8086e..01b41fbe8 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -69,7 +69,6 @@ #include "content/public/common/renderer_preferences.h" #include "content/public/common/url_constants.h" #include "content/public/common/web_preferences.h" -#include "ui/shell_dialogs/selected_file_info.h" #include "third_party/WebKit/public/web/WebFindOptions.h" #include @@ -166,19 +165,6 @@ static void callbackOnEvaluateJS(WebContentsAdapterClient *adapterClient, quint6 adapterClient->didRunJavaScript(requestId, fromJSValue(result)); } -static QStringList listRecursively(const QDir& dir) { - QStringList ret; - QFileInfoList infoList(dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot |QDir::Hidden)); - Q_FOREACH (const QFileInfo &fileInfo, infoList) { - if (fileInfo.isDir()) { - ret.append(fileInfo.absolutePath() + QStringLiteral("/.")); // Match chromium's behavior. See chrome/browser/file_select_helper.cc - ret.append(listRecursively(QDir(fileInfo.absoluteFilePath()))); - } else - ret.append(fileInfo.absoluteFilePath()); - } - return ret; -} - static content::WebContents *createBlankWebContents(WebContentsAdapterClient *adapterClient, content::BrowserContext *browserContext) { content::WebContents::CreateParams create_params(browserContext, NULL); @@ -814,23 +800,6 @@ void WebContentsAdapter::dpiScaleChanged() impl->NotifyScreenInfoChanged(); } -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::Open, content::FileChooserParams::Open) -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::OpenMultiple, content::FileChooserParams::OpenMultiple) -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::UploadFolder, content::FileChooserParams::UploadFolder) -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::Save, content::FileChooserParams::Save) - -void WebContentsAdapter::filesSelectedInChooser(const QStringList &fileList, WebContentsAdapterClient::FileChooserMode mode) -{ - Q_D(WebContentsAdapter); - content::RenderViewHost *rvh = d->webContents->GetRenderViewHost(); - Q_ASSERT(rvh); - QStringList files(fileList); - if (mode == WebContentsAdapterClient::UploadFolder && !fileList.isEmpty() - && QFileInfo(fileList.first()).isDir()) // Enumerate the directory - files = listRecursively(QDir(fileList.first())); - rvh->FilesSelectedInChooser(toVector(files), static_cast(mode)); -} - content::WebContents *WebContentsAdapter::webContents() const { Q_D(const WebContentsAdapter); diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index b4b4e80a6..0f8fbf947 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -100,7 +100,6 @@ public: void serializeNavigationHistory(QDataStream &output); void setZoomFactor(qreal); qreal currentZoomFactor() const; - void filesSelectedInChooser(const QStringList &fileList, WebContentsAdapterClient::FileChooserMode); void runJavaScript(const QString &javaScript); quint64 runJavaScriptCallbackResult(const QString &javaScript); quint64 fetchDocumentMarkup(); diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h index 5a199a470..a230d7fb3 100644 --- a/src/core/web_contents_adapter_client.h +++ b/src/core/web_contents_adapter_client.h @@ -51,6 +51,7 @@ QT_FORWARD_DECLARE_CLASS(QVariant) QT_FORWARD_DECLARE_CLASS(CertificateErrorController) class BrowserContextAdapter; +class FilePickerController; class JavaScriptDialogController; class RenderWidgetHostViewQt; class RenderWidgetHostViewQtDelegate; @@ -102,14 +103,6 @@ public: InternalAuthorizationDialog = 0x10, }; - // Must match the ones in file_chooser_params.h - enum FileChooserMode { - Open, - OpenMultiple, - UploadFolder, - Save - }; - enum NavigationRequestAction { AcceptRequest, // Make room in the valid range of the enum for extra actions exposed in Experimental. @@ -163,7 +156,7 @@ public: virtual void requestFullScreen(bool) = 0; virtual bool isFullScreen() const = 0; virtual void javascriptDialog(QSharedPointer) = 0; - virtual void runFileChooser(FileChooserMode, const QString &defaultFileName, const QStringList &acceptedMimeTypes) = 0; + virtual void runFileChooser(FilePickerController *controller) = 0; virtual void didRunJavaScript(quint64 requestId, const QVariant& result) = 0; virtual void didFetchDocumentMarkup(quint64 requestId, const QString& result) = 0; virtual void didFetchDocumentInnerText(quint64 requestId, const QString& result) = 0; diff --git a/src/core/web_contents_delegate_qt.cpp b/src/core/web_contents_delegate_qt.cpp index de4487576..1d63997f0 100644 --- a/src/core/web_contents_delegate_qt.cpp +++ b/src/core/web_contents_delegate_qt.cpp @@ -41,6 +41,7 @@ #include "web_contents_delegate_qt.h" #include "browser_context_adapter.h" +#include "file_picker_controller.h" #include "media_capture_devices_dispatcher.h" #include "type_conversion.h" #include "web_contents_adapter_client.h" @@ -231,18 +232,20 @@ bool WebContentsDelegateQt::IsFullscreenForTabOrPending(const content::WebConten return m_viewClient->isFullScreen(); } -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::Open, content::FileChooserParams::Open) -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::Save, content::FileChooserParams::Save) +ASSERT_ENUMS_MATCH(FilePickerController::Open, content::FileChooserParams::Open) +ASSERT_ENUMS_MATCH(FilePickerController::OpenMultiple, content::FileChooserParams::OpenMultiple) +ASSERT_ENUMS_MATCH(FilePickerController::UploadFolder, content::FileChooserParams::UploadFolder) +ASSERT_ENUMS_MATCH(FilePickerController::Save, content::FileChooserParams::Save) void WebContentsDelegateQt::RunFileChooser(content::WebContents *web_contents, const content::FileChooserParams ¶ms) { - Q_UNUSED(web_contents) QStringList acceptedMimeTypes; acceptedMimeTypes.reserve(params.accept_types.size()); for (std::vector::const_iterator it = params.accept_types.begin(); it < params.accept_types.end(); ++it) acceptedMimeTypes.append(toQt(*it)); - m_viewClient->runFileChooser(static_cast(params.mode), toQt(params.default_file_name.value()), acceptedMimeTypes); + FilePickerController *controller = new FilePickerController(static_cast(params.mode), web_contents, toQt(params.default_file_name.value()), acceptedMimeTypes); + m_viewClient->runFileChooser(controller); } bool WebContentsDelegateQt::AddMessageToConsole(content::WebContents *source, int32 level, const base::string16 &message, int32 line_no, const base::string16 &source_id) diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp index 13eb63e07..48d741d20 100644 --- a/src/webengine/api/qquickwebengineview.cpp +++ b/src/webengine/api/qquickwebengineview.cpp @@ -39,6 +39,7 @@ #include "browser_context_adapter.h" #include "certificate_error_controller.h" +#include "file_picker_controller.h" #include "javascript_dialog_controller.h" #include "qquickwebenginehistory_p.h" #include "qquickwebenginecertificateerror_p.h" @@ -240,9 +241,9 @@ void QQuickWebEngineViewPrivate::runGeolocationPermissionRequest(const QUrl &url Q_EMIT e->featurePermissionRequested(url, QQuickWebEngineViewExperimental::Geolocation); } -void QQuickWebEngineViewPrivate::runFileChooser(FileChooserMode mode, const QString &defaultFileName, const QStringList &acceptedMimeTypes) +void QQuickWebEngineViewPrivate::runFileChooser(FilePickerController* controller) { - ui()->showFilePicker(mode, defaultFileName, acceptedMimeTypes, adapter); + ui()->showFilePicker(controller); } void QQuickWebEngineViewPrivate::passOnFocus(bool reverse) diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index faf567d3a..98a7a9c5f 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -156,7 +156,7 @@ public: virtual bool contextMenuRequested(const WebEngineContextMenuData &) Q_DECL_OVERRIDE; virtual void navigationRequested(int navigationType, const QUrl &url, int &navigationRequestAction, bool isMainFrame) Q_DECL_OVERRIDE; virtual void javascriptDialog(QSharedPointer) Q_DECL_OVERRIDE; - virtual void runFileChooser(FileChooserMode, const QString &defaultFileName, const QStringList &acceptedMimeTypes) Q_DECL_OVERRIDE; + virtual void runFileChooser(FilePickerController *controller) Q_DECL_OVERRIDE; virtual void didRunJavaScript(quint64, const QVariant&) Q_DECL_OVERRIDE; virtual void didFetchDocumentMarkup(quint64, const QString&) Q_DECL_OVERRIDE { } virtual void didFetchDocumentInnerText(quint64, const QString&) Q_DECL_OVERRIDE { } diff --git a/src/webengine/ui_delegates_manager.cpp b/src/webengine/ui_delegates_manager.cpp index 23816caff..ed51bd8f2 100644 --- a/src/webengine/ui_delegates_manager.cpp +++ b/src/webengine/ui_delegates_manager.cpp @@ -37,6 +37,7 @@ #include "ui_delegates_manager.h" #include "api/qquickwebengineview_p.h" +#include "file_picker_controller.h" #include "javascript_dialog_controller.h" #include @@ -338,50 +339,8 @@ void UIDelegatesManager::showDialog(QSharedPointer d QMetaObject::invokeMethod(dialog, "open"); } -namespace { -class FilePickerController : public QObject { - Q_OBJECT -public: - FilePickerController(WebContentsAdapterClient::FileChooserMode, const QExplicitlySharedDataPointer &, QObject * = 0); - -public Q_SLOTS: - void accepted(const QVariant &files); - void rejected(); - -private: - QExplicitlySharedDataPointer m_adapter; - WebContentsAdapterClient::FileChooserMode m_mode; - -}; - - -FilePickerController::FilePickerController(WebContentsAdapterClient::FileChooserMode mode, const QExplicitlySharedDataPointer &adapter, QObject *parent) - : QObject(parent) - , m_adapter(adapter) - , m_mode(mode) -{ -} - -void FilePickerController::accepted(const QVariant &files) +void UIDelegatesManager::showFilePicker(FilePickerController *controller) { - QStringList stringList; - Q_FOREACH (const QUrl &url, files.value >()) - stringList.append(url.toLocalFile()); - m_adapter->filesSelectedInChooser(stringList, m_mode); -} - -void FilePickerController::rejected() -{ - m_adapter->filesSelectedInChooser(QStringList(), m_mode); -} - -} // namespace - - -void UIDelegatesManager::showFilePicker(WebContentsAdapterClient::FileChooserMode mode, const QString &defaultFileName, const QStringList &acceptedMimeTypes, const QExplicitlySharedDataPointer &adapter) -{ - Q_UNUSED(defaultFileName); - Q_UNUSED(acceptedMimeTypes); if (!ensureComponentLoaded(FilePicker)) return; @@ -394,23 +353,24 @@ void UIDelegatesManager::showFilePicker(WebContentsAdapterClient::FileChooserMod filePickerComponent->completeCreate(); // Fine-tune some properties depending on the mode. - switch (mode) { - case WebContentsAdapterClient::Open: + switch (controller->mode()) { + case FilePickerController::Open: break; - case WebContentsAdapterClient::Save: + case FilePickerController::Save: filePicker->setProperty("selectExisting", false); break; - case WebContentsAdapterClient::OpenMultiple: + case FilePickerController::OpenMultiple: filePicker->setProperty("selectMultiple", true); break; - case WebContentsAdapterClient::UploadFolder: + case FilePickerController::UploadFolder: filePicker->setProperty("selectFolder", true); break; default: Q_UNREACHABLE(); } - FilePickerController *controller = new FilePickerController(mode, adapter, filePicker); + controller->setParent(filePicker); + QQmlProperty filesPickedSignal(filePicker, QStringLiteral("onFilesSelected")); CHECK_QML_SIGNAL_PROPERTY(filesPickedSignal, filePickerComponent->url()); QQmlProperty rejectSignal(filePicker, QStringLiteral("onRejected")); @@ -427,5 +387,3 @@ void UIDelegatesManager::showFilePicker(WebContentsAdapterClient::FileChooserMod QMetaObject::invokeMethod(filePicker, "open"); } - -#include "ui_delegates_manager.moc" diff --git a/src/webengine/ui_delegates_manager.h b/src/webengine/ui_delegates_manager.h index e0eeb4679..797aaec74 100644 --- a/src/webengine/ui_delegates_manager.h +++ b/src/webengine/ui_delegates_manager.h @@ -64,6 +64,7 @@ QQmlComponent *COMPONENT##Component class JavaScriptDialogController; +class FilePickerController; QT_BEGIN_NAMESPACE class QObject; class QQmlContext; @@ -120,8 +121,7 @@ public: QObject *addMenu(QObject *parentMenu, const QString &title, const QPoint &pos = QPoint()); QQmlContext *creationContextForComponent(QQmlComponent *); void showDialog(QSharedPointer); - void showFilePicker(WebContentsAdapterClient::FileChooserMode, const QString &defaultFileName, const QStringList &acceptedMimeTypes - , const QExplicitlySharedDataPointer &); + void showFilePicker(FilePickerController *controller); private: bool ensureComponentLoaded(ComponentType); diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index bdce04978..72b651cf1 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -25,6 +25,7 @@ #include "browser_context_adapter.h" #include "certificate_error_controller.h" +#include "file_picker_controller.h" #include "javascript_dialog_controller.h" #include "qwebenginehistory.h" #include "qwebenginehistory_p.h" @@ -874,17 +875,22 @@ void QWebEnginePage::setFeaturePermission(const QUrl &securityOrigin, QWebEngine } } -static inline QWebEnginePage::FileSelectionMode toPublic(WebContentsAdapterClient::FileChooserMode mode) +static inline QWebEnginePage::FileSelectionMode toPublic(FilePickerController::FileChooserMode mode) { // Should the underlying values change, we'll need a switch here. return static_cast(mode); } -void QWebEnginePagePrivate::runFileChooser(WebContentsAdapterClient::FileChooserMode mode, const QString &defaultFileName, const QStringList &acceptedMimeTypes) +void QWebEnginePagePrivate::runFileChooser(FilePickerController *controller) { Q_Q(QWebEnginePage); - QStringList selectedFileNames = q->chooseFiles(toPublic(mode), (QStringList() << defaultFileName), acceptedMimeTypes); - adapter->filesSelectedInChooser(selectedFileNames, mode); + + QStringList selectedFileNames = q->chooseFiles(toPublic(controller->mode()), (QStringList() << controller->defaultFileName()), controller->acceptedMimeTypes()); + + if (!selectedFileNames.empty()) + controller->accepted(selectedFileNames); + else + controller->rejected(); } WebEngineSettings *QWebEnginePagePrivate::webEngineSettings() const @@ -1001,8 +1007,8 @@ QWebEnginePage *QWebEnginePage::createWindow(WebWindowType type) return 0; } -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::Open, QWebEnginePage::FileSelectOpen) -ASSERT_ENUMS_MATCH(WebContentsAdapterClient::OpenMultiple, QWebEnginePage::FileSelectOpenMultiple) +ASSERT_ENUMS_MATCH(FilePickerController::Open, QWebEnginePage::FileSelectOpen) +ASSERT_ENUMS_MATCH(FilePickerController::OpenMultiple, QWebEnginePage::FileSelectOpenMultiple) QStringList QWebEnginePage::chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes) { @@ -1010,19 +1016,19 @@ QStringList QWebEnginePage::chooseFiles(FileSelectionMode mode, const QStringLis // can work with) and mimetypes ranging from text/plain or images/* to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet Q_UNUSED(acceptedMimeTypes); QStringList ret; - switch (static_cast(mode)) { - case WebContentsAdapterClient::OpenMultiple: + switch (static_cast(mode)) { + case FilePickerController::OpenMultiple: ret = QFileDialog::getOpenFileNames(view(), QString()); break; // Chromium extension, not exposed as part of the public API for now. - case WebContentsAdapterClient::UploadFolder: + case FilePickerController::UploadFolder: ret << QFileDialog::getExistingDirectory(view(), tr("Select folder to upload")) + QLatin1Char('/'); break; - case WebContentsAdapterClient::Save: + case FilePickerController::Save: ret << QFileDialog::getSaveFileName(view(), QString(), (QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + oldFiles.first())); break; default: - case WebContentsAdapterClient::Open: + case FilePickerController::Open: ret << QFileDialog::getOpenFileName(view(), QString(), oldFiles.first()); break; } diff --git a/src/webenginewidgets/api/qwebenginepage.h b/src/webenginewidgets/api/qwebenginepage.h index f2067c6aa..63892d197 100644 --- a/src/webenginewidgets/api/qwebenginepage.h +++ b/src/webenginewidgets/api/qwebenginepage.h @@ -261,7 +261,6 @@ Q_SIGNALS: protected: virtual QWebEnginePage *createWindow(WebWindowType type); - virtual QStringList chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes); virtual void javaScriptAlert(const QUrl &securityOrigin, const QString& msg); virtual bool javaScriptConfirm(const QUrl &securityOrigin, const QString& msg); diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index dc07ce2c6..74e17fb39 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -129,7 +129,7 @@ public: virtual void requestFullScreen(bool) Q_DECL_OVERRIDE { } virtual bool isFullScreen() const Q_DECL_OVERRIDE { return false; } virtual void javascriptDialog(QSharedPointer) Q_DECL_OVERRIDE; - virtual void runFileChooser(FileChooserMode, const QString &defaultFileName, const QStringList &acceptedMimeTypes) Q_DECL_OVERRIDE; + virtual void runFileChooser(FilePickerController *controller) Q_DECL_OVERRIDE; virtual void didRunJavaScript(quint64 requestId, const QVariant& result) Q_DECL_OVERRIDE; virtual void didFetchDocumentMarkup(quint64 requestId, const QString& result) Q_DECL_OVERRIDE; virtual void didFetchDocumentInnerText(quint64 requestId, const QString& result) Q_DECL_OVERRIDE; -- cgit v1.2.3