File indexing completed on 2024-05-12 04:57:50

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 
0019 #include "adblockplugin.h"
0020 #include "adblockmanager.h"
0021 #include "adblockicon.h"
0022 
0023 #include "scripts.h"
0024 #include "webpage.h"
0025 #include "pluginproxy.h"
0026 #include "browserwindow.h"
0027 #include "navigationbar.h"
0028 #include "mainapplication.h"
0029 #include "statusbar.h"
0030 #include "desktopfile.h"
0031 
0032 AdBlockPlugin::AdBlockPlugin()
0033     : QObject()
0034 {
0035 }
0036 
0037 void AdBlockPlugin::init(InitState state, const QString &settingsPath)
0038 {
0039     Q_UNUSED(settingsPath)
0040 
0041     connect(mApp, &MainApplication::aboutToQuit, AdBlockManager::instance(), &AdBlockManager::save);
0042     connect(mApp->plugins(), &PluginProxy::webPageCreated, this, &AdBlockPlugin::webPageCreated);
0043     connect(mApp->plugins(), &PluginProxy::webPageDeleted, this, &AdBlockPlugin::webPageDeleted);
0044     connect(mApp->plugins(), &PluginProxy::mainWindowCreated, this, &AdBlockPlugin::mainWindowCreated);
0045     connect(mApp->plugins(), &PluginProxy::mainWindowDeleted, this, &AdBlockPlugin::mainWindowDeleted);
0046 
0047     if (state == LateInitState) {
0048         const auto windows = mApp->windows();
0049         for (BrowserWindow *window : windows) {
0050             mainWindowCreated(window);
0051         }
0052     }
0053 }
0054 
0055 void AdBlockPlugin::unload()
0056 {
0057     const auto windows = mApp->windows();
0058     for (BrowserWindow *window : windows) {
0059         mainWindowDeleted(window);
0060     }
0061 }
0062 
0063 bool AdBlockPlugin::testPlugin()
0064 {
0065     return true;
0066 }
0067 
0068 void AdBlockPlugin::showSettings(QWidget *parent)
0069 {
0070     AdBlockManager::instance()->showDialog(parent);
0071 }
0072 
0073 void AdBlockPlugin::webPageCreated(WebPage *page)
0074 {
0075     connect(page, &WebPage::loadFinished, this, [=]() {
0076         AdBlockManager *manager = AdBlockManager::instance();
0077         if (!manager->isEnabled()) {
0078             return;
0079         }
0080         // Apply global element hiding rules
0081         const QString elementHiding = manager->elementHidingRules(page->url());
0082         if (!elementHiding.isEmpty()) {
0083             page->runJavaScript(Scripts::setCss(elementHiding), WebPage::SafeJsWorld);
0084         }
0085         // Apply domain-specific element hiding rules
0086         const QString siteElementHiding = manager->elementHidingRulesForDomain(page->url());
0087         if (!siteElementHiding.isEmpty()) {
0088             page->runJavaScript(Scripts::setCss(siteElementHiding), WebPage::SafeJsWorld);
0089         }
0090     });
0091 }
0092 
0093 void AdBlockPlugin::webPageDeleted(WebPage *page)
0094 {
0095     AdBlockManager::instance()->clearBlockedRequestsForUrl(page->url());
0096 }
0097 
0098 void AdBlockPlugin::mainWindowCreated(BrowserWindow *window)
0099 {
0100     auto *icon = new AdBlockIcon(window);
0101     m_icons[window] = icon;
0102     window->statusBar()->addButton(icon);
0103     window->navigationBar()->addToolButton(icon);
0104 }
0105 
0106 void AdBlockPlugin::mainWindowDeleted(BrowserWindow *window)
0107 {
0108     AdBlockIcon *icon = m_icons.take(window);
0109     window->statusBar()->removeButton(icon);
0110     window->navigationBar()->removeToolButton(icon);
0111     delete icon;
0112 }
0113 
0114 bool AdBlockPlugin::acceptNavigationRequest(WebPage *page, const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
0115 {
0116     Q_UNUSED(type)
0117 
0118     AdBlockManager *manager = AdBlockManager::instance();
0119     if (isMainFrame) {
0120         manager->clearBlockedRequestsForUrl(page->url());
0121     }
0122     if (url.scheme() == QL1S("abp") && AdBlockManager::instance()->addSubscriptionFromUrl(url)) {
0123         return false;
0124     }
0125     return true;
0126 }