File indexing completed on 2025-01-19 04:47:00

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "adblockfilter.h"
0010 #include "libadblockplugin_export.h"
0011 #include <QList>
0012 #include <QNetworkAccessManager>
0013 #include <QObject>
0014 #include <QWebEngineUrlRequestInfo>
0015 
0016 #include <adblock.rs.h>
0017 #include <future>
0018 #include <optional>
0019 
0020 class AdblockListsManager;
0021 class QNetworkReply;
0022 
0023 class LIBADBLOCKPLUGIN_EXPORT AdblockManager : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     explicit AdblockManager(QObject *parent = nullptr);
0028     ~AdblockManager() override;
0029 
0030     static AdblockManager *self();
0031 
0032     [[nodiscard]] QList<AdblockFilter> adblockFilterLists() const;
0033     void setAdblockFilterLists(const QList<AdblockFilter> &newAdblockFilterLists);
0034 
0035     [[nodiscard]] bool interceptRequest(QWebEngineUrlRequestInfo &info);
0036 
0037     void refreshLists();
0038 
0039     [[nodiscard]] QString filterListIdFromUrl(const QString &url) const;
0040     [[nodiscard]] QString adblockListText(const QString &url);
0041 
0042 Q_SIGNALS:
0043     void enabledChanged(bool enabled);
0044     void adblockInitialized();
0045     void refreshFinished();
0046 
0047 private Q_SLOTS:
0048     void handleListFetched(QNetworkReply *reply);
0049 
0050 private:
0051     void reloadConfig();
0052     void writeConfig();
0053     [[nodiscard]] QString adblockCacheLocation() const;
0054 
0055     /// If an adblock cache is found, loads it, otherwise creates a new adblock
0056     /// from the current filter lists.
0057     rust::Box<Adblock> createOrRestoreAdblock();
0058 
0059     std::future<rust::Box<Adblock>> mAdblockInitFuture;
0060     std::optional<rust::Box<Adblock>> mAdblock;
0061     QList<AdblockFilter> mAdblockFilterLists;
0062     AdblockListsManager *const mAdblockListManager;
0063     QNetworkAccessManager m_networkManager;
0064     int m_runningRequests = 0;
0065 };
0066 
0067 extern "C" {
0068 void q_cdebug_adblock(const char *message);
0069 }