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
-1
View File
@@ -34,7 +34,6 @@ class DeviceInfo {
virtual std::string GetOsDeviceName() const = 0;
virtual api::DeviceInfo::DeviceType GetDeviceType() const = 0;
virtual api::DeviceInfo::OsType GetOsType() const = 0;
virtual std::optional<std::string> GetGivenName() const = 0;
virtual std::filesystem::path GetDownloadPath() const = 0;
virtual std::filesystem::path GetAppDataPath() const = 0;
-4
View File
@@ -43,10 +43,6 @@ api::DeviceInfo::OsType DeviceInfoImpl::GetOsType() const {
return device_info_impl_->GetOsType();
}
std::optional<std::string> DeviceInfoImpl::GetGivenName() const {
return device_info_impl_->GetGivenName();
}
std::filesystem::path DeviceInfoImpl::GetDownloadPath() const {
std::optional<std::filesystem::path> path =
device_info_impl_->GetDownloadPath();
-2
View File
@@ -38,8 +38,6 @@ class DeviceInfoImpl : public DeviceInfo {
api::DeviceInfo::DeviceType GetDeviceType() const override;
api::DeviceInfo::OsType GetOsType() const override;
std::optional<std::string> GetGivenName() const override;
std::filesystem::path GetDownloadPath() const override;
std::filesystem::path GetAppDataPath() const override;
std::filesystem::path GetTemporaryPath() const override;
@@ -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());
}
-13
View File
@@ -40,10 +40,6 @@ class FakeDeviceInfo : public DeviceInfo {
api::DeviceInfo::OsType GetOsType() const override { return os_type_; }
std::optional<std::string> GetGivenName() const override {
return given_name_;
}
std::filesystem::path GetDownloadPath() const override {
return download_path_;
}
@@ -96,14 +92,6 @@ class FakeDeviceInfo : public DeviceInfo {
void SetOsType(api::DeviceInfo::OsType os_type) { os_type_ = os_type; }
void SetGivenName(std::optional<std::string> given_name) {
if (given_name.has_value() && !given_name->empty()) {
given_name_ = given_name;
} else {
given_name_ = std::nullopt;
}
}
void SetDownloadPath(std::filesystem::path path) { download_path_ = path; }
void SetAppDataPath(std::filesystem::path path) { app_data_path_ = path; }
@@ -133,7 +121,6 @@ class FakeDeviceInfo : public DeviceInfo {
api::DeviceInfo::DeviceType device_type_ =
api::DeviceInfo::DeviceType::kLaptop;
api::DeviceInfo::OsType os_type_ = api::DeviceInfo::OsType::kWindows;
std::optional<std::string> given_name_ = "Nearby";
std::filesystem::path download_path_ = std::filesystem::temp_directory_path();
std::filesystem::path app_data_path_ = std::filesystem::temp_directory_path();
std::filesystem::path temp_path_ = std::filesystem::temp_directory_path();
-8
View File
@@ -43,14 +43,6 @@ TEST(FakeDeviceInfo, OsType) {
EXPECT_EQ(device_info.GetOsType(), api::DeviceInfo::OsType::kWindows);
}
TEST(FakeDeviceInfo, GivenName) {
FakeDeviceInfo device_info;
device_info.SetGivenName("windows");
EXPECT_EQ(device_info.GetGivenName(), "windows");
device_info.SetGivenName(std::nullopt);
EXPECT_FALSE(device_info.GetGivenName().has_value());
}
TEST(FakeDeviceInfo, GetDownloadPath) {
FakeDeviceInfo device_info;
EXPECT_EQ(device_info.GetDownloadPath(),
-15
View File
@@ -247,7 +247,6 @@ cc_library(
"nearby_connections_service.cc",
"nearby_connections_service_impl.cc",
"nearby_connections_stream_buffer_manager.cc",
"nearby_share_profile_info_provider_impl.cc",
"nearby_sharing_service.cc",
"nearby_sharing_service_factory.cc",
"nearby_sharing_service_impl.cc",
@@ -264,7 +263,6 @@ cc_library(
"nearby_connections_service.h",
"nearby_connections_service_impl.h",
"nearby_connections_stream_buffer_manager.h",
"nearby_share_profile_info_provider_impl.h",
"nearby_sharing_service.h",
"nearby_sharing_service_extension.h",
"nearby_sharing_service_factory.h",
@@ -627,19 +625,6 @@ cc_test(
],
)
cc_test(
name = "nearby_share_profile_info_provider_impl_test",
srcs = ["nearby_share_profile_info_provider_impl_test.cc"],
deps = [
":nearby_sharing_service",
"//internal/platform/implementation:account_manager",
"//internal/platform/implementation/g3", # fixdeps: keep
"//internal/test",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "nearby_sharing_service_extension_test",
srcs = ["nearby_sharing_service_extension_test.cc"],
-15
View File
@@ -22,7 +22,6 @@ cc_library(
],
hdrs = [
"nearby_share_prefs.h",
"nearby_share_profile_info_provider.h",
"nearby_share_switches.h",
],
visibility = ["//visibility:public"],
@@ -35,20 +34,6 @@ cc_library(
],
)
cc_library(
name = "test_support",
testonly = True,
srcs = [
"fake_nearby_share_profile_info_provider.cc",
],
hdrs = [
"fake_nearby_share_profile_info_provider.h",
"nearby_share_profile_info_provider.h",
],
visibility = ["//visibility:public"],
deps = [],
)
cc_library(
name = "enum",
hdrs = ["nearby_share_enums.h"],
@@ -1,35 +0,0 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sharing/common/fake_nearby_share_profile_info_provider.h"
#include <optional>
#include <string>
namespace nearby {
namespace sharing {
FakeNearbyShareProfileInfoProvider::FakeNearbyShareProfileInfoProvider() =
default;
FakeNearbyShareProfileInfoProvider::~FakeNearbyShareProfileInfoProvider() =
default;
std::optional<std::string> FakeNearbyShareProfileInfoProvider::GetGivenName()
const {
return given_name_;
}
} // namespace sharing
} // namespace nearby
@@ -1,46 +0,0 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_SHARING_COMMON_FAKE_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
#define THIRD_PARTY_NEARBY_SHARING_COMMON_FAKE_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
#include <optional>
#include <string>
#include "sharing/common/nearby_share_profile_info_provider.h"
namespace nearby {
namespace sharing {
class FakeNearbyShareProfileInfoProvider
: public NearbyShareProfileInfoProvider {
public:
FakeNearbyShareProfileInfoProvider();
~FakeNearbyShareProfileInfoProvider() override;
// NearbyShareProfileInfoProvider:
std::optional<std::string> GetGivenName() const override;
void set_given_name(const std::optional<std::string>& given_name) {
given_name_ = given_name;
}
private:
std::optional<std::string> given_name_;
};
} // namespace sharing
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_SHARING_COMMON_FAKE_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
@@ -1,35 +0,0 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_SHARING_COMMON_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
#define THIRD_PARTY_NEARBY_SHARING_COMMON_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
#include <optional>
#include <string>
namespace nearby {
namespace sharing {
class NearbyShareProfileInfoProvider {
public:
NearbyShareProfileInfoProvider() = default;
virtual ~NearbyShareProfileInfoProvider() = default;
// Returns UTF-8 encoded given name of current account.
// Returns absl::nullopt if a valid given name cannot be returned.
virtual std::optional<std::string> GetGivenName() const = 0;
};
} // namespace sharing
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_SHARING_COMMON_NEARBY_SHARE_PROFILE_INFO_PROVIDER_H_
-1
View File
@@ -79,7 +79,6 @@ cc_test(
"//internal/test",
"//sharing/common",
"//sharing/common:enum",
"//sharing/common:test_support",
"//sharing/internal/api:mock_sharing_platform",
"//sharing/internal/test:nearby_test",
"//sharing/proto:share_cc_proto",
@@ -31,7 +31,6 @@
namespace nearby {
namespace sharing {
class NearbyShareClientFactory;
class NearbyShareProfileInfoProvider;
namespace {
@@ -48,10 +47,8 @@ FakeNearbyShareLocalDeviceDataManager::Factory::~Factory() = default;
std::unique_ptr<NearbyShareLocalDeviceDataManager>
FakeNearbyShareLocalDeviceDataManager::Factory::CreateInstance(
nearby::Context* context, SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider) {
nearby::Context* context, SharingRpcClientFactory* rpc_client_factory) {
latest_rpc_client_factory_ = rpc_client_factory;
latest_profile_info_provider_ = profile_info_provider;
auto instance = std::make_unique<FakeNearbyShareLocalDeviceDataManager>(
kDefaultDeviceName);
@@ -57,21 +57,16 @@ class FakeNearbyShareLocalDeviceDataManager
return latest_rpc_client_factory_;
}
NearbyShareProfileInfoProvider* latest_profile_info_provider() const {
return latest_profile_info_provider_;
}
protected:
std::unique_ptr<NearbyShareLocalDeviceDataManager> CreateInstance(
nearby::Context* context,
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider) override;
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory)
override;
private:
std::vector<FakeNearbyShareLocalDeviceDataManager*> instances_;
nearby::sharing::api::SharingRpcClientFactory* latest_rpc_client_factory_ =
nullptr;
NearbyShareProfileInfoProvider* latest_profile_info_provider_ = nullptr;
};
struct UploadContactsCall {
@@ -37,7 +37,6 @@
#include "internal/platform/implementation/device_info.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/common/nearby_share_prefs.h"
#include "sharing/common/nearby_share_profile_info_provider.h"
#include "sharing/internal/api/preference_manager.h"
#include "sharing/internal/api/sharing_rpc_client.h"
#include "sharing/internal/base/utf_string_conversions.h"
@@ -108,16 +107,14 @@ std::unique_ptr<NearbyShareLocalDeviceDataManager>
NearbyShareLocalDeviceDataManagerImpl::Factory::Create(
Context* context, PreferenceManager& preference_manager,
AccountManager& account_manager, nearby::DeviceInfo& device_info,
SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider) {
SharingRpcClientFactory* rpc_client_factory) {
if (test_factory_) {
return test_factory_->CreateInstance(context, rpc_client_factory,
profile_info_provider);
return test_factory_->CreateInstance(context, rpc_client_factory);
}
return absl::WrapUnique(new NearbyShareLocalDeviceDataManagerImpl(
context, preference_manager, account_manager, device_info,
rpc_client_factory, profile_info_provider));
rpc_client_factory));
}
// static
@@ -131,12 +128,10 @@ NearbyShareLocalDeviceDataManagerImpl::Factory::~Factory() = default;
NearbyShareLocalDeviceDataManagerImpl::NearbyShareLocalDeviceDataManagerImpl(
Context* context, PreferenceManager& preference_manager,
AccountManager& account_manager, nearby::DeviceInfo& device_info,
SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider)
SharingRpcClientFactory* rpc_client_factory)
: preference_manager_(preference_manager),
account_manager_(account_manager),
device_info_(device_info),
profile_info_provider_(profile_info_provider),
nearby_share_client_(rpc_client_factory->CreateInstance()),
device_id_(GetId()),
download_device_data_scheduler_(
@@ -222,17 +217,17 @@ DeviceNameValidationResult NearbyShareLocalDeviceDataManagerImpl::SetDeviceName(
void NearbyShareLocalDeviceDataManagerImpl::DownloadDeviceData() {
executor_->PostTask([&]() {
NL_LOG(INFO) << __func__ << ": started";
LOG(INFO) << __func__ << ": started";
if (!is_running()) {
NL_LOG(WARNING) << "DownloadDeviceData: skip to download device data due "
"to manager is stopped.";
LOG(WARNING) << "DownloadDeviceData: skip to download device data due "
"to manager is stopped.";
return;
}
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING) << __func__
<< ": skip to download device data due "
"to no login account.";
LOG(WARNING) << __func__
<< ": skip to download device data due "
"to no login account.";
download_device_data_scheduler_->HandleResult(/*success=*/true);
return;
}
@@ -244,17 +239,17 @@ void NearbyShareLocalDeviceDataManagerImpl::DownloadDeviceData() {
request, [this](const absl::StatusOr<UpdateDeviceResponse>& response) {
// check whether the manager is running again
if (!is_running()) {
NL_LOG(WARNING)
LOG(WARNING)
<< "DownloadDeviceData: skip to download device data due "
"to manager is stopped.";
return;
}
if (response.ok()) {
NL_LOG(WARNING) << "DownloadDeviceData: Got response from backend.";
LOG(WARNING) << "DownloadDeviceData: Got response from backend.";
HandleUpdateDeviceResponse(*response);
} else {
NL_LOG(WARNING)
LOG(WARNING)
<< "DownloadDeviceData: Failed to get response from backend: "
<< response.status();
}
@@ -270,18 +265,18 @@ void NearbyShareLocalDeviceDataManagerImpl::UploadContacts(
UploadCompleteCallback callback) {
executor_->PostTask(
[&, contacts = std::move(contacts), callback = std::move(callback)]() {
NL_LOG(INFO) << __func__ << ": size=" << contacts.size();
LOG(INFO) << __func__ << ": size=" << contacts.size();
if (!is_running()) {
NL_LOG(WARNING) << "UploadContacts: skip to upload contacts due "
"to manager is stopped.";
LOG(WARNING) << "UploadContacts: skip to upload contacts due "
"to manager is stopped.";
callback(false);
return;
}
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING) << __func__
<< ": skip to upload contacts due "
"to no login account.";
LOG(WARNING) << __func__
<< ": skip to upload contacts due "
"to no login account.";
callback(/*success=*/true);
return;
}
@@ -297,7 +292,7 @@ void NearbyShareLocalDeviceDataManagerImpl::UploadContacts(
request, [callback = std::move(callback)](
const absl::StatusOr<UpdateDeviceResponse>& response) {
if (!response.ok()) {
NL_LOG(WARNING)
LOG(WARNING)
<< "UploadContacts: Failed to get response from backend: "
<< response.status();
}
@@ -311,19 +306,19 @@ void NearbyShareLocalDeviceDataManagerImpl::UploadCertificates(
UploadCompleteCallback callback) {
executor_->PostTask([&, certificates = std::move(certificates),
callback = std::move(callback)]() {
NL_LOG(INFO) << __func__ << ": Upload " << certificates.size()
<< " certificates.";
LOG(INFO) << __func__ << ": Upload " << certificates.size()
<< " certificates.";
if (!is_running()) {
NL_LOG(WARNING) << "UploadContacts: skip to upload certificates due "
"to manager is stopped.";
LOG(WARNING) << "UploadContacts: skip to upload certificates due "
"to manager is stopped.";
callback(false);
return;
}
if (!account_manager_.GetCurrentAccount().has_value()) {
NL_LOG(WARNING) << __func__
<< ": skip to upload certificates due "
"to no login account.";
LOG(WARNING) << __func__
<< ": skip to upload certificates due "
"to no login account.";
callback(/*success=*/true);
return;
}
@@ -337,17 +332,17 @@ void NearbyShareLocalDeviceDataManagerImpl::UploadCertificates(
std::string(kCertificatesFieldMaskPath));
nearby_share_client_->UpdateDevice(
request, [this, callback = std::move(callback)](
const absl::StatusOr<UpdateDeviceResponse>& response) {
const absl::StatusOr<UpdateDeviceResponse>& response) {
// check whether the manager is running again
if (!is_running()) {
NL_LOG(WARNING)
LOG(WARNING)
<< "DownloadDeviceData: skip to upload certificates due "
"to manager is stopped.";
callback(false);
return;
}
if (!response.ok()) {
NL_LOG(WARNING)
LOG(WARNING)
<< "UploadCertificates: Failed to get response from backend: "
<< response.status();
}
@@ -368,27 +363,29 @@ void NearbyShareLocalDeviceDataManagerImpl::OnStop() {
std::string NearbyShareLocalDeviceDataManagerImpl::GetDefaultDeviceName()
const {
std::string device_name = device_info_.GetOsDeviceName();
std::optional<AccountManager::Account> account =
account_manager_.GetCurrentAccount();
DeviceInfo::OsType os_type = device_info_.GetOsType();
std::string device_type = device_info_.GetDeviceTypeName();
std::optional<std::string> given_name =
profile_info_provider_->GetGivenName();
// If not logged in or account has not given name, use machine name instead.
// For iOS and macOS, the device name is already localized and generally works
// well for Quick Share purposes (i.e. "Niko's MacBook Pro"), so avoid using
// the non-localized account name and device type concatenation.
if (os_type == DeviceInfo::OsType::kMacOS ||
os_type == DeviceInfo::OsType::kIos || !given_name.has_value()) {
os_type == DeviceInfo::OsType::kIos || !account.has_value() ||
account->given_name.empty()) {
std::string device_name = device_info_.GetOsDeviceName();
return GetTruncatedName(device_name, kNearbyShareDeviceNameMaxLength);
}
std::string given_name = account->given_name;
std::string device_type = device_info_.GetDeviceTypeName();
uint64_t untruncated_length =
absl::Substitute(kDefaultDeviceName, *given_name, device_type).length();
absl::Substitute(kDefaultDeviceName, given_name, device_type).length();
uint64_t overflow_length =
untruncated_length - kNearbyShareDeviceNameMaxLength;
std::string truncated_name =
GetTruncatedName(*given_name, given_name->length() - overflow_length);
GetTruncatedName(given_name, given_name.length() - overflow_length);
return absl::Substitute(kDefaultDeviceName, truncated_name, device_type);
}
@@ -51,16 +51,14 @@ class NearbyShareLocalDeviceDataManagerImpl
Context* context,
nearby::sharing::api::PreferenceManager& preference_manager,
AccountManager& account_manager, nearby::DeviceInfo& device_info,
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider);
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory);
static void SetFactoryForTesting(Factory* test_factory);
protected:
virtual ~Factory();
virtual std::unique_ptr<NearbyShareLocalDeviceDataManager> CreateInstance(
Context* context,
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider) = 0;
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory) = 0;
private:
static Factory* test_factory_;
@@ -73,8 +71,7 @@ class NearbyShareLocalDeviceDataManagerImpl
Context* context,
nearby::sharing::api::PreferenceManager& preference_manager,
AccountManager& account_manager, nearby::DeviceInfo& device_info,
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory,
NearbyShareProfileInfoProvider* profile_info_provider);
nearby::sharing::api::SharingRpcClientFactory* rpc_client_factory);
// NearbyShareLocalDeviceDataManager:
std::string GetId() override;
@@ -108,7 +105,6 @@ class NearbyShareLocalDeviceDataManagerImpl
nearby::sharing::api::PreferenceManager& preference_manager_;
AccountManager& account_manager_;
nearby::DeviceInfo& device_info_;
NearbyShareProfileInfoProvider* const profile_info_provider_;
std::unique_ptr<nearby::sharing::api::SharingRpcClient> nearby_share_client_;
const std::string device_id_;
std::unique_ptr<NearbyShareScheduler> download_device_data_scheduler_;
@@ -33,7 +33,6 @@
#include "internal/test/fake_account_manager.h"
#include "internal/test/fake_device_info.h"
#include "internal/test/fake_task_runner.h"
#include "sharing/common/fake_nearby_share_profile_info_provider.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/common/nearby_share_prefs.h"
#include "sharing/internal/api/fake_nearby_share_client.h"
@@ -130,11 +129,11 @@ class NearbyShareLocalDeviceDataManagerImplTest
FakeTaskRunner::ResetPendingTasksCount();
prefs::RegisterNearbySharingPrefs(preference_manager_);
NearbyShareSchedulerFactory::SetFactoryForTesting(&scheduler_factory_);
profile_info_provider()->set_given_name(kFakeGivenName);
AccountManager::Account account;
account.id = kTestAccountId;
account.email = kTestProfileUserName;
account.given_name = kFakeGivenName;
fake_account_manager_.SetAccount(account);
}
@@ -153,7 +152,7 @@ class NearbyShareLocalDeviceDataManagerImplTest
void CreateManager() {
manager_ = NearbyShareLocalDeviceDataManagerImpl::Factory::Create(
&context_, preference_manager_, fake_account_manager_,
fake_device_info_, &nearby_client_factory_, &profile_info_provider_);
fake_device_info_, &nearby_client_factory_);
manager_->AddObserver(this);
++num_manager_creations_;
num_download_device_data_ = 0;
@@ -247,9 +246,7 @@ class NearbyShareLocalDeviceDataManagerImplTest
NearbyShareLocalDeviceDataManager* manager() { return manager_.get(); }
FakeNearbyShareProfileInfoProvider* profile_info_provider() {
return &profile_info_provider_;
}
FakeAccountManager& fake_account_manager() { return fake_account_manager_; }
const std::vector<ObserverNotification>& notifications() {
return notifications_;
@@ -299,7 +296,6 @@ class NearbyShareLocalDeviceDataManagerImplTest
size_t num_download_device_data_ = 0;
std::vector<ObserverNotification> notifications_;
FakeNearbyShareClientFactory nearby_client_factory_;
FakeNearbyShareProfileInfoProvider profile_info_provider_;
FakeNearbyShareSchedulerFactory scheduler_factory_;
std::unique_ptr<NearbyShareLocalDeviceDataManager> manager_;
};
@@ -322,14 +318,15 @@ TEST_F(NearbyShareLocalDeviceDataManagerImplTest, DeviceId) {
TEST_F(NearbyShareLocalDeviceDataManagerImplTest, DefaultDeviceName) {
CreateManager();
// If given name is null, only return the device type.
profile_info_provider()->set_given_name(std::nullopt);
AccountManager::Account account = *fake_account_manager().GetCurrentAccount();
// Clear login account.
fake_account_manager().SetAccount(std::nullopt);
EXPECT_EQ(manager()->GetDeviceName(),
GetDeviceName());
// Set given name and expect full default device name of the form
// "<given name>'s <device type>."
profile_info_provider()->set_given_name(kFakeGivenName);
fake_account_manager().SetAccount(account);
EXPECT_EQ(absl::Substitute(kDefaultDeviceName,
kFakeGivenName,
GetDeviceTypeName()),
@@ -337,7 +334,8 @@ TEST_F(NearbyShareLocalDeviceDataManagerImplTest, DefaultDeviceName) {
// Make sure that when we use a given name that is very long we truncate
// correctly.
profile_info_provider()->set_given_name(kFakeTooLongGivenName);
account.given_name = kFakeTooLongGivenName;
fake_account_manager().SetAccount(account);
EXPECT_EQ(kNearbyShareDeviceNameMaxLength, manager()->GetDeviceName().size());
}
@@ -356,7 +354,6 @@ TEST_F(NearbyShareLocalDeviceDataManagerImplTest, ValidateDeviceName) {
TEST_F(NearbyShareLocalDeviceDataManagerImplTest, SetDeviceName) {
CreateManager();
profile_info_provider()->set_given_name(kFakeGivenName);
std::string expected_default_device_name =
absl::Substitute(kDefaultDeviceName, kFakeGivenName, GetDeviceTypeName());
EXPECT_EQ(manager()->GetDeviceName(), expected_default_device_name);
@@ -1,47 +0,0 @@
// Copyright 2022-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sharing/nearby_share_profile_info_provider_impl.h"
#include <optional>
#include <string>
#include "internal/platform/device_info.h"
#include "internal/platform/implementation/account_manager.h"
namespace nearby {
namespace sharing {
NearbyShareProfileInfoProviderImpl::NearbyShareProfileInfoProviderImpl(
nearby::DeviceInfo& device_info, AccountManager& account_manager)
: device_info_(device_info), account_manager_(account_manager) {}
NearbyShareProfileInfoProviderImpl::~NearbyShareProfileInfoProviderImpl() =
default;
std::optional<std::string> NearbyShareProfileInfoProviderImpl::GetGivenName()
const {
// Use the given name when the user logs in to the backend.
std::optional<AccountManager::Account> account =
account_manager_.GetCurrentAccount();
if (account.has_value() && !account->given_name.empty()) {
return account->given_name;
}
return device_info_.GetGivenName();
}
} // namespace sharing
} // namespace nearby
@@ -1,52 +0,0 @@
// Copyright 2022-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_SHARING_NEARBY_SHARE_PROFILE_INFO_PROVIDER_IMPL_H_
#define THIRD_PARTY_NEARBY_SHARING_NEARBY_SHARE_PROFILE_INFO_PROVIDER_IMPL_H_
#include <optional>
#include <string>
#include "internal/platform/device_info.h"
#include "internal/platform/implementation/account_manager.h"
#include "sharing/common/nearby_share_profile_info_provider.h"
namespace nearby {
namespace sharing {
// An implementation of NearbyShareProfileInfoProvider that accesses the actual
// profile data.
class NearbyShareProfileInfoProviderImpl
: public NearbyShareProfileInfoProvider {
public:
NearbyShareProfileInfoProviderImpl(nearby::DeviceInfo& device_info,
AccountManager& account_manager);
NearbyShareProfileInfoProviderImpl(
const NearbyShareProfileInfoProviderImpl&) = delete;
NearbyShareProfileInfoProviderImpl& operator=(
const NearbyShareProfileInfoProviderImpl&) = delete;
~NearbyShareProfileInfoProviderImpl() override;
// NearbyShareProfileInfoProvider:
std::optional<std::string> GetGivenName() const override;
private:
nearby::DeviceInfo& device_info_;
AccountManager& account_manager_;
};
} // namespace sharing
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_SHARING_NEARBY_SHARE_PROFILE_INFO_PROVIDER_IMPL_H_
@@ -1,101 +0,0 @@
// Copyright 2022-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sharing/nearby_share_profile_info_provider_impl.h"
#include <optional>
#include <string>
#include "gtest/gtest.h"
#include "internal/platform/implementation/account_manager.h"
#include "internal/test/fake_account_manager.h"
#include "internal/test/fake_device_info.h"
namespace nearby {
namespace sharing {
namespace {
constexpr char kTestAccountId[] = "test_account_id";
constexpr char kTestAccountGivenName[] = "given_name";
constexpr char kExpectedTestAccountGivenName[] = "given_name";
constexpr char kProfileGivenName[] = "Barack";
} // namespace
class NearbyShareProfileInfoProviderImplTest : public ::testing::Test {
protected:
NearbyShareProfileInfoProviderImplTest() = default;
~NearbyShareProfileInfoProviderImplTest() override = default;
void SetUp() override {
fake_device_info_.SetGivenName(std::nullopt);
}
void SetUserGivenName(const std::string& name) {
fake_device_info_.SetGivenName(name);
}
FakeDeviceInfo& fake_device_info() {
return fake_device_info_;
}
FakeAccountManager& fake_account_manager() { return fake_account_manager_; }
private:
FakeAccountManager fake_account_manager_;
FakeDeviceInfo fake_device_info_;
};
TEST_F(NearbyShareProfileInfoProviderImplTest, GivenName) {
NearbyShareProfileInfoProviderImpl profile_info_provider(
fake_device_info(), fake_account_manager());
// If no user, return std::nullopt.
EXPECT_FALSE(profile_info_provider.GetGivenName());
// If given name is empty, return std::nullopt.
SetUserGivenName(std::string());
EXPECT_FALSE(profile_info_provider.GetGivenName());
SetUserGivenName(kProfileGivenName);
EXPECT_EQ(profile_info_provider.GetGivenName(), kProfileGivenName);
}
TEST_F(NearbyShareProfileInfoProviderImplTest, GivenNameUseLoginAccount) {
AccountManager::Account account;
account.id = kTestAccountId;
account.given_name = kTestAccountGivenName;
fake_account_manager().SetAccount(account);
SetUserGivenName(kProfileGivenName);
NearbyShareProfileInfoProviderImpl profile_info_provider(
fake_device_info(), fake_account_manager());
EXPECT_EQ(profile_info_provider.GetGivenName(),
kExpectedTestAccountGivenName);
}
TEST_F(NearbyShareProfileInfoProviderImplTest,
GivenNameNotUseLoginAccountWhenGivenNameEmpty) {
AccountManager::Account account;
account.id = kTestAccountId;
fake_account_manager().SetAccount(account);
SetUserGivenName(kProfileGivenName);
NearbyShareProfileInfoProviderImpl profile_info_provider(
fake_device_info(), fake_account_manager());
EXPECT_EQ(profile_info_provider.GetGivenName(), kProfileGivenName);
}
} // namespace sharing
} // namespace nearby
+1 -6
View File
@@ -86,7 +86,6 @@
#include "sharing/nearby_connections_manager.h"
#include "sharing/nearby_connections_types.h"
#include "sharing/nearby_file_handler.h"
#include "sharing/nearby_share_profile_info_provider_impl.h"
#include "sharing/nearby_sharing_decoder.h"
#include "sharing/nearby_sharing_service.h"
#include "sharing/nearby_sharing_service_extension.h"
@@ -202,14 +201,10 @@ NearbySharingServiceImpl::NearbySharingServiceImpl(
nearby_connections_manager_(std::move(nearby_connections_manager)),
nearby_share_client_factory_(
sharing_platform.CreateSharingRpcClientFactory(&analytics_recorder_)),
profile_info_provider_(
std::make_unique<NearbyShareProfileInfoProviderImpl>(
device_info_, account_manager_)),
local_device_data_manager_(
NearbyShareLocalDeviceDataManagerImpl::Factory::Create(
context_, preference_manager_, account_manager_, device_info_,
nearby_share_client_factory_.get(),
profile_info_provider_.get())),
nearby_share_client_factory_.get())),
contact_manager_(NearbyShareContactManagerImpl::Factory::Create(
context_, preference_manager_, account_manager_,
nearby_share_client_factory_.get(),
-2
View File
@@ -47,7 +47,6 @@
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "sharing/certificates/nearby_share_private_certificate.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/common/nearby_share_profile_info_provider.h"
#include "sharing/fast_initiation/nearby_fast_initiation.h"
#include "sharing/incoming_share_session.h"
#include "sharing/internal/api/app_info.h"
@@ -481,7 +480,6 @@ class NearbySharingServiceImpl
std::unique_ptr<NearbyConnectionsManager> nearby_connections_manager_;
std::unique_ptr<nearby::sharing::api::SharingRpcClientFactory>
nearby_share_client_factory_;
std::unique_ptr<NearbyShareProfileInfoProvider> profile_info_provider_;
std::unique_ptr<NearbyShareLocalDeviceDataManager> local_device_data_manager_;
std::unique_ptr<NearbyShareContactManager> contact_manager_;
std::unique_ptr<NearbyShareCertificateManager> certificate_manager_;