Commit 96c29d02 authored by Laurent Montel's avatar Laurent Montel 😁

Astyle kdelibs

parent a97ae0e2
......@@ -37,7 +37,7 @@
#include "adblocksubscription.h"
using namespace AdBlock;
AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager)
AdBlockMatcher::AdBlockMatcher(AdBlockManager *manager)
: QObject(manager)
, m_manager(manager)
{
......@@ -49,28 +49,32 @@ AdBlockMatcher::~AdBlockMatcher()
clear();
}
const AdBlockRule* AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const
const AdBlockRule *AdBlockMatcher::match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const
{
// Exception rules
if (m_networkExceptionTree.find(request, urlDomain, urlString))
if (m_networkExceptionTree.find(request, urlDomain, urlString)) {
return 0;
}
int count = m_networkExceptionRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_networkExceptionRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString))
const AdBlockRule *rule = m_networkExceptionRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString)) {
return 0;
}
}
// Block rules
if (const AdBlockRule* rule = m_networkBlockTree.find(request, urlDomain, urlString))
if (const AdBlockRule *rule = m_networkBlockTree.find(request, urlDomain, urlString)) {
return rule;
}
count = m_networkBlockRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_networkBlockRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString))
const AdBlockRule *rule = m_networkBlockRules.at(i);
if (rule->networkMatch(request, urlDomain, urlString)) {
return rule;
}
}
return 0;
......@@ -81,22 +85,25 @@ bool AdBlockMatcher::adBlockDisabledForUrl(const QUrl &url) const
int count = m_documentRules.count();
for (int i = 0; i < count; ++i)
if (m_documentRules.at(i)->urlMatch(url))
if (m_documentRules.at(i)->urlMatch(url)) {
return true;
}
return false;
}
bool AdBlockMatcher::elemHideDisabledForUrl(const QUrl &url) const
{
if (adBlockDisabledForUrl(url))
if (adBlockDisabledForUrl(url)) {
return true;
}
int count = m_elemhideRules.count();
for (int i = 0; i < count; ++i)
if (m_elemhideRules.at(i)->urlMatch(url))
if (m_elemhideRules.at(i)->urlMatch(url)) {
return true;
}
return false;
}
......@@ -113,16 +120,16 @@ QString AdBlockMatcher::elementHidingRulesForDomain(const QString &domain) const
int count = m_domainRestrictedCssRules.count();
for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain))
const AdBlockRule *rule = m_domainRestrictedCssRules.at(i);
if (!rule->matchDomain(domain)) {
continue;
}
if (Q_UNLIKELY(addedRulesCount == 1000)) {
rules.append(rule->cssSelector());
rules.append(QL1S("{display:none !important;}\n"));
addedRulesCount = 0;
}
else {
} else {
rules.append(rule->cssSelector() + QLatin1Char(','));
addedRulesCount++;
}
......@@ -140,51 +147,53 @@ void AdBlockMatcher::update()
{
clear();
QHash<QString, const AdBlockRule*> cssRulesHash;
QVector<const AdBlockRule*> exceptionCssRules;
QHash<QString, const AdBlockRule *> cssRulesHash;
QVector<const AdBlockRule *> exceptionCssRules;
foreach (AdBlockSubscription* subscription, m_manager->subscriptions()) {
foreach (const AdBlockRule* rule, subscription->allRules()) {
foreach (AdBlockSubscription *subscription, m_manager->subscriptions()) {
foreach (const AdBlockRule *rule, subscription->allRules()) {
// Don't add internally disabled rules to cache
if (rule->isInternalDisabled())
if (rule->isInternalDisabled()) {
continue;
}
if (rule->isCssRule()) {
// We will add only enabled css rules to cache, because there is no enabled/disabled
// check on match. They are directly embedded to pages.
if (!rule->isEnabled())
if (!rule->isEnabled()) {
continue;
}
if (rule->isException())
if (rule->isException()) {
exceptionCssRules.append(rule);
else
} else {
cssRulesHash.insert(rule->cssSelector(), rule);
}
else if (rule->isDocument()) {
}
} else if (rule->isDocument()) {
m_documentRules.append(rule);
}
else if (rule->isElemhide()) {
} else if (rule->isElemhide()) {
m_elemhideRules.append(rule);
}
else if (rule->isException()) {
if (!m_networkExceptionTree.add(rule))
} else if (rule->isException()) {
if (!m_networkExceptionTree.add(rule)) {
m_networkExceptionRules.append(rule);
}
else {
if (!m_networkBlockTree.add(rule))
}
} else {
if (!m_networkBlockTree.add(rule)) {
m_networkBlockRules.append(rule);
}
}
}
}
foreach (const AdBlockRule* rule, exceptionCssRules) {
const AdBlockRule* originalRule = cssRulesHash.value(rule->cssSelector());
foreach (const AdBlockRule *rule, exceptionCssRules) {
const AdBlockRule *originalRule = cssRulesHash.value(rule->cssSelector());
// If we don't have this selector, the exception does nothing
if (!originalRule)
if (!originalRule) {
continue;
}
AdBlockRule* copiedRule = originalRule->copy();
AdBlockRule *copiedRule = originalRule->copy();
copiedRule->m_options |= AdBlockRule::DomainRestrictedOption;
copiedRule->m_blockedDomains.append(rule->m_allowedDomains);
......@@ -197,20 +206,18 @@ void AdBlockMatcher::update()
// So let's split it by 1000 selectors...
int hidingRulesCount = 0;
QHashIterator<QString, const AdBlockRule*> it(cssRulesHash);
QHashIterator<QString, const AdBlockRule *> it(cssRulesHash);
while (it.hasNext()) {
it.next();
const AdBlockRule* rule = it.value();
const AdBlockRule *rule = it.value();
if (rule->isDomainRestricted()) {
m_domainRestrictedCssRules.append(rule);
}
else if (Q_UNLIKELY(hidingRulesCount == 1000)) {
} else if (Q_UNLIKELY(hidingRulesCount == 1000)) {
m_elementHidingRules.append(rule->cssSelector());
m_elementHidingRules.append(QL1S("{display:none !important;} "));
hidingRulesCount = 0;
}
else {
} else {
m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(','));
hidingRulesCount++;
}
......@@ -238,8 +245,9 @@ void AdBlockMatcher::clear()
void AdBlockMatcher::enabledChanged(bool enabled)
{
if (enabled)
if (enabled) {
update();
else
} else {
clear();
}
}
......@@ -49,10 +49,10 @@ class AdBlockMatcher : public QObject
{
Q_OBJECT
public:
explicit AdBlockMatcher(AdBlockManager* manager);
explicit AdBlockMatcher(AdBlockManager *manager);
~AdBlockMatcher();
const AdBlockRule* match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const;
const AdBlockRule *match(const QWebEngineUrlRequestInfo &request, const QString &urlDomain, const QString &urlString) const;
bool adBlockDisabledForUrl(const QUrl &url) const;
bool elemHideDisabledForUrl(const QUrl &url) const;
......@@ -68,14 +68,14 @@ private Q_SLOTS:
void enabledChanged(bool enabled);
private:
AdBlockManager* m_manager;
QVector<AdBlockRule*> m_createdRules;
QVector<const AdBlockRule*> m_networkExceptionRules;
QVector<const AdBlockRule*> m_networkBlockRules;
QVector<const AdBlockRule*> m_domainRestrictedCssRules;
QVector<const AdBlockRule*> m_documentRules;
QVector<const AdBlockRule*> m_elemhideRules;
AdBlockManager *m_manager;
QVector<AdBlockRule *> m_createdRules;
QVector<const AdBlockRule *> m_networkExceptionRules;
QVector<const AdBlockRule *> m_networkBlockRules;
QVector<const AdBlockRule *> m_domainRestrictedCssRules;
QVector<const AdBlockRule *> m_documentRules;
QVector<const AdBlockRule *> m_elemhideRules;
QString m_elementHidingRules;
AdBlockSearchTree m_networkBlockTree;
......
......@@ -94,7 +94,7 @@ static QString toSecondLevelDomain(const QUrl &url)
return domain + topLevelDomain;
}
AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription* subscription)
AdBlockRule::AdBlockRule(const QString &filter, AdBlockSubscription *subscription)
: m_subscription(subscription)
, m_type(StringContainsMatchRule)
, m_caseSensitivity(Qt::CaseInsensitive)
......@@ -111,9 +111,9 @@ AdBlockRule::~AdBlockRule()
delete m_regExp;
}
AdBlockRule* AdBlockRule::copy() const
AdBlockRule *AdBlockRule::copy() const
{
AdBlockRule* rule = new AdBlockRule();
AdBlockRule *rule = new AdBlockRule();
rule->m_subscription = m_subscription;
rule->m_type = m_type;
rule->m_options = m_options;
......@@ -136,12 +136,12 @@ AdBlockRule* AdBlockRule::copy() const
return rule;
}
AdBlockSubscription* AdBlockRule::subscription() const
AdBlockSubscription *AdBlockRule::subscription() const
{
return m_subscription;
}
void AdBlockRule::setSubscription(AdBlockSubscription* subscription)
void AdBlockRule::setSubscription(AdBlockSubscription *subscription)
{
m_subscription = subscription;
}
......@@ -218,7 +218,6 @@ bool AdBlockRule::urlMatch(const QUrl &url) const
return false;
}
const QString encodedUrl = QString::fromLatin1(url.toEncoded());
const QString domain = url.host();
......@@ -299,16 +298,14 @@ bool AdBlockRule::matchDomain(const QString &domain) const
return true;
}
}
}
else if (m_allowedDomains.isEmpty()) {
} else if (m_allowedDomains.isEmpty()) {
foreach (const QString &d, m_blockedDomains) {
if (isMatchingDomain(domain, d)) {
return false;
}
}
return true;
}
else {
} else {
foreach (const QString &d, m_blockedDomains) {
if (isMatchingDomain(domain, d)) {
return false;
......@@ -433,60 +430,48 @@ void AdBlockRule::parseFilter()
if (option.startsWith(QLatin1String("domain="))) {
parseDomains(option.mid(7), QLatin1Char('|'));
++handledOptions;
}
else if (option == QLatin1String("match-case")) {
} else if (option == QLatin1String("match-case")) {
m_caseSensitivity = Qt::CaseSensitive;
++handledOptions;
}
else if (option.endsWith(QLatin1String("third-party"))) {
} else if (option.endsWith(QLatin1String("third-party"))) {
setOption(ThirdPartyOption);
setException(ThirdPartyOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("object"))) {
} else if (option.endsWith(QLatin1String("object"))) {
setOption(ObjectOption);
setException(ObjectOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("subdocument"))) {
} else if (option.endsWith(QLatin1String("subdocument"))) {
setOption(SubdocumentOption);
setException(SubdocumentOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("xmlhttprequest"))) {
} else if (option.endsWith(QLatin1String("xmlhttprequest"))) {
setOption(XMLHttpRequestOption);
setException(XMLHttpRequestOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("image"))) {
} else if (option.endsWith(QLatin1String("image"))) {
setOption(ImageOption);
setException(ImageOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("script"))) {
} else if (option.endsWith(QLatin1String("script"))) {
setOption(ScriptOption);
setException(ScriptOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("stylesheet"))) {
} else if (option.endsWith(QLatin1String("stylesheet"))) {
setOption(StyleSheetOption);
setException(StyleSheetOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option.endsWith(QLatin1String("object-subrequest"))) {
} else if (option.endsWith(QLatin1String("object-subrequest"))) {
setOption(ObjectSubrequestOption);
setException(ObjectSubrequestOption, option.startsWith(QLatin1Char('~')));
++handledOptions;
}
else if (option == QLatin1String("document") && m_isException) {
} else if (option == QLatin1String("document") && m_isException) {
setOption(DocumentOption);
++handledOptions;
}
else if (option == QLatin1String("elemhide") && m_isException) {
} else if (option == QLatin1String("elemhide") && m_isException) {
setOption(ElementHideOption);
++handledOptions;
}
else if (option == QLatin1String("collapse")) {
} else if (option == QLatin1String("collapse")) {
// Hiding placeholders of blocked elements is enabled by default
++handledOptions;
}
......@@ -545,8 +530,8 @@ void AdBlockRule::parseFilter()
// If we still find a wildcard (*) or separator (^) or (|)
// we must modify parsedLine to comply with QzRegExp
if (parsedLine.contains(QLatin1Char('*')) ||
parsedLine.contains(QLatin1Char('^')) ||
parsedLine.contains(QLatin1Char('|'))
parsedLine.contains(QLatin1Char('^')) ||
parsedLine.contains(QLatin1Char('|'))
) {
m_type = RegExpMatchRule;
m_regExp = new RegExp;
......@@ -570,8 +555,7 @@ void AdBlockRule::parseDomains(const QString &domains, const QChar &separator)
}
if (domain.startsWith(QLatin1Char('~'))) {
m_blockedDomains.append(domain.mid(1));
}
else {
} else {
m_allowedDomains.append(domain);
}
}
......@@ -583,8 +567,9 @@ void AdBlockRule::parseDomains(const QString &domains, const QChar &separator)
bool AdBlockRule::filterIsOnlyDomain(const QString &filter) const
{
if (!filter.endsWith(QLatin1Char('^')) || !filter.startsWith(QLatin1String("||")))
if (!filter.endsWith(QLatin1Char('^')) || !filter.startsWith(QLatin1String("||"))) {
return false;
}
for (int i = 0; i < filter.size(); ++i) {
switch (filter.at(i).toLatin1()) {
......@@ -640,8 +625,9 @@ QString AdBlockRule::createRegExpFromFilter(const QString &filter) const
break;
case '*':
if (!hadWildcard)
if (!hadWildcard) {
parsed.append(QLatin1String(".*"));
}
break;
case '|':
......@@ -649,23 +635,22 @@ QString AdBlockRule::createRegExpFromFilter(const QString &filter) const
if (filter.size() > 1 && filter.at(1) == QLatin1Char('|')) {
parsed.append(QLatin1String("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?"));
i++;
}
else {
} else {
parsed.append(QLatin1Char('^'));
}
break;
}
else if (i == filter.size() - 1) {
} else if (i == filter.size() - 1) {
parsed.append(QLatin1Char('$'));
break;
}
// fallthrough
// fallthrough
default:
if (!wordCharacter(c))
if (!wordCharacter(c)) {
parsed.append(QLatin1Char('\\') + c);
else
} else {
parsed.append(c);
}
}
hadWildcard = c == QLatin1Char('*');
......@@ -690,14 +675,11 @@ bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl)
{
if (m_type == StringContainsMatchRule) {
return encodedUrl.contains(m_matchString, m_caseSensitivity);
}
else if (m_type == DomainMatchRule) {
} else if (m_type == DomainMatchRule) {
return isMatchingDomain(domain, m_matchString);
}
else if (m_type == StringEndsMatchRule) {
} else if (m_type == StringEndsMatchRule) {
return encodedUrl.endsWith(m_matchString, m_caseSensitivity);
}
else if (m_type == RegExpMatchRule) {
} else if (m_type == RegExpMatchRule) {
if (!isMatchingRegExpStrings(encodedUrl)) {
return false;
}
......@@ -709,7 +691,7 @@ bool AdBlockRule::stringMatch(const QString &domain, const QString &encodedUrl)
bool AdBlockRule::isMatchingDomain(const QString &domain, const QString &filter) const
{
return false;//FIXME QzTools::matchDomain(filter, domain);
return QzTools::matchDomain(filter, domain);
}
bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const
......@@ -717,8 +699,9 @@ bool AdBlockRule::isMatchingRegExpStrings(const QString &url) const
Q_ASSERT(m_regExp);
foreach (const QStringMatcher &matcher, m_regExp->matchers) {
if (matcher.indexIn(url) == -1)
if (matcher.indexIn(url) == -1) {
return false;
}
}
return true;
......@@ -736,15 +719,17 @@ QStringList AdBlockRule::parseRegExpFilter(const QString &filter) const
// Meta characters in AdBlock rules are | * ^
if (c == QLatin1Char('|') || c == QLatin1Char('*') || c == QLatin1Char('^')) {
const QString sub = filter.mid(startPos, i - startPos);
if (sub.size() > 1)
if (sub.size() > 1) {
list.append(sub);
}
startPos = i + 1;
}
}
const QString sub = filter.mid(startPos);
if (sub.size() > 1)
if (sub.size() > 1) {
list.append(sub);
}
list.removeDuplicates();
......
......@@ -78,13 +78,13 @@ class AdBlockRule
Q_DISABLE_COPY(AdBlockRule)
public:
AdBlockRule(const QString &filter = QString(), AdBlockSubscription* subscription = Q_NULLPTR);
AdBlockRule(const QString &filter = QString(), AdBlockSubscription *subscription = Q_NULLPTR);
~AdBlockRule();
AdBlockRule* copy() const;
AdBlockRule *copy() const;
AdBlockSubscription* subscription() const;
void setSubscription(AdBlockSubscription* subscription);
AdBlockSubscription *subscription() const;
void setSubscription(AdBlockSubscription *subscription);
QString filter() const;
void setFilter(const QString &filter);
......@@ -165,7 +165,7 @@ private:
QString createRegExpFromFilter(const QString &filter) const;
QList<QStringMatcher> createStringMatchers(const QStringList &filters) const;
AdBlockSubscription* m_subscription;
AdBlockSubscription *m_subscription;
RuleType m_type;
RuleOptions m_options;
......@@ -191,7 +191,7 @@ private:
};
// Use dynamic allocation to save memory
RegExp* m_regExp;
RegExp *m_regExp;
friend class AdBlockMatcher;
friend class AdBlockSearchTree;
......
......@@ -53,7 +53,7 @@ void AdBlockSearchTree::clear()
m_root = new Node;
}
bool AdBlockSearchTree::add(const AdBlockRule* rule)
bool AdBlockSearchTree::add(const AdBlockRule *rule)
{
if (rule->m_type != AdBlockRule::StringContainsMatchRule) {
return false;
......@@ -67,12 +67,12 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule)
return false;
}
Node* node = m_root;
Node *node = m_root;
for (int i = 0; i < len; ++i) {
const QChar c = filter.at(i);
if (!node->children.contains(c)) {
Node* n = new Node;
Node *n = new Node;
n->c = c;
node->children[c] = n;
......@@ -86,7 +86,7 @@ bool AdBlockSearchTree::add(const AdBlockRule* rule)
return true;
}
const AdBlockRule* AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
const AdBlockRule *AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &urlString) const
{
int len = urlString.size();
......@@ -94,10 +94,10 @@ const AdBlockRule* AdBlockSearchTree::find(const QWebEngineUrlRequestInfo &reque
return 0;
}