UNPKG

5.75 kBJavaScriptView Raw
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/*jslint sloppy:true, plusplus:true*/
23/*global require, module, console */
24
25var cordova = require('cordova');
26var execProxy = require('cordova/exec/proxy');
27
28/**
29 * Execute a cordova command. It is up to the native side whether this action
30 * is synchronous or asynchronous. The native side can return:
31 * Synchronous: PluginResult object as a JSON string
32 * Asynchronous: Empty string ""
33 * If async, the native side will cordova.callbackSuccess or cordova.callbackError,
34 * depending upon the result of the action.
35 *
36 * @param {Function} success The success callback
37 * @param {Function} fail The fail callback
38 * @param {String} service The name of the service to use
39 * @param {String} action Action to be run in cordova
40 * @param {String[]} [args] Zero or more arguments to pass to the method
41 */
42module.exports = function (success, fail, service, action, args) {
43
44 // Handle the case when we have an old version of splashscreen plugin to avoid the API calls failures
45 if (service === 'SplashScreen') {
46 var pluginsVersions = require("cordova/plugin_list").metadata;
47 var splashscreenVersion = pluginsVersions['cordova-plugin-splashscreen'];
48 var MIN_SPLASHSCREEN_SUPPORTED_VER = 4;
49 if (splashscreenVersion && ((parseInt(splashscreenVersion.split('.')[0], 10) || 0) < MIN_SPLASHSCREEN_SUPPORTED_VER)) {
50 console.log('Warning: A more recent version of cordova-plugin-splashscreen has been hooked into for compatibility reasons. Update the plugin to version >= 4.');
51
52 var platformSplashscreen = require('cordova/splashscreen');
53 // Replace old plugin proxy with the platform's one
54 require('cordova/exec/proxy').add(service, platformSplashscreen);
55 }
56 }
57
58 var proxy = execProxy.get(service, action),
59 callbackId,
60 onSuccess,
61 onError;
62
63 args = args || [];
64
65 if (proxy) {
66 callbackId = service + cordova.callbackId++;
67 // console.log("EXEC:" + service + " : " + action);
68 if (typeof success === "function" || typeof fail === "function") {
69 cordova.callbacks[callbackId] = {success: success, fail: fail};
70 }
71 try {
72 // callbackOptions param represents additional optional parameters command could pass back, like keepCallback or
73 // custom callbackId, for example {callbackId: id, keepCallback: true, status: cordova.callbackStatus.JSON_EXCEPTION }
74 // CB-5806 [Windows8] Add keepCallback support to proxy
75 onSuccess = function (result, callbackOptions) {
76 callbackOptions = callbackOptions || {};
77 var callbackStatus;
78 // covering both undefined and null.
79 // strict null comparison was causing callbackStatus to be undefined
80 // and then no callback was called because of the check in cordova.callbackFromNative
81 // see CB-8996 Mobilespec app hang on windows
82 if (callbackOptions.status !== undefined && callbackOptions.status !== null) {
83 callbackStatus = callbackOptions.status;
84 }
85 else {
86 callbackStatus = cordova.callbackStatus.OK;
87 }
88 cordova.callbackSuccess(callbackOptions.callbackId || callbackId,
89 {
90 status: callbackStatus,
91 message: result,
92 keepCallback: callbackOptions.keepCallback || false
93 });
94 };
95 onError = function (err, callbackOptions) {
96 callbackOptions = callbackOptions || {};
97 var callbackStatus;
98 // covering both undefined and null.
99 // strict null comparison was causing callbackStatus to be undefined
100 // and then no callback was called because of the check in cordova.callbackFromNative
101 // see CB-8996 Mobilespec app hang on windows
102 if (callbackOptions.status !== undefined && callbackOptions.status !== null) {
103 callbackStatus = callbackOptions.status;
104 }
105 else {
106 callbackStatus = cordova.callbackStatus.OK;
107 }
108 cordova.callbackError(callbackOptions.callbackId || callbackId,
109 {
110 status: callbackStatus,
111 message: err,
112 keepCallback: callbackOptions.keepCallback || false
113 });
114 };
115 proxy(onSuccess, onError, args);
116
117 } catch (e) {
118 console.log("Exception calling native with command :: " + service + " :: " + action + " ::exception=" + e);
119 }
120 } else {
121 if (typeof fail === "function") {
122 fail("Missing Command Error");
123 }
124 }
125};