From 8b447b929169a53566336c2150a692743ffc2d75 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 12 Dec 2017 13:08:09 -0500 Subject: [PATCH 1/4] fixing error in registerDefaults --- .../api/atom_api_system_preferences.cc | 1 + .../browser/api/atom_api_system_preferences.h | 1 + .../api/atom_api_system_preferences_mac.mm | 15 +++++++ docs/api/system-preferences.md | 8 ++++ spec/api-system-preferences-spec.js | 44 ++++++++++++++++++- 5 files changed, 68 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 74d6d03ce7cb..82c3b2e1017f 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -65,6 +65,7 @@ void SystemPreferences::BuildPrototype( &SystemPreferences::SubscribeLocalNotification) .SetMethod("unsubscribeLocalNotification", &SystemPreferences::UnsubscribeLocalNotification) + .SetMethod("registerDefaults", &SystemPreferences::RegisterDefaults) .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault) .SetMethod("setUserDefault", &SystemPreferences::SetUserDefault) .SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault) diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index ea5daed94a5f..4e432d7e51e1 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -73,6 +73,7 @@ class SystemPreferences : public mate::EventEmitter void UnsubscribeLocalNotification(int request_id); v8::Local GetUserDefault(const std::string& name, const std::string& type); + void RegisterDefaults(mate::Arguments* args); void SetUserDefault(const std::string& name, const std::string& type, mate::Arguments* args); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 58e9b848e25c..64732c37258a 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -144,6 +144,21 @@ v8::Local SystemPreferences::GetUserDefault( } } +void SystemPreferences::RegisterDefaults(mate::Arguments* args) { + base::DictionaryValue value; + + if(!args->GetNext(&value)) { + args->ThrowError("Invalid userDefault data provided"); + } else { + @try { + NSDictionary* dict = DictionaryValueToNSDictionary(value); + [[NSUserDefaults standardUserDefaults] registerDefaults:dict]; + } @catch (NSException* exception) { + args->ThrowError("Invalid userDefault data provided"); + } + } +} + void SystemPreferences::SetUserDefault(const std::string& name, const std::string& type, mate::Arguments* args) { diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 1fe53eed360b..9bf148466096 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -106,6 +106,14 @@ This is necessary for events such as `NSUserDefaultsDidChangeNotification`. Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificationCenter`. +### `systemPreferences.registerDefaults(defaults)` _macOS_ + +* `defaults` Object - a dictionary of (`key: value`) user defaults + * `key` String + * `value` Any + +Add the specified defaults to your application's `NSUserDefaults`. + ### `systemPreferences.getUserDefault(key, type)` _macOS_ * `key` String diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index d6513b364b33..27955b3d7055 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -2,7 +2,7 @@ const assert = require('assert') const {remote} = require('electron') const {systemPreferences} = remote -describe('systemPreferences module', () => { +describe.only('systemPreferences module', () => { describe('systemPreferences.getAccentColor', () => { before(function () { if (process.platform !== 'win32') { @@ -35,6 +35,48 @@ describe('systemPreferences module', () => { }) }) + describe('systemPreferences.registerDefaults(defaults)', () => { + before(function () { + if (process.platform !== 'darwin') { + this.skip() + } + }) + + it('registers defaults', () => { + const defaultsMap = [ + { key: 'one', type: 'string', value: 'ONE' }, + { key: 'two', value: 2, type: 'integer' }, + { key: 'three', value: [1, 2, 3], type: 'array' } + ] + + const defaultsDict = {} + defaultsMap.forEach(row => { defaultsDict[row.key] = row.value }) + + systemPreferences.registerDefaults(defaultsDict) + + for (const userDefault of defaultsMap) { + const { key, value: expectedValue, type } = userDefault + const actualValue = systemPreferences.getUserDefault(key, type) + assert.deepEqual(actualValue, expectedValue) + } + }) + + it('throws when bad defaults are passed', () => { + const badDefaults = [ + 1, + null, + new Date(), + { 'one': null } + ] + + for (const badDefault of badDefaults) { + assert.throws(() => { + systemPreferences.registerDefaults(badDefault) + }, 'Invalid userDefault data provided') + } + }) + }) + describe('systemPreferences.getUserDefault(key, type)', () => { before(function () { if (process.platform !== 'darwin') { From 6252ce466ce900e0c68d36eca76d9f401039dfdb Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 12 Dec 2017 13:39:38 -0500 Subject: [PATCH 2/4] remove failing badDefault --- atom/browser/api/atom_api_system_preferences_mac.mm | 7 +++++++ spec/api-system-preferences-spec.js | 3 +-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 64732c37258a..b078db585d57 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -152,6 +152,13 @@ void SystemPreferences::RegisterDefaults(mate::Arguments* args) { } else { @try { NSDictionary* dict = DictionaryValueToNSDictionary(value); + // for (id key in dict) { + // id value = [dict objectForKey:key]; + // if (value == nil) { + // printf("HELLO\n"); + // args->ThrowError("Invalid userDefault data provided"); + // } + // } [[NSUserDefaults standardUserDefaults] registerDefaults:dict]; } @catch (NSException* exception) { args->ThrowError("Invalid userDefault data provided"); diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index 27955b3d7055..36902ffaf18f 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -2,7 +2,7 @@ const assert = require('assert') const {remote} = require('electron') const {systemPreferences} = remote -describe.only('systemPreferences module', () => { +describe('systemPreferences module', () => { describe('systemPreferences.getAccentColor', () => { before(function () { if (process.platform !== 'win32') { @@ -66,7 +66,6 @@ describe.only('systemPreferences module', () => { 1, null, new Date(), - { 'one': null } ] for (const badDefault of badDefaults) { From 3fc844c15aeaab16bb7566934bd1865793c6cf04 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 13 Dec 2017 14:02:43 -0500 Subject: [PATCH 3/4] add loop to catch null values --- .../browser/api/atom_api_system_preferences_mac.mm | 14 +++++++------- spec/api-system-preferences-spec.js | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index b078db585d57..8f7ce7f144c5 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -152,13 +152,13 @@ void SystemPreferences::RegisterDefaults(mate::Arguments* args) { } else { @try { NSDictionary* dict = DictionaryValueToNSDictionary(value); - // for (id key in dict) { - // id value = [dict objectForKey:key]; - // if (value == nil) { - // printf("HELLO\n"); - // args->ThrowError("Invalid userDefault data provided"); - // } - // } + for (id key in dict) { + id value = [dict objectForKey:key]; + if ([value isKindOfClass:[NSNull class]] || value == nil) { + args->ThrowError("Invalid userDefault data provided"); + return; + } + } [[NSUserDefaults standardUserDefaults] registerDefaults:dict]; } @catch (NSException* exception) { args->ThrowError("Invalid userDefault data provided"); diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index 36902ffaf18f..96e52979fdd5 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -66,6 +66,7 @@ describe('systemPreferences module', () => { 1, null, new Date(), + { 'one': null } ] for (const badDefault of badDefaults) { From 26ad37c4d1668e202f849a7ce8f829d57d40331e Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 13 Dec 2017 16:02:15 -0500 Subject: [PATCH 4/4] remove key and value bullts --- docs/api/system-preferences.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 9bf148466096..5f6f004a3c1e 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -108,9 +108,7 @@ Same as `unsubscribeNotification`, but removes the subscriber from `NSNotificati ### `systemPreferences.registerDefaults(defaults)` _macOS_ -* `defaults` Object - a dictionary of (`key: value`) user defaults - * `key` String - * `value` Any +* `defaults` Object - a dictionary of (`key: value`) user defaults Add the specified defaults to your application's `NSUserDefaults`.