Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 // Gets the device id string for storing audio preference. The format of | 25 // Gets the device id string for storing audio preference. The format of |
| 26 // device string is a string consisting of 3 parts. | 26 // device string is a string consisting of 3 parts. |
| 27 // |device_name| : |integer from lower 32 bit of device id| : | 27 // |device_name| : |integer from lower 32 bit of device id| : |
| 28 // |0(output device) or 1(input device)| | 28 // |0(output device) or 1(input device)| |
| 29 // If an audio device has both integrated input and output devices, the first 2 | 29 // If an audio device has both integrated input and output devices, the first 2 |
| 30 // parts of the string could be identical, only the last part will differentiate | 30 // parts of the string could be identical, only the last part will differentiate |
| 31 // them. | 31 // them. |
| 32 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { | 32 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { |
| 33 std::string device_id_string = | 33 std::string device_id_string = |
| 34 device.device_name + " : " + | 34 base::Uint64ToString(device.stable_device_id & |
| 35 base::Uint64ToString(device.id & static_cast<uint64>(0xffffffff)) + | 35 static_cast<uint64>(0xffffffff)) + |
| 36 " : " + (device.is_input ? "1" : "0"); | 36 " : " + (device.is_input ? "1" : "0"); |
| 37 // Replace any periods from the device id string with a space, since setting | 37 // Replace any periods from the device id string with a space, since setting |
| 38 // names cannot contain periods. | 38 // names cannot contain periods. |
| 39 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); | 39 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); |
| 40 return device_id_string; | 40 return device_id_string; |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 namespace chromeos { | 45 namespace chromeos { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 return (mute == kPrefMuteOn); | 78 return (mute == kPrefMuteOn); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, | 81 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, |
| 82 bool mute) { | 82 bool mute) { |
| 83 device_mute_settings_->SetInteger(GetDeviceIdString(device), | 83 device_mute_settings_->SetInteger(GetDeviceIdString(device), |
| 84 mute ? kPrefMuteOn : kPrefMuteOff); | 84 mute ? kPrefMuteOn : kPrefMuteOff); |
| 85 SaveDevicesMutePref(); | 85 SaveDevicesMutePref(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 AudioDeviceState AudioDevicesPrefHandlerImpl::GetDeviceState( | |
| 89 const AudioDevice& device) { | |
| 90 UpdateDevicesStatePref(); | |
| 91 std::string device_id_str = GetDeviceIdString(device); | |
| 92 if (!device_state_settings_->HasKey(device_id_str)) { | |
| 93 device_state_settings_->SetInteger(device_id_str, | |
| 94 (int)AUDIO_STATE_NOT_AVAILABLE); | |
|
jennyz
2015/12/17 22:50:21
I guess this is what you mean in the .h comment fo
hychao
2015/12/29 10:57:25
Updated the header file.
| |
| 95 SaveDevicesStatePref(); | |
| 96 } | |
| 97 int state = (int)AUDIO_STATE_NOT_AVAILABLE; | |
| 98 device_state_settings_->GetInteger(device_id_str, &state); | |
| 99 return (AudioDeviceState)state; | |
| 100 } | |
| 101 | |
| 102 void AudioDevicesPrefHandlerImpl::SetDeviceState(const AudioDevice& device, | |
| 103 AudioDeviceState state) { | |
| 104 device_state_settings_->SetInteger(GetDeviceIdString(device), | |
| 105 (int)state); | |
| 106 SaveDevicesStatePref(); | |
| 107 } | |
| 108 | |
| 88 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { | 109 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { |
| 89 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); | 110 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); |
| 90 } | 111 } |
| 91 | 112 |
| 92 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | 113 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( |
| 93 AudioPrefObserver* observer) { | 114 AudioPrefObserver* observer) { |
| 94 observers_.AddObserver(observer); | 115 observers_.AddObserver(observer); |
| 95 } | 116 } |
| 96 | 117 |
| 97 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | 118 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 123 if (device.type == AUDIO_TYPE_HDMI) | 144 if (device.type == AUDIO_TYPE_HDMI) |
| 124 return kDefaultHdmiOutputVolumePercent; | 145 return kDefaultHdmiOutputVolumePercent; |
| 125 else | 146 else |
| 126 return kDefaultOutputVolumePercent; | 147 return kDefaultOutputVolumePercent; |
| 127 } | 148 } |
| 128 | 149 |
| 129 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( | 150 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( |
| 130 PrefService* local_state) | 151 PrefService* local_state) |
| 131 : device_mute_settings_(new base::DictionaryValue()), | 152 : device_mute_settings_(new base::DictionaryValue()), |
| 132 device_volume_settings_(new base::DictionaryValue()), | 153 device_volume_settings_(new base::DictionaryValue()), |
| 154 device_state_settings_(new base::DictionaryValue()), | |
| 133 local_state_(local_state) { | 155 local_state_(local_state) { |
| 134 InitializePrefObservers(); | 156 InitializePrefObservers(); |
| 135 | 157 |
| 136 UpdateDevicesMutePref(); | 158 UpdateDevicesMutePref(); |
| 137 UpdateDevicesVolumePref(); | 159 UpdateDevicesVolumePref(); |
| 138 } | 160 } |
| 139 | 161 |
| 140 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { | 162 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { |
| 141 } | 163 } |
| 142 | 164 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 base::DictionaryValue::Iterator it(*device_volume_settings_); | 201 base::DictionaryValue::Iterator it(*device_volume_settings_); |
| 180 while (!it.IsAtEnd()) { | 202 while (!it.IsAtEnd()) { |
| 181 double volume = kDefaultOutputVolumePercent; | 203 double volume = kDefaultOutputVolumePercent; |
| 182 bool success = it.value().GetAsDouble(&volume); | 204 bool success = it.value().GetAsDouble(&volume); |
| 183 DCHECK(success); | 205 DCHECK(success); |
| 184 dict_update->SetDouble(it.key(), volume); | 206 dict_update->SetDouble(it.key(), volume); |
| 185 it.Advance(); | 207 it.Advance(); |
| 186 } | 208 } |
| 187 } | 209 } |
| 188 | 210 |
| 211 void AudioDevicesPrefHandlerImpl::UpdateDevicesStatePref() { | |
| 212 const base::DictionaryValue* state_prefs = | |
| 213 local_state_->GetDictionary(prefs::kAudioDevicesState); | |
| 214 if (state_prefs) | |
| 215 device_state_settings_.reset(state_prefs->DeepCopy()); | |
| 216 } | |
| 217 | |
| 218 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() { | |
| 219 DictionaryPrefUpdate dict_update(local_state_, | |
| 220 prefs::kAudioDevicesState); | |
| 221 base::DictionaryValue::Iterator it(*device_state_settings_); | |
| 222 while (!it.IsAtEnd()) { | |
| 223 int state = AUDIO_STATE_NOT_AVAILABLE; | |
| 224 bool success = it.value().GetAsInteger(&state); | |
| 225 DCHECK(success); | |
| 226 dict_update->SetInteger(it.key(), state); | |
| 227 it.Advance(); | |
| 228 } | |
| 229 } | |
| 230 | |
| 189 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | 231 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
| 190 const std::string& active_device) { | 232 const std::string& active_device) { |
| 191 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | 233 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
| 192 device_mute_settings_->SetInteger(active_device, old_mute); | 234 device_mute_settings_->SetInteger(active_device, old_mute); |
| 193 SaveDevicesMutePref(); | 235 SaveDevicesMutePref(); |
| 194 } | 236 } |
| 195 | 237 |
| 196 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( | 238 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( |
| 197 const std::string& active_device) { | 239 const std::string& active_device) { |
| 198 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); | 240 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); |
| 199 device_volume_settings_->SetDouble(active_device, old_volume); | 241 device_volume_settings_->SetDouble(active_device, old_volume); |
| 200 SaveDevicesVolumePref(); | 242 SaveDevicesVolumePref(); |
| 201 } | 243 } |
| 202 | 244 |
| 203 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { | 245 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { |
| 204 FOR_EACH_OBSERVER(AudioPrefObserver, | 246 FOR_EACH_OBSERVER(AudioPrefObserver, |
| 205 observers_, | 247 observers_, |
| 206 OnAudioPolicyPrefChanged()); | 248 OnAudioPolicyPrefChanged()); |
| 207 } | 249 } |
| 208 | 250 |
| 209 // static | 251 // static |
| 210 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { | 252 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
| 211 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); | 253 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); |
| 212 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | 254 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); |
| 255 registry->RegisterDictionaryPref(prefs::kAudioDevicesState); | |
| 213 | 256 |
| 214 // Register the prefs backing the audio muting policies. | 257 // Register the prefs backing the audio muting policies. |
| 215 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome | 258 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome |
| 216 // media system. | 259 // media system. |
| 217 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); | 260 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); |
| 218 | 261 |
| 219 // Register the legacy audio prefs for migration. | 262 // Register the legacy audio prefs for migration. |
| 220 registry->RegisterDoublePref(prefs::kAudioVolumePercent, | 263 registry->RegisterDoublePref(prefs::kAudioVolumePercent, |
| 221 kDefaultOutputVolumePercent); | 264 kDefaultOutputVolumePercent); |
| 222 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); | 265 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); |
| 223 } | 266 } |
| 224 | 267 |
| 225 } // namespace chromeos | 268 } // namespace chromeos |
| OLD | NEW |