exec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /*jslint sloppy:true, plusplus:true*/
  22. /*global require, module, console */
  23. var cordova = require('cordova');
  24. var execProxy = require('cordova/exec/proxy');
  25. /**
  26. * Execute a cordova command. It is up to the native side whether this action
  27. * is synchronous or asynchronous. The native side can return:
  28. * Synchronous: PluginResult object as a JSON string
  29. * Asynchronous: Empty string ""
  30. * If async, the native side will cordova.callbackSuccess or cordova.callbackError,
  31. * depending upon the result of the action.
  32. *
  33. * @param {Function} success The success callback
  34. * @param {Function} fail The fail callback
  35. * @param {String} service The name of the service to use
  36. * @param {String} action Action to be run in cordova
  37. * @param {String[]} [args] Zero or more arguments to pass to the method
  38. */
  39. module.exports = function (success, fail, service, action, args) {
  40. var proxy = execProxy.get(service, action);
  41. args = args || [];
  42. if (proxy) {
  43. var callbackId = service + cordova.callbackId++;
  44. if (typeof success === "function" || typeof fail === "function") {
  45. cordova.callbacks[callbackId] = {success: success, fail: fail};
  46. }
  47. try {
  48. // callbackOptions param represents additional optional parameters command could pass back, like keepCallback or
  49. // custom callbackId, for example {callbackId: id, keepCallback: true, status: cordova.callbackStatus.JSON_EXCEPTION }
  50. var onSuccess = function (result, callbackOptions) {
  51. callbackOptions = callbackOptions || {};
  52. var callbackStatus;
  53. // covering both undefined and null.
  54. // strict null comparison was causing callbackStatus to be undefined
  55. // and then no callback was called because of the check in cordova.callbackFromNative
  56. // see CB-8996 Mobilespec app hang on windows
  57. if (callbackOptions.status !== undefined && callbackOptions.status !== null) {
  58. callbackStatus = callbackOptions.status;
  59. }
  60. else {
  61. callbackStatus = cordova.callbackStatus.OK;
  62. }
  63. cordova.callbackSuccess(callbackOptions.callbackId || callbackId,
  64. {
  65. status: callbackStatus,
  66. message: result,
  67. keepCallback: callbackOptions.keepCallback || false
  68. });
  69. };
  70. var onError = function (err, callbackOptions) {
  71. callbackOptions = callbackOptions || {};
  72. var callbackStatus;
  73. // covering both undefined and null.
  74. // strict null comparison was causing callbackStatus to be undefined
  75. // and then no callback was called because of the check in cordova.callbackFromNative
  76. // note: status can be 0
  77. if (callbackOptions.status !== undefined && callbackOptions.status !== null) {
  78. callbackStatus = callbackOptions.status;
  79. }
  80. else {
  81. callbackStatus = cordova.callbackStatus.OK;
  82. }
  83. cordova.callbackError(callbackOptions.callbackId || callbackId,
  84. {
  85. status: callbackStatus,
  86. message: err,
  87. keepCallback: callbackOptions.keepCallback || false
  88. });
  89. };
  90. proxy(onSuccess, onError, args);
  91. } catch (e) {
  92. console.log("Exception calling native with command :: " + service + " :: " + action + " ::exception=" + e);
  93. }
  94. } else {
  95. console.log("Error: exec proxy not found for :: " + service + " :: " + action);
  96. if(typeof fail === "function" ) {
  97. fail("Missing Command Error");
  98. }
  99. }
  100. };