-
Notifications
You must be signed in to change notification settings - Fork 3
/
iosdriver.js
449 lines (370 loc) · 12.6 KB
/
iosdriver.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Copyright (c) 2013 Left Logic Ltd, http://leftlogic.com
* Copyright (c) 2013 Andy Davies http://andydavies.me
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the “Software”), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
* to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* @TODO:
*
* - Which _rpc responses are missing (this is a process of discovery)
* - Needs to support more than one tab
* - Needs tests
*/
// Dependencies
var net = require('net');
plist = require('plist'),
bplist_create = require('node-bplist-creator'),
bplist_parse = require('bplist-parser'),
bufferpack = require('bufferpack'),
uuid = require('node-uuid'),
util = require('util'),
events = require('events');
var noop = function () {};
// Used in _handleResponse to flag missing handlers
var noopHandler = function (plist) { console.warn("Add handler for:" + plist.__selector.slice(0, -1))};
/**
* @constructor
* @param {string} host must be IPv6 address
* @param {int} port
*/
var iOSDriver = function (host, port) {
// default if not provided
host = host || '::1';
port = port || 27753;
this.conn_id = uuid.v4();
this.sender_id = uuid.v4();
this.page_id = 1; // @TODO this probably shouldn't be defaulted to 1
// Some responses from iOS span more than one packet so need to be buffered
this.received = new Buffer(0);
this.read_pos = 0;
// unique id for each message
this.msg_id = 0;
// store state of connected device
this.device = undefined;
this.browser = undefined;
this.tabs = undefined;
// Socket and handlers
var driver = this;
// @TODO:
// - What other response types are there? (is this list documented anywhere?)
// - Use registration method to allow replacement?
// - Move emit('open')
this.handlers = {
_rpc_reportSetup: function (plist) { driver._onReportSetup(plist); },
_rpc_reportConnectedApplicationList: function (plist) { driver.emit('open'); driver._onReportConnectedApplicationList(plist); },
_rpc_applicationSentListing: function (plist) { driver._onApplicationSentListing(plist); },
_rpc_applicationSentData: function (plist) { driver._onApplicationSentData(plist); },
_rpc_applicationDisconnected: function (plist) { driver._onApplicationDisconnected(plist); }
};
this.socket = new net.Socket({type: 'tcp6'});
this.socket.connect(port, host, function () { noop(); });
this.socket.on('data', function (data) { driver._onData(data); });
this.socket.on('close', function () { noop(); });
// @TODO these methods may need renaming
this.setConnectionKey();
this.connectToApp();
this.setSenderKey();
}
util.inherits(iOSDriver, events.EventEmitter);
/**
* Closes connection
*/
iOSDriver.prototype.close = function() {
this.socket.end(); // @TODO check this is right?
}
/**
* @returns {object} details of connected device
*/
iOSDriver.prototype.getDevice = function() {
return this.device;
}
/**
* @returns {object} list of active browsers (webviews as well?)
*/
iOSDriver.prototype.getBrowser = function() {
return this.browser;
}
/**
* @returns {object} list of tabs in connected browser
*/
iOSDriver.prototype.getTabs = function() {
return this.tabs;
}
/**
* Sets connection id, on success iOS returns _rpc_reportSetup
*/
iOSDriver.prototype.setConnectionKey = function() {
var msg = {
__argument: {
WIRConnectionIdentifierKey: this.conn_id
},
__selector : '_rpc_reportIdentifier:'
};
this._send(msg);
};
/**
* Connects to app, on success iOS returns _rpc_reportConnectedApplicationList
*/
iOSDriver.prototype.connectToApp = function() {
var msg = {
__argument: {
WIRConnectionIdentifierKey: this.conn_id,
WIRApplicationIdentifierKey: 'com.apple.mobilesafari'
},
__selector : '_rpc_forwardGetListing:'
};
this._send(msg);
};
/**
* Sets sender key, on success iOS returns _rpc_applicationSentListing
*/
iOSDriver.prototype.setSenderKey = function() {
var msg = {
__argument: {
WIRApplicationIdentifierKey: 'com.apple.mobilesafari',
WIRConnectionIdentifierKey: this.conn_id,
WIRSenderKey: this.sender_id,
WIRPageIdentifierKey: this.page_id
},
__selector: '_rpc_forwardSocketSetup:'
};
this._send(msg);
};
/**
* @TODO: Safari calls this but what does it do, seems no response to it?
*/
iOSDriver.prototype.indicateWebView = function(isWebView) {
var msg = {
__argument: {
WIRApplicationIdentifierKey: 'com.apple.mobilesafari',
WIRIndicateEnabledKey: isWebView,
WIRConnectionIdentifierKey: this.conn_id,
WIRPageIdentifierKey: this.page_id
},
__selector: '_rpc_forwardIndicateWebView:'
};
this._send(msg);
};
/**
* Sends command to attached Safari instance
* @param {string} method
* @param {object} params
* @returns {int} id of message sent
*
* @see For commands and parameters: https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/index
*/
iOSDriver.prototype.sendCommand = function(method, params) {
// data must be a buffer otherwise Safari will crash
var data = new Buffer(JSON.stringify({
id: this.msg_id++,
method: method,
params: params
}));
var msg = {
__argument: {
WIRApplicationIdentifierKey: 'com.apple.mobilesafari',
WIRSocketDataKey: data,
WIRConnectionIdentifierKey: this.conn_id,
WIRSenderKey: this.sender_id,
WIRPageIdentifierKey: this.page_id // @TODO
},
__selector: '_rpc_forwardSocketData:'
};
this._send(msg);
return this.msg_id;
};
// Socket
/**
* Converts plist binary and sends to iOS
* @param {object} data plist to be send
* @param {object} callback function to be called after data sent
*/
iOSDriver.prototype._send = function(data, callback) {
callback = callback || noop;
var plist;
try {
plist = bplist_create(data);
}
catch(e) {
return console.error(e);
}
this.socket.write(bufferpack.pack('L', [plist.length]));
this.socket.write(plist, callback);
};
/**
* Reassembles response from iOS (if it spans multiple messages) and
* converts from binary plist to plist
* @param {object} data response from iOS
*/
iOSDriver.prototype._onData = function(data) {
// Append this new data to the existing Buffer
this.received = Buffer.concat([this.received, data]);
var data_left_over = true;
// Parse multiple messages in the same packet
while(data_left_over) {
// Store a reference to where we were
var old_read_pos = this.read_pos;
// Read the prefix (plist length) to see how far to read next
// It's always 4 bytes long
var prefix = this.received.slice(this.read_pos, this.read_pos + 4);
var msg_length;
try {
msg_length = bufferpack.unpack('L', prefix)[0];
}
catch(e) {
return log(e);
}
// Jump forward 4 bytes
this.read_pos += 4;
// Is there enough data here?
// If not, jump back to our original position and gtfo
if( this.received.length < msg_length + this.read_pos ) {
this.read_pos = old_read_pos;
break;
}
// Extract the main body of the message (where the plist should be)
var body = this.received.slice(this.read_pos, msg_length + this.read_pos);
// Extract the plist
var plist;
try {
plist = bplist_parse.parseBuffer(body);
}
catch (e) {
console.error(e);
}
// bplist_parse.parseBuffer returns an array
if(plist.length === 1) {
plist = plist[0];
}
// Jump forward the length of the plist
this.read_pos += msg_length;
// Calculate how much buffer is left
var left_over = this.received.length - this.read_pos;
// Is there some left over?
if( left_over !== 0 ) {
// Copy what's left over into a new buffer, and save it for next time
var chunk = new Buffer(left_over);
this.received.copy(chunk, 0, this.read_pos);
this.received = chunk;
}
else {
// Otherwise, empty the buffer and get out of the loop
this.received = new Buffer(0);
data_left_over = false;
}
// Reset the read position
this.read_pos = 0;
// Now do something with the plist
if(plist) {
this._handleResponse(plist);
}
}
};
/**
* Dispatches reponse from iOS to appropriate handler
* @param {object} plist response from iOS as plist
*/
iOSDriver.prototype._handleResponse = function(plist) {
if(!plist.__selector) {
return;
}
var selector = plist.__selector.slice(0, -1);
(this.handlers[selector] || noopHandler)(plist);
};
/**
* Handles _rpc_reportSetup, stores connected device details
* @param {object} plist response from iOS as plist
*
* @TODO: What about other e.g. non-simulator, devices?
*/
iOSDriver.prototype._onReportSetup = function(plist) {
if(plist.__argument && plist.__argument.WIRSimulatorNameKey) {
this.device = {
name: plist.__argument.WIRSimulatorNameKey,
version: plist.__argument.WIRSimulatorBuildKey
};
}
else {
this.device = undefined;
}
// console.log(this.device);
}
/**
* Handles _rpc_reportConnectedApplicationList, stores app details
* @param {object} plist response from iOS as plist
*
* @TODO:
* - What if more that one app can be connected to, is this code still right?
* - Cleanup iteration code
*/
iOSDriver.prototype._onReportConnectedApplicationList = function(plist) {
if(plist.__argument && plist.__argument.WIRApplicationDictionaryKey) {
this.browser = {};
for(entry in plist.__argument.WIRApplicationDictionaryKey) {
this.browser[plist.__argument.WIRApplicationDictionaryKey[entry].WIRApplicationIdentifierKey] = {
name: plist.__argument.WIRApplicationDictionaryKey[entry].WIRApplicationNameKey,
proxy: plist.__argument.WIRApplicationDictionaryKey[entry].WIRIsApplicationProxyKey
};
}
}
else {
this.browser = undefined;
}
// console.log(this.browser);
}
/**
* Handles _rpc_applicationSentListing, stores list of available pages
* @param {object} plist response from iOS as plist
*
* @TODO:
* - What if more that one app can be connected to, is this code still right?
* - Cleanup iteration code
* - WIRConnectionIdentifierKey
*/
iOSDriver.prototype._onApplicationSentListing = function(plist) {
if(plist.__argument && plist.__argument.WIRListingKey) {
this.tabs = {};
for(entry in plist.__argument.WIRListingKey) {
this.tabs[plist.__argument.WIRListingKey[entry].WIRPageIdentifierKey] = {
title: plist.__argument.WIRListingKey[entry].WIRTitleKey,
url: plist.__argument.WIRListingKey[entry].WIRURLKey
};
}
}
else {
this.tabs = undefined;
}
// console.log(this.tabs);
}
/**
* Handles _rpc_applicationSentData i.e. response from sendCommand, extracts JSON reponse object and emits message with it
* @param {object} plist response from iOS as plist
*/
iOSDriver.prototype._onApplicationSentData = function(plist) {
if(plist.__argument && plist.__argument.WIRMessageDataKey) {
this.emit('message', plist.__argument.WIRMessageDataKey.toString());
}
}
/**
* Handles _rpc_applicationDisconnected
* @param {object} plist response from iOS as plist
*/
iOSDriver.prototype._onApplicationDisconnected = function (plist) {
this.emit('close');
}
module.exports = iOSDriver;