Chromium Code Reviews| 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 } | |
|
stevenjb
2013/05/02 19:29:47
nit: no {}
rkc
2013/05/02 19:43:44
Done.
| |
| 41 double volume; | |
|
stevenjb
2013/05/02 19:29:47
Needs a default; GetDouble will not set |volume| i
rkc
2013/05/02 19:43:44
It is should be impossible for this call to fail s
| |
| 42 device_volume_settings_->GetDouble(active_device_id, &volume); | |
| 43 return volume; | |
| 44 } | |
| 45 | |
| 46 void AudioDevicesPrefHandlerImpl::SetOutputVolumeValue(double volume_percent) { | |
| 47 std::string active_device_id = base::Uint64ToString( | |
| 48 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 49 device_volume_settings_->SetDouble(active_device_id, volume_percent); | |
|
stevenjb
2013/05/02 19:29:47
Do we need to check/limit the range of volume_perc
rkc
2013/05/02 19:43:44
I am not sure if cras handles it, so added a check
| |
| 50 SaveDevicesVolumePref(); | |
| 51 } | |
| 52 | |
| 53 bool AudioDevicesPrefHandlerImpl::GetOutputMuteValue() { | |
| 54 if (!CrasAudioHandler::IsInitialized()) | |
| 55 return false; | |
| 56 | |
| 57 UpdateDevicesVolumePref(); | |
| 58 std::string active_device_id = base::Uint64ToString( | |
| 59 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 60 if (!device_mute_settings_->HasKey(active_device_id)) { | |
| 61 MigrateDeviceMuteSettings(active_device_id); | |
| 62 } | |
|
stevenjb
2013/05/02 19:29:47
nit: no {}
rkc
2013/05/02 19:43:44
Done.
| |
| 63 int mute; | |
|
stevenjb
2013/05/02 19:29:47
Set default
rkc
2013/05/02 19:43:44
Done.
| |
| 64 device_mute_settings_->GetInteger(active_device_id, &mute); | |
| 65 return (mute == kPrefMuteOn); | |
| 66 } | |
| 67 | |
| 68 void AudioDevicesPrefHandlerImpl::SetOutputMuteValue(bool mute) { | |
| 69 std::string active_device_id = base::Uint64ToString( | |
| 70 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 71 device_mute_settings_->SetBoolean(active_device_id, | |
| 72 mute ? kPrefMuteOn : kPrefMuteOff); | |
| 73 SaveDevicesVolumePref(); | |
| 74 } | |
| 75 | |
| 76 bool AudioDevicesPrefHandlerImpl::GetAudioCaptureAllowedValue() { | |
| 77 return local_state_->GetBoolean(prefs::kAudioCaptureAllowed); | |
| 78 } | |
| 79 | |
| 80 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { | |
| 81 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); | |
| 82 } | |
| 83 | |
| 84 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | |
| 85 AudioPrefObserver* observer) { | |
| 86 observers_.AddObserver(observer); | |
| 87 } | |
| 88 | |
| 89 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | |
| 90 AudioPrefObserver* observer) { | |
| 91 observers_.RemoveObserver(observer); | |
| 92 } | |
| 93 | |
| 94 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( | |
| 95 PrefService* local_state) | |
| 96 : device_mute_settings_(new base::DictionaryValue()), | |
|
stevenjb
2013/05/02 19:29:47
Align : with P
rkc
2013/05/02 19:43:44
Done.
| |
| 97 device_volume_settings_(new base::DictionaryValue()), | |
| 98 local_state_(local_state) { | |
| 99 InitializePrefObservers(); | |
| 100 | |
| 101 UpdateDevicesMutePref(); | |
| 102 UpdateDevicesVolumePref(); | |
| 103 } | |
| 104 | |
| 105 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { | |
| 106 }; | |
| 107 | |
| 108 void AudioDevicesPrefHandlerImpl::InitializePrefObservers() { | |
| 109 pref_change_registrar_.Init(local_state_); | |
| 110 base::Closure callback = | |
| 111 base::Bind(&AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange, | |
| 112 base::Unretained(this)); | |
| 113 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback); | |
| 114 pref_change_registrar_.Add(prefs::kAudioCaptureAllowed, callback); | |
| 115 } | |
| 116 | |
| 117 void AudioDevicesPrefHandlerImpl::UpdateDevicesMutePref() { | |
| 118 const base::DictionaryValue* mute_prefs = | |
| 119 local_state_->GetDictionary(prefs::kAudioDevicesMute); | |
| 120 if (mute_prefs) | |
| 121 device_mute_settings_ .reset(mute_prefs->DeepCopy()); | |
|
stevenjb
2013/05/02 19:29:47
no ' ' before .
rkc
2013/05/02 19:43:44
Done.
| |
| 122 } | |
| 123 | |
| 124 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { | |
| 125 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); | |
| 126 base::DictionaryValue::Iterator it(*device_mute_settings_); | |
| 127 while (!it.IsAtEnd()) { | |
| 128 int mute; | |
|
stevenjb
2013/05/02 19:29:47
needs default (or handle GetAsInteger returning fa
rkc
2013/05/02 19:43:44
Done.
| |
| 129 it.value().GetAsInteger(&mute); | |
| 130 dict_update->Set(it.key(), new base::FundamentalValue(mute)); | |
| 131 it.Advance(); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void AudioDevicesPrefHandlerImpl::UpdateDevicesVolumePref() { | |
| 136 const base::DictionaryValue* volume_prefs = | |
| 137 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); | |
| 138 if (volume_prefs) | |
| 139 device_volume_settings_ .reset(volume_prefs->DeepCopy()); | |
|
stevenjb
2013/05/02 19:29:47
no ' ' before .
rkc
2013/05/02 19:43:44
Done.
| |
| 140 } | |
| 141 | |
| 142 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { | |
| 143 DictionaryPrefUpdate dict_update(local_state_, | |
| 144 prefs::kAudioDevicesVolumePercent); | |
| 145 base::DictionaryValue::Iterator it(*device_volume_settings_); | |
| 146 while (!it.IsAtEnd()) { | |
| 147 double volume; | |
|
stevenjb
2013/05/02 19:29:47
default
rkc
2013/05/02 19:43:44
Done.
| |
| 148 it.value().GetAsDouble(&volume); | |
| 149 dict_update->Set(it.key(), new base::FundamentalValue(volume)); | |
| 150 it.Advance(); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | |
| 155 std::string active_device) { | |
| 156 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | |
| 157 device_mute_settings_->SetInteger(active_device, old_mute); | |
| 158 SaveDevicesMutePref(); | |
| 159 } | |
| 160 | |
| 161 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( | |
| 162 std::string active_device) { | |
| 163 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); | |
| 164 device_volume_settings_->SetDouble(active_device, old_volume); | |
| 165 SaveDevicesVolumePref(); | |
| 166 } | |
| 167 | |
| 168 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { | |
| 169 FOR_EACH_OBSERVER(AudioPrefObserver, | |
| 170 observers_, | |
| 171 OnAudioPolicyPrefChanged()); | |
| 172 } | |
| 173 | |
| 174 // static | |
| 175 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 176 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); | |
| 177 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | |
| 178 | |
| 179 // TODO(jennyz,rkc): Move the rest of the preferences registered by | |
| 180 // AudioPrefHandlerImpl::RegisterPrefs here once we remove the old audio | |
| 181 // handler code. | |
| 182 } | |
| 183 | |
| 184 // static | |
| 185 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( | |
| 186 PrefService* local_state) { | |
| 187 return new AudioDevicesPrefHandlerImpl(local_state); | |
| 188 } | |
| 189 | |
| 190 } // namespace chromeos | |
| OLD | NEW |