From 024fcb1457e1f82335a30fd89775c127cfeeda46 Mon Sep 17 00:00:00 2001 From: Lasan Mahaliyana Date: Sat, 13 Jun 2026 18:28:09 +0530 Subject: [PATCH] Revert "Cleanup DeviceInfo" This reverts commit 981d7db30579c62135cf56ea551bd65bc5f10e8a. --- internal/platform/device_info_impl.cc | 21 +++++-- .../apple/Tests/GNCDeviceInfoTest.mm | 17 ++++-- .../implementation/apple/device_info.h | 12 ++-- .../implementation/apple/device_info.mm | 18 ++++-- .../platform/implementation/device_info.h | 10 ++-- .../platform/implementation/g3/device_info.h | 21 +++++-- .../implementation/windows/device_info.cc | 55 +++++++++++++++---- .../implementation/windows/device_info.h | 10 ++-- .../windows/device_info_test.cc | 13 +++-- .../windows/preferences_repository_test.cc | 12 ++-- 10 files changed, 133 insertions(+), 56 deletions(-) diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index 91c04021..79d5be7f 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -44,19 +44,32 @@ api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const { } FilePath DeviceInfoImpl::GetDownloadPath() const { - return device_info_impl_->GetDownloadPath(); + std::optional path = device_info_impl_->GetDownloadPath(); + if (path.has_value()) { + return *path; + } + return Files::GetTemporaryDirectory(); } FilePath DeviceInfoImpl::GetAppDataPath() const { - return device_info_impl_->GetLocalAppDataPath(FilePath()); + std::optional path = device_info_impl_->GetLocalAppDataPath(); + if (path.has_value()) { + return *path; + } + return Files::GetTemporaryDirectory(); } FilePath DeviceInfoImpl::GetTemporaryPath() const { - return device_info_impl_->GetTemporaryPath(); + std::optional path = device_info_impl_->GetTemporaryPath(); + if (path.has_value()) { + return *path; + } + return Files::GetTemporaryDirectory(); } FilePath DeviceInfoImpl::GetLogPath() const { - return device_info_impl_->GetLogPath(); + std::optional path = device_info_impl_->GetLogPath(); + return path.value_or(GetTemporaryPath()); } std::optional DeviceInfoImpl::GetAvailableDiskSpaceInBytes( diff --git a/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm b/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm index edaf54ad..39988866 100644 --- a/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm @@ -49,20 +49,27 @@ } - (void)testGetDownloadPath { - XCTAssertNotNil(@(_deviceInfo->GetDownloadPath().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetDownloadPath().value().GetPath().c_str())); } - (void)testGetLocalAppDataPath { - XCTAssertNotNil( - @(_deviceInfo->GetLocalAppDataPath(nearby::FilePath("sub_path")).GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetLocalAppDataPath().value().GetPath().c_str())); +} + +- (void)testGetCommonAppDataPath { + XCTAssertNotNil(@(_deviceInfo->GetCommonAppDataPath().value().GetPath().c_str())); } - (void)testGetTemporaryPath { - XCTAssertNotNil(@(_deviceInfo->GetTemporaryPath().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetTemporaryPath().value().GetPath().c_str())); } - (void)testGetLogPath { - XCTAssertNotNil(@(_deviceInfo->GetLogPath().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetLogPath().value().GetPath().c_str())); +} + +- (void)testGetCrashDumpPath { + XCTAssertNotNil(@(_deviceInfo->GetCrashDumpPath().value().GetPath().c_str())); } - (void)testIsScreenLocked { diff --git a/internal/platform/implementation/apple/device_info.h b/internal/platform/implementation/apple/device_info.h index e42b2b16..ce436201 100644 --- a/internal/platform/implementation/apple/device_info.h +++ b/internal/platform/implementation/apple/device_info.h @@ -34,13 +34,17 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::OsType GetOsType() const override; - FilePath GetDownloadPath() const override; + std::optional GetDownloadPath() const override; - FilePath GetLocalAppDataPath(FilePath sub_path) const override; + std::optional GetLocalAppDataPath() const override; - FilePath GetTemporaryPath() const override; + std::optional GetCommonAppDataPath() const override; - FilePath GetLogPath() const override; + std::optional GetTemporaryPath() const override; + + std::optional GetLogPath() const override; + + std::optional GetCrashDumpPath() const override; bool IsScreenLocked() const override; diff --git a/internal/platform/implementation/apple/device_info.mm b/internal/platform/implementation/apple/device_info.mm index c66f3148..08dc3536 100644 --- a/internal/platform/implementation/apple/device_info.mm +++ b/internal/platform/implementation/apple/device_info.mm @@ -79,7 +79,7 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { #endif } -FilePath DeviceInfo::GetDownloadPath() const { +std::optional DeviceInfo::GetDownloadPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -90,24 +90,30 @@ FilePath DeviceInfo::GetDownloadPath() const { error:&error]; if (!downloadsURL) { GNCLoggerError(@"Failed to get download path: %@", error); - return GetTemporaryPath(); + return std::nullopt; } return FilePath(absl::string_view([downloadsURL.path cString])); } -FilePath DeviceInfo::GetLocalAppDataPath(FilePath sub_path) const { - return FilePath(absl::string_view([GNCLocalAppDataPath().path cString])).append(sub_path); +std::optional DeviceInfo::GetLocalAppDataPath() const { + return FilePath(absl::string_view([GNCLocalAppDataPath().path cString])); } -FilePath DeviceInfo::GetTemporaryPath() const { +std::optional DeviceInfo::GetCommonAppDataPath() const { return GetLocalAppDataPath(); } + +std::optional DeviceInfo::GetTemporaryPath() const { return FilePath(absl::string_view([NSTemporaryDirectory() cString])); } -FilePath DeviceInfo::GetLogPath() const { +std::optional DeviceInfo::GetLogPath() const { return FilePath(absl::string_view([GNCLogPath().path cString])); } +std::optional DeviceInfo::GetCrashDumpPath() const { + return FilePath(absl::string_view([GNCCrashDumpPath().path cString])); +} + bool DeviceInfo::IsScreenLocked() const { return false; } void DeviceInfo::RegisterScreenLockedListener( diff --git a/internal/platform/implementation/device_info.h b/internal/platform/implementation/device_info.h index 4426ef7d..19aca037 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -46,10 +46,12 @@ class DeviceInfo { virtual OsType GetOsType() const = 0; // Gets known paths of current user. - virtual FilePath GetDownloadPath() const = 0; - virtual FilePath GetLocalAppDataPath(FilePath sub_path) const = 0; - virtual FilePath GetTemporaryPath() const = 0; - virtual FilePath GetLogPath() const = 0; + virtual std::optional GetDownloadPath() const = 0; + virtual std::optional GetLocalAppDataPath() const = 0; + virtual std::optional GetCommonAppDataPath() const = 0; + virtual std::optional GetTemporaryPath() const = 0; + virtual std::optional GetLogPath() const = 0; + virtual std::optional GetCrashDumpPath() const = 0; // Monitor screen status virtual bool IsScreenLocked() const = 0; diff --git a/internal/platform/implementation/g3/device_info.h b/internal/platform/implementation/g3/device_info.h index 6754d46f..5ff18863 100644 --- a/internal/platform/implementation/g3/device_info.h +++ b/internal/platform/implementation/g3/device_info.h @@ -45,24 +45,33 @@ class DeviceInfo : public api::DeviceInfo { return api::DeviceInfo::OsType::kChromeOs; } - FilePath GetDownloadPath() const override { + std::optional GetDownloadPath() const override { return Files::GetTemporaryDirectory(); } - FilePath GetLocalAppDataPath(FilePath sub_path) const override { + std::optional GetLocalAppDataPath() const override { if (MediumEnvironment::Instance() .GetEnvironmentConfig() .use_temporary_directory_for_app_path) { - return Files::GetTemporaryDirectory().append(sub_path); + return Files::GetTemporaryDirectory(); } - return GetAppDataPath().append(sub_path); + + return GetAppDataPath(); } - FilePath GetTemporaryPath() const override { + std::optional GetCommonAppDataPath() const override { return Files::GetTemporaryDirectory(); } - FilePath GetLogPath() const override { + std::optional GetTemporaryPath() const override { + return Files::GetTemporaryDirectory(); + } + + std::optional GetLogPath() const override { + return Files::GetTemporaryDirectory(); + } + + std::optional GetCrashDumpPath() const override { return Files::GetTemporaryDirectory(); } diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index 348f7ce6..ba05e5d3 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -30,11 +30,27 @@ #include "internal/platform/implementation/windows/device_paths.h" #include "internal/platform/implementation/windows/string_utils.h" #include "internal/platform/implementation/windows/utils.h" +#include "winrt/Windows.Foundation.Collections.h" +#include "winrt/Windows.Foundation.h" +#include "winrt/Windows.System.h" namespace nearby::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; + using ::nearby::windows::string_utils::WideStringToString; +template +using IVectorView = winrt::Windows::Foundation::Collections::IVectorView; + +template +using IAsyncOperation = winrt::Windows::Foundation::IAsyncOperation; + std::optional DeviceInfo::GetOsDeviceName() const { std::optional device_name = GetDnsHostName(); if (device_name.has_value()) { @@ -44,6 +60,7 @@ std::optional DeviceInfo::GetOsDeviceName() const { } api::DeviceInfo::DeviceType DeviceInfo::GetDeviceType() const { + // TODO(b/230132370): return correct device type on the Windows platform. return api::DeviceInfo::DeviceType::kLaptop; } @@ -51,7 +68,7 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { return api::DeviceInfo::OsType::kWindows; } -FilePath DeviceInfo::GetDownloadPath() const { +std::optional DeviceInfo::GetDownloadPath() const { PWSTR path; HRESULT result = SHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_DEFAULT, nullptr, &path); @@ -62,21 +79,39 @@ FilePath DeviceInfo::GetDownloadPath() const { } CoTaskMemFree(path); + return std::nullopt; +} + +std::optional DeviceInfo::GetLocalAppDataPath() const { + return nearby::platform::windows::GetLocalAppDataPath(FilePath()); +} + +std::optional DeviceInfo::GetCommonAppDataPath() const { + PWSTR path; + HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, KF_FLAG_DEFAULT, + /*hToken=*/nullptr, &path); + if (result == S_OK) { + std::wstring common_app_data_path{path}; + CoTaskMemFree(path); + return FilePath(std::wstring_view(common_app_data_path)); + } + + CoTaskMemFree(path); + return std::nullopt; +} + +std::optional DeviceInfo::GetTemporaryPath() const { return Files::GetTemporaryDirectory(); } -FilePath DeviceInfo::GetLocalAppDataPath(FilePath sub_path) const { - return nearby::platform::windows::GetLocalAppDataPath(sub_path); -} - -FilePath DeviceInfo::GetTemporaryPath() const { - return Files::GetTemporaryDirectory(); -} - -FilePath DeviceInfo::GetLogPath() const { +std::optional DeviceInfo::GetLogPath() const { return nearby::platform::windows::GetLogPath(); } +std::optional DeviceInfo::GetCrashDumpPath() const { + return nearby::platform::windows::GetCrashDumpPath(); +} + bool DeviceInfo::IsScreenLocked() const { absl::MutexLock lock(mutex_); return session_manager_.IsScreenLocked(); diff --git a/internal/platform/implementation/windows/device_info.h b/internal/platform/implementation/windows/device_info.h index 8de0d388..1b4cfe99 100644 --- a/internal/platform/implementation/windows/device_info.h +++ b/internal/platform/implementation/windows/device_info.h @@ -37,10 +37,12 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - FilePath GetDownloadPath() const override; - FilePath GetLocalAppDataPath(FilePath sub_path) const override; - FilePath GetTemporaryPath() const override; - FilePath GetLogPath() const override; + std::optional GetDownloadPath() const override; + std::optional GetLocalAppDataPath() const override; + std::optional GetCommonAppDataPath() const override; + std::optional GetTemporaryPath() const override; + std::optional GetLogPath() const override; + std::optional GetCrashDumpPath() const override; bool IsScreenLocked() const override; void RegisterScreenLockedListener( diff --git a/internal/platform/implementation/windows/device_info_test.cc b/internal/platform/implementation/windows/device_info_test.cc index 295992a6..6431e5ea 100644 --- a/internal/platform/implementation/windows/device_info_test.cc +++ b/internal/platform/implementation/windows/device_info_test.cc @@ -40,20 +40,23 @@ TEST(DeviceInfo, GetOsType) { } TEST(DeviceInfo, DISABLED_GetLocalAppDataPath) { - EXPECT_FALSE( - DeviceInfo().GetLocalAppDataPath(FilePath("sub_path")).IsEmpty()); + EXPECT_TRUE(DeviceInfo().GetLocalAppDataPath().has_value()); } TEST(DeviceInfo, DISABLED_GetDownloadPath) { - EXPECT_FALSE(DeviceInfo().GetDownloadPath().IsEmpty()); + EXPECT_TRUE(DeviceInfo().GetDownloadPath().has_value()); } TEST(DeviceInfo, DISABLED_GetTemporaryPath) { - EXPECT_FALSE(DeviceInfo().GetTemporaryPath().IsEmpty()); + EXPECT_TRUE(DeviceInfo().GetTemporaryPath().has_value()); } TEST(DeviceInfo, DISABLED_GetLogPath) { - EXPECT_FALSE(DeviceInfo().GetLogPath().IsEmpty()); + EXPECT_TRUE(DeviceInfo().GetLogPath().has_value()); +} + +TEST(DeviceInfo, DISABLED_GetCrashDumpPath) { + EXPECT_TRUE(DeviceInfo().GetCrashDumpPath().has_value()); } TEST(DeviceInfo, DISABLED_IsScreenLocked) { diff --git a/internal/platform/implementation/windows/preferences_repository_test.cc b/internal/platform/implementation/windows/preferences_repository_test.cc index 473e5105..0ef8de90 100644 --- a/internal/platform/implementation/windows/preferences_repository_test.cc +++ b/internal/platform/implementation/windows/preferences_repository_test.cc @@ -44,8 +44,7 @@ TEST(PreferencesRepository, LoadWithBadPath) { TEST(PreferencesRepository, RecoverFromBadPreferences) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( - FilePath()); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); @@ -64,8 +63,7 @@ TEST(PreferencesRepository, RecoverFromBadPreferences) { TEST(PreferencesRepository, SaveAndLoadPreferences) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( - FilePath()); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); @@ -88,8 +86,7 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { TEST(PreferencesRepository, LoadFromBackup) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( - FilePath()); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName)); @@ -126,8 +123,7 @@ TEST(PreferencesRepository, LoadFromBackup) { TEST(PreferencesRepository, RecoverFromCorruption) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( - FilePath()); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); ASSERT_TRUE(app_data_path.has_value()); FilePath full_path = app_data_path->append(FilePath(kPreferencesPath)); FilePath full_name = app_data_path->append(FilePath(kPreferencesFileName));