mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 10:46:35 +01:00
Added ExtensionScheme API
This commit is contained in:
parent
97dbf5aff6
commit
855b30b575
@ -186,6 +186,8 @@ set(SRCS ${SRCS}
|
|||||||
plugins/qml/api/userscript/qmluserscript.cpp
|
plugins/qml/api/userscript/qmluserscript.cpp
|
||||||
plugins/qml/api/userscript/qmluserscripts.cpp
|
plugins/qml/api/userscript/qmluserscripts.cpp
|
||||||
plugins/qml/api/userscript/qmlexternaljsobject.cpp
|
plugins/qml/api/userscript/qmlexternaljsobject.cpp
|
||||||
|
plugins/qml/api/extensionscheme/qmlextensionscheme.cpp
|
||||||
|
plugins/qml/api/extensionscheme/qmlwebengineurlrequestjob.cpp
|
||||||
popupwindow/popuplocationbar.cpp
|
popupwindow/popuplocationbar.cpp
|
||||||
popupwindow/popupstatusbarmessage.cpp
|
popupwindow/popupstatusbarmessage.cpp
|
||||||
popupwindow/popupwebview.cpp
|
popupwindow/popupwebview.cpp
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qmlextensionscheme.h"
|
||||||
|
#include "mainapplication.h"
|
||||||
|
#include "networkmanager.h"
|
||||||
|
#include <QWebEngineUrlRequestJob>
|
||||||
|
#include <QQmlEngine>
|
||||||
|
#include <QMap>
|
||||||
|
|
||||||
|
QmlExtensionScheme::QmlExtensionScheme(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{
|
||||||
|
m_schemeHandler = new QmlExtensionSchemeHandler;
|
||||||
|
connect(m_schemeHandler, &QmlExtensionSchemeHandler::_requestStarted, this, [this](QWebEngineUrlRequestJob *job) {
|
||||||
|
QmlWebEngineUrlRequestJob *request = new QmlWebEngineUrlRequestJob(job);
|
||||||
|
emit requestStarted(request);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
QmlExtensionScheme::~QmlExtensionScheme()
|
||||||
|
{
|
||||||
|
mApp->networkManager()->unregisterExtensionSchemeHandler(m_schemeHandler);
|
||||||
|
m_schemeHandler->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlExtensionScheme::componentComplete()
|
||||||
|
{
|
||||||
|
mApp->networkManager()->registerExtensionSchemeHandler(m_name, m_schemeHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QmlExtensionScheme::name() const
|
||||||
|
{
|
||||||
|
return m_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlExtensionScheme::setName(const QString &name)
|
||||||
|
{
|
||||||
|
m_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlExtensionSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
|
||||||
|
{
|
||||||
|
emit _requestStarted(job);
|
||||||
|
}
|
53
src/lib/plugins/qml/api/extensionscheme/qmlextensionscheme.h
Normal file
53
src/lib/plugins/qml/api/extensionscheme/qmlextensionscheme.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "schemehandlers/extensionschemehandler.h"
|
||||||
|
#include "qmlwebengineurlrequestjob.h"
|
||||||
|
#include <QQmlParserStatus>
|
||||||
|
#include <QJSValue>
|
||||||
|
|
||||||
|
class QmlExtensionSchemeHandler;
|
||||||
|
|
||||||
|
class QmlExtensionScheme : public QObject, public QQmlParserStatus
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(QString name READ name WRITE setName)
|
||||||
|
public:
|
||||||
|
explicit QmlExtensionScheme(QObject *parent = nullptr);
|
||||||
|
~QmlExtensionScheme();
|
||||||
|
void classBegin() {}
|
||||||
|
void componentComplete();
|
||||||
|
Q_SIGNALS:
|
||||||
|
void requestStarted(QmlWebEngineUrlRequestJob *request);
|
||||||
|
private:
|
||||||
|
QString m_name;
|
||||||
|
QmlExtensionSchemeHandler *m_schemeHandler;
|
||||||
|
|
||||||
|
QString name() const;
|
||||||
|
void setName(const QString &name);
|
||||||
|
};
|
||||||
|
|
||||||
|
class QmlExtensionSchemeHandler : public ExtensionSchemeHandler
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
void requestStarted(QWebEngineUrlRequestJob *job);
|
||||||
|
Q_SIGNALS:
|
||||||
|
void _requestStarted(QWebEngineUrlRequestJob *job);
|
||||||
|
};
|
@ -0,0 +1,85 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#include "qmlwebengineurlrequestjob.h"
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QVariantMap>
|
||||||
|
#include <qztools.h>
|
||||||
|
|
||||||
|
QmlWebEngineUrlRequestJob::QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job, QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_job(job)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlWebEngineUrlRequestJob::fail(QmlWebEngineUrlRequestJob::Error error)
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_job->fail(QWebEngineUrlRequestJob::Error(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlWebEngineUrlRequestJob::redirect(const QString &urlString)
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return m_job->redirect(QUrl::fromEncoded(urlString.toUtf8()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlWebEngineUrlRequestJob::reply(const QVariantMap &map)
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!map.contains(QSL("content")) || !map.contains(QSL("contentType"))) {
|
||||||
|
qWarning() << "Unable to call" << __FUNCTION__ << ": invalid parameters";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QByteArray content = map.value(QSL("content")).toString().toUtf8();
|
||||||
|
QByteArray contentType = map.value(QSL("contentType")).toString().toUtf8();
|
||||||
|
QBuffer *buffer = new QBuffer();
|
||||||
|
buffer->open(QIODevice::ReadWrite);
|
||||||
|
buffer->write(content);
|
||||||
|
buffer->seek(0);
|
||||||
|
m_job->reply(contentType, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QmlWebEngineUrlRequestJob::initiator() const
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
return QString::fromUtf8(m_job->initiator().toEncoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QmlWebEngineUrlRequestJob::requestUrl() const
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
return QString::fromUtf8(m_job->requestUrl().toEncoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QmlWebEngineUrlRequestJob::requestMethod() const
|
||||||
|
{
|
||||||
|
if (!m_job) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
return QString::fromUtf8(m_job->requestMethod());
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/* ============================================================
|
||||||
|
* Falkon - Qt web browser
|
||||||
|
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
* ============================================================ */
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QWebEngineUrlRequestJob>
|
||||||
|
|
||||||
|
class QmlWebEngineUrlRequestJob : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PROPERTY(QString initiator READ initiator CONSTANT)
|
||||||
|
Q_PROPERTY(QString requestUrl READ requestUrl CONSTANT)
|
||||||
|
Q_PROPERTY(QString requestMethod READ requestMethod CONSTANT)
|
||||||
|
public:
|
||||||
|
enum Error {
|
||||||
|
NoError = QWebEngineUrlRequestJob::NoError,
|
||||||
|
UrlNotFound = QWebEngineUrlRequestJob::UrlNotFound,
|
||||||
|
UrlInvaild = QWebEngineUrlRequestJob::UrlInvalid,
|
||||||
|
RequestAborted = QWebEngineUrlRequestJob::RequestAborted,
|
||||||
|
RequestDenied = QWebEngineUrlRequestJob::RequestDenied,
|
||||||
|
RequestFailed = QWebEngineUrlRequestJob::RequestFailed
|
||||||
|
};
|
||||||
|
Q_ENUMS(Error)
|
||||||
|
explicit QmlWebEngineUrlRequestJob(QWebEngineUrlRequestJob *job = nullptr, QObject *parent = nullptr);
|
||||||
|
Q_INVOKABLE void fail(Error error);
|
||||||
|
Q_INVOKABLE void redirect(const QString &urlString);
|
||||||
|
Q_INVOKABLE void reply(const QVariantMap &map);
|
||||||
|
private:
|
||||||
|
QWebEngineUrlRequestJob *m_job;
|
||||||
|
|
||||||
|
QString initiator() const;
|
||||||
|
QString requestUrl() const;
|
||||||
|
QString requestMethod() const;
|
||||||
|
};
|
@ -45,6 +45,8 @@
|
|||||||
#include "api/userscript/qmluserscript.h"
|
#include "api/userscript/qmluserscript.h"
|
||||||
#include "api/userscript/qmluserscripts.h"
|
#include "api/userscript/qmluserscripts.h"
|
||||||
#include "api/userscript/qmlexternaljsobject.h"
|
#include "api/userscript/qmlexternaljsobject.h"
|
||||||
|
#include "api/extensionscheme/qmlextensionscheme.h"
|
||||||
|
#include "api/extensionscheme/qmlwebengineurlrequestjob.h"
|
||||||
|
|
||||||
#ifdef LibIntl_FOUND
|
#ifdef LibIntl_FOUND
|
||||||
#include "qml/api/i18n/qmli18n.h"
|
#include "qml/api/i18n/qmli18n.h"
|
||||||
@ -202,4 +204,9 @@ void QmlPlugins::registerQmlTypes()
|
|||||||
auto *object = new QmlExternalJsObject();
|
auto *object = new QmlExternalJsObject();
|
||||||
return object;
|
return object;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// SchemeHandler
|
||||||
|
qmlRegisterType<QmlExtensionScheme>("org.kde.falkon", 1, 0, "ExtensionScheme");
|
||||||
|
|
||||||
|
qmlRegisterUncreatableType<QmlWebEngineUrlRequestJob>("org.kde.falkon", 1, 0, "WebEngineUrlRequestJob", "Unable to register type: WebEngineUrlRequestJob");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user