Migrate device_info from location/nearby to third_party/nearby

PiperOrigin-RevId: 504935151
This commit is contained in:
Qin Wang
2023-01-26 14:01:11 -08:00
committed by Copybara-Service
parent 54fec27c00
commit a0692e6306
38 changed files with 942 additions and 625 deletions
-2
View File
@@ -36,7 +36,6 @@ cc_library(
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/internal/api:platform",
"//fastpair/scanning/fastpair:scanning",
"//internal/platform:base",
"//internal/platform:logging",
@@ -57,7 +56,6 @@ cc_test(
shard_count = 8,
deps = [
":fastpair_controller",
"//fastpair/internal/api:platform",
"//fastpair/scanning/fastpair:scanning",
"//internal/platform:logging",
"//internal/platform/implementation/g3",
-2
View File
@@ -36,7 +36,6 @@ lexan.cc_windows_dll(
],
deps = [
"//fastpair:fastpair_controller",
"//fastpair/internal/api:platform",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
@@ -67,7 +66,6 @@ lexan.cc_windows_dll(
],
deps = [
"//fastpair:fastpair_controller",
"//fastpair/internal/api:platform",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
-12
View File
@@ -1,12 +0,0 @@
licenses(["notice"])
cc_library(
name = "platform",
textual_hdrs = glob(["**/*.h"]),
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"@com_google_absl//absl/strings",
],
)
@@ -1,36 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_FAST_PAIR_PLATFORM_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_FAST_PAIR_PLATFORM_H_
#include <functional>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "fastpair/internal/api/device_info.h"
namespace nearby {
namespace api {
class ImplementationFastPairPlatform {
public:
static std::unique_ptr<DeviceInfo> CreateDeviceInfo();
};
} // namespace api
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_FAST_PAIR_PLATFORM_H_
-36
View File
@@ -1,36 +0,0 @@
licenses(["notice"])
cc_library(
name = "platform_g3",
srcs = [
"fast_pair_platform.cc",
],
hdrs = [
"device_info.h",
],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/internal/api:platform",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
],
)
cc_test(
name = "g3_test",
size = "small",
timeout = "short",
srcs = [
"device_info_test.cc",
],
shard_count = 8,
deps = [
":platform_g3",
"//fastpair/internal/api:platform",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/synchronization",
"@com_google_googletest//:gtest_main",
],
)
@@ -1,74 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/impl/g3/device_info.h"
#include <functional>
#include <string>
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "fastpair/internal/api/device_info.h"
namespace nearby {
namespace g3 {
namespace {
TEST(DeviceInfo, GetOsType) {
EXPECT_EQ(DeviceInfo().GetOsType(), api::DeviceInfo::OsType::kChromeOs);
}
TEST(DeviceInfo, IsScreenLocked) {
EXPECT_FALSE(DeviceInfo().IsScreenLocked());
}
TEST(DeviceInfo, RegisterScreenLockedListener) {
std::function<void(api::DeviceInfo::ScreenStatus)> listener_1 =
[](api::DeviceInfo::ScreenStatus) {};
std::function<void(api::DeviceInfo::ScreenStatus)> listener_2 =
[](api::DeviceInfo::ScreenStatus) {};
DeviceInfo device_info;
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 0);
device_info.RegisterScreenLockedListener("listener_1", listener_1);
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 1);
device_info.RegisterScreenLockedListener("listener_2", listener_2);
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 2);
}
TEST(DeviceInfo, UnregisterScreenLockedListener) {
std::function<void(api::DeviceInfo::ScreenStatus)> listener_1 =
[](api::DeviceInfo::ScreenStatus) {};
std::function<void(api::DeviceInfo::ScreenStatus)> listener_2 =
[](api::DeviceInfo::ScreenStatus) {};
DeviceInfo device_info;
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 0);
device_info.RegisterScreenLockedListener("listener_1", listener_1);
device_info.RegisterScreenLockedListener("listener_2", listener_2);
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 2);
device_info.UnregisterScreenLockedListener("listener_1");
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 1);
device_info.UnregisterScreenLockedListener("listener_2");
EXPECT_EQ(device_info.screen_locked_listeners_.size(), 0);
}
} // namespace
} // namespace g3
} // namespace nearby
@@ -1,30 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/api/fast_pair_platform.h"
#include <memory>
#include "fastpair/internal/impl/g3/device_info.h"
namespace nearby {
namespace api {
std::unique_ptr<api::DeviceInfo>
ImplementationFastPairPlatform::CreateDeviceInfo() {
return std::make_unique<g3::DeviceInfo>();
}
} // namespace api
} // namespace nearby
-32
View File
@@ -27,13 +27,10 @@ cc_library(
name = "platform_windows",
srcs = [
"ble_gatt_client.cc",
"device_info.cc",
"fast_pair_platform.cc",
"utils.cc",
],
hdrs = [
"ble_gatt_client.h",
"device_info.h",
"utils.h",
],
copts = [
@@ -42,7 +39,6 @@ cc_library(
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
visibility = ["//fastpair:__subpackages__"],
deps = [
"//fastpair/internal/api:platform",
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
@@ -54,31 +50,3 @@ cc_library(
"@com_google_absl//absl/synchronization",
],
)
cc_test(
name = "platform_windows_test",
size = "small",
timeout = "short",
srcs = [
"device_info_test.cc",
],
copts = [
"-Ithird_party/nearby/internal/platform/implementation/windows/generated",
],
shard_count = 1,
deps = [
":platform_windows",
":platform_windows_libs",
"//fastpair/internal/api:platform",
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform/implementation:types",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_googletest//:gtest_main",
],
)
@@ -1,200 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/impl/windows/device_info.h"
#include <shlobj_core.h>
#include <windows.h>
#include <wtsapi32.h>
#include <array>
#include <functional>
#include <optional>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/internal/api/device_info.h"
#include "internal/platform/logging.h"
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.System.h"
namespace nearby {
namespace windows {
constexpr char window_class_name[] = "FastPairDLL_MessageWindowClass";
constexpr char window_name[] = "FastPairDLL_MessageWindow";
namespace {
// This WindowProc method must be static for the successful initialization of
// WNDCLASS
// window_class.lpfnWndProc = (WNDPROC) &DeviceInfo::WindowProc;
// where a WNDPROC typed function pointer is expected
// typedef LRESULT (CALLBACK* WNDPROC)(HWND,UINT,WPARAM,LPARAM)
// the calling convention used here CALLBACK is a macro defined as
// #define CALLBACK __stdcall
//
// If WindProc is not static and defined as a member function, it uses the
// __thiscall calling convention instead
// https://docs.microsoft.com/en-us/cpp/cpp/thiscall?view=msvc-170
// https://isocpp.org/wiki/faq/pointers-to-members
// https://en.cppreference.com/w/cpp/language/pointer
//
// This is problematic because the function pointer now looks like this
// typedef LRESULT (CALLBACK* DeviceInfo_WNDPROC)(DeviceInfo*
// this,HWND,UINT,WPARAM,LPARAM)
// which causes casting errors
LRESULT CALLBACK WindowProc(HWND window_handle, UINT message, WPARAM wparam,
LPARAM lparam) {
DeviceInfo* self = reinterpret_cast<DeviceInfo*>(
GetWindowLongPtr(window_handle, GWLP_USERDATA));
CREATESTRUCT* create_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
LONG_PTR result = 0L;
switch (message) {
case WM_CREATE:
self = reinterpret_cast<DeviceInfo*>(create_struct->lpCreateParams);
self->message_window_handle_ = window_handle;
// Store pointer to the self to the window's user data.
SetLastError(0);
result = SetWindowLongPtr(window_handle, GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(self));
if (result == 0 && GetLastError() != 0) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error connecting message window to Fast Pair DLL.";
}
break;
case WM_WTSSESSION_CHANGE:
if (self) {
switch (wparam) {
case WTS_SESSION_LOCK:
for (auto& listener : self->screen_locked_listeners_) {
listener.second(api::DeviceInfo::ScreenStatus::
kLocked); // Trigger registered callbacks
}
break;
case WTS_SESSION_UNLOCK:
for (auto& listener : self->screen_locked_listeners_) {
listener.second(api::DeviceInfo::ScreenStatus::
kUnlocked); // Trigger registered callbacks
}
break;
}
}
break;
case WM_DESTROY:
SetLastError(0);
result = SetWindowLongPtr(window_handle, GWLP_USERDATA, NULL);
if (result == 0 && GetLastError() != 0) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error disconnecting message window to Fast Pair DLL.";
}
break;
}
return DefWindowProc(window_handle, message, wparam, lparam);
}
} // namespace
DeviceInfo::~DeviceInfo() {
UnregisterClass(MAKEINTATOM(registered_class_), instance_);
}
api::DeviceInfo::OsType DeviceInfo::GetOsType() const {
return api::DeviceInfo::OsType::kWindows;
}
bool DeviceInfo::IsScreenLocked() const {
DWORD session_id = WTSGetActiveConsoleSessionId();
WTS_INFO_CLASS wts_info_class = WTSSessionInfoEx;
LPTSTR session_info_buffer = nullptr;
DWORD session_info_buffer_size_bytes = 0;
WTSINFOEXW* wts_info = nullptr;
LONG session_state = WTS_SESSIONSTATE_UNKNOWN;
if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, session_id,
wts_info_class, &session_info_buffer,
&session_info_buffer_size_bytes)) {
if (session_info_buffer_size_bytes > 0) {
wts_info = (WTSINFOEXW*)session_info_buffer;
if (wts_info->Level == 1) {
session_state = wts_info->Data.WTSInfoExLevel1.SessionFlags;
}
}
WTSFreeMemory(session_info_buffer);
session_info_buffer = nullptr;
}
bool isScreenLocked = session_state == WTS_SESSIONSTATE_LOCK;
NEARBY_LOGS(INFO) << __func__ << "ScreenStatus: "
<< (isScreenLocked ? "Locked" : "NotLocked");
return isScreenLocked;
}
void DeviceInfo::RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
if (message_window_handle_ == nullptr) {
instance_ = (HINSTANCE)GetModuleHandle(NULL);
WNDCLASS window_class;
window_class.style = 0;
window_class.lpfnWndProc = (WNDPROC)&WindowProc;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = instance_;
window_class.hIcon = nullptr;
window_class.hCursor = nullptr;
window_class.hbrBackground = nullptr;
window_class.lpszMenuName = nullptr;
window_class.lpszClassName = window_class_name;
registered_class_ = RegisterClass(&window_class);
message_window_handle_ = CreateWindow(
MAKEINTATOM(registered_class_), // class atom from RegisterClass
window_name, // window name
0, // window style
0, // initial x position of window
0, // initial y position of window
0, // width
0, // height
HWND_MESSAGE, // handle to the parent of window
// (message-only window in this case)
nullptr, // handle to a menu
instance_, // handle to the instance of the module
// associated to the window
this); // pointer to be passed to the window for additional data
if (!message_window_handle_) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Failed to create message window for Fast Pair DLL.";
}
}
screen_locked_listeners_.emplace(listener_name, callback);
}
void DeviceInfo::UnregisterScreenLockedListener(
absl::string_view listener_name) {
screen_locked_listeners_.erase(listener_name);
}
} // namespace windows
} // namespace nearby
@@ -1,33 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/api/fast_pair_platform.h"
#include <functional>
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "fastpair/internal/impl/windows/device_info.h"
namespace nearby {
namespace api {
std::unique_ptr<api::DeviceInfo>
ImplementationFastPairPlatform::CreateDeviceInfo() {
return std::make_unique<windows::DeviceInfo>();
}
} // namespace api
} // namespace nearby
-32
View File
@@ -1,32 +0,0 @@
licenses(["notice"])
cc_library(
name = "types",
hdrs = [
"device_info.h",
],
visibility = ["//fastpair:__subpackages__"],
deps = [
"//fastpair/internal/api:platform",
"//fastpair/internal/impl/g3:platform_g3",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
],
)
cc_library(
name = "device_info",
srcs = [
"device_info_impl.cc",
],
hdrs = [
"device_info_impl.h",
],
visibility = ["//fastpair:__subpackages__"],
deps = [
":types",
"//fastpair/internal/api:platform",
"//fastpair/internal/impl/g3:platform_g3",
],
)
-45
View File
@@ -1,45 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_H_
#include <functional>
#include <string>
#include "absl/strings/string_view.h"
#include "fastpair/internal/api/device_info.h"
#include "fastpair/internal/api/fast_pair_platform.h"
namespace nearby {
namespace fastpair {
class DeviceInfo {
public:
virtual ~DeviceInfo() = default;
virtual api::DeviceInfo::OsType GetOsType() const = 0;
virtual bool IsScreenLocked() const = 0;
virtual void RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) = 0;
virtual void UnregisterScreenLockedListener(
absl::string_view listener_name) = 0;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_H_
@@ -1,43 +0,0 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/public/device_info_impl.h"
#include <functional>
#include <string>
namespace nearby {
namespace fastpair {
api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const {
return device_info_impl_->GetOsType();
}
bool DeviceInfoImpl::IsScreenLocked() const {
return device_info_impl_->IsScreenLocked();
}
void DeviceInfoImpl::RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
device_info_impl_->RegisterScreenLockedListener(listener_name, callback);
}
void DeviceInfoImpl::UnregisterScreenLockedListener(
absl::string_view listener_name) {
device_info_impl_->UnregisterScreenLockedListener(listener_name);
}
} // namespace fastpair
} // namespace nearby
+1 -1
View File
@@ -18,10 +18,10 @@ cc_library(
visibility = ["//fastpair:__subpackages__"],
deps = [
"//fastpair/common",
"//fastpair/internal/api:platform",
"//fastpair/proto:fastpair_cc_proto",
"//internal/network:types",
"//internal/platform:logging",
"//internal/platform/implementation:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
@@ -19,7 +19,7 @@
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/internal/api/device_info.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/logging.h"
namespace nearby {
@@ -22,10 +22,10 @@
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "fastpair/internal/api/device_info.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "internal/network/http_client.h"
#include "internal/network/url.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace fastpair {
@@ -23,15 +23,15 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "fastpair/common/fast_pair_http_result.h"
#include "fastpair/internal/api/device_info.h"
#include "fastpair/proto/fastpair_rpcs.proto.h"
#include "fastpair/repository/fast_pair_metadata_fetcher.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "fastpair/repository/fast_pair_metadata_fetcher_impl.h"
#include "fastpair/repository/fast_pair_metadata_repository.h"
#include "internal/network/http_client.h"
#include "internal/network/http_client_factory.h"
#include "internal/network/url.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/logging.h"
#include "internal/network/http_client.h"
namespace nearby {
namespace fastpair {
+1
View File
@@ -33,6 +33,7 @@ cc_library(
],
visibility = [
"//fastpair:__subpackages__",
"//internal:__subpackages__",
"//location/nearby/cpp/fastpair:__subpackages__",
],
deps = [
+4
View File
@@ -303,6 +303,7 @@ cc_test(
cc_library(
name = "types",
srcs = [
"device_info_impl.cc",
"monitored_runnable.cc",
"pending_job_registry.cc",
"pipe.cc",
@@ -319,6 +320,8 @@ cc_library(
"condition_variable.h",
"count_down_latch.h",
"crypto.h",
"device_info.h",
"device_info_impl.h",
"direct_executor.h",
"file.h",
"future.h",
@@ -362,6 +365,7 @@ cc_library(
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_glog//:glog",
+73
View File
@@ -0,0 +1,73 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_PUBLIC_DEVICE_INFO_H_
#define PLATFORM_PUBLIC_DEVICE_INFO_H_
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/platform.h"
namespace nearby {
class DeviceInfo {
public:
virtual ~DeviceInfo() = default;
virtual std::u16string GetOsDeviceName() const = 0;
virtual api::DeviceInfo::DeviceType GetDeviceType() const = 0;
virtual api::DeviceInfo::OsType GetOsType() const = 0;
virtual std::optional<std::u16string> GetFullName() const = 0;
virtual std::optional<std::u16string> GetGivenName() const = 0;
virtual std::optional<std::u16string> GetLastName() const = 0;
virtual std::optional<std::string> GetProfileUserName() const = 0;
virtual std::filesystem::path GetDownloadPath() const = 0;
virtual std::filesystem::path GetAppDataPath() const = 0;
virtual std::filesystem::path GetTemporaryPath() const = 0;
virtual std::optional<size_t> GetAvailableDiskSpaceInBytes(
const std::filesystem::path& path) const = 0;
virtual bool IsScreenLocked() const = 0;
virtual void RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) = 0;
virtual void UnregisterScreenLockedListener(
absl::string_view listener_name) = 0;
// Returns localized device name depends on device type.
std::u16string GetDeviceTypeName() const {
// TODO(b/230132370): return localized device name.
switch (GetDeviceType()) {
case api::DeviceInfo::DeviceType::kPhone:
return u"Phone";
case api::DeviceInfo::DeviceType::kTablet:
return u"Tablet";
case api::DeviceInfo::DeviceType::kLaptop:
return u"PC";
default:
return u"Unknown";
}
}
};
} // namespace nearby
#endif // PLATFORM_PUBLIC_DEVICE_INFO_H_
+111
View File
@@ -0,0 +1,111 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/device_info_impl.h"
#include <functional>
#include <optional>
#include <string>
#include <system_error>
namespace nearby {
std::u16string DeviceInfoImpl::GetOsDeviceName() const {
std::optional<std::u16string> device_name =
device_info_impl_->GetOsDeviceName();
if (device_name.has_value()) {
return *device_name;
}
return u"unknown";
}
api::DeviceInfo::DeviceType DeviceInfoImpl::GetDeviceType() const {
return device_info_impl_->GetDeviceType();
}
api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const {
return device_info_impl_->GetOsType();
}
std::optional<std::u16string> DeviceInfoImpl::GetFullName() const {
return device_info_impl_->GetFullName();
}
std::optional<std::u16string> DeviceInfoImpl::GetGivenName() const {
return device_info_impl_->GetGivenName();
}
std::optional<std::u16string> DeviceInfoImpl::GetLastName() const {
return device_info_impl_->GetLastName();
}
std::optional<std::string> DeviceInfoImpl::GetProfileUserName() const {
return device_info_impl_->GetProfileUserName();
}
std::filesystem::path DeviceInfoImpl::GetDownloadPath() const {
std::optional<std::filesystem::path> path =
device_info_impl_->GetDownloadPath();
if (path.has_value()) {
return *path;
}
return std::filesystem::temp_directory_path();
}
std::filesystem::path DeviceInfoImpl::GetAppDataPath() const {
std::optional<std::filesystem::path> path =
device_info_impl_->GetAppDataPath();
if (path.has_value()) {
return *path;
}
return std::filesystem::temp_directory_path();
}
std::filesystem::path DeviceInfoImpl::GetTemporaryPath() const {
std::optional<std::filesystem::path> path =
device_info_impl_->GetTemporaryPath();
if (path.has_value()) {
return *path;
}
return std::filesystem::temp_directory_path();
}
std::optional<size_t> DeviceInfoImpl::GetAvailableDiskSpaceInBytes(
const std::filesystem::path& path) const {
std::error_code error_code;
std::filesystem::space_info space_info =
std::filesystem::space(path, error_code);
if (error_code.value() == 0) {
return space_info.available;
}
return std::nullopt;
}
bool DeviceInfoImpl::IsScreenLocked() const {
return device_info_impl_->IsScreenLocked();
}
void DeviceInfoImpl::RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
device_info_impl_->RegisterScreenLockedListener(listener_name, callback);
}
void DeviceInfoImpl::UnregisterScreenLockedListener(
absl::string_view listener_name) {
device_info_impl_->UnregisterScreenLockedListener(listener_name);
}
} // namespace nearby
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,27 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_IMPL_H_
#ifndef PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
#define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include "fastpair/internal/api/fast_pair_platform.h"
#include "fastpair/internal/public/device_info.h"
#include "internal/platform/device_info.h"
#include "internal/platform/implementation/platform.h"
namespace nearby {
namespace fastpair {
class DeviceInfoImpl : public DeviceInfo {
public:
DeviceInfoImpl()
: device_info_impl_(
api::ImplementationFastPairPlatform::CreateDeviceInfo()) {}
: device_info_impl_(api::ImplementationPlatform::CreateDeviceInfo()) {}
std::u16string GetOsDeviceName() const override;
api::DeviceInfo::DeviceType GetDeviceType() const override;
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::u16string> GetFullName() const override;
std::optional<std::u16string> GetGivenName() const override;
std::optional<std::u16string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::filesystem::path GetDownloadPath() const override;
std::filesystem::path GetAppDataPath() const override;
std::filesystem::path GetTemporaryPath() const override;
std::optional<size_t> GetAvailableDiskSpaceInBytes(
const std::filesystem::path& path) const override;
bool IsScreenLocked() const override;
void RegisterScreenLockedListener(
absl::string_view listener_name,
@@ -42,8 +56,6 @@ class DeviceInfoImpl : public DeviceInfo {
private:
std::unique_ptr<api::DeviceInfo> device_info_impl_;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_PUBLIC_DEVICE_INFO_IMPL_H_
#endif // PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
+2
View File
@@ -23,6 +23,7 @@ cc_library(
"condition_variable.h",
"count_down_latch.h",
"crypto.h",
"device_info.h",
"executor.h",
"future.h",
"input_file.h",
@@ -109,6 +110,7 @@ cc_library(
"//connections/implementation:__subpackages__",
"//fastpair:__subpackages__",
"//internal:__pkg__",
"//internal:__subpackages__",
"//internal/network:__subpackages__",
"//internal/platform:__pkg__",
"//internal/platform/implementation:__subpackages__",
@@ -25,6 +25,7 @@ objc_library(
"ble.mm",
"bluetooth_adapter.mm",
"crypto.mm",
"device_info.mm",
"log_message.mm",
"multi_thread_executor.mm",
"platform.mm",
@@ -36,6 +37,7 @@ objc_library(
hdrs = [
"ble.h",
"bluetooth_adapter.h",
"device_info.h",
"log_message.h",
"multi_thread_executor.h",
"scheduled_executor.h",
@@ -0,0 +1,60 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PLATFORM_IMPL_APPLE_DEVICE_INFO_H_
#define PLATFORM_IMPL_APPLE_DEVICE_INFO_H_
#include <filesystem> // NOLINT(build/c++17)
#include <functional>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace apple {
class DeviceInfo : public api::DeviceInfo {
public:
std::optional<std::u16string> GetOsDeviceName() const override;
api::DeviceInfo::DeviceType GetDeviceType() const override;
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::u16string> GetFullName() const override;
std::optional<std::u16string> GetGivenName() const override;
std::optional<std::u16string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetAppDataPath() const override;
std::optional<std::filesystem::path> GetTemporaryPath() const override;
bool IsScreenLocked() const override;
void RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override;
void UnregisterScreenLockedListener(absl::string_view listener_name) override;
};
} // namespace apple
} // namespace nearby
#endif // PLATFORM_IMPL_APPLE_DEVICE_INFO_H_
@@ -0,0 +1,62 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "internal/platform/implementation/apple/device_info.h"
#include <filesystem> // NOLINT(build/c++17)
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace apple {
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const { return u"OS Device Name"; }
api::DeviceInfo::DeviceType DeviceInfo::GetDeviceType() const {
return api::DeviceInfo::DeviceType::kLaptop;
}
api::DeviceInfo::OsType DeviceInfo::GetOsType() const { return api::DeviceInfo::OsType::kIos; }
std::optional<std::u16string> DeviceInfo::GetFullName() const { return u"Full Name"; }
std::optional<std::u16string> DeviceInfo::GetGivenName() const { return u"Given Name"; }
std::optional<std::u16string> DeviceInfo::GetLastName() const { return u"Last Name"; }
std::optional<std::string> DeviceInfo::GetProfileUserName() const { return "Username"; }
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
return std::filesystem::temp_directory_path();
}
std::optional<std::filesystem::path> DeviceInfo::GetAppDataPath() const {
return std::filesystem::temp_directory_path();
}
std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
return std::filesystem::temp_directory_path();
}
bool DeviceInfo::IsScreenLocked() const { return false; }
void DeviceInfo::RegisterScreenLockedListener(
absl::string_view listener_name, std::function<void(api::DeviceInfo::ScreenStatus)> callback) {}
void DeviceInfo::UnregisterScreenLockedListener(absl::string_view listener_name) {}
} // namespace apple
} // namespace nearby
@@ -22,6 +22,7 @@
#include "internal/platform/implementation/apple/ble.h"
#include "internal/platform/implementation/apple/condition_variable.h"
#include "internal/platform/implementation/apple/count_down_latch.h"
#include "internal/platform/implementation/apple/device_info.h"
#import "internal/platform/implementation/apple/log_message.h"
#import "internal/platform/implementation/apple/multi_thread_executor.h"
#include "internal/platform/implementation/apple/mutex.h"
@@ -182,5 +183,10 @@ std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<apple::Timer>();
}
// TODO(b/261511530): Add implementation.
std::unique_ptr<nearby::api::DeviceInfo> ImplementationPlatform::CreateDeviceInfo() {
return std::make_unique<apple::DeviceInfo>();
}
} // namespace api
} // namespace nearby
@@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_DEVICE_INFO_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_DEVICE_INFO_H_
#ifndef PLATFORM_API_DEVICE_INFO_H_
#define PLATFORM_API_DEVICE_INFO_H_
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include "absl/strings/string_view.h"
@@ -25,24 +27,28 @@ namespace api {
class DeviceInfo {
public:
enum class ScreenStatus {
kUndetermined = 0,
kLocked = 1,
kUnlocked = 2,
};
enum class OsType {
kUnknown = 0,
kAndroid = 1,
kChromeOs = 2,
kIos = 3,
kWindows = 4,
};
enum class ScreenStatus { kUndetermined = 0, kLocked, kUnlocked };
enum class DeviceType { kUnknown = 0, kPhone, kTablet, kLaptop };
enum class OsType { kUnknown = 0, kAndroid, kChromeOs, kIos, kWindows };
virtual ~DeviceInfo() = default;
// Gets device name.
virtual std::optional<std::u16string> GetOsDeviceName() const = 0;
virtual DeviceType GetDeviceType() const = 0;
virtual OsType GetOsType() const = 0;
// Gets basic information of current user.
virtual std::optional<std::u16string> GetFullName() const = 0;
virtual std::optional<std::u16string> GetGivenName() const = 0;
virtual std::optional<std::u16string> GetLastName() const = 0;
virtual std::optional<std::string> GetProfileUserName() const = 0;
// Gets known paths of current user.
virtual std::optional<std::filesystem::path> GetDownloadPath() const = 0;
virtual std::optional<std::filesystem::path> GetAppDataPath() const = 0;
virtual std::optional<std::filesystem::path> GetTemporaryPath() const = 0;
// Monitor screen status
virtual bool IsScreenLocked() const = 0;
virtual void RegisterScreenLockedListener(
@@ -55,4 +61,4 @@ class DeviceInfo {
} // namespace api
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_API_DEVICE_INFO_H_
#endif // PLATFORM_API_DEVICE_INFO_H_
@@ -25,6 +25,7 @@ cc_library(
"atomic_boolean.h",
"atomic_reference.h",
"condition_variable.h",
"device_info.h",
"log_message.h",
"multi_thread_executor.h",
"mutex.h",
@@ -41,6 +42,8 @@ cc_library(
"//internal/platform/implementation:types",
"//internal/platform/implementation/shared:count_down_latch",
"//internal/platform/implementation/shared:posix_mutex",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_glog//:glog",
@@ -12,26 +12,61 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_G3_DEVICE_INFO_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_G3_DEVICE_INFO_H_
#ifndef PLATFORM_IMPL_G3_DEVICE_INFO_H_
#define PLATFORM_IMPL_G3_DEVICE_INFO_H_
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/internal/api/device_info.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace g3 {
class DeviceInfo : public api::DeviceInfo {
public:
api::DeviceInfo::OsType GetOsType() const override {
std::optional<std::u16string> GetOsDeviceName() const override {
return u"Windows";
}
api::DeviceInfo::DeviceType GetDeviceType() const override {
return api::DeviceInfo::DeviceType::kLaptop;
}
api::DeviceInfo::OsType GetOsType() const override{
return api::DeviceInfo::OsType::kChromeOs;
}
std::optional<std::u16string> GetFullName() const override {
return u"nearby";
}
std::optional<std::u16string> GetGivenName() const override {
return u"nearby";
}
std::optional<std::u16string> GetLastName() const override {
return u"nearby";
}
std::optional<std::string> GetProfileUserName() const override {
return "nearby";
}
std::optional<std::filesystem::path> GetDownloadPath() const override {
return std::filesystem::temp_directory_path();
}
std::optional<std::filesystem::path> GetAppDataPath() const override {
return std::filesystem::temp_directory_path();
}
std::optional<std::filesystem::path> GetTemporaryPath() const override {
return std::filesystem::temp_directory_path();
}
bool IsScreenLocked() const override { return false; }
void RegisterScreenLockedListener(
@@ -45,6 +80,7 @@ class DeviceInfo : public api::DeviceInfo {
screen_locked_listeners_.erase(listener_name);
}
private:
absl::flat_hash_map<std::string,
std::function<void(api::DeviceInfo::ScreenStatus)>>
screen_locked_listeners_;
@@ -53,4 +89,4 @@ class DeviceInfo : public api::DeviceInfo {
} // namespace g3
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_G3_DEVICE_INFO_H_
#endif // PLATFORM_IMPL_G3_DEVICE_INFO_H_
@@ -48,6 +48,7 @@
#include "internal/platform/implementation/g3/bluetooth_classic.h"
#include "internal/platform/implementation/g3/condition_variable.h"
#include "internal/platform/implementation/g3/credential_storage_impl.h"
#include "internal/platform/implementation/g3/device_info.h"
#include "internal/platform/implementation/g3/log_message.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/mutex.h"
@@ -229,5 +230,10 @@ std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<g3::Timer>();
}
std::unique_ptr<nearby::api::DeviceInfo>
ImplementationPlatform::CreateDeviceInfo() {
return std::make_unique<g3::DeviceInfo>();
}
} // namespace api
} // namespace nearby
@@ -31,6 +31,7 @@
#include "internal/platform/implementation/count_down_latch.h"
#include "internal/platform/implementation/credential_storage.h"
#include "internal/platform/implementation/crypto.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/http_loader.h"
#include "internal/platform/implementation/input_file.h"
#include "internal/platform/implementation/log_message.h"
@@ -130,6 +131,7 @@ class ImplementationPlatform {
static std::unique_ptr<WifiHotspotMedium> CreateWifiHotspotMedium();
static std::unique_ptr<WifiDirectMedium> CreateWifiDirectMedium();
static std::unique_ptr<Timer> CreateTimer();
static std::unique_ptr<DeviceInfo> CreateDeviceInfo();
#ifndef NO_WEBRTC
static std::unique_ptr<WebRtcMedium> CreateWebRtcMedium();
#endif
@@ -16,6 +16,7 @@ licenses(["notice"])
cc_library(
name = "types",
srcs = [
"device_info.cc",
"log_message.cc",
"timer.cc",
],
@@ -24,6 +25,7 @@ cc_library(
"atomic_reference.h",
"bluetooth_adapter.h",
"condition_variable.h",
"device_info.h",
"executor.h",
"future.h",
"input_file.h",
@@ -37,9 +39,12 @@ cc_library(
"timer.h",
"utils.h",
],
copts = ["-Ithird_party/nearby/internal/platform/implementation/windows/generated"],
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
deps = [
"//base",
"//base:stringprintf",
"//internal/base:bluetooth_address",
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform/implementation:types",
@@ -219,6 +224,7 @@ cc_test(
"bluetooth_adapter_test.cc",
"count_down_latch_test.cc",
"crypto_test.cc",
"device_info_test.cc",
"executor_test.cc",
"file_path_test.cc",
"http_loader_test.cc",
@@ -0,0 +1,447 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "internal/platform/implementation/windows/device_info.h"
#include <shlobj_core.h>
#include <windows.h>
#include <wtsapi32.h>
#include <array>
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "internal/base/bluetooth_address.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/logging.h"
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.System.h"
namespace nearby {
namespace windows {
using IInspectable = winrt::Windows::Foundation::IInspectable;
using KnownUserProperties = winrt::Windows::System::KnownUserProperties;
using User = winrt::Windows::System::User;
using UserType = winrt::Windows::System::UserType;
using UserAuthenticationStatus =
winrt::Windows::System::UserAuthenticationStatus;
template <typename T>
using IVectorView = winrt::Windows::Foundation::Collections::IVectorView<T>;
template <typename T>
using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation<T>;
constexpr char window_class_name[] = "NearbySharingDLL_MessageWindowClass";
constexpr char window_name[] = "NearbySharingDLL_MessageWindow";
namespace {
// This WindowProc method must be static for the successful initialization of
// WNDCLASS
// window_class.lpfnWndProc = (WNDPROC) &DeviceInfo::WindowProc;
// where a WNDPROC typed function pointer is expected
// typedef LRESULT (CALLBACK* WNDPROC)(HWND,UINT,WPARAM,LPARAM)
// the calling convention used here CALLBACK is a macro defined as
// #define CALLBACK __stdcall
//
// If WindProc is not static and defined as a member function, it uses the
// __thiscall calling convention instead
// https://docs.microsoft.com/en-us/cpp/cpp/thiscall?view=msvc-170
// https://isocpp.org/wiki/faq/pointers-to-members
// https://en.cppreference.com/w/cpp/language/pointer
//
// This is problematic because the function pointer now looks like this
// typedef LRESULT (CALLBACK* DeviceInfo_WNDPROC)(DeviceInfo*
// this,HWND,UINT,WPARAM,LPARAM)
// which causes casting errors
LRESULT CALLBACK WindowProc(HWND window_handle, UINT message, WPARAM wparam,
LPARAM lparam) {
DeviceInfo* self = reinterpret_cast<DeviceInfo*>(
GetWindowLongPtr(window_handle, GWLP_USERDATA));
CREATESTRUCT* create_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
LONG_PTR result = 0L;
switch (message) {
case WM_CREATE:
self = reinterpret_cast<DeviceInfo*>(create_struct->lpCreateParams);
self->message_window_handle_ = window_handle;
// Store pointer to the self to the window's user data.
SetLastError(0);
result = SetWindowLongPtr(window_handle, GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(self));
if (result == 0 && GetLastError() != 0) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error connecting message window to Nearby Sharing DLL.";
}
break;
case WM_WTSSESSION_CHANGE:
if (self) {
switch (wparam) {
case WTS_SESSION_LOCK:
for (auto& listener : self->screen_locked_listeners_) {
listener.second(api::DeviceInfo::ScreenStatus::
kLocked); // Trigger registered callbacks
}
break;
case WTS_SESSION_UNLOCK:
for (auto& listener : self->screen_locked_listeners_) {
listener.second(api::DeviceInfo::ScreenStatus::
kUnlocked); // Trigger registered callbacks
}
break;
}
}
break;
case WM_DESTROY:
SetLastError(0);
result = SetWindowLongPtr(window_handle, GWLP_USERDATA, NULL);
if (result == 0 && GetLastError() != 0) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error disconnecting message window to Nearby Sharing DLL.";
}
break;
}
return DefWindowProc(window_handle, message, wparam, lparam);
}
} // namespace
DeviceInfo::~DeviceInfo() {
UnregisterClass(MAKEINTATOM(registered_class_), instance_);
}
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const {
DWORD size = 0;
// Get length of the computer name.
if (!GetComputerNameExW(ComputerNameDnsHostname, nullptr, &size)) {
if (GetLastError() != ERROR_MORE_DATA) {
NEARBY_LOGS(ERROR) << ": Failed to get device name size, error:"
<< GetLastError();
return std::nullopt;
}
}
WCHAR device_name[size];
if (GetComputerNameExW(ComputerNameDnsHostname, device_name, &size)) {
std::wstring wide_name(device_name);
return std::u16string(wide_name.begin(), wide_name.end());
}
NEARBY_LOGS(ERROR) << ": Failed to get device name, error:" << GetLastError();
return std::nullopt;
}
api::DeviceInfo::DeviceType DeviceInfo::GetDeviceType() const {
// TODO(b/230132370): return correct device type on the Windows platform.
return api::DeviceInfo::DeviceType::kLaptop;
}
api::DeviceInfo::OsType DeviceInfo::GetOsType() const {
return api::DeviceInfo::OsType::kWindows;
}
std::optional<std::u16string> DeviceInfo::GetFullName() const {
// FindAllAsync finds all users that are using this app. When we "Switch User"
// on Desktop,FindAllAsync() will still return the current user instead of all
// of them because the users who are switched out are not using the apps of
// the user who is switched in, so FindAllAsync() will not find them. (Under
// the UWP application model, each process runs under its own user account.
// That user account is different from the user account of the logged-in user.
// Processes aren't owned by the logged-in user for purposes of isolation.)
IVectorView<User> users =
User::FindAllAsync(UserType::LocalUser,
UserAuthenticationStatus::LocallyAuthenticated)
.get();
if (users == nullptr) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error retrieving locally authenticated user.";
return std::nullopt;
}
// On Windows Desktop apps, the first Windows.System.User instance
// returned in the IVectorView is always the current user.
// https://github.com/microsoft/Windows-task-snippets/blob/master/tasks/User-info.md
User current_user = users.GetAt(0);
// Retrieve the human-readable properties for the current user
IAsyncOperation<IInspectable> full_name_obj_async =
current_user.GetPropertyAsync(KnownUserProperties::DisplayName());
IInspectable full_name_obj = full_name_obj_async.get();
if (full_name_obj == nullptr) {
NEARBY_LOGS(ERROR) << __func__ << ": Error retrieving full name of user.";
return std::nullopt;
}
winrt::hstring full_name = full_name_obj.as<winrt::hstring>();
std::wstring wstr(full_name);
std::u16string u16str(wstr.begin(), wstr.end());
if (u16str.empty()) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Error unboxing string value for full name of user.";
return std::nullopt;
}
return u16str;
}
std::optional<std::u16string> DeviceInfo::GetGivenName() const {
// FindAllAsync finds all users that are using this app. When we "Switch User"
// on Desktop,FindAllAsync() will still return the current user instead of all
// of them because the users who are switched out are not using the apps of
// the user who is switched in, so FindAllAsync() will not find them. (Under
// the UWP application model, each process runs under its own user account.
// That user account is different from the user account of the logged-in user.
// Processes aren't owned by the logged-in user for purposes of isolation.)
IVectorView<User> users =
User::FindAllAsync(UserType::LocalUser,
UserAuthenticationStatus::LocallyAuthenticated)
.get();
if (users == nullptr) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error retrieving locally authenticated user.";
return std::nullopt;
}
// On Windows Desktop apps, the first Windows.System.User instance
// returned in the IVectorView is always the current user.
// https://github.com/microsoft/Windows-task-snippets/blob/master/tasks/User-info.md
User current_user = users.GetAt(0);
// Retrieve the human-readable properties for the current user
IAsyncOperation<IInspectable> given_name_obj_async =
current_user.GetPropertyAsync(KnownUserProperties::FirstName());
IInspectable given_name_obj = given_name_obj_async.get();
if (given_name_obj == nullptr) {
NEARBY_LOGS(ERROR) << __func__ << ": Error retrieving first name of user.";
return std::nullopt;
}
winrt::hstring given_name = given_name_obj.as<winrt::hstring>();
std::wstring wstr(given_name);
std::u16string u16str(wstr.begin(), wstr.end());
if (u16str.empty()) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Error unboxing string value for first name of user.";
return std::nullopt;
}
return u16str;
}
std::optional<std::u16string> DeviceInfo::GetLastName() const {
// FindAllAsync finds all users that are using this app. When we "Switch User"
// on Desktop,FindAllAsync() will still return the current user instead of all
// of them because the users who are switched out are not using the apps of
// the user who is switched in, so FindAllAsync() will not find them. (Under
// the UWP application model, each process runs under its own user account.
// That user account is different from the user account of the logged-in user.
// Processes aren't owned by the logged-in user for purposes of isolation.)
IVectorView<User> users =
User::FindAllAsync(UserType::LocalUser,
UserAuthenticationStatus::LocallyAuthenticated)
.get();
if (users == nullptr) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error retrieving locally authenticated user.";
return std::nullopt;
}
// On Windows Desktop apps, the first Windows.System.User instance
// returned in the IVectorView is always the current user.
// https://github.com/microsoft/Windows-task-snippets/blob/master/tasks/User-info.md
User current_user = users.GetAt(0);
// Retrieve the human-readable properties for the current user
IAsyncOperation<IInspectable> last_name_obj_async =
current_user.GetPropertyAsync(KnownUserProperties::LastName());
IInspectable last_name_obj = last_name_obj_async.get();
if (last_name_obj == nullptr) {
NEARBY_LOGS(ERROR) << __func__ << ": Error retrieving last name of user.";
return std::nullopt;
}
winrt::hstring last_name = last_name_obj.as<winrt::hstring>();
std::wstring wstr(last_name);
std::u16string u16str(wstr.begin(), wstr.end());
if (u16str.empty()) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Error unboxing string value for last name of user.";
return std::nullopt;
}
return u16str;
}
std::optional<std::string> DeviceInfo::GetProfileUserName() const {
// FindAllAsync finds all users that are using this app. When we "Switch User"
// on Desktop,FindAllAsync() will still return the current user instead of all
// of them because the users who are switched out are not using the apps of
// the user who is switched in, so FindAllAsync() will not find them. (Under
// the UWP application model, each process runs under its own user account.
// That user account is different from the user account of the logged-in user.
// Processes aren't owned by the logged-in user for purposes of isolation.)
IVectorView<User> users =
User::FindAllAsync(UserType::LocalUser,
UserAuthenticationStatus::LocallyAuthenticated)
.get();
if (users == nullptr) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error retrieving locally authenticated user.";
return std::nullopt;
}
// On Windows Desktop apps, the first Windows.System.User instance
// returned in the IVectorView is always the current user.
// https://github.com/microsoft/Windows-task-snippets/blob/master/tasks/User-info.md
User current_user = users.GetAt(0);
// Retrieve the human-readable properties for the current user
IAsyncOperation<IInspectable> account_name_obj_async =
current_user.GetPropertyAsync(KnownUserProperties::AccountName());
IInspectable account_name_obj = account_name_obj_async.get();
if (account_name_obj == nullptr) {
NEARBY_LOGS(ERROR) << __func__
<< ": Error retrieving account name of user.";
return std::nullopt;
}
winrt::hstring account_name = account_name_obj.as<winrt::hstring>();
std::string account_name_string = winrt::to_string(account_name);
if (account_name_string.empty()) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Error unboxing string value for profile username of user.";
return std::nullopt;
}
return account_name_string;
}
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
PWSTR path;
HRESULT result =
SHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_DEFAULT, nullptr, &path);
if (result == S_OK) {
std::wstring download_path{path};
CoTaskMemFree(path);
return std::filesystem::path(download_path);
}
CoTaskMemFree(path);
return std::nullopt;
}
std::optional<std::filesystem::path> DeviceInfo::GetAppDataPath() const {
std::string path;
path.resize(MAX_PATH);
HRESULT result = SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr,
SHGFP_TYPE_CURRENT, path.data());
if (result == S_OK) {
path.resize(strlen(path.data()));
return std::filesystem::path(path);
}
return std::nullopt;
}
std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
return std::filesystem::temp_directory_path();
}
bool DeviceInfo::IsScreenLocked() const {
DWORD session_id = WTSGetActiveConsoleSessionId();
WTS_INFO_CLASS wts_info_class = WTSSessionInfoEx;
LPTSTR session_info_buffer = nullptr;
DWORD session_info_buffer_size_bytes = 0;
WTSINFOEXW* wts_info = nullptr;
LONG session_state = WTS_SESSIONSTATE_UNKNOWN;
if (WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, session_id,
wts_info_class, &session_info_buffer,
&session_info_buffer_size_bytes)) {
if (session_info_buffer_size_bytes > 0) {
wts_info = (WTSINFOEXW*)session_info_buffer;
if (wts_info->Level == 1) {
session_state = wts_info->Data.WTSInfoExLevel1.SessionFlags;
}
}
WTSFreeMemory(session_info_buffer);
session_info_buffer = nullptr;
}
return (session_state == WTS_SESSIONSTATE_LOCK);
}
void DeviceInfo::RegisterScreenLockedListener(
absl::string_view listener_name,
std::function<void(api::DeviceInfo::ScreenStatus)> callback) {
if (message_window_handle_ == nullptr) {
instance_ = (HINSTANCE)GetModuleHandle(NULL);
WNDCLASS window_class;
window_class.style = 0;
window_class.lpfnWndProc = (WNDPROC)&WindowProc;
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = instance_;
window_class.hIcon = nullptr;
window_class.hCursor = nullptr;
window_class.hbrBackground = nullptr;
window_class.lpszMenuName = nullptr;
window_class.lpszClassName = window_class_name;
registered_class_ = RegisterClass(&window_class);
message_window_handle_ = CreateWindow(
MAKEINTATOM(registered_class_), // class atom from RegisterClass
window_name, // window name
0, // window style
0, // initial x position of window
0, // initial y position of window
0, // width
0, // height
HWND_MESSAGE, // handle to the parent of window
// (message-only window in this case)
nullptr, // handle to a menu
instance_, // handle to the instance of the module
// associated to the window
this); // pointer to be passed to the window for additional data
if (!message_window_handle_) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Failed to create message window for Nearby Sharing DLL.";
}
}
screen_locked_listeners_.emplace(listener_name, callback);
}
void DeviceInfo::UnregisterScreenLockedListener(
absl::string_view listener_name) {
screen_locked_listeners_.erase(listener_name);
}
} // namespace windows
} // namespace nearby
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,18 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_WINDOWS_DEVICE_INFO_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_WINDOWS_DEVICE_INFO_H_
#ifndef PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#define PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#include <guiddef.h>
#include <windows.h>
#include <array>
#include <functional>
#include <optional>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "fastpair/internal/api/device_info.h"
#include "internal/platform/implementation/device_info.h"
#include "winrt/Windows.Foundation.h"
namespace nearby {
namespace windows {
@@ -32,7 +35,17 @@ class DeviceInfo : public api::DeviceInfo {
public:
~DeviceInfo() override;
std::optional<std::u16string> GetOsDeviceName() const override;
api::DeviceInfo::DeviceType GetDeviceType() const override;
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::u16string> GetFullName() const override;
std::optional<std::u16string> GetGivenName() const override;
std::optional<std::u16string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetAppDataPath() const override;
std::optional<std::filesystem::path> GetTemporaryPath() const override;
bool IsScreenLocked() const override;
void RegisterScreenLockedListener(
@@ -51,4 +64,4 @@ class DeviceInfo : public api::DeviceInfo {
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_IMPL_WINDOWS_DEVICE_INFO_H_
#endif // PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,25 +12,61 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "fastpair/internal/impl/windows/device_info.h"
#include "internal/platform/implementation/windows/device_info.h"
#include <functional>
#include <string>
#include "gtest/gtest.h"
#include "absl/synchronization/notification.h"
#include "fastpair/internal/api/device_info.h"
#include "internal/platform/implementation/device_info.h"
namespace nearby {
namespace windows {
namespace {
TEST(DeviceInfo, DISABLED_GetOsType) {
TEST(DeviceInfo, DISABLED_GetComputerName) {
EXPECT_TRUE(DeviceInfo().GetOsDeviceName().has_value());
}
TEST(DeviceInfo, DISABLED_GetDeviceType) {
EXPECT_EQ(DeviceInfo().GetDeviceType(), api::DeviceInfo::DeviceType::kLaptop);
}
TEST(DeviceInfo, GetOsType) {
EXPECT_EQ(DeviceInfo().GetOsType(), api::DeviceInfo::OsType::kWindows);
}
TEST(DeviceInfo, DISABLED_GetFullName) {
EXPECT_TRUE(DeviceInfo().GetFullName().has_value());
}
TEST(DeviceInfo, DISABLED_GetGivenName) {
EXPECT_TRUE(DeviceInfo().GetGivenName().has_value());
}
TEST(DeviceInfo, DISABLED_GetLastName) {
EXPECT_TRUE(DeviceInfo().GetLastName().has_value());
}
TEST(DeviceInfo, DISABLED_GetProfileUserName) {
EXPECT_TRUE(DeviceInfo().GetProfileUserName().has_value());
}
TEST(DeviceInfo, DISABLED_GetAppDataPath) {
EXPECT_TRUE(DeviceInfo().GetAppDataPath().has_value());
}
TEST(DeviceInfo, DISABLED_GetDownloadPath) {
EXPECT_TRUE(DeviceInfo().GetDownloadPath().has_value());
}
TEST(DeviceInfo, DISABLED_GetTemporaryPath) {
EXPECT_TRUE(DeviceInfo().GetTemporaryPath().has_value());
}
TEST(DeviceInfo, DISABLED_IsScreenLocked) {
EXPECT_TRUE(DeviceInfo().IsScreenLocked());
EXPECT_FALSE(DeviceInfo().IsScreenLocked());
}
TEST(DeviceInfo, DISABLED_RegisterScreenLockedListener) {
@@ -30,6 +30,7 @@ cc_library(
"uuid.lib",
"winmm.lib",
"winspool.lib",
"wtsapi32.lib",
"comsuppwd.lib",
"setupapi.lib",
"dnsapi.lib",
@@ -44,6 +44,7 @@
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/bluetooth_classic_medium.h"
#include "internal/platform/implementation/windows/condition_variable.h"
#include "internal/platform/implementation/windows/device_info.h"
#include "internal/platform/implementation/windows/executor.h"
#include "internal/platform/implementation/windows/file.h"
#include "internal/platform/implementation/windows/file_path.h"
@@ -312,5 +313,9 @@ std::unique_ptr<Timer> ImplementationPlatform::CreateTimer() {
return std::make_unique<windows::Timer>();
}
std::unique_ptr<DeviceInfo> ImplementationPlatform::CreateDeviceInfo() {
return std::make_unique<windows::DeviceInfo>();
}
} // namespace api
} // namespace nearby