|
| 1 | +#include "agentimpl.hpp" |
| 2 | +#include <algorithm> |
| 3 | +#include <utility> |
| 4 | + |
| 5 | +#include <qlist.h> |
| 6 | +#include <qlogging.h> |
| 7 | +#include <qloggingcategory.h> |
| 8 | +#include <qobject.h> |
| 9 | +#include <qproperty.h> |
| 10 | +#include <qtmetamacros.h> |
| 11 | + |
| 12 | +#include "../../core/generation.hpp" |
| 13 | +#include "../../core/logcat.hpp" |
| 14 | +#include "gobjectref.hpp" |
| 15 | +#include "listener.hpp" |
| 16 | +#include "qml.hpp" |
| 17 | + |
| 18 | +namespace { |
| 19 | +QS_LOGGING_CATEGORY(logPolkit, "quickshell.service.polkit", QtWarningMsg); |
| 20 | +} |
| 21 | + |
| 22 | +namespace qs::service::polkit { |
| 23 | +PolkitAgentImpl* PolkitAgentImpl::instance = nullptr; |
| 24 | + |
| 25 | +PolkitAgentImpl::PolkitAgentImpl(PolkitAgent* agent) |
| 26 | + : QObject(nullptr) |
| 27 | + , listener(qs_polkit_agent_new(this), G_OBJECT_NO_REF) |
| 28 | + , qmlAgent(agent) |
| 29 | + , path(this->qmlAgent->path()) { |
| 30 | +auto utf8Path = this->path.toUtf8(); |
| 31 | +qs_polkit_agent_register(this->listener.get(), utf8Path.constData()); |
| 32 | +} |
| 33 | + |
| 34 | +PolkitAgentImpl::~PolkitAgentImpl() { this->cancelAllRequests("PolkitAgent is being destroyed"); } |
| 35 | + |
| 36 | +void PolkitAgentImpl::cancelAllRequests(const QString& reason) { |
| 37 | +for (; !this->queuedRequests.empty(); this->queuedRequests.pop_back()) { |
| 38 | +AuthRequest* req = this->queuedRequests.back(); |
| 39 | +qCDebug(logPolkit) << "destroying queued authentication request for action" << req->actionId; |
| 40 | +req->cancel(reason); |
| 41 | +delete req; |
| 42 | +} |
| 43 | + |
| 44 | +auto* flow = this->bActiveFlow.value(); |
| 45 | +if (flow) { |
| 46 | +flow->cancelAuthenticationRequest(); |
| 47 | +flow->deleteLater(); |
| 48 | +} |
| 49 | + |
| 50 | +if (this->bIsRegistered.value()) qs_polkit_agent_unregister(this->listener.get()); |
| 51 | +} |
| 52 | + |
| 53 | +PolkitAgentImpl* PolkitAgentImpl::tryGetOrCreate(PolkitAgent* agent) { |
| 54 | +if (instance == nullptr) instance = new PolkitAgentImpl(agent); |
| 55 | +if (instance->qmlAgent == agent) return instance; |
| 56 | +return nullptr; |
| 57 | +} |
| 58 | + |
| 59 | +PolkitAgentImpl* PolkitAgentImpl::tryGet(const PolkitAgent* agent) { |
| 60 | +if (instance == nullptr) return nullptr; |
| 61 | +if (instance->qmlAgent == agent) return instance; |
| 62 | +return nullptr; |
| 63 | +} |
| 64 | + |
| 65 | +PolkitAgentImpl* PolkitAgentImpl::tryTakeoverOrCreate(PolkitAgent* agent) { |
| 66 | +if (auto* impl = tryGetOrCreate(agent); impl != nullptr) return impl; |
| 67 | + |
| 68 | +auto* prevGen = EngineGeneration::findObjectGeneration(instance->qmlAgent); |
| 69 | +auto* myGen = EngineGeneration::findObjectGeneration(agent); |
| 70 | +if (prevGen == myGen) return nullptr; |
| 71 | + |
| 72 | +qCDebug(logPolkit) << "taking over listener from previous generation"; |
| 73 | +instance->qmlAgent = agent; |
| 74 | +instance->setPath(agent->path()); |
| 75 | + |
| 76 | +return instance; |
| 77 | +} |
| 78 | + |
| 79 | +void PolkitAgentImpl::onEndOfQmlAgent(PolkitAgent* agent) { |
| 80 | +if (instance != nullptr && instance->qmlAgent == agent) { |
| 81 | +delete instance; |
| 82 | +instance = nullptr; |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +void PolkitAgentImpl::setPath(const QString& path) { |
| 87 | +if (this->path == path) return; |
| 88 | + |
| 89 | +this->path = path; |
| 90 | +auto utf8Path = path.toUtf8(); |
| 91 | + |
| 92 | +this->cancelAllRequests("PolkitAgent path changed"); |
| 93 | +qs_polkit_agent_unregister(this->listener.get()); |
| 94 | +this->bIsRegistered = false; |
| 95 | + |
| 96 | +qs_polkit_agent_register(this->listener.get(), utf8Path.constData()); |
| 97 | +} |
| 98 | + |
| 99 | +void PolkitAgentImpl::registerComplete(bool success) { |
| 100 | +if (success) this->bIsRegistered = true; |
| 101 | +else qCWarning(logPolkit) << "failed to register listener on path" << this->qmlAgent->path(); |
| 102 | +} |
| 103 | + |
| 104 | +void PolkitAgentImpl::initiateAuthentication(AuthRequest* request) { |
| 105 | +qCDebug(logPolkit) << "incoming authentication request for action" << request->actionId; |
| 106 | + |
| 107 | +this->queuedRequests.emplace_back(request); |
| 108 | + |
| 109 | +if (this->queuedRequests.size() == 1) { |
| 110 | +this->activateAuthenticationRequest(); |
| 111 | +} |
| 112 | +} |
| 113 | + |
| 114 | +void PolkitAgentImpl::cancelAuthentication(AuthRequest* request) { |
| 115 | +qCDebug(logPolkit) << "cancelling authentication request from agent"; |
| 116 | + |
| 117 | +auto* flow = this->bActiveFlow.value(); |
| 118 | +if (flow && flow->authRequest() == request) { |
| 119 | +flow->cancelFromAgent(); |
| 120 | +} else if (auto it = std::ranges::find(this->queuedRequests, request); |
| 121 | + it != this->queuedRequests.end()) |
| 122 | +{ |
| 123 | +qCDebug(logPolkit) << "removing queued authentication request for action" << (*it)->actionId; |
| 124 | +(*it)->cancel("Authentication request was cancelled"); |
| 125 | +delete (*it); |
| 126 | +this->queuedRequests.erase(it); |
| 127 | +} else { |
| 128 | +qCWarning(logPolkit) << "the cancelled request was not found in the queue."; |
| 129 | +} |
| 130 | +} |
| 131 | + |
| 132 | +void PolkitAgentImpl::activateAuthenticationRequest() { |
| 133 | +if (this->queuedRequests.empty()) return; |
| 134 | + |
| 135 | +AuthRequest* req = this->queuedRequests.front(); |
| 136 | +this->queuedRequests.pop_front(); |
| 137 | +qCDebug(logPolkit) << "activating authentication request for action" << req->actionId |
| 138 | + << ", cookie: " << req->cookie; |
| 139 | + |
| 140 | +QList<Identity*> identities; |
| 141 | +for (auto& identity: req->identities) { |
| 142 | +auto* obj = Identity::fromPolkitIdentity(identity); |
| 143 | +if (obj) identities.append(obj); |
| 144 | +} |
| 145 | +if (identities.isEmpty()) { |
| 146 | +qCWarning(logPolkit |
| 147 | +) << "no supported identities available for authentication request, cancelling."; |
| 148 | +req->cancel("Error requesting authentication: no supported identities available."); |
| 149 | +delete req; |
| 150 | +return; |
| 151 | +} |
| 152 | + |
| 153 | +this->bActiveFlow = new AuthFlow(req, std::move(identities)); |
| 154 | + |
| 155 | +QObject::connect( |
| 156 | + this->bActiveFlow.value(), |
| 157 | + &AuthFlow::isCompletedChanged, |
| 158 | + this, |
| 159 | + &PolkitAgentImpl::finishAuthenticationRequest |
| 160 | +); |
| 161 | + |
| 162 | +emit this->qmlAgent->authenticationRequestStarted(); |
| 163 | +} |
| 164 | + |
| 165 | +void PolkitAgentImpl::finishAuthenticationRequest() { |
| 166 | +if (!this->bActiveFlow.value()) return; |
| 167 | + |
| 168 | +qCDebug(logPolkit) << "finishing authentication request for action" |
| 169 | + << this->bActiveFlow.value()->actionId(); |
| 170 | + |
| 171 | +this->bActiveFlow.value()->deleteLater(); |
| 172 | + |
| 173 | +if (!this->queuedRequests.empty()) { |
| 174 | +this->activateAuthenticationRequest(); |
| 175 | +} else { |
| 176 | +this->bActiveFlow = nullptr; |
| 177 | +} |
| 178 | +} |
| 179 | +} // namespace qs::service::polkit |
0 commit comments