#ifndef SMOLBOTE_FILTERRULE_H #define SMOLBOTE_FILTERRULE_H #include #include #include #include #include #include #include #include class QUrl; class FilterRule { public: enum ActionType { Whitelist, Blacklist, Redirect, SetHeader, InvalidAction }; enum MatchType { // 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, // InvalidMatch = 6, }; FilterRule(const QJsonObject &filter); ~FilterRule() = default; void setActionType(ActionType type); void setMatchType(MatchType type, const QString &pattern = QString()); void setRedirectUrl(const QUrl &url); void addHeaderRule(const QByteArray &header, const QByteArray &value); bool isValid() const; bool process(QWebEngineUrlRequestInfo &info) const; bool matchRequestUrl(const QString &requestUrl, const QWebEngineUrlRequestInfo::ResourceType type) const; private: Q_DISABLE_COPY(FilterRule) ActionType m_actionType = ActionType::InvalidAction; MatchType m_matchType = MatchType::InvalidMatch; QHash m_resourceTypeOptions; QHash m_headers; // Parsed rule for string matching (CSS Selector for CSS rules) QString m_matchString; // Case sensitivity for string matching Qt::CaseSensitivity m_caseSensitivity = Qt::CaseInsensitive; QUrl m_redirectUrl; QRegularExpression m_regexp; QStringMatcher m_matcher; QString m_pattern; }; void parseJson(std::unique_ptr &rule, const QJsonObject &filter); #endif // SMOLBOTE_FILTERRULE_H