CommandProcessor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. /**
  3. * Copyright 2021 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.CommandProcessor = void 0;
  20. const protocol_js_1 = require("../protocol/protocol.js");
  21. const log_js_1 = require("../utils/log.js");
  22. const EventEmitter_js_1 = require("../utils/EventEmitter.js");
  23. const browsingContextProcessor_js_1 = require("./domains/context/browsingContextProcessor.js");
  24. const OutgoingBidiMessage_js_1 = require("./OutgoingBidiMessage.js");
  25. class BidiNoOpParser {
  26. parseAddPreloadScriptParams(params) {
  27. return params;
  28. }
  29. parseRemovePreloadScriptParams(params) {
  30. return params;
  31. }
  32. parseGetRealmsParams(params) {
  33. return params;
  34. }
  35. parseCallFunctionParams(params) {
  36. return params;
  37. }
  38. parseEvaluateParams(params) {
  39. return params;
  40. }
  41. parseDisownParams(params) {
  42. return params;
  43. }
  44. parseSendCommandParams(params) {
  45. return params;
  46. }
  47. parseGetSessionParams(params) {
  48. return params;
  49. }
  50. parseSubscribeParams(params) {
  51. return params;
  52. }
  53. parseNavigateParams(params) {
  54. return params;
  55. }
  56. parseReloadParams(params) {
  57. return params;
  58. }
  59. parseGetTreeParams(params) {
  60. return params;
  61. }
  62. parseCreateParams(params) {
  63. return params;
  64. }
  65. parseCloseParams(params) {
  66. return params;
  67. }
  68. parseCaptureScreenshotParams(params) {
  69. return params;
  70. }
  71. parsePrintParams(params) {
  72. return params;
  73. }
  74. parsePerformActionsParams(params) {
  75. return params;
  76. }
  77. parseReleaseActionsParams(params) {
  78. return params;
  79. }
  80. parseSetViewportParams(params) {
  81. return params;
  82. }
  83. }
  84. class CommandProcessor extends EventEmitter_js_1.EventEmitter {
  85. #contextProcessor;
  86. #eventManager;
  87. #parser;
  88. #logger;
  89. constructor(cdpConnection, eventManager, selfTargetId, parser = new BidiNoOpParser(), browsingContextStorage, realmStorage, logger) {
  90. super();
  91. this.#eventManager = eventManager;
  92. this.#logger = logger;
  93. this.#contextProcessor = new browsingContextProcessor_js_1.BrowsingContextProcessor(cdpConnection, selfTargetId, eventManager, browsingContextStorage, realmStorage, logger);
  94. this.#parser = parser;
  95. }
  96. static #process_session_status() {
  97. return { result: { ready: false, message: 'already connected' } };
  98. }
  99. async #process_session_subscribe(params, channel) {
  100. await this.#eventManager.subscribe(params.events, params.contexts ?? [null], channel);
  101. return { result: {} };
  102. }
  103. async #process_session_unsubscribe(params, channel) {
  104. await this.#eventManager.unsubscribe(params.events, params.contexts ?? [null], channel);
  105. return { result: {} };
  106. }
  107. async #processCommand(commandData) {
  108. switch (commandData.method) {
  109. case 'session.status':
  110. return CommandProcessor.#process_session_status();
  111. case 'session.subscribe':
  112. return this.#process_session_subscribe(this.#parser.parseSubscribeParams(commandData.params), commandData.channel ?? null);
  113. case 'session.unsubscribe':
  114. return this.#process_session_unsubscribe(this.#parser.parseSubscribeParams(commandData.params), commandData.channel ?? null);
  115. case 'browsingContext.create':
  116. return this.#contextProcessor.process_browsingContext_create(this.#parser.parseCreateParams(commandData.params));
  117. case 'browsingContext.close':
  118. return this.#contextProcessor.process_browsingContext_close(this.#parser.parseCloseParams(commandData.params));
  119. case 'browsingContext.getTree':
  120. return this.#contextProcessor.process_browsingContext_getTree(this.#parser.parseGetTreeParams(commandData.params));
  121. case 'browsingContext.navigate':
  122. return this.#contextProcessor.process_browsingContext_navigate(this.#parser.parseNavigateParams(commandData.params));
  123. case 'browsingContext.captureScreenshot':
  124. return this.#contextProcessor.process_browsingContext_captureScreenshot(this.#parser.parseCaptureScreenshotParams(commandData.params));
  125. case 'browsingContext.print':
  126. return this.#contextProcessor.process_browsingContext_print(this.#parser.parsePrintParams(commandData.params));
  127. case 'browsingContext.reload':
  128. return this.#contextProcessor.process_browsingContext_reload(this.#parser.parseReloadParams(commandData.params));
  129. case 'browsingContext.setViewport':
  130. return this.#contextProcessor.process_browsingContext_setViewport(this.#parser.parseSetViewportParams(commandData.params));
  131. case 'script.addPreloadScript':
  132. return this.#contextProcessor.process_script_addPreloadScript(this.#parser.parseAddPreloadScriptParams(commandData.params));
  133. case 'script.removePreloadScript':
  134. return this.#contextProcessor.process_script_removePreloadScript(this.#parser.parseRemovePreloadScriptParams(commandData.params));
  135. case 'script.getRealms':
  136. return this.#contextProcessor.process_script_getRealms(this.#parser.parseGetRealmsParams(commandData.params));
  137. case 'script.callFunction':
  138. return this.#contextProcessor.process_script_callFunction(this.#parser.parseCallFunctionParams(commandData.params));
  139. case 'script.evaluate':
  140. return this.#contextProcessor.process_script_evaluate(this.#parser.parseEvaluateParams(commandData.params));
  141. case 'script.disown':
  142. return this.#contextProcessor.process_script_disown(this.#parser.parseDisownParams(commandData.params));
  143. case 'input.performActions':
  144. return this.#contextProcessor.process_input_performActions(this.#parser.parsePerformActionsParams(commandData.params));
  145. case 'input.releaseActions':
  146. return this.#contextProcessor.process_input_releaseActions(this.#parser.parseReleaseActionsParams(commandData.params));
  147. case 'cdp.sendCommand':
  148. return this.#contextProcessor.process_cdp_sendCommand(this.#parser.parseSendCommandParams(commandData.params));
  149. case 'cdp.getSession':
  150. return this.#contextProcessor.process_cdp_getSession(this.#parser.parseGetSessionParams(commandData.params));
  151. }
  152. // Intentionally kept outside of the switch statement to ensure that
  153. // ESLint @typescript-eslint/switch-exhaustiveness-check triggers if a new
  154. // command is added.
  155. throw new protocol_js_1.Message.UnknownCommandException(`Unknown command '${commandData.method}'.`);
  156. }
  157. async processCommand(command) {
  158. try {
  159. const result = await this.#processCommand(command);
  160. const response = {
  161. id: command.id,
  162. ...result,
  163. };
  164. this.emit('response', OutgoingBidiMessage_js_1.OutgoingBidiMessage.createResolved(response, command.channel ?? null));
  165. }
  166. catch (e) {
  167. if (e instanceof protocol_js_1.Message.ErrorResponse) {
  168. const errorResponse = e;
  169. this.emit('response', OutgoingBidiMessage_js_1.OutgoingBidiMessage.createResolved(errorResponse.toErrorResponse(command.id), command.channel ?? null));
  170. }
  171. else {
  172. const error = e;
  173. this.#logger?.(log_js_1.LogType.bidi, error);
  174. this.emit('response', OutgoingBidiMessage_js_1.OutgoingBidiMessage.createResolved(new protocol_js_1.Message.UnknownErrorException(error.message).toErrorResponse(command.id), command.channel ?? null));
  175. }
  176. }
  177. }
  178. }
  179. exports.CommandProcessor = CommandProcessor;
  180. //# sourceMappingURL=CommandProcessor.js.map