Debian Codegraph @

src/librssguard/network-web/adblock/adblockmanager.h

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #ifndef ADBLOCKMANAGER_H
4 #define ADBLOCKMANAGER_H
5
6 #include "miscellaneous/nodejs.h"
7
8 #include <QHash>
9 #include <QObject>
10 #include <QProcess>
11
12 #define CLIQZ_ADBLOCKED_PACKAGE "@ghostery/adblocker"
13 #define CLIQZ_ADBLOCKED_VERSION "2.5.1"
14
15 class QUrl;
16 class AdblockRequestInfo;
17 class AdBlockUrlInterceptor;
18 class AdBlockIcon;
19
20 struct BlockingResult {
21 bool m_blocked;
22 QString m_blockedByFilter;
23
24 BlockingResult() : m_blocked(false), m_blockedByFilter(QString()) {}
25
26 BlockingResult(bool blocked, QString blocked_by_filter = {})
27 : m_blocked(blocked), m_blockedByFilter(std::move(blocked_by_filter)) {}
28 };
29
30 class AdBlockManager : public QObject {
31 Q_OBJECT
32
33 public:
34 explicit AdBlockManager(QObject* parent = nullptr);
35 virtual ~AdBlockManager();
36
37 // Enables (or disables) AdBlock feature asynchronously.
38 // This method will start/stop AdBlock in separate process
39 // and thus cannot run synchronously (when enabling) as process takes
40 // some time to start.
41 //
42 // If the process fails then signal processTerminated() is thrown.
43 // If AdBlock is switched on/off then signal enabledChanged(bool, QString) is thrown.
44 void setEnabled(bool enabled);
45 bool isEnabled() const;
46
47 bool canRunOnScheme(const QString& scheme) const;
48 AdBlockIcon* adBlockIcon() const;
49
50 // General methods for adblocking.
51 BlockingResult block(const AdblockRequestInfo& request);
52 QString elementHidingRulesForDomain(const QUrl& url) const;
53
54 QStringList filterLists() const;
55 void setFilterLists(const QStringList& filter_lists);
56
57 QStringList customFilters() const;
58 void setCustomFilters(const QStringList& custom_filters);
59
60 static QString generateJsForElementHiding(const QString& css);
61
62 public slots:
63 void showDialog();
64
65 signals:
66 void enabledChanged(bool enabled, QString error = {});
67 void processTerminated();
68
69 private slots:
70 void onPackageReady(const QObject* sndr, const QList<NodeJs::PackageMetadata>& pkgs, bool already_up_to_date);
71 void onPackageError(const QObject* sndr, const QList<NodeJs::PackageMetadata>& pkgs, const QString& error);
72 void onServerProcessFinished(int exit_code, QProcess::ExitStatus exit_status);
73
74 private:
75 void updateUnifiedFilters();
76 void updateUnifiedFiltersFileAndStartServer();
77
78 QProcess* startServer(int port);
79 void killServer();
80
81 BlockingResult askServerIfBlocked(const QString& fp_url, const QString& url, const QString& url_type) const;
82 QString askServerForCosmeticRules(const QString& url) const;
83
84 private:
85 bool m_loaded;
86 bool m_enabled;
87 bool m_installing;
88 AdBlockIcon* m_adblockIcon;
89
90 #if defined(NO_LITE)
91 AdBlockUrlInterceptor* m_interceptor;
92 #endif
93
94 QString m_unifiedFiltersFile;
95 QProcess* m_serverProcess;
96 QHash<QPair<QString, QString>, BlockingResult> m_cacheBlocks;
97 };
98
99 inline AdBlockIcon* AdBlockManager::adBlockIcon() const {
100 return m_adblockIcon;
101 }
102
103 #endif // ADBLOCKMANAGER_H