1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/device_name/device_name_store_impl.h"
#include "base/logging.h"
#include "chrome/browser/ash/device_name/device_name_applier_impl.h"
#include "chrome/browser/ash/device_name/device_name_validator.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
namespace ash {
namespace {
const char kDefaultDeviceName[] = "ChromeOS";
} // namespace
DeviceNameStoreImpl::DeviceNameStoreImpl(
PrefService* prefs,
policy::DeviceNamePolicyHandler* handler)
: DeviceNameStoreImpl(prefs,
handler,
std::make_unique<DeviceNameApplierImpl>()) {}
DeviceNameStoreImpl::DeviceNameStoreImpl(
PrefService* prefs,
policy::DeviceNamePolicyHandler* handler,
std::unique_ptr<DeviceNameApplier> device_name_applier)
: prefs_(prefs),
handler_(handler),
device_name_applier_(std::move(device_name_applier)) {
policy_handler_observation_.Observe(handler_.get());
if (g_browser_process->profile_manager())
profile_manager_observer_.Observe(g_browser_process->profile_manager());
// Gets the device name according to the device name policy set. If empty, the
// name in prefs is not set yet and hence we set it to the default name.
std::string device_name = ComputeDeviceName();
if (device_name.empty())
device_name = kDefaultDeviceName;
device_name_state_ = ComputeDeviceNameState();
ChangeDeviceName(device_name);
}
DeviceNameStoreImpl::~DeviceNameStoreImpl() = default;
std::string DeviceNameStoreImpl::GetDeviceName() const {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(prefs_);
return prefs_->GetString(prefs::kDeviceName);
}
DeviceNameStore::DeviceNameMetadata DeviceNameStoreImpl::GetDeviceNameMetadata()
const {
return {GetDeviceName(), device_name_state_};
}
DeviceNameStore::DeviceNameState DeviceNameStoreImpl::ComputeDeviceNameState()
const {
if (IsConfiguringDeviceNameProhibitedByPolicy())
return DeviceNameState::kCannotBeModifiedBecauseOfPolicy;
if (CannotModifyBecauseNotDeviceOwner())
return DeviceNameState::kCannotBeModifiedBecauseNotDeviceOwner;
return DeviceNameState::kCanBeModified;
}
bool DeviceNameStoreImpl::CannotModifyBecauseNotDeviceOwner() const {
// If kNoPolicy is not in place, then the device is managed and user does not
// have to be device owner to be able to change the name.
if (handler_->GetDeviceNamePolicy() !=
policy::DeviceNamePolicyHandler::DeviceNamePolicy::kNoPolicy) {
return false;
}
// If device is unmanaged, then user has to be the device owner to be able to
// change the name.
if (is_user_owner_)
return false;
return true;
}
bool DeviceNameStoreImpl::IsConfiguringDeviceNameProhibitedByPolicy() const {
switch (handler_->GetDeviceNamePolicy()) {
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameNotConfigurable:
[[fallthrough]];
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameChosenByAdmin:
return true;
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameConfigurableByManagedUser:
[[fallthrough]];
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::kNoPolicy:
return false;
}
}
void DeviceNameStoreImpl::ChangeDeviceName(const std::string& device_name) {
device_name_applier_->SetDeviceName(device_name);
prefs_->SetString(prefs::kDeviceName, device_name);
}
void DeviceNameStoreImpl::AttemptDeviceNameUpdate(
const std::string& new_device_name) {
std::string old_device_name = GetDeviceName();
DeviceNameStore::DeviceNameState new_state = ComputeDeviceNameState();
if (old_device_name == new_device_name && device_name_state_ == new_state)
return;
if (old_device_name != new_device_name)
ChangeDeviceName(new_device_name);
device_name_state_ = new_state;
NotifyDeviceNameMetadataChanged();
}
DeviceNameStore::SetDeviceNameResult DeviceNameStoreImpl::SetDeviceName(
const std::string& new_device_name) {
if (device_name_state_ == DeviceNameState::kCannotBeModifiedBecauseOfPolicy)
return SetDeviceNameResult::kProhibitedByPolicy;
if (device_name_state_ ==
DeviceNameState::kCannotBeModifiedBecauseNotDeviceOwner)
return SetDeviceNameResult::kNotDeviceOwner;
if (!IsValidDeviceName(new_device_name))
return SetDeviceNameResult::kInvalidName;
AttemptDeviceNameUpdate(new_device_name);
return SetDeviceNameResult::kSuccess;
}
std::string DeviceNameStoreImpl::ComputeDeviceName() const {
switch (handler_->GetDeviceNamePolicy()) {
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameChosenByAdmin:
return *handler_->GetHostnameChosenByAdministrator();
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameNotConfigurable:
return kDefaultDeviceName;
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::
kPolicyHostnameConfigurableByManagedUser:
return GetDeviceName();
case policy::DeviceNamePolicyHandler::DeviceNamePolicy::kNoPolicy:
return GetDeviceName();
}
}
void DeviceNameStoreImpl::AttemptUpdateToComputedDeviceName() {
AttemptDeviceNameUpdate(ComputeDeviceName());
}
void DeviceNameStoreImpl::OnHostnamePolicyChanged() {
AttemptUpdateToComputedDeviceName();
}
void DeviceNameStoreImpl::AttemptDeviceNameStateUpdate(bool is_user_owner) {
is_user_owner_ = is_user_owner;
AttemptUpdateToComputedDeviceName();
}
void DeviceNameStoreImpl::OnProfileAdded(Profile* profile) {
DCHECK(profile);
OwnerSettingsServiceAsh* service =
OwnerSettingsServiceAshFactory::GetForBrowserContext(profile);
if (service) {
service->IsOwnerAsync(
base::BindOnce(&DeviceNameStoreImpl::AttemptDeviceNameStateUpdate,
weak_factory_.GetWeakPtr()));
} else {
VLOG(1) << "Owner settings service unavailable for added profile, will "
"not update device name state.";
}
}
void DeviceNameStoreImpl::OnProfileManagerDestroying() {
profile_manager_observer_.Reset();
}
} // namespace ash
|