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

source code of qtbase/src/network/access/qnetworkaccesscache_p.h