mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Handle system standby
PiperOrigin-RevId: 949324299
This commit is contained in:
committed by
Copybara-Service
parent
953d9ea26c
commit
4301bc4fce
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_API_DEVICE_INFO_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -39,6 +40,7 @@ class DeviceInfo {
|
||||
kWindows,
|
||||
kMacOS
|
||||
};
|
||||
enum class SuspendResumeEvent { kSuspend, kResume };
|
||||
|
||||
virtual ~DeviceInfo() = default;
|
||||
|
||||
@@ -69,6 +71,14 @@ class DeviceInfo {
|
||||
// Control device sleep
|
||||
virtual bool PreventSleep() = 0;
|
||||
virtual bool AllowSleep() = 0;
|
||||
|
||||
// Monitor suspend/resume events.
|
||||
// Returns a listener id that can be used to unregister the listener.
|
||||
virtual int64_t RegisterSuspendResumeListener(
|
||||
std::function<void(SuspendResumeEvent)> callback) {
|
||||
return 0;
|
||||
}
|
||||
virtual void UnregisterSuspendResumeListener(int64_t listener_id) {}
|
||||
};
|
||||
|
||||
template <typename Sink>
|
||||
|
||||
@@ -72,7 +72,13 @@ cc_library(
|
||||
"timer.h",
|
||||
"utils.h",
|
||||
],
|
||||
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
|
||||
defines = [
|
||||
"_SILENCE_CLANG_COROUTINE_MESSAGE",
|
||||
"_WIN32_WINNT=_WIN32_WINNT_WIN10",
|
||||
],
|
||||
linkopts = [
|
||||
"powrprof.lib",
|
||||
],
|
||||
tags = ["windows"],
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
|
||||
@@ -14,13 +14,19 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/device_info.h"
|
||||
|
||||
// clang-format off
|
||||
#include <shlobj_core.h>
|
||||
#include <windows.h>
|
||||
#include <wtsapi32.h>
|
||||
#include <powrprof.h>
|
||||
#include <powerbase.h>
|
||||
// clang-format on
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
@@ -30,10 +36,19 @@
|
||||
#include "internal/platform/implementation/windows/device_paths.h"
|
||||
#include "internal/platform/implementation/windows/string_utils.h"
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby::windows {
|
||||
|
||||
namespace {
|
||||
using ::nearby::windows::string_utils::WideStringToString;
|
||||
} // namespace
|
||||
|
||||
DeviceInfo::~DeviceInfo() {
|
||||
if (suspend_resume_notification_handle_ != nullptr) {
|
||||
PowerUnregisterSuspendResumeNotification(
|
||||
suspend_resume_notification_handle_);
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> DeviceInfo::GetOsDeviceName() const {
|
||||
std::optional<std::wstring> device_name = GetDnsHostName();
|
||||
@@ -113,4 +128,61 @@ bool DeviceInfo::AllowSleep() {
|
||||
return session_manager_.AllowSleep();
|
||||
}
|
||||
|
||||
ULONG DeviceInfo::PowerSuspendResumeCallback(PVOID context, ULONG type,
|
||||
PVOID setting) {
|
||||
api::DeviceInfo::SuspendResumeEvent event;
|
||||
switch (type) {
|
||||
case PBT_APMSUSPEND:
|
||||
event = api::DeviceInfo::SuspendResumeEvent::kSuspend;
|
||||
break;
|
||||
case PBT_APMRESUMESUSPEND:
|
||||
event = api::DeviceInfo::SuspendResumeEvent::kResume;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
DeviceInfo* device_info = static_cast<DeviceInfo*>(context);
|
||||
device_info->OnSuspendResumeEvent(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t DeviceInfo::RegisterSuspendResumeListener(
|
||||
std::function<void(api::DeviceInfo::SuspendResumeEvent)> callback) {
|
||||
absl::MutexLock lock(suspend_resume_mutex_);
|
||||
int64_t listener_id = ++next_suspend_resume_listener_id_;
|
||||
suspend_resume_listeners_.emplace(listener_id, std::move(callback));
|
||||
if (suspend_resume_listeners_.size() == 1) {
|
||||
DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS subscribe_params;
|
||||
subscribe_params.Callback = PowerSuspendResumeCallback;
|
||||
subscribe_params.Context = this;
|
||||
PowerRegisterSuspendResumeNotification(
|
||||
DEVICE_NOTIFY_CALLBACK, &subscribe_params,
|
||||
&suspend_resume_notification_handle_);
|
||||
}
|
||||
return listener_id;
|
||||
}
|
||||
|
||||
void DeviceInfo::UnregisterSuspendResumeListener(int64_t listener_id) {
|
||||
absl::MutexLock lock(suspend_resume_mutex_);
|
||||
suspend_resume_listeners_.erase(listener_id);
|
||||
if (suspend_resume_listeners_.empty()) {
|
||||
if (suspend_resume_notification_handle_ != nullptr) {
|
||||
PowerUnregisterSuspendResumeNotification(
|
||||
suspend_resume_notification_handle_);
|
||||
}
|
||||
suspend_resume_notification_handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceInfo::OnSuspendResumeEvent(
|
||||
api::DeviceInfo::SuspendResumeEvent event) {
|
||||
LOG(INFO) << "OnSuspendResumeEvent: "
|
||||
<< (event == DeviceInfo::SuspendResumeEvent::kSuspend ? "kSuspend"
|
||||
: "kResume");
|
||||
absl::MutexLock lock(suspend_resume_mutex_);
|
||||
for (auto& it : suspend_resume_listeners_) {
|
||||
it.second(event);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nearby::windows
|
||||
|
||||
@@ -15,11 +15,19 @@
|
||||
#ifndef PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
|
||||
#define PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
|
||||
|
||||
// clang-format off
|
||||
#include <windows.h>
|
||||
#include <powerbase.h>
|
||||
// clang-format on
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/base/thread_annotations.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/base/file_path.h"
|
||||
@@ -31,7 +39,7 @@ namespace windows {
|
||||
|
||||
class DeviceInfo : public api::DeviceInfo {
|
||||
public:
|
||||
~DeviceInfo() override = default;
|
||||
~DeviceInfo() override;
|
||||
|
||||
std::optional<std::string> GetOsDeviceName() const override;
|
||||
api::DeviceInfo::DeviceType GetDeviceType() const override;
|
||||
@@ -51,9 +59,26 @@ class DeviceInfo : public api::DeviceInfo {
|
||||
bool PreventSleep() override;
|
||||
bool AllowSleep() override;
|
||||
|
||||
int64_t RegisterSuspendResumeListener(
|
||||
std::function<void(api::DeviceInfo::SuspendResumeEvent)> callback)
|
||||
override;
|
||||
void UnregisterSuspendResumeListener(int64_t listener_id) override;
|
||||
|
||||
private:
|
||||
static ULONG PowerSuspendResumeCallback(PVOID context, ULONG type,
|
||||
PVOID setting);
|
||||
void OnSuspendResumeEvent(SuspendResumeEvent event);
|
||||
|
||||
mutable absl::Mutex mutex_;
|
||||
SessionManager session_manager_ ABSL_GUARDED_BY(mutex_);
|
||||
absl::Mutex suspend_resume_mutex_;
|
||||
int64_t next_suspend_resume_listener_id_ ABSL_GUARDED_BY(
|
||||
suspend_resume_mutex_) = 0;
|
||||
absl::flat_hash_map<
|
||||
int64_t, absl::AnyInvocable<void(SuspendResumeEvent)>>
|
||||
suspend_resume_listeners_ ABSL_GUARDED_BY(suspend_resume_mutex_);
|
||||
HPOWERNOTIFY suspend_resume_notification_handle_
|
||||
ABSL_GUARDED_BY(suspend_resume_mutex_) = nullptr;
|
||||
};
|
||||
|
||||
} // namespace windows
|
||||
|
||||
Reference in New Issue
Block a user