Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

exec.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /*global require, module, atob, document */
  22. /**
  23. * Creates a gap bridge iframe used to notify the native code about queued
  24. * commands.
  25. */
  26. var cordova = require('cordova'),
  27. utils = require('cordova/utils'),
  28. base64 = require('cordova/base64'),
  29. execIframe,
  30. commandQueue = [], // Contains pending JS->Native messages.
  31. isInContextOfEvalJs = 0,
  32. failSafeTimerId = 0;
  33. function massageArgsJsToNative(args) {
  34. if (!args || utils.typeName(args) != 'Array') {
  35. return args;
  36. }
  37. var ret = [];
  38. args.forEach(function(arg, i) {
  39. if (utils.typeName(arg) == 'ArrayBuffer') {
  40. ret.push({
  41. 'CDVType': 'ArrayBuffer',
  42. 'data': base64.fromArrayBuffer(arg)
  43. });
  44. } else {
  45. ret.push(arg);
  46. }
  47. });
  48. return ret;
  49. }
  50. function massageMessageNativeToJs(message) {
  51. if (message.CDVType == 'ArrayBuffer') {
  52. var stringToArrayBuffer = function(str) {
  53. var ret = new Uint8Array(str.length);
  54. for (var i = 0; i < str.length; i++) {
  55. ret[i] = str.charCodeAt(i);
  56. }
  57. return ret.buffer;
  58. };
  59. var base64ToArrayBuffer = function(b64) {
  60. return stringToArrayBuffer(atob(b64));
  61. };
  62. message = base64ToArrayBuffer(message.data);
  63. }
  64. return message;
  65. }
  66. function convertMessageToArgsNativeToJs(message) {
  67. var args = [];
  68. if (!message || !message.hasOwnProperty('CDVType')) {
  69. args.push(message);
  70. } else if (message.CDVType == 'MultiPart') {
  71. message.messages.forEach(function(e) {
  72. args.push(massageMessageNativeToJs(e));
  73. });
  74. } else {
  75. args.push(massageMessageNativeToJs(message));
  76. }
  77. return args;
  78. }
  79. function iOSExec() {
  80. var successCallback, failCallback, service, action, actionArgs;
  81. var callbackId = null;
  82. if (typeof arguments[0] !== 'string') {
  83. // FORMAT ONE
  84. successCallback = arguments[0];
  85. failCallback = arguments[1];
  86. service = arguments[2];
  87. action = arguments[3];
  88. actionArgs = arguments[4];
  89. // Since we need to maintain backwards compatibility, we have to pass
  90. // an invalid callbackId even if no callback was provided since plugins
  91. // will be expecting it. The Cordova.exec() implementation allocates
  92. // an invalid callbackId and passes it even if no callbacks were given.
  93. callbackId = 'INVALID';
  94. } else {
  95. throw new Error('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' +
  96. 'cordova.exec(null, null, \'Service\', \'action\', [ arg1, arg2 ]);'
  97. );
  98. }
  99. // If actionArgs is not provided, default to an empty array
  100. actionArgs = actionArgs || [];
  101. // Register the callbacks and add the callbackId to the positional
  102. // arguments if given.
  103. if (successCallback || failCallback) {
  104. callbackId = service + cordova.callbackId++;
  105. cordova.callbacks[callbackId] =
  106. {success:successCallback, fail:failCallback};
  107. }
  108. actionArgs = massageArgsJsToNative(actionArgs);
  109. var command = [callbackId, service, action, actionArgs];
  110. // Stringify and queue the command. We stringify to command now to
  111. // effectively clone the command arguments in case they are mutated before
  112. // the command is executed.
  113. commandQueue.push(JSON.stringify(command));
  114. // If we're in the context of a stringByEvaluatingJavaScriptFromString call,
  115. // then the queue will be flushed when it returns; no need for a poke.
  116. // Also, if there is already a command in the queue, then we've already
  117. // poked the native side, so there is no reason to do so again.
  118. if (!isInContextOfEvalJs && commandQueue.length == 1) {
  119. pokeNative();
  120. }
  121. }
  122. // CB-10530
  123. function proxyChanged() {
  124. var cexec = cordovaExec();
  125. return (execProxy !== cexec && // proxy objects are different
  126. iOSExec !== cexec // proxy object is not the current iOSExec
  127. );
  128. }
  129. // CB-10106
  130. function handleBridgeChange() {
  131. if (proxyChanged()) {
  132. var commandString = commandQueue.shift();
  133. while(commandString) {
  134. var command = JSON.parse(commandString);
  135. var callbackId = command[0];
  136. var service = command[1];
  137. var action = command[2];
  138. var actionArgs = command[3];
  139. var callbacks = cordova.callbacks[callbackId] || {};
  140. execProxy(callbacks.success, callbacks.fail, service, action, actionArgs);
  141. commandString = commandQueue.shift();
  142. };
  143. return true;
  144. }
  145. return false;
  146. }
  147. function pokeNative() {
  148. // CB-5488 - Don't attempt to create iframe before document.body is available.
  149. if (!document.body) {
  150. setTimeout(pokeNative);
  151. return;
  152. }
  153. // Check if they've removed it from the DOM, and put it back if so.
  154. if (execIframe && execIframe.contentWindow) {
  155. execIframe.contentWindow.location = 'gap://ready';
  156. } else {
  157. execIframe = document.createElement('iframe');
  158. execIframe.style.display = 'none';
  159. execIframe.src = 'gap://ready';
  160. document.body.appendChild(execIframe);
  161. }
  162. // Use a timer to protect against iframe being unloaded during the poke (CB-7735).
  163. // This makes the bridge ~ 7% slower, but works around the poke getting lost
  164. // when the iframe is removed from the DOM.
  165. // An onunload listener could be used in the case where the iframe has just been
  166. // created, but since unload events fire only once, it doesn't work in the normal
  167. // case of iframe reuse (where unload will have already fired due to the attempted
  168. // navigation of the page).
  169. failSafeTimerId = setTimeout(function() {
  170. if (commandQueue.length) {
  171. // CB-10106 - flush the queue on bridge change
  172. if (!handleBridgeChange()) {
  173. pokeNative();
  174. }
  175. }
  176. }, 50); // Making this > 0 improves performance (marginally) in the normal case (where it doesn't fire).
  177. }
  178. iOSExec.nativeFetchMessages = function() {
  179. // Stop listing for window detatch once native side confirms poke.
  180. if (failSafeTimerId) {
  181. clearTimeout(failSafeTimerId);
  182. failSafeTimerId = 0;
  183. }
  184. // Each entry in commandQueue is a JSON string already.
  185. if (!commandQueue.length) {
  186. return '';
  187. }
  188. var json = '[' + commandQueue.join(',') + ']';
  189. commandQueue.length = 0;
  190. return json;
  191. };
  192. iOSExec.nativeCallback = function(callbackId, status, message, keepCallback, debug) {
  193. return iOSExec.nativeEvalAndFetch(function() {
  194. var success = status === 0 || status === 1;
  195. var args = convertMessageToArgsNativeToJs(message);
  196. function nc2() {
  197. cordova.callbackFromNative(callbackId, success, status, args, keepCallback);
  198. }
  199. setTimeout(nc2, 0);
  200. });
  201. };
  202. iOSExec.nativeEvalAndFetch = function(func) {
  203. // This shouldn't be nested, but better to be safe.
  204. isInContextOfEvalJs++;
  205. try {
  206. func();
  207. return iOSExec.nativeFetchMessages();
  208. } finally {
  209. isInContextOfEvalJs--;
  210. }
  211. };
  212. // Proxy the exec for bridge changes. See CB-10106
  213. function cordovaExec() {
  214. var cexec = require('cordova/exec');
  215. var cexec_valid = (typeof cexec.nativeFetchMessages === 'function') && (typeof cexec.nativeEvalAndFetch === 'function') && (typeof cexec.nativeCallback === 'function');
  216. return (cexec_valid && execProxy !== cexec)? cexec : iOSExec;
  217. }
  218. function execProxy() {
  219. cordovaExec().apply(null, arguments);
  220. };
  221. execProxy.nativeFetchMessages = function() {
  222. return cordovaExec().nativeFetchMessages.apply(null, arguments);
  223. };
  224. execProxy.nativeEvalAndFetch = function() {
  225. return cordovaExec().nativeEvalAndFetch.apply(null, arguments);
  226. };
  227. execProxy.nativeCallback = function() {
  228. return cordovaExec().nativeCallback.apply(null, arguments);
  229. };
  230. module.exports = execProxy;