| 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> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 11 #include "base/logging.h" | 9 #include "base/logging.h" |
| 12 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 13 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 18 #include "chromeos/audio/cras_audio_handler.h" | 16 #include "chromeos/audio/cras_audio_handler.h" |
| 19 | 17 |
| 20 namespace chromeos { | 18 namespace chromeos { |
| 21 | 19 |
| 22 double AudioDevicesPrefHandlerImpl::GetVolumeGainValue( | 20 namespace { |
| 23 uint64 device_id) { | 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 |
| 24 UpdateDevicesVolumePref(); | 35 UpdateDevicesVolumePref(); |
| 25 | 36 std::string active_device_id = base::Uint64ToString( |
| 26 std::string device_id_str = base::Uint64ToString(device_id); | 37 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 27 if (!device_volume_settings_->HasKey(device_id_str)) | 38 if (!device_volume_settings_->HasKey(active_device_id)) |
| 28 MigrateDeviceVolumeSettings(device_id_str); | 39 MigrateDeviceVolumeSettings(active_device_id); |
| 29 | 40 double volume = kDefaultVolumePercent; |
| 30 double volume = kDefaultVolumeGainPercent; | 41 device_volume_settings_->GetDouble(active_device_id, &volume); |
| 31 device_volume_settings_->GetDouble(device_id_str, &volume); | |
| 32 | |
| 33 return volume; | 42 return volume; |
| 34 } | 43 } |
| 35 | 44 |
| 36 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( | 45 void AudioDevicesPrefHandlerImpl::SetOutputVolumeValue(double volume_percent) { |
| 37 uint64 device_id, double value) { | 46 std::string active_device_id = base::Uint64ToString( |
| 38 value = std::min(std::max(value, 0.0), 100.0); | 47 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 39 device_volume_settings_->SetDouble(base::Uint64ToString(device_id), value); | 48 if (volume_percent > 100.0) |
| 40 | 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); |
| 41 SaveDevicesVolumePref(); | 53 SaveDevicesVolumePref(); |
| 42 } | 54 } |
| 43 | 55 |
| 44 bool AudioDevicesPrefHandlerImpl::GetMuteValue(uint64 device_id) { | 56 bool AudioDevicesPrefHandlerImpl::GetOutputMuteValue() { |
| 57 if (!CrasAudioHandler::IsInitialized()) |
| 58 return false; |
| 59 |
| 45 UpdateDevicesVolumePref(); | 60 UpdateDevicesVolumePref(); |
| 46 | 61 std::string active_device_id = base::Uint64ToString( |
| 47 std::string device_id_str = base::Uint64ToString(device_id); | 62 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 48 if (!device_mute_settings_->HasKey(device_id_str)) | 63 if (!device_mute_settings_->HasKey(active_device_id)) |
| 49 MigrateDeviceMuteSettings(device_id_str); | 64 MigrateDeviceMuteSettings(active_device_id); |
| 50 | |
| 51 int mute = kPrefMuteOff; | 65 int mute = kPrefMuteOff; |
| 52 device_mute_settings_->GetInteger(device_id_str, &mute); | 66 device_mute_settings_->GetInteger(active_device_id, &mute); |
| 53 | |
| 54 return (mute == kPrefMuteOn); | 67 return (mute == kPrefMuteOn); |
| 55 } | 68 } |
| 56 | 69 |
| 57 void AudioDevicesPrefHandlerImpl::SetMuteValue(uint64 device_id, | 70 void AudioDevicesPrefHandlerImpl::SetOutputMuteValue(bool mute) { |
| 58 bool mute) { | 71 std::string active_device_id = base::Uint64ToString( |
| 59 device_mute_settings_->SetBoolean(base::Uint64ToString(device_id), | 72 CrasAudioHandler::Get()->GetActiveOutputNode()); |
| 60 mute ? kPrefMuteOn : kPrefMuteOff); | 73 device_mute_settings_->SetBoolean(active_device_id, |
| 74 mute ? kPrefMuteOn : kPrefMuteOff); |
| 61 SaveDevicesVolumePref(); | 75 SaveDevicesVolumePref(); |
| 62 } | 76 } |
| 63 | 77 |
| 64 | |
| 65 bool AudioDevicesPrefHandlerImpl::GetAudioCaptureAllowedValue() { | 78 bool AudioDevicesPrefHandlerImpl::GetAudioCaptureAllowedValue() { |
| 66 return local_state_->GetBoolean(prefs::kAudioCaptureAllowed); | 79 return local_state_->GetBoolean(prefs::kAudioCaptureAllowed); |
| 67 } | 80 } |
| 68 | 81 |
| 69 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { | 82 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { |
| 70 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); | 83 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); |
| 71 } | 84 } |
| 72 | 85 |
| 73 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( | 86 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( |
| 74 AudioPrefObserver* observer) { | 87 AudioPrefObserver* observer) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); | 139 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); |
| 127 if (volume_prefs) | 140 if (volume_prefs) |
| 128 device_volume_settings_.reset(volume_prefs->DeepCopy()); | 141 device_volume_settings_.reset(volume_prefs->DeepCopy()); |
| 129 } | 142 } |
| 130 | 143 |
| 131 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { | 144 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { |
| 132 DictionaryPrefUpdate dict_update(local_state_, | 145 DictionaryPrefUpdate dict_update(local_state_, |
| 133 prefs::kAudioDevicesVolumePercent); | 146 prefs::kAudioDevicesVolumePercent); |
| 134 base::DictionaryValue::Iterator it(*device_volume_settings_); | 147 base::DictionaryValue::Iterator it(*device_volume_settings_); |
| 135 while (!it.IsAtEnd()) { | 148 while (!it.IsAtEnd()) { |
| 136 double volume = kDefaultVolumeGainPercent; | 149 double volume = kDefaultVolumePercent; |
| 137 it.value().GetAsDouble(&volume); | 150 it.value().GetAsDouble(&volume); |
| 138 dict_update->Set(it.key(), new base::FundamentalValue(volume)); | 151 dict_update->Set(it.key(), new base::FundamentalValue(volume)); |
| 139 it.Advance(); | 152 it.Advance(); |
| 140 } | 153 } |
| 141 } | 154 } |
| 142 | 155 |
| 143 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( | 156 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( |
| 144 std::string active_device) { | 157 std::string active_device) { |
| 145 int old_mute = local_state_->GetInteger(prefs::kAudioMute); | 158 int old_mute = local_state_->GetInteger(prefs::kAudioMute); |
| 146 device_mute_settings_->SetInteger(active_device, old_mute); | 159 device_mute_settings_->SetInteger(active_device, old_mute); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 170 // handler code. | 183 // handler code. |
| 171 } | 184 } |
| 172 | 185 |
| 173 // static | 186 // static |
| 174 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( | 187 AudioDevicesPrefHandler* AudioDevicesPrefHandler::Create( |
| 175 PrefService* local_state) { | 188 PrefService* local_state) { |
| 176 return new AudioDevicesPrefHandlerImpl(local_state); | 189 return new AudioDevicesPrefHandlerImpl(local_state); |
| 177 } | 190 } |
| 178 | 191 |
| 179 } // namespace chromeos | 192 } // namespace chromeos |
| OLD | NEW |