Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 83905E3BC for ; Mon, 14 Jan 2013 21:40:44 +0000 (UTC) Received: (qmail 82931 invoked by uid 500); 14 Jan 2013 21:40:44 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 82909 invoked by uid 500); 14 Jan 2013 21:40:44 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 82901 invoked by uid 99); 14 Jan 2013 21:40:44 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jan 2013 21:40:44 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id B98971D50F; Mon, 14 Jan 2013 21:40:43 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mmocny@apache.org To: commits@cordova.apache.org X-Mailer: ASF-Git Admin Mailer Subject: js commit: [ios] CB-2189: Implement ArrayBuffer native->js. Message-Id: <20130114214043.B98971D50F@tyr.zones.apache.org> Date: Mon, 14 Jan 2013 21:40:43 +0000 (UTC) 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 Authored: Mon Jan 14 14:43:39 2013 -0500 Committer: Michal Mocny 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); }); };