Skip to content
Permalink
Browse files

Add support for AdBlock

Overall design:

AdBlockManager (QObject)
- One per browser with a property saying if adblock is enabled.
- Owns all of the subscriptions

AdBlockSubscription (QObject)
- Constructed from an abp: url
- Owns all of the rules for this subscription
- Downloads rules when needed.

AdBlockRule
- Has a bool networkMatch(const QString &) function.
- Various properties such as if enabled, is a css rule etc.

AdBlockNetwork (applies network rules)
- QNetworkAccessManager uses this to see if a connection should be blocked.

AdBlockPage (applies css rules)
- WebPage uses this to hide elements.

AdBlockModel/AdBlockDialog
- Sits on top of AdBlockManager and presents a dialog to the user so
  they can add rules, subscriptions etc.

AdBlockScheme
- Handle the abp url scheme to subscribe to a new adblock subscriptions.

Misc:
- Added right click action to block
  • Loading branch information...
icefox committed Sep 19, 2009
1 parent 6ed449b commit fd2a265b1d80bbd395bfc27d27eef596a50af198
Showing with 3,902 additions and 0 deletions.
  1. +9 −0 autotests/adblock/adblock.pro
  2. +1 −0 autotests/adblock/adblockmanager/.gitignore
  3. +10 −0 autotests/adblock/adblockmanager/adblockmanager.pro
  4. +238 −0 autotests/adblock/adblockmanager/tst_adblockmanager.cpp
  5. +1 −0 autotests/adblock/adblocknetwork/.gitignore
  6. +10 −0 autotests/adblock/adblocknetwork/adblocknetwork.pro
  7. +224 −0 autotests/adblock/adblocknetwork/tst_adblocknetwork.cpp
  8. +1 −0 autotests/adblock/adblockpage/.gitignore
  9. +10 −0 autotests/adblock/adblockpage/adblockpage.pro
  10. +8 −0 autotests/adblock/adblockpage/test.html
  11. +160 −0 autotests/adblock/adblockpage/tst_adblockpage.cpp
  12. +1 −0 autotests/adblock/adblockrule/.gitignore
  13. +10 −0 autotests/adblock/adblockrule/adblockrule.pro
  14. +385 −0 autotests/adblock/adblockrule/tst_adblockrule.cpp
  15. +2 −0 autotests/adblock/adblocksubscription/.gitignore
  16. +10 −0 autotests/adblock/adblocksubscription/adblocksubscription.pro
  17. +3 −0 autotests/adblock/adblocksubscription/rules.txt
  18. +358 −0 autotests/adblock/adblocksubscription/tst_adblocksubscription.cpp
  19. +1 −0 autotests/autotests.pro
  20. +6 −0 manualtests/adblock/adblock.pro
  21. +1 −0 manualtests/adblock/adblockdialog/.gitignore
  22. +11 −0 manualtests/adblock/adblockdialog/adblockdialog.pro
  23. +44 −0 manualtests/adblock/adblockdialog/main_adblockdialog.cpp
  24. +1 −0 manualtests/adblock/adblockmodel/.gitignore
  25. +11 −0 manualtests/adblock/adblockmodel/adblockmodel.pro
  26. +46 −0 manualtests/adblock/adblockmodel/main_adblockmodel.cpp
  27. +1 −0 manualtests/manualtests.pro
  28. +28 −0 src/adblock/adblock.pri
  29. +58 −0 src/adblock/adblockblockednetworkreply.cpp
  30. +52 −0 src/adblock/adblockblockednetworkreply.h
  31. +150 −0 src/adblock/adblockdialog.cpp
  32. +61 −0 src/adblock/adblockdialog.h
  33. +127 −0 src/adblock/adblockdialog.ui
  34. +228 −0 src/adblock/adblockmanager.cpp
  35. +88 −0 src/adblock/adblockmanager.h
  36. +267 −0 src/adblock/adblockmodel.cpp
  37. +67 −0 src/adblock/adblockmodel.h
  38. +80 −0 src/adblock/adblocknetwork.cpp
  39. +48 −0 src/adblock/adblocknetwork.h
  40. +116 −0 src/adblock/adblockpage.cpp
  41. +50 −0 src/adblock/adblockpage.h
  42. +216 −0 src/adblock/adblockrule.cpp
  43. +68 −0 src/adblock/adblockrule.h
  44. +69 −0 src/adblock/adblockschemeaccesshandler.cpp
  45. +42 −0 src/adblock/adblockschemeaccesshandler.h
  46. +375 −0 src/adblock/adblocksubscription.cpp
  47. +102 −0 src/adblock/adblocksubscription.h
  48. +7 −0 src/browsermainwindow.cpp
  49. +1 −0 src/browsermainwindow.h
  50. +2 −0 src/data/128x128/run.license
  51. BIN src/data/128x128/run.png
  52. +1 −0 src/data/data.qrc
  53. +14 −0 src/network/networkaccessmanager.cpp
  54. +2 −0 src/network/networkaccessmanager.h
  55. +1 −0 src/src.pri
  56. +3 −0 src/webpage.cpp
  57. +15 −0 src/webview.cpp
  58. +1 −0 src/webview.h
@@ -0,0 +1,9 @@
TEMPLATE = subdirs
SUBDIRS = \
adblockmanager \
adblocknetwork \
adblockpage \
adblockrule \
adblocksubscription

CONFIG += ordered
@@ -0,0 +1 @@
adblockmanager
@@ -0,0 +1,10 @@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

