From 93b1104611bb37ec629f9d40f89d92604e8c5b13 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 23 Feb 2017 19:55:04 +0100 Subject: [PATCH] AdBlock: Guard AdBlockMatcher with mutex Closes #2231 --- src/lib/adblock/adblockmanager.cpp | 26 ++++++++++++++++++++++++-- src/lib/adblock/adblockmanager.h | 7 +++++-- src/lib/adblock/adblockmatcher.cpp | 13 ++----------- src/lib/adblock/adblockmatcher.h | 7 ++----- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/lib/adblock/adblockmanager.cpp b/src/lib/adblock/adblockmanager.cpp index 98cefe81c..9f7f6d6a2 100644 --- a/src/lib/adblock/adblockmanager.cpp +++ b/src/lib/adblock/adblockmanager.cpp @@ -34,6 +34,7 @@ #include #include #include +#include //#define ADBLOCK_DEBUG @@ -80,6 +81,14 @@ void AdBlockManager::setEnabled(bool enabled) load(); mApp->reloadUserStyleSheet(); + + QMutexLocker locker(&m_mutex); + + if (m_enabled) { + m_matcher->update(); + } else { + m_matcher->clear(); + } } QList AdBlockManager::subscriptions() const @@ -89,6 +98,8 @@ QList AdBlockManager::subscriptions() const bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) { + QMutexLocker locker(&m_mutex); + #ifdef ADBLOCK_DEBUG QElapsedTimer timer; timer.start(); @@ -201,13 +212,15 @@ AdBlockSubscription* AdBlockManager::addSubscription(const QString &title, const m_subscriptions.insert(m_subscriptions.count() - 1, subscription); connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); - connect(subscription, SIGNAL(subscriptionChanged()), m_matcher, SLOT(update())); + connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher())); return subscription; } bool AdBlockManager::removeSubscription(AdBlockSubscription* subscription) { + QMutexLocker locker(&m_mutex); + if (!m_subscriptions.contains(subscription) || !subscription->canBeRemoved()) { return false; } @@ -236,6 +249,8 @@ AdBlockCustomList* AdBlockManager::customList() const void AdBlockManager::load() { + QMutexLocker locker(&m_mutex); + if (m_loaded) { return; } @@ -310,7 +325,7 @@ void AdBlockManager::load() subscription->loadSubscription(m_disabledRules); connect(subscription, SIGNAL(subscriptionUpdated()), mApp, SLOT(reloadUserStyleSheet())); - connect(subscription, SIGNAL(subscriptionChanged()), m_matcher, SLOT(update())); + connect(subscription, SIGNAL(subscriptionChanged()), this, SLOT(updateMatcher())); } if (lastUpdate.addDays(5) < QDateTime::currentDateTime()) { @@ -327,6 +342,13 @@ void AdBlockManager::load() mApp->networkManager()->installUrlInterceptor(m_interceptor); } +void AdBlockManager::updateMatcher() +{ + QMutexLocker locker(&m_mutex); + + m_matcher->update(); +} + void AdBlockManager::updateAllSubscriptions() { foreach (AdBlockSubscription* subscription, m_subscriptions) { diff --git a/src/lib/adblock/adblockmanager.h b/src/lib/adblock/adblockmanager.h index 3c627eaa8..a0887101f 100644 --- a/src/lib/adblock/adblockmanager.h +++ b/src/lib/adblock/adblockmanager.h @@ -1,6 +1,6 @@ /* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2010-2016 David Rosca +* QupZilla - Qt web browser +* Copyright (C) 2010-2017 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ #include #include #include +#include #include "qzcommon.h" @@ -79,6 +80,7 @@ public slots: void setEnabled(bool enabled); void showRule(); + void updateMatcher(); void updateAllSubscriptions(); AdBlockDialog* showDialog(); @@ -96,6 +98,7 @@ private: AdBlockUrlInterceptor *m_interceptor; QPointer m_adBlockDialog; + QMutex m_mutex; }; #endif // ADBLOCKMANAGER_H diff --git a/src/lib/adblock/adblockmatcher.cpp b/src/lib/adblock/adblockmatcher.cpp index 51a01d1b8..98581d906 100644 --- a/src/lib/adblock/adblockmatcher.cpp +++ b/src/lib/adblock/adblockmatcher.cpp @@ -1,6 +1,6 @@ /* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2014 David Rosca +* QupZilla - Qt web browser +* Copyright (C) 2014-2017 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +24,6 @@ AdBlockMatcher::AdBlockMatcher(AdBlockManager* manager) : QObject(manager) , m_manager(manager) { - connect(manager, SIGNAL(enabledChanged(bool)), this, SLOT(enabledChanged(bool))); } AdBlockMatcher::~AdBlockMatcher() @@ -218,11 +217,3 @@ void AdBlockMatcher::clear() qDeleteAll(m_createdRules); m_createdRules.clear(); } - -void AdBlockMatcher::enabledChanged(bool enabled) -{ - if (enabled) - update(); - else - clear(); -} diff --git a/src/lib/adblock/adblockmatcher.h b/src/lib/adblock/adblockmatcher.h index 49502d191..12ff2318d 100644 --- a/src/lib/adblock/adblockmatcher.h +++ b/src/lib/adblock/adblockmatcher.h @@ -1,6 +1,6 @@ /* ============================================================ -* QupZilla - WebKit based browser -* Copyright (C) 2014 David Rosca +* QupZilla - Qt web browser +* Copyright (C) 2014-2017 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,9 +48,6 @@ public slots: void update(); void clear(); -private slots: - void enabledChanged(bool enabled); - private: AdBlockManager* m_manager;