/** * Copyright 2017 Google Inc. All rights reserved. * Modifications copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { assert } from '../helper'; import * as platform from '../platform'; import { ConnectionTransport } from '../transport'; import { Protocol } from './protocol'; const debugProtocol = platform.debug('playwright:protocol'); const debugWrappedMessage = platform.debug('wrapped'); export const WKConnectionEvents = { Disconnected: Symbol('Disconnected'), PageProxyCreated: Symbol('ConnectionEvents.PageProxyCreated'), PageProxyDestroyed: Symbol('Connection.PageProxyDestroyed') }; export const kBrowserCloseMessageId = -9999; export class WKConnection extends platform.EventEmitter { private _lastId = 0; private readonly _callbacks = new Map void, reject: (e: Error) => void, error: Error, method: string}>(); private readonly _transport: ConnectionTransport; private readonly _pageProxySessions = new Map(); private _closed = false; constructor(transport: ConnectionTransport) { super(); this._transport = transport; this._transport.onmessage = this._dispatchMessage.bind(this); this._transport.onclose = this._onClose.bind(this); } nextMessageId(): number { return ++this._lastId; } send( method: T, params?: Protocol.CommandParameters[T], pageProxyId?: string ): Promise { const id = this._rawSend({pageProxyId, method, params}); return new Promise((resolve, reject) => { this._callbacks.set(id, {resolve, reject, error: new Error(), method}); }); } _rawSend(message: any): number { const id = this.nextMessageId(); message = JSON.stringify(Object.assign({}, message, {id})); debugProtocol('SEND ► ' + message); this._transport.send(message); return id; } private _dispatchMessage(message: string) { debugProtocol('◀ RECV ' + message); const object = JSON.parse(message); this._dispatchPageProxyMessage(object, message); if (object.id) { const callback = this._callbacks.get(object.id); // Callbacks could be all rejected if someone has called `.dispose()`. if (callback) { this._callbacks.delete(object.id); if (object.error) callback.reject(createProtocolError(callback.error, callback.method, object)); else callback.resolve(object.result); } else if (object.id !== kBrowserCloseMessageId) { assert(this._closed, 'Received response for unknown callback: ' + object.id); } } else { Promise.resolve().then(() => this.emit(object.method, object.params)); } } _dispatchPageProxyMessage(object: {method: string, params: any, id?: string, pageProxyId?: string}, message: string) { if (object.method === 'Browser.pageProxyCreated') { const pageProxyId = object.params.pageProxyInfo.pageProxyId; const pageProxySession = new WKPageProxySession(this, pageProxyId); this._pageProxySessions.set(pageProxyId, pageProxySession); Promise.resolve().then(() => this.emit(WKConnectionEvents.PageProxyCreated, pageProxySession, object.params.pageProxyInfo)); } else if (object.method === 'Browser.pageProxyDestroyed') { const pageProxyId = object.params.pageProxyId as string; const pageProxySession = this._pageProxySessions.get(pageProxyId); this._pageProxySessions.delete(pageProxyId); pageProxySession.dispose(); Promise.resolve().then(() => this.emit(WKConnectionEvents.PageProxyDestroyed, pageProxyId)); } else if (!object.id && object.pageProxyId) { const pageProxySession = this._pageProxySessions.get(object.pageProxyId); Promise.resolve().then(() => pageProxySession.emit(object.method, object.params)); } } _onClose() { if (this._closed) return; this._closed = true; this._transport.onmessage = null; this._transport.onclose = null; for (const callback of this._callbacks.values()) callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): Target closed.`)); this._callbacks.clear(); for (const pageProxySession of this._pageProxySessions.values()) pageProxySession.dispose(); this._pageProxySessions.clear(); this.emit(WKConnectionEvents.Disconnected); } dispose() { this._onClose(); this._transport.close(); } } export const WKSessionEvents = { Disconnected: Symbol('WKSessionEvents.Disconnected') }; export class WKPageProxySession extends platform.EventEmitter { _connection: WKConnection; readonly _pageProxyId: string; private readonly _closePromise: Promise; private _closePromiseCallback: () => void; on: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; addListener: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; off: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; removeListener: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; once: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; constructor(connection: WKConnection, pageProxyId: string) { super(); this._connection = connection; this._pageProxyId = pageProxyId; this._closePromise = new Promise(r => this._closePromiseCallback = r); } send( method: T, params?: Protocol.CommandParameters[T] ): Promise { if (!this._connection) return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the pageProxy has been closed.`)); return Promise.race([ this._closePromise.then(() => { throw new Error('Page proxy closed'); }), this._connection.send(method, params, this._pageProxyId) ]); } isClosed() { return !this._connection; } dispose() { this._closePromiseCallback(); this._connection = null; } } export class WKSession extends platform.EventEmitter { connection?: WKConnection; errorText: string; readonly sessionId: string; private readonly _rawSend: (message: any) => void; private readonly _callbacks = new Map void, reject: (e: Error) => void, error: Error, method: string}>(); on: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; addListener: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; off: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; removeListener: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; once: (event: T, listener: (payload: T extends symbol ? any : Protocol.Events[T extends keyof Protocol.Events ? T : never]) => void) => this; constructor(connection: WKConnection, sessionId: string, errorText: string, rawSend: (message: any) => void) { super(); this.connection = connection; this.sessionId = sessionId; this._rawSend = rawSend; this.errorText = errorText; } send( method: T, params?: Protocol.CommandParameters[T] ): Promise { if (!this.connection) return Promise.reject(new Error(`Protocol error (${method}): ${this.errorText}`)); const id = this.connection.nextMessageId(); const messageObj = { id, method, params }; debugWrappedMessage('SEND ► ' + JSON.stringify(messageObj, null, 2)); const result = new Promise((resolve, reject) => { this._callbacks.set(id, {resolve, reject, error: new Error(), method}); }); this._rawSend(messageObj); return result; } isDisposed(): boolean { return !this.connection; } dispose() { for (const callback of this._callbacks.values()) callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): ${this.errorText}`)); this._callbacks.clear(); this.connection = undefined; this.emit(WKSessionEvents.Disconnected); } dispatchMessage(object: any) { debugWrappedMessage('◀ RECV ' + JSON.stringify(object, null, 2)); if (object.id && this._callbacks.has(object.id)) { const callback = this._callbacks.get(object.id); this._callbacks.delete(object.id); if (object.error) callback.reject(createProtocolError(callback.error, callback.method, object)); else callback.resolve(object.result); } else if (object.id) { // Response might come after session has been disposed and rejected all callbacks. assert(this.isDisposed()); } else { Promise.resolve().then(() => this.emit(object.method, object.params)); } } } export function createProtocolError(error: Error, method: string, object: { error: { message: string; data: any; }; }): Error { let message = `Protocol error (${method}): ${object.error.message}`; if ('data' in object.error) message += ` ${object.error.data}`; return rewriteError(error, message); } export function rewriteError(error: Error, message: string): Error { error.message = message; return error; } export function isSwappedOutError(e: Error) { return e.message.includes('Target was swapped out.'); }