| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/audio/audio_devices_pref_handler_impl.h" | 5 #include "chromeos/audio/audio_devices_pref_handler_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 // Values used for muted preference. | 23 // Values used for muted preference. |
| 24 const int kPrefMuteOff = 0; | 24 const int kPrefMuteOff = 0; |
| 25 const int kPrefMuteOn = 1; | 25 const int kPrefMuteOn = 1; |
| 26 | 26 |
| 27 // Prefs keys. | 27 // Prefs keys. |
| 28 const char kActiveKey[] = "active"; | 28 const char kActiveKey[] = "active"; |
| 29 const char kActivateByUserKey[] = "activate_by_user"; | 29 const char kActivateByUserKey[] = "activate_by_user"; |
| 30 | 30 |
| 31 // Gets the device id string for storing audio preference. The format of | 31 // Gets the device id string for storing audio preference. The format of |
| 32 // device string is a string consisting of 2 parts. | 32 // device string is a string consisting of 3 parts: |
| 33 // |version of stable device ID| : |
| 33 // |integer from lower 32 bit of device id| : | 34 // |integer from lower 32 bit of device id| : |
| 34 // |0(output device) or 1(input device)| | 35 // |0(output device) or 1(input device)| |
| 35 // If an audio device has both integrated input and output devices, the first 2 | 36 // If an audio device has both integrated input and output devices, the first 2 |
| 36 // parts of the string could be identical, only the last part will differentiate | 37 // parts of the string could be identical, only the last part will differentiate |
| 37 // them. | 38 // them. |
| 38 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { | 39 // Note that |version of stable device ID| is present only for devices with |
| 40 // stable device ID version >= 2. For devices with version 1, the device id |
| 41 // string contains only latter 2 parts - in order to preserve backward |
| 42 // compatibility with existing ID from before v2 stable device ID was |
| 43 // introduced. |
| 44 std::string GetVersionedDeviceIdString(const chromeos::AudioDevice& device, |
| 45 int version) { |
| 46 CHECK(device.stable_device_id_version >= version); |
| 47 DCHECK_GE(device.stable_device_id_version, 1); |
| 48 DCHECK_LE(device.stable_device_id_version, 2); |
| 49 |
| 50 bool use_deprecated_id = version == 1 && device.stable_device_id_version == 2; |
| 51 uint64_t stable_device_id = use_deprecated_id |
| 52 ? device.deprecated_stable_device_id |
| 53 : device.stable_device_id; |
| 54 std::string version_prefix = version == 2 ? "2 : " : ""; |
| 39 std::string device_id_string = | 55 std::string device_id_string = |
| 40 base::Uint64ToString(device.stable_device_id & | 56 version_prefix + base::Uint64ToString(stable_device_id & |
| 41 static_cast<uint64_t>(0xffffffff)) + | 57 static_cast<uint64_t>(0xffffffff)) + |
| 42 " : " + (device.is_input ? "1" : "0"); | 58 " : " + (device.is_input ? "1" : "0"); |
| 43 // Replace any periods from the device id string with a space, since setting | 59 // Replace any periods from the device id string with a space, since setting |
| 44 // names cannot contain periods. | 60 // names cannot contain periods. |
| 45 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); | 61 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); |
| 46 return device_id_string; | 62 return device_id_string; |
| 47 } | 63 } |
| 48 | 64 |
| 65 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { |
| 66 return GetVersionedDeviceIdString(device, device.stable_device_id_version); |
| 67 } |
| 68 |
| 69 // Migrates an entry associated with |device|'s v1 stable device ID in |
| 70 // |settings| to the key derived from |device|'s v2 stable device ID |
| 71 // (which is expected to be equal to |intended_key|), if the entry can |
| 72 // be found. |
| 73 // Returns whether the migration occurred. |
| 74 bool MigrateDeviceIdInSettings(base::DictionaryValue* settings, |
| 75 const std::string& intended_key, |
| 76 const chromeos::AudioDevice& device) { |
| 77 if (device.stable_device_id_version == 1) |
| 78 return false; |
| 79 |
| 80 DCHECK_EQ(2, device.stable_device_id_version); |
| 81 |
| 82 std::string old_device_id = GetVersionedDeviceIdString(device, 1); |
| 83 std::unique_ptr<base::Value> value; |
| 84 if (!settings->Remove(old_device_id, &value)) |
| 85 return false; |
| 86 |
| 87 DCHECK_EQ(intended_key, GetDeviceIdString(device)); |
| 88 settings->Set(intended_key, std::move(value)); |
| 89 return true; |
| 90 } |
| 91 |
| 49 } // namespace | 92 } // namespace |
| 50 | 93 |
| 51 namespace chromeos { | 94 namespace chromeos { |
| 52 | 95 |
| 53 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue( | 96 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue( |
| 54 const AudioDevice* device) { | 97 const AudioDevice* device) { |
| 55 if (!device) | 98 if (!device) |
| 56 return kDefaultOutputVolumePercent; | 99 return kDefaultOutputVolumePercent; |
| 57 else | 100 else |
| 58 return GetVolumeGainPrefValue(*device); | 101 return GetVolumeGainPrefValue(*device); |
| 59 } | 102 } |
| 60 | 103 |
| 61 double AudioDevicesPrefHandlerImpl::GetInputGainValue( | 104 double AudioDevicesPrefHandlerImpl::GetInputGainValue( |
| 62 const AudioDevice* device) { | 105 const AudioDevice* device) { |
| 63 DCHECK(device); | 106 DCHECK(device); |
| 64 return GetVolumeGainPrefValue(*device); | 107 return GetVolumeGainPrefValue(*device); |
| 65 } | 108 } |
| 66 | 109 |
| 67 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( | 110 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( |
| 68 const AudioDevice& device, double value) { | 111 const AudioDevice& device, double value) { |
| 112 // Use this opportunity to remove device record under deprecated device ID, |
| 113 // if one exists. |
| 114 if (device.stable_device_id_version == 2) { |
| 115 std::string old_device_id = GetVersionedDeviceIdString(device, 1); |
| 116 device_volume_settings_->Remove(old_device_id, nullptr); |
| 117 } |
| 69 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); | 118 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); |
| 70 | 119 |
| 71 SaveDevicesVolumePref(); | 120 SaveDevicesVolumePref(); |
| 72 } | 121 } |
| 73 | 122 |
| 74 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { | 123 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { |
| 75 std::string device_id_str = GetDeviceIdString(device); | 124 std::string device_id_str = GetDeviceIdString(device); |
| 76 if (!device_mute_settings_->HasKey(device_id_str)) | 125 if (!device_mute_settings_->HasKey(device_id_str)) |
| 77 MigrateDeviceMuteSettings(device_id_str); | 126 MigrateDeviceMuteSettings(device_id_str, device); |
| 78 | 127 |
| 79 int mute = kPrefMuteOff; | 128 int mute = kPrefMuteOff; |
| 80 device_mute_settings_->GetInteger(device_id_str, &mute); | 129 device_mute_settings_->GetInteger(device_id_str, &mute); |
| 81 | 130 |
| 82 return (mute == kPrefMuteOn); | 131 return (mute == kPrefMuteOn); |
| 83 } | 132 } |
| 84 | 133 |
| 85 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, | 134 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, |
| 86 bool mute) { | 135 bool mute) { |
| 136 // Use this opportunity to remove device record under deprecated device ID, |
| 137 // if one exists. |
| 138 if (device.stable_device_id_version == 2) { |
| 139 std::string old_device_id = GetVersionedDeviceIdString(device, 1); |
| 140 device_mute_settings_->Remove(old_device_id, nullptr); |
| 141 } |
| 87 device_mute_settings_->SetInteger(GetDeviceIdString(device), | 142 device_mute_settings_->SetInteger(GetDeviceIdString(device), |
| 88 mute ? kPrefMuteOn : kPrefMuteOff); | 143 mute ? kPrefMuteOn : kPrefMuteOff); |
| 89 SaveDevicesMutePref(); | 144 SaveDevicesMutePref(); |
| 90 } | 145 } |
| 91 | 146 |
| 92 void AudioDevicesPrefHandlerImpl::SetDeviceActive(const AudioDevice& device, | 147 void AudioDevicesPrefHandlerImpl::SetDeviceActive(const AudioDevice& device, |
| 93 bool active, | 148 bool active, |
| 94 bool activate_by_user) { | 149 bool activate_by_user) { |
| 95 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 150 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 96 dict->SetBoolean(kActiveKey, active); | 151 dict->SetBoolean(kActiveKey, active); |
| 97 if (active) | 152 if (active) |
| 98 dict->SetBoolean(kActivateByUserKey, activate_by_user); | 153 dict->SetBoolean(kActivateByUserKey, activate_by_user); |
| 99 | 154 |
| 155 // Use this opportunity to remove device record under deprecated device ID, |
| 156 // if one exists. |
| 157 if (device.stable_device_id_version == 2) { |
| 158 std::string old_device_id = GetVersionedDeviceIdString(device, 1); |
| 159 device_state_settings_->Remove(old_device_id, nullptr); |
| 160 } |
| 100 device_state_settings_->Set(GetDeviceIdString(device), std::move(dict)); | 161 device_state_settings_->Set(GetDeviceIdString(device), std::move(dict)); |
| 101 SaveDevicesStatePref(); | 162 SaveDevicesStatePref(); |
| 102 } | 163 } |
| 103 | 164 |
| 104 bool AudioDevicesPrefHandlerImpl::GetDeviceActive(const AudioDevice& device, | 165 bool AudioDevicesPrefHandlerImpl::GetDeviceActive(const AudioDevice& device, |
| 105 bool* active, | 166 bool* active, |
| 106 bool* activate_by_user) { | 167 bool* activate_by_user) { |
| 107 const std::string device_id_str = GetDeviceIdString(device); | 168 const std::string device_id_str = GetDeviceIdString(device); |
| 108 if (!device_state_settings_->HasKey(device_id_str)) | 169 if (!device_state_settings_->HasKey(device_id_str) && |
| 170 !MigrateDevicesStatePref(device_id_str, device)) { |
| 109 return false; | 171 return false; |
| 172 } |
| 110 | 173 |
| 111 base::DictionaryValue* dict = NULL; | 174 base::DictionaryValue* dict = NULL; |
| 112 if (!device_state_settings_->GetDictionary(device_id_str, &dict)) { | 175 if (!device_state_settings_->GetDictionary(device_id_str, &dict)) { |
| 113 LOG(ERROR) << "Could not get device state for device:" << device.ToString(); | 176 LOG(ERROR) << "Could not get device state for device:" << device.ToString(); |
| 114 return false; | 177 return false; |
| 115 } | 178 } |
| 116 if (!dict->GetBoolean(kActiveKey, active)) { | 179 if (!dict->GetBoolean(kActiveKey, active)) { |
| 117 LOG(ERROR) << "Could not get active value for device:" << device.ToString(); | 180 LOG(ERROR) << "Could not get active value for device:" << device.ToString(); |
| 118 return false; | 181 return false; |
| 119 } | 182 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 139 | 202 |
| 140 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | 203 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( |
| 141 AudioPrefObserver* observer) { | 204 AudioPrefObserver* observer) { |
| 142 observers_.RemoveObserver(observer); | 205 observers_.RemoveObserver(observer); |
| 143 } | 206 } |
| 144 | 207 |
| 145 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue( | 208 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue( |
| 146 const AudioDevice& device) { | 209 const AudioDevice& device) { |
| 147 std::string device_id_str = GetDeviceIdString(device); | 210 std::string device_id_str = GetDeviceIdString(device); |
| 148 if (!device_volume_settings_->HasKey(device_id_str)) | 211 if (!device_volume_settings_->HasKey(device_id_str)) |
| 149 MigrateDeviceVolumeSettings(device_id_str); | 212 MigrateDeviceVolumeGainSettings(device_id_str, device); |
| 150 | 213 |
| 151 // TODO(jennyz, rkc): Return a meaningful input gain default value, when | 214 // TODO(jennyz, rkc): Return a meaningful input gain default value, when |
| 152 // cras has added support for normalizing input gain range. | 215 // cras has added support for normalizing input gain range. |
| 153 double value = device.is_input ? | 216 double value = device.is_input ? |
| 154 0.0 : GetDeviceDefaultOutputVolume(device); | 217 0.0 : GetDeviceDefaultOutputVolume(device); |
| 155 // TODO(rkc): The above code is completely ignored since we 'always' have a | 218 // TODO(rkc): The above code is completely ignored since we 'always' have a |
| 156 // default pref value. Fix this. http://crbug.com/442489 | 219 // default pref value. Fix this. http://crbug.com/442489 |
| 157 device_volume_settings_->GetDouble(device_id_str, &value); | 220 device_volume_settings_->GetDouble(device_id_str, &value); |
| 158 | 221 |
| 159 return value; | 222 return value; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 | 256 |
| 194 void AudioDevicesPrefHandlerImpl::LoadDevicesMutePref() { | 257 void AudioDevicesPrefHandlerImpl::LoadDevicesMutePref() { |
| 195 const base::DictionaryValue* mute_prefs = | 258 const base::DictionaryValue* mute_prefs = |
| 196 local_state_->GetDictionary(prefs::kAudioDevicesMute); | 259 local_state_->GetDictionary(prefs::kAudioDevicesMute); |
| 197 if (mute_prefs) | 260 if (mute_prefs) |
| 198 device_mute_settings_.reset(mute_prefs->DeepCopy()); | 261 device_mute_settings_.reset(mute_prefs->DeepCopy()); |
| 199 } | 262 } |
| 200 | 263 |
| 201 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { | 264 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { |
| 202 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); | 265 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); |
| 266 dict_update->Clear(); |
| 203 dict_update->MergeDictionary(device_mute_settings_.get()); | 267 dict_update->MergeDictionary(device_mute_settings_.get()); |
| 204 } | 268 } |
| 205 | 269 |
| 206 void AudioDevicesPrefHandlerImpl::LoadDevicesVolumePref() { | 270 void AudioDevicesPrefHandlerImpl::LoadDevicesVolumePref() { |
| 207 const base::DictionaryValue* volume_prefs = | 271 const base::DictionaryValue* volume_prefs = |
| 208 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); | 272 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); |
| 209 if (volume_prefs) | 273 if (volume_prefs) |
| 210 device_volume_settings_.reset(volume_prefs->DeepCopy()); | 274 device_volume_settings_.reset(volume_prefs->DeepCopy()); |
| 211 } | 275 } |
| 212 | 276 |
| 213 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { | 277 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { |
| 214 DictionaryPrefUpdate dict_update(local_state_, | 278 DictionaryPrefUpdate dict_update(local_state_, |
| 215 prefs::kAudioDevicesVolumePercent); | 279 prefs::kAudioDevicesVolumePercent); |
| 280 dict_update->Clear(); |
| 216 dict_update->MergeDictionary(device_volume_settings_.get()); | 281 dict_update->MergeDictionary(device_volume_settings_.get()); |
| 217 } | 282 } |
| 218 | 283 |
| 219 void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() { | 284 void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() { |
| 220 const base::DictionaryValue* state_prefs = | 285 const base::DictionaryValue* state_prefs = |
| 221 local_state_->GetDictionary(prefs::kAudioDevicesState); | 286 local_state_->GetDictionary(prefs::kAudioDevicesState); |
| 222 if (state_prefs) | 287 if (state_prefs) |
| 223 device_state_settings_.reset(state_prefs->DeepCopy()); | 288 device_state_settings_.reset(state_prefs->DeepCopy()); |
| 224 } | 289 } |
| 225 | 290 |
| 226 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() { | 291 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() { |
| 227 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesState); | 292 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesState); |
| 228 base::DictionaryValue::Iterator it(*device_state_settings_); | 293 dict_update->Clear(); |
| 229 dict_update->MergeDictionary(device_state_settings_.get()); | 294 dict_update->MergeDictionary(device_state_settings_.get()); |
| 230 } | 295 } |
| 231 | 296 |
| 297 bool AudioDevicesPrefHandlerImpl::MigrateDevicesStatePref( |
| 298 const std::string& device_key, |
| 299 const AudioDevice& device) { |
| 300 if (!MigrateDeviceIdInSettings(device_state_settings_.get(), device_key, |
| 301 device)) { |
| 302 return false; |
| 303 } |
| 304 |
| 305 SaveDevicesStatePref(); |
| 306 return true; |
| 307 } |
| 308 |
| 232 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | 309 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
| 233 const std::string& active_device) { | 310 const std::string& device_key, |
| 234 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | 311 const AudioDevice& device) { |
| 235 device_mute_settings_->SetInteger(active_device, old_mute); | 312 if (!MigrateDeviceIdInSettings(device_mute_settings_.get(), device_key, |
| 313 device)) { |
| 314 // If there was no recorded value for deprecated device ID, use value from |
| 315 // global mute pref. |
| 316 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
| 317 device_mute_settings_->SetInteger(device_key, old_mute); |
| 318 } |
| 236 SaveDevicesMutePref(); | 319 SaveDevicesMutePref(); |
| 237 } | 320 } |
| 238 | 321 |
| 239 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( | 322 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeGainSettings( |
| 240 const std::string& active_device) { | 323 const std::string& device_key, |
| 241 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); | 324 const AudioDevice& device) { |
| 242 device_volume_settings_->SetDouble(active_device, old_volume); | 325 if (!MigrateDeviceIdInSettings(device_volume_settings_.get(), device_key, |
| 326 device)) { |
| 327 // If there was no recorded value for deprecated device ID, use value from |
| 328 // global vloume pref. |
| 329 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); |
| 330 device_volume_settings_->SetDouble(device_key, old_volume); |
| 331 } |
| 243 SaveDevicesVolumePref(); | 332 SaveDevicesVolumePref(); |
| 244 } | 333 } |
| 245 | 334 |
| 246 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { | 335 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { |
| 247 for (auto& observer : observers_) | 336 for (auto& observer : observers_) |
| 248 observer.OnAudioPolicyPrefChanged(); | 337 observer.OnAudioPolicyPrefChanged(); |
| 249 } | 338 } |
| 250 | 339 |
| 251 // static | 340 // static |
| 252 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { | 341 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
| 253 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); | 342 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); |
| 254 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | 343 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); |
| 255 registry->RegisterDictionaryPref(prefs::kAudioDevicesState); | 344 registry->RegisterDictionaryPref(prefs::kAudioDevicesState); |
| 256 | 345 |
| 257 // Register the prefs backing the audio muting policies. | 346 // Register the prefs backing the audio muting policies. |
| 258 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome | 347 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome |
| 259 // media system. | 348 // media system. |
| 260 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); | 349 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); |
| 261 | 350 |
| 262 // Register the legacy audio prefs for migration. | 351 // Register the legacy audio prefs for migration. |
| 263 registry->RegisterDoublePref(prefs::kAudioVolumePercent, | 352 registry->RegisterDoublePref(prefs::kAudioVolumePercent, |
| 264 kDefaultOutputVolumePercent); | 353 kDefaultOutputVolumePercent); |
| 265 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); | 354 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); |
| 266 } | 355 } |
| 267 | 356 |
| 268 } // namespace chromeos | 357 } // namespace chromeos |
| OLD | NEW |