File indexing completed on 2024-05-12 16:23:45
0001 // SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im> 0002 // 0003 // SPDX-License-Identifier: GPL-2.0-or-later 0004 0005 #pragma once 0006 0007 #include <QWebEngineUrlRequestInterceptor> 0008 0009 #include <angelfishsettings.h> 0010 0011 #ifdef BUILD_ADBLOCK 0012 #include <adblock.rs.h> 0013 #include <optional> 0014 #include <future> 0015 #endif 0016 0017 class QWebEngineUrlRequestInfo; 0018 class QQuickWebEngineProfile; 0019 0020 class AdblockUrlInterceptor : public QWebEngineUrlRequestInterceptor 0021 { 0022 Q_OBJECT 0023 0024 Q_PROPERTY(bool downloadNeeded READ downloadNeeded NOTIFY downloadNeededChanged) 0025 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) 0026 Q_PROPERTY(bool adblockSupported READ adblockSupported CONSTANT) 0027 0028 public: 0029 static AdblockUrlInterceptor &instance(); 0030 ~AdblockUrlInterceptor(); 0031 0032 void interceptRequest(QWebEngineUrlRequestInfo &info) override; 0033 0034 /// returns true when no filterlists exist 0035 bool downloadNeeded() const; 0036 Q_SIGNAL void downloadNeededChanged(); 0037 0038 constexpr static bool adblockSupported() 0039 { 0040 #ifdef BUILD_ADBLOCK 0041 return true; 0042 #endif 0043 return false; 0044 } 0045 0046 bool enabled() const; 0047 void setEnabled(bool enabled); 0048 Q_SIGNAL void enabledChanged(); 0049 0050 /// Deletes the old adblock and creates a new one from the current filter lists. 0051 /// This needs to be called after lists were changed. 0052 void resetAdblock(); 0053 0054 #ifdef BUILD_ADBLOCK 0055 Q_INVOKABLE std::vector<QString> getCosmeticFilters(const QUrl &url, 0056 const std::vector<QString> &classes, 0057 const std::vector<QString> &ids) const; 0058 Q_INVOKABLE QString getInjectedScript(const QUrl &url) const; 0059 #endif 0060 0061 Q_SIGNAL void adblockInitialized(); 0062 0063 private: 0064 explicit AdblockUrlInterceptor(QObject *parent = nullptr); 0065 0066 #ifdef BUILD_ADBLOCK 0067 /// If an adblock cache is found, loads it, otherwise creates a new adblock 0068 /// from the current filter lists. 0069 rust::Box<Adblock> createOrRestoreAdblock(); 0070 0071 std::future<rust::Box<Adblock>> m_adblockInitFuture; 0072 std::optional<rust::Box<Adblock>> m_adblock; 0073 #endif 0074 0075 static QString adblockCacheLocation(); 0076 bool m_enabled; 0077 }; 0078 0079 extern "C" { 0080 void q_cdebug_adblock(const char *message); 0081 }