File indexing completed on 2024-06-23 04:53:41

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 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 #include "extensionschemehandler.h"
0019 
0020 #include <QBuffer>
0021 #include <QUrlQuery>
0022 #include <QWebEngineUrlRequestJob>
0023 
0024 // ExtensionSchemeHandler
0025 void ExtensionSchemeHandler::setReply(QWebEngineUrlRequestJob *job, const QByteArray &contentType, const QByteArray &content)
0026 {
0027     auto *buffer = new QBuffer(job);
0028     buffer->open(QIODevice::ReadWrite);
0029     buffer->write(content);
0030     buffer->seek(0);
0031     job->reply(contentType, buffer);
0032 }
0033 
0034 // ExtensionSchemeManager
0035 ExtensionSchemeManager::ExtensionSchemeManager(QObject *parent)
0036     : QWebEngineUrlSchemeHandler(parent)
0037 {
0038 }
0039 
0040 void ExtensionSchemeManager::requestStarted(QWebEngineUrlRequestJob *job)
0041 {
0042     ExtensionSchemeHandler *handler = m_handlers.value(job->requestUrl().host());
0043     if (handler) {
0044         handler->requestStarted(job);
0045     } else {
0046         job->fail(QWebEngineUrlRequestJob::UrlInvalid);
0047     }
0048 }
0049 
0050 void ExtensionSchemeManager::registerHandler(const QString &name, ExtensionSchemeHandler *handler)
0051 {
0052     m_handlers[name] = handler;
0053 }
0054 
0055 void ExtensionSchemeManager::unregisterHandler(ExtensionSchemeHandler *handler)
0056 {
0057     m_handlers.remove(m_handlers.key(handler));
0058 }