Commit 2659bec6 authored by Josh Nohle's avatar Josh Nohle Committed by Commit Bot

[Nearby] Create default device name

Create a default device name of the form

  <profile name>'s <device model>.

The profile name is retrieved in the same way as profile_info_handler.cc
[1]. So, the name should be consistent with the one shown in Settings >
"You and Google". The device model is set to model name from SysInfo
HardwareInfo. If the model name is empty, a variation of "Chromebook" is
used for Chrome OS and "device" for other platforms.

[1]https://source.chromium.org/chromium/chromium/src/+/master:chrome/browser/ui/webui/settings/profile_info_handler.cc;l=132-168;drc=b93f16ad9f7a986d964033bbd539a8eec677bfbf

Bug: b/154863679, 1127017
Change-Id: I219cc3965ae0180f66c3953f0a9213dc5c55a1fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2298169
Commit-Queue: Josh Nohle <nohle@chromium.org>
Reviewed-by: default avatarJames Vecore <vecore@google.com>
Cr-Commit-Position: refs/heads/master@{#806294}
parent 74749523
......@@ -3428,6 +3428,8 @@ static_library("browser") {
"nearby_sharing/nearby_process_manager.h",
"nearby_sharing/nearby_receive_manager.cc",
"nearby_sharing/nearby_receive_manager.h",
"nearby_sharing/nearby_share_default_device_name.cc",
"nearby_sharing/nearby_share_default_device_name.h",
"nearby_sharing/nearby_share_settings.cc",
"nearby_sharing/nearby_share_settings.h",
"nearby_sharing/nearby_sharing_service.h",
......@@ -4094,6 +4096,7 @@ static_library("browser") {
"//components/services/font:lib",
"//components/services/font/public/mojom",
"//components/user_manager",
"//ui/chromeos",
"//ui/events/ozone",
"//ui/ozone",
]
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "chrome/browser/nearby_sharing/nearby_share_default_device_name.h"
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
// For profile name retrieval:
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chromeos/constants/devicetype.h"
#include "components/user_manager/user_manager.h"
#include "ui/chromeos/devicetype_utils.h"
#else
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#endif
namespace {
base::Optional<std::string> GetNameFromProfile(Profile* profile) {
if (!profile)
return base::nullopt;
std::string name;
#if defined(OS_CHROMEOS)
const user_manager::User* user =
chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
if (!user)
return base::nullopt;
name = base::UTF16ToUTF8(user->GetDisplayName());
#else // !defined(OS_CHROMEOS)
ProfileAttributesEntry* entry;
if (!g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetProfileAttributesWithPath(profile->GetPath(), &entry)) {
return base::nullopt;
}
name = base::UTF16ToUTF8(entry->GetLocalProfileName());
#endif // defined(OS_CHROMEOS)
return name.empty() ? base::nullopt : base::make_optional(name);
}
void OnHardwareInfoFetched(
Profile* profile,
base::OnceCallback<void(const base::Optional<std::string>&)> callback,
base::SysInfo::HardwareInfo hardware_info) {
base::Optional<std::string> name_from_profile = GetNameFromProfile(profile);
if (!name_from_profile) {
std::move(callback).Run(base::nullopt);
return;
}
#if defined(OS_CHROMEOS)
// For Chrome OS, the returned model values are product code names like Eve.
// We want to use generic names like "Chromebook".
std::string model_name = base::UTF16ToUTF8(ui::GetChromeOSDeviceName());
#else // !defined(OS_CHROMEOS)
// TODO(https://crbug.com/1127017): Localize "Device".
std::string model_name =
hardware_info.model.empty() ? "Device" : hardware_info.model;
#endif // defined(OS_CHROMEOS)
// TODO(https://crbug.com/1127017): Localize string combination.
std::move(callback).Run(*name_from_profile + "'s " + model_name);
}
} // namespace
void GetNearbyShareDefaultDeviceName(
Profile* profile,
base::OnceCallback<void(const base::Optional<std::string>&)> callback) {
base::SysInfo::GetHardwareInfo(
base::BindOnce(&OnHardwareInfoFetched, profile, std::move(callback)));
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARE_DEFAULT_DEVICE_NAME_H_
#define CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARE_DEFAULT_DEVICE_NAME_H_
#include <string>
#include "base/callback.h"
#include "base/optional.h"
class Profile;
// Creates a default device name of the form <profile name>'s <device model>.
void GetNearbyShareDefaultDeviceName(
Profile* profile,
base::OnceCallback<void(const base::Optional<std::string>&)> callback);
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARE_DEFAULT_DEVICE_NAME_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ptr_util.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/system/sys_info.h"
#include "base/test/bind_test_util.h"
#include "chrome/browser/nearby_sharing/nearby_share_default_device_name.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_CHROMEOS)
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "ui/chromeos/devicetype_utils.h"
#endif // defined(OS_CHROMEOS)
namespace {
const char kFakeNameFromProfile[] = "Profile Name";
#if defined(OS_CHROMEOS)
const char kFakeEmail[] = "fake_account_id@gmail.com";
#endif // defined(OS_CHROMEOS)
std::string GetModelNameBlocking() {
base::RunLoop run_loop;
std::string model_name;
base::SysInfo::GetHardwareInfo(base::BindLambdaForTesting(
[&](base::SysInfo::HardwareInfo hardware_info) {
model_name = std::move(hardware_info.model);
run_loop.Quit();
}));
run_loop.Run();
return model_name;
}
} // namespace
TEST(NearbyShareDefaultDeviceNameTest, DefaultDeviceName) {
content::BrowserTaskEnvironment task_environment;
// Configure test profile.
TestingProfileManager profile_manager(TestingBrowserProcess::GetGlobal());
ASSERT_TRUE(profile_manager.SetUp());
Profile* profile = nullptr;
#if defined(OS_CHROMEOS)
chromeos::FakeChromeUserManager* user_manager =
new chromeos::FakeChromeUserManager();
user_manager::ScopedUserManager enabler(base::WrapUnique(user_manager));
profile = profile_manager.CreateTestingProfile(kFakeEmail);
user_manager->AddUser(AccountId::FromUserEmail(kFakeEmail));
user_manager->SaveUserDisplayName(AccountId::FromUserEmail(kFakeEmail),
base::UTF8ToUTF16(kFakeNameFromProfile));
#else // !defined(OS_CHROMEOS)
profile = profile_manager.CreateTestingProfile(kFakeNameFromProfile);
#endif // defined(OS_CHROMEOS)
base::Optional<std::string> device_name;
base::RunLoop run_loop;
GetNearbyShareDefaultDeviceName(
profile,
base::BindLambdaForTesting(
[&run_loop, &device_name](const base::Optional<std::string>& name) {
device_name = std::move(name);
run_loop.Quit();
}));
run_loop.Run();
#if defined(OS_CHROMEOS)
EXPECT_EQ(std::string(kFakeNameFromProfile) + "'s " +
base::UTF16ToUTF8(ui::GetChromeOSDeviceName()),
*device_name);
#else // !defined(OS_CHROMEOS)
std::string expected_model_name = GetModelNameBlocking();
if (expected_model_name.empty()) {
EXPECT_TRUE(
device_name->rfind(std::string(kFakeNameFromProfile) + "'s ", 0) == 0);
} else {
EXPECT_EQ(std::string(kFakeNameFromProfile) + "'s " + expected_model_name,
device_name);
}
#endif // defined(OS_CHROMEOS)
}
......@@ -3800,6 +3800,7 @@ test("unit_tests") {
"../browser/nearby_sharing/nearby_per_session_discovery_manager_unittest.cc",
"../browser/nearby_sharing/nearby_process_manager_unittest.cc",
"../browser/nearby_sharing/nearby_receive_manager_unittest.cc",
"../browser/nearby_sharing/nearby_share_default_device_name_unittest.cc",
"../browser/nearby_sharing/nearby_share_settings_unittest.cc",
"../browser/nearby_sharing/nearby_sharing_service_impl_unittest.cc",
"../browser/nearby_sharing/paired_key_verification_runner_unittest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment