-
Couldn't load subscription status.
- Fork 57
Add service module to write PolKit agents #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Cu3PO42
wants to merge
1
commit into
quickshell-mirror:master
Choose a base branch
from
Cu3PO42:polkit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,590
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ jobs: | |
| libxcb \ | ||
| libpipewire \ | ||
| cli11 \ | ||
| polkit \ | ||
| jemalloc | ||
|
|
||
| - name: Build | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| libxcb | ||
| libxkbcommon | ||
| linux-pam | ||
| polkit | ||
| mesa | ||
| pipewire | ||
| qtbase | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| find_package(PkgConfig REQUIRED) | ||
| pkg_check_modules(glib REQUIRED IMPORTED_TARGET glib-2.0>=2.36) | ||
| pkg_check_modules(gobject REQUIRED IMPORTED_TARGET gobject-2.0) | ||
| pkg_check_modules(polkit_agent REQUIRED IMPORTED_TARGET polkit-agent-1) | ||
| pkg_check_modules(polkit REQUIRED IMPORTED_TARGET polkit-gobject-1) | ||
|
|
||
| qt_add_library(quickshell-service-polkit STATIC | ||
| agentimpl.cpp | ||
| flow.cpp | ||
| identity.cpp | ||
| listener.cpp | ||
| session.cpp | ||
| qml.cpp | ||
| ) | ||
|
|
||
| qt_add_qml_module(quickshell-service-polkit | ||
| URI Quickshell.Services.Polkit | ||
| VERSION 0.1 | ||
| DEPENDENCIES QtQml | ||
| ) | ||
|
|
||
| install_qml_module(quickshell-service-polkit) | ||
|
|
||
| target_link_libraries(quickshell-service-polkit PRIVATE | ||
| Qt::Qml | ||
| Qt::Quick | ||
| PkgConfig::glib | ||
| PkgConfig::gobject | ||
| PkgConfig::polkit_agent | ||
| PkgConfig::polkit | ||
| ) | ||
|
|
||
| qs_module_pch(quickshell-service-polkit) | ||
|
|
||
| target_link_libraries(quickshell PRIVATE quickshell-service-polkitplugin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| #include "agentimpl.hpp" | ||
| #include <algorithm> | ||
| #include <utility> | ||
|
|
||
| #include <qlist.h> | ||
| #include <qloggingcategory.h> | ||
| #include <qobject.h> | ||
| #include <qtmetamacros.h> | ||
|
|
||
| #include "../../core/generation.hpp" | ||
| #include "../../core/logcat.hpp" | ||
| #include "gobjectref.hpp" | ||
| #include "listener.hpp" | ||
| #include "qml.hpp" | ||
|
|
||
| namespace { | ||
| QS_LOGGING_CATEGORY(logPolkit, "quickshell.service.polkit"); | ||
| } | ||
|
|
||
| namespace qs::service::polkit { | ||
| PolkitAgentImpl* PolkitAgentImpl::instance = nullptr; | ||
|
|
||
| PolkitAgentImpl::PolkitAgentImpl(PolkitAgent* agent) | ||
| : QObject(nullptr) | ||
| , listener(qs_polkit_agent_new(this), G_OBJECT_NO_REF) | ||
| , qmlAgent(agent) | ||
| , path(this->qmlAgent->path()) { | ||
| auto utf8Path = this->path.toUtf8(); | ||
| qs_polkit_agent_register(this->listener.get(), utf8Path.constData()); | ||
| } | ||
|
|
||
| PolkitAgentImpl::~PolkitAgentImpl() { this->cancelAllRequests("PolkitAgent is being destroyed"); } | ||
|
|
||
| void PolkitAgentImpl::cancelAllRequests(const QString& reason) { | ||
| for (; !this->queuedRequests.empty(); this->queuedRequests.pop_back()) { | ||
| AuthRequest* req = this->queuedRequests.back(); | ||
| qCDebug(logPolkit) << "destroying queued authentication request for action" << req->actionId; | ||
| req->cancel(reason); | ||
| delete req; | ||
| } | ||
|
|
||
| auto* flow = this->bActiveFlow.value(); | ||
| if (flow) { | ||
| flow->cancelAuthenticationRequest(); | ||
| flow->deleteLater(); | ||
| } | ||
|
|
||
| if (this->bIsRegistered.value()) qs_polkit_agent_unregister(this->listener.get()); | ||
| } | ||
|
|
||
| PolkitAgentImpl* PolkitAgentImpl::tryGetOrCreate(PolkitAgent* agent) { | ||
| if (instance == nullptr) instance = new PolkitAgentImpl(agent); | ||
| if (instance->qmlAgent == agent) return instance; | ||
| return nullptr; | ||
| } | ||
|
|
||
| PolkitAgentImpl* PolkitAgentImpl::tryGet(const PolkitAgent* agent) { | ||
| if (instance == nullptr) return nullptr; | ||
| if (instance->qmlAgent == agent) return instance; | ||
| return nullptr; | ||
| } | ||
|
|
||
| PolkitAgentImpl* PolkitAgentImpl::tryTakeover(PolkitAgent* agent) { | ||
| if (instance == nullptr) return nullptr; | ||
|
|
||
| auto* prevGen = EngineGeneration::findObjectGeneration(instance->qmlAgent); | ||
| auto* myGen = EngineGeneration::findObjectGeneration(agent); | ||
| if (prevGen == myGen) return nullptr; | ||
|
|
||
| qCDebug(logPolkit) << "taking over listener from previous generation"; | ||
| instance->qmlAgent = agent; | ||
|
|
||
| return instance; | ||
| } | ||
|
|
||
| void PolkitAgentImpl::onEndOfQmlAgent(PolkitAgent* agent) { | ||
| if (instance != nullptr && instance->qmlAgent == agent) { | ||
| delete instance; | ||
| instance = nullptr; | ||
| } | ||
| } | ||
|
|
||
| QBindable<AuthFlow*> PolkitAgentImpl::activeFlow() { return &this->bActiveFlow; } | ||
| QBindable<bool> PolkitAgentImpl::isRegistered() { return &this->bIsRegistered; } | ||
|
|
||
| const QString& PolkitAgentImpl::getPath() const { return this->path; } | ||
|
|
||
| void PolkitAgentImpl::setPath(const QString& path) { | ||
| if (this->path == path) return; | ||
|
|
||
| this->path = path; | ||
| auto utf8Path = path.toUtf8(); | ||
|
|
||
| this->cancelAllRequests("PolkitAgent path changed"); | ||
| qs_polkit_agent_unregister(this->listener.get()); | ||
| this->bIsRegistered = false; | ||
|
|
||
| qs_polkit_agent_register(this->listener.get(), utf8Path.constData()); | ||
| } | ||
|
|
||
| void PolkitAgentImpl::registerComplete(bool success) { | ||
| if (success) this->bIsRegistered = true; | ||
| else qCWarning(logPolkit) << "failed to register listener on path" << this->qmlAgent->path(); | ||
| } | ||
|
|
||
| void PolkitAgentImpl::initiateAuthentication(AuthRequest* request) { | ||
| qCDebug(logPolkit) << "incoming authentication request for action" << request->actionId; | ||
|
|
||
| this->queuedRequests.emplace_back(request); | ||
|
|
||
| if (this->queuedRequests.size() == 1) { | ||
| this->activateAuthenticationRequest(); | ||
| } | ||
| } | ||
|
|
||
| void PolkitAgentImpl::cancelAuthentication(AuthRequest* request) { | ||
| qCDebug(logPolkit) << "cancelling authentication request from agent"; | ||
|
|
||
| auto* flow = this->bActiveFlow.value(); | ||
| if (flow && flow->authRequest() == request) { | ||
| flow->cancelFromAgent(); | ||
| } else if (auto it = std::ranges::find(this->queuedRequests, request); | ||
| it != this->queuedRequests.end()) | ||
| { | ||
| qCDebug(logPolkit) << "removing queued authentication request for action" << (*it)->actionId; | ||
| (*it)->cancel("Authentication request was cancelled"); | ||
| delete (*it); | ||
| this->queuedRequests.erase(it); | ||
| } else { | ||
| qCWarning(logPolkit) << "the cancelled request was not found in the queue."; | ||
| } | ||
| } | ||
|
|
||
| void PolkitAgentImpl::activateAuthenticationRequest() { | ||
| if (this->queuedRequests.empty()) return; | ||
|
|
||
| AuthRequest* req = this->queuedRequests.front(); | ||
| this->queuedRequests.pop_front(); | ||
| qCDebug(logPolkit) << "activating authentication request for action" << req->actionId | ||
| << ", cookie: " << req->cookie; | ||
|
|
||
| QList<Identity*> identities; | ||
| for (auto& identity: req->identities) { | ||
| auto* obj = Identity::fromPolkitIdentity(identity); | ||
| if (obj) identities.append(obj); | ||
| } | ||
| if (identities.isEmpty()) { | ||
| qCWarning(logPolkit | ||
| ) << "no supported identities available for authentication request, cancelling."; | ||
| req->cancel("Error requesting authentication: no supported identities available."); | ||
| delete req; | ||
| return; | ||
| } | ||
|
|
||
| this->bActiveFlow = new AuthFlow(req, std::move(identities)); | ||
|
|
||
| QObject::connect( | ||
| this->bActiveFlow.value(), | ||
| &AuthFlow::isCompletedChanged, | ||
| this, | ||
| &PolkitAgentImpl::finishAuthenticationRequest | ||
| ); | ||
|
|
||
| emit this->qmlAgent->isActiveChanged(); | ||
| emit this->qmlAgent->flowChanged(); | ||
| emit this->qmlAgent->authenticationRequestStarted(); | ||
| } | ||
|
|
||
| void PolkitAgentImpl::finishAuthenticationRequest() { | ||
| if (!this->bActiveFlow.value()) return; | ||
|
|
||
| qCDebug(logPolkit) << "finishing authentication request for action" | ||
| << this->bActiveFlow.value()->actionId(); | ||
|
|
||
| this->bActiveFlow.value()->deleteLater(); | ||
|
|
||
| if (!this->queuedRequests.empty()) { | ||
| this->activateAuthenticationRequest(); | ||
| } else { | ||
| this->bActiveFlow = nullptr; | ||
| } | ||
| } | ||
| } // namespace qs::service::polkit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #pragma once | ||
|
|
||
| #include <deque> | ||
|
|
||
| #include <qobject.h> | ||
| #include <qproperty.h> | ||
|
|
||
| #include "flow.hpp" | ||
| #include "gobjectref.hpp" | ||
| #include "listener.hpp" | ||
|
|
||
| namespace qs::service::polkit { | ||
| class PolkitAgent; | ||
|
|
||
| class PolkitAgentImpl | ||
| : public QObject | ||
| , public ListenerCb { | ||
| Q_OBJECT; | ||
| Q_DISABLE_COPY_MOVE(PolkitAgentImpl); | ||
|
|
||
| public: | ||
| ~PolkitAgentImpl() override; | ||
|
|
||
| static PolkitAgentImpl* tryGetOrCreate(PolkitAgent* agent); | ||
| static PolkitAgentImpl* tryGet(const PolkitAgent* agent); | ||
| static PolkitAgentImpl* tryTakeover(PolkitAgent* agent); | ||
| static void onEndOfQmlAgent(PolkitAgent* agent); | ||
|
|
||
| [[nodiscard]] QBindable<AuthFlow*> activeFlow(); | ||
| [[nodiscard]] QBindable<bool> isRegistered(); | ||
|
|
||
| [[nodiscard]] const QString& getPath() const; | ||
| void setPath(const QString& path); | ||
|
|
||
| void initiateAuthentication(AuthRequest* request) override; | ||
| void cancelAuthentication(AuthRequest* request) override; | ||
| void registerComplete(bool success) override; | ||
|
|
||
| void cancelAllRequests(const QString& reason); | ||
|
|
||
| signals: | ||
| void activeFlowChanged(); | ||
| void isRegisteredChanged(); | ||
|
|
||
| private: | ||
| PolkitAgentImpl(PolkitAgent* agent); | ||
|
|
||
| static PolkitAgentImpl* instance; | ||
|
|
||
| /// Start handling of the next authentication request in the queue. | ||
| void activateAuthenticationRequest(); | ||
| /// Finalize and remove the current authentication request. | ||
| void finishAuthenticationRequest(); | ||
|
|
||
| GObjectRef<QsPolkitAgent> listener; | ||
| PolkitAgent* qmlAgent = nullptr; | ||
| QString path; | ||
|
|
||
| std::deque<AuthRequest*> queuedRequests; | ||
|
|
||
| Q_OBJECT_BINDABLE_PROPERTY( | ||
| PolkitAgentImpl, | ||
| AuthFlow*, | ||
| bActiveFlow, | ||
| &PolkitAgentImpl::activeFlowChanged | ||
| ); | ||
| Q_OBJECT_BINDABLE_PROPERTY( | ||
| PolkitAgentImpl, | ||
| bool, | ||
| bIsRegistered, | ||
| &PolkitAgentImpl::isRegisteredChanged | ||
| ); | ||
| }; | ||
| } // namespace qs::service::polkit | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.