From aa1b8cd74be1bc65f80c332401dc21579829e8ab Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Wed, 1 Jun 2016 18:53:49 -0700 Subject: [PATCH 1/5] Expose the NSArrayToListValue converter. --- atom/browser/mac/dict_util.h | 4 +++- atom/browser/mac/dict_util.mm | 4 ---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/atom/browser/mac/dict_util.h b/atom/browser/mac/dict_util.h index 404636d1901a..3ffd8ba51006 100644 --- a/atom/browser/mac/dict_util.h +++ b/atom/browser/mac/dict_util.h @@ -10,7 +10,7 @@ #include "base/memory/scoped_ptr.h" namespace base { -class Value; +class ListValue; class DictionaryValue; } @@ -21,6 +21,8 @@ NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value); std::unique_ptr NSDictionaryToDictionaryValue( NSDictionary* dict); +std::unique_ptr NSArrayToListValue(NSArray* arr); + } // namespace atom #endif // ATOM_BROWSER_MAC_DICT_UTIL_H_ diff --git a/atom/browser/mac/dict_util.mm b/atom/browser/mac/dict_util.mm index 43b629cc32be..8692f001f6a8 100644 --- a/atom/browser/mac/dict_util.mm +++ b/atom/browser/mac/dict_util.mm @@ -10,8 +10,6 @@ namespace atom { -namespace { - std::unique_ptr NSArrayToListValue(NSArray* arr) { if (!arr) return nullptr; @@ -51,8 +49,6 @@ std::unique_ptr NSArrayToListValue(NSArray* arr) { return result; } -} // namespace - NSDictionary* DictionaryValueToNSDictionary(const base::DictionaryValue& value) { std::string json; if (!base::JSONWriter::Write(value, &json)) From 0230567891a7ba1850ff7901f5174c9b16b07464 Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Wed, 1 Jun 2016 18:55:20 -0700 Subject: [PATCH 2/5] Write a new method that returns prefs under the NSGlobalDomain. --- .../browser/api/atom_api_system_preferences.h | 1 + .../api/atom_api_system_preferences_mac.mm | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 7779ce007816..0234d8434408 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -39,6 +39,7 @@ class SystemPreferences : public mate::EventEmitter { void UnsubscribeNotification(int id); v8::Local GetUserDefault(const std::string& name, const std::string& type); + v8::Local GetGlobalDefault(const std::string& name); #endif bool IsDarkMode(); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index f0b48adf2564..2556fef35a65 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -9,6 +9,7 @@ #import #include "atom/browser/mac/dict_util.h" +#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" #include "base/strings/sys_string_conversions.h" #include "base/values.h" @@ -84,6 +85,26 @@ v8::Local SystemPreferences::GetUserDefault( } } +v8::Local SystemPreferences::GetGlobalDefault(const std::string& name) { + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + NSString* key = base::SysUTF8ToNSString(name); + + NSDictionary* globalPrefs = [defaults persistentDomainForName:NSGlobalDomain]; + id value = [globalPrefs objectForKey:key]; + + if ([value isKindOfClass:[NSString class]]) { + return mate::StringToV8(isolate(), base::SysNSStringToUTF8(value)); + } else if ([value isKindOfClass:[NSNumber class]]) { + return v8::Integer::New(isolate(), [value integerValue]); + } else if ([value isKindOfClass:[NSArray class]]) { + return mate::ConvertToV8(isolate(), *NSArrayToListValue(value)); + } else if ([value isKindOfClass:[NSDictionary class]]) { + return mate::ConvertToV8(isolate(), *NSDictionaryToDictionaryValue(value)); + } else { + return v8::Undefined(isolate()); + } +} + bool SystemPreferences::IsDarkMode() { NSString* mode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"]; From 587dd2fe51767ceb362cfb326921775e4b5d8315 Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Wed, 1 Jun 2016 18:56:25 -0700 Subject: [PATCH 3/5] Expose the method to JS and write a simple test. --- .../api/atom_api_system_preferences.cc | 1 + spec/api-system-preferences-spec.js | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 spec/api-system-preferences-spec.js diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 2b11aad25278..8a02859f2970 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -54,6 +54,7 @@ void SystemPreferences::BuildPrototype( .SetMethod("unsubscribeNotification", &SystemPreferences::UnsubscribeNotification) .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault) + .SetMethod("getGlobalDefault", &SystemPreferences::GetGlobalDefault) #endif .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode); } diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js new file mode 100644 index 000000000000..3cb0cd406660 --- /dev/null +++ b/spec/api-system-preferences-spec.js @@ -0,0 +1,25 @@ +const assert = require('assert') +const {remote} = require('electron') +const {systemPreferences} = remote + +describe('systemPreferences module', function () { + if (process.platform !== 'darwin') { + return + } + + it('returns user defaults', function () { + assert.notEqual(systemPreferences.getUserDefault('AppleInterfaceStyle', 'string'), null) + assert.notEqual(systemPreferences.getUserDefault('AppleAquaColorVariant', 'integer'), null) + }) + + it('returns defaults under the global domain', function () { + let locale = systemPreferences.getGlobalDefault('AppleLocale') + assert.notEqual(locale, null) + assert(locale.length > 0) + + let languages = systemPreferences.getGlobalDefault('AppleLanguages') + assert.notEqual(languages, null) + assert(languages.length > 0) + }) + +}) From fa3b17444f7b11d1aa687ea23c98937419e5193f Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Wed, 1 Jun 2016 19:21:17 -0700 Subject: [PATCH 4/5] All we really need to do is make getUserDefault support array / object types. --- .../api/atom_api_system_preferences.cc | 1 - .../browser/api/atom_api_system_preferences.h | 1 - .../api/atom_api_system_preferences_mac.mm | 34 ++++++------------- docs/api/system-preferences.md | 5 ++- spec/api-system-preferences-spec.js | 11 ++---- 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 8a02859f2970..2b11aad25278 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -54,7 +54,6 @@ void SystemPreferences::BuildPrototype( .SetMethod("unsubscribeNotification", &SystemPreferences::UnsubscribeNotification) .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault) - .SetMethod("getGlobalDefault", &SystemPreferences::GetGlobalDefault) #endif .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode); } diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 0234d8434408..7779ce007816 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -39,7 +39,6 @@ class SystemPreferences : public mate::EventEmitter { void UnsubscribeNotification(int id); v8::Local GetUserDefault(const std::string& name, const std::string& type); - v8::Local GetGlobalDefault(const std::string& name); #endif bool IsDarkMode(); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 2556fef35a65..72011500bd04 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -67,8 +67,8 @@ v8::Local SystemPreferences::GetUserDefault( NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSString* key = base::SysUTF8ToNSString(name); if (type == "string") { - return mate::StringToV8( - isolate(), base::SysNSStringToUTF8([defaults stringForKey:key])); + return mate::StringToV8(isolate(), + base::SysNSStringToUTF8([defaults stringForKey:key])); } else if (type == "boolean") { return v8::Boolean::New(isolate(), [defaults boolForKey:key]); } else if (type == "float") { @@ -78,28 +78,14 @@ v8::Local SystemPreferences::GetUserDefault( } else if (type == "double") { return v8::Number::New(isolate(), [defaults doubleForKey:key]); } else if (type == "url") { - return mate::ConvertToV8( - isolate(), net::GURLWithNSURL([defaults URLForKey:key])); - } else { - return v8::Undefined(isolate()); - } -} - -v8::Local SystemPreferences::GetGlobalDefault(const std::string& name) { - NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; - NSString* key = base::SysUTF8ToNSString(name); - - NSDictionary* globalPrefs = [defaults persistentDomainForName:NSGlobalDomain]; - id value = [globalPrefs objectForKey:key]; - - if ([value isKindOfClass:[NSString class]]) { - return mate::StringToV8(isolate(), base::SysNSStringToUTF8(value)); - } else if ([value isKindOfClass:[NSNumber class]]) { - return v8::Integer::New(isolate(), [value integerValue]); - } else if ([value isKindOfClass:[NSArray class]]) { - return mate::ConvertToV8(isolate(), *NSArrayToListValue(value)); - } else if ([value isKindOfClass:[NSDictionary class]]) { - return mate::ConvertToV8(isolate(), *NSDictionaryToDictionaryValue(value)); + return mate::ConvertToV8(isolate(), + net::GURLWithNSURL([defaults URLForKey:key])); + } else if (type == "array") { + return mate::ConvertToV8(isolate(), + *NSArrayToListValue([defaults arrayForKey:key])); + } else if (type == "dictionary") { + return mate::ConvertToV8(isolate(), + *NSDictionaryToDictionaryValue([defaults dictionaryForKey:key])); } else { return v8::Undefined(isolate()); } diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 8f4dc8993852..7b2f0724a4aa 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -39,7 +39,7 @@ Removes the subscriber with `id`. * `key` String * `type` String - Can be `string`, `boolean`, `integer`, `float`, `double`, - `url`. + `url`, `array`, `dictionary` Get the value of `key` in system preferences. @@ -50,6 +50,9 @@ are: * `AppleAquaColorVariant: integer` * `AppleHighlightColor: string` * `AppleShowScrollBars: string` +* `NSNavRecentPlaces: array` +* `NSPreferredWebServices: dictionary` +* `NSUserDictionaryReplacementItems: array` ### `systemPreferences.isAeroGlassEnabled()` _Windows_ diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index 3cb0cd406660..220ff92709b4 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -7,17 +7,12 @@ describe('systemPreferences module', function () { return } - it('returns user defaults', function () { - assert.notEqual(systemPreferences.getUserDefault('AppleInterfaceStyle', 'string'), null) - assert.notEqual(systemPreferences.getUserDefault('AppleAquaColorVariant', 'integer'), null) - }) - - it('returns defaults under the global domain', function () { - let locale = systemPreferences.getGlobalDefault('AppleLocale') + it('returns values for known user defaults', function () { + let locale = systemPreferences.getUserDefault('AppleLocale', 'string') assert.notEqual(locale, null) assert(locale.length > 0) - let languages = systemPreferences.getGlobalDefault('AppleLanguages') + let languages = systemPreferences.getUserDefault('AppleLanguages', 'array') assert.notEqual(languages, null) assert(languages.length > 0) }) From e377298cd2e6184ef57bc5f4cfaa939b9b571ff4 Mon Sep 17 00:00:00 2001 From: Charlie Hess Date: Wed, 1 Jun 2016 19:29:24 -0700 Subject: [PATCH 5/5] Be consistent in spec organization. --- spec/api-system-preferences-spec.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/spec/api-system-preferences-spec.js b/spec/api-system-preferences-spec.js index 220ff92709b4..333c4dbacb4e 100644 --- a/spec/api-system-preferences-spec.js +++ b/spec/api-system-preferences-spec.js @@ -7,14 +7,16 @@ describe('systemPreferences module', function () { return } - it('returns values for known user defaults', function () { - let locale = systemPreferences.getUserDefault('AppleLocale', 'string') - assert.notEqual(locale, null) - assert(locale.length > 0) + describe('systemPreferences.getUserDefault(key, type)', function () { + it('returns values for known user defaults', function () { + let locale = systemPreferences.getUserDefault('AppleLocale', 'string') + assert.notEqual(locale, null) + assert(locale.length > 0) - let languages = systemPreferences.getUserDefault('AppleLanguages', 'array') - assert.notEqual(languages, null) - assert(languages.length > 0) + let languages = systemPreferences.getUserDefault('AppleLanguages', 'array') + assert.notEqual(languages, null) + assert(languages.length > 0) + }) }) })