Messagelib

checkphishingurlcache.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
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 
14 using namespace WebEngineViewer;
15 
16 struct UrlCacheInfo {
17  UrlCacheInfo() = default;
18 
19  Q_REQUIRED_RESULT bool isMalWare() const;
20  Q_REQUIRED_RESULT bool isValid() const;
21  CheckPhishingUrlCache::UrlStatus status = CheckPhishingUrlCache::Unknown;
22  uint verifyCacheAfterThisTime = 0;
23 };
24 
25 bool UrlCacheInfo::isMalWare() const
26 {
27  return status == CheckPhishingUrlCache::MalWare;
28 }
29 
30 bool UrlCacheInfo::isValid() const
31 {
32  return status != CheckPhishingUrlCache::Unknown;
33 }
34 
35 class WebEngineViewer::CheckPhishingUrlCachePrivate
36 {
37 public:
38  CheckPhishingUrlCachePrivate()
39  {
40  load();
41  }
42 
43  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 
49 private:
50  QMap<QUrl, UrlCacheInfo> mCacheCheckedUrl;
51 };
52 
53 void CheckPhishingUrlCachePrivate::clearCache()
54 {
55  mCacheCheckedUrl.clear();
56  save();
57 }
58 
59 void 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::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 
81 void 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 
104 CheckPhishingUrlCache::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::Unknown;
113  }
114  } else {
115  return info.status;
116  }
117  } else {
118  return CheckPhishingUrlCache::Unknown;
119  }
120 }
121 
122 void CheckPhishingUrlCachePrivate::addCheckPhishingUrlResult(const QUrl &url, bool correctUrl, uint verifyCacheAfterThisTime)
123 {
124  UrlCacheInfo info;
125  info.status = correctUrl ? CheckPhishingUrlCache::UrlOk : CheckPhishingUrlCache::MalWare;
126  info.verifyCacheAfterThisTime = correctUrl ? 0 : verifyCacheAfterThisTime;
127  mCacheCheckedUrl.insert(url, info);
128  if (info.status == CheckPhishingUrlCache::MalWare) {
129  save();
130  }
131 }
132 
133 CheckPhishingUrlCache::CheckPhishingUrlCache(QObject *parent)
134  : QObject(parent)
135  , d(new CheckPhishingUrlCachePrivate)
136 {
137 }
138 
139 CheckPhishingUrlCache::~CheckPhishingUrlCache() = default;
140 
141 void CheckPhishingUrlCache::addCheckingUrlResult(const QUrl &url, bool correctUrl, uint verifyCacheAfterThisTime)
142 {
143  d->addCheckPhishingUrlResult(url, correctUrl, verifyCacheAfterThisTime);
144 }
145 
146 CheckPhishingUrlCache::UrlStatus CheckPhishingUrlCache::urlStatus(const QUrl &url)
147 {
148  return d->urlStatus(url);
149 }
150 
151 void CheckPhishingUrlCache::clearCache()
152 {
153  d->clearCache();
154 }
155 
156 CheckPhishingUrlCache *CheckPhishingUrlCache::self()
157 {
158  static CheckPhishingUrlCache s_self;
159  return &s_self;
160 }
bool sync() override
QMap::const_iterator constBegin() const const
const T & at(int i) const const
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
void clear()
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
int count(const T &value) const const
void append(const T &value)
const QList< QKeySequence > & save()
const Key key(const T &value, const Key &defaultKey) const const
KConfigGroup group(const QString &group)
const QList< QKeySequence > & end()
bool isValid(QStringView ifopt)
T readEntry(const QString &key, const T &aDefault) const
The CheckPhishingUrlCache class.
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.