actor-registry.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 { components } = require("chrome");
  6. const Services = require("Services");
  7. const { actorActorSpec, actorRegistrySpec } = require("devtools/shared/specs/actor-registry");
  8. const protocol = require("devtools/shared/protocol");
  9. const { custom } = protocol;
  10. loader.lazyImporter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm");
  11. const ActorActorFront = protocol.FrontClassWithSpec(actorActorSpec, {
  12. initialize: function (client, form) {
  13. protocol.Front.prototype.initialize.call(this, client, form);
  14. }
  15. });
  16. exports.ActorActorFront = ActorActorFront;
  17. function request(uri) {
  18. return new Promise((resolve, reject) => {
  19. try {
  20. uri = Services.io.newURI(uri, null, null);
  21. } catch (e) {
  22. reject(e);
  23. }
  24. NetUtil.asyncFetch({
  25. uri,
  26. loadUsingSystemPrincipal: true,
  27. }, (stream, status, req) => {
  28. if (!components.isSuccessCode(status)) {
  29. reject(new Error("Request failed with status code = "
  30. + status
  31. + " after NetUtil.asyncFetch for url = "
  32. + uri));
  33. return;
  34. }
  35. let source = NetUtil.readInputStreamToString(stream, stream.available());
  36. stream.close();
  37. resolve(source);
  38. });
  39. });
  40. }
  41. const ActorRegistryFront = protocol.FrontClassWithSpec(actorRegistrySpec, {
  42. initialize: function (client, form) {
  43. protocol.Front.prototype.initialize.call(this, client,
  44. { actor: form.actorRegistryActor });
  45. this.manage(this);
  46. },
  47. registerActor: custom(function (uri, options) {
  48. return request(uri, options)
  49. .then(sourceText => {
  50. return this._registerActor(sourceText, uri, options);
  51. });
  52. }, {
  53. impl: "_registerActor"
  54. })
  55. });
  56. exports.ActorRegistryFront = ActorRegistryFront;