Merge pull request #7993 from electron/get-user-default-crash

Prevent systemPreferences.getUserDefault crash for missing arrays/dictionaries
This commit is contained in:
Kevin Sawicki 2016-11-21 09:50:37 -08:00 committed by GitHub
commit 82d17cb05d
2 changed files with 29 additions and 11 deletions

View file

@ -128,11 +128,17 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
return mate::ConvertToV8(isolate(), return mate::ConvertToV8(isolate(),
net::GURLWithNSURL([defaults URLForKey:key])); net::GURLWithNSURL([defaults URLForKey:key]));
} else if (type == "array") { } else if (type == "array") {
return mate::ConvertToV8(isolate(), std::unique_ptr<base::ListValue> list =
*NSArrayToListValue([defaults arrayForKey:key])); NSArrayToListValue([defaults arrayForKey:key]);
if (list == nullptr)
list.reset(new base::ListValue());
return mate::ConvertToV8(isolate(), *list);
} else if (type == "dictionary") { } else if (type == "dictionary") {
return mate::ConvertToV8(isolate(), std::unique_ptr<base::DictionaryValue> dictionary =
*NSDictionaryToDictionaryValue([defaults dictionaryForKey:key])); NSDictionaryToDictionaryValue([defaults dictionaryForKey:key]);
if (dictionary == nullptr)
dictionary.reset(new base::DictionaryValue());
return mate::ConvertToV8(isolate(), *dictionary);
} else { } else {
return v8::Undefined(isolate()); return v8::Undefined(isolate());
} }

View file

@ -37,14 +37,26 @@ describe('systemPreferences module', function () {
} }
it('returns values for known user defaults', function () { it('returns values for known user defaults', function () {
let locale = systemPreferences.getUserDefault('AppleLocale', 'string') const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.notEqual(locale, null) assert.equal(typeof locale, 'string')
assert(locale.length > 0) assert(locale.length > 0)
let languages = systemPreferences.getUserDefault('AppleLanguages', 'array') const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert.notEqual(languages, null) assert(Array.isArray(languages))
assert(languages.length > 0) assert(languages.length > 0)
}) })
it('returns values for unknown user defaults', function () {
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype'), undefined)
assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array'), [])
assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary'), {})
})
}) })
describe('systemPreferences.isInvertedColorScheme()', function () { describe('systemPreferences.isInvertedColorScheme()', function () {