Messagelib

checkphishingurlcache.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "checkphishingurlcache.h"
8#include "checkphishingurlutil.h"
9#include "webengineviewer_debug.h"
10#include <KConfig>
11#include <KConfigGroup>
12#include <QMap>
13
14using namespace WebEngineViewer;
15
16struct UrlCacheInfo {
17 UrlCacheInfo() = default;
18
19 [[nodiscard]] bool isMalWare() const;
20 [[nodiscard]] bool isValid() const;
21 CheckPhishingUrlCache::UrlStatus status = CheckPhishingUrlCache::UrlStatus::Unknown;
22 uint verifyCacheAfterThisTime = 0;
23};
24
25bool UrlCacheInfo::isMalWare() const
26{
27 return status == CheckPhishingUrlCache::UrlStatus::MalWare;
28}
29
30bool UrlCacheInfo::isValid() const
31{
32 return status != CheckPhishingUrlCache::UrlStatus::Unknown;
33}
34
35class WebEngineViewer::CheckPhishingUrlCachePrivate
36{
37public:
38 CheckPhishingUrlCachePrivate()
39 {
40 load();
41 }
42
43 [[nodiscard]] CheckPhishingUrlCache::UrlStatus urlStatus(const QUrl &url);
44 void addCheckPhishingUrlResult(const QUrl &url, bool correctUrl, uint verifyCacheAfterThisTime);
45 void clearCache();
46 void load();
47 void save();
48
49private:
50 QMap<QUrl, UrlCacheInfo> mCacheCheckedUrl;
51};
52
53void CheckPhishingUrlCachePrivate::clearCache()
54{
55 mCacheCheckedUrl.clear();
56 save();
57}
58
59void CheckPhishingUrlCachePrivate::load()
60{
61 mCacheCheckedUrl.clear();
62 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
63 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("MalwareUrl"));
64 const QList<QUrl> listMalware = grp.readEntry("Url", QList<QUrl>());
65 const QList<double> listMalwareCachedTime = grp.readEntry("CachedTime", QList<double>());
66 if (listMalware.count() != listMalwareCachedTime.count()) {
67 qCWarning(WEBENGINEVIEWER_LOG) << "CheckPhishingUrlCachePrivate invalid number of data stored";
68 } else {
69 UrlCacheInfo info;
70 const int numberOfMalware = listMalware.count();
71 for (int i = 0; i < numberOfMalware; ++i) {
72 info.status = WebEngineViewer::CheckPhishingUrlCache::UrlStatus::MalWare;
73 info.verifyCacheAfterThisTime = listMalwareCachedTime.at(i);
74 if (WebEngineViewer::CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
75 mCacheCheckedUrl.insert(listMalware.at(i), info);
76 }
77 }
78 }
79}
80
81void CheckPhishingUrlCachePrivate::save()
82{
83 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
84 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("MalwareUrl"));
85
86 QList<QUrl> listMalware;
87 QList<double> listMalwareCachedTime;
88
90 const QMap<QUrl, UrlCacheInfo>::const_iterator end = mCacheCheckedUrl.constEnd();
91 while (i != end) {
92 const UrlCacheInfo info = i.value();
93 if (info.isMalWare() && WebEngineViewer::CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
94 listMalware.append(i.key());
95 listMalwareCachedTime.append(info.verifyCacheAfterThisTime);
96 }
97 ++i;
98 }
99 grp.writeEntry("Url", listMalware);
100 grp.writeEntry("CachedTime", listMalwareCachedTime);
101 grp.sync();
102}
103
104CheckPhishingUrlCache::UrlStatus CheckPhishingUrlCachePrivate::urlStatus(const QUrl &url)
105{
106 const UrlCacheInfo info = mCacheCheckedUrl.value(url, UrlCacheInfo());
107 if (info.isValid()) {
108 if (info.verifyCacheAfterThisTime > 0) {
109 if (CheckPhishingUrlUtil::cachedValueStillValid(info.verifyCacheAfterThisTime)) {
110 return info.status;
111 } else {
112 return CheckPhishingUrlCache::UrlStatus::Unknown;
113 }
114 } else {
115 return info.status;
116 }
117 } else {
118 return CheckPhishingUrlCache::UrlStatus::Unknown;
119 }
120}
121
122void CheckPhishingUrlCachePrivate::addCheckPhishingUrlResult(const QUrl &url, bool correctUrl, uint verifyCacheAfterThisTime)
123{
124 UrlCacheInfo info;
125 info.status = correctUrl ? CheckPhishingUrlCache::UrlStatus::UrlOk : CheckPhishingUrlCache::UrlStatus::MalWare;
126 info.verifyCacheAfterThisTime = correctUrl ? 0 : verifyCacheAfterThisTime;
127 mCacheCheckedUrl.insert(url, info);
128 if (info.status == CheckPhishingUrlCache::UrlStatus::MalWare) {
129 save();
130 }
131}
132
133CheckPhishingUrlCache::CheckPhishingUrlCache(QObject *parent)
134 : QObject(parent)
135 , d(new CheckPhishingUrlCachePrivate)
136{
137}
138
139CheckPhishingUrlCache::~CheckPhishingUrlCache() = default;
140
141void CheckPhishingUrlCache::addCheckingUrlResult(const QUrl &url, bool correctUrl, uint verifyCacheAfterThisTime)
142{
143 d->addCheckPhishingUrlResult(url, correctUrl, verifyCacheAfterThisTime);
144}
145
146CheckPhishingUrlCache::UrlStatus CheckPhishingUrlCache::urlStatus(const QUrl &url)
147{
148 return d->urlStatus(url);
149}
150
152{
153 d->clearCache();
154}
155
156CheckPhishingUrlCache *CheckPhishingUrlCache::self()
157{
158 static CheckPhishingUrlCache s_self;
159 return &s_self;
160}
161
162#include "moc_checkphishingurlcache.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
bool sync() override
The CheckPhishingUrlCache class.
CheckPhishingUrlCache::UrlStatus urlStatus(const QUrl &url)
urlStatus Return the status of cached Url.
void addCheckingUrlResult(const QUrl &url, bool correctUrl, uint cacheDuration=0)
addCheckingUrlResult cache url.
void clearCache()
clearCache clear the cache and save result in config file.
const QList< QKeySequence > & end()
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
void clear()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.