You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by mm...@apache.org on 2013/02/12 17:21:25 UTC

ios commit: [ios] CB-2239: Support multiple plugin result values.

Updated Branches:
  refs/heads/multipart_plugin_result [created] a394533f1


[ios] CB-2239: Support multiple plugin result values.

This allows a plugin to return multiple cordova exec bridge supported
plugin results.  The difference between a multipart message result and a
normal NSArray, is that a multipart message is "massaged" across the
bridge while an NSArray is just sent as-is.  This means that a multipart
message can include ArrayBuffers, while a normal NSArray plugin result
will not be scanned at all (possibly better perf).


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/a394533f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/a394533f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/a394533f

Branch: refs/heads/multipart_plugin_result
Commit: a394533f144e12606b58a671d38d4d252f13d268
Parents: 6fe71ae
Author: Michal Mocny <mm...@gmail.com>
Authored: Mon Feb 11 16:56:18 2013 -0500
Committer: Michal Mocny <mm...@gmail.com>
Committed: Tue Feb 12 10:43:12 2013 -0500

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCommandDelegateImpl.m |   12 +----
 CordovaLib/Classes/CDVPluginResult.h        |    4 ++
 CordovaLib/Classes/CDVPluginResult.m        |   49 +++++++++++++++++++--
 3 files changed, 51 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/a394533f/CordovaLib/Classes/CDVCommandDelegateImpl.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandDelegateImpl.m b/CordovaLib/Classes/CDVCommandDelegateImpl.m
index 8845e40..0fabc76 100644
--- a/CordovaLib/Classes/CDVCommandDelegateImpl.m
+++ b/CordovaLib/Classes/CDVCommandDelegateImpl.m
@@ -84,15 +84,9 @@
     }
     int status = [result.status intValue];
     BOOL keepCallback = [result.keepCallback boolValue];
-    id message = result.message == nil ? [NSNull null] : result.message;
-
-    // Use an array to encode the message as JSON.
-    message = [NSArray arrayWithObject:message];
-    NSString* encodedMessage = [message JSONString];
-    // And then strip off the outer []s.
-    encodedMessage = [encodedMessage substringWithRange:NSMakeRange(1, [encodedMessage length] - 2)];
-    NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)",
-        callbackId, status, encodedMessage, keepCallback];
+    NSString* argumentsAsJSON = [result argumentsAsJSON];
+
+    NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d)", callbackId, status, argumentsAsJSON, keepCallback];
 
     [self evalJsHelper:js];
 }

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/a394533f/CordovaLib/Classes/CDVPluginResult.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPluginResult.h b/CordovaLib/Classes/CDVPluginResult.h
index 8683205..8393df2 100644
--- a/CordovaLib/Classes/CDVPluginResult.h
+++ b/CordovaLib/Classes/CDVPluginResult.h
@@ -47,6 +47,7 @@ typedef enum {
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsBool:(BOOL)theMessage;
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsDictionary:(NSDictionary*)theMessage;
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArrayBuffer:(NSData*)theMessage;
++ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsMultipart:(NSArray*)theMessages;
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageToErrorObject:(int)errorCode;
 
 + (void)setVerbose:(BOOL)verbose;
@@ -54,6 +55,9 @@ typedef enum {
 
 - (void)setKeepCallbackAsBool:(BOOL)bKeepCallback;
 
+- (NSString*)argumentsAsJSON;
+
+// These methods are used by the legacy plugin return result method
 - (NSString*)toJSONString;
 - (NSString*)toSuccessCallbackString:(NSString*)callbackId;
 - (NSString*)toErrorCallbackString:(NSString*)callbackId;

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/a394533f/CordovaLib/Classes/CDVPluginResult.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPluginResult.m b/CordovaLib/Classes/CDVPluginResult.m
index d9ba08f..a8f5689 100644
--- a/CordovaLib/Classes/CDVPluginResult.m
+++ b/CordovaLib/Classes/CDVPluginResult.m
@@ -33,6 +33,32 @@
 
 static NSArray* org_apache_cordova_CommandStatusMsgs;
 
+id messageFromArrayBuffer(NSData* data)
+{
+    return [NSDictionary dictionaryWithObjectsAndKeys:
+        @"ArrayBuffer", @"CDVType",
+        [data base64EncodedString], @"data",
+        nil];
+}
+
+id messageFromMultipart(NSArray* theMessages)
+{
+    NSMutableArray* messages = [NSMutableArray arrayWithArray:theMessages];
+
+    for (NSUInteger i = 0; i < messages.count; ++i) {
+        id message = [messages objectAtIndex:i];
+
+        if ([message isKindOfClass:[NSData class]]) {
+            [messages replaceObjectAtIndex:i withObject:messageFromArrayBuffer(message)];
+        }
+    }
+
+    return [NSDictionary dictionaryWithObjectsAndKeys:
+        @"MultiPart", @"CDVType",
+        messages, @"messages",
+        nil];
+}
+
 + (void)initialize
 {
     org_apache_cordova_CommandStatusMsgs = [[NSArray alloc] initWithObjects:@"No result",
@@ -101,12 +127,12 @@ static NSArray* org_apache_cordova_CommandStatusMsgs;
 
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsArrayBuffer:(NSData*)theMessage
 {
-    NSDictionary* arrDict = [NSDictionary dictionaryWithObjectsAndKeys:
-        @"ArrayBuffer", @"CDVType",
-        [theMessage base64EncodedString], @"data",
-        nil];
+    return [[self alloc] initWithStatus:statusOrdinal message:messageFromArrayBuffer(theMessage)];
+}
 
-    return [[self alloc] initWithStatus:statusOrdinal message:arrDict];
++ (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageAsMultipart:(NSArray*)theMessages
+{
+    return [[self alloc] initWithStatus:statusOrdinal message:messageFromMultipart(theMessages)];
 }
 
 + (CDVPluginResult*)resultWithStatus:(CDVCommandStatus)statusOrdinal messageToErrorObject:(int)errorCode
@@ -121,6 +147,19 @@ static NSArray* org_apache_cordova_CommandStatusMsgs;
     [self setKeepCallback:[NSNumber numberWithBool:bKeepCallback]];
 }
 
+- (NSString*)argumentsAsJSON
+{
+    id arguments = (self.message == nil ? [NSNull null] : self.message);
+    NSArray* argumentsWrappedInArray = [NSArray arrayWithObject:arguments];
+
+    NSString* argumentsJSON = [argumentsWrappedInArray JSONString];
+
+    argumentsJSON = [argumentsJSON substringWithRange:NSMakeRange(1, [argumentsJSON length] - 2)];
+
+    return argumentsJSON;
+}
+
+// These methods are used by the legacy plugin return result method
 - (NSString*)toJSONString
 {
     NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: