diff --git a/src/lib/adblock/adblockplugin.cpp b/src/lib/adblock/adblockplugin.cpp index 0b43c1a4f..95838c85d 100644 --- a/src/lib/adblock/adblockplugin.cpp +++ b/src/lib/adblock/adblockplugin.cpp @@ -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; +} diff --git a/src/lib/adblock/adblockplugin.h b/src/lib/adblock/adblockplugin.h index 9350abba0..8ef50abd4 100644 --- a/src/lib/adblock/adblockplugin.h +++ b/src/lib/adblock/adblockplugin.h @@ -17,18 +17,26 @@ * ============================================================ */ #pragma once -#include +#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; + }; diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 80b1dc22e..a61031160 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -47,7 +47,6 @@ #include "scripts.h" #include "sessionmanager.h" #include "closedwindowsmanager.h" -#include "adblock/adblockplugin.h" #include #include @@ -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(); diff --git a/src/lib/plugins/plugins.cpp b/src/lib/plugins/plugins.cpp index 10b4dfd4d..c066440fd 100644 --- a/src/lib/plugins/plugins.cpp +++ b/src/lib/plugins/plugins.cpp @@ -21,6 +21,7 @@ #include "speeddial.h" #include "settings.h" #include "datapaths.h" +#include "adblock/adblockplugin.h" #include #include @@ -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()) { diff --git a/src/lib/plugins/plugins.h b/src/lib/plugins/plugins.h index f38d68bfc..d4ad50252 100644 --- a/src/lib/plugins/plugins.h +++ b/src/lib/plugins/plugins.h @@ -94,6 +94,7 @@ private: bool m_pluginsLoaded; SpeedDial* m_speedDial; + QList m_internalPlugins; }; Q_DECLARE_METATYPE(Plugins::Plugin) diff --git a/src/lib/webengine/webpage.cpp b/src/lib/webengine/webpage.cpp index 345e8053e..544b13b9a 100644 --- a/src/lib/webengine/webpage.cpp +++ b/src/lib/webengine/webpage.cpp @@ -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) {