cdpTarget.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CdpTarget = void 0;
  4. const logManager_js_1 = require("../log/logManager.js");
  5. const deferred_js_1 = require("../../../utils/deferred.js");
  6. const networkProcessor_js_1 = require("../network/networkProcessor.js");
  7. class CdpTarget {
  8. #targetId;
  9. #parentTargetId;
  10. #cdpClient;
  11. #cdpSessionId;
  12. #eventManager;
  13. #preloadScriptStorage;
  14. #targetUnblocked;
  15. #networkDomainActivated;
  16. static create(targetId, parentTargetId, cdpClient, cdpSessionId, realmStorage, eventManager, preloadScriptStorage) {
  17. const cdpTarget = new CdpTarget(targetId, parentTargetId, cdpClient, cdpSessionId, eventManager, preloadScriptStorage);
  18. logManager_js_1.LogManager.create(cdpTarget, realmStorage, eventManager);
  19. cdpTarget.#setEventListeners();
  20. // No need to await.
  21. // Deferred will be resolved when the target is unblocked.
  22. void cdpTarget.#unblock();
  23. return cdpTarget;
  24. }
  25. constructor(targetId, parentTargetId, cdpClient, cdpSessionId, eventManager, preloadScriptStorage) {
  26. this.#targetId = targetId;
  27. this.#parentTargetId = parentTargetId;
  28. this.#cdpClient = cdpClient;
  29. this.#cdpSessionId = cdpSessionId;
  30. this.#eventManager = eventManager;
  31. this.#preloadScriptStorage = preloadScriptStorage;
  32. this.#networkDomainActivated = false;
  33. this.#targetUnblocked = new deferred_js_1.Deferred();
  34. }
  35. /** Returns a promise that resolves when the target is unblocked. */
  36. get targetUnblocked() {
  37. return this.#targetUnblocked;
  38. }
  39. get targetId() {
  40. return this.#targetId;
  41. }
  42. get cdpClient() {
  43. return this.#cdpClient;
  44. }
  45. /**
  46. * Needed for CDP escape path.
  47. */
  48. get cdpSessionId() {
  49. return this.#cdpSessionId;
  50. }
  51. /**
  52. * Enables all the required CDP domains and unblocks the target.
  53. */
  54. async #unblock() {
  55. try {
  56. // Enable Network domain, if it is enabled globally.
  57. // TODO: enable Network domain for OOPiF targets.
  58. if (this.#eventManager.isNetworkDomainEnabled) {
  59. await this.enableNetworkDomain();
  60. }
  61. await this.#cdpClient.sendCommand('Runtime.enable');
  62. await this.#cdpClient.sendCommand('Page.enable');
  63. await this.#cdpClient.sendCommand('Page.setLifecycleEventsEnabled', {
  64. enabled: true,
  65. });
  66. await this.#cdpClient.sendCommand('Target.setAutoAttach', {
  67. autoAttach: true,
  68. waitForDebuggerOnStart: true,
  69. flatten: true,
  70. });
  71. await this.#initAndEvaluatePreloadScripts();
  72. await this.#cdpClient.sendCommand('Runtime.runIfWaitingForDebugger');
  73. }
  74. catch (error) {
  75. // The target might have been closed before the initialization finished.
  76. if (!this.#cdpClient.isCloseError(error)) {
  77. throw error;
  78. }
  79. }
  80. this.#targetUnblocked.resolve();
  81. }
  82. /**
  83. * Enables the Network domain (creates NetworkProcessor on the target's cdp
  84. * client) if it is not enabled yet.
  85. */
  86. async enableNetworkDomain() {
  87. if (!this.#networkDomainActivated) {
  88. this.#networkDomainActivated = true;
  89. await networkProcessor_js_1.NetworkProcessor.create(this.cdpClient, this.#eventManager);
  90. }
  91. }
  92. #setEventListeners() {
  93. this.#cdpClient.on('*', (event, params) => {
  94. // We may encounter uses for EventEmitter other than CDP events,
  95. // which we want to skip.
  96. if (typeof event !== 'string') {
  97. return;
  98. }
  99. this.#eventManager.registerEvent({
  100. method: `cdp.${event}`,
  101. params: {
  102. event,
  103. params: params,
  104. session: this.#cdpSessionId,
  105. },
  106. }, null);
  107. });
  108. }
  109. /**
  110. * All the ProxyChannels from all the preload scripts of the given
  111. * BrowsingContext.
  112. */
  113. getChannels(contextId) {
  114. return this.#preloadScriptStorage
  115. .findPreloadScripts({
  116. contextIds: [null, contextId],
  117. })
  118. .flatMap((script) => script.channels);
  119. }
  120. /** Loads all top-level and parent preload scripts. */
  121. async #initAndEvaluatePreloadScripts() {
  122. for (const script of this.#preloadScriptStorage.findPreloadScripts({
  123. contextIds: [null, this.#parentTargetId],
  124. })) {
  125. await script.initInTarget(this);
  126. // Upon attaching to a new target, schedule running preload scripts right
  127. // after `Runtime.runIfWaitingForDebugger`, but don't wait for the result.
  128. script.scheduleEvaluateInTarget(this);
  129. }
  130. }
  131. }
  132. exports.CdpTarget = CdpTarget;
  133. //# sourceMappingURL=cdpTarget.js.map