Use machine name for advertisement when user not logged in.

PiperOrigin-RevId: 690479544
This commit is contained in:
Francis Tsui
2024-10-27 22:52:21 -07:00
committed by Copybara-Service
parent 17e2bb8ed3
commit b4d11ecbda
28 changed files with 59 additions and 517 deletions
@@ -34,8 +34,6 @@ class DeviceInfo : public api::DeviceInfo {
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::string> GetGivenName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
@@ -78,8 +78,6 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const {
#endif
}
std::optional<std::string> DeviceInfo::GetGivenName() const { return std::nullopt; }
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
NSFileManager *manager = [NSFileManager defaultManager];
@@ -15,7 +15,7 @@
#ifndef PLATFORM_API_DEVICE_INFO_H_
#define PLATFORM_API_DEVICE_INFO_H_
#include <filesystem>
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
@@ -45,9 +45,6 @@ class DeviceInfo {
virtual DeviceType GetDeviceType() const = 0;
virtual OsType GetOsType() const = 0;
// Gets basic information of current user.
virtual std::optional<std::string> GetGivenName() const = 0;
// Gets known paths of current user.
virtual std::optional<std::filesystem::path> GetDownloadPath() const = 0;
virtual std::optional<std::filesystem::path> GetLocalAppDataPath() const = 0;
@@ -42,10 +42,6 @@ class DeviceInfo : public api::DeviceInfo {
return api::DeviceInfo::OsType::kChromeOs;
}
std::optional<std::string> GetGivenName() const override {
return "nearby";
}
std::optional<std::filesystem::path> GetDownloadPath() const override {
return std::filesystem::temp_directory_path();
}
@@ -27,7 +27,6 @@
#include "absl/synchronization/mutex.h"
#include "internal/base/files.h"
#include "internal/platform/implementation/device_info.h"
#include "internal/platform/implementation/windows/generated/winrt/base.h"
#include "internal/platform/implementation/windows/string_utils.h"
#include "internal/platform/logging.h"
#include "winrt/Windows.Foundation.Collections.h"
@@ -89,48 +88,6 @@ api::DeviceInfo::OsType DeviceInfo::GetOsType() const {
return api::DeviceInfo::OsType::kWindows;
}
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
// the user who is switched in, so FindAllAsync() will not find them. (Under
// the UWP application model, each process runs under its own user account.
// That user account is different from the user account of the logged-in user.
// Processes aren't owned by the logged-in user for purposes of isolation.)
IVectorView<User> users =
User::FindAllAsync(UserType::LocalUser,
UserAuthenticationStatus::LocallyAuthenticated)
.get();
if (users == nullptr) {
LOG(ERROR) << __func__ << ": Error retrieving locally authenticated user.";
return std::nullopt;
}
// On Windows Desktop apps, the first Windows.System.User instance
// returned in the IVectorView is always the current user.
// https://github.com/microsoft/Windows-task-snippets/blob/master/tasks/User-info.md
User current_user = users.GetAt(0);
// Retrieve the human-readable properties for the current user
IAsyncOperation<IInspectable> given_name_obj_async =
current_user.GetPropertyAsync(KnownUserProperties::FirstName());
IInspectable given_name_obj = given_name_obj_async.get();
if (given_name_obj == nullptr) {
LOG(ERROR) << __func__ << ": Error retrieving first name of user.";
return std::nullopt;
}
winrt::hstring given_name = given_name_obj.as<winrt::hstring>();
std::string given_name_str = winrt::to_string(given_name);
if (given_name_str.empty()) {
LOG(ERROR) << __func__
<< ": Error unboxing string value for first name of user.";
return std::nullopt;
}
return given_name_str;
}
std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
PWSTR path;
HRESULT result =
@@ -15,8 +15,10 @@
#ifndef PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#define PLATFORM_IMPL_WINDOWS_DEVICE_INFO_H_
#include <filesystem> // NOLINT
#include <functional>
#include <optional>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
@@ -34,7 +36,6 @@ class DeviceInfo : public api::DeviceInfo {
std::optional<std::string> GetOsDeviceName() const override;
api::DeviceInfo::DeviceType GetDeviceType() const override;
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::string> GetGivenName() const override;
std::optional<std::filesystem::path> GetDownloadPath() const override;
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
@@ -39,11 +39,6 @@ TEST(DeviceInfo, GetOsType) {
EXPECT_EQ(DeviceInfo().GetOsType(), api::DeviceInfo::OsType::kWindows);
}
TEST(DeviceInfo, DISABLED_GetGivenName) {
EXPECT_TRUE(DeviceInfo().GetGivenName().has_value());
}
TEST(DeviceInfo, DISABLED_GetLocalAppDataPath) {
EXPECT_TRUE(DeviceInfo().GetLocalAppDataPath().has_value());
}