| 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 "chrome/browser/chromeos/audio/audio_devices_pref_handler_impl.h" | 5 #include "chrome/browser/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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chromeos/audio/audio_device.h" | 18 #include "chromeos/audio/audio_device.h" |
| 19 #include "chromeos/audio/cras_audio_handler.h" | |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 22 const double kDefaultOutputVolume = 75.0; |
| 23 const double kDefaultHDMIOutputVolume = 100.0; |
| 24 |
| 25 // Values used for muted preference. |
| 26 const int kPrefMuteOff = 0; |
| 27 const int kPrefMuteOn = 1; |
| 28 |
| 23 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { | 29 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { |
| 24 return device.device_name + " : " + | 30 return device.device_name + " : " + |
| 25 base::Uint64ToString(device.id & static_cast<uint64>(0xffffffff)); | 31 base::Uint64ToString(device.id & static_cast<uint64>(0xffffffff)); |
| 26 } | 32 } |
| 27 | 33 |
| 28 } | 34 } // namespace |
| 29 | 35 |
| 30 namespace chromeos { | 36 namespace chromeos { |
| 31 | 37 |
| 32 double AudioDevicesPrefHandlerImpl::GetVolumeGainValue( | 38 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue( |
| 33 const AudioDevice& device) { | 39 const AudioDevice* device) { |
| 34 UpdateDevicesVolumePref(); | 40 if (!device) |
| 41 return kDefaultOutputVolume; |
| 42 else |
| 43 return GetVolumeGainPrefValue(*device); |
| 44 } |
| 35 | 45 |
| 36 std::string device_id_str = GetDeviceIdString(device); | 46 double AudioDevicesPrefHandlerImpl::GetInputGainValue( |
| 37 if (!device_volume_settings_->HasKey(device_id_str)) | 47 const AudioDevice* device) { |
| 38 MigrateDeviceVolumeSettings(device_id_str); | 48 DCHECK(device); |
| 39 | 49 return GetVolumeGainPrefValue(*device); |
| 40 double volume = kDefaultVolumeGainPercent; | |
| 41 device_volume_settings_->GetDouble(device_id_str, &volume); | |
| 42 | |
| 43 return volume; | |
| 44 } | 50 } |
| 45 | 51 |
| 46 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( | 52 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( |
| 47 const AudioDevice& device, double value) { | 53 const AudioDevice& device, double value) { |
| 48 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); | 54 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); |
| 49 | 55 |
| 50 SaveDevicesVolumePref(); | 56 SaveDevicesVolumePref(); |
| 51 } | 57 } |
| 52 | 58 |
| 53 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { | 59 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 82 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | 88 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( |
| 83 AudioPrefObserver* observer) { | 89 AudioPrefObserver* observer) { |
| 84 observers_.AddObserver(observer); | 90 observers_.AddObserver(observer); |
| 85 } | 91 } |
| 86 | 92 |
| 87 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | 93 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( |
| 88 AudioPrefObserver* observer) { | 94 AudioPrefObserver* observer) { |
| 89 observers_.RemoveObserver(observer); | 95 observers_.RemoveObserver(observer); |
| 90 } | 96 } |
| 91 | 97 |
| 98 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue( |
| 99 const AudioDevice& device) { |
| 100 UpdateDevicesVolumePref(); |
| 101 |
| 102 std::string device_id_str = GetDeviceIdString(device); |
| 103 if (!device_volume_settings_->HasKey(device_id_str)) |
| 104 MigrateDeviceVolumeSettings(device_id_str); |
| 105 |
| 106 // TODO(jennyz, rkc): Return a meaningful input gain default value, when |
| 107 // cras has added support for normalizing input gain range. |
| 108 double value = device.is_input ? |
| 109 0.0 : GetDeviceDefaultOutputVolume(device); |
| 110 device_volume_settings_->GetDouble(device_id_str, &value); |
| 111 |
| 112 return value; |
| 113 } |
| 114 |
| 115 double AudioDevicesPrefHandlerImpl::GetDeviceDefaultOutputVolume( |
| 116 const AudioDevice& device) { |
| 117 if (device.type == AUDIO_TYPE_HDMI) |
| 118 return kDefaultHDMIOutputVolume; |
| 119 else |
| 120 return kDefaultOutputVolume; |
| 121 } |
| 122 |
| 92 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( | 123 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( |
| 93 PrefService* local_state) | 124 PrefService* local_state) |
| 94 : device_mute_settings_(new base::DictionaryValue()), | 125 : device_mute_settings_(new base::DictionaryValue()), |
| 95 device_volume_settings_(new base::DictionaryValue()), | 126 device_volume_settings_(new base::DictionaryValue()), |
| 96 local_state_(local_state) { | 127 local_state_(local_state) { |
| 97 InitializePrefObservers(); | 128 InitializePrefObservers(); |
| 98 | 129 |
| 99 UpdateDevicesMutePref(); | 130 UpdateDevicesMutePref(); |
| 100 UpdateDevicesVolumePref(); | 131 UpdateDevicesVolumePref(); |
| 101 } | 132 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); | 166 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); |
| 136 if (volume_prefs) | 167 if (volume_prefs) |
| 137 device_volume_settings_.reset(volume_prefs->DeepCopy()); | 168 device_volume_settings_.reset(volume_prefs->DeepCopy()); |
| 138 } | 169 } |
| 139 | 170 |
| 140 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { | 171 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { |
| 141 DictionaryPrefUpdate dict_update(local_state_, | 172 DictionaryPrefUpdate dict_update(local_state_, |
| 142 prefs::kAudioDevicesVolumePercent); | 173 prefs::kAudioDevicesVolumePercent); |
| 143 base::DictionaryValue::Iterator it(*device_volume_settings_); | 174 base::DictionaryValue::Iterator it(*device_volume_settings_); |
| 144 while (!it.IsAtEnd()) { | 175 while (!it.IsAtEnd()) { |
| 145 double volume = kDefaultVolumeGainPercent; | 176 double volume = kDefaultOutputVolume; |
| 146 it.value().GetAsDouble(&volume); | 177 bool success = it.value().GetAsDouble(&volume); |
| 178 DCHECK(success); |
| 147 dict_update->SetDouble(it.key(), volume); | 179 dict_update->SetDouble(it.key(), volume); |
| 148 it.Advance(); | 180 it.Advance(); |
| 149 } | 181 } |
| 150 } | 182 } |
| 151 | 183 |
| 152 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | 184 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
| 153 std::string active_device) { | 185 std::string active_device) { |
| 154 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | 186 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
| 155 device_mute_settings_->SetInteger(active_device, old_mute); | 187 device_mute_settings_->SetInteger(active_device, old_mute); |
| 156 SaveDevicesMutePref(); | 188 SaveDevicesMutePref(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 175 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | 207 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); |
| 176 | 208 |
| 177 // Register the prefs backing the audio muting policies. | 209 // Register the prefs backing the audio muting policies. |
| 178 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); | 210 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); |
| 179 // This pref has moved to the media subsystem but we should verify it is there | 211 // This pref has moved to the media subsystem but we should verify it is there |
| 180 // before we use it. | 212 // before we use it. |
| 181 registry->RegisterBooleanPref(::prefs::kAudioCaptureAllowed, true); | 213 registry->RegisterBooleanPref(::prefs::kAudioCaptureAllowed, true); |
| 182 | 214 |
| 183 // Register the legacy audio prefs for migration. | 215 // Register the legacy audio prefs for migration. |
| 184 registry->RegisterDoublePref(prefs::kAudioVolumePercent, | 216 registry->RegisterDoublePref(prefs::kAudioVolumePercent, |
| 185 kDefaultVolumeGainPercent); | 217 kDefaultOutputVolume); |
| 186 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); | 218 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); |
| 187 } | 219 } |
| 188 | 220 |
| 189 // static | 221 // static |
| 190 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( | 222 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( |
| 191 PrefService* local_state) { | 223 PrefService* local_state) { |
| 192 return new AudioDevicesPrefHandlerImpl(local_state); | 224 return new AudioDevicesPrefHandlerImpl(local_state); |
| 193 } | 225 } |
| 194 | 226 |
| 195 } // namespace chromeos | 227 } // namespace chromeos |
| OLD | NEW |