cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmo...@apache.org
Subject js commit: [ios] CB-2189: Implement ArrayBuffer native->js.
Date Mon, 14 Jan 2013 21:40:43 GMT
Updated Branches:
  refs/heads/master d41650bec -> 099a51d15


[ios] CB-2189: Implement ArrayBuffer native->js.


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

Branch: refs/heads/master
Commit: 099a51d157bf9bfcb112075f5406453504f40d72
Parents: d41650b
Author: Michal Mocny <mmocny@gmail.com>
Authored: Mon Jan 14 14:43:39 2013 -0500
Committer: Michal Mocny <mmocny@gmail.com>
Committed: Mon Jan 14 16:20:49 2013 -0500

----------------------------------------------------------------------
 lib/ios/exec.js |   56 +++++++++++++++++++++++++++++++++-----------------
 1 files changed, 37 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/099a51d1/lib/ios/exec.js
----------------------------------------------------------------------
diff --git a/lib/ios/exec.js b/lib/ios/exec.js
index b8bc850..f8bff52 100644
--- a/lib/ios/exec.js
+++ b/lib/ios/exec.js
@@ -64,6 +64,41 @@ function shouldBundleCommandJson() {
     return false;
 }
 
+function massageArgsJsToNative(args) {
+    var encodeArrayBufferAs8bitString = function(ab) {
+        return String.fromCharCode.apply(null, new Uint8Array(ab));
+    };
+    var encodeArrayBufferAsBase64 = function(ab) {
+        return window.btoa(encodeArrayBufferAs8bitString(ab));
+    };
+    args.forEach(function(arg, i) {
+        if (arg instanceof ArrayBuffer) {
+            args[i] = {
+                'CDVType': 'ArrayBuffer',
+                'data': encodeArrayBufferAsBase64(arg)
+            };
+        } // else if (arg instanceof OTHER_TYPE)
+    });
+    return args;
+}
+
+function massagePayloadNativeToJs(payload) {
+    if (payload && payload.hasOwnProperty('CDVType') && payload.CDVType ==
'ArrayBuffer') {
+        var stringToArrayBuffer = function(str) {
+            var ret = new Uint8Array(str.length);
+            for (var i = 0; i < str.length; i++) {
+                ret[i] = str.charCodeAt(i);
+            }
+            return ret.buffer;
+        };
+        var base64ToArrayBuffer = function(b64) {
+            return stringToArrayBuffer(atob(b64));
+        };
+        payload = base64ToArrayBuffer(payload.data);
+    }
+    return payload;
+}
+
 function iOSExec() {
     // XHR mode does not work on iOS 4.2, so default to IFRAME_NAV for such devices.
     // XHR mode's main advantage is working around a bug in -webkit-scroll, which
@@ -103,25 +138,7 @@ function iOSExec() {
             {success:successCallback, fail:failCallback};
     }
 
-    // Binary!
-    var encodeArrayBufferAsBase64 = function(ab) {
-        var arr = new Uint8Array(ab);
-        var binary = '';
-        var len = arr.length;
-        for (var i = 0; i < len; i++) {
-            binary += String.fromCharCode(arr[i]);
-        }
-        return window.btoa(binary);
-    };
-    actionArgs.forEach(function(arg, i) {
-        if (Object.prototype.toString.call(arg) != Object.prototype.toString.call(new ArrayBuffer()))
{
-            return;
-        }
-        actionArgs[i] = {
-            'CDVType': 'ArrayBuffer',
-            'data': encodeArrayBufferAsBase64(arg)
-        };
-    });
+    actionArgs = massageArgsJsToNative(actionArgs);
 
     var command = [callbackId, service, action, actionArgs];
 
@@ -188,6 +205,7 @@ iOSExec.nativeFetchMessages = function() {
 iOSExec.nativeCallback = function(callbackId, status, payload, keepCallback) {
     return iOSExec.nativeEvalAndFetch(function() {
         var success = status === 0 || status === 1;
+        payload = massagePayloadNativeToJs(payload);
         cordova.callbackFromNative(callbackId, success, status, payload, keepCallback);
     });
 };


Mime
View raw message