From 606e1150e381e0d824e6850befb101a2ed8e0542 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 9 Oct 2009 12:03:42 +0200 Subject: Add a way to allow tracking the originating object with QNetworkRequest. Added setOriginatingObject() and originatingObject() to QNetworkRequest that internally tracks the QObject using a QWeakPointer. Reviewed-by: Lars Knoll Rubberstamped-by: Thiago --- src/network/access/qnetworkrequest.cpp | 30 ++++++++++++++++++++++ src/network/access/qnetworkrequest.h | 3 +++ src/network/access/qnetworkrequest_p.h | 2 ++ tests/auto/qnetworkrequest/tst_qnetworkrequest.cpp | 16 ++++++++++++ 4 files changed, 51 insertions(+) diff --git a/src/network/access/qnetworkrequest.cpp b/src/network/access/qnetworkrequest.cpp index cde9858..ca9e606 100644 --- a/src/network/access/qnetworkrequest.cpp +++ b/src/network/access/qnetworkrequest.cpp @@ -481,6 +481,36 @@ void QNetworkRequest::setSslConfiguration(const QSslConfiguration &config) } #endif +/*! + \since 4.6 + + Allows setting a reference to the object initiating + the request. + + For example QtWebKit sets the originating object to the + QWebFrame that initiated the request. + + \sa originatingObject() +*/ +void QNetworkRequest::setOriginatingObject(QObject *object) +{ + d->originatingObject = object; +} + +/*! + \since 4.6 + + Returns a reference to the object that initiated this + network request; returns 0 if not set or the object has + been destroyed. + + \sa setOriginatingObject() +*/ +QObject *QNetworkRequest::originatingObject() const +{ + return d->originatingObject.data(); +} + static QByteArray headerName(QNetworkRequest::KnownHeaders header) { switch (header) { diff --git a/src/network/access/qnetworkrequest.h b/src/network/access/qnetworkrequest.h index 7b15237..62c6dda 100644 --- a/src/network/access/qnetworkrequest.h +++ b/src/network/access/qnetworkrequest.h @@ -120,6 +120,9 @@ public: void setSslConfiguration(const QSslConfiguration &configuration); #endif + void setOriginatingObject(QObject *object); + QObject *originatingObject() const; + private: QSharedDataPointer d; friend class QNetworkRequestPrivate; diff --git a/src/network/access/qnetworkrequest_p.h b/src/network/access/qnetworkrequest_p.h index 22b239f..9b3632f 100644 --- a/src/network/access/qnetworkrequest_p.h +++ b/src/network/access/qnetworkrequest_p.h @@ -58,6 +58,7 @@ #include "QtCore/qlist.h" #include "QtCore/qhash.h" #include "QtCore/qshareddata.h" +#include "QtCore/qsharedpointer.h" QT_BEGIN_NAMESPACE @@ -73,6 +74,7 @@ public: RawHeadersList rawHeaders; CookedHeadersMap cookedHeaders; AttributesMap attributes; + QWeakPointer originatingObject; RawHeadersList::ConstIterator findRawHeader(const QByteArray &key) const; QList rawHeadersKeys() const; diff --git a/tests/auto/qnetworkrequest/tst_qnetworkrequest.cpp b/tests/auto/qnetworkrequest/tst_qnetworkrequest.cpp index 3f9632c..2e21087 100644 --- a/tests/auto/qnetworkrequest/tst_qnetworkrequest.cpp +++ b/tests/auto/qnetworkrequest/tst_qnetworkrequest.cpp @@ -66,6 +66,7 @@ private slots: void setHeader(); void rawHeaderParsing_data(); void rawHeaderParsing(); + void originatingObject(); void removeHeader(); }; @@ -476,5 +477,20 @@ void tst_QNetworkRequest::removeHeader() QVERIFY(request.hasRawHeader("bar")); } +void tst_QNetworkRequest::originatingObject() +{ + QNetworkRequest request; + + QVERIFY(!request.originatingObject()); + + { + QObject dummy; + request.setOriginatingObject(&dummy); + QCOMPARE(request.originatingObject(), &dummy); + } + + QVERIFY(!request.originatingObject()); +} + QTEST_MAIN(tst_QNetworkRequest) #include "tst_qnetworkrequest.moc" -- cgit v0.12