/**************************************************************************** ** ** Copyright (C) 2018 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:FDL$ ** 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 The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free ** Documentation License version 1.3 as published by the Free Software ** Foundation and appearing in the file included in the packaging of ** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements ** will be met: https://www.gnu.org/licenses/fdl-1.3.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ /*! \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