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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/* 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/. */
/**
* Injects the form autofill section into about:preferences.
*/
// Add addresses enabled flag in telemetry environment for recording the number of
// users who disable/enable the address autofill feature.
const BUNDLE_URI = "chrome://formautofill/locale/formautofill.properties";
const MANAGE_ADDRESSES_URL =
"chrome://formautofill/content/manageAddresses.xhtml";
const MANAGE_CREDITCARDS_URL =
"chrome://formautofill/content/manageCreditCards.xhtml";
import { FormAutofill } from "resource://autofill/FormAutofill.sys.mjs";
import { FormAutofillUtils } from "resource://gre/modules/shared/FormAutofillUtils.sys.mjs";
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
OSKeyStore: "resource://gre/modules/OSKeyStore.sys.mjs",
});
ChromeUtils.defineLazyGetter(
lazy,
"l10n",
() => new Localization(["browser/preferences/preferences.ftl"], true)
);
const {
ENABLED_AUTOFILL_ADDRESSES_PREF,
ENABLED_AUTOFILL_CREDITCARDS_PREF,
ENABLED_AUTOFILL_CREDITCARDS_REAUTH_PREF,
} = FormAutofill;
const {
MANAGE_ADDRESSES_L10N_IDS,
EDIT_ADDRESS_L10N_IDS,
MANAGE_CREDITCARDS_L10N_IDS,
EDIT_CREDITCARD_L10N_IDS,
} = FormAutofillUtils;
// Add credit card enabled flag in telemetry environment for recording the number of
// users who disable/enable the credit card autofill feature.
const HTML_NS = "http://www.w3.org/1999/xhtml";
export function FormAutofillPreferences() {
this.bundle = Services.strings.createBundle(BUNDLE_URI);
}
FormAutofillPreferences.prototype = {
/**
* Create the Form Autofill preference group.
*
* @param {HTMLDocument} document
* @returns {XULElement}
*/
init(document) {
this.createPreferenceGroup(document);
this.attachEventListeners();
return this.refs.formAutofillFragment;
},
/**
* Remove event listeners and the preference group.
*/
uninit() {
this.detachEventListeners();
this.refs.formAutofillGroup.remove();
},
/**
* Create Form Autofill preference group
*
* @param {HTMLDocument} document
*/
createPreferenceGroup(document) {
let formAutofillFragment = document.createDocumentFragment();
let formAutofillGroupBoxLabel = document.createXULElement("label");
let formAutofillGroupBoxLabelHeading = document.createElementNS(
HTML_NS,
"h2"
);
let formAutofillGroup = document.createXULElement("vbox");
// Wrappers are used to properly compute the search tooltip positions
// let savedAddressesBtnWrapper = document.createXULElement("hbox");
// let savedCreditCardsBtnWrapper = document.createXULElement("hbox");
this.refs = {};
this.refs.formAutofillGroup = formAutofillGroup;
this.refs.formAutofillFragment = formAutofillFragment;
formAutofillGroupBoxLabel.appendChild(formAutofillGroupBoxLabelHeading);
formAutofillFragment.appendChild(formAutofillGroupBoxLabel);
formAutofillFragment.appendChild(formAutofillGroup);
let showAddressUI = FormAutofill.isAutofillAddressesAvailable;
let showCreditCardUI = FormAutofill.isAutofillCreditCardsAvailable;
if (!showAddressUI && !showCreditCardUI) {
return;
}
formAutofillGroupBoxLabelHeading.textContent = lazy.l10n.formatValueSync(
"pane-privacy-autofill-header"
);
if (showAddressUI) {
let savedAddressesBtnWrapper = document.createXULElement("hbox");
let addressAutofill = document.createXULElement("hbox");
let addressAutofillCheckboxGroup = document.createXULElement("hbox");
let addressAutofillCheckbox = document.createXULElement("checkbox");
let addressAutofillLearnMore = document.createElement("a", {
is: "moz-support-link",
});
let savedAddressesBtn = document.createXULElement("button", {
is: "highlightable-button",
});
savedAddressesBtn.className = "accessory-button";
addressAutofillCheckbox.className = "tail-with-learn-more";
formAutofillGroup.id = "formAutofillGroup";
addressAutofill.id = "addressAutofill";
addressAutofillLearnMore.id = "addressAutofillLearnMore";
addressAutofill.setAttribute("data-subcategory", "address-autofill");
addressAutofillCheckbox.setAttribute(
"label",
lazy.l10n.formatValueSync("autofill-addresses-checkbox")
);
savedAddressesBtn.setAttribute(
"label",
lazy.l10n.formatValueSync("autofill-saved-addresses-button")
);
// Align the start to keep the savedAddressesBtn as original size
// when addressAutofillCheckboxGroup's height is changed by a longer l10n string
savedAddressesBtnWrapper.setAttribute("align", "start");
addressAutofillLearnMore.setAttribute(
"support-page",
"autofill-card-address"
);
// Add preferences search support
savedAddressesBtn.setAttribute(
"search-l10n-ids",
MANAGE_ADDRESSES_L10N_IDS.concat(EDIT_ADDRESS_L10N_IDS).join(",")
);
// Manually set the checked state
if (FormAutofill.isAutofillAddressesEnabled) {
addressAutofillCheckbox.setAttribute("checked", true);
}
if (FormAutofill.isAutofillAddressesLocked) {
addressAutofillCheckbox.disabled = true;
}
addressAutofillCheckboxGroup.setAttribute("align", "center");
addressAutofillCheckboxGroup.setAttribute("flex", "1");
formAutofillGroup.appendChild(addressAutofill);
addressAutofill.appendChild(addressAutofillCheckboxGroup);
addressAutofillCheckboxGroup.appendChild(addressAutofillCheckbox);
addressAutofillCheckboxGroup.appendChild(addressAutofillLearnMore);
addressAutofill.appendChild(savedAddressesBtnWrapper);
savedAddressesBtnWrapper.appendChild(savedAddressesBtn);
this.refs.formAutofillFragment = formAutofillFragment;
this.refs.addressAutofillCheckbox = addressAutofillCheckbox;
this.refs.savedAddressesBtn = savedAddressesBtn;
}
if (showCreditCardUI) {
let savedCreditCardsBtnWrapper = document.createXULElement("hbox");
let creditCardAutofill = document.createXULElement("hbox");
let creditCardAutofillCheckboxGroup = document.createXULElement("hbox");
let creditCardAutofillCheckbox = document.createXULElement("checkbox");
let creditCardAutofillLearnMore = document.createElement("a", {
is: "moz-support-link",
});
let savedCreditCardsBtn = document.createXULElement("button", {
is: "highlightable-button",
});
savedCreditCardsBtn.className = "accessory-button";
creditCardAutofillCheckbox.className = "tail-with-learn-more";
creditCardAutofill.id = "creditCardAutofill";
creditCardAutofillLearnMore.id = "creditCardAutofillLearnMore";
creditCardAutofill.setAttribute(
"data-subcategory",
"credit-card-autofill"
);
creditCardAutofillCheckbox.setAttribute(
"label",
lazy.l10n.formatValueSync("autofill-payment-methods-checkbox-message")
);
savedCreditCardsBtn.setAttribute(
"label",
lazy.l10n.formatValueSync("autofill-saved-payment-methods-button")
);
// Align the start to keep the savedCreditCardsBtn as original size
// when creditCardAutofillCheckboxGroup's height is changed by a longer l10n string
savedCreditCardsBtnWrapper.setAttribute("align", "start");
creditCardAutofillLearnMore.setAttribute(
"support-page",
"credit-card-autofill"
);
let creditCardsAutofillDescription =
document.createXULElement("description");
creditCardsAutofillDescription.setAttribute("flex", "1");
creditCardsAutofillDescription.className = "indent tip-caption";
creditCardsAutofillDescription.setAttribute("data-l10n-attrs", "hidden");
creditCardsAutofillDescription.setAttribute(
"data-l10n-id",
"autofill-payment-methods-checkbox-submessage"
);
// Add preferences search support
savedCreditCardsBtn.setAttribute(
"search-l10n-ids",
MANAGE_CREDITCARDS_L10N_IDS.concat(EDIT_CREDITCARD_L10N_IDS).join(",")
);
// Manually set the checked state
if (FormAutofill.isAutofillCreditCardsEnabled) {
creditCardAutofillCheckbox.setAttribute("checked", true);
}
if (FormAutofill.isAutofillCreditCardsLocked) {
creditCardAutofillCheckbox.disabled = true;
}
creditCardAutofillCheckboxGroup.setAttribute("align", "center");
creditCardAutofillCheckboxGroup.setAttribute("flex", "1");
formAutofillGroup.appendChild(creditCardAutofill);
creditCardAutofill.appendChild(creditCardAutofillCheckboxGroup);
creditCardAutofillCheckboxGroup.appendChild(creditCardAutofillCheckbox);
creditCardAutofillCheckboxGroup.appendChild(creditCardAutofillLearnMore);
creditCardAutofill.appendChild(savedCreditCardsBtnWrapper);
savedCreditCardsBtnWrapper.appendChild(savedCreditCardsBtn);
formAutofillGroup.appendChild(creditCardsAutofillDescription);
this.refs.creditCardAutofillCheckbox = creditCardAutofillCheckbox;
this.refs.savedCreditCardsBtn = savedCreditCardsBtn;
if (lazy.OSKeyStore.canReauth()) {
let reauth = document.createXULElement("hbox");
let reauthCheckboxGroup = document.createXULElement("hbox");
let reauthCheckbox = document.createXULElement("checkbox");
let reauthLearnMore = document.createElement("a", {
is: "moz-support-link",
});
reauthCheckboxGroup.classList.add("indent");
reauthCheckbox.classList.add("tail-with-learn-more");
reauthCheckbox.disabled = !FormAutofill.isAutofillCreditCardsEnabled;
reauth.id = "creditCardReauthenticate";
reauthLearnMore.id = "creditCardReauthenticateLearnMore";
reauth.setAttribute("data-subcategory", "reauth-credit-card-autofill");
reauthCheckbox.setAttribute(
"label",
lazy.l10n.formatValueSync("autofill-reauth-checkbox")
);
reauthLearnMore.setAttribute(
"support-page",
"credit-card-autofill#w_require-authentication-for-autofill"
);
// Manually set the checked state
if (FormAutofillUtils._reauthEnabledByUser) {
reauthCheckbox.setAttribute("checked", true);
}
reauthCheckboxGroup.setAttribute("align", "center");
reauthCheckboxGroup.setAttribute("flex", "1");
formAutofillGroup.appendChild(reauth);
reauth.appendChild(reauthCheckboxGroup);
reauthCheckboxGroup.appendChild(reauthCheckbox);
reauthCheckboxGroup.appendChild(reauthLearnMore);
this.refs.reauthCheckbox = reauthCheckbox;
}
}
},
/**
* Handle events
*
* @param {DOMEvent} event
*/
async handleEvent(event) {
switch (event.type) {
case "command": {
let target = event.target;
if (target == this.refs.addressAutofillCheckbox) {
// Set preference directly instead of relying on <Preference>
Services.prefs.setBoolPref(
ENABLED_AUTOFILL_ADDRESSES_PREF,
target.checked
);
} else if (target == this.refs.creditCardAutofillCheckbox) {
Services.prefs.setBoolPref(
ENABLED_AUTOFILL_CREDITCARDS_PREF,
target.checked
);
if (this.refs.reauthCheckbox) {
this.refs.reauthCheckbox.disabled = !target.checked;
}
} else if (target == this.refs.reauthCheckbox) {
if (!lazy.OSKeyStore.canReauth()) {
break;
}
let messageTextId = "autofillReauthOSDialog";
// We reuse the if/else order from wizard markup to increase
// odds of consistent behavior.
if (AppConstants.platform == "macosx") {
messageTextId += "Mac";
} else if (AppConstants.platform == "linux") {
messageTextId += "Lin";
} else {
messageTextId += "Win";
}
let messageText = this.bundle.GetStringFromName(messageTextId);
const brandBundle = Services.strings.createBundle(
"chrome://branding/locale/brand.properties"
);
let win = target.ownerGlobal.docShell.chromeEventHandler.ownerGlobal;
let loggedIn = await lazy.OSKeyStore.ensureLoggedIn(
messageText,
brandBundle.GetStringFromName("brandFullName"),
win,
false
);
if (!loggedIn.authenticated) {
target.checked = !target.checked;
break;
}
Services.prefs.setBoolPref(
ENABLED_AUTOFILL_CREDITCARDS_REAUTH_PREF,
target.checked
);
} else if (target == this.refs.savedAddressesBtn) {
target.ownerGlobal.gSubDialog.open(MANAGE_ADDRESSES_URL);
} else if (target == this.refs.savedCreditCardsBtn) {
target.ownerGlobal.gSubDialog.open(MANAGE_CREDITCARDS_URL);
}
break;
}
case "click": {
let target = event.target;
if (target == this.refs.addressAutofillCheckboxLabel) {
let pref = FormAutofill.isAutofillAddressesEnabled;
Services.prefs.setBoolPref(ENABLED_AUTOFILL_ADDRESSES_PREF, !pref);
this.refs.addressAutofillCheckbox.checked = !pref;
} else if (target == this.refs.creditCardAutofillCheckboxLabel) {
let pref = FormAutofill.isAutofillCreditCardsEnabled;
Services.prefs.setBoolPref(ENABLED_AUTOFILL_CREDITCARDS_PREF, !pref);
this.refs.creditCardAutofillCheckbox.checked = !pref;
this.refs.reauthCheckbox.disabled = pref;
}
break;
}
}
},
/**
* Attach event listener
*/
attachEventListeners() {
this.refs.formAutofillGroup.addEventListener("command", this);
this.refs.formAutofillGroup.addEventListener("click", this);
},
/**
* Remove event listener
*/
detachEventListeners() {
this.refs.formAutofillGroup.removeEventListener("command", this);
this.refs.formAutofillGroup.removeEventListener("click", this);
},
};
|