| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/audio/audio_devices_pref_handler_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/pref_names.h" |
| 16 #include "chromeos/audio/cras_audio_handler.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Default value for the volume pref, as a percent in the range [0.0, 100.0]. |
| 23 const double kDefaultVolumePercent = 75.0; |
| 24 |
| 25 // Values used for muted preference. |
| 26 const int kPrefMuteOff = 0; |
| 27 const int kPrefMuteOn = 1; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue() { |
| 32 if (!CrasAudioHandler::IsInitialized()) |
| 33 return kDefaultVolumePercent; |
| 34 |
| 35 UpdateDevicesVolumePref(); |
| 36 std::string active_device_id = base::Uint64ToString( |
| 37 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 38 if (!device_volume_settings_->HasKey(active_device_id)) |
| 39 MigrateDeviceVolumeSettings(active_device_id); |
| 40 double volume = kDefaultVolumePercent; |
| 41 device_volume_settings_->GetDouble(active_device_id, &volume); |
| 42 return volume; |
| 43 } |
| 44 |
| 45 void AudioDevicesPrefHandlerImpl::SetOutputVolumeValue(double volume_percent) { |
| 46 std::string active_device_id = base::Uint64ToString( |
| 47 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 48 if (volume_percent > 100.0) |
| 49 volume_percent = 100.0; |
| 50 if (volume_percent < 0.0) |
| 51 volume_percent = 0.0; |
| 52 device_volume_settings_->SetDouble(active_device_id, volume_percent); |
| 53 SaveDevicesVolumePref(); |
| 54 } |
| 55 |
| 56 bool AudioDevicesPrefHandlerImpl::GetOutputMuteValue() { |
| 57 if (!CrasAudioHandler::IsInitialized()) |
| 58 return false; |
| 59 |
| 60 UpdateDevicesVolumePref(); |
| 61 std::string active_device_id = base::Uint64ToString( |
| 62 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 63 if (!device_mute_settings_->HasKey(active_device_id)) |
| 64 MigrateDeviceMuteSettings(active_device_id); |
| 65 int mute = kPrefMuteOff; |
| 66 device_mute_settings_->GetInteger(active_device_id, &mute); |
| 67 return (mute == kPrefMuteOn); |
| 68 } |
| 69 |
| 70 void AudioDevicesPrefHandlerImpl::SetOutputMuteValue(bool mute) { |
| 71 std::string active_device_id = base::Uint64ToString( |
| 72 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 73 device_mute_settings_->SetBoolean(active_device_id, |
| 74 mute ? kPrefMuteOn : kPrefMuteOff); |
| 75 SaveDevicesVolumePref(); |
| 76 } |
| 77 |
| 78 bool AudioDevicesPrefHandlerImpl::GetAudioCaptureAllowedValue() { |
| 79 return local_state_->GetBoolean(prefs::kAudioCaptureAllowed); |
| 80 } |
| 81 |
| 82 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { |
| 83 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); |
| 84 } |
| 85 |
| 86 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( |
| 87 AudioPrefObserver* observer) { |
| 88 observers_.AddObserver(observer); |
| 89 } |
| 90 |
| 91 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( |
| 92 AudioPrefObserver* observer) { |
| 93 observers_.RemoveObserver(observer); |
| 94 } |
| 95 |
| 96 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( |
| 97 PrefService* local_state) |
| 98 : device_mute_settings_(new base::DictionaryValue()), |
| 99 device_volume_settings_(new base::DictionaryValue()), |
| 100 local_state_(local_state) { |
| 101 InitializePrefObservers(); |
| 102 |
| 103 UpdateDevicesMutePref(); |
| 104 UpdateDevicesVolumePref(); |
| 105 } |
| 106 |
| 107 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { |
| 108 }; |
| 109 |
| 110 void AudioDevicesPrefHandlerImpl::InitializePrefObservers() { |
| 111 pref_change_registrar_.Init(local_state_); |
| 112 base::Closure callback = |
| 113 base::Bind(&AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange, |
| 114 base::Unretained(this)); |
| 115 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback); |
| 116 pref_change_registrar_.Add(prefs::kAudioCaptureAllowed, callback); |
| 117 } |
| 118 |
| 119 void AudioDevicesPrefHandlerImpl::UpdateDevicesMutePref() { |
| 120 const base::DictionaryValue* mute_prefs = |
| 121 local_state_->GetDictionary(prefs::kAudioDevicesMute); |
| 122 if (mute_prefs) |
| 123 device_mute_settings_.reset(mute_prefs->DeepCopy()); |
| 124 } |
| 125 |
| 126 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { |
| 127 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); |
| 128 base::DictionaryValue::Iterator it(*device_mute_settings_); |
| 129 while (!it.IsAtEnd()) { |
| 130 int mute = kPrefMuteOff; |
| 131 it.value().GetAsInteger(&mute); |
| 132 dict_update->Set(it.key(), new base::FundamentalValue(mute)); |
| 133 it.Advance(); |
| 134 } |
| 135 } |
| 136 |
| 137 void AudioDevicesPrefHandlerImpl::UpdateDevicesVolumePref() { |
| 138 const base::DictionaryValue* volume_prefs = |
| 139 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); |
| 140 if (volume_prefs) |
| 141 device_volume_settings_.reset(volume_prefs->DeepCopy()); |
| 142 } |
| 143 |
| 144 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { |
| 145 DictionaryPrefUpdate dict_update(local_state_, |
| 146 prefs::kAudioDevicesVolumePercent); |
| 147 base::DictionaryValue::Iterator it(*device_volume_settings_); |
| 148 while (!it.IsAtEnd()) { |
| 149 double volume = kDefaultVolumePercent; |
| 150 it.value().GetAsDouble(&volume); |
| 151 dict_update->Set(it.key(), new base::FundamentalValue(volume)); |
| 152 it.Advance(); |
| 153 } |
| 154 } |
| 155 |
| 156 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
| 157 std::string active_device) { |
| 158 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
| 159 device_mute_settings_->SetInteger(active_device, old_mute); |
| 160 SaveDevicesMutePref(); |
| 161 } |
| 162 |
| 163 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( |
| 164 std::string active_device) { |
| 165 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); |
| 166 device_volume_settings_->SetDouble(active_device, old_volume); |
| 167 SaveDevicesVolumePref(); |
| 168 } |
| 169 |
| 170 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { |
| 171 FOR_EACH_OBSERVER(AudioPrefObserver, |
| 172 observers_, |
| 173 OnAudioPolicyPrefChanged()); |
| 174 } |
| 175 |
| 176 // static |
| 177 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { |
| 178 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); |
| 179 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); |
| 180 |
| 181 // TODO(jennyz,rkc): Move the rest of the preferences registered by |
| 182 // AudioPrefHandlerImpl::RegisterPrefs here once we remove the old audio |
| 183 // handler code. |
| 184 } |
| 185 |
| 186 // static |
| 187 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( |
| 188 PrefService* local_state) { |
| 189 return new AudioDevicesPrefHandlerImpl(local_state); |
| 190 } |
| 191 |
| 192 } // namespace chromeos |
| OLD | NEW |