aboutsummaryrefslogtreecommitdiff
path: root/lib/web/urlfilter/filterrule.h
blob: 46690b13ddd9abb82c823ed7d4b86d2a3789ade1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef SMOLBOTE_FILTERRULE_H
#define SMOLBOTE_FILTERRULE_H

#include <QObject>
#include <QRegularExpression>
#include <QStringList>
#include <QStringMatcher>
#include <QJsonObject>
#include <QUrl>
#include <QWebEngineUrlRequestInfo>

class QUrl;
class FilterRule
{
public:
    FilterRule(const QJsonObject &filter);
    ~FilterRule() = default;

    bool isValid() const;
    bool process(QWebEngineUrlRequestInfo &info) const;
    bool matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const;

private:
    Q_DISABLE_COPY(FilterRule)

    enum ActionType {
        Whitelist,
        Blacklist,
        Redirect,
        SetHeader
    };

    enum RuleType {
        CssRule = 0,                    //
        DomainMatchRule = 1,            //
        RegExpMatchRule = 2,            // match request url with regexp
        StringEndsMatchRule = 3,        // request url ends with string
        StringContainsMatchRule = 4,    // request url contains string
        MatchAllUrlsRule = 5,           //
        Invalid = 6
    };

    ActionType m_action;
    RuleType m_type = RuleType::Invalid;

    QHash<QWebEngineUrlRequestInfo::ResourceType, bool> m_options;

    // Parsed rule for string matching (CSS Selector for CSS rules)
    QString m_matchString;
    // Case sensitivity for string matching
    Qt::CaseSensitivity m_caseSensitivity = Qt::CaseInsensitive;

    bool m_isException = false;

    // domains this rule is allowed or blocked on
    QStringList m_allowedForDomains;
    QStringList m_blockedForDomains;

    QUrl m_redirectUrl;

    QRegularExpression regexp;
    QStringMatcher matcher;
    QString pattern;
};

//bool isMatchingDomain(const QString &domain, const QString &filter);

#endif // SMOLBOTE_FILTERRULE_H