Commit 76ec6902 authored by Laurent Montel's avatar Laurent Montel 😁

Implement job

parent 4ec792b7
......@@ -15,11 +15,36 @@
*/
#include "adblockblockableitemsjob.h"
#include <WebEngineViewer/WebEngineScript>
#include <QDebug>
#include <QWebEngineView>
#include <QWebEnginePage>
#include <QPointer>
using namespace AdBlock;
template<typename Arg, typename R, typename C>
struct InvokeWrapper {
QPointer<R> receiver;
void (C::*memberFunction)(Arg);
void operator()(Arg result)
{
if (receiver) {
(receiver->*memberFunction)(result);
}
}
};
template<typename Arg, typename R, typename C>
InvokeWrapper<Arg, R, C> invoke(R *receiver, void (C::*memberFunction)(Arg))
{
InvokeWrapper<Arg, R, C> wrapper = {receiver, memberFunction};
return wrapper;
}
AdBlockBlockableItemsJob::AdBlockBlockableItemsJob(QObject *parent)
: QObject(parent)
: QObject(parent),
mWebEngineView(Q_NULLPTR)
{
}
......@@ -28,3 +53,93 @@ AdBlockBlockableItemsJob::~AdBlockBlockableItemsJob()
{
}
void AdBlockBlockableItemsJob::setWebEngineView(QWebEngineView *webEngineView)
{
mWebEngineView = webEngineView;
}
void AdBlockBlockableItemsJob::searchBlockableItems()
{
if (mWebEngineView) {
mWebEngineView->page()->runJavaScript(WebEngineViewer::WebEngineScript::findAllImages(), invoke(this, &AdBlockBlockableItemsJob::handleSearchBlockableImageItems));
} else {
Q_EMIT searchItemsDone(QVector<AdBlock::AdBlockResult>());
deleteLater();
}
}
void AdBlockBlockableItemsJob::handleSearchBlockableImageItems(const QVariant &result)
{
const QList<QVariant> lst = result.toList();
const QUrl url = mWebEngineView->url();
const QString host = url.host();
Q_FOREACH (const QVariant &var, lst) {
QMap<QString, QVariant> mapVariant = var.toMap();
QString src = mapVariant.value(QStringLiteral("src")).toString();
if (!src.isEmpty()) {
adaptSource(src, host);
if (src.isEmpty()) {
continue;
}
AdBlock::AdBlockResult result;
result.src = src;
result.type = Image;
if (!mAdblockResultList.contains(result)) {
mAdblockResultList.append(result);
}
}
}
mWebEngineView->page()->runJavaScript(WebEngineViewer::WebEngineScript::findAllScripts(), invoke(this, &AdBlockBlockableItemsJob::handleSearchBlockableScriptsItems));
}
QWebEngineView *AdBlockBlockableItemsJob::webEngineView() const
{
return mWebEngineView;
}
void AdBlockBlockableItemsJob::start()
{
mAdblockResultList.clear();
searchBlockableItems();
}
void AdBlockBlockableItemsJob::adaptSource(QString &src, const QString &hostName)
{
if (src.startsWith(QStringLiteral("http://")) || src.startsWith(QStringLiteral("https://"))) {
//Nothing
} else if (src.startsWith(QStringLiteral("//"))) {
src = QLatin1String("https:") + src;
} else if (src.startsWith(QLatin1Char('/'))) {
src = QLatin1String("https://") + hostName + src;
} else {
src = QString();
}
}
void AdBlockBlockableItemsJob::handleSearchBlockableScriptsItems(const QVariant &result)
{
const QList<QVariant> lst = result.toList();
const QUrl url = mWebEngineView->url();
const QString host = url.host();
Q_FOREACH (const QVariant &var, lst) {
QMap<QString, QVariant> mapVariant = var.toMap();
QString src = mapVariant.value(QStringLiteral("src")).toString();
if (!src.isEmpty()) {
adaptSource(src, host);
if (src.isEmpty()) {
continue;
}
AdBlock::AdBlockResult result;
result.src = src;
result.type = Script;
if (!mAdblockResultList.contains(result)) {
mAdblockResultList.append(result);
}
}
}
//TODO more check ?
Q_EMIT searchItemsDone(mAdblockResultList);
deleteLater();
}
......@@ -18,17 +18,61 @@
#define ADBLOCKBLOCKABLEITEMSJOB_H
#include <QObject>
#include <QVector>
class QWebEngineView;
namespace AdBlock
{
struct AdBlockResult;
class AdBlockBlockableItemsJob : public QObject
{
Q_OBJECT
public:
enum TypeElement {
UnKnown = 0,
Image,
Script
};
explicit AdBlockBlockableItemsJob(QObject *parent = Q_NULLPTR);
~AdBlockBlockableItemsJob();
void setWebEngineView(QWebEngineView *webEngineView);
QWebEngineView *webEngineView() const;
void start();
Q_SIGNALS:
void searchItemsDone();
void searchItemsDone(const QVector<AdBlock::AdBlockResult> &);
private Q_SLOTS:
void handleSearchBlockableImageItems(const QVariant &result);
void handleSearchBlockableScriptsItems(const QVariant &result);
private:
void searchBlockableItems();
void adaptSource(QString &src, const QString &hostName);
QVector<AdBlock::AdBlockResult> mAdblockResultList;
QWebEngineView *mWebEngineView;
};
struct AdBlockResult
{
AdBlockResult()
: type(AdBlockBlockableItemsJob::UnKnown)
{
}
bool operator==(const AdBlockResult &other) const {
return (src == other.src) && (type == other.type);
}
QString src;
AdBlockBlockableItemsJob::TypeElement type;
};
}
Q_DECLARE_TYPEINFO(AdBlock::AdBlockResult, Q_MOVABLE_TYPE);
#endif // ADBLOCKBLOCKABLEITEMSJOB_H
......@@ -16,3 +16,10 @@ add_test(adblockblockableitemsdialogtest adblockblockableitemsdialogtest)
ecm_mark_as_test(adblockblockableitemsdialogtest)
target_link_libraries( adblockblockableitemsdialogtest adblocklibprivate Qt5::Test Qt5::Widgets KF5::I18n KF5::MessageViewer
KF5::PimCommon KF5::ItemViews Qt5::WebEngine Qt5::WebEngineWidgets KF5::ConfigCore KF5::KIOWidgets KF5::WebEngineViewer)
set( adblockblockableitemsjobtest_SRCS ${messageviewer_adblock_test_common_SRCS} adblockblockableitemsjobtest.cpp ../adblockblockableitemsjob.cpp)
add_executable( adblockblockableitemsjobtest ${adblockblockableitemsjobtest_SRCS})
add_test(adblockblockableitemsjobtest adblockblockableitemsjobtest)
ecm_mark_as_test(adblockblockableitemsjobtest)
target_link_libraries( adblockblockableitemsjobtest adblocklibprivate Qt5::Test Qt5::WebEngine Qt5::WebEngineWidgets KF5::I18n KF5::WebEngineViewer)
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "adblockblockableitemsjobtest.h"
#include "../adblockblockableitemsjob.h"
#include <QTest>
#include <QSignalSpy>
Q_DECLARE_METATYPE(QVector<AdBlock::AdBlockResult>)
AdBlockBlockableItemsJobTest::AdBlockBlockableItemsJobTest(QObject *parent)
: QObject(parent)
{
qRegisterMetaType<QVector<AdBlock::AdBlockResult> >();
}
AdBlockBlockableItemsJobTest::~AdBlockBlockableItemsJobTest()
{
}
void AdBlockBlockableItemsJobTest::shouldHaveDefaultValue()
{
AdBlock::AdBlockBlockableItemsJob job;
QVERIFY(!job.webEngineView());
}
void AdBlockBlockableItemsJobTest::shouldReturnEmptyListWhenWebEngineIsNotSet()
{
AdBlock::AdBlockBlockableItemsJob job;
QSignalSpy blockableItemsSpy(&job, SIGNAL(searchItemsDone(QVector<AdBlock::AdBlockResult>)));
job.start();
QCOMPARE(blockableItemsSpy.count(), 1);
const QVector<AdBlock::AdBlockResult> resultLst = blockableItemsSpy.at(0).at(0).value<QVector<AdBlock::AdBlockResult> >();
QCOMPARE(resultLst.count(), 0);
}
QTEST_MAIN(AdBlockBlockableItemsJobTest)
/*
Copyright (c) 2016 Montel Laurent <montel@kde.org>
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ADBLOCKBLOCKABLEITEMSJOBTEST_H
#define ADBLOCKBLOCKABLEITEMSJOBTEST_H
#include <QObject>
class AdBlockBlockableItemsJobTest : public QObject
{
Q_OBJECT
public:
explicit AdBlockBlockableItemsJobTest(QObject *parent = Q_NULLPTR);
~AdBlockBlockableItemsJobTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldReturnEmptyListWhenWebEngineIsNotSet();
};
#endif // ADBLOCKBLOCKABLEITEMSJOBTEST_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment