Add some shady methods to get V8 objects or arrays from NSDictionary or NSArray.
authorCharlie Hess <charlie@slack-corp.com>
Mon, 2 May 2016 21:45:59 +0000 (14:45 -0700)
committerCharlie Hess <charlie@slack-corp.com>
Mon, 2 May 2016 21:45:59 +0000 (14:45 -0700)
atom/browser/mac/atom_application_delegate.mm
atom/browser/mac/mac_native_converter.h [new file with mode: 0644]
atom/browser/mac/mac_native_converter.mm [new file with mode: 0644]
filenames.gypi

index 33aa47c..f08cc84 100644 (file)
@@ -63,9 +63,9 @@
 continueUserActivity:(NSUserActivity *)userActivity
   restorationHandler:(void (^)(NSArray *restorableObjects))restorationHandler {
   std::string activity_type(base::SysNSStringToUTF8(userActivity.activityType));
-  
+
   std::map<std::string, std::string> user_info;
-  
+
   NSArray* keys = [userActivity.userInfo allKeys];
   for (NSString* key in keys)
   {
diff --git a/atom/browser/mac/mac_native_converter.h b/atom/browser/mac/mac_native_converter.h
new file mode 100644 (file)
index 0000000..ac1a9a6
--- /dev/null
@@ -0,0 +1,11 @@
+#import <Cocoa/Cocoa.h>
+
+#include "base/values.h"
+#include "base/strings/sys_string_conversions.h"
+
+@interface MacNativeConverter : NSObject
+
+- (base::ListValue*)arrayToV8:(NSArray*)nsArray;
+- (base::DictionaryValue*)dictionaryToV8:(NSDictionary*)nsDictionary;
+
+@end
diff --git a/atom/browser/mac/mac_native_converter.mm b/atom/browser/mac/mac_native_converter.mm
new file mode 100644 (file)
index 0000000..6324403
--- /dev/null
@@ -0,0 +1,46 @@
+#import "atom/browser/mac/mac_native_converter.h"
+
+@implementation MacNativeConverter
+
+- (base::ListValue*)arrayToV8:(NSArray*)nsArray {
+  scoped_ptr<base::ListValue> list(new base::ListValue);
+
+  for (id value in nsArray) {
+    if ([value isKindOfClass:[NSArray class]]) {
+      list->Append([self arrayToV8:value]);
+    } else if ([value isKindOfClass:[NSDictionary class]]) {
+      list->Append([self dictionaryToV8:value]);
+    } else if ([value isKindOfClass:[NSString class]]) {
+      list->AppendString(base::SysNSStringToUTF8(value));
+    } else if ([value isKindOfClass:[NSNumber class]]) {
+      list->AppendInteger(((NSNumber* )value).intValue);
+    }
+  }
+
+  return list.get();
+}
+
+- (base::DictionaryValue*)dictionaryToV8:(NSDictionary*)nsDictionary {
+  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
+
+  NSEnumerator *it = [nsDictionary keyEnumerator];
+  while (NSString *key = [it nextObject]) {
+    id value = [nsDictionary objectForKey:key];
+
+    std::string key_str(base::SysNSStringToUTF8(key));
+
+    if ([value isKindOfClass:[NSArray class]]) {
+      dict->Set(key_str, [self arrayToV8:value]);
+    } else if ([value isKindOfClass:[NSDictionary class]]) {
+      dict->Set(key_str, [self dictionaryToV8:value]);
+    } else if ([value isKindOfClass:[NSString class]]) {
+      dict->SetString(key_str, base::SysNSStringToUTF8(value));
+    } else if ([value isKindOfClass:[NSNumber class]]) {
+      dict->SetInteger(key_str, ((NSNumber* )value).intValue);
+    }
+  }
+
+  return dict.get();
+}
+
+@end
index 783b5cb..033d495 100644 (file)
       'atom/browser/mac/atom_application.mm',
       'atom/browser/mac/atom_application_delegate.h',
       'atom/browser/mac/atom_application_delegate.mm',
+      'atom/browser/mac/mac_native_converter.h',
+      'atom/browser/mac/mac_native_converter.mm',
       'atom/browser/native_window.cc',
       'atom/browser/native_window.h',
       'atom/browser/native_window_views_win.cc',