Add support for inhibition locks.

This commit is contained in:
Vibhav Pant
2023-08-27 14:59:43 +05:30
parent e63c5087f9
commit 6350b14f3b
2 changed files with 68 additions and 14 deletions
@@ -8,24 +8,23 @@
#include <sdbus-c++/IConnection.h>
#include <sdbus-c++/IProxy.h>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/linux/dbus.h"
#include "internal/platform/implementation/linux/device_info.h"
#include "internal/platform/logging.h"
#include "absl/synchronization/mutex.h"
namespace nearby {
namespace linux {
void CurrentUserSession::RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
absl::MutexLock l(&screen_lock_listeners_mutex_);
screen_lock_listeners_[listener_name] = callback;
}
void
CurrentUserSession::UnregisterScreenLockedListener(absl::string_view listener_name)
{
void CurrentUserSession::UnregisterScreenLockedListener(
absl::string_view listener_name) {
absl::MutexLock l(&screen_lock_listeners_mutex_);
screen_lock_listeners_.erase(listener_name);
}
@@ -46,8 +45,8 @@ void CurrentUserSession::onUnlock() {
DeviceInfo::DeviceInfo(sdbus::IConnection &system_bus)
: system_bus_(system_bus),
current_user_session_(std::make_unique<CurrentUserSession>(system_bus_)) {
}
current_user_session_(std::make_unique<CurrentUserSession>(system_bus_)),
login_manager_(std::make_unique<LoginManager>(system_bus_)) {}
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const {
Hostnamed hostnamed(system_bus_);
@@ -128,8 +127,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "Google Nearby" /
"logs";
return std::filesystem::path(std::string(dir)) / "Google Nearby" / "logs";
}
std::optional<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
@@ -137,8 +135,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "Google Nearby" /
"crashes";
return std::filesystem::path(std::string(dir)) / "Google Nearby" / "crashes";
}
bool DeviceInfo::IsScreenLocked() const {
@@ -149,5 +146,28 @@ bool DeviceInfo::IsScreenLocked() const {
return false;
}
}
bool DeviceInfo::PreventSleep() {
try {
inhibit_fd_ = login_manager_->Inhibit("sleep", "Google Nearby",
"Google Nearby", "block");
return true;
} catch (const sdbus::Error& e) {
DBUS_LOG_METHOD_CALL_ERROR(login_manager_, "Inhibit", e);
return false;
}
}
bool DeviceInfo::AllowSleep() {
if (!inhibit_fd_.has_value()) {
NEARBY_LOGS(ERROR) << __func__
<< "No inhibit lock is acquired at the moment";
return false;
}
inhibit_fd_.reset();
return true;
}
} // namespace linux
} // namespace nearby
@@ -3,6 +3,7 @@
#include <optional>
#include <sdbus-c++/ProxyInterfaces.h>
#include <sdbus-c++/Types.h>
#include <string>
#include <sdbus-c++/IConnection.h>
@@ -13,6 +14,7 @@
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/linux/hostname_client_glue.h"
#include "internal/platform/implementation/linux/login_manager_client_glue.h"
#include "internal/platform/implementation/linux/login_session_client_glue.h"
namespace nearby {
@@ -62,6 +64,33 @@ public:
~Hostnamed() { unregisterProxy(); }
};
class LoginManager
: public sdbus::ProxyInterfaces<org::freedesktop::login1::Manager_proxy> {
public:
LoginManager(sdbus::IConnection &system_bus)
: ProxyInterfaces("org.freedesktop.login1",
"/org/freedesktop/hostname1") {
registerProxy();
}
~LoginManager() { unregisterProxy(); }
protected:
void onSessionNew(const std::string &session_id,
const sdbus::ObjectPath &object_path) override {}
void onSessionRemoved(const std::string &session_id,
const sdbus::ObjectPath &object_path) override {}
void onUserNew(const uint32_t &uid,
const sdbus::ObjectPath &object_path) override {}
void onUserRemoved(const uint32_t &uid,
const sdbus::ObjectPath &object_path) override {}
void onSeatNew(const std::string &seat_id,
const sdbus::ObjectPath &object_path) override {}
void onSeatRemoved(const std::string &seat_id,
const sdbus::ObjectPath &object_path) override {}
void onPrepareForShutdown(const bool &start) override {}
void onPrepareForSleep(const bool &start) override {}
};
class DeviceInfo : public api::DeviceInfo {
public:
DeviceInfo(sdbus::IConnection &system_bus);
@@ -94,13 +123,18 @@ public:
// TODO: Implement listening to logind for changes to LockedState.
void RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override{};
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override {}
void
UnregisterScreenLockedListener(absl::string_view listener_name) override{};
UnregisterScreenLockedListener(absl::string_view listener_name) override {}
bool PreventSleep() override;
bool AllowSleep() override;
private:
sdbus::IConnection &system_bus_;
std::unique_ptr<CurrentUserSession> current_user_session_;
std::unique_ptr<LoginManager> login_manager_;
std::optional<sdbus::UnixFd> inhibit_fd_;
};
} // namespace linux
} // namespace nearby