Messagelib

backoffmodemanager.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2025 Laurent Montel <montel@kde.org>
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
16using namespace WebEngineViewer;
17
18Q_GLOBAL_STATIC(BackOffModeManager, s_backOffModeManager)
19
20class WebEngineViewer::BackOffModeManagerPrivate
21{
22public:
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 [[nodiscard]] 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
46void 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
62void BackOffModeManagerPrivate::slotTimerFinished()
63{
64 qCDebug(WEBENGINEVIEWER_LOG) << "Existing from BlackOffMode";
65 exitBackOffMode();
66 save();
67}
68
69void BackOffModeManagerPrivate::updateTimer(int seconds)
70{
71 if (mTimer->isActive()) {
72 mTimer->stop();
73 }
74 mTimer->setInterval(seconds * 1000);
75 mTimer->start();
76}
77
78void 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);
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
96int BackOffModeManagerPrivate::calculateBackModeTime() const
97{
98 return WebEngineViewer::CheckPhishingUrlUtil::generateRandomSecondValue(mNumberOfHttpFailed);
99}
100
101void 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
113void BackOffModeManagerPrivate::exitBackOffMode()
114{
115 isInOffMode = false;
116 mNumberOfHttpFailed = 0;
117}
118
119BackOffModeManager::BackOffModeManager(QObject *parent)
120 : QObject(parent)
121 , d(new BackOffModeManagerPrivate(this))
122{
123}
124
125BackOffModeManager::~BackOffModeManager() = default;
126
127BackOffModeManager *BackOffModeManager::self()
128{
129 return s_backOffModeManager;
130}
131
132bool BackOffModeManager::isInBackOffMode() const
133{
134 return d->isInOffMode;
135}
136
137void BackOffModeManager::startOffMode()
138{
139 d->startOffMode();
140}
141
142int BackOffModeManager::numberOfHttpFailed() const
143{
144 return d->mNumberOfHttpFailed;
145}
146
147void BackOffModeManager::slotTimerFinished()
148{
149 d->slotTimerFinished();
150}
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
bool sync() override
The BackOffModeManager class.
QDateTime addSecs(qint64 s) const const
QDateTime currentDateTime()
QDateTime currentDateTimeUtc()
qint64 toSecsSinceEpoch() const const
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 31 2025 12:05:42 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.