Add additional methods for DeviceInfo.

This commit is contained in:
Vibhav Pant
2023-07-22 20:16:26 +05:30
parent af5bea6a3d
commit 112072dbad
2 changed files with 97 additions and 3 deletions
@@ -1,10 +1,13 @@
#include <cstdlib>
#include <cstring>
#include <optional>
#include <string>
#include <pwd.h>
#include <sys/types.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-login.h>
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/linux/device_info.h"
@@ -18,6 +21,10 @@ const char *HOSTNAME_DEST = "org.freedesktop.hostname1";
const char *HOSTNAME_PATH = "/org/freedesktop/hostname1";
const char *HOSTNAME_INTERFACE = "org.freedesktop.hostname1";
const char *LOGIN_DEST = "org.freedesktop.login1";
const char *LOGIN_PATH = "/org/freedesktop/login1/session/_";
const char *LOGIN_INTERFACE = "org.freedesktop.login1.Session";
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const {
sd_bus *bus = nullptr;
if (sd_bus_default_system(&bus) < 0) {
@@ -117,5 +124,88 @@ std::optional<std::string> DeviceInfo::GetProfileUserName() const {
char *name = strtok(pwd->pw_gecos, ",");
return std::string(name);
}
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
char *dir = getenv("XDG_DOWNLOAD_DIR");
if (dir == NULL) {
std::filesystem::path home_path(std::string(getenv("HOME")));
return home_path / "Desktop";
}
return std::filesystem::path(std::string(dir));
}
std::optional<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
char *dir = getenv("XDG_STATE_HOME");
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "com.github.google.nearby";
}
std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
char *dir = getenv("XDG_CACHE_HOME");
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "com.github.google.nearby";
}
std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
char *dir = getenv("XDG_STATE_HOME");
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "com.github.google.nearby" /
"logs";
}
std::optional<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
char *dir = getenv("XDG_STATE_HOME");
if (dir == NULL) {
return std::filesystem::path("/tmp");
}
return std::filesystem::path(std::string(dir)) / "com.github.google.nearby" /
"crash";
}
bool DeviceInfo::IsScreenLocked() const {
char *session = nullptr;
if (sd_pid_get_session(getpid(), &session) < 0) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error getting session for current user";
return false;
}
sd_bus *bus;
if (sd_bus_default_system(&bus) < 0) {
NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to systemd bus.";
free(session);
return false;
}
std::string session_path(LOGIN_PATH);
session_path += session;
free(session);
sd_bus_error err = SD_BUS_ERROR_NULL;
bool locked;
if (sd_bus_get_property_trivial(bus, LOGIN_DEST, session_path.c_str(),
LOGIN_INTERFACE, "LockedHint", &err, 'b',
&locked) < 0) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error getting LockedState from org.freedesktop.login1: "
<< err.message;
locked = false;
}
sd_bus_error_free(&err);
sd_bus_unref(bus);
return locked;
}
} // namespace linux
} // namespace nearby
@@ -32,16 +32,20 @@ public:
std::optional<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
std::optional<std::filesystem::path> GetCommonAppDataPath() const override;
std::optional<std::filesystem::path> GetCommonAppDataPath() const override {
return std::nullopt;
};
std::optional<std::filesystem::path> GetTemporaryPath() const override;
std::optional<std::filesystem::path> GetLogPath() const override;
std::optional<std::filesystem::path> GetCrashDumpPath() const override;
bool IsScreenLocked() const override;
// TODO: Implement listening to logind for changes to LockedState.
void RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override;
void UnregisterScreenLockedListener(absl::string_view listener_name) override;
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override{};
void
UnregisterScreenLockedListener(absl::string_view listener_name) override{};
sd_bus *system_bus;
};