|
| 1 | +// Copyright 2021 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "chromeos/services/bluetooth_config/bluetooth_power_controller_impl.h" |
| 6 | + |
| 7 | +#include "ash/constants/ash_pref_names.h" |
| 8 | +#include "chromeos/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h" |
| 9 | +#include "components/device_event_log/device_event_log.h" |
| 10 | +#include "components/prefs/pref_registry_simple.h" |
| 11 | +#include "components/prefs/pref_service.h" |
| 12 | +#include "components/user_manager/user_manager.h" |
| 13 | + |
| 14 | +namespace chromeos { |
| 15 | +namespace bluetooth_config { |
| 16 | +namespace { |
| 17 | + |
| 18 | +// Decides whether to apply Bluetooth setting based on user type. |
| 19 | +// Returns true if the user type represents a human individual, currently this |
| 20 | +// includes: regular, child, supervised, or active directory. The other types |
| 21 | +// do not represent human account so those account should follow system-wide |
| 22 | +// Bluetooth setting instead. |
| 23 | +bool ShouldApplyUserBluetoothSetting(user_manager::UserType user_type) { |
| 24 | + return user_type == user_manager::USER_TYPE_REGULAR || |
| 25 | + user_type == user_manager::USER_TYPE_CHILD || |
| 26 | + user_type == user_manager::USER_TYPE_ACTIVE_DIRECTORY; |
| 27 | +} |
| 28 | + |
| 29 | +} // namespace |
| 30 | + |
| 31 | +// static |
| 32 | +void BluetoothPowerControllerImpl::RegisterLocalStatePrefs( |
| 33 | + PrefRegistrySimple* registry) { |
| 34 | + registry->RegisterBooleanPref(prefs::kSystemBluetoothAdapterEnabled, |
| 35 | + /*default_value=*/false); |
| 36 | +} |
| 37 | + |
| 38 | +// static |
| 39 | +void BluetoothPowerControllerImpl::RegisterProfilePrefs( |
| 40 | + PrefRegistrySimple* registry) { |
| 41 | + registry->RegisterBooleanPref(prefs::kUserBluetoothAdapterEnabled, |
| 42 | + /*default_value=*/false); |
| 43 | +} |
| 44 | + |
| 45 | +BluetoothPowerControllerImpl::BluetoothPowerControllerImpl( |
| 46 | + AdapterStateController* adapter_state_controller) |
| 47 | + : adapter_state_controller_(adapter_state_controller) {} |
| 48 | + |
| 49 | +BluetoothPowerControllerImpl::~BluetoothPowerControllerImpl() = default; |
| 50 | + |
| 51 | +void BluetoothPowerControllerImpl::SetBluetoothEnabledState(bool enabled) { |
| 52 | + if (primary_profile_prefs_) { |
| 53 | + BLUETOOTH_LOG(EVENT) << "Saving Bluetooth power state of " << enabled |
| 54 | + << " to user prefs."; |
| 55 | + |
| 56 | + primary_profile_prefs_->SetBoolean(ash::prefs::kUserBluetoothAdapterEnabled, |
| 57 | + enabled); |
| 58 | + } else if (local_state_) { |
| 59 | + BLUETOOTH_LOG(EVENT) << "Saving Bluetooth power state of " << enabled |
| 60 | + << " to local state."; |
| 61 | + |
| 62 | + local_state_->SetBoolean(ash::prefs::kSystemBluetoothAdapterEnabled, |
| 63 | + enabled); |
| 64 | + } else { |
| 65 | + BLUETOOTH_LOG(ERROR) |
| 66 | + << "SetBluetoothEnabledState() called before preferences were set"; |
| 67 | + } |
| 68 | + SetAdapterState(enabled); |
| 69 | +} |
| 70 | + |
| 71 | +void BluetoothPowerControllerImpl::SetPrefs(PrefService* primary_profile_prefs, |
| 72 | + PrefService* local_state) { |
| 73 | + InitLocalStatePrefService(local_state); |
| 74 | + InitPrimaryUserPrefService(primary_profile_prefs); |
| 75 | +} |
| 76 | + |
| 77 | +void BluetoothPowerControllerImpl::InitLocalStatePrefService( |
| 78 | + PrefService* local_state) { |
| 79 | + // Return early if |local_state_| has already been initialized or |
| 80 | + // |local_state| is invalid. |
| 81 | + if (local_state_ || !local_state) |
| 82 | + return; |
| 83 | + |
| 84 | + local_state_ = local_state; |
| 85 | + |
| 86 | + // Apply the local state pref if no user has logged in (still in login |
| 87 | + // screen). |
| 88 | + if (!user_manager::UserManager::Get()->GetActiveUser()) |
| 89 | + ApplyBluetoothLocalStatePref(); |
| 90 | +} |
| 91 | + |
| 92 | +void BluetoothPowerControllerImpl::ApplyBluetoothLocalStatePref() { |
| 93 | + if (local_state_->FindPreference(prefs::kSystemBluetoothAdapterEnabled) |
| 94 | + ->IsDefaultValue()) { |
| 95 | + // If the device has not had the local state Bluetooth pref, set the pref |
| 96 | + // according to whatever the current Bluetooth power is. |
| 97 | + BLUETOOTH_LOG(EVENT) << "Saving current power state of " |
| 98 | + << adapter_state_controller_->GetAdapterState() |
| 99 | + << " to local state."; |
| 100 | + SaveCurrentPowerStateToPrefs(local_state_, |
| 101 | + prefs::kSystemBluetoothAdapterEnabled); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + bool enabled = |
| 106 | + local_state_->GetBoolean(prefs::kSystemBluetoothAdapterEnabled); |
| 107 | + BLUETOOTH_LOG(EVENT) << "Applying local state pref Bluetooth power: " |
| 108 | + << enabled; |
| 109 | + SetAdapterState(enabled); |
| 110 | +} |
| 111 | + |
| 112 | +void BluetoothPowerControllerImpl::InitPrimaryUserPrefService( |
| 113 | + PrefService* primary_profile_prefs) { |
| 114 | + primary_profile_prefs_ = primary_profile_prefs; |
| 115 | + if (!primary_profile_prefs_) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + DCHECK_EQ(user_manager::UserManager::Get()->GetActiveUser(), |
| 120 | + user_manager::UserManager::Get()->GetPrimaryUser()); |
| 121 | + |
| 122 | + if (!has_attempted_apply_primary_user_pref_) { |
| 123 | + ApplyBluetoothPrimaryUserPref(); |
| 124 | + has_attempted_apply_primary_user_pref_ = true; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +void BluetoothPowerControllerImpl::ApplyBluetoothPrimaryUserPref() { |
| 129 | + absl::optional<user_manager::UserType> user_type = |
| 130 | + user_manager::UserManager::Get()->GetActiveUser()->GetType(); |
| 131 | + |
| 132 | + // Apply the Bluetooth pref only for regular users (i.e. users representing |
| 133 | + // a human individual). We don't want to apply Bluetooth pref for other users |
| 134 | + // e.g. kiosk, guest etc. For non-human users, Bluetooth power should be left |
| 135 | + // to the current power state. |
| 136 | + if (!user_type || !ShouldApplyUserBluetoothSetting(*user_type)) { |
| 137 | + BLUETOOTH_LOG(EVENT) << "Not applying primary user pref because user has " |
| 138 | + "no type or is not a regular user."; |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (!primary_profile_prefs_ |
| 143 | + ->FindPreference(prefs::kUserBluetoothAdapterEnabled) |
| 144 | + ->IsDefaultValue()) { |
| 145 | + bool enabled = |
| 146 | + primary_profile_prefs_->GetBoolean(prefs::kUserBluetoothAdapterEnabled); |
| 147 | + BLUETOOTH_LOG(EVENT) << "Applying primary user pref Bluetooth power: " |
| 148 | + << enabled; |
| 149 | + SetAdapterState(enabled); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + // If the user has not had the Bluetooth pref yet, set the user pref |
| 154 | + // according to whatever the current Bluetooth power is, except for |
| 155 | + // new users (first login on the device) always set the new pref to true. |
| 156 | + if (user_manager::UserManager::Get()->IsCurrentUserNew()) { |
| 157 | + BLUETOOTH_LOG(EVENT) << "Setting Bluetooth power to enabled for new user."; |
| 158 | + SetBluetoothEnabledState(true); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + BLUETOOTH_LOG(EVENT) << "Saving current power state of " |
| 163 | + << adapter_state_controller_->GetAdapterState() |
| 164 | + << " to user prefs."; |
| 165 | + SaveCurrentPowerStateToPrefs(primary_profile_prefs_, |
| 166 | + prefs::kUserBluetoothAdapterEnabled); |
| 167 | +} |
| 168 | + |
| 169 | +void BluetoothPowerControllerImpl::SetAdapterState(bool enabled) { |
| 170 | + BLUETOOTH_LOG(EVENT) << "Setting adapter state to " |
| 171 | + << (enabled ? "enabled " : "disabled"); |
| 172 | + adapter_state_controller_->SetBluetoothEnabledState(enabled); |
| 173 | +} |
| 174 | + |
| 175 | +void BluetoothPowerControllerImpl::SaveCurrentPowerStateToPrefs( |
| 176 | + PrefService* prefs, |
| 177 | + const char* pref_name) { |
| 178 | + prefs->SetBoolean(pref_name, |
| 179 | + IsBluetoothEnabledOrEnabling( |
| 180 | + adapter_state_controller_->GetAdapterState())); |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace bluetooth_config |
| 184 | +} // namespace chromeos |
0 commit comments