From e3fad280d9eac6ff5cafb19fb30fcfd0ff2532d6 Mon Sep 17 00:00:00 2001 From: Lasan Mahaliyana Date: Sat, 13 Jun 2026 18:30:06 +0530 Subject: [PATCH] Reapply "Cleanup DeviceInfo" This reverts commit 024fcb1457e1f82335a30fd89775c127cfeeda46. --- 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 | 53 ++++--------------- .../implementation/windows/device_info.h | 10 ++-- .../windows/device_info_test.cc | 13 ++--- .../windows/preferences_repository_test.cc | 12 +++-- 10 files changed, 55 insertions(+), 132 deletions(-) diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index 79d5be7f..91c04021 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -44,32 +44,19 @@ api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const { } FilePath DeviceInfoImpl::GetDownloadPath() const { - std::optional path = device_info_impl_->GetDownloadPath(); - if (path.has_value()) { - return *path; - } - return Files::GetTemporaryDirectory(); + return device_info_impl_->GetDownloadPath(); } FilePath DeviceInfoImpl::GetAppDataPath() const { - std::optional path = device_info_impl_->GetLocalAppDataPath(); - if (path.has_value()) { - return *path; - } - return Files::GetTemporaryDirectory(); + return device_info_impl_->GetLocalAppDataPath(FilePath()); } FilePath DeviceInfoImpl::GetTemporaryPath() const { - std::optional path = device_info_impl_->GetTemporaryPath(); - if (path.has_value()) { - return *path; - } - return Files::GetTemporaryDirectory(); + return device_info_impl_->GetTemporaryPath(); } FilePath DeviceInfoImpl::GetLogPath() const { - std::optional path = device_info_impl_->GetLogPath(); - return path.value_or(GetTemporaryPath()); + return device_info_impl_->GetLogPath(); } std::optional DeviceInfoImpl::GetAvailableDiskSpaceInBytes( diff --git a/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm b/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm index 39988866..edaf54ad 100644 --- a/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCDeviceInfoTest.mm @@ -49,27 +49,20 @@ } - (void)testGetDownloadPath { - XCTAssertNotNil(@(_deviceInfo->GetDownloadPath().value().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetDownloadPath().GetPath().c_str())); } - (void)testGetLocalAppDataPath { - XCTAssertNotNil(@(_deviceInfo->GetLocalAppDataPath().value().GetPath().c_str())); -} - -- (void)testGetCommonAppDataPath { - XCTAssertNotNil(@(_deviceInfo->GetCommonAppDataPath().value().GetPath().c_str())); + XCTAssertNotNil( + @(_deviceInfo->GetLocalAppDataPath(nearby::FilePath("sub_path")).GetPath().c_str())); } - (void)testGetTemporaryPath { - XCTAssertNotNil(@(_deviceInfo->GetTemporaryPath().value().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetTemporaryPath().GetPath().c_str())); } - (void)testGetLogPath { - XCTAssertNotNil(@(_deviceInfo->GetLogPath().value().GetPath().c_str())); -} - -- (void)testGetCrashDumpPath { - XCTAssertNotNil(@(_deviceInfo->GetCrashDumpPath().value().GetPath().c_str())); + XCTAssertNotNil(@(_deviceInfo->GetLogPath().GetPath().c_str())); } - (void)testIsScreenLocked { diff --git a/internal/platform/implementation/apple/device_info.h b/internal/platform/implementation/apple/device_info.h index ce436201..e42b2b16 100644 --- a/internal/platform/implementation/apple/device_info.h +++ b/internal/platform/implementation/apple/device_info.h @@ -34,17 +34,13 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::OsType GetOsType() const override; - std::optional GetDownloadPath() const override; + FilePath GetDownloadPath() const override; - std::optional GetLocalAppDataPath() const override; + FilePath GetLocalAppDataPath(FilePath sub_path) const override; - std::optional GetCommonAppDataPath() const override; + FilePath GetTemporaryPath() const override; - std::optional GetTemporaryPath() const override; - - std::optional GetLogPath() const override; - - std::optional GetCrashDumpPath() const override; + FilePath GetLogPath() 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 08dc3536..c66f3148 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 } -std::optional DeviceInfo::GetDownloadPath() const { +FilePath DeviceInfo::GetDownloadPath() const { NSFileManager *manager = [NSFileManager defaultManager]; NSError *error = nil; @@ -90,30 +90,24 @@ std::optional DeviceInfo::GetDownloadPath() const { error:&error]; if (!downloadsURL) { GNCLoggerError(@"Failed to get download path: %@", error); - return std::nullopt; + return GetTemporaryPath(); } return FilePath(absl::string_view([downloadsURL.path cString])); } -std::optional DeviceInfo::GetLocalAppDataPath() const { - return FilePath(absl::string_view([GNCLocalAppDataPath().path cString])); +FilePath DeviceInfo::GetLocalAppDataPath(FilePath sub_path) const { + return FilePath(absl::string_view([GNCLocalAppDataPath().path cString])).append(sub_path); } -std::optional DeviceInfo::GetCommonAppDataPath() const { return GetLocalAppDataPath(); } - -std::optional DeviceInfo::GetTemporaryPath() const { +FilePath DeviceInfo::GetTemporaryPath() const { return FilePath(absl::string_view([NSTemporaryDirectory() cString])); } -std::optional DeviceInfo::GetLogPath() const { +FilePath 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 19aca037..4426ef7d 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -46,12 +46,10 @@ class DeviceInfo { virtual OsType GetOsType() const = 0; // Gets known paths of current user. - 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; + virtual FilePath GetDownloadPath() const = 0; + virtual FilePath GetLocalAppDataPath(FilePath sub_path) const = 0; + virtual FilePath GetTemporaryPath() const = 0; + virtual FilePath GetLogPath() 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 5ff18863..6754d46f 100644 --- a/internal/platform/implementation/g3/device_info.h +++ b/internal/platform/implementation/g3/device_info.h @@ -45,33 +45,24 @@ class DeviceInfo : public api::DeviceInfo { return api::DeviceInfo::OsType::kChromeOs; } - std::optional GetDownloadPath() const override { + FilePath GetDownloadPath() const override { return Files::GetTemporaryDirectory(); } - std::optional GetLocalAppDataPath() const override { + FilePath GetLocalAppDataPath(FilePath sub_path) const override { if (MediumEnvironment::Instance() .GetEnvironmentConfig() .use_temporary_directory_for_app_path) { - return Files::GetTemporaryDirectory(); + return Files::GetTemporaryDirectory().append(sub_path); } - - return GetAppDataPath(); + return GetAppDataPath().append(sub_path); } - std::optional GetCommonAppDataPath() const override { + FilePath GetTemporaryPath() const override { return Files::GetTemporaryDirectory(); } - std::optional GetTemporaryPath() const override { - return Files::GetTemporaryDirectory(); - } - - std::optional GetLogPath() const override { - return Files::GetTemporaryDirectory(); - } - - std::optional GetCrashDumpPath() const override { + FilePath GetLogPath() const override { return Files::GetTemporaryDirectory(); } diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index ba05e5d3..348f7ce6 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -30,27 +30,11 @@ #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()) { @@ -60,7 +44,6 @@ 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; } @@ -68,7 +51,7 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { return api::DeviceInfo::OsType::kWindows; } -std::optional DeviceInfo::GetDownloadPath() const { +FilePath DeviceInfo::GetDownloadPath() const { PWSTR path; HRESULT result = SHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_DEFAULT, nullptr, &path); @@ -79,37 +62,19 @@ std::optional 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(); } -std::optional DeviceInfo::GetLogPath() const { - return nearby::platform::windows::GetLogPath(); +FilePath DeviceInfo::GetLocalAppDataPath(FilePath sub_path) const { + return nearby::platform::windows::GetLocalAppDataPath(sub_path); } -std::optional DeviceInfo::GetCrashDumpPath() const { - return nearby::platform::windows::GetCrashDumpPath(); +FilePath DeviceInfo::GetTemporaryPath() const { + return Files::GetTemporaryDirectory(); +} + +FilePath DeviceInfo::GetLogPath() const { + return nearby::platform::windows::GetLogPath(); } bool DeviceInfo::IsScreenLocked() const { diff --git a/internal/platform/implementation/windows/device_info.h b/internal/platform/implementation/windows/device_info.h index 1b4cfe99..8de0d388 100644 --- a/internal/platform/implementation/windows/device_info.h +++ b/internal/platform/implementation/windows/device_info.h @@ -37,12 +37,10 @@ class DeviceInfo : public api::DeviceInfo { api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() 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; + FilePath GetDownloadPath() const override; + FilePath GetLocalAppDataPath(FilePath sub_path) const override; + FilePath GetTemporaryPath() const override; + FilePath GetLogPath() 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 6431e5ea..295992a6 100644 --- a/internal/platform/implementation/windows/device_info_test.cc +++ b/internal/platform/implementation/windows/device_info_test.cc @@ -40,23 +40,20 @@ TEST(DeviceInfo, GetOsType) { } TEST(DeviceInfo, DISABLED_GetLocalAppDataPath) { - EXPECT_TRUE(DeviceInfo().GetLocalAppDataPath().has_value()); + EXPECT_FALSE( + DeviceInfo().GetLocalAppDataPath(FilePath("sub_path")).IsEmpty()); } TEST(DeviceInfo, DISABLED_GetDownloadPath) { - EXPECT_TRUE(DeviceInfo().GetDownloadPath().has_value()); + EXPECT_FALSE(DeviceInfo().GetDownloadPath().IsEmpty()); } TEST(DeviceInfo, DISABLED_GetTemporaryPath) { - EXPECT_TRUE(DeviceInfo().GetTemporaryPath().has_value()); + EXPECT_FALSE(DeviceInfo().GetTemporaryPath().IsEmpty()); } TEST(DeviceInfo, DISABLED_GetLogPath) { - EXPECT_TRUE(DeviceInfo().GetLogPath().has_value()); -} - -TEST(DeviceInfo, DISABLED_GetCrashDumpPath) { - EXPECT_TRUE(DeviceInfo().GetCrashDumpPath().has_value()); + EXPECT_FALSE(DeviceInfo().GetLogPath().IsEmpty()); } 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 0ef8de90..473e5105 100644 --- a/internal/platform/implementation/windows/preferences_repository_test.cc +++ b/internal/platform/implementation/windows/preferences_repository_test.cc @@ -44,7 +44,8 @@ TEST(PreferencesRepository, LoadWithBadPath) { TEST(PreferencesRepository, RecoverFromBadPreferences) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( + FilePath()); 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)); @@ -63,7 +64,8 @@ TEST(PreferencesRepository, RecoverFromBadPreferences) { TEST(PreferencesRepository, SaveAndLoadPreferences) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( + FilePath()); 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)); @@ -86,7 +88,8 @@ TEST(PreferencesRepository, SaveAndLoadPreferences) { TEST(PreferencesRepository, LoadFromBackup) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( + FilePath()); 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)); @@ -123,7 +126,8 @@ TEST(PreferencesRepository, LoadFromBackup) { TEST(PreferencesRepository, RecoverFromCorruption) { std::optional app_data_path = - api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath(); + api::ImplementationPlatform::CreateDeviceInfo()->GetLocalAppDataPath( + FilePath()); 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));