gcli.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { Front, FrontClassWithSpec } = require("devtools/shared/protocol");
  6. const { gcliSpec } = require("devtools/shared/specs/gcli");
  7. /**
  8. *
  9. */
  10. const GcliFront = exports.GcliFront = FrontClassWithSpec(gcliSpec, {
  11. initialize: function (client, tabForm) {
  12. Front.prototype.initialize.call(this, client);
  13. this.actorID = tabForm.gcliActor;
  14. // XXX: This is the first actor type in its hierarchy to use the protocol
  15. // library, so we're going to self-own on the client side for now.
  16. this.manage(this);
  17. },
  18. });
  19. // A cache of created fronts: WeakMap<Client, Front>
  20. const knownFronts = new WeakMap();
  21. /**
  22. * Create a GcliFront only when needed (returns a promise)
  23. * For notes on target.makeRemote(), see
  24. * https://bugzilla.mozilla.org/show_bug.cgi?id=1016330#c7
  25. */
  26. exports.GcliFront.create = function (target) {
  27. return target.makeRemote().then(() => {
  28. let front = knownFronts.get(target.client);
  29. if (front == null && target.form.gcliActor != null) {
  30. front = new GcliFront(target.client, target.form);
  31. knownFronts.set(target.client, front);
  32. }
  33. return front;
  34. });
  35. };