Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: chromeos/audio/audio_devices_pref_handler_impl.cc

Issue 1380103003: Store audio device's active state in preference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix string format Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chromeos/audio/audio_devices_pref_handler_impl.h" 5 #include "chromeos/audio/audio_devices_pref_handler_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/prefs/pref_registry_simple.h" 14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service.h" 15 #include "base/prefs/pref_service.h"
16 #include "base/prefs/scoped_user_pref_update.h" 16 #include "base/prefs/scoped_user_pref_update.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "chromeos/audio/audio_device.h" 18 #include "chromeos/audio/audio_device.h"
19 #include "chromeos/chromeos_pref_names.h" 19 #include "chromeos/chromeos_pref_names.h"
20 20
21 namespace { 21 namespace {
22 22
23 // Values used for muted preference. 23 // Values used for muted preference.
24 const int kPrefMuteOff = 0; 24 const int kPrefMuteOff = 0;
25 const int kPrefMuteOn = 1; 25 const int kPrefMuteOn = 1;
26 26
27 // Gets the device id string for storing audio preference. The format of 27 // Gets the device id string for storing audio preference. The format of
28 // device string is a string consisting of 3 parts. 28 // device string is a string consisting of 2 parts.
29 // |device_name| : |integer from lower 32 bit of device id| : 29 // |integer from lower 32 bit of device id| :
30 // |0(output device) or 1(input device)| 30 // |0(output device) or 1(input device)|
31 // If an audio device has both integrated input and output devices, the first 2 31 // If an audio device has both integrated input and output devices, the first 2
32 // parts of the string could be identical, only the last part will differentiate 32 // parts of the string could be identical, only the last part will differentiate
33 // them. 33 // them.
34 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { 34 std::string GetDeviceIdString(const chromeos::AudioDevice& device) {
35 std::string device_id_string = 35 std::string device_id_string =
36 device.device_name + " : " + 36 base::Uint64ToString(device.stable_device_id &
37 base::Uint64ToString(device.id & static_cast<uint64_t>(0xffffffff)) + 37 static_cast<uint64_t>(0xffffffff)) +
38 " : " + (device.is_input ? "1" : "0"); 38 " : " + (device.is_input ? "1" : "0");
39 // Replace any periods from the device id string with a space, since setting 39 // Replace any periods from the device id string with a space, since setting
40 // names cannot contain periods. 40 // names cannot contain periods.
41 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); 41 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' ');
42 return device_id_string; 42 return device_id_string;
43 } 43 }
44 44
45 } // namespace 45 } // namespace
46 46
47 namespace chromeos { 47 namespace chromeos {
(...skipping 13 matching lines...) Expand all
61 } 61 }
62 62
63 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( 63 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue(
64 const AudioDevice& device, double value) { 64 const AudioDevice& device, double value) {
65 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); 65 device_volume_settings_->SetDouble(GetDeviceIdString(device), value);
66 66
67 SaveDevicesVolumePref(); 67 SaveDevicesVolumePref();
68 } 68 }
69 69
70 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { 70 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) {
71 UpdateDevicesMutePref();
72
73 std::string device_id_str = GetDeviceIdString(device); 71 std::string device_id_str = GetDeviceIdString(device);
74 if (!device_mute_settings_->HasKey(device_id_str)) 72 if (!device_mute_settings_->HasKey(device_id_str))
75 MigrateDeviceMuteSettings(device_id_str); 73 MigrateDeviceMuteSettings(device_id_str);
76 74
77 int mute = kPrefMuteOff; 75 int mute = kPrefMuteOff;
78 device_mute_settings_->GetInteger(device_id_str, &mute); 76 device_mute_settings_->GetInteger(device_id_str, &mute);
79 77
80 return (mute == kPrefMuteOn); 78 return (mute == kPrefMuteOn);
81 } 79 }
82 80
83 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, 81 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device,
84 bool mute) { 82 bool mute) {
85 device_mute_settings_->SetInteger(GetDeviceIdString(device), 83 device_mute_settings_->SetInteger(GetDeviceIdString(device),
86 mute ? kPrefMuteOn : kPrefMuteOff); 84 mute ? kPrefMuteOn : kPrefMuteOff);
87 SaveDevicesMutePref(); 85 SaveDevicesMutePref();
88 } 86 }
89 87
88 AudioDeviceState AudioDevicesPrefHandlerImpl::GetDeviceState(
89 const AudioDevice& device) {
90 std::string device_id_str = GetDeviceIdString(device);
91 if (!device_state_settings_->HasKey(device_id_str)) {
92 device_state_settings_->SetInteger(
93 device_id_str, static_cast<int>(AUDIO_STATE_NOT_AVAILABLE));
94 SaveDevicesStatePref();
95 }
96 int state = static_cast<int>(AUDIO_STATE_NOT_AVAILABLE);
97 device_state_settings_->GetInteger(device_id_str, &state);
98 return (AudioDeviceState)state;
99 }
100
101 void AudioDevicesPrefHandlerImpl::SetDeviceState(const AudioDevice& device,
102 AudioDeviceState state) {
103 device_state_settings_->SetInteger(GetDeviceIdString(device),
104 static_cast<int>(state));
105 SaveDevicesStatePref();
106 }
107
90 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() { 108 bool AudioDevicesPrefHandlerImpl::GetAudioOutputAllowedValue() {
91 return local_state_->GetBoolean(prefs::kAudioOutputAllowed); 109 return local_state_->GetBoolean(prefs::kAudioOutputAllowed);
92 } 110 }
93 111
94 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver( 112 void AudioDevicesPrefHandlerImpl::AddAudioPrefObserver(
95 AudioPrefObserver* observer) { 113 AudioPrefObserver* observer) {
96 observers_.AddObserver(observer); 114 observers_.AddObserver(observer);
97 } 115 }
98 116
99 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( 117 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver(
100 AudioPrefObserver* observer) { 118 AudioPrefObserver* observer) {
101 observers_.RemoveObserver(observer); 119 observers_.RemoveObserver(observer);
102 } 120 }
103 121
104 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue( 122 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue(
105 const AudioDevice& device) { 123 const AudioDevice& device) {
106 UpdateDevicesVolumePref();
107
108 std::string device_id_str = GetDeviceIdString(device); 124 std::string device_id_str = GetDeviceIdString(device);
109 if (!device_volume_settings_->HasKey(device_id_str)) 125 if (!device_volume_settings_->HasKey(device_id_str))
110 MigrateDeviceVolumeSettings(device_id_str); 126 MigrateDeviceVolumeSettings(device_id_str);
111 127
112 // TODO(jennyz, rkc): Return a meaningful input gain default value, when 128 // TODO(jennyz, rkc): Return a meaningful input gain default value, when
113 // cras has added support for normalizing input gain range. 129 // cras has added support for normalizing input gain range.
114 double value = device.is_input ? 130 double value = device.is_input ?
115 0.0 : GetDeviceDefaultOutputVolume(device); 131 0.0 : GetDeviceDefaultOutputVolume(device);
116 // TODO(rkc): The above code is completely ignored since we 'always' have a 132 // TODO(rkc): The above code is completely ignored since we 'always' have a
117 // default pref value. Fix this. http://crbug.com/442489 133 // default pref value. Fix this. http://crbug.com/442489
118 device_volume_settings_->GetDouble(device_id_str, &value); 134 device_volume_settings_->GetDouble(device_id_str, &value);
119 135
120 return value; 136 return value;
121 } 137 }
122 138
123 double AudioDevicesPrefHandlerImpl::GetDeviceDefaultOutputVolume( 139 double AudioDevicesPrefHandlerImpl::GetDeviceDefaultOutputVolume(
124 const AudioDevice& device) { 140 const AudioDevice& device) {
125 if (device.type == AUDIO_TYPE_HDMI) 141 if (device.type == AUDIO_TYPE_HDMI)
126 return kDefaultHdmiOutputVolumePercent; 142 return kDefaultHdmiOutputVolumePercent;
127 else 143 else
128 return kDefaultOutputVolumePercent; 144 return kDefaultOutputVolumePercent;
129 } 145 }
130 146
131 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl( 147 AudioDevicesPrefHandlerImpl::AudioDevicesPrefHandlerImpl(
132 PrefService* local_state) 148 PrefService* local_state)
133 : device_mute_settings_(new base::DictionaryValue()), 149 : device_mute_settings_(new base::DictionaryValue()),
134 device_volume_settings_(new base::DictionaryValue()), 150 device_volume_settings_(new base::DictionaryValue()),
151 device_state_settings_(new base::DictionaryValue()),
135 local_state_(local_state) { 152 local_state_(local_state) {
136 InitializePrefObservers(); 153 InitializePrefObservers();
137 154
138 UpdateDevicesMutePref(); 155 LoadDevicesMutePref();
139 UpdateDevicesVolumePref(); 156 LoadDevicesVolumePref();
157 LoadDevicesStatePref();
140 } 158 }
141 159
142 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() { 160 AudioDevicesPrefHandlerImpl::~AudioDevicesPrefHandlerImpl() {
143 } 161 }
144 162
145 void AudioDevicesPrefHandlerImpl::InitializePrefObservers() { 163 void AudioDevicesPrefHandlerImpl::InitializePrefObservers() {
146 pref_change_registrar_.Init(local_state_); 164 pref_change_registrar_.Init(local_state_);
147 base::Closure callback = 165 base::Closure callback =
148 base::Bind(&AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange, 166 base::Bind(&AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange,
149 base::Unretained(this)); 167 base::Unretained(this));
150 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback); 168 pref_change_registrar_.Add(prefs::kAudioOutputAllowed, callback);
151 } 169 }
152 170
153 void AudioDevicesPrefHandlerImpl::UpdateDevicesMutePref() { 171 void AudioDevicesPrefHandlerImpl::LoadDevicesMutePref() {
154 const base::DictionaryValue* mute_prefs = 172 const base::DictionaryValue* mute_prefs =
155 local_state_->GetDictionary(prefs::kAudioDevicesMute); 173 local_state_->GetDictionary(prefs::kAudioDevicesMute);
156 if (mute_prefs) 174 if (mute_prefs)
157 device_mute_settings_.reset(mute_prefs->DeepCopy()); 175 device_mute_settings_.reset(mute_prefs->DeepCopy());
158 } 176 }
159 177
160 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { 178 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() {
161 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); 179 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute);
162 base::DictionaryValue::Iterator it(*device_mute_settings_); 180 dict_update->MergeDictionary(device_mute_settings_.get());
163 while (!it.IsAtEnd()) {
164 int mute = kPrefMuteOff;
165 it.value().GetAsInteger(&mute);
166 dict_update->SetInteger(it.key(), mute);
167 it.Advance();
168 }
169 } 181 }
170 182
171 void AudioDevicesPrefHandlerImpl::UpdateDevicesVolumePref() { 183 void AudioDevicesPrefHandlerImpl::LoadDevicesVolumePref() {
172 const base::DictionaryValue* volume_prefs = 184 const base::DictionaryValue* volume_prefs =
173 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); 185 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent);
174 if (volume_prefs) 186 if (volume_prefs)
175 device_volume_settings_.reset(volume_prefs->DeepCopy()); 187 device_volume_settings_.reset(volume_prefs->DeepCopy());
176 } 188 }
177 189
178 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { 190 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() {
179 DictionaryPrefUpdate dict_update(local_state_, 191 DictionaryPrefUpdate dict_update(local_state_,
180 prefs::kAudioDevicesVolumePercent); 192 prefs::kAudioDevicesVolumePercent);
181 base::DictionaryValue::Iterator it(*device_volume_settings_); 193 dict_update->MergeDictionary(device_volume_settings_.get());
182 while (!it.IsAtEnd()) { 194 }
183 double volume = kDefaultOutputVolumePercent; 195
184 bool success = it.value().GetAsDouble(&volume); 196 void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() {
185 DCHECK(success); 197 const base::DictionaryValue* state_prefs =
186 dict_update->SetDouble(it.key(), volume); 198 local_state_->GetDictionary(prefs::kAudioDevicesState);
187 it.Advance(); 199 if (state_prefs)
188 } 200 device_state_settings_.reset(state_prefs->DeepCopy());
201 }
202
203 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() {
204 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesState);
205 base::DictionaryValue::Iterator it(*device_state_settings_);
206 dict_update->MergeDictionary(device_state_settings_.get());
189 } 207 }
190 208
191 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( 209 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings(
192 const std::string& active_device) { 210 const std::string& active_device) {
193 int old_mute = local_state_->GetInteger(prefs::kAudioMute); 211 int old_mute = local_state_->GetInteger(prefs::kAudioMute);
194 device_mute_settings_->SetInteger(active_device, old_mute); 212 device_mute_settings_->SetInteger(active_device, old_mute);
195 SaveDevicesMutePref(); 213 SaveDevicesMutePref();
196 } 214 }
197 215
198 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( 216 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings(
199 const std::string& active_device) { 217 const std::string& active_device) {
200 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); 218 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent);
201 device_volume_settings_->SetDouble(active_device, old_volume); 219 device_volume_settings_->SetDouble(active_device, old_volume);
202 SaveDevicesVolumePref(); 220 SaveDevicesVolumePref();
203 } 221 }
204 222
205 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { 223 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() {
206 FOR_EACH_OBSERVER(AudioPrefObserver, 224 FOR_EACH_OBSERVER(AudioPrefObserver,
207 observers_, 225 observers_,
208 OnAudioPolicyPrefChanged()); 226 OnAudioPolicyPrefChanged());
209 } 227 }
210 228
211 // static 229 // static
212 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { 230 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) {
213 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); 231 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent);
214 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); 232 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute);
233 registry->RegisterDictionaryPref(prefs::kAudioDevicesState);
215 234
216 // Register the prefs backing the audio muting policies. 235 // Register the prefs backing the audio muting policies.
217 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome 236 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome
218 // media system. 237 // media system.
219 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); 238 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true);
220 239
221 // Register the legacy audio prefs for migration. 240 // Register the legacy audio prefs for migration.
222 registry->RegisterDoublePref(prefs::kAudioVolumePercent, 241 registry->RegisterDoublePref(prefs::kAudioVolumePercent,
223 kDefaultOutputVolumePercent); 242 kDefaultOutputVolumePercent);
224 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); 243 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff);
225 } 244 }
226 245
227 } // namespace chromeos 246 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/audio/audio_devices_pref_handler_impl.h ('k') | chromeos/audio/audio_devices_pref_handler_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698