Add systemPreferences.getUserDefault
authorCheng Zhao <zcbenz@gmail.com>
Mon, 25 Apr 2016 06:35:52 +0000 (15:35 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 25 Apr 2016 06:35:52 +0000 (15:35 +0900)
atom/browser/api/atom_api_system_preferences.cc
atom/browser/api/atom_api_system_preferences.h
atom/browser/api/atom_api_system_preferences_mac.mm

index 86d3484..b8c6654 100644 (file)
@@ -52,6 +52,7 @@ void SystemPreferences::BuildPrototype(
                  &SystemPreferences::SubscribeNotification)
       .SetMethod("unsubscribeNotification",
                  &SystemPreferences::UnsubscribeNotification)
+      .SetMethod("getUserDefault", &SystemPreferences::GetUserDefault)
 #endif
       .SetMethod("isDarkMode", &SystemPreferences::IsDarkMode);
 }
index 6fd7ca9..fed1c52 100644 (file)
@@ -28,6 +28,8 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
   int SubscribeNotification(const std::string& name,
                             const base::Closure& callback);
   void UnsubscribeNotification(int id);
+  v8::Local<v8::Value> GetUserDefault(const std::string& name,
+                                      const std::string& type);
 #endif
   bool IsDarkMode();
 
index 39eb7e6..2d12b27 100644 (file)
@@ -8,7 +8,9 @@
 
 #import <Cocoa/Cocoa.h>
 
+#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<v8::Value> 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"];