File indexing completed on 2024-05-12 04:58:30

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef WEBPAGE_H
0019 #define WEBPAGE_H
0020 
0021 #include <QWebEnginePage>
0022 #include <QWebEngineScript>
0023 #include <QWebEngineFullScreenRequest>
0024 #include <QPrinter>
0025 #include <QVector>
0026 #include <QElapsedTimer>
0027 
0028 #include "qzcommon.h"
0029 
0030 class QEventLoop;
0031 class QWebEngineDownloadItem;
0032 class QWebEngineRegisterProtocolHandlerRequest;
0033 
0034 class WebView;
0035 class WebHitTestResult;
0036 class DelayedFileWatcher;
0037 
0038 class FALKON_EXPORT WebPage : public QWebEnginePage
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     enum JsWorld {
0044         UnsafeJsWorld = QWebEngineScript::MainWorld,
0045         SafeJsWorld = QWebEngineScript::ApplicationWorld
0046     };
0047 
0048     explicit WebPage(QObject* parent = nullptr);
0049     ~WebPage() override;
0050 
0051     WebView *view() const;
0052 
0053     bool execPrintPage(QPrinter *printer, int timeout = 1000);
0054     QVariant execJavaScript(const QString &scriptSource, quint32 worldId = UnsafeJsWorld, int timeout = 500);
0055 
0056     QPointF mapToViewport(const QPointF &pos) const;
0057     WebHitTestResult hitTestContent(const QPoint &pos) const;
0058 
0059     void scroll(int x, int y);
0060     void setScrollPosition(const QPointF &pos);
0061 
0062     bool javaScriptPrompt(const QUrl &securityOrigin, const QString &msg, const QString &defaultValue, QString* result) override;
0063     bool javaScriptConfirm(const QUrl &securityOrigin, const QString &msg) override;
0064     void javaScriptAlert(const QUrl &securityOrigin, const QString &msg) override;
0065     void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID) override;
0066 
0067     QStringList autoFillUsernames() const;
0068 
0069     QUrl registerProtocolHandlerRequestUrl() const;
0070     QString registerProtocolHandlerRequestScheme() const;
0071 
0072     bool isRunningLoop();
0073 
0074     bool isLoading() const;
0075 
0076     static QStringList internalSchemes();
0077     static QStringList supportedSchemes();
0078     static void addSupportedScheme(const QString &scheme);
0079     static void removeSupportedScheme(const QString &scheme);
0080 
0081 Q_SIGNALS:
0082     void privacyChanged(bool status);
0083     void printRequested();
0084     void navigationRequestAccepted(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame);
0085 
0086 protected Q_SLOTS:
0087     void progress(int prog);
0088     void finished();
0089 
0090 private Q_SLOTS:
0091     void urlChanged(const QUrl &url);
0092     void watchedFileChanged(const QString &file);
0093     void windowCloseRequested();
0094     void fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest);
0095     void featurePermissionRequested(const QUrl &origin, const QWebEnginePage::Feature &feature);
0096     void renderProcessTerminated(QWebEnginePage::RenderProcessTerminationStatus terminationStatus, int exitCode);
0097     void onCertificateError(QWebEngineCertificateError error);
0098 
0099 private:
0100     bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override;
0101 
0102     QStringList chooseFiles(FileSelectionMode mode, const QStringList &oldFiles, const QStringList &acceptedMimeTypes) override;
0103     QWebEnginePage* createWindow(QWebEnginePage::WebWindowType type) override;
0104 
0105     void handleUnknownProtocol(const QUrl &url);
0106     void desktopServicesOpen(const QUrl &url);
0107 
0108     static QString s_lastUploadLocation;
0109     static QUrl s_lastUnsupportedUrl;
0110     static QElapsedTimer s_lastUnsupportedUrlTime;
0111 
0112     DelayedFileWatcher* m_fileWatcher;
0113     QEventLoop* m_runningLoop;
0114 
0115     QStringList m_autoFillUsernames;
0116     QWebEngineRegisterProtocolHandlerRequest *m_registerProtocolHandlerRequest = nullptr;
0117 
0118     int m_loadProgress;
0119     bool m_blockAlerts;
0120     bool m_secureStatus;
0121 
0122     QMetaObject::Connection m_contentsResizedConnection;
0123 
0124     friend class WebView;
0125 };
0126 
0127 #endif // WEBPAGE_H