From aea7a09a24f6fe6ba5d41984c3bc52535e9b85de Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Thu, 21 Dec 2023 11:33:58 -0800 Subject: [PATCH] Switch DeviceInfo to use UTF-8 strings. - Move conversion from native to UTF-8 into platform impl. PiperOrigin-RevId: 592912664 --- internal/platform/device_info.h | 22 +++++------ internal/platform/device_info_impl.cc | 12 +++--- internal/platform/device_info_impl.h | 11 ++++-- .../implementation/apple/device_info.h | 8 ++-- .../implementation/apple/device_info.mm | 16 ++++---- .../platform/implementation/device_info.h | 8 ++-- .../platform/implementation/g3/device_info.h | 16 ++++---- .../implementation/windows/device_info.cc | 39 ++++++++----------- .../implementation/windows/device_info.h | 8 ++-- internal/test/fake_device_info.h | 26 ++++++------- internal/test/fake_device_info_test.cc | 16 ++++---- 11 files changed, 89 insertions(+), 93 deletions(-) diff --git a/internal/platform/device_info.h b/internal/platform/device_info.h index c95f592d..8769d560 100644 --- a/internal/platform/device_info.h +++ b/internal/platform/device_info.h @@ -22,7 +22,6 @@ #include "absl/strings/string_view.h" #include "internal/platform/implementation/device_info.h" -#include "internal/platform/implementation/platform.h" namespace nearby { @@ -30,12 +29,13 @@ class DeviceInfo { public: virtual ~DeviceInfo() = default; - virtual std::u16string GetOsDeviceName() const = 0; + // All strings are UTF-8 encoded. + virtual std::string GetOsDeviceName() const = 0; virtual api::DeviceInfo::DeviceType GetDeviceType() const = 0; virtual api::DeviceInfo::OsType GetOsType() const = 0; - virtual std::optional GetFullName() const = 0; - virtual std::optional GetGivenName() const = 0; - virtual std::optional GetLastName() const = 0; + virtual std::optional GetFullName() const = 0; + virtual std::optional GetGivenName() const = 0; + virtual std::optional GetLastName() const = 0; virtual std::optional GetProfileUserName() const = 0; virtual std::filesystem::path GetDownloadPath() const = 0; @@ -55,18 +55,18 @@ class DeviceInfo { virtual bool PreventSleep() = 0; virtual bool AllowSleep() = 0; - // Returns localized device name depends on device type. - std::u16string GetDeviceTypeName() const { + // Returns UTF-8 encoded localized device name depending on device type. + std::string GetDeviceTypeName() const { // TODO(b/230132370): return localized device name. switch (GetDeviceType()) { case api::DeviceInfo::DeviceType::kPhone: - return u"Phone"; + return "Phone"; case api::DeviceInfo::DeviceType::kTablet: - return u"Tablet"; + return "Tablet"; case api::DeviceInfo::DeviceType::kLaptop: - return u"PC"; + return "PC"; default: - return u"Unknown"; + return "Unknown"; } } }; diff --git a/internal/platform/device_info_impl.cc b/internal/platform/device_info_impl.cc index 6ac551d5..4b18f66a 100644 --- a/internal/platform/device_info_impl.cc +++ b/internal/platform/device_info_impl.cc @@ -21,14 +21,14 @@ namespace nearby { -std::u16string DeviceInfoImpl::GetOsDeviceName() const { - std::optional device_name = +std::string DeviceInfoImpl::GetOsDeviceName() const { + std::optional device_name = device_info_impl_->GetOsDeviceName(); if (device_name.has_value()) { return *device_name; } - return u"unknown"; + return "unknown"; } api::DeviceInfo::DeviceType DeviceInfoImpl::GetDeviceType() const { @@ -39,15 +39,15 @@ api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const { return device_info_impl_->GetOsType(); } -std::optional DeviceInfoImpl::GetFullName() const { +std::optional DeviceInfoImpl::GetFullName() const { return device_info_impl_->GetFullName(); } -std::optional DeviceInfoImpl::GetGivenName() const { +std::optional DeviceInfoImpl::GetGivenName() const { return device_info_impl_->GetGivenName(); } -std::optional DeviceInfoImpl::GetLastName() const { +std::optional DeviceInfoImpl::GetLastName() const { return device_info_impl_->GetLastName(); } diff --git a/internal/platform/device_info_impl.h b/internal/platform/device_info_impl.h index 98e6032f..5c89c588 100644 --- a/internal/platform/device_info_impl.h +++ b/internal/platform/device_info_impl.h @@ -15,13 +15,16 @@ #ifndef PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ #define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_ +#include #include #include #include #include #include +#include "absl/strings/string_view.h" #include "internal/platform/device_info.h" +#include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/platform.h" namespace nearby { @@ -31,13 +34,13 @@ class DeviceInfoImpl : public DeviceInfo { DeviceInfoImpl() : device_info_impl_(api::ImplementationPlatform::CreateDeviceInfo()) {} - std::u16string GetOsDeviceName() const override; + std::string GetOsDeviceName() const override; api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - std::optional GetFullName() const override; - std::optional GetGivenName() const override; - std::optional GetLastName() const override; + std::optional GetFullName() const override; + std::optional GetGivenName() const override; + std::optional GetLastName() const override; std::optional GetProfileUserName() const override; std::filesystem::path GetDownloadPath() const override; diff --git a/internal/platform/implementation/apple/device_info.h b/internal/platform/implementation/apple/device_info.h index 4315040c..753f166a 100644 --- a/internal/platform/implementation/apple/device_info.h +++ b/internal/platform/implementation/apple/device_info.h @@ -28,15 +28,15 @@ namespace apple { class DeviceInfo : public api::DeviceInfo { public: - std::optional GetOsDeviceName() const override; + std::optional GetOsDeviceName() const override; api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - std::optional GetFullName() const override; - std::optional GetGivenName() const override; - std::optional GetLastName() const override; + std::optional GetFullName() const override; + std::optional GetGivenName() const override; + std::optional GetLastName() const override; std::optional GetProfileUserName() const override; std::optional GetDownloadPath() const override; diff --git a/internal/platform/implementation/apple/device_info.mm b/internal/platform/implementation/apple/device_info.mm index 6a8d09ee..780dbc8f 100644 --- a/internal/platform/implementation/apple/device_info.mm +++ b/internal/platform/implementation/apple/device_info.mm @@ -33,15 +33,15 @@ namespace nearby { namespace apple { -std::optional DeviceInfo::GetOsDeviceName() const { +std::optional DeviceInfo::GetOsDeviceName() const { #if TARGET_OS_IPHONE NSString *name = UIDevice.currentDevice.name; - const char16_t *cName = (const char16_t *)[name cStringUsingEncoding:NSUTF16StringEncoding]; - return std::u16string(cName); + const char *cName = (const char *)[name cStringUsingEncoding:NSUTF8StringEncoding]; + return std::string(cName); #elif TARGET_OS_OSX NSString *name = NSHost.currentHost.localizedName; - const char16_t *cName = (const char16_t *)[name cStringUsingEncoding:NSUTF16StringEncoding]; - return std::u16string(cName); + const char *cName = (const char *)[name cStringUsingEncoding:NSUTF8StringEncoding]; + return std::string(cName); #else return std::nullopt; #endif @@ -78,9 +78,9 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { #endif } -std::optional DeviceInfo::GetFullName() const { return std::nullopt; } -std::optional DeviceInfo::GetGivenName() const { return std::nullopt; } -std::optional DeviceInfo::GetLastName() const { return std::nullopt; } +std::optional DeviceInfo::GetFullName() const { return std::nullopt; } +std::optional DeviceInfo::GetGivenName() const { return std::nullopt; } +std::optional DeviceInfo::GetLastName() const { return std::nullopt; } std::optional DeviceInfo::GetProfileUserName() const { return std::nullopt; } std::optional DeviceInfo::GetDownloadPath() const { diff --git a/internal/platform/implementation/device_info.h b/internal/platform/implementation/device_info.h index 7b48c608..9c529df0 100644 --- a/internal/platform/implementation/device_info.h +++ b/internal/platform/implementation/device_info.h @@ -41,14 +41,14 @@ class DeviceInfo { virtual ~DeviceInfo() = default; // Gets device name. - virtual std::optional GetOsDeviceName() const = 0; + virtual std::optional GetOsDeviceName() const = 0; virtual DeviceType GetDeviceType() const = 0; virtual OsType GetOsType() const = 0; // Gets basic information of current user. - virtual std::optional GetFullName() const = 0; - virtual std::optional GetGivenName() const = 0; - virtual std::optional GetLastName() const = 0; + virtual std::optional GetFullName() const = 0; + virtual std::optional GetGivenName() const = 0; + virtual std::optional GetLastName() const = 0; virtual std::optional GetProfileUserName() const = 0; // Gets known paths of current user. diff --git a/internal/platform/implementation/g3/device_info.h b/internal/platform/implementation/g3/device_info.h index 87d6a958..8b865916 100644 --- a/internal/platform/implementation/g3/device_info.h +++ b/internal/platform/implementation/g3/device_info.h @@ -30,8 +30,8 @@ namespace g3 { class DeviceInfo : public api::DeviceInfo { public: - std::optional GetOsDeviceName() const override { - return u"Windows"; + std::optional GetOsDeviceName() const override { + return "Windows"; } api::DeviceInfo::DeviceType GetDeviceType() const override { @@ -42,14 +42,14 @@ class DeviceInfo : public api::DeviceInfo { return api::DeviceInfo::OsType::kChromeOs; } - std::optional GetFullName() const override { - return u"nearby"; + std::optional GetFullName() const override { + return "nearby"; } - std::optional GetGivenName() const override { - return u"nearby"; + std::optional GetGivenName() const override { + return "nearby"; } - std::optional GetLastName() const override { - return u"nearby"; + std::optional GetLastName() const override { + return "nearby"; } std::optional GetProfileUserName() const override { return "nearby"; diff --git a/internal/platform/implementation/windows/device_info.cc b/internal/platform/implementation/windows/device_info.cc index 5978f27c..eb21b2b9 100644 --- a/internal/platform/implementation/windows/device_info.cc +++ b/internal/platform/implementation/windows/device_info.cc @@ -18,19 +18,15 @@ #include #include -#include #include #include #include #include -#include -#include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" -#include "internal/base/bluetooth_address.h" #include "internal/platform/implementation/device_info.h" -#include "internal/platform/implementation/windows/session_manager.h" +#include "internal/platform/implementation/windows/generated/winrt/base.h" #include "internal/platform/logging.h" #include "winrt/Windows.Foundation.Collections.h" #include "winrt/Windows.Foundation.h" @@ -56,7 +52,7 @@ constexpr char logs_relative_path[] = "Google\\Nearby\\Sharing\\Logs"; constexpr char crash_dumps_relative_path[] = "Google\\Nearby\\Sharing\\CrashDumps"; -std::optional DeviceInfo::GetOsDeviceName() const { +std::optional DeviceInfo::GetOsDeviceName() const { DWORD size = 0; // Get length of the computer name. @@ -70,8 +66,8 @@ std::optional DeviceInfo::GetOsDeviceName() const { 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()); + winrt::hstring device_name_str(device_name); + return winrt::to_string(device_name_str); } NEARBY_LOGS(ERROR) << ": Failed to get device name, error:" << GetLastError(); @@ -87,7 +83,7 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const { return api::DeviceInfo::OsType::kWindows; } -std::optional DeviceInfo::GetFullName() const { +std::optional 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 @@ -119,19 +115,18 @@ std::optional DeviceInfo::GetFullName() const { return std::nullopt; } winrt::hstring full_name = full_name_obj.as(); - std::wstring wstr(full_name); - std::u16string u16str(wstr.begin(), wstr.end()); + std::string full_name_str = winrt::to_string(full_name); - if (u16str.empty()) { + if (full_name_str.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": Error unboxing string value for full name of user."; return std::nullopt; } - return u16str; + return full_name_str; } -std::optional DeviceInfo::GetGivenName() const { +std::optional 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 @@ -163,19 +158,18 @@ std::optional DeviceInfo::GetGivenName() const { return std::nullopt; } winrt::hstring given_name = given_name_obj.as(); - std::wstring wstr(given_name); - std::u16string u16str(wstr.begin(), wstr.end()); + std::string given_name_str = winrt::to_string(given_name); - if (u16str.empty()) { + if (given_name_str.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": Error unboxing string value for first name of user."; return std::nullopt; } - return u16str; + return given_name_str; } -std::optional DeviceInfo::GetLastName() const { +std::optional 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 @@ -207,16 +201,15 @@ std::optional DeviceInfo::GetLastName() const { return std::nullopt; } winrt::hstring last_name = last_name_obj.as(); - std::wstring wstr(last_name); - std::u16string u16str(wstr.begin(), wstr.end()); + std::string last_name_str = winrt::to_string(last_name); - if (u16str.empty()) { + if (last_name_str.empty()) { NEARBY_LOGS(ERROR) << __func__ << ": Error unboxing string value for last name of user."; return std::nullopt; } - return u16str; + return last_name_str; } std::optional DeviceInfo::GetProfileUserName() const { diff --git a/internal/platform/implementation/windows/device_info.h b/internal/platform/implementation/windows/device_info.h index 79c96771..ced7ddbd 100644 --- a/internal/platform/implementation/windows/device_info.h +++ b/internal/platform/implementation/windows/device_info.h @@ -31,12 +31,12 @@ class DeviceInfo : public api::DeviceInfo { public: ~DeviceInfo() override = default; - std::optional GetOsDeviceName() const override; + std::optional GetOsDeviceName() const override; api::DeviceInfo::DeviceType GetDeviceType() const override; api::DeviceInfo::OsType GetOsType() const override; - std::optional GetFullName() const override; - std::optional GetGivenName() const override; - std::optional GetLastName() const override; + std::optional GetFullName() const override; + std::optional GetGivenName() const override; + std::optional GetLastName() const override; std::optional GetProfileUserName() const override; std::optional GetDownloadPath() const override; diff --git a/internal/test/fake_device_info.h b/internal/test/fake_device_info.h index b7fbf160..f33e223f 100644 --- a/internal/test/fake_device_info.h +++ b/internal/test/fake_device_info.h @@ -15,6 +15,7 @@ #ifndef THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DEVICE_INFO_H_ #define THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DEVICE_INFO_H_ +#include #include #include #include @@ -24,7 +25,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" -#include "internal/base/bluetooth_address.h" #include "internal/platform/device_info.h" #include "internal/platform/implementation/device_info.h" @@ -32,7 +32,7 @@ namespace nearby { class FakeDeviceInfo : public DeviceInfo { public: - std::u16string GetOsDeviceName() const override { return device_name_; } + std::string GetOsDeviceName() const override { return device_name_; } api::DeviceInfo::DeviceType GetDeviceType() const override { return device_type_; @@ -40,13 +40,13 @@ class FakeDeviceInfo : public DeviceInfo { api::DeviceInfo::OsType GetOsType() const override { return os_type_; } - std::optional GetFullName() const override { + std::optional GetFullName() const override { return full_name_; } - std::optional GetGivenName() const override { + std::optional GetGivenName() const override { return given_name_; } - std::optional GetLastName() const override { + std::optional GetLastName() const override { return last_name_; } std::optional GetProfileUserName() const override { @@ -93,7 +93,7 @@ class FakeDeviceInfo : public DeviceInfo { int GetScreenLockedListenerCount() { return screen_locked_listeners_.size(); } // Mock methods. - void SetOsDeviceName(std::u16string_view device_name) { + void SetOsDeviceName(std::string_view device_name) { device_name_ = device_name; } @@ -103,7 +103,7 @@ class FakeDeviceInfo : public DeviceInfo { void SetOsType(api::DeviceInfo::OsType os_type) { os_type_ = os_type; } - void SetFullName(std::optional full_name) { + void SetFullName(std::optional full_name) { if (full_name.has_value() && !full_name->empty()) { full_name_ = full_name; } else { @@ -111,7 +111,7 @@ class FakeDeviceInfo : public DeviceInfo { } } - void SetGivenName(std::optional given_name) { + void SetGivenName(std::optional given_name) { if (given_name.has_value() && !given_name->empty()) { given_name_ = given_name; } else { @@ -119,7 +119,7 @@ class FakeDeviceInfo : public DeviceInfo { } } - void SetLastName(std::optional last_name) { + void SetLastName(std::optional last_name) { if (last_name.has_value() && !last_name->empty()) { last_name_ = last_name; } else { @@ -160,13 +160,13 @@ class FakeDeviceInfo : public DeviceInfo { } private: - std::u16string device_name_ = u"nearby"; + std::string device_name_ = "nearby"; api::DeviceInfo::DeviceType device_type_ = api::DeviceInfo::DeviceType::kLaptop; api::DeviceInfo::OsType os_type_ = api::DeviceInfo::OsType::kWindows; - std::optional full_name_ = u"Nearby"; - std::optional given_name_ = u"Nearby"; - std::optional last_name_ = u"Nearby"; + std::optional full_name_ = "Nearby"; + std::optional given_name_ = "Nearby"; + std::optional last_name_ = "Nearby"; std::optional profile_user_name_ = "nearby"; std::filesystem::path download_path_ = std::filesystem::temp_directory_path(); std::filesystem::path app_data_path_ = std::filesystem::temp_directory_path(); diff --git a/internal/test/fake_device_info_test.cc b/internal/test/fake_device_info_test.cc index 16935c43..1a5f120f 100644 --- a/internal/test/fake_device_info_test.cc +++ b/internal/test/fake_device_info_test.cc @@ -27,8 +27,8 @@ namespace { TEST(FakeDeviceInfo, DeviceName) { FakeDeviceInfo device_info; - device_info.SetOsDeviceName(u"windows"); - EXPECT_EQ(device_info.GetOsDeviceName(), u"windows"); + device_info.SetOsDeviceName("windows"); + EXPECT_EQ(device_info.GetOsDeviceName(), "windows"); } TEST(FakeDeviceInfo, DeviceType) { @@ -45,24 +45,24 @@ TEST(FakeDeviceInfo, OsType) { TEST(FakeDeviceInfo, FullName) { FakeDeviceInfo device_info; - device_info.SetFullName(u"windows"); - EXPECT_EQ(device_info.GetFullName(), u"windows"); + device_info.SetFullName("windows"); + EXPECT_EQ(device_info.GetFullName(), "windows"); device_info.SetFullName(std::nullopt); EXPECT_FALSE(device_info.GetFullName().has_value()); } TEST(FakeDeviceInfo, GivenName) { FakeDeviceInfo device_info; - device_info.SetGivenName(u"windows"); - EXPECT_EQ(device_info.GetGivenName(), u"windows"); + device_info.SetGivenName("windows"); + EXPECT_EQ(device_info.GetGivenName(), "windows"); device_info.SetGivenName(std::nullopt); EXPECT_FALSE(device_info.GetGivenName().has_value()); } TEST(FakeDeviceInfo, LastName) { FakeDeviceInfo device_info; - device_info.SetLastName(u"windows"); - EXPECT_EQ(device_info.GetLastName(), u"windows"); + device_info.SetLastName("windows"); + EXPECT_EQ(device_info.GetLastName(), "windows"); device_info.SetLastName(std::nullopt); EXPECT_FALSE(device_info.GetLastName().has_value()); }