no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / content / aboutGlean.js
blob44339c80d13d34f0a5669ee115926bbfd38ead7b
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/. */
5 "use strict";
7 const { AppConstants } = ChromeUtils.importESModule(
8   "resource://gre/modules/AppConstants.sys.mjs"
9 );
11 function updatePrefsAndDefines() {
12   let upload = Services.prefs.getBoolPref(
13     "datareporting.healthreport.uploadEnabled"
14   );
15   document.l10n.setAttributes(
16     document.querySelector("[data-l10n-id='about-glean-data-upload']"),
17     "about-glean-data-upload",
18     {
19       "data-upload-pref-value": upload,
20     }
21   );
22   let port = Services.prefs.getIntPref("telemetry.fog.test.localhost_port");
23   document.l10n.setAttributes(
24     document.querySelector("[data-l10n-id='about-glean-local-port']"),
25     "about-glean-local-port",
26     {
27       "local-port-pref-value": port,
28     }
29   );
30   document.l10n.setAttributes(
31     document.querySelector("[data-l10n-id='about-glean-glean-android']"),
32     "about-glean-glean-android",
33     { "glean-android-define-value": AppConstants.MOZ_GLEAN_ANDROID }
34   );
35   document.l10n.setAttributes(
36     document.querySelector("[data-l10n-id='about-glean-moz-official']"),
37     "about-glean-moz-official",
38     { "moz-official-define-value": AppConstants.MOZILLA_OFFICIAL }
39   );
41   // Knowing what we know, and copying logic from viaduct_uploader.rs,
42   // (which is documented in Preferences and Defines),
43   // tell the fine user whether and why upload is disabled.
44   let uploadMessageEl = document.getElementById("upload-status");
45   let uploadL10nId = "about-glean-upload-enabled";
46   if (!upload) {
47     uploadL10nId = "about-glean-upload-disabled";
48   } else if (port < 0 || (port == 0 && !AppConstants.MOZILLA_OFFICIAL)) {
49     uploadL10nId = "about-glean-upload-fake-enabled";
50     // This message has a link to the Glean Debug Ping Viewer in it.
51     // We must add the anchor element now so that Fluent can match it.
52     let a = document.createElement("a");
53     a.href = "https://debug-ping-preview.firebaseapp.com/";
54     a.setAttribute("data-l10n-name", "glean-debug-ping-viewer");
55     uploadMessageEl.appendChild(a);
56   } else if (port > 0) {
57     uploadL10nId = "about-glean-upload-enabled-local";
58   }
59   document.l10n.setAttributes(uploadMessageEl, uploadL10nId);
62 function camelToKebab(str) {
63   let out = "";
64   for (let i = 0; i < str.length; i++) {
65     let c = str.charAt(i);
66     if (c == c.toUpperCase()) {
67       out += "-";
68       c = c.toLowerCase();
69     }
70     out += c;
71   }
72   return out;
75 // I'm consciously omitting "deletion-request" until someone can come up with
76 // a use-case for sending it via about:glean.
77 const GLEAN_BUILTIN_PINGS = ["metrics", "events", "baseline"];
78 const NO_PING = "(don't submit any ping)";
79 function refillPingNames() {
80   let select = document.getElementById("ping-names");
81   let pings = GLEAN_BUILTIN_PINGS.slice().concat(Object.keys(GleanPings));
83   pings.forEach(ping => {
84     let option = document.createElement("option");
85     option.textContent = camelToKebab(ping);
86     select.appendChild(option);
87   });
88   let option = document.createElement("option");
89   document.l10n.setAttributes(option, "about-glean-no-ping-label");
90   option.value = NO_PING;
91   select.appendChild(option);
94 // If there's been a previous tag, use it.
95 // If not, be _slightly_ clever and derive a default one from the profile dir.
96 function fillDebugTag() {
97   const DEBUG_TAG_PREF = "telemetry.fog.aboutGlean.debugTag";
98   let debugTag;
99   if (Services.prefs.prefHasUserValue(DEBUG_TAG_PREF)) {
100     debugTag = Services.prefs.getStringPref(DEBUG_TAG_PREF);
101   } else {
102     const debugTagPrefix = "about-glean-";
103     const profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
104     let charSum = Array.from(profileDir).reduce(
105       (prev, cur) => prev + cur.charCodeAt(0),
106       0
107     );
109     debugTag = debugTagPrefix + (charSum % 1000);
110   }
112   let tagInput = document.getElementById("tag-pings");
113   tagInput.value = debugTag;
114   const updateDebugTagValues = () => {
115     document.l10n.setAttributes(
116       document.querySelector(
117         "[data-l10n-id='about-glean-label-for-controls-submit']"
118       ),
119       "about-glean-label-for-controls-submit",
120       { "debug-tag": tagInput.value }
121     );
122     const GDPV_ROOT = "https://debug-ping-preview.firebaseapp.com/pings/";
123     let gdpvLink = document.querySelector(
124       "[data-l10n-name='gdpv-tagged-pings-link']"
125     );
126     gdpvLink.href = GDPV_ROOT + tagInput.value;
127   };
128   tagInput.addEventListener("change", () => {
129     Services.prefs.setStringPref(DEBUG_TAG_PREF, tagInput.value);
130     updateDebugTagValues();
131   });
132   updateDebugTagValues();
135 function onLoad() {
136   updatePrefsAndDefines();
137   refillPingNames();
138   fillDebugTag();
139   document.getElementById("controls-submit").addEventListener("click", () => {
140     let tag = document.getElementById("tag-pings").value;
141     let log = document.getElementById("log-pings").checked;
142     let ping = document.getElementById("ping-names").value;
143     Services.fog.setLogPings(log);
144     Services.fog.setTagPings(tag);
145     if (ping != NO_PING) {
146       Services.fog.sendPing(ping);
147     }
148   });
151 window.addEventListener("load", onLoad);