paintflashing.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 { Ci } = require("chrome");
  6. loader.lazyRequireGetter(this, "getOuterId", "sdk/window/utils", true);
  7. loader.lazyRequireGetter(this, "getBrowserForTab", "sdk/tabs/utils", true);
  8. var telemetry;
  9. try {
  10. const Telemetry = require("devtools/client/shared/telemetry");
  11. telemetry = new Telemetry();
  12. } catch(e) {
  13. // DevTools Telemetry module only available in Firefox
  14. }
  15. const EventEmitter = require("devtools/shared/event-emitter");
  16. const eventEmitter = new EventEmitter();
  17. const gcli = require("gcli/index");
  18. const l10n = require("gcli/l10n");
  19. const enabledPaintFlashing = new Set();
  20. const isCheckedFor = (tab) =>
  21. tab ? enabledPaintFlashing.has(getBrowserForTab(tab).outerWindowID) : false;
  22. /**
  23. * Fire events and telemetry when paintFlashing happens
  24. */
  25. function onPaintFlashingChanged(target, state) {
  26. const { flashing, id } = state;
  27. if (flashing) {
  28. enabledPaintFlashing.add(id);
  29. } else {
  30. enabledPaintFlashing.delete(id);
  31. }
  32. eventEmitter.emit("changed", { target: target });
  33. function fireChange() {
  34. eventEmitter.emit("changed", { target: target });
  35. }
  36. target.off("navigate", fireChange);
  37. target.once("navigate", fireChange);
  38. if (!telemetry) {
  39. return;
  40. }
  41. if (flashing) {
  42. telemetry.toolOpened("paintflashing");
  43. } else {
  44. telemetry.toolClosed("paintflashing");
  45. }
  46. }
  47. /**
  48. * Alter the paintFlashing state of a window and report on the new value.
  49. * This works with chrome or content windows.
  50. *
  51. * This is a bizarre method that you could argue should be broken up into
  52. * separate getter and setter functions, however keeping it as one helps
  53. * to simplify the commands below.
  54. *
  55. * @param state {string} One of:
  56. * - "on" which does window.paintFlashing = true
  57. * - "off" which does window.paintFlashing = false
  58. * - "toggle" which does window.paintFlashing = !window.paintFlashing
  59. * - "query" which does nothing
  60. * @return The new value of the window.paintFlashing flag
  61. */
  62. function setPaintFlashing(window, state) {
  63. const winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
  64. .getInterface(Ci.nsIDOMWindowUtils);
  65. if (!["on", "off", "toggle", "query"].includes(state)) {
  66. throw new Error(`Unsupported state: ${state}`);
  67. }
  68. if (state === "on") {
  69. winUtils.paintFlashing = true;
  70. } else if (state === "off") {
  71. winUtils.paintFlashing = false;
  72. } else if (state === "toggle") {
  73. winUtils.paintFlashing = !winUtils.paintFlashing;
  74. }
  75. return winUtils.paintFlashing;
  76. }
  77. exports.items = [
  78. {
  79. name: "paintflashing",
  80. description: l10n.lookup("paintflashingDesc")
  81. },
  82. {
  83. item: "command",
  84. runAt: "client",
  85. name: "paintflashing on",
  86. description: l10n.lookup("paintflashingOnDesc"),
  87. manual: l10n.lookup("paintflashingManual"),
  88. params: [{
  89. group: "options",
  90. params: [
  91. {
  92. type: "boolean",
  93. name: "chrome",
  94. get hidden() {
  95. return gcli.hiddenByChromePref();
  96. },
  97. description: l10n.lookup("paintflashingChromeDesc"),
  98. }
  99. ]
  100. }],
  101. exec: function*(args, context) {
  102. if (!args.chrome) {
  103. const output = yield context.updateExec("paintflashing_server --state on");
  104. onPaintFlashingChanged(context.environment.target, output.data);
  105. } else {
  106. setPaintFlashing(context.environment.chromeWindow, "on");
  107. }
  108. }
  109. },
  110. {
  111. item: "command",
  112. runAt: "client",
  113. name: "paintflashing off",
  114. description: l10n.lookup("paintflashingOffDesc"),
  115. manual: l10n.lookup("paintflashingManual"),
  116. params: [{
  117. group: "options",
  118. params: [
  119. {
  120. type: "boolean",
  121. name: "chrome",
  122. get hidden() {
  123. return gcli.hiddenByChromePref();
  124. },
  125. description: l10n.lookup("paintflashingChromeDesc"),
  126. }
  127. ]
  128. }],
  129. exec: function*(args, context) {
  130. if (!args.chrome) {
  131. const output = yield context.updateExec("paintflashing_server --state off");
  132. onPaintFlashingChanged(context.environment.target, output.data);
  133. } else {
  134. setPaintFlashing(context.environment.chromeWindow, "off");
  135. }
  136. }
  137. },
  138. {
  139. item: "command",
  140. runAt: "client",
  141. name: "paintflashing toggle",
  142. hidden: true,
  143. buttonId: "command-button-paintflashing",
  144. buttonClass: "command-button command-button-invertable",
  145. state: {
  146. isChecked: ({_tab}) => isCheckedFor(_tab),
  147. onChange: (_, handler) => eventEmitter.on("changed", handler),
  148. offChange: (_, handler) => eventEmitter.off("changed", handler),
  149. },
  150. tooltipText: l10n.lookup("paintflashingTooltip"),
  151. description: l10n.lookup("paintflashingToggleDesc"),
  152. manual: l10n.lookup("paintflashingManual"),
  153. exec: function*(args, context) {
  154. const output = yield context.updateExec("paintflashing_server --state toggle");
  155. onPaintFlashingChanged(context.environment.target, output.data);
  156. }
  157. },
  158. {
  159. item: "command",
  160. runAt: "server",
  161. name: "paintflashing_server",
  162. hidden: true,
  163. params: [
  164. {
  165. name: "state",
  166. type: {
  167. name: "selection",
  168. data: [ "on", "off", "toggle", "query" ]
  169. }
  170. },
  171. ],
  172. returnType: "paintFlashingState",
  173. exec: function(args, context) {
  174. let { window } = context.environment;
  175. let id = getOuterId(window);
  176. let flashing = setPaintFlashing(window, args.state);
  177. return { flashing, id };
  178. }
  179. }
  180. ];