diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/webenginewidgets/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/webenginewidgets/webenginewidgets.pro | 3 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/CMakeLists.txt | 49 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/about.html | 129 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/doc/images/webui-example.png | bin | 28862 -> 0 bytes | |||
-rw-r--r-- | examples/webenginewidgets/webui/doc/src/webui.qdoc | 141 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/main.cpp | 33 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/webui.pro | 16 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/webui.qrc | 5 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/webuihandler.cpp | 51 | ||||
-rw-r--r-- | examples/webenginewidgets/webui/webuihandler.h | 23 |
11 files changed, 1 insertions, 450 deletions
diff --git a/examples/webenginewidgets/CMakeLists.txt b/examples/webenginewidgets/CMakeLists.txt index 0b5757e35..c15089420 100644 --- a/examples/webenginewidgets/CMakeLists.txt +++ b/examples/webenginewidgets/CMakeLists.txt @@ -8,7 +8,6 @@ qt_internal_add_example(simplebrowser) qt_internal_add_example(stylesheetbrowser) qt_internal_add_example(push-notifications) qt_internal_add_example(videoplayer) -qt_internal_add_example(webui) if(QT_FEATURE_webengine_geolocation) qt_internal_add_example(maps) endif() diff --git a/examples/webenginewidgets/webenginewidgets.pro b/examples/webenginewidgets/webenginewidgets.pro index 119f6e1b1..aedd2cc62 100644 --- a/examples/webenginewidgets/webenginewidgets.pro +++ b/examples/webenginewidgets/webenginewidgets.pro @@ -9,8 +9,7 @@ SUBDIRS += \ simplebrowser \ stylesheetbrowser \ push-notifications \ - videoplayer \ - webui + videoplayer qtConfig(webengine-geolocation): SUBDIRS += maps qtConfig(webengine-webchannel): SUBDIRS += markdowneditor diff --git a/examples/webenginewidgets/webui/CMakeLists.txt b/examples/webenginewidgets/webui/CMakeLists.txt deleted file mode 100644 index 8f6e95045..000000000 --- a/examples/webenginewidgets/webui/CMakeLists.txt +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(webui LANGUAGES CXX) - -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/webenginewidgets/webui") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui WebEngineWidgets) - -qt_add_executable(webui - main.cpp - webuihandler.cpp webuihandler.h -) - -set_target_properties(webui PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(webui PUBLIC - Qt::Core - Qt::Gui - Qt::WebEngineWidgets -) - -# Resources: -set(webui_resource_files - "about.html" -) - -qt_add_resources(webui "webui" - PREFIX - "/" - FILES - ${webui_resource_files} -) - -install(TARGETS webui - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/webenginewidgets/webui/about.html b/examples/webenginewidgets/webui/about.html deleted file mode 100644 index 7b5a58969..000000000 --- a/examples/webenginewidgets/webui/about.html +++ /dev/null @@ -1,129 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>Qt WebEngine WebUI Example</title> - <style> - html { - background: #f0f0f0; - color: #303030; - font: 16px system-ui; - height: 100%; - } - - body { - margin: 0; - padding: 0; - height: 100%; - display: flex; - flex-direction: column; - align-items: stretch; - } - - body > * { - padding-left: 20px; - padding-right: 20px; - } - - header { - flex: none; - display: flex; - align-items: center; - background: #f0fff0; - border-bottom: 1px solid #e0e0e0; - padding-top: 20px; - padding-bottom: 20px; - } - - header > h1 { - font: bold 20px system-ui; - margin-left: 18px; - } - - main { - flex: auto; - } - - footer { - flex: none; - display: flex; - justify-content: center; - padding-bottom: 20px; - } - - button { - background: #41cd52; - color: #f0f0f0; - font: 16px system-ui; - border: 0; - box-shadow: 0px 1px 3px rgb(0,0,0,0.5); - cursor: pointer; - margin: 0 0 1px; - padding: 10px 24px; - } - - button:hover { - background: #50dc61; - } - - button:active { - background: #50dc61; - box-shadow: 0px 1px 2px rgb(0,0,0,0.5); - margin: 1px 0 0; - } - - button:focus { - outline: 0; - } - - </style> - </head> - <body> - <header> - <img width="48px" height="48px" - src="qrc:/qt-project.org/qmessagebox/images/qtlogo-64.png"> - <h1>WebEngine Widgets<br>WebUI Example</h1> - </header> - <main> - <p> - Aside from the built-in schemes, such as <code>http</code> and - <code>qrc</code>, Qt WebEngine may be extended with <em>custom - schemes</em> by creating <em>custom scheme handlers</em>. - </p> - - <p> - This is a simple HTML page loaded from a custom scheme and - displayed by a <code>QWebEngineView</code>. Even the Quit button - below is a standard HTML <code><button></code> element. - </p> - - <p> - Read the documentation to find out - </p> - <ul> - <li> - <p> - How to create a custom scheme handler which serves HTML - and handles HTML form submissions. - </p> - </li> - <li> - <p> - How to prevent ordinary web content from accessing the - custom scheme. - </p> - </li> - <li> - <p> - How to prevent any other scheme from submitting HTML - form data. - </p> - </li> - </ul> - </main> - <footer> - <form action="" method="post"> - <button name="quit">Quit</button> - </form> - </footer> - </body> -</html> diff --git a/examples/webenginewidgets/webui/doc/images/webui-example.png b/examples/webenginewidgets/webui/doc/images/webui-example.png Binary files differdeleted file mode 100644 index 84e2c7fc3..000000000 --- a/examples/webenginewidgets/webui/doc/images/webui-example.png +++ /dev/null diff --git a/examples/webenginewidgets/webui/doc/src/webui.qdoc b/examples/webenginewidgets/webui/doc/src/webui.qdoc deleted file mode 100644 index c8ab43ea8..000000000 --- a/examples/webenginewidgets/webui/doc/src/webui.qdoc +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example webenginewidgets/webui - \title WebEngine Widgets WebUI Example - \ingroup webengine-widgetexamples - \brief Displays HTML over a custom scheme. - - \image webui-example.png - - \e {WebUI} demonstrates how to implement a custom scheme in a secure way. - - Aside from the built-in URL schemes, such as \c {http} and \c {qrc}, - \QWE may be extended with \e {custom schemes} by creating \e {custom - scheme handlers}. This example shows: - - \list - \li How to create a custom scheme handler which serves HTML and handles - HTML form submissions. - \li How to prevent ordinary web content from accessing the custom scheme. - \li How to prevent any other scheme from submitting HTML form data. - \endlist - - \include examples-run.qdocinc - - \section1 Overview - - The example program consists of a single \l {QWebEngineView} showing a - simple HTML page loaded from the URL \c {webui:about}, over our custom - scheme. Pressing the button at the bottom of the page will trigger an HTML - form submission via POST to the same URL, at which point our custom scheme - handler will cause the application to exit. - - The program is divided into two parts, the \c {main} function for setting - everything up, and the \c {WebUiHandler} class for implementing our custom - scheme handler. The \c {main} function is quite short: - - \quotefromfile webenginewidgets/webui/main.cpp - \skipto int main - \printuntil /^\}/ - - Aside from the relatively standard setup of widgets, two points are - noteworthy. First, we call the static method \c - {WebUiHandler::registerUrlScheme()} to register our custom scheme with the - web engine. Second, we create and install our custom scheme handler \c - {WebUiHandler} using \l - {QWebEngineProfile::installUrlSchemeHandler()}{installUrlSchemeHandler()}. - The following sections describe these aspects in more detail. - - \section1 Registering the Scheme - - As custom schemes are integrated directly into the web engine, they do not - necessarily need to follow the standard security rules which apply to - ordinary web content. Depending on the chosen configuration, content served - over a custom scheme may be given access to local resources, be set to - ignore Content-Security-Policy rules, or conversely, be denied access to any - other content entirely. - - In order to take advantage of these possibilities, the custom scheme must - first be registered. This means creating and configuring a \l - {QWebEngineUrlScheme} object and then handing it over to \l - {QWebEngineUrlScheme::registerScheme()}. The example program does exactly this in - the static method \c {WebUiHandler::registerUrlScheme()}: - - \quotefromfile webenginewidgets/webui/webuihandler.cpp - \skipto void WebUiHandler::registerUrlScheme - \printuntil /^\}/ - - A custom scheme needs a name, which can be set by passing it to - the constructor of \c {QWebEngineUrlScheme} or by calling \l - {QWebEngineUrlScheme::setName}. In the above, the name \c {webui} is set - through the constructor. Additionally, we activate the flags \l - {QWebEngineUrlScheme::SecureScheme}{SecureScheme}, \l - {QWebEngineUrlScheme::LocalScheme}{LocalScheme} and \l - {QWebEngineUrlScheme::LocalAccessAllowed}{LocalAccessAllowed}. Since our - custom scheme handler will not deliver resources received from insecure - network connections, we can safely mark it as a \c {SecureScheme}. The \c {LocalScheme} - flag prevents content from non-local schemes (such as \c {http}) from - interacting with our custom scheme. Without this flag it would be possible, - for example, to embed the \c {webui:about} page in an \c <iframe> element on - a remotely loaded HTML page, perhaps to attempt a phishing attack. We also - need the \c {LocalAccessAllowed} flag without which we would not be able to - access the \c {webui} scheme from our \c {webui:about} page. - - Earlier we saw that the call to \c {WebUiHandler::registerUrlScheme()} is - made already at the top of the \c {main} function. This is so because custom - schemes need to be registered as early as possible so that that they can be - passed to all subprocesses. Specifically, custom schemes need to be registered - before any other \QWE classes are instantiated by the application. - - \section1 Handling Requests - - A custom scheme handler is, broadly speaking, similar to a web application - served over HTTP. However, because custom schemes are integrated directly - into the web engine, they have the advantage in terms of efficiency: there's - no need for generating and parsing HTTP messages or for transferring data - over sockets. - - Implementing a handler means creating a subclass of \l - {QWebEngineUrlSchemeHandler}, which is just what is done by the \c - {WebUiHandler} class of the example program: - - \quotefromfile webenginewidgets/webui/webuihandler.h - \skipto class WebUiHandler - \printuntil /^\}/ - - For each request to a \c {webui} URL, the \c - {WebUiHandler::requestStarted()} method will be called: - - \quotefromfile webenginewidgets/webui/webuihandler.cpp - \skipto void WebUiHandler::requestStarted - \printuntil /^\}/ - - The \l {QWebEngineUrlRequestJob} object \c {job} contains the request's - attributes and provides methods for replying to the request with a response. - Responses are generated asynchronously by reading them from the \l - {QIODevice} that the application passes to \l - {QWebEngineUrlRequestJob::reply()}{reply()}. - - \warning The \c requestStarted() method is not called from the main thread, - but from the web engine's IO thread. Care must be taken to synchronize - access to any resources on the main thread. - - Aside from the usual fare of \l - {QWebEngineUrlRequestJob::requestMethod()}{requestMethod} and \l - {QWebEngineUrlRequestJob::requestUrl()}{requestUrl}, there is also the \l - {QWebEngineUrlRequestJob::initiator()}{initiator}, holding the origin of the - content which initiated the request. An empty \c initiator means the request - was initiated directly by the application (via \l - {QWebEnginePage::setUrl()}, for example). The special value \c "null" - corresponds to an opaque origin (a sandboxed \c {<iframe>} element, for - example). Otherwise, the \c initiator will contain the URL scheme, hostname, - and port of the content which initiated the request. - - In this example, the \c initiator is used to ensure that \c {POST} requests - to \c {webui:about} will only trigger the application's exit if they - originate from the \c {webui} scheme. This prevents content loaded over - other schemes from triggering the application's exit. - -*/ diff --git a/examples/webenginewidgets/webui/main.cpp b/examples/webenginewidgets/webui/main.cpp deleted file mode 100644 index cbbb9ad25..000000000 --- a/examples/webenginewidgets/webui/main.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "webuihandler.h" - -#include <QApplication> -#include <QWebEnginePage> -#include <QWebEngineProfile> -#include <QWebEngineView> - -int main(int argc, char *argv[]) -{ - QCoreApplication::setOrganizationName("QtExamples"); - - WebUiHandler::registerUrlScheme(); - - QApplication app(argc, argv); - - QWebEngineProfile profile; - - WebUiHandler handler; - profile.installUrlSchemeHandler(WebUiHandler::schemeName, &handler); - - QWebEnginePage page(&profile); - QWebEngineView view; - view.setPage(&page); - page.load(WebUiHandler::aboutUrl); - view.setContextMenuPolicy(Qt::NoContextMenu); - view.resize(500, 600); - view.show(); - - return app.exec(); -} diff --git a/examples/webenginewidgets/webui/webui.pro b/examples/webenginewidgets/webui/webui.pro deleted file mode 100644 index 714833587..000000000 --- a/examples/webenginewidgets/webui/webui.pro +++ /dev/null @@ -1,16 +0,0 @@ -TEMPLATE = app - -QT += webenginewidgets - -HEADERS += \ - webuihandler.h - -SOURCES += \ - main.cpp \ - webuihandler.cpp - -RESOURCES += \ - webui.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/webenginewidgets/webui -INSTALLS += target diff --git a/examples/webenginewidgets/webui/webui.qrc b/examples/webenginewidgets/webui/webui.qrc deleted file mode 100644 index 6ddf01fa2..000000000 --- a/examples/webenginewidgets/webui/webui.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>about.html</file> - </qresource> -</RCC> diff --git a/examples/webenginewidgets/webui/webuihandler.cpp b/examples/webenginewidgets/webui/webuihandler.cpp deleted file mode 100644 index 932d622e5..000000000 --- a/examples/webenginewidgets/webui/webuihandler.cpp +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "webuihandler.h" - -#include <QApplication> -#include <QFile> -#include <QWebEngineUrlRequestJob> -#include <QWebEngineUrlScheme> - -#define SCHEMENAME "webui" - -const QByteArray WebUiHandler::schemeName = QByteArrayLiteral(SCHEMENAME); -const QUrl WebUiHandler::aboutUrl = QUrl(QStringLiteral(SCHEMENAME ":about")); - -WebUiHandler::WebUiHandler(QObject *parent) - : QWebEngineUrlSchemeHandler(parent) -{ -} - -void WebUiHandler::requestStarted(QWebEngineUrlRequestJob *job) -{ - static const QUrl webUiOrigin(QStringLiteral(SCHEMENAME ":")); - static const QByteArray GET(QByteArrayLiteral("GET")); - static const QByteArray POST(QByteArrayLiteral("POST")); - - QByteArray method = job->requestMethod(); - QUrl url = job->requestUrl(); - QUrl initiator = job->initiator(); - - if (method == GET && url == aboutUrl) { - QFile *file = new QFile(QStringLiteral(":/about.html"), job); - file->open(QIODevice::ReadOnly); - job->reply(QByteArrayLiteral("text/html"), file); - } else if (method == POST && url == aboutUrl && initiator == webUiOrigin) { - job->fail(QWebEngineUrlRequestJob::RequestAborted); - QApplication::exit(); - } else { - job->fail(QWebEngineUrlRequestJob::UrlNotFound); - } -} - -// static -void WebUiHandler::registerUrlScheme() -{ - QWebEngineUrlScheme webUiScheme(schemeName); - webUiScheme.setFlags(QWebEngineUrlScheme::SecureScheme | - QWebEngineUrlScheme::LocalScheme | - QWebEngineUrlScheme::LocalAccessAllowed); - QWebEngineUrlScheme::registerScheme(webUiScheme); -} diff --git a/examples/webenginewidgets/webui/webuihandler.h b/examples/webenginewidgets/webui/webuihandler.h deleted file mode 100644 index 072c0809c..000000000 --- a/examples/webenginewidgets/webui/webuihandler.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef WEBUIHANDLER_H -#define WEBUIHANDLER_H - -#include <QWebEngineUrlSchemeHandler> - -class WebUiHandler : public QWebEngineUrlSchemeHandler -{ - Q_OBJECT -public: - explicit WebUiHandler(QObject *parent = nullptr); - - void requestStarted(QWebEngineUrlRequestJob *job) override; - - static void registerUrlScheme(); - - const static QByteArray schemeName; - const static QUrl aboutUrl; -}; - -#endif // !WEBUIHANDLER_H |