include(../../autotests.pri)

# Input
SOURCES += tst_adblockmanager.cpp
HEADERS +=
@@ -0,0 +1,238 @@
/**
* Copyright (c) 2009, Benjamin C. Meyer <ben@meyerhome.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Benjamin Meyer nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <qtest.h>
#include <qsignalspy.h>
#include <qtry.h>

#include "adblockdialog.h"
#include "adblockmanager.h"
#include "adblocksubscription.h"

#include <qdebug.h>

class tst_AdBlockManager : public QObject
{
Q_OBJECT

public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();

private slots:
void adblockmanager_data();
void adblockmanager();

void addSubscription();
void customRules();
void isEnabled_data();
void isEnabled();
void load();
void removeSubscription();
void showDialog();
void rulesChanged();
};

// Subclass that exposes the protected functions.
class SubAdBlockManager : public AdBlockManager
{
public:
~SubAdBlockManager() {
QList<AdBlockSubscription*> list = subscriptions();
foreach (AdBlockSubscription *s, list)
removeSubscription(s);
setEnabled(false);
}

void call_rulesChanged()
{ return SubAdBlockManager::rulesChanged(); }
};

// This will be called before the first test function is executed.
// It is only called once.
void tst_AdBlockManager::initTestCase()
{
}

// This will be called after the last test function is executed.
// It is only called once.
void tst_AdBlockManager::cleanupTestCase()
{
}

// This will be called before each test function is executed.
void tst_AdBlockManager::init()
{
}

// This will be called after every test function.
void tst_AdBlockManager::cleanup()
{
}

void tst_AdBlockManager::adblockmanager_data()
{
}

void tst_AdBlockManager::adblockmanager()
{
SubAdBlockManager manager;
manager.addSubscription((AdBlockSubscription*)0);
QVERIFY(manager.customRules());
QVERIFY(manager.instance() != (AdBlockManager*)0);
QCOMPARE(manager.isEnabled(), false);
manager.load();
QVERIFY(manager.network());
QVERIFY(manager.page());
manager.removeSubscription((AdBlockSubscription*)0);
manager.setEnabled(false);
QVERIFY(manager.showDialog());
QList<AdBlockSubscription*> list;
list.append(manager.customRules());
QCOMPARE(manager.subscriptions(), list);
}

// public void addSubscription(AdBlockSubscription *subscription)
void tst_AdBlockManager::addSubscription()
{
SubAdBlockManager manager;

QList<AdBlockSubscription*> list = manager.subscriptions();

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
manager.addSubscription(subscription);
QCOMPARE(manager.subscriptions(), (list += subscription));

QCOMPARE(spy0.count(), 1);
}

// public AdBlockSubscription *customRules()
void tst_AdBlockManager::customRules()
{
SubAdBlockManager manager;
QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

AdBlockSubscription *subscription = manager.customRules();
QVERIFY(subscription);
QVERIFY(!subscription->title().isEmpty());
QVERIFY(subscription->allRules().isEmpty());

QCOMPARE(spy0.count(), 1);

subscription = manager.customRules();
QCOMPARE(spy0.count(), 1);
}

void tst_AdBlockManager::isEnabled_data()
{
QTest::addColumn<bool>("isEnabled");
QTest::newRow("true") << true;
QTest::newRow("false") << false;
}

// public bool isEnabled() const
void tst_AdBlockManager::isEnabled()
{
QFETCH(bool, isEnabled);

SubAdBlockManager manager;

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

bool before = manager.isEnabled();

manager.setEnabled(isEnabled);
manager.setEnabled(isEnabled);
QCOMPARE(manager.isEnabled(), isEnabled);

QCOMPARE(spy0.count(), before == isEnabled ? 0 : 1);
}

// public void load()
void tst_AdBlockManager::load()
{
SubAdBlockManager manager;

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

manager.load();

QCOMPARE(spy0.count(), 0);
}

// public void removeSubscription(AdBlockSubscription *subscription)
void tst_AdBlockManager::removeSubscription()
{
SubAdBlockManager manager;

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

QList<AdBlockSubscription*> list = manager.subscriptions();
AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
manager.addSubscription(subscription);
manager.removeSubscription(subscription);
QCOMPARE(manager.subscriptions(), list);

QCOMPARE(spy0.count(), 2);
}


// public AdBlockDialog *showDialog()
void tst_AdBlockManager::showDialog()
{
SubAdBlockManager manager;

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));

AdBlockDialog *dialog = manager.showDialog();
QVERIFY(dialog);
QTRY_VERIFY(dialog->isVisible());
}

void tst_AdBlockManager::rulesChanged()
{
SubAdBlockManager manager;

QSignalSpy spy0(&manager, SIGNAL(rulesChanged()));


AdBlockSubscription *subscription = new AdBlockSubscription(QUrl(), &manager);
manager.addSubscription(subscription);
subscription->setEnabled(true);
subscription->addRule(AdBlockRule());

QCOMPARE(spy0.count(), 3);
}

QTEST_MAIN(tst_AdBlockManager)
#include "tst_adblockmanager.moc"

@@ -0,0 +1 @@
adblocknetwork
@@ -0,0 +1,10 @@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

include(../../autotests.pri)

# Input
SOURCES += tst_adblocknetwork.cpp
HEADERS +=
Oops, something went wrong.

0 comments on commit fd2a265

Please sign in to comment.
You can’t perform that action at this time.