1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

AdBlockPlugin: Change into real internal plugin

This commit is contained in:
David Rosca 2018-01-09 18:10:35 +01:00
parent 9bff0f1b5c
commit f16580dc05
6 changed files with 61 additions and 22 deletions

View File

@ -27,15 +27,36 @@
#include "navigationbar.h"
#include "mainapplication.h"
AdBlockPlugin::AdBlockPlugin(QObject *parent)
: QObject(parent)
AdBlockPlugin::AdBlockPlugin()
: QObject()
{
}
PluginSpec AdBlockPlugin::pluginSpec()
{
return PluginSpec();
}
void AdBlockPlugin::init(InitState state, const QString &settingsPath)
{
Q_UNUSED(settingsPath)
Q_ASSERT(state == StartupInitState);
connect(mApp, &MainApplication::aboutToQuit, AdBlockManager::instance(), &AdBlockManager::save);
connect(mApp->plugins(), &PluginProxy::webPageCreated, this, &AdBlockPlugin::webPageCreated);
connect(mApp->plugins(), &PluginProxy::webPageDeleted, this, &AdBlockPlugin::webPageDeleted);
connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &AdBlockPlugin::mainWindowCreated);
}
void AdBlockPlugin::unload()
{
}
bool AdBlockPlugin::testPlugin()
{
return true;
}
void AdBlockPlugin::webPageCreated(WebPage *page)
{
connect(page, &WebPage::loadFinished, this, [=]() {
@ -65,3 +86,17 @@ void AdBlockPlugin::mainWindowCreated(BrowserWindow *window)
{
window->navigationBar()->addToolButton(new AdBlockIcon(window));
}
bool AdBlockPlugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
{
Q_UNUSED(type)
AdBlockManager *manager = AdBlockManager::instance();
if (isMainFrame) {
manager->clearBlockedRequestsForUrl(page->url());
}
if (url.scheme() == QL1S("abp") && AdBlockManager::instance()->addSubscriptionFromUrl(url)) {
return false;
}
return true;
}

View File

@ -17,18 +17,26 @@
* ============================================================ */
#pragma once
#include <QObject>
#include "plugininterface.h"
class WebPage;
class BrowserWindow;
class AdBlockPlugin : public QObject
class AdBlockPlugin : public QObject, public PluginInterface
{
Q_OBJECT
public:
explicit AdBlockPlugin(QObject *parent = nullptr);
explicit AdBlockPlugin();
PluginSpec pluginSpec();
void init(InitState state, const QString &settingsPath);
void unload();
bool testPlugin();
private:
void webPageCreated(WebPage *page);
void webPageDeleted(WebPage *page);
void mainWindowCreated(BrowserWindow *window);
bool acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) override;
};

View File

@ -47,7 +47,6 @@
#include "scripts.h"
#include "sessionmanager.h"
#include "closedwindowsmanager.h"
#include "adblock/adblockplugin.h"
#include <QWebEngineSettings>
#include <QDesktopServices>
@ -311,7 +310,6 @@ MainApplication::MainApplication(int &argc, char** argv)
m_plugins = new PluginProxy;
m_autoFill = new AutoFill(this);
new AdBlockPlugin(this);
if (!noAddons)
m_plugins->loadPlugins();

View File

@ -21,6 +21,7 @@
#include "speeddial.h"
#include "settings.h"
#include "datapaths.h"
#include "adblock/adblockplugin.h"
#include <iostream>
#include <QPluginLoader>
@ -134,15 +135,19 @@ void Plugins::loadPlugins()
if (plugin.isLoaded()) {
plugin.pluginSpec = iPlugin->pluginSpec();
m_loadedPlugins.append(plugin.instance);
m_availablePlugins.append(plugin);
}
}
// Internal plugins
AdBlockPlugin *adBlock = new AdBlockPlugin();
if (initPlugin(PluginInterface::StartupInitState, adBlock, nullptr)) {
m_internalPlugins.append(adBlock);
}
refreshLoadedPlugins();
std::cout << "Falkon: " << m_loadedPlugins.count() << " extensions loaded" << std::endl;
std::cout << "Falkon: " << (m_loadedPlugins.count() - m_internalPlugins.count()) << " extensions loaded" << std::endl;
}
void Plugins::loadAvailablePlugins()
@ -200,7 +205,9 @@ PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInt
if (!pluginInterface->testPlugin()) {
pluginInterface->unload();
loader->unload();
if (loader) {
loader->unload();
}
emit pluginUnloaded(pluginInterface);
@ -214,7 +221,7 @@ PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInt
void Plugins::refreshLoadedPlugins()
{
m_loadedPlugins.clear();
m_loadedPlugins = m_internalPlugins;
foreach (const Plugin &plugin, m_availablePlugins) {
if (plugin.isLoaded()) {

View File

@ -94,6 +94,7 @@ private:
bool m_pluginsLoaded;
SpeedDial* m_speedDial;
QList<PluginInterface*> m_internalPlugins;
};
Q_DECLARE_METATYPE(Plugins::Plugin)

View File

@ -38,7 +38,6 @@
#include "tabwidget.h"
#include "networkmanager.h"
#include "webhittestresult.h"
#include "adblock/adblockmanager.h"
#include "ui_jsconfirm.h"
#include "ui_jsalert.h"
#include "ui_jsprompt.h"
@ -368,15 +367,6 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
if (!mApp->plugins()->acceptNavigationRequest(this, url, type, isMainFrame))
return false;
// AdBlock
AdBlockManager *manager = AdBlockManager::instance();
if (isMainFrame) {
manager->clearBlockedRequestsForUrl(WebPage::url());
}
if (url.scheme() == QL1S("abp") && AdBlockManager::instance()->addSubscriptionFromUrl(url)) {
return false;
}
const bool result = QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
if (result && isMainFrame) {