File indexing completed on 2024-05-19 05:01:22

0001 /*
0002     This file is part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2021 Stefano Crocco <stefano.crocco@alice.it>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #ifndef WEBENGINEPARTCONTROLS_H
0010 #define WEBENGINEPARTCONTROLS_H
0011 
0012 #include <QObject>
0013 #include <QWebEngineCertificateError>
0014 
0015 #include <kwebenginepartlib_export.h>
0016 
0017 #ifdef MANAGE_COOKIES_INTERNALLY
0018 #include "cookies/webenginepartcookiejar6.h"
0019 typedef WebEnginePartCookieJar6 WebEnginePartCookieJar;
0020 #else
0021 #include "cookies/webenginepartcookiejar_kio.h"
0022 typedef WebEnginePartCookieJarKIO WebEnginePartCookieJar;
0023 #endif
0024 
0025 class QWebEngineProfile;
0026 class QWebEngineScript;
0027 
0028 class SpellCheckerManager;
0029 class WebEnginePartDownloadManager;
0030 class WebEnginePage;
0031 class NavigationRecorder;
0032 
0033 namespace KonqWebEnginePart {
0034     class CertificateErrorDialogManager;
0035 #if QT_VERSION_MAJOR == 6
0036     class Profile;
0037 #else
0038     typedef QWebEngineProfile Profile;
0039 #endif
0040 }
0041 
0042 class KWEBENGINEPARTLIB_EXPORT WebEnginePartControls : public QObject
0043 {
0044     Q_OBJECT
0045 
0046 public:
0047 
0048     static WebEnginePartControls *self();
0049 
0050     ~WebEnginePartControls();
0051 
0052     bool isReady() const;
0053 
0054     void setup(KonqWebEnginePart::Profile *profile);
0055 
0056     SpellCheckerManager* spellCheckerManager() const;
0057 
0058     WebEnginePartDownloadManager* downloadManager() const;
0059 
0060     NavigationRecorder* navigationRecorder() const;
0061 
0062     //TODO KF6: change the return value to void
0063     bool handleCertificateError(const QWebEngineCertificateError &ce, WebEnginePage *page);
0064 
0065     QString httpUserAgent() const;
0066 
0067     QString defaultHttpUserAgent() const;
0068 
0069 private slots:
0070     void reparseConfiguration();
0071     void setHttpUserAgent(const QString &uaString);
0072 
0073 signals:
0074     void userAgentChanged(const QString &uaString);
0075 
0076     /**
0077      * @brief Informs pages that the user-defined stylesheet has changed
0078      * @param script the source of the javascript script to run to update the stylesheet
0079      */
0080     void updateStyleSheet(const QString &script);
0081 
0082 private:
0083 
0084     WebEnginePartControls();
0085 
0086     /**
0087      * @brief Constructs an `Accept-Language` http header from the language settings
0088      *
0089      * The accepted languages are determined according to the following sources:
0090      * - the language settings in the application itself (using the `Application Language...` menu entry)
0091      * - the language settings in `SystemSettings`
0092      * - the locale returned by `QLocale::system()`
0093      *
0094      * These sources are tried in order: the first to return a valid value is used. If all return an invalid
0095      * value (which shouldn't happen), an empty string is returned.
0096      * @return a suitable `AcceptLanguage` header according to the user preferences
0097      */
0098     QString determineHttpAcceptLanguageHeader() const;
0099 
0100     /**
0101      * @brief Inserts in the profile script collection the scripts described in the `:/scripts.json` file
0102      *
0103      * The `:/scripts.json` file should contain information about all scripts needed to be injected in each
0104      * HTML document (for example, the one used by WebEngineWallet to detect forms).
0105      *
0106      * The `:scripts.json` file should have the following structure:
0107      * @code{.json}
0108      * {
0109      *      "first script name": {
0110      *          "file": "path to first script file",
0111      *          "injectionPoint": injectionPointAsInt,
0112      *          "worldId": worldIdAsInt,
0113      *          "runsOnSubFrames": true
0114      *      },
0115      *      "second script name": {
0116      *         ...
0117      *      },
0118      *      ...
0119      * }
0120      * @endcode
0121      * Each script is represented by the one of the inner objects. The corresponding key can be anything and
0122      * represents a name by which the script can be recognized. It's passed to `QWebEngineScript::setName()`.
0123      * The keys of the object representing a script have the following meaning:
0124      * - `"file"`: it's the path of the file containing the actual source code of the script. It usually refers
0125      * to a `qrc` resource
0126      * - `"injectionPoint"` (optional): it's the script injection point, as described by `QWebEngineScript::InjectionPoint`.
0127      *  it should be one of the values of the enum. If not given, the default injection point is used. If an invalid value
0128      *  is given, the behavior is undefined
0129      * - `"worldId"` (optional): it's the world id where the script should be registered. It's passed to `QWebEngineScript::setWorldId()`
0130      *  If not given, the default application world is used. If a negative value is given, the behavior is undefined
0131      * - `"runsOnSubFrames" (optional): if given, it's passed to `QWebEngineScript::setRunsOnSubFrames`
0132      */
0133     void registerScripts();
0134 
0135     /**
0136      * @brief Creates a QWebEngineScript from it's JSON description and its name
0137      * @param name the name to give to the script (will be passed to QWebEngineScript::setName()
0138      * @param obj the JSON object describing the script
0139      * @return the script object. If script creation fails for any reason, the name of the script will be empty
0140      * @see registerScripts() for the description of the fields in @p object
0141      */
0142     static QWebEngineScript scriptFromJson(const QString &name, const QJsonObject &obj);
0143 
0144     /**
0145      * @brief Applies the user stylesheet according to the user settings
0146      *
0147      * Since QtWebEngine doesn't provide special support for using custom stylesheets, custom stylesheets are
0148      * applied using a script. This script is inserted in the profile script list so that it's automatically
0149      * applied to new pages. To update existing pages, the updateStyleSheet() signal is emitted with the code
0150      * of the script.
0151      *
0152      * If the user chose to use the default stylesheet while previously a custom one was in use, the script
0153      * will delete the old stylesheet.
0154      */
0155     void updateUserStyleSheetScript();
0156 
0157     KonqWebEnginePart::Profile *m_profile;
0158     WebEnginePartCookieJar *m_cookieJar;
0159     SpellCheckerManager *m_spellCheckerManager;
0160     WebEnginePartDownloadManager *m_downloadManager;
0161     KonqWebEnginePart::CertificateErrorDialogManager *m_certificateErrorDialogManager;
0162     NavigationRecorder *m_navigationRecorder;
0163     QString m_defaultUserAgent;
0164     static constexpr const char* s_userStyleSheetScriptName{"apply konqueror user stylesheet"};
0165 };
0166 
0167 #endif // WEBENGINEPARTCONTROLS_H