1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var cordova = require('cordova'),
|
29 | utils = require('cordova/utils'),
|
30 | base64 = require('cordova/base64'),
|
31 | execIframe,
|
32 | commandQueue = [],
|
33 | isInContextOfEvalJs = 0,
|
34 | failSafeTimerId = 0;
|
35 |
|
36 | function massageArgsJsToNative(args) {
|
37 | if (!args || utils.typeName(args) != 'Array') {
|
38 | return args;
|
39 | }
|
40 | var ret = [];
|
41 | args.forEach(function(arg, i) {
|
42 | if (utils.typeName(arg) == 'ArrayBuffer') {
|
43 | ret.push({
|
44 | 'CDVType': 'ArrayBuffer',
|
45 | 'data': base64.fromArrayBuffer(arg)
|
46 | });
|
47 | } else {
|
48 | ret.push(arg);
|
49 | }
|
50 | });
|
51 | return ret;
|
52 | }
|
53 |
|
54 | function massageMessageNativeToJs(message) {
|
55 | if (message.CDVType == 'ArrayBuffer') {
|
56 | var stringToArrayBuffer = function(str) {
|
57 | var ret = new Uint8Array(str.length);
|
58 | for (var i = 0; i < str.length; i++) {
|
59 | ret[i] = str.charCodeAt(i);
|
60 | }
|
61 | return ret.buffer;
|
62 | };
|
63 | var base64ToArrayBuffer = function(b64) {
|
64 | return stringToArrayBuffer(atob(b64));
|
65 | };
|
66 | message = base64ToArrayBuffer(message.data);
|
67 | }
|
68 | return message;
|
69 | }
|
70 |
|
71 | function convertMessageToArgsNativeToJs(message) {
|
72 | var args = [];
|
73 | if (!message || !message.hasOwnProperty('CDVType')) {
|
74 | args.push(message);
|
75 | } else if (message.CDVType == 'MultiPart') {
|
76 | message.messages.forEach(function(e) {
|
77 | args.push(massageMessageNativeToJs(e));
|
78 | });
|
79 | } else {
|
80 | args.push(massageMessageNativeToJs(message));
|
81 | }
|
82 | return args;
|
83 | }
|
84 |
|
85 | function iOSExec() {
|
86 |
|
87 | var successCallback, failCallback, service, action, actionArgs;
|
88 | var callbackId = null;
|
89 | if (typeof arguments[0] !== 'string') {
|
90 |
|
91 | successCallback = arguments[0];
|
92 | failCallback = arguments[1];
|
93 | service = arguments[2];
|
94 | action = arguments[3];
|
95 | actionArgs = arguments[4];
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | callbackId = 'INVALID';
|
102 | } else {
|
103 | throw new Error('The old format of this exec call has been removed (deprecated since 2.1). Change to: ' +
|
104 | 'cordova.exec(null, null, \'Service\', \'action\', [ arg1, arg2 ]);'
|
105 | );
|
106 | }
|
107 |
|
108 |
|
109 | actionArgs = actionArgs || [];
|
110 |
|
111 |
|
112 |
|
113 | if (successCallback || failCallback) {
|
114 | callbackId = service + cordova.callbackId++;
|
115 | cordova.callbacks[callbackId] =
|
116 | {success:successCallback, fail:failCallback};
|
117 | }
|
118 |
|
119 | actionArgs = massageArgsJsToNative(actionArgs);
|
120 |
|
121 | var command = [callbackId, service, action, actionArgs];
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | commandQueue.push(JSON.stringify(command));
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | if (!isInContextOfEvalJs && commandQueue.length == 1) {
|
133 | pokeNative();
|
134 | }
|
135 | }
|
136 |
|
137 |
|
138 | function proxyChanged() {
|
139 | var cexec = cordovaExec();
|
140 |
|
141 | return (execProxy !== cexec &&
|
142 | iOSExec !== cexec
|
143 | );
|
144 | }
|
145 |
|
146 |
|
147 | function handleBridgeChange() {
|
148 | if (proxyChanged()) {
|
149 | var commandString = commandQueue.shift();
|
150 | while(commandString) {
|
151 | var command = JSON.parse(commandString);
|
152 | var callbackId = command[0];
|
153 | var service = command[1];
|
154 | var action = command[2];
|
155 | var actionArgs = command[3];
|
156 | var callbacks = cordova.callbacks[callbackId] || {};
|
157 |
|
158 | execProxy(callbacks.success, callbacks.fail, service, action, actionArgs);
|
159 |
|
160 | commandString = commandQueue.shift();
|
161 | };
|
162 | return true;
|
163 | }
|
164 |
|
165 | return false;
|
166 | }
|
167 |
|
168 | function pokeNative() {
|
169 |
|
170 | if (!document.body) {
|
171 | setTimeout(pokeNative);
|
172 | return;
|
173 | }
|
174 |
|
175 |
|
176 | if (execIframe && execIframe.contentWindow) {
|
177 | execIframe.contentWindow.location = 'gap://ready';
|
178 | } else {
|
179 | execIframe = document.createElement('iframe');
|
180 | execIframe.style.display = 'none';
|
181 | execIframe.src = 'gap://ready';
|
182 | document.body.appendChild(execIframe);
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | failSafeTimerId = setTimeout(function() {
|
192 | if (commandQueue.length) {
|
193 |
|
194 | if (!handleBridgeChange()) {
|
195 | pokeNative();
|
196 | }
|
197 | }
|
198 | }, 50);
|
199 | }
|
200 |
|
201 | iOSExec.nativeFetchMessages = function() {
|
202 |
|
203 | if (failSafeTimerId) {
|
204 | clearTimeout(failSafeTimerId);
|
205 | failSafeTimerId = 0;
|
206 | }
|
207 |
|
208 | if (!commandQueue.length) {
|
209 | return '';
|
210 | }
|
211 | var json = '[' + commandQueue.join(',') + ']';
|
212 | commandQueue.length = 0;
|
213 | return json;
|
214 | };
|
215 |
|
216 | iOSExec.nativeCallback = function(callbackId, status, message, keepCallback, debug) {
|
217 | return iOSExec.nativeEvalAndFetch(function() {
|
218 | var success = status === 0 || status === 1;
|
219 | var args = convertMessageToArgsNativeToJs(message);
|
220 | function nc2() {
|
221 | cordova.callbackFromNative(callbackId, success, status, args, keepCallback);
|
222 | }
|
223 | setTimeout(nc2, 0);
|
224 | });
|
225 | };
|
226 |
|
227 | iOSExec.nativeEvalAndFetch = function(func) {
|
228 |
|
229 | isInContextOfEvalJs++;
|
230 | try {
|
231 | func();
|
232 | return iOSExec.nativeFetchMessages();
|
233 | } finally {
|
234 | isInContextOfEvalJs--;
|
235 | }
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 | function cordovaExec() {
|
241 | var cexec = require('cordova/exec');
|
242 | var cexec_valid = (typeof cexec.nativeFetchMessages === 'function') && (typeof cexec.nativeEvalAndFetch === 'function') && (typeof cexec.nativeCallback === 'function');
|
243 | return (cexec_valid && execProxy !== cexec)? cexec : iOSExec;
|
244 | }
|
245 |
|
246 | function execProxy() {
|
247 | cordovaExec().apply(null, arguments);
|
248 | };
|
249 |
|
250 | execProxy.nativeFetchMessages = function() {
|
251 | return cordovaExec().nativeFetchMessages.apply(null, arguments);
|
252 | };
|
253 |
|
254 | execProxy.nativeEvalAndFetch = function() {
|
255 | return cordovaExec().nativeEvalAndFetch.apply(null, arguments);
|
256 | };
|
257 |
|
258 | execProxy.nativeCallback = function() {
|
259 | return cordovaExec().nativeCallback.apply(null, arguments);
|
260 | };
|
261 |
|
262 | module.exports = execProxy;
|