File indexing completed on 2024-05-12 04:57:50
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-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 #ifndef ADBLOCKMANAGER_H 0019 #define ADBLOCKMANAGER_H 0020 0021 #include <QObject> 0022 #include <QStringList> 0023 #include <QPointer> 0024 #include <QMutex> 0025 #include <QUrl> 0026 #include <QWebEngineUrlRequestInfo> 0027 0028 #include "qzcommon.h" 0029 0030 #define ADBLOCK_EASYLIST_URL QSL("https://easylist-downloads.adblockplus.org/easylist.txt") 0031 #define ADBLOCK_NOCOINLIST_URL QSL("https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/nocoin.txt") 0032 0033 class AdBlockRule; 0034 class AdBlockDialog; 0035 class AdBlockMatcher; 0036 class AdBlockCustomList; 0037 class AdBlockSubscription; 0038 class AdBlockUrlInterceptor; 0039 0040 struct AdBlockedRequest 0041 { 0042 QUrl requestUrl; 0043 QUrl firstPartyUrl; 0044 QByteArray requestMethod; 0045 QWebEngineUrlRequestInfo::ResourceType resourceType; 0046 QWebEngineUrlRequestInfo::NavigationType navigationType; 0047 QString rule; 0048 }; 0049 Q_DECLARE_METATYPE(AdBlockedRequest) 0050 0051 class FALKON_EXPORT AdBlockManager : public QObject 0052 { 0053 Q_OBJECT 0054 0055 public: 0056 AdBlockManager(QObject* parent = nullptr); 0057 ~AdBlockManager(); 0058 0059 void load(); 0060 void save(); 0061 0062 bool isEnabled() const; 0063 bool canRunOnScheme(const QString &scheme) const; 0064 bool canBeBlocked(const QUrl &url) const; 0065 0066 QString elementHidingRules(const QUrl &url) const; 0067 QString elementHidingRulesForDomain(const QUrl &url) const; 0068 0069 AdBlockSubscription* subscriptionByName(const QString &name) const; 0070 QList<AdBlockSubscription*> subscriptions() const; 0071 0072 bool block(QWebEngineUrlRequestInfo &request, QString &ruleFilter, QString &ruleSubscription); 0073 0074 QVector<AdBlockedRequest> blockedRequestsForUrl(const QUrl &url) const; 0075 void clearBlockedRequestsForUrl(const QUrl &url); 0076 0077 QStringList disabledRules() const; 0078 void addDisabledRule(const QString &filter); 0079 void removeDisabledRule(const QString &filter); 0080 0081 bool addSubscriptionFromUrl(const QUrl &url); 0082 0083 AdBlockSubscription* addSubscription(const QString &title, const QString &url); 0084 bool removeSubscription(AdBlockSubscription* subscription); 0085 0086 AdBlockCustomList* customList() const; 0087 0088 static AdBlockManager* instance(); 0089 0090 Q_SIGNALS: 0091 void enabledChanged(bool enabled); 0092 void blockedRequestsChanged(const QUrl &url); 0093 0094 public Q_SLOTS: 0095 void setEnabled(bool enabled); 0096 void showRule(); 0097 0098 void updateMatcher(); 0099 void updateAllSubscriptions(); 0100 0101 AdBlockDialog *showDialog(QWidget *parent = nullptr); 0102 0103 private: 0104 bool m_loaded; 0105 bool m_enabled; 0106 0107 QList<AdBlockSubscription*> m_subscriptions; 0108 AdBlockMatcher* m_matcher; 0109 QStringList m_disabledRules; 0110 0111 AdBlockUrlInterceptor *m_interceptor; 0112 QPointer<AdBlockDialog> m_adBlockDialog; 0113 QMutex m_mutex; 0114 QHash<QUrl, QVector<AdBlockedRequest>> m_blockedRequests; 0115 }; 0116 0117 #endif // ADBLOCKMANAGER_H 0118