Commit ed123256 authored by Michael Hansen's avatar Michael Hansen Committed by Commit Bot

[Nearby] Limit length of default device name.

When generating a default device name for Nearby Share, we include the
user's given name. If the name is very long, the generated device name
will overflow the maximum allowed length. In this case we will truncate
the user's given name, for example "Mic...'s Chromebook".

Bug: b:169452700
Change-Id: I948e0b7a37ca0e8e7381e19165087ab0a9fc85c0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2446429
Commit-Queue: Michael Hansen <hansenmichael@google.com>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813461}
parent 997e0f11
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h" #include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h"
const size_t kNearbyShareDeviceNameMaxLength = 32;
NearbyShareLocalDeviceDataManager::NearbyShareLocalDeviceDataManager() = NearbyShareLocalDeviceDataManager::NearbyShareLocalDeviceDataManager() =
default; default;
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
#include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h" #include "chrome/browser/nearby_sharing/proto/rpc_resources.pb.h"
#include "chrome/browser/ui/webui/nearby_share/public/mojom/nearby_share_settings.mojom.h" #include "chrome/browser/ui/webui/nearby_share/public/mojom/nearby_share_settings.mojom.h"
// The maximum length in bytes allowed for a device name, as encoded in UTF-8 in
// a std::string, which will not contain a null terminator.
extern const size_t kNearbyShareDeviceNameMaxLength;
// Manages local device data related to the UpdateDevice RPC such as the device // Manages local device data related to the UpdateDevice RPC such as the device
// ID, name, and icon url; provides the user's full name and icon URL returned // ID, name, and icon url; provides the user's full name and icon URL returned
// from the Nearby server; and handles uploading contacts and certificates to // from the Nearby server; and handles uploading contacts and certificates to
......
...@@ -38,10 +38,6 @@ constexpr base::TimeDelta kUpdateDeviceDataTimeout = ...@@ -38,10 +38,6 @@ constexpr base::TimeDelta kUpdateDeviceDataTimeout =
constexpr base::TimeDelta kDeviceDataDownloadPeriod = constexpr base::TimeDelta kDeviceDataDownloadPeriod =
base::TimeDelta::FromHours(1); base::TimeDelta::FromHours(1);
// The maximum length allowed for a device name, as encoded in UTF-8 in a
// std::string, which will not contain a null terminator.
size_t kDeviceNameMaxByteLength = 32;
} // namespace } // namespace
// static // static
...@@ -145,7 +141,7 @@ NearbyShareLocalDeviceDataManagerImpl::ValidateDeviceName( ...@@ -145,7 +141,7 @@ NearbyShareLocalDeviceDataManagerImpl::ValidateDeviceName(
if (!base::IsStringUTF8(name)) if (!base::IsStringUTF8(name))
return nearby_share::mojom::DeviceNameValidationResult::kErrorNotValidUtf8; return nearby_share::mojom::DeviceNameValidationResult::kErrorNotValidUtf8;
if (name.length() > kDeviceNameMaxByteLength) if (name.length() > kNearbyShareDeviceNameMaxLength)
return nearby_share::mojom::DeviceNameValidationResult::kErrorTooLong; return nearby_share::mojom::DeviceNameValidationResult::kErrorTooLong;
return nearby_share::mojom::DeviceNameValidationResult::kValid; return nearby_share::mojom::DeviceNameValidationResult::kValid;
......
...@@ -6,8 +6,10 @@ ...@@ -6,8 +6,10 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chromeos/constants/devicetype.h" #include "chromeos/constants/devicetype.h"
#include "components/user_manager/user_manager.h" #include "components/user_manager/user_manager.h"
...@@ -29,6 +31,16 @@ base::Optional<base::string16> GetNameFromProfile(Profile* profile) { ...@@ -29,6 +31,16 @@ base::Optional<base::string16> GetNameFromProfile(Profile* profile) {
return name.empty() ? base::nullopt : base::make_optional(name); return name.empty() ? base::nullopt : base::make_optional(name);
} }
std::string GetTruncatedName(std::string name, size_t overflow_length) {
std::string ellipsis("...");
size_t max_name_length = name.length() - overflow_length - ellipsis.length();
DCHECK_GT(max_name_length, 0);
std::string truncated;
base::TruncateUTF8ToByteSize(name, max_name_length, &truncated);
truncated.append(ellipsis);
return truncated;
}
} // namespace } // namespace
std::string GetNearbyShareDefaultDeviceName(Profile* profile) { std::string GetNearbyShareDefaultDeviceName(Profile* profile) {
...@@ -38,6 +50,16 @@ std::string GetNearbyShareDefaultDeviceName(Profile* profile) { ...@@ -38,6 +50,16 @@ std::string GetNearbyShareDefaultDeviceName(Profile* profile) {
if (!name_from_profile) if (!name_from_profile)
return base::UTF16ToUTF8(device_type); return base::UTF16ToUTF8(device_type);
return l10n_util::GetStringFUTF8(IDS_NEARBY_DEFAULT_DEVICE_NAME, std::string device_name = l10n_util::GetStringFUTF8(
*name_from_profile, device_type); IDS_NEARBY_DEFAULT_DEVICE_NAME, *name_from_profile, device_type);
if (device_name.length() > kNearbyShareDeviceNameMaxLength) {
std::string truncated_name = GetTruncatedName(
base::UTF16ToUTF8(*name_from_profile),
device_name.length() - kNearbyShareDeviceNameMaxLength);
device_name = l10n_util::GetStringFUTF8(IDS_NEARBY_DEFAULT_DEVICE_NAME,
base::UTF8ToUTF16(truncated_name),
device_type);
}
return device_name;
} }
...@@ -11,7 +11,8 @@ class Profile; ...@@ -11,7 +11,8 @@ class Profile;
// Creates a default device name of the form "<given name>'s <device type>." For // Creates a default device name of the form "<given name>'s <device type>." For
// example, "Josh's Chromebook." If a given name cannot be found, returns just // example, "Josh's Chromebook." If a given name cannot be found, returns just
// the device type. // the device type. If the resulting name is too long the user's name will be
// truncated, for example "Mi...'s Chromebook."
std::string GetNearbyShareDefaultDeviceName(Profile* profile); std::string GetNearbyShareDefaultDeviceName(Profile* profile);
#endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARE_DEFAULT_DEVICE_NAME_H_ #endif // CHROME_BROWSER_NEARBY_SHARING_NEARBY_SHARE_DEFAULT_DEVICE_NAME_H_
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/nearby_sharing/local_device_data/nearby_share_local_device_data_manager.h"
#include "chrome/browser/nearby_sharing/nearby_share_default_device_name.h" #include "chrome/browser/nearby_sharing/nearby_share_default_device_name.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_browser_process.h"
...@@ -24,6 +26,9 @@ namespace { ...@@ -24,6 +26,9 @@ namespace {
const char kFakeGivenName[] = "Josh"; const char kFakeGivenName[] = "Josh";
const char kFakeEmail[] = "fake_account_id@gmail.com"; const char kFakeEmail[] = "fake_account_id@gmail.com";
const char kFakeTooLongGivenName[] = "this is a 33-byte string in utf-8";
const char kFakeTooLongTruncatedDeviceName[] =
"this is a 33-...'s Chrome device";
} // namespace } // namespace
...@@ -60,4 +65,14 @@ TEST(NearbyShareDefaultDeviceNameTest, DefaultDeviceName) { ...@@ -60,4 +65,14 @@ TEST(NearbyShareDefaultDeviceNameTest, DefaultDeviceName) {
base::UTF8ToUTF16(kFakeGivenName), base::UTF8ToUTF16(kFakeGivenName),
ui::GetChromeOSDeviceName()), ui::GetChromeOSDeviceName()),
GetNearbyShareDefaultDeviceName(profile)); GetNearbyShareDefaultDeviceName(profile));
// Make sure that when we use a given name that is very long we truncate
// correctly.
fake_user_manager->UpdateUserAccountData(
id, user_manager::UserManager::UserAccountData(
/*display_name=*/base::string16(),
/*given_name=*/base::UTF8ToUTF16(kFakeTooLongGivenName),
/*locale=*/std::string()));
EXPECT_EQ(kFakeTooLongTruncatedDeviceName,
GetNearbyShareDefaultDeviceName(profile));
} }
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