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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
/* 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/. */
/*
* Implements doorhanger singleton that wraps up the PopupNotifications and handles
* the doorhager UI for formautofill related features.
*/
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { FormAutofill } from "resource://autofill/FormAutofill.sys.mjs";
import { FormAutofillUtils } from "resource://gre/modules/shared/FormAutofillUtils.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const { AutofillTelemetry } = ChromeUtils.import(
"resource://autofill/AutofillTelemetry.jsm"
);
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
CreditCard: "resource://gre/modules/CreditCard.sys.mjs",
});
XPCOMUtils.defineLazyGetter(lazy, "log", () =>
FormAutofill.defineLogGetter(lazy, "FormAutofillPrompter")
);
const { ENABLED_AUTOFILL_CREDITCARDS_PREF } = FormAutofill;
const GetStringFromName = FormAutofillUtils.stringBundle.GetStringFromName;
const formatStringFromName =
FormAutofillUtils.stringBundle.formatStringFromName;
const brandShortName =
FormAutofillUtils.brandBundle.GetStringFromName("brandShortName");
let changeAutofillOptsKey = "changeAutofillOptions";
let autofillOptsKey = "autofillOptionsLink";
if (AppConstants.platform == "macosx") {
changeAutofillOptsKey += "OSX";
autofillOptsKey += "OSX";
}
const CONTENT = {
addFirstTimeUse: {
notificationId: "autofill-address",
message: formatStringFromName("saveAddressesMessage", [brandShortName]),
anchor: {
id: "autofill-address-notification-icon",
URL: "chrome://formautofill/content/formfill-anchor.svg",
tooltiptext: GetStringFromName("openAutofillMessagePanel"),
},
mainAction: {
label: GetStringFromName(changeAutofillOptsKey),
accessKey: GetStringFromName("changeAutofillOptionsAccessKey"),
callbackState: "open-pref",
},
options: {
persistWhileVisible: true,
popupIconURL: "chrome://formautofill/content/icon-address-save.svg",
checkbox: {
get checked() {
return Services.prefs.getBoolPref("services.sync.engine.addresses");
},
get label() {
// If sync account is not set, return null label to hide checkbox
return Services.prefs.prefHasUserValue("services.sync.username")
? GetStringFromName("addressesSyncCheckbox")
: null;
},
callback(event) {
let checked = event.target.checked;
Services.prefs.setBoolPref("services.sync.engine.addresses", checked);
lazy.log.debug("Set addresses sync to", checked);
},
},
hideClose: true,
},
},
addAddress: {
notificationId: "autofill-address",
message: formatStringFromName("saveAddressesMessage", [brandShortName]),
descriptionLabel: GetStringFromName("saveAddressDescriptionLabel"),
descriptionIcon: true,
linkMessage: GetStringFromName(autofillOptsKey),
spotlightURL: "about:preferences#privacy-address-autofill",
anchor: {
id: "autofill-address-notification-icon",
URL: "chrome://formautofill/content/formfill-anchor.svg",
tooltiptext: GetStringFromName("openAutofillMessagePanel"),
},
mainAction: {
label: GetStringFromName("saveAddressLabel"),
accessKey: GetStringFromName("saveAddressAccessKey"),
callbackState: "create",
},
secondaryActions: [
{
label: GetStringFromName("cancelAddressLabel"),
accessKey: GetStringFromName("cancelAddressAccessKey"),
callbackState: "cancel",
},
],
options: {
persistWhileVisible: true,
popupIconURL: "chrome://formautofill/content/icon-address-update.svg",
hideClose: true,
},
},
updateAddress: {
notificationId: "autofill-address",
message: GetStringFromName("updateAddressMessage"),
descriptionLabel: GetStringFromName("updateAddressNewDescriptionLabel"),
additionalDescriptionLabel: GetStringFromName(
"updateAddressOldDescriptionLabel"
),
descriptionIcon: false,
linkMessage: GetStringFromName(autofillOptsKey),
spotlightURL: "about:preferences#privacy-address-autofill",
anchor: {
id: "autofill-address-notification-icon",
URL: "chrome://formautofill/content/formfill-anchor.svg",
tooltiptext: GetStringFromName("openAutofillMessagePanel"),
},
mainAction: {
label: GetStringFromName("updateAddressLabel"),
accessKey: GetStringFromName("updateAddressAccessKey"),
callbackState: "update",
},
secondaryActions: [
{
label: GetStringFromName("createAddressLabel"),
accessKey: GetStringFromName("createAddressAccessKey"),
callbackState: "create",
},
],
options: {
persistWhileVisible: true,
popupIconURL: "chrome://formautofill/content/icon-address-update.svg",
hideClose: true,
},
},
addCreditCard: {
notificationId: "autofill-credit-card",
message: formatStringFromName("saveCreditCardMessage", [brandShortName]),
descriptionLabel: GetStringFromName("saveCreditCardDescriptionLabel"),
descriptionIcon: true,
linkMessage: GetStringFromName(autofillOptsKey),
spotlightURL: "about:preferences#privacy-credit-card-autofill",
anchor: {
id: "autofill-credit-card-notification-icon",
URL: "chrome://formautofill/content/formfill-anchor.svg",
tooltiptext: GetStringFromName("openAutofillMessagePanel"),
},
mainAction: {
label: GetStringFromName("saveCreditCardLabel"),
accessKey: GetStringFromName("saveCreditCardAccessKey"),
callbackState: "save",
},
secondaryActions: [
{
label: GetStringFromName("cancelCreditCardLabel"),
accessKey: GetStringFromName("cancelCreditCardAccessKey"),
callbackState: "cancel",
},
{
label: GetStringFromName("neverSaveCreditCardLabel"),
accessKey: GetStringFromName("neverSaveCreditCardAccessKey"),
callbackState: "disable",
},
],
options: {
persistWhileVisible: true,
popupIconURL: "chrome://formautofill/content/icon-credit-card.svg",
hideClose: true,
checkbox: {
get checked() {
return Services.prefs.getBoolPref("services.sync.engine.creditcards");
},
get label() {
// Only set the label when the fallowing conditions existed:
// - sync account is set
// - credit card sync is disabled
// - credit card sync is available
// otherwise return null label to hide checkbox.
return Services.prefs.prefHasUserValue("services.sync.username") &&
!Services.prefs.getBoolPref("services.sync.engine.creditcards") &&
Services.prefs.getBoolPref(
"services.sync.engine.creditcards.available"
)
? GetStringFromName("creditCardsSyncCheckbox")
: null;
},
callback(event) {
let { secondaryButton, menubutton } =
event.target.closest("popupnotification");
let checked = event.target.checked;
Services.prefs.setBoolPref(
"services.sync.engine.creditcards",
checked
);
secondaryButton.disabled = checked;
menubutton.disabled = checked;
lazy.log.debug("Set creditCard sync to", checked);
},
},
},
},
updateCreditCard: {
notificationId: "autofill-credit-card",
message: GetStringFromName("updateCreditCardMessage"),
descriptionLabel: GetStringFromName("updateCreditCardDescriptionLabel"),
descriptionIcon: true,
linkMessage: GetStringFromName(autofillOptsKey),
spotlightURL: "about:preferences#privacy-credit-card-autofill",
anchor: {
id: "autofill-credit-card-notification-icon",
URL: "chrome://formautofill/content/formfill-anchor.svg",
tooltiptext: GetStringFromName("openAutofillMessagePanel"),
},
mainAction: {
label: GetStringFromName("updateCreditCardLabel"),
accessKey: GetStringFromName("updateCreditCardAccessKey"),
callbackState: "update",
},
secondaryActions: [
{
label: GetStringFromName("createCreditCardLabel"),
accessKey: GetStringFromName("createCreditCardAccessKey"),
callbackState: "create",
},
],
options: {
persistWhileVisible: true,
popupIconURL: "chrome://formautofill/content/icon-credit-card.svg",
hideClose: true,
},
},
};
export let FormAutofillPrompter = {
/**
* Generate the main action and secondary actions from content parameters and
* promise resolve.
*
* @private
* @param {object} mainActionParams
* Parameters for main action.
* @param {Array<object>} secondaryActionParams
* Array of the parameters for secondary actions.
* @param {Function} resolve Should be called in action callback.
* @returns {Array<object>}
Return the mainAction and secondary actions in an array for showing doorhanger
*/
_createActions(mainActionParams, secondaryActionParams, resolve) {
if (!mainActionParams) {
return [null, null];
}
let { label, accessKey, callbackState } = mainActionParams;
let callback = resolve.bind(null, callbackState);
let mainAction = { label, accessKey, callback };
if (!secondaryActionParams) {
return [mainAction, null];
}
let secondaryActions = [];
for (let params of secondaryActionParams) {
let cb = resolve.bind(null, params.callbackState);
secondaryActions.push({
label: params.label,
accessKey: params.accessKey,
callback: cb,
});
}
return [mainAction, secondaryActions];
},
_getNotificationElm(browser, id) {
let notificationId = id + "-notification";
let chromeDoc = browser.ownerDocument;
return chromeDoc.getElementById(notificationId);
},
/**
* Append the link label element to the popupnotificationcontent.
*
* @param {XULElement} content
* popupnotificationcontent
* @param {string} message
* The localized string for link title.
* @param {string} link
* Makes it possible to open and highlight a section in preferences
*/
_appendPrivacyPanelLink(content, message, link) {
let chromeDoc = content.ownerDocument;
let privacyLinkElement = chromeDoc.createXULElement("label", {
is: "text-link",
});
privacyLinkElement.setAttribute("useoriginprincipal", true);
privacyLinkElement.setAttribute(
"href",
link || "about:preferences#privacy-form-autofill"
);
privacyLinkElement.setAttribute("value", message);
content.appendChild(privacyLinkElement);
},
/**
* Append the description section to the popupnotificationcontent.
*
* @param {XULElement} content
* popupnotificationcontent
* @param {string} descriptionLabel
* The label showing above description.
* @param {string} descriptionIcon
* The src of description icon.
* @param {string} descriptionId
* The id of description
*/
_appendDescription(
content,
descriptionLabel,
descriptionIcon,
descriptionId
) {
let chromeDoc = content.ownerDocument;
let docFragment = chromeDoc.createDocumentFragment();
let descriptionLabelElement = chromeDoc.createXULElement("label");
descriptionLabelElement.setAttribute("value", descriptionLabel);
docFragment.appendChild(descriptionLabelElement);
let descriptionWrapper = chromeDoc.createXULElement("hbox");
descriptionWrapper.className = "desc-message-box";
if (descriptionIcon) {
let descriptionIconElement = chromeDoc.createXULElement("image");
if (
typeof descriptionIcon == "string" &&
(descriptionIcon.includes("cc-logo") ||
descriptionIcon.includes("icon-credit"))
) {
descriptionIconElement.setAttribute("src", descriptionIcon);
}
descriptionWrapper.appendChild(descriptionIconElement);
}
let descriptionElement = chromeDoc.createXULElement(descriptionId);
descriptionWrapper.appendChild(descriptionElement);
docFragment.appendChild(descriptionWrapper);
content.appendChild(docFragment);
},
_updateDescription(content, descriptionId, description) {
let element = content.querySelector(descriptionId);
element.textContent = description;
},
/**
* Create an image element for notification anchor if it doesn't already exist.
*
* @param {XULElement} browser
* Target browser element for showing doorhanger.
* @param {object} anchor
* Anchor options for setting the anchor element.
* @param {string} anchor.id
* ID of the anchor element.
* @param {string} anchor.URL
* Path of the icon asset.
* @param {string} anchor.tooltiptext
* Tooltip string for the anchor.
*/
_setAnchor(browser, anchor) {
let chromeDoc = browser.ownerDocument;
let { id, URL, tooltiptext } = anchor;
let anchorEt = chromeDoc.getElementById(id);
if (!anchorEt) {
let notificationPopupBox = chromeDoc.getElementById(
"notification-popup-box"
);
// Icon shown on URL bar
let anchorElement = chromeDoc.createXULElement("image");
anchorElement.id = id;
anchorElement.setAttribute("src", URL);
anchorElement.classList.add("notification-anchor-icon");
anchorElement.setAttribute("role", "button");
anchorElement.setAttribute("tooltiptext", tooltiptext);
notificationPopupBox.appendChild(anchorElement);
}
},
_addCheckboxListener(browser, { notificationId, options }) {
if (!options.checkbox) {
return;
}
let { checkbox } = this._getNotificationElm(browser, notificationId);
if (checkbox && !checkbox.hidden) {
checkbox.addEventListener("command", options.checkbox.callback);
}
},
_removeCheckboxListener(browser, { notificationId, options }) {
if (!options.checkbox) {
return;
}
let { checkbox } = this._getNotificationElm(browser, notificationId);
if (checkbox && !checkbox.hidden) {
checkbox.removeEventListener("command", options.checkbox.callback);
}
},
/**
* Show save or update address doorhanger
*
* @param {Element<browser>} browser Browser to show the save/update address prompt
* @param {object} storage Address storage
* @param {object} newRecord Address record to save
* @param {string} flowId Unique GUID to record a series of the same user action
* @param {object} options
* @param {object} [options.mergeableRecord] Record to be merged
* @param {Array} [options.mergeableFields] List of field name that can be merged
*/
async promptToSaveAddress(
browser,
storage,
newRecord,
flowId,
{ mergeableRecord, mergeableFields }
) {
// Overwrite the guid if there is a duplicate
let doorhangerType;
if (mergeableRecord) {
doorhangerType = "updateAddress";
} else if (FormAutofill.isAutofillAddressesCaptureV2Enabled) {
doorhangerType = "addAddress";
} else {
doorhangerType = "addFirstTimeUse";
this._updateStorageAfterInteractWithPrompt("save", storage, newRecord);
// Show first time use doorhanger
if (FormAutofill.isAutofillAddressesFirstTimeUse) {
Services.prefs.setBoolPref(
FormAutofill.ADDRESSES_FIRST_TIME_USE_PREF,
false
);
} else {
return;
}
}
const description = FormAutofillUtils.getAddressLabel(newRecord);
const additionalDescription = mergeableRecord
? FormAutofillUtils.getAddressLabel(mergeableRecord)
: null;
const state = await FormAutofillPrompter._showCCorAddressCaptureDoorhanger(
browser,
doorhangerType,
description,
flowId,
{ additionalDescription }
);
if (state == "cancel") {
return;
} else if (state == "open-pref") {
browser.ownerGlobal.openPreferences("privacy-address-autofill");
return;
}
this._updateStorageAfterInteractWithPrompt(
state,
storage,
newRecord,
mergeableRecord?.guid
);
},
async promptToSaveCreditCard(browser, storage, record, flowId) {
// Overwrite the guid if there is a duplicate
let doorhangerType;
const duplicateRecord = (await storage.getDuplicateRecords(record).next())
.value;
if (duplicateRecord) {
doorhangerType = "updateCreditCard";
} else {
doorhangerType = "addCreditCard";
}
const number = record["cc-number"] || record["cc-number-decrypted"];
const name = record["cc-name"];
const network = lazy.CreditCard.getType(number);
const maskedNumber = lazy.CreditCard.getMaskedNumber(number);
const description = `${maskedNumber}` + (name ? `, ${name}` : ``);
const descriptionIcon = lazy.CreditCard.getCreditCardLogo(network);
const state = await FormAutofillPrompter._showCCorAddressCaptureDoorhanger(
browser,
doorhangerType,
description,
flowId,
{ descriptionIcon }
);
if (state == "cancel") {
return;
} else if (state == "disable") {
Services.prefs.setBoolPref(ENABLED_AUTOFILL_CREDITCARDS_PREF, false);
return;
}
if (!(await FormAutofillUtils.ensureLoggedIn()).authenticated) {
lazy.log.warn("User canceled encryption login");
return;
}
this._updateStorageAfterInteractWithPrompt(
state,
storage,
record,
duplicateRecord?.guid
);
},
async _updateStorageAfterInteractWithPrompt(
state,
storage,
record,
guid = null
) {
let changedGUID = null;
if (state == "create" || state == "save") {
changedGUID = await storage.add(record);
} else if (state == "update") {
await storage.update(guid, record, true);
changedGUID = guid;
}
storage.notifyUsed(changedGUID);
},
_getUpdatedCCIcon(network) {
return FormAutofillUtils.getCreditCardLogo(network);
},
/**
* Show different types of doorhanger by leveraging PopupNotifications.
*
* @param {XULElement} browser Target browser element for showing doorhanger.
* @param {string} type The type of the doorhanger. There will have first time use/update/credit card.
* @param {string} description The message that provides more information on doorhanger.
* @param {string} flowId guid used to correlate events relating to the same form
* @param {object} [options = {}] a list of options for this method
* @param {string} options.descriptionIcon The icon for descriotion
* @param {string} options.additionalDescription The message that provides more information on doorhanger.
* @returns {Promise} Resolved with action type when action callback is triggered.
*/
async _showCCorAddressCaptureDoorhanger(
browser,
type,
description,
flowId,
{ descriptionIcon = null, additionalDescription = null }
) {
const telemetryType = type.endsWith("CreditCard")
? AutofillTelemetry.CREDIT_CARD
: AutofillTelemetry.ADDRESS;
const isCapture = type.startsWith("add");
AutofillTelemetry.recordDoorhangerShown(telemetryType, flowId, isCapture);
lazy.log.debug("show doorhanger with type:", type);
return new Promise(resolve => {
let {
notificationId,
message,
descriptionLabel,
additionalDescriptionLabel,
linkMessage,
spotlightURL,
anchor,
mainAction,
secondaryActions,
options,
} = CONTENT[type];
descriptionIcon = descriptionIcon ?? CONTENT[type].descriptionIcon;
const { ownerGlobal: chromeWin, ownerDocument: chromeDoc } = browser;
options.eventCallback = topic => {
lazy.log.debug("eventCallback:", topic);
if (topic == "removed" || topic == "dismissed") {
this._removeCheckboxListener(browser, { notificationId, options });
return;
}
// The doorhanger is customizable only when notification box is shown
if (topic != "shown") {
return;
}
this._addCheckboxListener(browser, { notificationId, options });
// There's no preferences link or other customization in first time use doorhanger.
if (type == "addFirstTimeUse") {
return;
}
const DESCRIPTION_ID = "description";
const ADDITIONAL_DESCRIPTION_ID = "additional-description";
const NOTIFICATION_ID = notificationId + "-notification";
const notification = chromeDoc.getElementById(NOTIFICATION_ID);
const notificationContent =
notification.querySelector("popupnotificationcontent") ||
chromeDoc.createXULElement("popupnotificationcontent");
if (!notification.contains(notificationContent)) {
notificationContent.setAttribute("orient", "vertical");
this._appendDescription(
notificationContent,
descriptionLabel,
descriptionIcon,
DESCRIPTION_ID
);
if (additionalDescription) {
this._appendDescription(
notificationContent,
additionalDescriptionLabel,
descriptionIcon,
ADDITIONAL_DESCRIPTION_ID
);
}
this._appendPrivacyPanelLink(
notificationContent,
linkMessage,
spotlightURL
);
notification.appendNotificationContent(notificationContent);
}
this._updateDescription(
notificationContent,
DESCRIPTION_ID,
description
);
if (additionalDescription) {
this._updateDescription(
notificationContent,
ADDITIONAL_DESCRIPTION_ID,
additionalDescription
);
}
};
this._setAnchor(browser, anchor);
chromeWin.PopupNotifications.show(
browser,
notificationId,
message,
anchor.id,
...this._createActions(mainAction, secondaryActions, resolve),
options
);
}).then(state => {
AutofillTelemetry.recordDoorhangerClicked(
telemetryType,
state,
flowId,
isCapture
);
return state;
});
},
};
|