HSTS policy - fix API

API-review follow-up:

1. make a ctor explicit
2. add swap member-function
3. make move-assignment inlined
4. make comparison operators non-members
5. make d_ptr QSharedDataPointer (and private implementation - QSharedData).

Change-Id: I3257ca03cccd0f1254c9b95461752911359352a5
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Timur Pocheptsov 2017-02-23 13:40:30 +01:00
parent e6d23860f0
commit 14db1d5560
2 changed files with 30 additions and 27 deletions

View File

@ -63,7 +63,7 @@ QT_BEGIN_NAMESPACE
\sa QNetworkAccessManager::setStrictTransportSecurityEnabled()
*/
class QHstsPolicyPrivate
class QHstsPolicyPrivate : public QSharedData
{
public:
QUrl url;
@ -77,6 +77,15 @@ public:
}
};
/*!
Returns \c true if the two policies have the same host and expiration date
while agreeing on whether to include or exclude subdomains.
*/
bool operator==(const QHstsPolicy &lhs, const QHstsPolicy &rhs)
{
return *lhs.d == *rhs.d;
}
/*!
Constructs an invalid (expired) policy with empty host name and subdomains
not included.
@ -121,17 +130,7 @@ QHstsPolicy::~QHstsPolicy()
*/
QHstsPolicy &QHstsPolicy::operator=(const QHstsPolicy &other)
{
*d = *other.d;
return *this;
}
/*!
Move-assignment operator.
*/
QHstsPolicy &QHstsPolicy::operator=(QHstsPolicy &&other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
d = other.d;
return *this;
}
@ -195,15 +194,6 @@ bool QHstsPolicy::includesSubDomains() const
return d->includeSubDomains;
}
/*!
Returns \c true if the two policies have the same host and expiration date
while agreeing on whether to include or exclude subdomains.
*/
bool QHstsPolicy::operator==(const QHstsPolicy &other) const
{
return *d == *other.d;
}
/*!
Return \c true if this policy has a valid expiration date and this date
is greater than QDateTime::currentGetDateTimeUtc().

View File

@ -42,7 +42,7 @@
#include <QtNetwork/qtnetworkglobal.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qurl.h>
QT_BEGIN_NAMESPACE
@ -55,13 +55,15 @@ class Q_NETWORK_EXPORT QHstsPolicy
public:
QHstsPolicy();
QHstsPolicy(const QDateTime &expiry, bool includeSubDomains, const QString &host,
QUrl::ParsingMode mode = QUrl::DecodedMode);
explicit QHstsPolicy(const QDateTime &expiry, bool includeSubDomains, const QString &host,
QUrl::ParsingMode mode = QUrl::DecodedMode);
QHstsPolicy(const QHstsPolicy &rhs);
QHstsPolicy &operator=(const QHstsPolicy &rhs);
QHstsPolicy &operator=(QHstsPolicy &&rhs) Q_DECL_NOTHROW;
QHstsPolicy &operator=(QHstsPolicy &&other) Q_DECL_NOTHROW { swap(other); return *this; }
~QHstsPolicy();
void swap(QHstsPolicy &other) Q_DECL_NOTHROW { qSwap(d, other.d); }
void setHost(const QString &host, QUrl::ParsingMode mode = QUrl::DecodedMode);
QString host(QUrl::ComponentFormattingOptions options = QUrl::FullyDecoded) const;
void setExpiry(const QDateTime &expiry);
@ -69,14 +71,25 @@ public:
void setIncludesSubDomains(bool include);
bool includesSubDomains() const;
bool operator==(const QHstsPolicy &rhs) const;
bool isExpired() const;
private:
QScopedPointer<QHstsPolicyPrivate> d;
QSharedDataPointer<QHstsPolicyPrivate> d;
friend Q_NETWORK_EXPORT bool operator==(const QHstsPolicy &lhs, const QHstsPolicy &rhs);
};
Q_DECLARE_SHARED(QHstsPolicy)
Q_NETWORK_EXPORT bool operator==(const QHstsPolicy &lhs, const QHstsPolicy &rhs);
inline bool operator!=(const QHstsPolicy &lhs, const QHstsPolicy &rhs)
{
return !(lhs == rhs);
}
QT_END_NAMESPACE
#endif // QHSTSPOLICY_H