Switch DeviceInfo to use UTF-8 strings.

- Move conversion from native to UTF-8 into platform impl.

PiperOrigin-RevId: 592912664
This commit is contained in:
Francis Tsui
2023-12-21 11:35:20 -08:00
committed by Copybara-Service
parent 16cb7f70b3
commit aea7a09a24
11 changed files with 89 additions and 93 deletions
+11 -11
View File
@@ -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<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> GetFullName() const = 0;
virtual std::optional<std::string> GetGivenName() const = 0;
virtual std::optional<std::string> GetLastName() const = 0;
virtual std::optional<std::string> 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";
}
}
};
+6 -6
View File
@@ -21,14 +21,14 @@
namespace nearby {
std::u16string DeviceInfoImpl::GetOsDeviceName() const {
std::optional<std::u16string> device_name =
std::string DeviceInfoImpl::GetOsDeviceName() const {
std::optional<std::string> 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<std::u16string> DeviceInfoImpl::GetFullName() const {
std::optional<std::string> DeviceInfoImpl::GetFullName() const {
return device_info_impl_->GetFullName();
}
std::optional<std::u16string> DeviceInfoImpl::GetGivenName() const {
std::optional<std::string> DeviceInfoImpl::GetGivenName() const {
return device_info_impl_->GetGivenName();
}
std::optional<std::u16string> DeviceInfoImpl::GetLastName() const {
std::optional<std::string> DeviceInfoImpl::GetLastName() const {
return device_info_impl_->GetLastName();
}
+7 -4
View File
@@ -15,13 +15,16 @@
#ifndef PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
#define PLATFORM_PUBLIC_DEVICE_INFO_IMPL_H_
#include <cstddef>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#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<std::u16string> GetFullName() const override;
std::optional<std::u16string> GetGivenName() const override;
std::optional<std::u16string> GetLastName() const override;
std::optional<std::string> GetFullName() const override;
std::optional<std::string> GetGivenName() const override;
std::optional<std::string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::filesystem::path GetDownloadPath() const override;
@@ -28,15 +28,15 @@ namespace apple {
class DeviceInfo : public api::DeviceInfo {
public:
std::optional<std::u16string> GetOsDeviceName() const override;
std::optional<std::string> 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> GetFullName() const override;
std::optional<std::string> GetGivenName() const override;
std::optional<std::string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
@@ -33,15 +33,15 @@
namespace nearby {
namespace apple {
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const {
std::optional<std::string> 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<std::u16string> DeviceInfo::GetFullName() const { return std::nullopt; }
std::optional<std::u16string> DeviceInfo::GetGivenName() const { return std::nullopt; }
std::optional<std::u16string> DeviceInfo::GetLastName() const { return std::nullopt; }
std::optional<std::string> DeviceInfo::GetFullName() const { return std::nullopt; }
std::optional<std::string> DeviceInfo::GetGivenName() const { return std::nullopt; }
std::optional<std::string> DeviceInfo::GetLastName() const { return std::nullopt; }
std::optional<std::string> DeviceInfo::GetProfileUserName() const { return std::nullopt; }
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
@@ -41,14 +41,14 @@ class DeviceInfo {
virtual ~DeviceInfo() = default;
// Gets device name.
virtual std::optional<std::u16string> GetOsDeviceName() const = 0;
virtual std::optional<std::string> 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> GetFullName() const = 0;
virtual std::optional<std::string> GetGivenName() const = 0;
virtual std::optional<std::string> GetLastName() const = 0;
virtual std::optional<std::string> GetProfileUserName() const = 0;
// Gets known paths of current user.
@@ -30,8 +30,8 @@ namespace g3 {
class DeviceInfo : public api::DeviceInfo {
public:
std::optional<std::u16string> GetOsDeviceName() const override {
return u"Windows";
std::optional<std::string> 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<std::u16string> GetFullName() const override {
return u"nearby";
std::optional<std::string> GetFullName() const override {
return "nearby";
}
std::optional<std::u16string> GetGivenName() const override {
return u"nearby";
std::optional<std::string> GetGivenName() const override {
return "nearby";
}
std::optional<std::u16string> GetLastName() const override {
return u"nearby";
std::optional<std::string> GetLastName() const override {
return "nearby";
}
std::optional<std::string> GetProfileUserName() const override {
return "nearby";
@@ -18,19 +18,15 @@
#include <windows.h>
#include <wtsapi32.h>
#include <array>
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#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<std::u16string> DeviceInfo::GetOsDeviceName() const {
std::optional<std::string> DeviceInfo::GetOsDeviceName() const {
DWORD size = 0;
// Get length of the computer name.
@@ -70,8 +66,8 @@ std::optional<std::u16string> 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<std::u16string> DeviceInfo::GetFullName() const {
std::optional<std::string> 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<std::u16string> DeviceInfo::GetFullName() const {
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());
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<std::u16string> DeviceInfo::GetGivenName() const {
std::optional<std::string> 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<std::u16string> DeviceInfo::GetGivenName() const {
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());
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<std::u16string> DeviceInfo::GetLastName() const {
std::optional<std::string> 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<std::u16string> DeviceInfo::GetLastName() const {
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());
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<std::string> DeviceInfo::GetProfileUserName() const {
@@ -31,12 +31,12 @@ class DeviceInfo : public api::DeviceInfo {
public:
~DeviceInfo() override = default;
std::optional<std::u16string> GetOsDeviceName() const override;
std::optional<std::string> 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> GetFullName() const override;
std::optional<std::string> GetGivenName() const override;
std::optional<std::string> GetLastName() const override;
std::optional<std::string> GetProfileUserName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
+13 -13
View File
@@ -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 <cstddef>
#include <filesystem>
#include <functional>
#include <limits>
@@ -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<std::u16string> GetFullName() const override {
std::optional<std::string> GetFullName() const override {
return full_name_;
}
std::optional<std::u16string> GetGivenName() const override {
std::optional<std::string> GetGivenName() const override {
return given_name_;
}
std::optional<std::u16string> GetLastName() const override {
std::optional<std::string> GetLastName() const override {
return last_name_;
}
std::optional<std::string> 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<std::u16string> full_name) {
void SetFullName(std::optional<std::string> 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<std::u16string> given_name) {
void SetGivenName(std::optional<std::string> 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<std::u16string> last_name) {
void SetLastName(std::optional<std::string> 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<std::u16string> full_name_ = u"Nearby";
std::optional<std::u16string> given_name_ = u"Nearby";
std::optional<std::u16string> last_name_ = u"Nearby";
std::optional<std::string> full_name_ = "Nearby";
std::optional<std::string> given_name_ = "Nearby";
std::optional<std::string> last_name_ = "Nearby";
std::optional<std::string> 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();
+8 -8
View File
@@ -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());
}