1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

NetworkManager: Add support for "extension://" scheme

This commit is contained in:
David Rosca 2018-02-05 14:11:29 +01:00
parent ea432a7de7
commit e28b2048df
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
7 changed files with 130 additions and 5 deletions

View File

@ -132,6 +132,7 @@ set(SRCS ${SRCS}
network/networkmanager.cpp
network/networkproxyfactory.cpp
network/networkurlinterceptor.cpp
network/schemehandlers/extensionschemehandler.cpp
network/schemehandlers/falkonschemehandler.cpp
network/sslerrordialog.cpp
notifications/desktopnotification.cpp

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@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
@ -26,6 +26,7 @@
#include "sslerrordialog.h"
#include "networkurlinterceptor.h"
#include "schemehandlers/falkonschemehandler.h"
#include "schemehandlers/extensionschemehandler.h"
#include <QLabel>
#include <QDialog>
@ -44,6 +45,8 @@ NetworkManager::NetworkManager(QObject *parent)
{
// Create scheme handlers
mApp->webProfile()->installUrlSchemeHandler(QByteArrayLiteral("falkon"), new FalkonSchemeHandler());
m_extensionScheme = new ExtensionSchemeManager();
mApp->webProfile()->installUrlSchemeHandler(QByteArrayLiteral("extension"), m_extensionScheme);
// Create url interceptor
m_urlInterceptor = new NetworkUrlInterceptor(this);
@ -228,6 +231,16 @@ void NetworkManager::removeUrlInterceptor(UrlInterceptor *interceptor)
m_urlInterceptor->removeUrlInterceptor(interceptor);
}
void NetworkManager::registerExtensionSchemeHandler(const QString &name, ExtensionSchemeHandler *handler)
{
m_extensionScheme->registerHandler(name, handler);
}
void NetworkManager::unregisterExtensionSchemeHandler(const QString &name)
{
m_extensionScheme->unregisterHandler(name);
}
void NetworkManager::loadSettings()
{
Settings settings;

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@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
@ -25,6 +25,8 @@
class UrlInterceptor;
class NetworkUrlInterceptor;
class ExtensionSchemeManager;
class ExtensionSchemeHandler;
class FALKON_EXPORT NetworkManager : public QNetworkAccessManager
{
@ -40,6 +42,9 @@ public:
void installUrlInterceptor(UrlInterceptor *interceptor);
void removeUrlInterceptor(UrlInterceptor *interceptor);
void registerExtensionSchemeHandler(const QString &name, ExtensionSchemeHandler *handler);
void unregisterExtensionSchemeHandler(const QString &name);
void loadSettings();
void shutdown();
@ -48,6 +53,7 @@ protected:
private:
NetworkUrlInterceptor *m_urlInterceptor;
ExtensionSchemeManager *m_extensionScheme;
QHash<QString, QWebEngineCertificateError::Error> m_ignoredSslErrors;
};

View File

@ -0,0 +1,58 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@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 "extensionschemehandler.h"
#include <QBuffer>
#include <QUrlQuery>
#include <QWebEngineUrlRequestJob>
// ExtensionSchemeHandler
void ExtensionSchemeHandler::setReply(QWebEngineUrlRequestJob *job, const QByteArray &contentType, const QByteArray &content)
{
QBuffer *buffer = new QBuffer();
buffer->open(QIODevice::ReadWrite);
buffer->write(content);
buffer->seek(0);
job->reply(contentType, buffer);
}
// ExtensionSchemeManager
ExtensionSchemeManager::ExtensionSchemeManager(QObject *parent)
: QWebEngineUrlSchemeHandler(parent)
{
}
void ExtensionSchemeManager::requestStarted(QWebEngineUrlRequestJob *job)
{
ExtensionSchemeHandler *handler = m_handlers.value(job->requestUrl().host());
if (handler) {
handler->requestStarted(job);
} else {
job->fail(QWebEngineUrlRequestJob::UrlInvalid);
}
}
void ExtensionSchemeManager::registerHandler(const QString &name, ExtensionSchemeHandler *handler)
{
m_handlers[name] = handler;
}
void ExtensionSchemeManager::unregisterHandler(const QString &name)
{
m_handlers.remove(name);
}

View File

@ -0,0 +1,47 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@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 <QWebEngineUrlSchemeHandler>
#include "qzcommon.h"
class FALKON_EXPORT ExtensionSchemeHandler : public QObject
{
public:
explicit ExtensionSchemeHandler(QObject *parent = nullptr) : QObject(parent) { }
virtual void requestStarted(QWebEngineUrlRequestJob *job) = 0;
protected:
void setReply(QWebEngineUrlRequestJob *job, const QByteArray &contentType, const QByteArray &content);
};
class FALKON_EXPORT ExtensionSchemeManager : public QWebEngineUrlSchemeHandler
{
public:
explicit ExtensionSchemeManager(QObject *parent = nullptr);
void requestStarted(QWebEngineUrlRequestJob *job) override;
void registerHandler(const QString &name, ExtensionSchemeHandler *handler);
void unregisterHandler(const QString &name);
private:
QHash<QString, ExtensionSchemeHandler*> m_handlers;
};

View File

@ -1,6 +1,6 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2010-2016 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2018 David Rosca <nowrep@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
@ -139,7 +139,7 @@ bool SiteInfo::canShowSiteInfo(const QUrl &url)
if (LocationBar::convertUrlToText(url).isEmpty())
return false;
if (url.scheme() == QL1S("falkon") || url.scheme() == QL1S("view-source"))
if (url.scheme() == QL1S("falkon") || url.scheme() == QL1S("view-source") || url.scheme() == QL1S("extension"))
return false;
return true;

View File

@ -377,7 +377,7 @@ void WebPage::setupWebChannelForUrl(const QUrl &url)
channel = new QWebChannel(this);
ExternalJsObject::setupWebChannel(channel, this);
}
if (url.scheme() == QL1S("falkon")) {
if (url.scheme() == QL1S("falkon") || url.scheme() == QL1S("extension")) {
setWebChannel(channel, UnsafeJsWorld);
} else {
setWebChannel(channel, SafeJsWorld);