7#include "hashcachemanager.h"
8#include "checkphishingurlutil.h"
9#include "webengineviewer_debug.h"
11#include <KConfigGroup>
13using namespace WebEngineViewer;
16 HashCacheInfo() =
default;
18 [[nodiscard]]
bool isValid()
const;
19 HashCacheManager::UrlStatus status = HashCacheManager::Unknown;
20 uint verifyCacheAfterThisTime = 0;
23bool HashCacheInfo::isValid()
const
25 return status != HashCacheManager::Unknown;
28class WebEngineViewer::HashCacheManagerPrivate
31 HashCacheManagerPrivate()
39 void addHashStatus(
const QByteArray &hash, HashCacheManager::UrlStatus
status, uint cacheDuration);
40 [[nodiscard]] HashCacheManager::UrlStatus hashStatus(
const QByteArray &hash);
45 QMap<QByteArray, HashCacheInfo> mHashList;
48void HashCacheManagerPrivate::clear()
53void HashCacheManagerPrivate::clearCache()
59void HashCacheManagerPrivate::save()
61 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
62 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral(
"Hash"));
64 QList<QByteArray> lstMalware;
65 QList<double> lstMalwareDuration;
67 QList<QByteArray> lstOk;
68 QList<double> lstOkDuration;
70 QMapIterator<QByteArray, HashCacheInfo> i(mHashList);
73 switch (i.value().status) {
74 case HashCacheManager::UrlOk:
76 lstOkDuration << i.
value().verifyCacheAfterThisTime;
78 case HashCacheManager::MalWare:
79 lstMalware << i.key();
80 lstMalwareDuration << i.
value().verifyCacheAfterThisTime;
82 case HashCacheManager::Unknown:
87 grp.
writeEntry(
"malwareCacheDuration", lstMalwareDuration);
90 grp.
writeEntry(
"safeCacheDuration", lstOkDuration);
94void HashCacheManagerPrivate::load()
97 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
98 KConfigGroup grp = phishingurlKConfig.
group(QStringLiteral(
"Hash"));
99 QList<QByteArray> lstMalware = grp.
readEntry(
"malware", QList<QByteArray>());
100 QList<double> lstMalwareDuration = grp.
readEntry(
"malwareCacheDuration", QList<double>());
102 QList<QByteArray> lstOk = grp.
readEntry(
"safe", QList<QByteArray>());
103 QList<double> lstOkDuration = grp.
readEntry(
"safeCacheDuration", QList<double>());
104 if (lstMalware.
count() != lstMalwareDuration.
count()) {
105 qCWarning(WEBENGINEVIEWER_LOG) <<
"unsafe url: HashCacheManagerPrivate invalid number of data stored";
109 qCWarning(WEBENGINEVIEWER_LOG) <<
"safe url: HashCacheManagerPrivate invalid number of data stored";
113 const int nb(lstOk.
count());
114 for (
int i = 0; i < nb; ++i) {
116 info.status = HashCacheManager::UrlOk;
117 info.verifyCacheAfterThisTime = lstOkDuration.
at(i);
118 if (info.isValid()) {
119 mHashList.insert(lstOk.
at(i), info);
123 const int nb2(lstMalware.
count());
124 for (
int i = 0; i < nb2; ++i) {
126 info.status = HashCacheManager::MalWare;
127 info.verifyCacheAfterThisTime = lstMalwareDuration.
at(i);
128 if (info.isValid()) {
129 mHashList.insert(lstMalware.
at(i), info);
134HashCacheManager::UrlStatus HashCacheManagerPrivate::hashStatus(
const QByteArray &hash)
136 const HashCacheInfo info = mHashList.value(hash, HashCacheInfo());
137 if (info.isValid()) {
138 if (CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
141 return HashCacheManager::Unknown;
144 return HashCacheManager::Unknown;
148void HashCacheManagerPrivate::addHashStatus(
const QByteArray &hash, HashCacheManager::UrlStatus
status, uint cacheDuration)
152 info.verifyCacheAfterThisTime = cacheDuration;
154 case HashCacheManager::UrlOk:
155 mHashList.insert(hash, info);
157 case HashCacheManager::MalWare:
158 mHashList.insert(hash, info);
160 case HashCacheManager::Unknown:
161 qCWarning(WEBENGINEVIEWER_LOG()) <<
"HashCacheManagerPrivate::addHashStatus unknown status detected!";
169 static HashCacheManager s_self;
173HashCacheManager::HashCacheManager(QObject *parent)
175 , d(new HashCacheManagerPrivate)
179HashCacheManager::~HashCacheManager() =
default;
181void HashCacheManager::clearCache()
186void HashCacheManager::addHashStatus(
const QByteArray &hash, HashCacheManager::UrlStatus
status, uint cacheDuration)
188 d->addHashStatus(hash,
status, cacheDuration);
191HashCacheManager::UrlStatus HashCacheManager::hashStatus(
const QByteArray &hash)
193 return d->hashStatus(hash);
196#include "moc_hashcachemanager.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
The HashCacheManager class.
Q_SCRIPTABLE CaptureState status()
const_reference at(qsizetype i) const const
qsizetype count() const const
T value(qsizetype i) const const