From 067e9c1a850f077943c66a604085c9b38861bcc4 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 25 Apr 2016 15:35:52 +0900 Subject: [PATCH] Add systemPreferences.getUserDefault --- .../api/atom_api_system_preferences.cc | 1 + .../browser/api/atom_api_system_preferences.h | 2 ++ .../api/atom_api_system_preferences_mac.mm | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index 86d3484b6705..b8c665456ade 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -52,6 +52,7 @@ void SystemPreferences::BuildPrototype( &SystemPreferences::SubscribeNotification) .SetMethod("unsubscribeNotification", &SystemPreferences::UnsubscribeNotification) + .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault) #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 6fd7ca922dc8..fed1c52247b7 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -28,6 +28,8 @@ class SystemPreferences : public mate::EventEmitter { int SubscribeNotification(const std::string& name, const base::Closure& callback); void UnsubscribeNotification(int id); + v8::Local GetUserDefault(const std::string& name, + const std::string& type); #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 39eb7e6ede82..2d12b278ae92 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -8,7 +8,9 @@ #import +#include "atom/common/native_mate_converters/gurl_converter.h" #include "base/strings/sys_string_conversions.h" +#include "net/base/mac/url_conversions.h" namespace atom { @@ -47,6 +49,29 @@ void SystemPreferences::UnsubscribeNotification(int request_id) { } } +v8::Local SystemPreferences::GetUserDefault( + const std::string& name, const std::string& type) { + NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; + NSString* key = base::SysUTF8ToNSString(name); + if (type == "string") { + 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") { + return v8::Number::New(isolate(), [defaults floatForKey:key]); + } else if (type == "integer") { + return v8::Integer::New(isolate(), [defaults integerForKey:key]); + } 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()); + } +} + bool SystemPreferences::IsDarkMode() { NSString* mode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];