wkConnection.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.kPageProxyMessageReceived = exports.kBrowserCloseMessageId = exports.WKSession = exports.WKConnection = void 0;
  6. var _events = require("events");
  7. var _utils = require("../../utils");
  8. var _debugLogger = require("../../common/debugLogger");
  9. var _helper = require("../helper");
  10. var _protocolError = require("../protocolError");
  11. /**
  12. * Copyright 2017 Google Inc. All rights reserved.
  13. * Modifications copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. // WKPlaywright uses this special id to issue Browser.close command which we
  28. // should ignore.
  29. const kBrowserCloseMessageId = exports.kBrowserCloseMessageId = -9999;
  30. // We emulate kPageProxyMessageReceived message to unify it with Browser.pageProxyCreated
  31. // and Browser.pageProxyDestroyed for easier management.
  32. const kPageProxyMessageReceived = exports.kPageProxyMessageReceived = 'kPageProxyMessageReceived';
  33. class WKConnection {
  34. constructor(transport, onDisconnect, protocolLogger, browserLogsCollector) {
  35. this._transport = void 0;
  36. this._onDisconnect = void 0;
  37. this._protocolLogger = void 0;
  38. this._browserLogsCollector = void 0;
  39. this._browserDisconnectedLogs = void 0;
  40. this._lastId = 0;
  41. this._closed = false;
  42. this.browserSession = void 0;
  43. this._transport = transport;
  44. this._onDisconnect = onDisconnect;
  45. this._protocolLogger = protocolLogger;
  46. this._browserLogsCollector = browserLogsCollector;
  47. this.browserSession = new WKSession(this, '', message => {
  48. this.rawSend(message);
  49. });
  50. this._transport.onmessage = this._dispatchMessage.bind(this);
  51. // onclose should be set last, since it can be immediately called.
  52. this._transport.onclose = this._onClose.bind(this);
  53. }
  54. nextMessageId() {
  55. return ++this._lastId;
  56. }
  57. rawSend(message) {
  58. this._protocolLogger('send', message);
  59. this._transport.send(message);
  60. }
  61. _dispatchMessage(message) {
  62. this._protocolLogger('receive', message);
  63. if (message.id === kBrowserCloseMessageId) return;
  64. if (message.pageProxyId) {
  65. const payload = {
  66. message: message,
  67. pageProxyId: message.pageProxyId
  68. };
  69. this.browserSession.dispatchMessage({
  70. method: kPageProxyMessageReceived,
  71. params: payload
  72. });
  73. return;
  74. }
  75. this.browserSession.dispatchMessage(message);
  76. }
  77. _onClose() {
  78. this._closed = true;
  79. this._transport.onmessage = undefined;
  80. this._transport.onclose = undefined;
  81. this._browserDisconnectedLogs = _helper.helper.formatBrowserLogs(this._browserLogsCollector.recentLogs());
  82. this.browserSession.dispose();
  83. this._onDisconnect();
  84. }
  85. isClosed() {
  86. return this._closed;
  87. }
  88. close() {
  89. if (!this._closed) this._transport.close();
  90. }
  91. }
  92. exports.WKConnection = WKConnection;
  93. class WKSession extends _events.EventEmitter {
  94. constructor(connection, sessionId, rawSend) {
  95. super();
  96. this.connection = void 0;
  97. this.sessionId = void 0;
  98. this._disposed = false;
  99. this._rawSend = void 0;
  100. this._callbacks = new Map();
  101. this._crashed = false;
  102. this.on = void 0;
  103. this.addListener = void 0;
  104. this.off = void 0;
  105. this.removeListener = void 0;
  106. this.once = void 0;
  107. this.setMaxListeners(0);
  108. this.connection = connection;
  109. this.sessionId = sessionId;
  110. this._rawSend = rawSend;
  111. this.on = super.on;
  112. this.off = super.removeListener;
  113. this.addListener = super.addListener;
  114. this.removeListener = super.removeListener;
  115. this.once = super.once;
  116. }
  117. async send(method, params) {
  118. if (this._crashed || this._disposed || this.connection._browserDisconnectedLogs) throw new _protocolError.ProtocolError(this._crashed ? 'crashed' : 'closed', undefined, this.connection._browserDisconnectedLogs);
  119. const id = this.connection.nextMessageId();
  120. const messageObj = {
  121. id,
  122. method,
  123. params
  124. };
  125. this._rawSend(messageObj);
  126. return new Promise((resolve, reject) => {
  127. this._callbacks.set(id, {
  128. resolve,
  129. reject,
  130. error: new _protocolError.ProtocolError('error', method)
  131. });
  132. });
  133. }
  134. sendMayFail(method, params) {
  135. return this.send(method, params).catch(error => _debugLogger.debugLogger.log('error', error));
  136. }
  137. markAsCrashed() {
  138. this._crashed = true;
  139. }
  140. isDisposed() {
  141. return this._disposed;
  142. }
  143. dispose() {
  144. for (const callback of this._callbacks.values()) {
  145. callback.error.type = this._crashed ? 'crashed' : 'closed';
  146. callback.error.logs = this.connection._browserDisconnectedLogs;
  147. callback.reject(callback.error);
  148. }
  149. this._callbacks.clear();
  150. this._disposed = true;
  151. }
  152. dispatchMessage(object) {
  153. if (object.id && this._callbacks.has(object.id)) {
  154. const callback = this._callbacks.get(object.id);
  155. this._callbacks.delete(object.id);
  156. if (object.error) {
  157. callback.error.setMessage(object.error.message);
  158. callback.reject(callback.error);
  159. } else {
  160. callback.resolve(object.result);
  161. }
  162. } else if (object.id && !object.error) {
  163. // Response might come after session has been disposed and rejected all callbacks.
  164. (0, _utils.assert)(this.isDisposed());
  165. } else {
  166. Promise.resolve().then(() => this.emit(object.method, object.params));
  167. }
  168. }
  169. }
  170. exports.WKSession = WKSession;