| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QNETWORKACCESSCACHE_P_H |
| 5 | #define QNETWORKACCESSCACHE_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of the Network Access API. This header file may change from |
| 13 | // version to version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 19 | #include "QtCore/qobject.h" |
| 20 | #include "QtCore/qbasictimer.h" |
| 21 | #include "QtCore/qbytearray.h" |
| 22 | #include <QtCore/qflags.h> |
| 23 | #include "QtCore/qhash.h" |
| 24 | #include "QtCore/qmetatype.h" |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QNetworkRequest; |
| 29 | class QUrl; |
| 30 | |
| 31 | // this class is not about caching files but about |
| 32 | // caching objects used by QNetworkAccessManager, e.g. existing TCP connections |
| 33 | // or credentials. |
| 34 | class QNetworkAccessCache: public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | public: |
| 38 | struct Node; |
| 39 | typedef QHash<QByteArray, Node *> NodeHash; |
| 40 | class CacheableObject |
| 41 | { |
| 42 | friend class QNetworkAccessCache; |
| 43 | QByteArray key; |
| 44 | bool expires; |
| 45 | bool shareable; |
| 46 | qint64 expiryTimeoutSeconds = -1; |
| 47 | public: |
| 48 | enum class Option { |
| 49 | Expires = 0x01, |
| 50 | Shareable = 0x02, |
| 51 | }; |
| 52 | typedef QFlags<Option> Options; // #### QTBUG-127269 |
| 53 | |
| 54 | virtual ~CacheableObject(); |
| 55 | virtual void dispose() = 0; |
| 56 | inline QByteArray cacheKey() const { return key; } |
| 57 | protected: |
| 58 | explicit CacheableObject(Options options); |
| 59 | }; |
| 60 | |
| 61 | ~QNetworkAccessCache(); |
| 62 | |
| 63 | void clear(); |
| 64 | |
| 65 | void addEntry(const QByteArray &key, CacheableObject *entry, qint64 connectionCacheExpiryTimeoutSeconds = -1); |
| 66 | bool hasEntry(const QByteArray &key) const; |
| 67 | CacheableObject *requestEntryNow(const QByteArray &key); |
| 68 | void releaseEntry(const QByteArray &key); |
| 69 | void removeEntry(const QByteArray &key); |
| 70 | |
| 71 | signals: |
| 72 | void entryReady(QNetworkAccessCache::CacheableObject *); |
| 73 | |
| 74 | protected: |
| 75 | void timerEvent(QTimerEvent *) override; |
| 76 | |
| 77 | private: |
| 78 | // idea copied from qcache.h |
| 79 | NodeHash hash; |
| 80 | Node *firstExpiringNode = nullptr; |
| 81 | Node *lastExpiringNode = nullptr; |
| 82 | |
| 83 | QBasicTimer timer; |
| 84 | |
| 85 | void linkEntry(const QByteArray &key); |
| 86 | bool unlinkEntry(const QByteArray &key); |
| 87 | void updateTimer(); |
| 88 | bool emitEntryReady(Node *node, QObject *target, const char *member); |
| 89 | }; |
| 90 | |
| 91 | Q_DECLARE_OPERATORS_FOR_FLAGS(QNetworkAccessCache::CacheableObject::Options) |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |
| 95 | #endif |
| 96 | |