Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create a primary NetworkAcessManagerProxy class which can be tied to a
specific QWebPage and will insert a pointer to the webpage in all
QNetworkRequests before passing it to the main QNetworkAccessManager.

Using this class requires that the primary QNetworkAccessManager
is a subclass of NetworkAcessManagerProxy.
  • Loading branch information
icefox committed Sep 24, 2009
1 parent 45ad2bd commit bfb89a5
Show file tree
Hide file tree
Showing 15 changed files with 521 additions and 8 deletions.
1 change: 1 addition & 0 deletions autotests/utils/networkaccessmanagerproxy/.gitignore
@@ -0,0 +1 @@
networkaccessmanagerproxy
@@ -0,0 +1,12 @@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

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

# Input
SOURCES = tst_networkaccessmanagerproxy.cpp networkaccessmanagerproxy.cpp webpageproxy.cpp
HEADERS = networkaccessmanagerproxy.h webpageproxy.h
FORMS =
RESOURCES =
@@ -0,0 +1,146 @@
/*
* Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#include <qtest.h>

#include <networkaccessmanagerproxy.h>
#include <webpageproxy.h>

#include <qnetworkcookie.h>
#include <qnetworkrequest.h>
#include <qnetworkreply.h>

class tst_NetworkAccessManagerProxy : public QObject
{
Q_OBJECT

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

private slots:
void networkaccessmanagerproxy_data();
void networkaccessmanagerproxy();

void primaryNetworkAccessManager();
void webPage();

void createRequest_data();
void createRequest();
};

// Subclass that exposes the protected functions.
class SubNetworkAccessManagerProxy : public NetworkAccessManagerProxy
{
public:
QNetworkReply *call_createRequest(QNetworkAccessManager::Operation op, QNetworkRequest const &request, QIODevice *outgoingData = 0)
{ return SubNetworkAccessManagerProxy::createRequest(op, request, outgoingData); }
};

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

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

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

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

void tst_NetworkAccessManagerProxy::networkaccessmanagerproxy_data()
{
}

void tst_NetworkAccessManagerProxy::networkaccessmanagerproxy()
{
SubNetworkAccessManagerProxy proxy;
QVERIFY(proxy.call_createRequest(QNetworkAccessManager::GetOperation,
QNetworkRequest(),
(QIODevice*)0));
}

// public void setPrimaryNetworkAccessManager(NetworkAccessManagerProxy *primaryManager)
void tst_NetworkAccessManagerProxy::primaryNetworkAccessManager()
{
SubNetworkAccessManagerProxy proxy;

NetworkAccessManagerProxy primaryManager;

proxy.setPrimaryNetworkAccessManager(&primaryManager);
QCOMPARE(&primaryManager, proxy.primaryNetworkAccessManager());
QVERIFY(primaryManager.cookieJar()->parent() == &primaryManager);
}

// public void setWebPage(WebPageProxy *page)
void tst_NetworkAccessManagerProxy::webPage()
{
SubNetworkAccessManagerProxy proxy;

WebPageProxy webPage;
proxy.setWebPage(&webPage);
QCOMPARE(&webPage, proxy.webPage());
}

void tst_NetworkAccessManagerProxy::createRequest_data()
{
QTest::addColumn<bool>("primary");
QTest::newRow("true") << true;
QTest::newRow("false") << false;
}

// protected QNetworkReply *createRequest(QNetworkAccessManager::Operation op, QNetworkRequest const &request, QIODevice *outgoingData = 0)
void tst_NetworkAccessManagerProxy::createRequest()
{
QFETCH(bool, primary);

SubNetworkAccessManagerProxy proxy;
SubNetworkAccessManagerProxy primaryManager;
WebPageProxy webPage;
if (!primary) {
proxy.setPrimaryNetworkAccessManager(&primaryManager);
proxy.setWebPage(&webPage);
}

QIODevice *outgoingData = 0;
QNetworkRequest request;
QNetworkReply *reply = proxy.call_createRequest(QNetworkAccessManager::GetOperation, request, outgoingData);
QVERIFY(reply);
if (primary)
QCOMPARE(reply->request(), request);
else
QVERIFY(reply->request() != request);
}

QTEST_MAIN(tst_NetworkAccessManagerProxy)
#include "tst_networkaccessmanagerproxy.moc"

1 change: 1 addition & 0 deletions autotests/utils/webpageproxy/.gitignore
@@ -0,0 +1 @@
webpageproxy
103 changes: 103 additions & 0 deletions autotests/utils/webpageproxy/tst_webpageproxy.cpp
@@ -0,0 +1,103 @@
/*
* Copyright 2009 Benjamin C. Meyer <ben@meyerhome.net>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/

#include <qtest.h>

#include <webpageproxy.h>
#include <qnetworkrequest.h>

class tst_WebPageProxy : public QObject
{
Q_OBJECT

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

private slots:
void webpageproxy_data();
void webpageproxy();

void populateNetworkRequest();
};

// Subclass that exposes the protected functions.
class SubWebPageProxy : public WebPageProxy
{
public:
void call_populateNetworkRequest(QNetworkRequest &request)
{ return SubWebPageProxy::populateNetworkRequest(request); }
};

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

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

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

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

void tst_WebPageProxy::webpageproxy_data()
{
}

void tst_WebPageProxy::webpageproxy()
{
SubWebPageProxy proxy;
QCOMPARE(proxy.pageAttributeId(), 1100);
QNetworkRequest request;
proxy.call_populateNetworkRequest(request);
}

// protected void populateNetworkRequest(QNetworkRequest &request)
void tst_WebPageProxy::populateNetworkRequest()
{
SubWebPageProxy proxy;

QNetworkRequest emptyRequest;
QNetworkRequest request = emptyRequest;
proxy.call_populateNetworkRequest(request);

QVERIFY(request != emptyRequest);
QVariant v = request.attribute((QNetworkRequest::Attribute)(proxy.pageAttributeId()));
QVERIFY(v.isValid());
QWebPage *webPage = (QWebPage*)(v.value<void*>());
QVERIFY(webPage);
QCOMPARE(webPage, &proxy);
}

QTEST_MAIN(tst_WebPageProxy)
#include "tst_webpageproxy.moc"

12 changes: 12 additions & 0 deletions autotests/utils/webpageproxy/webpageproxy.pro
@@ -0,0 +1,12 @@
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

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

# Input
SOURCES = tst_webpageproxy.cpp webpageproxy.cpp
HEADERS = webpageproxy.h
FORMS =
RESOURCES =
2 changes: 1 addition & 1 deletion src/network/networkaccessmanager.cpp
Expand Up @@ -88,7 +88,7 @@
#include <qdatetime.h>

NetworkAccessManager::NetworkAccessManager(QObject *parent)
: QNetworkAccessManager(parent)
: NetworkAccessManagerProxy(parent)
, m_adblockNetwork(0)
{
connect(this, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
Expand Down
3 changes: 2 additions & 1 deletion src/network/networkaccessmanager.h
Expand Up @@ -65,11 +65,12 @@

#include <qnetworkaccessmanager.h>
#include <qsslconfiguration.h>
#include "networkaccessmanagerproxy.h"

class SchemeAccessHandler;

class AdBlockNetwork;
class NetworkAccessManager : public QNetworkAccessManager
class NetworkAccessManager : public NetworkAccessManagerProxy
{
Q_OBJECT

Expand Down
72 changes: 72 additions & 0 deletions src/utils/networkaccessmanagerproxy.cpp
@@ -0,0 +1,72 @@
/**
* 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 "networkaccessmanagerproxy.h"

#include "webpageproxy.h"

#include <qnetworkcookie.h>
#include <qnetworkrequest.h>

NetworkAccessManagerProxy *NetworkAccessManagerProxy::m_primaryManager = 0;

/*
NetworkAccessManagerProxy inserts a pointer to the QWebPage in the
QNetworkRequest before passing it on.
*/
NetworkAccessManagerProxy::NetworkAccessManagerProxy(QObject *parent)
: QNetworkAccessManager(parent)
, m_webPage(0)
{
}

void NetworkAccessManagerProxy::setWebPage(WebPageProxy *page)
{
Q_ASSERT(page);
m_webPage = page;
}

void NetworkAccessManagerProxy::setPrimaryNetworkAccessManager(NetworkAccessManagerProxy *manager)
{
Q_ASSERT(manager);
m_primaryManager = manager;
setCookieJar(m_primaryManager->cookieJar());
// Do not steal ownership!
cookieJar()->setParent(manager);
}

QNetworkReply *NetworkAccessManagerProxy::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
{
if (m_primaryManager && m_webPage) {
QNetworkRequest pageRequest = request;
m_webPage->populateNetworkRequest(pageRequest);
return m_primaryManager->createRequest(op, pageRequest, outgoingData);
}
return QNetworkAccessManager::createRequest(op, request, outgoingData);
}

0 comments on commit bfb89a5

Please sign in to comment.