File indexing completed on 2025-01-26 04:57:23

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "interceptormanager.h"
0007 #include "urlinterceptor/networkurlinterceptor.h"
0008 #include "urlinterceptor/networkurlinterceptormanager.h"
0009 #include <QWebEngineProfile>
0010 #include <QWebEngineView>
0011 #include <WebEngineViewer/NetworkPluginUrlInterceptorInterface>
0012 
0013 using namespace WebEngineViewer;
0014 
0015 class WebEngineViewer::NetworkAccessManagerWebEnginePrivate
0016 {
0017 public:
0018     NetworkAccessManagerWebEnginePrivate() = default;
0019 
0020     WebEngineViewer::NetworkUrlInterceptorManager *mManager = nullptr;
0021     WebEngineViewer::NetworkUrlInterceptor *mNetworkUrlInterceptor = nullptr;
0022 };
0023 
0024 InterceptorManager::InterceptorManager(QWebEngineView *webEngine, KActionCollection *ac, QObject *parent)
0025     : QObject(parent)
0026     , d(new NetworkAccessManagerWebEnginePrivate)
0027 {
0028     d->mManager = new WebEngineViewer::NetworkUrlInterceptorManager(webEngine, ac, this);
0029 
0030     // Add interceptor.
0031     d->mNetworkUrlInterceptor = new WebEngineViewer::NetworkUrlInterceptor(this);
0032     const auto interfaceList = d->mManager->interfaceList();
0033     for (WebEngineViewer::NetworkPluginUrlInterceptorInterface *interface : interfaceList) {
0034         d->mNetworkUrlInterceptor->addInterceptor(interface);
0035     }
0036     webEngine->page()->profile()->setUrlRequestInterceptor(d->mNetworkUrlInterceptor);
0037 }
0038 
0039 InterceptorManager::~InterceptorManager() = default;
0040 
0041 void InterceptorManager::addInterceptor(WebEngineViewer::NetworkPluginUrlInterceptorInterface *interceptor)
0042 {
0043     d->mNetworkUrlInterceptor->addInterceptor(interceptor);
0044 }
0045 
0046 void InterceptorManager::removeInterceptor(WebEngineViewer::NetworkPluginUrlInterceptorInterface *interceptor)
0047 {
0048     d->mNetworkUrlInterceptor->removeInterceptor(interceptor);
0049 }
0050 
0051 QList<QAction *> InterceptorManager::interceptorUrlActions(const WebEngineViewer::WebHitTestResult &result) const
0052 {
0053     QList<QAction *> actions;
0054     const auto interfaceList = d->mManager->interfaceList();
0055     actions.reserve(interfaceList.count());
0056     for (WebEngineViewer::NetworkPluginUrlInterceptorInterface *interface : interfaceList) {
0057         actions.append(interface->interceptorUrlActions(result));
0058     }
0059     return actions;
0060 }
0061 
0062 #include "moc_interceptormanager.cpp"