worker.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. // This function is used to do remote procedure calls from the worker to the
  3. // main thread. It is exposed as a built-in global to every module by the
  4. // worker loader. To make sure the worker loader can access it, it needs to be
  5. // defined before loading the worker loader script below.
  6. this.rpc = function (method, ...params) {
  7. let id = nextId++;
  8. postMessage(JSON.stringify({
  9. type: "rpc",
  10. method: method,
  11. params: params,
  12. id: id
  13. }));
  14. let deferred = Promise.defer();
  15. rpcDeferreds[id] = deferred;
  16. return deferred.promise;
  17. };
  18. loadSubScript("resource://devtools/shared/worker/loader.js");
  19. var Promise = worker.require("promise");
  20. var { ActorPool } = worker.require("devtools/server/actors/common");
  21. var { ThreadActor } = worker.require("devtools/server/actors/script");
  22. var { WebConsoleActor } = worker.require("devtools/server/actors/webconsole");
  23. var { TabSources } = worker.require("devtools/server/actors/utils/TabSources");
  24. var makeDebugger = worker.require("devtools/server/actors/utils/make-debugger");
  25. var { DebuggerServer } = worker.require("devtools/server/main");
  26. DebuggerServer.init();
  27. DebuggerServer.createRootActor = function () {
  28. throw new Error("Should never get here!");
  29. };
  30. var connections = Object.create(null);
  31. var nextId = 0;
  32. var rpcDeferreds = [];
  33. this.addEventListener("message", function (event) {
  34. let packet = JSON.parse(event.data);
  35. switch (packet.type) {
  36. case "connect":
  37. // Step 3: Create a connection to the parent.
  38. let connection = DebuggerServer.connectToParent(packet.id, this);
  39. connections[packet.id] = {
  40. connection : connection,
  41. rpcs: []
  42. };
  43. // Step 4: Create a thread actor for the connection to the parent.
  44. let pool = new ActorPool(connection);
  45. connection.addActorPool(pool);
  46. let sources = null;
  47. let parent = {
  48. actorID: packet.id,
  49. makeDebugger: makeDebugger.bind(null, {
  50. findDebuggees: () => {
  51. return [this.global];
  52. },
  53. shouldAddNewGlobalAsDebuggee: () => {
  54. return true;
  55. },
  56. }),
  57. get sources() {
  58. if (sources === null) {
  59. sources = new TabSources(threadActor);
  60. }
  61. return sources;
  62. },
  63. window: global
  64. };
  65. let threadActor = new ThreadActor(parent, global);
  66. pool.addActor(threadActor);
  67. let consoleActor = new WebConsoleActor(connection, parent);
  68. pool.addActor(consoleActor);
  69. // Step 5: Send a response packet to the parent to notify
  70. // it that a connection has been established.
  71. postMessage(JSON.stringify({
  72. type: "connected",
  73. id: packet.id,
  74. threadActor: threadActor.actorID,
  75. consoleActor: consoleActor.actorID,
  76. }));
  77. break;
  78. case "disconnect":
  79. connections[packet.id].connection.close();
  80. break;
  81. case "rpc":
  82. let deferred = rpcDeferreds[packet.id];
  83. delete rpcDeferreds[packet.id];
  84. if (packet.error) {
  85. deferred.reject(packet.error);
  86. }
  87. deferred.resolve(packet.result);
  88. break;
  89. }
  90. });