Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

exec.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  22. * Creates the exec bridge used to notify the native code of
  23. * commands.
  24. */
  25. var cordova = require('cordova');
  26. var utils = require('cordova/utils');
  27. var base64 = require('cordova/base64');
  28. function massageArgsJsToNative (args) {
  29. if (!args || utils.typeName(args) !== 'Array') {
  30. return args;
  31. }
  32. var ret = [];
  33. args.forEach(function (arg, i) {
  34. if (utils.typeName(arg) === 'ArrayBuffer') {
  35. ret.push({
  36. CDVType: 'ArrayBuffer',
  37. data: base64.fromArrayBuffer(arg)
  38. });
  39. } else {
  40. ret.push(arg);
  41. }
  42. });
  43. return ret;
  44. }
  45. function massageMessageNativeToJs (message) {
  46. if (message.CDVType === 'ArrayBuffer') {
  47. var stringToArrayBuffer = function (str) {
  48. var ret = new Uint8Array(str.length);
  49. for (var i = 0; i < str.length; i++) {
  50. ret[i] = str.charCodeAt(i);
  51. }
  52. return ret.buffer;
  53. };
  54. var base64ToArrayBuffer = function (b64) {
  55. return stringToArrayBuffer(atob(b64)); // eslint-disable-line no-undef
  56. };
  57. message = base64ToArrayBuffer(message.data);
  58. }
  59. return message;
  60. }
  61. function convertMessageToArgsNativeToJs (message) {
  62. var args = [];
  63. if (!message || !Object.prototype.hasOwnProperty.call(message, 'CDVType')) {
  64. args.push(message);
  65. } else if (message.CDVType === 'MultiPart') {
  66. message.messages.forEach(function (e) {
  67. args.push(massageMessageNativeToJs(e));
  68. });
  69. } else {
  70. args.push(massageMessageNativeToJs(message));
  71. }
  72. return args;
  73. }
  74. var iOSExec = function () {
  75. var successCallback, failCallback, service, action, actionArgs;
  76. var callbackId = null;
  77. if (typeof arguments[0] !== 'string') {
  78. // FORMAT ONE
  79. successCallback = arguments[0];
  80. failCallback = arguments[1];
  81. service = arguments[2];
  82. action = arguments[3];
  83. actionArgs = arguments[4];
  84. // Since we need to maintain backwards compatibility, we have to pass
  85. // an invalid callbackId even if no callback was provided since plugins
  86. // will be expecting it. The Cordova.exec() implementation allocates
  87. // an invalid callbackId and passes it even if no callbacks were given.
  88. callbackId = 'INVALID';
  89. } else {
  90. throw new Error('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' + // eslint-disable-line
  91. 'cordova.exec(null, null, \'Service\', \'action\', [ arg1, arg2 ]);');
  92. }
  93. // If actionArgs is not provided, default to an empty array
  94. actionArgs = actionArgs || [];
  95. // Register the callbacks and add the callbackId to the positional
  96. // arguments if given.
  97. if (successCallback || failCallback) {
  98. callbackId = service + cordova.callbackId++;
  99. cordova.callbacks[callbackId] =
  100. { success: successCallback, fail: failCallback };
  101. }
  102. actionArgs = massageArgsJsToNative(actionArgs);
  103. // CB-10133 DataClone DOM Exception 25 guard (fast function remover)
  104. var command = [callbackId, service, action, JSON.parse(JSON.stringify(actionArgs))];
  105. window.webkit.messageHandlers.cordova.postMessage(command);
  106. };
  107. iOSExec.nativeCallback = function (callbackId, status, message, keepCallback, debug) {
  108. var success = status === 0 || status === 1;
  109. var args = convertMessageToArgsNativeToJs(message);
  110. Promise.resolve().then(function () {
  111. cordova.callbackFromNative(callbackId, success, status, args, keepCallback); // eslint-disable-line
  112. });
  113. };
  114. // for backwards compatibility
  115. iOSExec.nativeEvalAndFetch = function (func) {
  116. try {
  117. func();
  118. } catch (e) {
  119. console.log(e);
  120. }
  121. };
  122. // Proxy the exec for bridge changes. See CB-10106
  123. function cordovaExec () {
  124. var cexec = require('cordova/exec');
  125. var cexec_valid = (typeof cexec.nativeFetchMessages === 'function') && (typeof cexec.nativeEvalAndFetch === 'function') && (typeof cexec.nativeCallback === 'function');
  126. return (cexec_valid && execProxy !== cexec) ? cexec : iOSExec;
  127. }
  128. function execProxy () {
  129. cordovaExec().apply(null, arguments);
  130. }
  131. execProxy.nativeFetchMessages = function () {
  132. return cordovaExec().nativeFetchMessages.apply(null, arguments);
  133. };
  134. execProxy.nativeEvalAndFetch = function () {
  135. return cordovaExec().nativeEvalAndFetch.apply(null, arguments);
  136. };
  137. execProxy.nativeCallback = function () {
  138. return cordovaExec().nativeCallback.apply(null, arguments);
  139. };
  140. module.exports = execProxy;