Refactor device path management functions.

PiperOrigin-RevId: 793755146
This commit is contained in:
Francis Tsui
2025-08-11 12:30:57 -07:00
committed by Copybara-Service
parent dcfb83f68b
commit 804b481b73
5 changed files with 193 additions and 25 deletions
@@ -17,6 +17,32 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test")
licenses(["notice"])
cc_library(
name = "device_paths",
srcs = [
"device_paths.cc",
],
hdrs = [
"device_paths.h",
],
compatible_with = ["//buildenv/target:non_prod"],
linkopts = [
"ole32.lib",
"shell32.lib",
],
tags = ["windows"],
visibility = [
"//location/nearby/apps/better_together/plugins/crashpad/windows:__pkg__",
"//location/nearby/apps/better_together/plugins/logging/windows:__pkg__",
"//location/nearby/apps/better_together/windows/common:__pkg__",
],
deps = [
"//internal/base:file_path",
"//internal/base:files",
"@com_google_absl//absl/strings",
],
)
cc_library(
name = "types",
srcs = [
@@ -42,11 +68,13 @@ cc_library(
"utils.h",
],
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
tags = ["windows"],
visibility = [
"//sharing/internal/impl/windows:__pkg__",
],
deps = [
":comm",
":device_paths",
"//internal/base:bluetooth_address",
"//internal/base:file_path",
"//internal/base:files",
@@ -111,6 +139,7 @@ cc_library(
"wifi_lan_mdns.h",
],
copts = ["-DNO_INTEL_PIE"],
tags = ["windows"],
visibility = ["//visibility:private"],
deps = [
"//connections/implementation/flags:connections_flags",
@@ -142,6 +171,7 @@ cc_library(
srcs = [
"crypto.cc",
],
tags = ["windows"],
visibility = ["//visibility:private"],
deps = [
"//internal/platform:base",
@@ -159,6 +189,7 @@ cc_library(
hdrs = [
"string_utils.h",
],
tags = ["windows"],
visibility = [
"//:__subpackages__",
"//location/nearby:__subpackages__",
@@ -227,6 +258,7 @@ cc_library(
"-Wno-unused-value",
],
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
tags = ["windows"],
visibility = [
"//chrome/chromeos/assistant/data_migration/lib:__pkg__",
"//connections:__subpackages__",
@@ -381,3 +413,18 @@ cc_test(
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "device_paths_test",
size = "small",
timeout = "short",
srcs = [
"device_paths_test.cc",
],
deps = [
":device_paths",
"//internal/base:file_path",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
@@ -27,6 +27,7 @@
#include "internal/base/files.h"
#include "internal/base/file_path.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/windows/device_paths.h"
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/logging.h"
#include "winrt/Windows.Foundation.Collections.h"
@@ -50,10 +51,6 @@ using IVectorView = winrt::Windows::Foundation::Collections::IVectorView<T>;
template <typename T>
using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation<T>;
constexpr absl::string_view kLogsRelativePath = "Google\\Nearby\\Sharing\\Logs";
constexpr absl::string_view kCrashDumpsRelativePath =
"Google\\Nearby\\Sharing\\CrashDumps";
std::optional<std::string> DeviceInfo::GetOsDeviceName() const {
DWORD size = 0;
@@ -102,17 +99,7 @@ std::optional<FilePath> DeviceInfo::GetDownloadPath() const {
}
std::optional<FilePath> DeviceInfo::GetLocalAppDataPath() const {
PWSTR path;
HRESULT result = SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT,
/*hToken=*/nullptr, &path);
if (result == S_OK) {
std::wstring local_appdata_path{path};
CoTaskMemFree(path);
return FilePath(std::wstring_view(local_appdata_path));
}
CoTaskMemFree(path);
return std::nullopt;
return nearby::platform::windows::GetLocalAppDataPath(FilePath());
}
std::optional<FilePath> DeviceInfo::GetCommonAppDataPath() const {
@@ -134,19 +121,11 @@ std::optional<FilePath> DeviceInfo::GetTemporaryPath() const {
}
std::optional<FilePath> DeviceInfo::GetLogPath() const {
std::optional<FilePath> prefix_path = GetLocalAppDataPath();
if (prefix_path.has_value()) {
return prefix_path.value().append(FilePath(kLogsRelativePath));
}
return std::nullopt;
return nearby::platform::windows::GetLogPath();
}
std::optional<FilePath> DeviceInfo::GetCrashDumpPath() const {
std::optional<FilePath> prefix_path = GetLocalAppDataPath();
if (prefix_path.has_value()) {
return prefix_path.value().append(FilePath(kCrashDumpsRelativePath));
}
return std::nullopt;
return nearby::platform::windows::GetCrashDumpPath();
}
bool DeviceInfo::IsScreenLocked() const {
@@ -0,0 +1,60 @@
// Copyright 2025 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.
#define _WIN32_WINNT _WIN32_WINNT_WIN10
#include "internal/platform/implementation/windows/device_paths.h"
#include <shlobj_core.h>
#include <windows.h>
#include "absl/strings/string_view.h"
#include "internal/base/file_path.h"
#include "internal/base/files.h"
namespace nearby::platform::windows {
namespace {
constexpr absl::string_view kLogsRelativePath = "Google\\Nearby\\Sharing\\Logs";
constexpr absl::string_view kCrashDumpsRelativePath =
"Google\\Nearby\\Sharing\\CrashDumps";
FilePath GetAppDataPath(GUID folder_id, const FilePath& app_path) {
FilePath app_data_path;
PWSTR path = nullptr;
HRESULT result = SHGetKnownFolderPath(folder_id, KF_FLAG_DEFAULT,
/*hToken=*/nullptr, &path);
if (result == S_OK) {
app_data_path = FilePath(path);
app_data_path.append(app_path);
} else {
app_data_path = Files::GetTemporaryDirectory();
}
CoTaskMemFree(path);
return app_data_path;
}
} // namespace
FilePath GetLocalAppDataPath(const FilePath& app_path) {
return GetAppDataPath(FOLDERID_LocalAppData, app_path);
}
FilePath GetLogPath() {
return GetLocalAppDataPath(FilePath(kLogsRelativePath));
}
FilePath GetCrashDumpPath() {
return GetLocalAppDataPath(FilePath(kCrashDumpsRelativePath));
}
} // namespace nearby::platform::windows
@@ -0,0 +1,34 @@
// Copyright 2025 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_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_DEVICE_PATHS_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_DEVICE_PATHS_H_
#include "internal/base/file_path.h"
namespace nearby::platform::windows {
// Utility functions for getting the paths of various device directories.
// Returns the path to the LocalAppData directory for the given app path.
FilePath GetLocalAppDataPath(const FilePath& app_path);
// Returns the path to the log directory.
FilePath GetLogPath();
// Returns the path to the crash dump directory.
FilePath GetCrashDumpPath();
} // namespace nearby::platform::windows
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_DEVICE_PATHS_H_
@@ -0,0 +1,48 @@
// Copyright 2025 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_paths.h"
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "internal/base/file_path.h"
namespace nearby::platform::windows {
namespace {
using ::testing::Eq;
TEST(Win32ProcessTest, GetLocalAppDataPath) {
FilePath path = GetLocalAppDataPath(FilePath("test\\data"));
EXPECT_THAT(path,
Eq(FilePath("C:\\Users\\lexan\\AppData\\Local\\test\\data")));
}
TEST(Win32ProcessTest, GetLogPath) {
FilePath path = GetLogPath();
EXPECT_THAT(
path,
Eq(FilePath(
"C:\\Users\\lexan\\AppData\\Local\\Google\\Nearby\\Sharing\\Logs")));
}
TEST(Win32ProcessTest, GetCrashDumpPath) {
FilePath path = GetCrashDumpPath();
EXPECT_THAT(path, Eq(FilePath("C:"
"\\Users\\lexan\\AppData\\Local\\Google\\Nearby"
"\\Sharing\\CrashDumps")));
}
} // namespace
} // namespace nearby::platform::windows