csscoverage.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 {cssUsageSpec} = require("devtools/shared/specs/csscoverage");
  6. const protocol = require("devtools/shared/protocol");
  7. const {custom} = protocol;
  8. const {LocalizationHelper} = require("devtools/shared/l10n");
  9. const L10N = new LocalizationHelper("devtools/shared/locales/csscoverage.properties");
  10. loader.lazyRequireGetter(this, "gDevTools", "devtools/client/framework/devtools", true);
  11. /**
  12. * Allow: let foo = l10n.lookup("csscoverageFoo");
  13. */
  14. const l10n = exports.l10n = {
  15. lookup: (msg) => L10N.getStr(msg)
  16. };
  17. /**
  18. * Running more than one usage report at a time is probably bad for performance
  19. * and it isn't particularly useful, and it's confusing from a notification POV
  20. * so we only allow one.
  21. */
  22. var isRunning = false;
  23. var notification;
  24. var target;
  25. var chromeWindow;
  26. /**
  27. * Front for CSSUsageActor
  28. */
  29. const CSSUsageFront = protocol.FrontClassWithSpec(cssUsageSpec, {
  30. initialize: function (client, form) {
  31. protocol.Front.prototype.initialize.call(this, client, form);
  32. this.actorID = form.cssUsageActor;
  33. this.manage(this);
  34. },
  35. _onStateChange: protocol.preEvent("state-change", function (ev) {
  36. isRunning = ev.isRunning;
  37. ev.target = target;
  38. if (isRunning) {
  39. let gnb = chromeWindow.document.getElementById("global-notificationbox");
  40. notification = gnb.getNotificationWithValue("csscoverage-running");
  41. if (notification == null) {
  42. let notifyStop = reason => {
  43. if (reason == "removed") {
  44. this.stop();
  45. }
  46. };
  47. let msg = l10n.lookup("csscoverageRunningReply");
  48. notification = gnb.appendNotification(msg, "csscoverage-running",
  49. "",
  50. gnb.PRIORITY_INFO_HIGH,
  51. null,
  52. notifyStop);
  53. }
  54. } else {
  55. if (notification) {
  56. notification.remove();
  57. notification = undefined;
  58. }
  59. gDevTools.showToolbox(target, "styleeditor");
  60. target = undefined;
  61. }
  62. }),
  63. /**
  64. * Server-side start is above. Client-side start adds a notification box
  65. */
  66. start: custom(function (newChromeWindow, newTarget, noreload = false) {
  67. target = newTarget;
  68. chromeWindow = newChromeWindow;
  69. return this._start(noreload);
  70. }, {
  71. impl: "_start"
  72. }),
  73. /**
  74. * Server-side start is above. Client-side start adds a notification box
  75. */
  76. toggle: custom(function (newChromeWindow, newTarget) {
  77. target = newTarget;
  78. chromeWindow = newChromeWindow;
  79. return this._toggle();
  80. }, {
  81. impl: "_toggle"
  82. }),
  83. /**
  84. * We count STARTING and STOPPING as 'running'
  85. */
  86. isRunning: function () {
  87. return isRunning;
  88. }
  89. });
  90. exports.CSSUsageFront = CSSUsageFront;
  91. const knownFronts = new WeakMap();
  92. /**
  93. * Create a CSSUsageFront only when needed (returns a promise)
  94. * For notes on target.makeRemote(), see
  95. * https://bugzilla.mozilla.org/show_bug.cgi?id=1016330#c7
  96. */
  97. exports.getUsage = function (trgt) {
  98. return trgt.makeRemote().then(() => {
  99. let front = knownFronts.get(trgt.client);
  100. if (front == null && trgt.form.cssUsageActor != null) {
  101. front = new CSSUsageFront(trgt.client, trgt.form);
  102. knownFronts.set(trgt.client, front);
  103. }
  104. return front;
  105. });
  106. };