123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- var cordova = require('cordova'),
- utils = require('cordova/utils'),
- base64 = require('cordova/base64'),
- execIframe,
- commandQueue = [],
- isInContextOfEvalJs = 0,
- failSafeTimerId = 0;
- function massageArgsJsToNative(args) {
- if (!args || utils.typeName(args) != 'Array') {
- return args;
- }
- var ret = [];
- args.forEach(function(arg, i) {
- if (utils.typeName(arg) == 'ArrayBuffer') {
- ret.push({
- 'CDVType': 'ArrayBuffer',
- 'data': base64.fromArrayBuffer(arg)
- });
- } else {
- ret.push(arg);
- }
- });
- return ret;
- }
- function massageMessageNativeToJs(message) {
- if (message.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));
- };
- message = base64ToArrayBuffer(message.data);
- }
- return message;
- }
- function convertMessageToArgsNativeToJs(message) {
- var args = [];
- if (!message || !message.hasOwnProperty('CDVType')) {
- args.push(message);
- } else if (message.CDVType == 'MultiPart') {
- message.messages.forEach(function(e) {
- args.push(massageMessageNativeToJs(e));
- });
- } else {
- args.push(massageMessageNativeToJs(message));
- }
- return args;
- }
- function iOSExec() {
- var successCallback, failCallback, service, action, actionArgs;
- var callbackId = null;
- if (typeof arguments[0] !== 'string') {
-
- successCallback = arguments[0];
- failCallback = arguments[1];
- service = arguments[2];
- action = arguments[3];
- actionArgs = arguments[4];
-
-
-
-
- callbackId = 'INVALID';
- } else {
- throw new Error('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' +
- 'cordova.exec(null, null, \'Service\', \'action\', [ arg1, arg2 ]);'
- );
- }
-
- actionArgs = actionArgs || [];
-
-
- if (successCallback || failCallback) {
- callbackId = service + cordova.callbackId++;
- cordova.callbacks[callbackId] =
- {success:successCallback, fail:failCallback};
- }
- actionArgs = massageArgsJsToNative(actionArgs);
- var command = [callbackId, service, action, actionArgs];
-
-
-
- commandQueue.push(JSON.stringify(command));
-
-
-
-
- if (!isInContextOfEvalJs && commandQueue.length == 1) {
- pokeNative();
- }
- }
- function proxyChanged() {
- var cexec = cordovaExec();
-
- return (execProxy !== cexec &&
- iOSExec !== cexec
- );
- }
- function handleBridgeChange() {
- if (proxyChanged()) {
- var commandString = commandQueue.shift();
- while(commandString) {
- var command = JSON.parse(commandString);
- var callbackId = command[0];
- var service = command[1];
- var action = command[2];
- var actionArgs = command[3];
- var callbacks = cordova.callbacks[callbackId] || {};
-
- execProxy(callbacks.success, callbacks.fail, service, action, actionArgs);
-
- commandString = commandQueue.shift();
- };
- return true;
- }
-
- return false;
- }
- function pokeNative() {
-
- if (!document.body) {
- setTimeout(pokeNative);
- return;
- }
-
-
- if (execIframe && execIframe.contentWindow) {
- execIframe.contentWindow.location = 'gap://ready';
- } else {
- execIframe = document.createElement('iframe');
- execIframe.style.display = 'none';
- execIframe.src = 'gap://ready';
- document.body.appendChild(execIframe);
- }
-
-
-
-
-
-
-
- failSafeTimerId = setTimeout(function() {
- if (commandQueue.length) {
-
- if (!handleBridgeChange()) {
- pokeNative();
- }
- }
- }, 50);
- }
- iOSExec.nativeFetchMessages = function() {
-
- if (failSafeTimerId) {
- clearTimeout(failSafeTimerId);
- failSafeTimerId = 0;
- }
-
- if (!commandQueue.length) {
- return '';
- }
- var json = '[' + commandQueue.join(',') + ']';
- commandQueue.length = 0;
- return json;
- };
- iOSExec.nativeCallback = function(callbackId, status, message, keepCallback, debug) {
- return iOSExec.nativeEvalAndFetch(function() {
- var success = status === 0 || status === 1;
- var args = convertMessageToArgsNativeToJs(message);
- function nc2() {
- cordova.callbackFromNative(callbackId, success, status, args, keepCallback);
- }
- setTimeout(nc2, 0);
- });
- };
- iOSExec.nativeEvalAndFetch = function(func) {
-
- isInContextOfEvalJs++;
- try {
- func();
- return iOSExec.nativeFetchMessages();
- } finally {
- isInContextOfEvalJs--;
- }
- };
- function cordovaExec() {
- var cexec = require('cordova/exec');
- var cexec_valid = (typeof cexec.nativeFetchMessages === 'function') && (typeof cexec.nativeEvalAndFetch === 'function') && (typeof cexec.nativeCallback === 'function');
- return (cexec_valid && execProxy !== cexec)? cexec : iOSExec;
- }
- function execProxy() {
- cordovaExec().apply(null, arguments);
- };
- execProxy.nativeFetchMessages = function() {
- return cordovaExec().nativeFetchMessages.apply(null, arguments);
- };
- execProxy.nativeEvalAndFetch = function() {
- return cordovaExec().nativeEvalAndFetch.apply(null, arguments);
- };
- execProxy.nativeCallback = function() {
- return cordovaExec().nativeCallback.apply(null, arguments);
- };
- module.exports = execProxy;
|