Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_handler.h" | 5 #include "chrome/browser/chromeos/audio/audio_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #if defined(USE_CRAS) | 13 #if defined(USE_CRAS) |
| 14 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" | 14 #include "chrome/browser/chromeos/audio/audio_mixer_cras.h" |
| 15 #else | 15 #else |
| 16 #include "chrome/browser/chromeos/audio/audio_mixer_alsa.h" | 16 #include "chrome/browser/chromeos/audio/audio_mixer_alsa.h" |
| 17 #endif | 17 #endif |
| 18 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 18 #include "chrome/browser/prefs/pref_service.h" | 19 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "chrome/common/chrome_notification_types.h" | |
| 19 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/notification_service.h" | |
| 21 | 25 |
| 22 using std::max; | 26 using std::max; |
| 23 using std::min; | 27 using std::min; |
| 24 | 28 |
| 25 namespace chromeos { | 29 namespace chromeos { |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 // Default value for the volume pref, as a percent in the range [0.0, 100.0]. | 33 // Default value for the volume pref, as a percent in the range [0.0, 100.0]. |
| 30 const double kDefaultVolumePercent = 75.0; | 34 const double kDefaultVolumePercent = 75.0; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 // Register the old decibel-based pref so we can clear it. | 87 // Register the old decibel-based pref so we can clear it. |
| 84 // TODO(derat): Remove this after R20: http://crbug.com/112039 | 88 // TODO(derat): Remove this after R20: http://crbug.com/112039 |
| 85 if (!local_state->FindPreference(prefs::kAudioVolumeDb)) | 89 if (!local_state->FindPreference(prefs::kAudioVolumeDb)) |
| 86 local_state->RegisterDoublePref(prefs::kAudioVolumeDb, | 90 local_state->RegisterDoublePref(prefs::kAudioVolumeDb, |
| 87 0, | 91 0, |
| 88 PrefService::UNSYNCABLE_PREF); | 92 PrefService::UNSYNCABLE_PREF); |
| 89 local_state->ClearPref(prefs::kAudioVolumeDb); | 93 local_state->ClearPref(prefs::kAudioVolumeDb); |
| 90 local_state->UnregisterPreference(prefs::kAudioVolumeDb); | 94 local_state->UnregisterPreference(prefs::kAudioVolumeDb); |
| 91 } | 95 } |
| 92 | 96 |
| 97 // static | |
| 98 void AudioHandler::RegisterUserPrefs(PrefService* user_prefs) { | |
| 99 user_prefs->RegisterBooleanPref(prefs::kAudioOutputDisabled, | |
| 100 false, | |
| 101 PrefService::UNSYNCABLE_PREF); | |
| 102 user_prefs->RegisterBooleanPref(prefs::kAudioCaptureDisabled, | |
| 103 false, | |
| 104 PrefService::UNSYNCABLE_PREF); | |
| 105 } | |
| 106 | |
| 107 void AudioHandler::InitializePrefObservers(PrefService* user_prefs) { | |
| 108 // Don't do this for the login profile. | |
| 109 if (!chromeos::UserManager::Get()->IsUserLoggedIn()) | |
|
Mattias Nissler (ping if slow)
2012/08/28 12:24:24
Isn't AudioHandler a singleton? Do you call Initia
pastarmovj
2012/08/28 15:11:50
Moved to local_state as suggested.
| |
| 110 return; | |
| 111 | |
| 112 user_prefs_ = user_prefs; | |
| 113 pref_change_registrar_.Init(user_prefs_); | |
| 114 pref_change_registrar_.Add(prefs::kAudioOutputDisabled, this); | |
| 115 pref_change_registrar_.Add(prefs::kAudioCaptureDisabled, this); | |
| 116 ApplyAudioPolicy(); | |
| 117 } | |
| 118 | |
| 93 double AudioHandler::GetVolumePercent() { | 119 double AudioHandler::GetVolumePercent() { |
| 94 return mixer_->GetVolumePercent(); | 120 return mixer_->GetVolumePercent(); |
| 95 } | 121 } |
| 96 | 122 |
| 97 void AudioHandler::SetVolumePercent(double volume_percent) { | 123 void AudioHandler::SetVolumePercent(double volume_percent) { |
| 98 volume_percent = min(max(volume_percent, 0.0), 100.0); | 124 volume_percent = min(max(volume_percent, 0.0), 100.0); |
| 99 if (IsMuted() && volume_percent > 0.0) | 125 if (IsMuted() && volume_percent > 0.0) |
| 100 SetMuted(false); | 126 SetMuted(false); |
| 101 mixer_->SetVolumePercent(volume_percent); | 127 mixer_->SetVolumePercent(volume_percent); |
| 102 prefs_->SetDouble(prefs::kAudioVolumePercent, volume_percent); | 128 local_state_->SetDouble(prefs::kAudioVolumePercent, volume_percent); |
| 103 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); | 129 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnVolumeChanged()); |
| 104 } | 130 } |
| 105 | 131 |
| 106 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { | 132 void AudioHandler::AdjustVolumeByPercent(double adjust_by_percent) { |
| 107 SetVolumePercent(mixer_->GetVolumePercent() + adjust_by_percent); | 133 SetVolumePercent(mixer_->GetVolumePercent() + adjust_by_percent); |
| 108 } | 134 } |
| 109 | 135 |
| 110 bool AudioHandler::IsMuted() { | 136 bool AudioHandler::IsMuted() { |
| 111 return mixer_->IsMuted(); | 137 return mixer_->IsMuted(); |
| 112 } | 138 } |
| 113 | 139 |
| 114 void AudioHandler::SetMuted(bool mute) { | 140 void AudioHandler::SetMuted(bool mute) { |
| 115 mixer_->SetMuted(mute); | 141 if (!mixer_->IsMuteLocked()) { |
| 116 prefs_->SetInteger(prefs::kAudioMute, mute ? kPrefMuteOn : kPrefMuteOff); | 142 mixer_->SetMuted(mute); |
| 143 local_state_->SetInteger(prefs::kAudioMute, | |
| 144 mute ? kPrefMuteOn : kPrefMuteOff); | |
| 145 } | |
| 117 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnMuteToggled()); | 146 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnMuteToggled()); |
| 118 } | 147 } |
| 119 | 148 |
| 149 bool AudioHandler::IsMicMuted() { | |
| 150 return mixer_->IsCaptureMuted(); | |
| 151 } | |
| 152 | |
| 153 void AudioHandler::SetMicMuted(bool mute) { | |
| 154 if (!mixer_->IsCaptureMuteLocked()) | |
| 155 mixer_->SetCaptureMuted(mute); | |
| 156 } | |
| 157 | |
| 120 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) { | 158 void AudioHandler::AddVolumeObserver(VolumeObserver* observer) { |
| 121 volume_observers_.AddObserver(observer); | 159 volume_observers_.AddObserver(observer); |
| 122 } | 160 } |
| 123 | 161 |
| 124 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) { | 162 void AudioHandler::RemoveVolumeObserver(VolumeObserver* observer) { |
| 125 volume_observers_.RemoveObserver(observer); | 163 volume_observers_.RemoveObserver(observer); |
| 126 } | 164 } |
| 127 | 165 |
| 166 void AudioHandler::Observe(int type, | |
| 167 const content::NotificationSource& source, | |
| 168 const content::NotificationDetails& details) { | |
| 169 if (type == chrome::NOTIFICATION_PROFILE_CREATED) { | |
| 170 Profile* profile = content::Source<Profile>(source).ptr(); | |
| 171 if (!profile->IsOffTheRecord()) | |
| 172 InitializePrefObservers(profile->GetPrefs()); | |
|
Mattias Nissler (ping if slow)
2012/08/28 12:24:24
So when a second profile gets created, we override
pastarmovj
2012/08/28 15:11:50
Done.
| |
| 173 } | |
| 174 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
|
Mattias Nissler (ping if slow)
2012/08/28 12:24:24
else if
pastarmovj
2012/08/28 15:11:50
Done.
| |
| 175 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
| 176 if (*pref_name == prefs::kAudioOutputDisabled || | |
| 177 *pref_name == prefs::kAudioCaptureDisabled) { | |
| 178 ApplyAudioPolicy(); | |
| 179 } | |
| 180 } | |
|
Mattias Nissler (ping if slow)
2012/08/28 12:24:24
else {
NOTREACHED() << type;
}
pastarmovj
2012/08/28 15:11:50
Done.
| |
| 181 } | |
| 182 | |
| 128 AudioHandler::AudioHandler(AudioMixer* mixer) | 183 AudioHandler::AudioHandler(AudioMixer* mixer) |
| 129 : mixer_(mixer), | 184 : mixer_(mixer), |
| 130 prefs_(g_browser_process->local_state()) { | 185 local_state_(g_browser_process->local_state()) { |
| 131 mixer_->SetVolumePercent(prefs_->GetDouble(prefs::kAudioVolumePercent)); | 186 mixer_->SetCaptureMuteLocked(false); |
| 132 mixer_->SetMuted(prefs_->GetInteger(prefs::kAudioMute) == kPrefMuteOn); | 187 mixer_->SetCaptureMuted(false); |
| 188 mixer_->SetMuteLocked(false); | |
| 189 mixer_->SetMuted(local_state_->GetInteger(prefs::kAudioMute) == kPrefMuteOn); | |
| 190 mixer_->SetVolumePercent(local_state_->GetDouble(prefs::kAudioVolumePercent)); | |
| 133 mixer_->Init(); | 191 mixer_->Init(); |
| 192 registrar_.Add(this, | |
| 193 chrome::NOTIFICATION_PROFILE_CREATED, | |
| 194 content::NotificationService::AllSources()); | |
| 134 } | 195 } |
| 135 | 196 |
| 136 AudioHandler::~AudioHandler() { | 197 AudioHandler::~AudioHandler() { |
| 137 mixer_.reset(); | 198 mixer_.reset(); |
| 138 }; | 199 }; |
| 139 | 200 |
| 201 void AudioHandler::ApplyAudioPolicy() { | |
| 202 if (user_prefs_->GetBoolean(prefs::kAudioOutputDisabled)) { | |
| 203 mixer_->SetMuted(true); | |
| 204 mixer_->SetMuteLocked(true); | |
| 205 } else { | |
| 206 mixer_->SetMuteLocked(false); | |
| 207 mixer_->SetMuted( | |
| 208 local_state_->GetInteger(prefs::kAudioMute) == kPrefMuteOn); | |
| 209 } | |
| 210 FOR_EACH_OBSERVER(VolumeObserver, volume_observers_, OnMuteToggled()); | |
| 211 if (user_prefs_->GetBoolean(prefs::kAudioCaptureDisabled)) { | |
| 212 mixer_->SetCaptureMuted(true); | |
| 213 mixer_->SetCaptureMuteLocked(true); | |
| 214 } else { | |
| 215 mixer_->SetCaptureMuteLocked(false); | |
| 216 mixer_->SetCaptureMuted(false); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 | |
| 140 } // namespace chromeos | 221 } // namespace chromeos |
| OLD | NEW |