1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* import-globals-from preferences.js */
ChromeUtils.defineESModuleGetters(this, {
GenAI: "resource:///modules/GenAI.sys.mjs",
});
var gExperimentalPane = {
inited: false,
_template: null,
_featureGatesContainer: null,
_boundRestartObserver: null,
_observedPrefs: [],
_shouldPromptForRestart: true,
_featureGatePrefTypeToPrefServiceType(featureGatePrefType) {
if (featureGatePrefType != "boolean") {
throw new Error("Only boolean FeatureGates are supported");
}
return "bool";
},
async _observeRestart(aSubject, aTopic, aData) {
if (!this._shouldPromptForRestart) {
return;
}
let prefValue = Services.prefs.getBoolPref(aData);
let buttonIndex = await confirmRestartPrompt(prefValue, 1, true, false);
if (buttonIndex == CONFIRM_RESTART_PROMPT_RESTART_NOW) {
Services.startup.quit(
Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart
);
return;
}
this._shouldPromptForRestart = false;
Services.prefs.setBoolPref(aData, !prefValue);
this._shouldPromptForRestart = true;
},
addPrefObserver(name, fn) {
this._observedPrefs.push({ name, fn });
Services.prefs.addObserver(name, fn);
},
removePrefObservers() {
for (let { name, fn } of this._observedPrefs) {
Services.prefs.removeObserver(name, fn);
}
this._observedPrefs = [];
},
// Reset the features to their default values
async resetAllFeatures() {
let features = await gExperimentalPane.getFeatures();
for (let feature of features) {
Services.prefs.setBoolPref(feature.preference, feature.defaultValue);
}
},
async getFeatures() {
let searchParams = new URLSearchParams(document.documentURIObject.query);
let definitionsUrl = searchParams.get("definitionsUrl");
let features = await FeatureGate.all(definitionsUrl);
return features.filter(f => f.isPublic);
},
async _sortFeatures(features) {
// Sort the features alphabetically by their title
let titles = await document.l10n.formatMessages(
features.map(f => {
return { id: f.title };
})
);
titles = titles.map((title, index) => [title.attributes[0].value, index]);
titles.sort((a, b) => a[0].toLowerCase().localeCompare(b[0].toLowerCase()));
// Get the features in order of sorted titles.
return titles.map(([, index]) => features[index]);
},
async init() {
if (this.inited) {
return;
}
this.inited = true;
let features = await this.getFeatures();
let shouldHide = !features.length;
document.getElementById("category-experimental").hidden = shouldHide;
// Cache the visibility so we can show it quicker in subsequent loads.
Services.prefs.setBoolPref(
"browser.preferences.experimental.hidden",
shouldHide
);
if (shouldHide) {
// Remove the 'experimental' category if there are no available features
document.getElementById("firefoxExperimentalCategory").remove();
if (
document.getElementById("categories").selectedItem?.id ==
"category-experimental"
) {
// Leave the 'experimental' category if there are no available features
gotoPref("general");
return;
}
}
features = await this._sortFeatures(features);
setEventListener(
"experimentalCategory-reset",
"command",
gExperimentalPane.resetAllFeatures
);
window.addEventListener("unload", () => this.removePrefObservers());
this._template = document.getElementById("template-featureGate");
this._featureGatesContainer = document.getElementById(
"pane-experimental-featureGates"
);
this._boundRestartObserver = this._observeRestart.bind(this);
let frag = document.createDocumentFragment();
for (let feature of features) {
if (Preferences.get(feature.preference)) {
console.error(
"Preference control already exists for experimental feature '" +
feature.id +
"' with preference '" +
feature.preference +
"'"
);
continue;
}
if (feature.restartRequired) {
this.addPrefObserver(feature.preference, this._boundRestartObserver);
}
let template = this._template.content.cloneNode(true);
let description = template.querySelector(".featureGateDescription");
description.id = feature.id + "-description";
let descriptionLinks = feature.descriptionLinks || {};
for (let [key, value] of Object.entries(descriptionLinks)) {
let link = document.createElement("a");
link.setAttribute("data-l10n-name", key);
link.setAttribute("href", value);
link.setAttribute("target", "_blank");
description.append(link);
}
document.l10n.setAttributes(description, feature.description);
let checkbox = template.querySelector(".featureGateCheckbox");
checkbox.setAttribute("preference", feature.preference);
checkbox.id = feature.id;
checkbox.setAttribute("aria-describedby", description.id);
document.l10n.setAttributes(checkbox, feature.title);
let extraTemplate = document.getElementById(`template-${feature.id}`);
if (extraTemplate) {
template
.querySelector(".featureGate")
.appendChild(extraTemplate.content.cloneNode(true));
}
frag.appendChild(template);
let preference = Preferences.add({
id: feature.preference,
type: gExperimentalPane._featureGatePrefTypeToPrefServiceType(
feature.type
),
});
preference.setElementValue(checkbox);
}
this._featureGatesContainer.appendChild(frag);
// Bug 1895494 to allow more generic logic
GenAI.buildPreferences(window);
},
};
|