Commit 8a91e29d authored by James Hawkins's avatar James Hawkins Committed by Commit Bot

OS Settings: Create DeviceNameStore.

The initial implementation creates and persists a device name at
startup, but this is not yet used anywhere else.

Bug: 126802
Test: DeviceNameStoreTest.*
Change-Id: Ibd185803947e90b53f87a789c4d4727e538d3a8a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2523741
Commit-Queue: James Hawkins <jhawkins@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827041}
parent d40c361d
......@@ -1159,6 +1159,8 @@ source_set("chromeos") {
"dbus/vm/vm_permission_service_provider.h",
"dbus/vm_applications_service_provider.cc",
"dbus/vm_applications_service_provider.h",
"device_name_store.cc",
"device_name_store.h",
"device_sync/device_sync_client_factory.cc",
"device_sync/device_sync_client_factory.h",
"display/quirks_manager_delegate_impl.cc",
......@@ -3415,6 +3417,7 @@ source_set("unit_tests") {
"cryptauth/client_app_metadata_provider_service_unittest.cc",
"customization/customization_document_unittest.cc",
"dbus/proxy_resolution_service_provider_unittest.cc",
"device_name_store_unittest.cc",
"drive/drive_integration_service_unittest.cc",
"drive/drivefs_native_message_host_unittest.cc",
"drive/file_system_util_unittest.cc",
......
......@@ -67,6 +67,7 @@
#include "chrome/browser/chromeos/dbus/virtual_file_request_service_provider.h"
#include "chrome/browser/chromeos/dbus/vm/vm_permission_service_provider.h"
#include "chrome/browser/chromeos/dbus/vm_applications_service_provider.h"
#include "chrome/browser/chromeos/device_name_store.h"
#include "chrome/browser/chromeos/display/quirks_manager_delegate_impl.h"
#include "chrome/browser/chromeos/events/event_rewriter_delegate_impl.h"
#include "chrome/browser/chromeos/extensions/default_app_order.h"
......@@ -757,6 +758,11 @@ void ChromeBrowserMainPartsChromeos::PreProfileInit() {
arc_kiosk_app_manager_.reset(new ArcKioskAppManager());
web_kiosk_app_manager_.reset(new WebKioskAppManager());
if (base::FeatureList::IsEnabled(features::kEnableHostnameSetting)) {
DeviceNameStore::GetInstance()->Initialize(
g_browser_process->local_state());
}
// Make sure that wallpaper boot transition and other delays in OOBE
// are disabled for tests and kiosk app launch by default.
// Individual tests may enable them if they want.
......
// 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 "chrome/browser/chromeos/device_name_store.h"
#include <math.h>
#include "base/rand_util.h"
#include "base/strings/char_traits.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
namespace chromeos {
namespace {
const size_t kMaxDeviceNameLength = 15;
// Returns a randomly generated device name of the form 'ChromeOS_123456'.
std::string GenerateDeviceName() {
static constexpr const char* kDeviceNamePrefix = "ChromeOS_";
constexpr size_t kPrefixLength =
base::CharTraits<char>::length(kDeviceNamePrefix);
constexpr size_t kNumDigits = kMaxDeviceNameLength - kPrefixLength;
// The algorithm below uses the range of integers between 10^n and double
// that value to create a string of n digits representing the 10^n values in
// that range while preserving leading zeroes.
//
// Example: 3 digits
// Expected output: 000...999
// Rand[1000, 1999] -> 1000 -> 1{000} -> "000"
// Rand[1000, 1999] -> 1782 -> 1{782} -> "782"
int min = std::pow(10, kNumDigits);
int max = 2 * min - 1;
int rand_num = base::RandInt(min, max);
std::string rand_num_str = base::NumberToString(rand_num).substr(1);
return kDeviceNamePrefix + rand_num_str;
}
} // namespace
// static
DeviceNameStore* DeviceNameStore::GetInstance() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return base::Singleton<DeviceNameStore>::get();
}
// static
void DeviceNameStore::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
DCHECK(registry);
registry->RegisterStringPref(prefs::kDeviceName, "");
}
void DeviceNameStore::Initialize(PrefService* prefs) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(prefs);
prefs_ = prefs;
std::string device_name = prefs_->GetString(prefs::kDeviceName);
if (device_name.empty()) {
device_name = GenerateDeviceName();
prefs_->SetString(prefs::kDeviceName, device_name);
}
}
std::string DeviceNameStore::GetDeviceName() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(prefs_);
return prefs_->GetString(prefs::kDeviceName);
}
} // namespace chromeos
// 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_CHROMEOS_DEVICE_NAME_STORE_H_
#define CHROME_BROWSER_CHROMEOS_DEVICE_NAME_STORE_H_
#include <string>
#include "base/gtest_prod_util.h"
#include "base/memory/singleton.h"
class PrefRegistrySimple;
class PrefService;
namespace chromeos {
// DeviceNameStore is a device-persistent model of the device name which
// clients use to display the device name to the user and broadcast the device
// name to local networks (e.g., the DHCP hostname). Clients also have the
// ability to set the device name (e.g., OS Settings).
//
// An initial device name is created on first boot and is of the form
// 'ChromeOS-123456'.
//
// DeviceNameStore is responsible for updating the system state to reflect
// changes to the device name, e.g., setting the hostname via Shill.
//
// Must only be used on the UI thread.
class DeviceNameStore {
public:
// Returns a pointer to the singleton instance for the current process.
static DeviceNameStore* GetInstance();
// Register the pref used to store the device name in the local state.
static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
// Checks the local state for a device name to store if it exists; otherwise,
// creates a new device name and persists it. Must be called before any other
// non-static method on DeviceNameStore.
// |prefs| is the PrefService used to persist and read the device name value.
void Initialize(PrefService* prefs);
std::string GetDeviceName() const;
private:
FRIEND_TEST_ALL_PREFIXES(DeviceNameStoreTest, Initialize);
FRIEND_TEST_ALL_PREFIXES(DeviceNameStoreTest, GenerateDeviceName);
friend class DeviceNameStoreTest;
friend struct base::DefaultSingletonTraits<DeviceNameStore>;
DeviceNameStore() = default;
~DeviceNameStore() = default;
DeviceNameStore(const DeviceNameStore&) = delete;
DeviceNameStore& operator=(const DeviceNameStore&) = delete;
// Provides access and persistence for the device name value.
PrefService* prefs_;
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_DEVICE_NAME_STORE_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 "chrome/browser/chromeos/device_name_store.h"
#include "base/strings/string_util.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
class DeviceNameStoreTest : public ::testing::Test {
public:
DeviceNameStoreTest() {
DeviceNameStore::RegisterLocalStatePrefs(local_state_.registry());
device_name_store_.Initialize(&local_state_);
}
~DeviceNameStoreTest() override = default;
DeviceNameStore* device_name_store() { return &device_name_store_; }
private:
// Run on the UI thread.
content::BrowserTaskEnvironment task_environment_;
// Test backing store for prefs.
TestingPrefServiceSimple local_state_;
DeviceNameStore device_name_store_;
};
TEST_F(DeviceNameStoreTest, Initialize) {
TestingPrefServiceSimple local_state;
DeviceNameStore device_name_store;
DeviceNameStore::RegisterLocalStatePrefs(local_state.registry());
// The device name is not set yet.
EXPECT_TRUE(local_state.GetString(prefs::kDeviceName).empty());
device_name_store.Initialize(&local_state);
// Initialize now set the device name and persisted it to the local state.
std::string device_name = device_name_store.GetDeviceName();
std::string persisted_device_name = local_state.GetString(prefs::kDeviceName);
EXPECT_FALSE(device_name.empty());
EXPECT_FALSE(persisted_device_name.empty());
EXPECT_EQ(device_name, persisted_device_name);
}
// Tests that the format of the generated device name matches the form
// ChromeOS_123456.
TEST_F(DeviceNameStoreTest, GenerateDeviceName) {
// The device name is already generated at this point because of the call to
// Initialize during test setup.
std::string device_name = device_name_store()->GetDeviceName();
EXPECT_TRUE(base::StartsWith(device_name, "ChromeOS_"));
// Check that the string after the prefix is composed of digits.
std::string digits = device_name.substr(strlen("ChromeOS_"));
for (size_t i = 0; i < digits.length(); ++i) {
EXPECT_TRUE(base::IsAsciiDigit(digits[i]));
}
}
} // namespace chromeos
......@@ -171,6 +171,7 @@
#include "extensions/browser/api/runtime/runtime_api.h"
#include "extensions/browser/extension_prefs.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/device_name_store.h"
#include "chrome/browser/chromeos/extensions/extensions_permissions_tracker.h"
#include "chrome/browser/chromeos/guest_os/guest_os_share_path.h"
#include "chrome/browser/chromeos/kerberos/kerberos_credentials_manager.h"
......@@ -669,6 +670,7 @@ void RegisterLocalState(PrefRegistrySimple* registry) {
chromeos::DemoModeResourcesRemover::RegisterLocalStatePrefs(registry);
chromeos::DemoSession::RegisterLocalStatePrefs(registry);
chromeos::DemoSetupController::RegisterLocalStatePrefs(registry);
chromeos::DeviceNameStore::RegisterLocalStatePrefs(registry);
chromeos::DeviceOAuth2TokenStoreChromeOS::RegisterPrefs(registry);
chromeos::device_settings_cache::RegisterPrefs(registry);
chromeos::EasyUnlockService::RegisterPrefs(registry);
......
......@@ -1029,6 +1029,9 @@ const char kLoginExtensionApiLaunchExtensionId[] =
// String containing last RSU lookup key uploaded. Empty until first upload.
const char kLastRsuDeviceIdUploaded[] = "rsu.last_rsu_device_id_uploaded";
// A string pref stored in local state containing the name of the device.
const char kDeviceName[] = "device_name";
// Boolean user profile pref that determines whether to show a banner in browser
// settings that links to OS settings.
const char kSettingsShowOSBanner[] = "settings.cros.show_os_banner";
......
......@@ -751,6 +751,7 @@ extern const char
kAutoScreenBrightnessMetricsUnsupportedAlsUserAdjustmentCount[];
extern const char kKnownUserParentAccessCodeConfig[];
extern const char kLastRsuDeviceIdUploaded[];
extern const char kDeviceName[];
#endif // defined(OS_CHROMEOS)
......
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