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
@@ -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);