#include "ash/system/human_presence/lock_on_leave_controller.h"
#include <optional>
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "chromeos/ash/components/dbus/human_presence/human_presence_dbus_client.h"
#include "chromeos/ash/components/human_presence/human_presence_configuration.h"
namespace ash {
namespace {
void EnableLockOnLeaveViaDBus() {
const auto config = hps::GetEnableLockOnLeaveConfig();
if (config.has_value()) {
HumanPresenceDBusClient::Get()->EnableHpsSense(config.value());
LOG(ERROR) << "LockOnLeaveController: enabling HpsSense from chrome.";
} else {
LOG(ERROR)
<< "LockOnLeaveController: couldn't parse HpsSense configuration.";
}
}
void DisableLockOnLeaveViaDBus() {
HumanPresenceDBusClient::Get()->DisableHpsSense();
}
}
LockOnLeaveController::LockOnLeaveController() {
human_presence_observation_.Observe(HumanPresenceDBusClient::Get());
HumanPresenceDBusClient::Get()->WaitForServiceToBeAvailable(
base::BindOnce(&LockOnLeaveController::OnServiceAvailable,
weak_ptr_factory_.GetWeakPtr()));
HumanPresenceOrientationController* orientation_controller =
Shell::Get()->human_presence_orientation_controller();
suitable_for_human_presence_ =
orientation_controller->IsOrientationSuitable();
orientation_observation_.Observe(orientation_controller);
}
LockOnLeaveController::~LockOnLeaveController() {
human_presence_observation_.Reset();
orientation_observation_.Reset();
}
void LockOnLeaveController::EnableLockOnLeave() {
want_lock_on_leave_ = true;
ReconfigViaDbus();
}
void LockOnLeaveController::DisableLockOnLeave() {
want_lock_on_leave_ = false;
ReconfigViaDbus();
}
void LockOnLeaveController::OnOrientationChanged(
bool suitable_for_human_presence) {
suitable_for_human_presence_ = suitable_for_human_presence;
ReconfigViaDbus();
}
void LockOnLeaveController::OnHpsSenseChanged(const hps::HpsResultProto&) {}
void LockOnLeaveController::OnHpsNotifyChanged(const hps::HpsResultProto&) {}
void LockOnLeaveController::OnRestart() {
service_available_ = true;
ReconfigViaDbus();
}
void LockOnLeaveController::OnShutdown() {
service_available_ = false;
configured_state_ = ConfiguredLockOnLeaveState::kDisabled;
}
void LockOnLeaveController::OnServiceAvailable(const bool service_available) {
service_available_ = service_available;
ReconfigViaDbus();
}
void LockOnLeaveController::ReconfigViaDbus() {
if (!service_available_)
return;
if (configured_state_ == ConfiguredLockOnLeaveState::kUnknown) {
DisableLockOnLeaveViaDBus();
configured_state_ = ConfiguredLockOnLeaveState::kDisabled;
}
const ConfiguredLockOnLeaveState wanted_state =
want_lock_on_leave_ && suitable_for_human_presence_
? ConfiguredLockOnLeaveState::kEnabled
: ConfiguredLockOnLeaveState::kDisabled;
if (wanted_state == configured_state_)
return;
if (wanted_state == ConfiguredLockOnLeaveState::kEnabled) {
EnableLockOnLeaveViaDBus();
} else {
DisableLockOnLeaveViaDBus();
}
configured_state_ = wanted_state;
}
}