|
| 1 | +/** |
| 2 | + * Copyright Microsoft Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as debug from 'debug'; |
| 18 | +import { EventEmitter } from 'events'; |
| 19 | +import * as stream from 'stream'; |
| 20 | +import * as ws from 'ws'; |
| 21 | +import { makeWaitForNextTask } from '../../utils/utils'; |
| 22 | + |
| 23 | +export interface Backend { |
| 24 | + devices(): Promise<DeviceBackend[]>; |
| 25 | +} |
| 26 | + |
| 27 | +export interface DeviceBackend { |
| 28 | + close(): Promise<void>; |
| 29 | + init(): Promise<void>; |
| 30 | + runCommand(command: string): Promise<string>; |
| 31 | + open(command: string): Promise<SocketBackend>; |
| 32 | +} |
| 33 | + |
| 34 | +export interface SocketBackend extends EventEmitter { |
| 35 | + write(data: Buffer): Promise<void>; |
| 36 | + close(): Promise<void>; |
| 37 | +} |
| 38 | + |
| 39 | +export class AndroidClient { |
| 40 | + backend: Backend; |
| 41 | + |
| 42 | + constructor(backend: Backend) { |
| 43 | + this.backend = backend; |
| 44 | + } |
| 45 | + |
| 46 | + async devices(): Promise<AndroidDevice[]> { |
| 47 | + const devices = await this.backend.devices(); |
| 48 | + return devices.map(b => new AndroidDevice(b)); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export class AndroidDevice { |
| 53 | + readonly backend: DeviceBackend; |
| 54 | + private _model: string | undefined; |
| 55 | + |
| 56 | + constructor(backend: DeviceBackend) { |
| 57 | + this.backend = backend; |
| 58 | + } |
| 59 | + |
| 60 | + async init() { |
| 61 | + await this.backend.init(); |
| 62 | + this._model = await this.backend.runCommand('shell:getprop ro.product.model'); |
| 63 | + } |
| 64 | + |
| 65 | + async close() { |
| 66 | + await this.backend.close(); |
| 67 | + } |
| 68 | + |
| 69 | + async launchBrowser(packageName: string): Promise<AndroidBrowser> { |
| 70 | + debug('pw:android')('Force-stopping', packageName); |
| 71 | + await this.backend.runCommand(`shell:am force-stop ${packageName}`); |
| 72 | + const hasDefaultSocket = !!(await this.backend.runCommand(`shell:cat /proc/net/unix | grep chrome_devtools_remote$`)); |
| 73 | + debug('pw:android')('Starting', packageName); |
| 74 | + await this.backend.runCommand(`shell:am start -n ${packageName}/com.google.android.apps.chrome.Main about:blank`); |
| 75 | + let pid = 0; |
| 76 | + debug('pw:android')('Polling pid for', packageName); |
| 77 | + while (!pid) { |
| 78 | + const ps = (await this.backend.runCommand(`shell:ps -A | grep ${packageName}`)).split('\n'); |
| 79 | + const proc = ps.find(line => line.endsWith(packageName)); |
| 80 | + if (proc) |
| 81 | + pid = +proc.replace(/\s+/g, ' ').split(' ')[1]; |
| 82 | + await new Promise(f => setTimeout(f, 100)); |
| 83 | + } |
| 84 | + debug('pw:android')('PID=' + pid); |
| 85 | + const socketName = hasDefaultSocket ? `chrome_devtools_remote_${pid}` : 'chrome_devtools_remote'; |
| 86 | + debug('pw:android')('Polling for socket', socketName); |
| 87 | + while (true) { |
| 88 | + const net = await this.backend.runCommand(`shell:cat /proc/net/unix | grep ${socketName}$`); |
| 89 | + if (net) |
| 90 | + break; |
| 91 | + await new Promise(f => setTimeout(f, 100)); |
| 92 | + } |
| 93 | + debug('pw:android')('Got the socket, connecting'); |
| 94 | + const browser = new AndroidBrowser(this, packageName, socketName, pid); |
| 95 | + await browser._open(); |
| 96 | + return browser; |
| 97 | + } |
| 98 | + |
| 99 | + model(): string | undefined { |
| 100 | + return this._model; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export class AndroidBrowser extends EventEmitter { |
| 105 | + readonly device: AndroidDevice; |
| 106 | + readonly socketName: string; |
| 107 | + readonly pid: number; |
| 108 | + private _socket: SocketBackend | undefined; |
| 109 | + private _receiver: stream.Writable; |
| 110 | + private _waitForNextTask = makeWaitForNextTask(); |
| 111 | + onmessage?: (message: any) => void; |
| 112 | + onclose?: () => void; |
| 113 | + private _packageName: string; |
| 114 | + |
| 115 | + constructor(device: AndroidDevice, packageName: string, socketName: string, pid: number) { |
| 116 | + super(); |
| 117 | + this._packageName = packageName; |
| 118 | + this.device = device; |
| 119 | + this.socketName = socketName; |
| 120 | + this.pid = pid; |
| 121 | + this._receiver = new (ws as any).Receiver() as stream.Writable; |
| 122 | + this._receiver.on('message', message => { |
| 123 | + this._waitForNextTask(() => { |
| 124 | + if (this.onmessage) |
| 125 | + this.onmessage(JSON.parse(message)); |
| 126 | + }); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + async _open() { |
| 131 | + this._socket = await this.device.backend.open(`localabstract:${this.socketName}`); |
| 132 | + this._socket.on('close', () => { |
| 133 | + this._waitForNextTask(() => { |
| 134 | + if (this.onclose) |
| 135 | + this.onclose(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + await this._socket.write(Buffer.from(`GET /devtools/browser HTTP/1.1\r |
| 139 | +Upgrade: WebSocket\r |
| 140 | +Connection: Upgrade\r |
| 141 | +Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r |
| 142 | +Sec-WebSocket-Version: 13\r |
| 143 | +\r |
| 144 | +`)); |
| 145 | + // HTTP Upgrade response. |
| 146 | + await new Promise(f => this._socket!.once('data', f)); |
| 147 | + |
| 148 | + // Start sending web frame to receiver. |
| 149 | + this._socket.on('data', data => this._receiver._write(data, 'binary', () => {})); |
| 150 | + } |
| 151 | + |
| 152 | + async send(s: any) { |
| 153 | + await this._socket!.write(encodeWebFrame(JSON.stringify(s))); |
| 154 | + } |
| 155 | + |
| 156 | + async close() { |
| 157 | + await this._socket!.close(); |
| 158 | + await this.device.backend.runCommand(`shell:am force-stop ${this._packageName}`); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +function encodeWebFrame(data: string): Buffer { |
| 163 | + return (ws as any).Sender.frame(Buffer.from(data), { |
| 164 | + opcode: 1, |
| 165 | + mask: true, |
| 166 | + fin: true, |
| 167 | + readOnly: true |
| 168 | + })[0]; |
| 169 | +} |
0 commit comments