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 using std::max; | |
| 19 using std::min; | |
|
hshi1
2013/05/02 00:57:05
I don't think you're using std::max and std::min i
rkc
2013/05/02 01:53:36
Done.
| |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Default value for the volume pref, as a percent in the range [0.0, 100.0]. | |
| 26 const double kDefaultVolumePercent = 75.0; | |
| 27 | |
| 28 // Values used for muted preference. | |
| 29 const int kPrefMuteOff = 0; | |
| 30 const int kPrefMuteOn = 1; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue() { | |
| 35 if (!CrasAudioHandler::IsInitialized()) | |
| 36 return kDefaultVolumePercent; | |
| 37 | |
| 38 UpdateDevicesVolumePref(); | |
| 39 std::string active_device_id = base::Uint64ToString( | |
| 40 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 41 if (!device_volume_settings_->HasKey(active_device_id)) { | |
| 42 MigrateDeviceVolumeSettings(active_device_id); | |
| 43 } | |
| 44 double volume; | |
| 45 device_volume_settings_->GetDouble(active_device_id, &volume); | |
| 46 return volume; | |
| 47 } | |
| 48 | |
| 49 void AudioDevicesPrefHandlerImpl::SetOutputVolumeValue(double volume_percent) { | |
| 50 std::string active_device_id = base::Uint64ToString( | |
| 51 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 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 } | |
| 66 int mute; | |
| 67 device_mute_settings_->GetInteger(active_device_id, &mute); | |
| 68 return (mute == kPrefMuteOn); | |
| 69 } | |
| 70 | |
| 71 void AudioDevicesPrefHandlerImpl::SetOutputMuteValue(bool mute) { | |
| 72 std::string active_device_id = base::Uint64ToString( | |
| 73 CrasAudioHandler::Get()->GetActiveOutputNode()); | |
| 74 device_mute_settings_->SetBoolean(active_device_id, | |
| 75 mute ? kPrefMuteOn : kPrefMuteOff); | |
| 76 SaveDevicesVolumePref(); | |
| 77 } | |
| 78 | |
| 79 bool AudioDevicesPrefHandlerImpl::GetAudioCaptureAllowedValue() { | |
| 80 return local_state_->GetBoolean(prefs::kAudioCaptureAllowed); | |
| 81 } | |
| 82 | |
| 83 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { | |
| 84 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); | |
| 85 } | |
| 86 | |
| 87 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | |
| 88 AudioPrefObserver* observer) { | |
| 89 observers_.AddObserver(observer); | |
| 90 } | |
| 91 | |
| 92 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( | |
| 93 AudioPrefObserver* observer) { | |
| 94 observers_.RemoveObserver(observer); | |
| 95 } | |
| 96 | |
| 97 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( | |
| 98 PrefService* local_state) | |
| 99 : device_mute_settings_(new base::DictionaryValue()), | |
| 100 device_volume_settings_(new base::DictionaryValue()), | |
| 101 local_state_(local_state) { | |
| 102 InitializePrefObservers(); | |
| 103 | |
| 104 UpdateDevicesMutePref(); | |
| 105 UpdateDevicesVolumePref(); | |
| 106 } | |
| 107 | |
| 108 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { | |
| 109 }; | |
| 110 | |
| 111 void AudioDevicesPrefHandlerImpl::InitializePrefObservers() { | |
| 112 pref_change_registrar_.Init(local_state_); | |
| 113 base::Closure callback = | |
| 114 base::Bind(&AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange, | |
| 115 base::Unretained(this)); | |
| 116 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback); | |
| 117 pref_change_registrar_.Add(::prefs::kAudioCaptureAllowed, callback); | |
| 118 } | |
| 119 | |
| 120 void AudioDevicesPrefHandlerImpl::UpdateDevicesMutePref() { | |
| 121 const base::DictionaryValue* mute_prefs = | |
| 122 local_state_->GetDictionary(prefs::kAudioDevicesMute); | |
| 123 if (mute_prefs) | |
| 124 device_mute_settings_ .reset(mute_prefs->DeepCopy()); | |
| 125 } | |
| 126 | |
| 127 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { | |
| 128 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); | |
| 129 base::DictionaryValue::Iterator it(*device_mute_settings_); | |
| 130 while (!it.IsAtEnd()) { | |
| 131 int mute; | |
| 132 it.value().GetAsInteger(&mute); | |
| 133 dict_update->Set(it.key(), new base::FundamentalValue(mute)); | |
| 134 it.Advance(); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void AudioDevicesPrefHandlerImpl::UpdateDevicesVolumePref() { | |
| 139 const base::DictionaryValue* volume_prefs = | |
| 140 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); | |
| 141 if (volume_prefs) | |
| 142 device_volume_settings_ .reset(volume_prefs->DeepCopy()); | |
| 143 } | |
| 144 | |
| 145 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { | |
| 146 DictionaryPrefUpdate dict_update(local_state_, | |
| 147 prefs::kAudioDevicesVolumePercent); | |
| 148 base::DictionaryValue::Iterator it(*device_volume_settings_); | |
| 149 while (!it.IsAtEnd()) { | |
| 150 double volume; | |
| 151 it.value().GetAsDouble(&volume); | |
| 152 dict_update->Set(it.key(), new base::FundamentalValue(volume)); | |
| 153 it.Advance(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | |
| 158 std::string active_device) { | |
| 159 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | |
| 160 device_mute_settings_->SetInteger(active_device, old_mute); | |
| 161 SaveDevicesMutePref(); | |
| 162 } | |
| 163 | |
| 164 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( | |
| 165 std::string active_device) { | |
| 166 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); | |
| 167 device_volume_settings_->SetDouble(active_device, old_volume); | |
| 168 SaveDevicesVolumePref(); | |
| 169 } | |
| 170 | |
| 171 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { | |
| 172 FOR_EACH_OBSERVER(AudioPrefObserver, | |
| 173 observers_, | |
| 174 OnAudioPolicyPrefChanged()); | |
| 175 } | |
| 176 | |
| 177 // static | |
| 178 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 179 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); | |
| 180 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); | |
| 181 | |
| 182 registry->RegisterDoublePref(prefs::kAudioVolumePercent, | |
| 183 kDefaultVolumePercent); | |
| 184 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); | |
| 185 | |
| 186 // Register the prefs backing the audio muting policies. | |
| 187 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); | |
| 188 // This pref has moved to the media subsystem but we should verify it is | |
| 189 // there before we use it. | |
| 190 registry->RegisterBooleanPref(::prefs::kAudioCaptureAllowed, true); | |
| 191 } | |
| 192 | |
| 193 // static | |
| 194 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( | |
| 195 PrefService* local_state) { | |
| 196 return new AudioDevicesPrefHandlerImpl(local_state); | |
| 197 } | |
| 198 | |
| 199 } // namespace chromeos | |
| OLD | NEW |