diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2020-12-16 17:28:55 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2021-01-13 12:13:33 +0300 |
commit | 1c103a42a9b02f95ec8f8420985a447b587cc5bd (patch) | |
tree | 045214e08d974c2a1cb54c38135fc25893270873 /src | |
parent | 6f4f259a1fc1777be8b23aa17bc668c2c5bc220f (diff) |
Ensure we don't call block multiple times in the same context
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/accountstate.cpp | 9 | ||||
-rw-r--r-- | src/gui/accountstate.h | 3 | ||||
-rw-r--r-- | src/libsync/account.cpp | 7 | ||||
-rw-r--r-- | src/libsync/account.h | 1 | ||||
-rw-r--r-- | src/libsync/jobqueue.cpp | 56 | ||||
-rw-r--r-- | src/libsync/jobqueue.h | 32 |
6 files changed, 86 insertions, 22 deletions
diff --git a/src/gui/accountstate.cpp b/src/gui/accountstate.cpp index 9e8ead69a..680fa8655 100644 --- a/src/gui/accountstate.cpp +++ b/src/gui/accountstate.cpp @@ -67,6 +67,7 @@ void AccountState::updateUrlDialog(AccountPtr account, const QUrl &newUrl, std:: AccountState::AccountState(AccountPtr account) : QObject() , _account(account) + , _queueGuard(_account->jobQueue()) , _state(AccountState::Disconnected) , _connectionStatus(ConnectionValidator::Undefined) , _waitingForNewCredentials(false) @@ -134,7 +135,7 @@ void AccountState::setState(State state) if (_state == SignedOut) { _connectionStatus = ConnectionValidator::Undefined; _connectionErrors.clear(); - _account->jobQueue()->clear(); + _queueGuard.clear(); } else if (oldState == SignedOut && _state == Disconnected) { // If we stop being voluntarily signed-out, try to connect and // auth right now! @@ -148,13 +149,12 @@ void AccountState::setState(State state) } if (oldState == Connected || _state == Connected) { emit isConnectedChanged(); - _account->jobQueue()->setBlocked(false); } } // might not have changed but the underlying _connectionErrors might have if (_state == Connected) { - _account->jobQueue()->setBlocked(false); + _queueGuard.unblock(); } // don't anounce a state change from connected to connected // https://github.com/owncloud/client/commit/2c6c21d7532f0cbba4b768fde47810f6673ed931 @@ -253,9 +253,8 @@ void AccountState::checkConnectivity(bool verifyServerState) return; } - if (verifyServerState) { - _account->jobQueue()->setBlocked(true); + _queueGuard.block(); } ConnectionValidator *conValidator = new ConnectionValidator(account()); _connectionValidator = conValidator; diff --git a/src/gui/accountstate.h b/src/gui/accountstate.h index 6ee4847ee..82d678d89 100644 --- a/src/gui/accountstate.h +++ b/src/gui/accountstate.h @@ -16,6 +16,8 @@ #ifndef ACCOUNTINFO_H #define ACCOUNTINFO_H +#include "jobqueue.h" + #include <QByteArray> #include <QElapsedTimer> #include <QPointer> @@ -154,6 +156,7 @@ protected Q_SLOTS: private: AccountPtr _account; + JobQueueGuard _queueGuard; State _state; ConnectionStatus _connectionStatus; QStringList _connectionErrors; diff --git a/src/libsync/account.cpp b/src/libsync/account.cpp index 12260fbc8..68ebb0bd7 100644 --- a/src/libsync/account.cpp +++ b/src/libsync/account.cpp @@ -43,6 +43,7 @@ Account::Account(QObject *parent) , _capabilities(QVariantMap()) , _davPath(Theme::instance()->webDavPath()) , _jobQueue(this) + , _queueGuard(&_jobQueue) { qRegisterMetaType<AccountPtr>("AccountPtr"); } @@ -176,10 +177,10 @@ void Account::setCredentials(AbstractCredentials *cred) connect(_credentials.data(), &AbstractCredentials::asked, this, &Account::slotCredentialsAsked); connect(_credentials.data(), &AbstractCredentials::authenticationStarted, this, [this] { - _jobQueue.setBlocked(true); + _queueGuard.block(); }); connect(_credentials.data(), &AbstractCredentials::authenticationFailed, this, [this] { - _jobQueue.clear(); + _queueGuard.clear(); }); } @@ -408,7 +409,7 @@ void Account::slotHandleSslErrors(QNetworkReply *reply, QList<QSslError> errors) void Account::slotCredentialsFetched() { emit credentialsFetched(_credentials.data()); - _jobQueue.setBlocked(false); + _queueGuard.unblock(); } void Account::slotCredentialsAsked() diff --git a/src/libsync/account.h b/src/libsync/account.h index db0cc0be7..05c59ba8a 100644 --- a/src/libsync/account.h +++ b/src/libsync/account.h @@ -282,6 +282,7 @@ private: QString _davPath; // defaults to value from theme, might be overwritten in brandings JobQueue _jobQueue; + JobQueueGuard _queueGuard; friend class AccountManager; }; } diff --git a/src/libsync/jobqueue.cpp b/src/libsync/jobqueue.cpp index 97a14b9c4..55022fcdd 100644 --- a/src/libsync/jobqueue.cpp +++ b/src/libsync/jobqueue.cpp @@ -33,14 +33,17 @@ JobQueue::JobQueue(Account *account) { } -void JobQueue::setBlocked(bool block) +void JobQueue::block() { - if (block) { - _blocked++; - } else if (_blocked > 0) { - _blocked--; - } - qCDebug(lcJobQUeue) << "Set blocked:" << block << _blocked << _account->displayName(); + _blocked++; + qCDebug(lcJobQUeue) << "block:" << _blocked << _account->displayName(); +} + +void JobQueue::unblock() +{ + _blocked--; + qCDebug(lcJobQUeue) << "unblock:" << _blocked << _account->displayName(); + OC_ENFORCE(_blocked >= 0); if (_blocked == 0) { auto tmp = std::move(_jobs); for (auto job : tmp) { @@ -123,4 +126,43 @@ bool JobQueue::needsRetry(AbstractNetworkJob *job) const return false; } +JobQueueGuard::JobQueueGuard(JobQueue *queue) + : _queue(queue) +{ +} + +JobQueueGuard::~JobQueueGuard() +{ + unblock(); +} + +bool JobQueueGuard::block() +{ + if (!_blocked) { + _blocked = true; + _queue->block(); + return true; + } + return false; +} + +bool JobQueueGuard::unblock() +{ + if (_blocked) { + _blocked = false; + _queue->unblock(); + return true; + } + return false; +} + +bool JobQueueGuard::clear() +{ + if (_blocked) { + _blocked = false; + _queue->clear(); + return true; + } + return false; +} } diff --git a/src/libsync/jobqueue.h b/src/libsync/jobqueue.h index a01738697..6a1806fb7 100644 --- a/src/libsync/jobqueue.h +++ b/src/libsync/jobqueue.h @@ -29,9 +29,8 @@ public: JobQueue(Account *account); /** - * Set whether jobs need to be enqued + * whether jobs need to be enqued */ - void setBlocked(bool block); bool isBlocked() const; /** @@ -46,19 +45,38 @@ public: */ bool enqueue(AbstractNetworkJob *job); - /** - * Clear the queue and abort all jobs - */ - void clear(); - size_t size() const; private: bool needsRetry(AbstractNetworkJob *job) const; + + void block(); + void unblock(); + /** + * Clear the queue and abort all jobs + */ + void clear(); + Account *_account; int _blocked = 0; std::vector<QPointer<AbstractNetworkJob>> _jobs; + + friend class JobQueueGuard; }; +class OWNCLOUDSYNC_EXPORT JobQueueGuard +{ +public: + JobQueueGuard(JobQueue *queue); + ~JobQueueGuard(); + + bool block(); + bool unblock(); + bool clear(); + +private: + JobQueue *_queue; + bool _blocked = false; +}; } |