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

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

Issue 2510093003: Handle audio node stable device ID change (Closed)
Patch Set: update few comments Created 4 years 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
(...skipping 11 matching lines...) Expand all
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 // Prefs keys. 27 // Prefs keys.
28 const char kActiveKey[] = "active"; 28 const char kActiveKey[] = "active";
29 const char kActivateByUserKey[] = "activate_by_user"; 29 const char kActivateByUserKey[] = "activate_by_user";
30 30
31 // Gets the device id string for storing audio preference. The format of 31 // Gets the device id string for storing audio preference. The format of
32 // device string is a string consisting of 2 parts. 32 // device string is a string consisting of 3 parts:
33 // |version of stable device ID| :
33 // |integer from lower 32 bit of device id| : 34 // |integer from lower 32 bit of device id| :
34 // |0(output device) or 1(input device)| 35 // |0(output device) or 1(input device)|
35 // If an audio device has both integrated input and output devices, the first 2 36 // If an audio device has both integrated input and output devices, the first 2
36 // parts of the string could be identical, only the last part will differentiate 37 // parts of the string could be identical, only the last part will differentiate
37 // them. 38 // them.
38 std::string GetDeviceIdString(const chromeos::AudioDevice& device) { 39 // Note that |version of stable device ID| is present only for devices with
40 // stable device ID version >= 2. For devices with version 1, the device id
41 // string contains only latter 2 parts - in order to preserve backward
42 // compatibility with existing ID from before v2 stable device ID was
43 // introduced.
44 std::string GetVersionedDeviceIdString(const chromeos::AudioDevice& device,
45 int version) {
jennyz 2016/12/09 23:52:18 It looks like |version| can only be either 1 or 2,
tbarzic 2016/12/10 02:35:08 Done.
46 CHECK(device.stable_device_id_version >= version);
47
48 bool use_deprecated_id = version == 1 && device.stable_device_id_version == 2;
49 uint64_t stable_device_id = use_deprecated_id
50 ? device.deprecated_stable_device_id
51 : device.stable_device_id;
52 std::string version_prefix = version == 2 ? "2 : " : "";
39 std::string device_id_string = 53 std::string device_id_string =
40 base::Uint64ToString(device.stable_device_id & 54 version_prefix + base::Uint64ToString(stable_device_id &
41 static_cast<uint64_t>(0xffffffff)) + 55 static_cast<uint64_t>(0xffffffff)) +
42 " : " + (device.is_input ? "1" : "0"); 56 " : " + (device.is_input ? "1" : "0");
43 // Replace any periods from the device id string with a space, since setting 57 // Replace any periods from the device id string with a space, since setting
44 // names cannot contain periods. 58 // names cannot contain periods.
45 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' '); 59 std::replace(device_id_string.begin(), device_id_string.end(), '.', ' ');
46 return device_id_string; 60 return device_id_string;
47 } 61 }
48 62
63 std::string GetDeviceIdString(const chromeos::AudioDevice& device) {
64 return GetVersionedDeviceIdString(device, device.stable_device_id_version);
65 }
66
67 // Migrates an entry associated with |device|'s v1 stable device ID in
68 // |settings| to the key derived from |device|'s v2 stable device ID
69 // (which is expected to be equal to |intended_key|), if the entry can
70 // be found.
71 // Returns whether the migration occurred.
72 bool MigrateDeviceIdInSettings(base::DictionaryValue* settings,
73 const std::string& intended_key,
74 const chromeos::AudioDevice& device) {
75 if (device.stable_device_id_version == 1)
76 return false;
77
jennyz 2016/12/09 23:52:18 Add a DCHECK(device.stable_device_id_version == 2)
tbarzic 2016/12/10 02:35:08 Done.
78 std::string old_device_id = GetVersionedDeviceIdString(device, 1);
79 std::unique_ptr<base::Value> value;
80 if (!settings->Remove(old_device_id, &value))
81 return false;
82
83 DCHECK_EQ(intended_key, GetDeviceIdString(device));
84 settings->Set(intended_key, std::move(value));
85 return true;
86 }
87
49 } // namespace 88 } // namespace
50 89
51 namespace chromeos { 90 namespace chromeos {
52 91
53 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue( 92 double AudioDevicesPrefHandlerImpl::GetOutputVolumeValue(
54 const AudioDevice* device) { 93 const AudioDevice* device) {
55 if (!device) 94 if (!device)
56 return kDefaultOutputVolumePercent; 95 return kDefaultOutputVolumePercent;
57 else 96 else
58 return GetVolumeGainPrefValue(*device); 97 return GetVolumeGainPrefValue(*device);
59 } 98 }
60 99
61 double AudioDevicesPrefHandlerImpl::GetInputGainValue( 100 double AudioDevicesPrefHandlerImpl::GetInputGainValue(
62 const AudioDevice* device) { 101 const AudioDevice* device) {
63 DCHECK(device); 102 DCHECK(device);
64 return GetVolumeGainPrefValue(*device); 103 return GetVolumeGainPrefValue(*device);
65 } 104 }
66 105
67 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue( 106 void AudioDevicesPrefHandlerImpl::SetVolumeGainValue(
68 const AudioDevice& device, double value) { 107 const AudioDevice& device, double value) {
108 // Use this opportunity to remove device record under deprecated device ID,
109 // if one exists.
110 if (device.stable_device_id_version == 2) {
111 std::string old_device_id = GetVersionedDeviceIdString(device, 1);
112 device_volume_settings_->Remove(old_device_id, nullptr);
113 }
69 device_volume_settings_->SetDouble(GetDeviceIdString(device), value); 114 device_volume_settings_->SetDouble(GetDeviceIdString(device), value);
70 115
71 SaveDevicesVolumePref(); 116 SaveDevicesVolumePref();
72 } 117 }
73 118
74 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) { 119 bool AudioDevicesPrefHandlerImpl::GetMuteValue(const AudioDevice& device) {
75 std::string device_id_str = GetDeviceIdString(device); 120 std::string device_id_str = GetDeviceIdString(device);
76 if (!device_mute_settings_->HasKey(device_id_str)) 121 if (!device_mute_settings_->HasKey(device_id_str))
77 MigrateDeviceMuteSettings(device_id_str); 122 MigrateDeviceMuteSettings(device_id_str, device);
78 123
79 int mute = kPrefMuteOff; 124 int mute = kPrefMuteOff;
80 device_mute_settings_->GetInteger(device_id_str, &mute); 125 device_mute_settings_->GetInteger(device_id_str, &mute);
81 126
82 return (mute == kPrefMuteOn); 127 return (mute == kPrefMuteOn);
83 } 128 }
84 129
85 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device, 130 void AudioDevicesPrefHandlerImpl::SetMuteValue(const AudioDevice& device,
86 bool mute) { 131 bool mute) {
132 // Use this opportunity to remove device record under deprecated device ID,
133 // if one exists.
134 if (device.stable_device_id_version == 2) {
135 std::string old_device_id = GetVersionedDeviceIdString(device, 1);
136 device_mute_settings_->Remove(old_device_id, nullptr);
137 }
87 device_mute_settings_->SetInteger(GetDeviceIdString(device), 138 device_mute_settings_->SetInteger(GetDeviceIdString(device),
88 mute ? kPrefMuteOn : kPrefMuteOff); 139 mute ? kPrefMuteOn : kPrefMuteOff);
89 SaveDevicesMutePref(); 140 SaveDevicesMutePref();
90 } 141 }
91 142
92 void AudioDevicesPrefHandlerImpl::SetDeviceActive(const AudioDevice& device, 143 void AudioDevicesPrefHandlerImpl::SetDeviceActive(const AudioDevice& device,
93 bool active, 144 bool active,
94 bool activate_by_user) { 145 bool activate_by_user) {
95 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); 146 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
96 dict->SetBoolean(kActiveKey, active); 147 dict->SetBoolean(kActiveKey, active);
97 if (active) 148 if (active)
98 dict->SetBoolean(kActivateByUserKey, activate_by_user); 149 dict->SetBoolean(kActivateByUserKey, activate_by_user);
99 150
151 // Use this opportunity to remove device record under deprecated device ID,
152 // if one exists.
153 if (device.stable_device_id_version == 2) {
154 std::string old_device_id = GetVersionedDeviceIdString(device, 1);
155 device_state_settings_->Remove(old_device_id, nullptr);
156 }
100 device_state_settings_->Set(GetDeviceIdString(device), std::move(dict)); 157 device_state_settings_->Set(GetDeviceIdString(device), std::move(dict));
101 SaveDevicesStatePref(); 158 SaveDevicesStatePref();
102 } 159 }
103 160
104 bool AudioDevicesPrefHandlerImpl::GetDeviceActive(const AudioDevice& device, 161 bool AudioDevicesPrefHandlerImpl::GetDeviceActive(const AudioDevice& device,
105 bool* active, 162 bool* active,
106 bool* activate_by_user) { 163 bool* activate_by_user) {
107 const std::string device_id_str = GetDeviceIdString(device); 164 const std::string device_id_str = GetDeviceIdString(device);
108 if (!device_state_settings_->HasKey(device_id_str)) 165 if (!device_state_settings_->HasKey(device_id_str) &&
166 !MigrateDevicesStatePref(device_id_str, device)) {
109 return false; 167 return false;
168 }
110 169
111 base::DictionaryValue* dict = NULL; 170 base::DictionaryValue* dict = NULL;
112 if (!device_state_settings_->GetDictionary(device_id_str, &dict)) { 171 if (!device_state_settings_->GetDictionary(device_id_str, &dict)) {
113 LOG(ERROR) << "Could not get device state for device:" << device.ToString(); 172 LOG(ERROR) << "Could not get device state for device:" << device.ToString();
114 return false; 173 return false;
115 } 174 }
116 if (!dict->GetBoolean(kActiveKey, active)) { 175 if (!dict->GetBoolean(kActiveKey, active)) {
117 LOG(ERROR) << "Could not get active value for device:" << device.ToString(); 176 LOG(ERROR) << "Could not get active value for device:" << device.ToString();
118 return false; 177 return false;
119 } 178 }
(...skipping 19 matching lines...) Expand all
139 198
140 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver( 199 void AudioDevicesPrefHandlerImpl::RemoveAudioPrefObserver(
141 AudioPrefObserver* observer) { 200 AudioPrefObserver* observer) {
142 observers_.RemoveObserver(observer); 201 observers_.RemoveObserver(observer);
143 } 202 }
144 203
145 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue( 204 double AudioDevicesPrefHandlerImpl::GetVolumeGainPrefValue(
146 const AudioDevice& device) { 205 const AudioDevice& device) {
147 std::string device_id_str = GetDeviceIdString(device); 206 std::string device_id_str = GetDeviceIdString(device);
148 if (!device_volume_settings_->HasKey(device_id_str)) 207 if (!device_volume_settings_->HasKey(device_id_str))
149 MigrateDeviceVolumeSettings(device_id_str); 208 MigrateDeviceVolumeGainSettings(device_id_str, device);
150 209
151 // TODO(jennyz, rkc): Return a meaningful input gain default value, when 210 // TODO(jennyz, rkc): Return a meaningful input gain default value, when
152 // cras has added support for normalizing input gain range. 211 // cras has added support for normalizing input gain range.
153 double value = device.is_input ? 212 double value = device.is_input ?
154 0.0 : GetDeviceDefaultOutputVolume(device); 213 0.0 : GetDeviceDefaultOutputVolume(device);
155 // TODO(rkc): The above code is completely ignored since we 'always' have a 214 // TODO(rkc): The above code is completely ignored since we 'always' have a
156 // default pref value. Fix this. http://crbug.com/442489 215 // default pref value. Fix this. http://crbug.com/442489
157 device_volume_settings_->GetDouble(device_id_str, &value); 216 device_volume_settings_->GetDouble(device_id_str, &value);
158 217
159 return value; 218 return value;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 252
194 void AudioDevicesPrefHandlerImpl::LoadDevicesMutePref() { 253 void AudioDevicesPrefHandlerImpl::LoadDevicesMutePref() {
195 const base::DictionaryValue* mute_prefs = 254 const base::DictionaryValue* mute_prefs =
196 local_state_->GetDictionary(prefs::kAudioDevicesMute); 255 local_state_->GetDictionary(prefs::kAudioDevicesMute);
197 if (mute_prefs) 256 if (mute_prefs)
198 device_mute_settings_.reset(mute_prefs->DeepCopy()); 257 device_mute_settings_.reset(mute_prefs->DeepCopy());
199 } 258 }
200 259
201 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() { 260 void AudioDevicesPrefHandlerImpl::SaveDevicesMutePref() {
202 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute); 261 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesMute);
262 dict_update->Clear();
203 dict_update->MergeDictionary(device_mute_settings_.get()); 263 dict_update->MergeDictionary(device_mute_settings_.get());
204 } 264 }
205 265
206 void AudioDevicesPrefHandlerImpl::LoadDevicesVolumePref() { 266 void AudioDevicesPrefHandlerImpl::LoadDevicesVolumePref() {
207 const base::DictionaryValue* volume_prefs = 267 const base::DictionaryValue* volume_prefs =
208 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent); 268 local_state_->GetDictionary(prefs::kAudioDevicesVolumePercent);
209 if (volume_prefs) 269 if (volume_prefs)
210 device_volume_settings_.reset(volume_prefs->DeepCopy()); 270 device_volume_settings_.reset(volume_prefs->DeepCopy());
211 } 271 }
212 272
213 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() { 273 void AudioDevicesPrefHandlerImpl::SaveDevicesVolumePref() {
214 DictionaryPrefUpdate dict_update(local_state_, 274 DictionaryPrefUpdate dict_update(local_state_,
215 prefs::kAudioDevicesVolumePercent); 275 prefs::kAudioDevicesVolumePercent);
276 dict_update->Clear();
216 dict_update->MergeDictionary(device_volume_settings_.get()); 277 dict_update->MergeDictionary(device_volume_settings_.get());
217 } 278 }
218 279
219 void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() { 280 void AudioDevicesPrefHandlerImpl::LoadDevicesStatePref() {
220 const base::DictionaryValue* state_prefs = 281 const base::DictionaryValue* state_prefs =
221 local_state_->GetDictionary(prefs::kAudioDevicesState); 282 local_state_->GetDictionary(prefs::kAudioDevicesState);
222 if (state_prefs) 283 if (state_prefs)
223 device_state_settings_.reset(state_prefs->DeepCopy()); 284 device_state_settings_.reset(state_prefs->DeepCopy());
224 } 285 }
225 286
226 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() { 287 void AudioDevicesPrefHandlerImpl::SaveDevicesStatePref() {
227 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesState); 288 DictionaryPrefUpdate dict_update(local_state_, prefs::kAudioDevicesState);
228 base::DictionaryValue::Iterator it(*device_state_settings_); 289 dict_update->Clear();
229 dict_update->MergeDictionary(device_state_settings_.get()); 290 dict_update->MergeDictionary(device_state_settings_.get());
230 } 291 }
231 292
293 bool AudioDevicesPrefHandlerImpl::MigrateDevicesStatePref(
294 const std::string& device_key,
295 const AudioDevice& device) {
296 if (!MigrateDeviceIdInSettings(device_state_settings_.get(), device_key,
297 device)) {
298 return false;
299 }
300
301 SaveDevicesStatePref();
302 return true;
303 }
304
232 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings( 305 void AudioDevicesPrefHandlerImpl::MigrateDeviceMuteSettings(
233 const std::string& active_device) { 306 const std::string& device_key,
234 int old_mute = local_state_->GetInteger(prefs::kAudioMute); 307 const AudioDevice& device) {
235 device_mute_settings_->SetInteger(active_device, old_mute); 308 if (!MigrateDeviceIdInSettings(device_mute_settings_.get(), device_key,
309 device)) {
310 // If there was no recorded value for deprecated device ID, use value from
311 // global mute pref.
312 int old_mute = local_state_->GetInteger(prefs::kAudioMute);
313 device_mute_settings_->SetInteger(device_key, old_mute);
314 }
236 SaveDevicesMutePref(); 315 SaveDevicesMutePref();
237 } 316 }
238 317
239 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeSettings( 318 void AudioDevicesPrefHandlerImpl::MigrateDeviceVolumeGainSettings(
240 const std::string& active_device) { 319 const std::string& device_key,
241 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent); 320 const AudioDevice& device) {
242 device_volume_settings_->SetDouble(active_device, old_volume); 321 if (!MigrateDeviceIdInSettings(device_volume_settings_.get(), device_key,
322 device)) {
323 // If there was no recorded value for deprecated device ID, use value from
324 // global vloume pref.
325 double old_volume = local_state_->GetDouble(prefs::kAudioVolumePercent);
326 device_volume_settings_->SetDouble(device_key, old_volume);
327 }
243 SaveDevicesVolumePref(); 328 SaveDevicesVolumePref();
244 } 329 }
245 330
246 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() { 331 void AudioDevicesPrefHandlerImpl::NotifyAudioPolicyChange() {
247 for (auto& observer : observers_) 332 for (auto& observer : observers_)
248 observer.OnAudioPolicyPrefChanged(); 333 observer.OnAudioPolicyPrefChanged();
249 } 334 }
250 335
251 // static 336 // static
252 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) { 337 void AudioDevicesPrefHandlerImpl::RegisterPrefs(PrefRegistrySimple* registry) {
253 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent); 338 registry->RegisterDictionaryPref(prefs::kAudioDevicesVolumePercent);
254 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute); 339 registry->RegisterDictionaryPref(prefs::kAudioDevicesMute);
255 registry->RegisterDictionaryPref(prefs::kAudioDevicesState); 340 registry->RegisterDictionaryPref(prefs::kAudioDevicesState);
256 341
257 // Register the prefs backing the audio muting policies. 342 // Register the prefs backing the audio muting policies.
258 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome 343 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome
259 // media system. 344 // media system.
260 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true); 345 registry->RegisterBooleanPref(prefs::kAudioOutputAllowed, true);
261 346
262 // Register the legacy audio prefs for migration. 347 // Register the legacy audio prefs for migration.
263 registry->RegisterDoublePref(prefs::kAudioVolumePercent, 348 registry->RegisterDoublePref(prefs::kAudioVolumePercent,
264 kDefaultOutputVolumePercent); 349 kDefaultOutputVolumePercent);
265 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff); 350 registry->RegisterIntegerPref(prefs::kAudioMute, kPrefMuteOff);
266 } 351 }
267 352
268 } // namespace chromeos 353 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698