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/qhash.h" |
23 | #include "QtCore/qmetatype.h" |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QNetworkRequest; |
28 | class QUrl; |
29 | |
30 | // this class is not about caching files but about |
31 | // caching objects used by QNetworkAccessManager, e.g. existing TCP connections |
32 | // or credentials. |
33 | class QNetworkAccessCache: public QObject |
34 | { |
35 | Q_OBJECT |
36 | public: |
37 | struct Node; |
38 | typedef QHash<QByteArray, Node *> NodeHash; |
39 | |
40 | class CacheableObject |
41 | { |
42 | friend class QNetworkAccessCache; |
43 | QByteArray key; |
44 | bool expires; |
45 | bool shareable; |
46 | qint64 expiryTimeoutSeconds; |
47 | public: |
48 | CacheableObject(); |
49 | virtual ~CacheableObject(); |
50 | virtual void dispose() = 0; |
51 | inline QByteArray cacheKey() const { return key; } |
52 | |
53 | protected: |
54 | void setExpires(bool enable); |
55 | void setShareable(bool enable); |
56 | }; |
57 | |
58 | ~QNetworkAccessCache(); |
59 | |
60 | void clear(); |
61 | |
62 | void addEntry(const QByteArray &key, CacheableObject *entry, qint64 connectionCacheExpiryTimeoutSeconds = -1); |
63 | bool hasEntry(const QByteArray &key) const; |
64 | CacheableObject *requestEntryNow(const QByteArray &key); |
65 | void releaseEntry(const QByteArray &key); |
66 | void removeEntry(const QByteArray &key); |
67 | |
68 | signals: |
69 | void entryReady(QNetworkAccessCache::CacheableObject *); |
70 | |
71 | protected: |
72 | void timerEvent(QTimerEvent *) override; |
73 | |
74 | private: |
75 | // idea copied from qcache.h |
76 | NodeHash hash; |
77 | Node *firstExpiringNode = nullptr; |
78 | Node *lastExpiringNode = nullptr; |
79 | |
80 | QBasicTimer timer; |
81 | |
82 | void linkEntry(const QByteArray &key); |
83 | bool unlinkEntry(const QByteArray &key); |
84 | void updateTimer(); |
85 | bool emitEntryReady(Node *node, QObject *target, const char *member); |
86 | }; |
87 | |
88 | QT_END_NAMESPACE |
89 | |
90 | #endif |
91 | |