7#include "backoffmodemanager.h"
8#include "checkphishingurlutil.h"
9#include "webengineviewer_debug.h"
12#include <KConfigGroup>
16using namespace WebEngineViewer;
20class WebEngineViewer::BackOffModeManagerPrivate
23 BackOffModeManagerPrivate(BackOffModeManager *qq)
25 , mTimer(new QTimer(q))
27 mTimer->setSingleShot(
true);
28 q->connect(mTimer, &
QTimer::timeout, q, &BackOffModeManager::slotTimerFinished);
34 [[nodiscard]]
int calculateBackModeTime()
const;
36 void exitBackOffMode();
37 void updateTimer(
int seconds);
38 void slotTimerFinished();
40 BackOffModeManager *
const q;
42 int mNumberOfHttpFailed = 0;
43 bool isInOffMode =
false;
46void BackOffModeManagerPrivate::save()
48 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
49 KConfigGroup grp = phishingurlKConfig.group(QStringLiteral(
"BackOffMode"));
52 const int calculateTimeInSeconds = calculateBackModeTime();
55 updateTimer(calculateTimeInSeconds);
62void BackOffModeManagerPrivate::slotTimerFinished()
64 qCDebug(WEBENGINEVIEWER_LOG) <<
"Existing from BlackOffMode";
69void BackOffModeManagerPrivate::updateTimer(
int seconds)
71 if (mTimer->isActive()) {
74 mTimer->setInterval(seconds * 1000);
78void BackOffModeManagerPrivate::load()
80 KConfig phishingurlKConfig(WebEngineViewer::CheckPhishingUrlUtil::configFileName());
81 KConfigGroup grp = phishingurlKConfig.
group(QStringLiteral(
"BackOffMode"));
82 isInOffMode = grp.
readEntry(
"Enabled",
false);
84 const qint64 delay = grp.
readEntry(
"Delay", 0);
87 const qint64 diff = (delay - now);
96int BackOffModeManagerPrivate::calculateBackModeTime()
const
98 return WebEngineViewer::CheckPhishingUrlUtil::generateRandomSecondValue(mNumberOfHttpFailed);
101void BackOffModeManagerPrivate::startOffMode()
103 mNumberOfHttpFailed++;
105 qCWarning(WEBENGINEVIEWER_LOG) <<
"New failed" << mNumberOfHttpFailed;
107 qCWarning(WEBENGINEVIEWER_LOG) <<
"starting back of mode";
113void BackOffModeManagerPrivate::exitBackOffMode()
116 mNumberOfHttpFailed = 0;
119BackOffModeManager::BackOffModeManager(QObject *parent)
121 , d(new BackOffModeManagerPrivate(this))
125BackOffModeManager::~BackOffModeManager() =
default;
129 return s_backOffModeManager;
132bool BackOffModeManager::isInBackOffMode()
const
134 return d->isInOffMode;
137void BackOffModeManager::startOffMode()
142int BackOffModeManager::numberOfHttpFailed()
const
144 return d->mNumberOfHttpFailed;
147void BackOffModeManager::slotTimerFinished()
149 d->slotTimerFinished();
KConfigGroup group(const QString &group)
void deleteEntry(const char *key, WriteConfigFlags pFlags=Normal)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
The BackOffModeManager class.
QDateTime addSecs(qint64 s) const const
QDateTime currentDateTime()
QDateTime currentDateTimeUtc()
qint64 toSecsSinceEpoch() const const