Messagelib

backoffmodemanager.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 "backoffmodemanager.h"
8 #include "checkphishingurlutil.h"
9 #include "webengineviewer_debug.h"
10 
11 #include <KConfig>
12 #include <KConfigGroup>
13 
14 #include <QTimer>
15 
16 using namespace WebEngineViewer;
17 
18 Q_GLOBAL_STATIC(BackOffModeManager, s_backOffModeManager)
19 
20 class WebEngineViewer::BackOffModeManagerPrivate
21 {
22 public:
23  BackOffModeManagerPrivate(BackOffModeManager *qq)
24  : q(qq)
25  , mTimer(new QTimer(q))
26  {
27  mTimer->setSingleShot(true);
28  q->connect(mTimer, &QTimer::timeout, q, &BackOffModeManager::slotTimerFinished);
29  load();
30  }
31 
32  void save();
33  void load();
34  Q_REQUIRED_RESULT int calculateBackModeTime() const;
35  void startOffMode();
36  void exitBackOffMode();
37  void updateTimer(int seconds);
38  void slotTimerFinished();
39 
40  BackOffModeManager *const q;
41  QTimer *const mTimer;
42  int mNumberOfHttpFailed = 0;
43  bool isInOffMode = false;
44 };
45 
46 void BackOffModeManagerPrivate::save()
47 {
48  KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
49  KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("BackOffMode"));
50  grp.writeEntry("Enabled", isInOffMode);
51  if (isInOffMode) {
52  const int calculateTimeInSeconds = calculateBackModeTime();
53  const qint64 delay = QDateTime::currentDateTime().addSecs(calculateTimeInSeconds).toSecsSinceEpoch();
54  grp.writeEntry("Delay", delay);
55  updateTimer(calculateTimeInSeconds);
56  } else {
57  grp.deleteEntry("Delay");
58  }
59  grp.sync();
60 }
61 
62 void BackOffModeManagerPrivate::slotTimerFinished()
63 {
64  qCDebug(WEBENGINEVIEWER_LOG) << "Existing from BlackOffMode";
65  exitBackOffMode();
66  save();
67 }
68 
69 void BackOffModeManagerPrivate::updateTimer(int seconds)
70 {
71  if (mTimer->isActive()) {
72  mTimer->stop();
73  }
74  mTimer->setInterval(seconds * 1000);
75  mTimer->start();
76 }
77 
78 void BackOffModeManagerPrivate::load()
79 {
80  KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
81  KConfigGroup grp = phishingurlKConfig.group(QStringLiteral("BackOffMode"));
82  isInOffMode = grp.readEntry("Enabled", false);
83  if (isInOffMode) {
84  const qint64 delay = grp.readEntry("Delay", 0);
85  const qint64 now = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
86  if (delay > now) {
87  const qint64 diff = (delay - now);
88  updateTimer(diff);
89  } else {
90  // Disable mode.
91  isInOffMode = false;
92  }
93  }
94 }
95 
96 int BackOffModeManagerPrivate::calculateBackModeTime() const
97 {
98  return WebEngineViewer::CheckPhishingUrlUtil::generateRandomSecondValue(mNumberOfHttpFailed);
99 }
100 
101 void BackOffModeManagerPrivate::startOffMode()
102 {
103  mNumberOfHttpFailed++;
104  if (isInOffMode) {
105  qCWarning(WEBENGINEVIEWER_LOG) << "New failed" << mNumberOfHttpFailed;
106  } else {
107  qCWarning(WEBENGINEVIEWER_LOG) << "starting back of mode";
108  isInOffMode = true;
109  }
110  save();
111 }
112 
113 void BackOffModeManagerPrivate::exitBackOffMode()
114 {
115  isInOffMode = false;
116  mNumberOfHttpFailed = 0;
117 }
118 
119 BackOffModeManager::BackOffModeManager(QObject *parent)
120  : QObject(parent)
121  , d(new BackOffModeManagerPrivate(this))
122 {
123 }
124 
125 BackOffModeManager::~BackOffModeManager() = default;
126 
127 BackOffModeManager *BackOffModeManager::self()
128 {
129  return s_backOffModeManager;
130 }
131 
132 bool BackOffModeManager::isInBackOffMode() const
133 {
134  return d->isInOffMode;
135 }
136 
137 void BackOffModeManager::startOffMode()
138 {
139  d->startOffMode();
140 }
141 
142 int BackOffModeManager::numberOfHttpFailed() const
143 {
144  return d->mNumberOfHttpFailed;
145 }
146 
147 void BackOffModeManager::slotTimerFinished()
148 {
149  d->slotTimerFinished();
150 }
bool sync() override
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
void deleteEntry(const QString &pKey, WriteConfigFlags pFlags=Normal)
void timeout()
QAction * load(const QObject *recvr, const char *slot, QObject *parent)
const QList< QKeySequence > & save()
KConfigGroup group(const QString &group)
QDateTime currentDateTime()
qint64 toSecsSinceEpoch() const const
The BackOffModeManager class.
QDateTime addSecs(qint64 s) const const
T readEntry(const QString &key, const T &aDefault) const
QDateTime currentDateTimeUtc()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.