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
|
/* 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 { DataSourceBase } from "resource://gre/modules/megalist/aggregator/datasources/DataSourceBase.sys.mjs";
import { CreditCardRecord } from "resource://gre/modules/shared/CreditCardRecord.sys.mjs";
import { formAutofillStorage } from "resource://autofill/FormAutofillStorage.sys.mjs";
import { OSKeyStore } from "resource://gre/modules/OSKeyStore.sys.mjs";
async function decryptCard(card) {
if (card["cc-number-encrypted"] && !card["cc-number-decrypted"]) {
try {
card["cc-number-decrypted"] = await OSKeyStore.decrypt(
card["cc-number-encrypted"],
false
);
card["cc-number"] = card["cc-number-decrypted"];
} catch (e) {
console.error(e);
}
}
}
async function updateCard(card, field, value) {
try {
await decryptCard(card);
const newCard = {
...card,
[field]: value ?? "",
};
formAutofillStorage.INTERNAL_FIELDS.forEach(name => delete newCard[name]);
formAutofillStorage.creditCards.VALID_COMPUTED_FIELDS.forEach(
name => delete newCard[name]
);
delete newCard["cc-number-decrypted"];
CreditCardRecord.normalizeFields(newCard);
if (card.guid) {
await formAutofillStorage.creditCards.update(card.guid, newCard);
} else {
await formAutofillStorage.creditCards.add(newCard);
}
} catch (error) {
//todo
console.error("failed to modify credit card", error);
return false;
}
return true;
}
/**
* Data source for Bank Cards.
*
* Each card is represented by 3 lines: card number, expiration date and holder name.
*
* Protypes are used to reduce memory need because for different records
* similar lines will differ in values only.
*/
export class BankCardDataSource extends DataSourceBase {
#cardNumberPrototype;
#expirationPrototype;
#holderNamePrototype;
#cardsDisabledMessage;
#enabled;
#header;
constructor(...args) {
super(...args);
// Wait for Fluent to provide strings before loading data
this.localizeStrings({
headerLabel: "payments-section-label",
numberLabel: "card-number-label",
expirationLabel: "card-expiration-label",
holderLabel: "card-holder-label",
cardsDisabled: "payments-disabled",
}).then(strings => {
const copyCommand = { id: "Copy", label: "command-copy" };
const editCommand = { id: "Edit", label: "command-edit", verify: true };
const deleteCommand = {
id: "Delete",
label: "command-delete",
verify: true,
};
this.#cardsDisabledMessage = strings.cardsDisabled;
this.#header = this.createHeaderLine(strings.headerLabel);
this.#header.commands.push({
id: "Create",
label: "payments-command-create",
});
this.#cardNumberPrototype = this.prototypeDataLine({
label: { value: strings.numberLabel },
concealed: { value: true, writable: true },
start: { value: true },
value: {
async get() {
if (this.isEditing()) {
return this.editingValue;
}
if (this.concealed) {
return (
"••••••••" +
this.record["cc-number"].replaceAll("*", "").substr(-4)
);
}
await decryptCard(this.record);
return this.record["cc-number-decrypted"];
},
},
valueIcon: {
get() {
const typeToImage = {
amex: "third-party/cc-logo-amex.png",
cartebancaire: "third-party/cc-logo-cartebancaire.png",
diners: "third-party/cc-logo-diners.svg",
discover: "third-party/cc-logo-discover.png",
jcb: "third-party/cc-logo-jcb.svg",
mastercard: "third-party/cc-logo-mastercard.svg",
mir: "third-party/cc-logo-mir.svg",
unionpay: "third-party/cc-logo-unionpay.svg",
visa: "third-party/cc-logo-visa.svg",
};
return (
"chrome://formautofill/content/" +
(typeToImage[this.record["cc-type"]] ??
"icon-credit-card-generic.svg")
);
},
},
commands: {
*value() {
if (this.concealed) {
yield { id: "Reveal", label: "command-reveal", verify: true };
} else {
yield { id: "Conceal", label: "command-conceal" };
}
yield { ...copyCommand, verify: true };
yield editCommand;
yield "-";
yield deleteCommand;
},
},
executeReveal: {
value() {
this.concealed = false;
this.refreshOnScreen();
},
},
executeConceal: {
value() {
this.concealed = true;
this.refreshOnScreen();
},
},
executeCopy: {
async value() {
await decryptCard(this.record);
this.copyToClipboard(this.record["cc-number-decrypted"]);
},
},
executeEdit: {
async value() {
await decryptCard(this.record);
this.editingValue = this.record["cc-number-decrypted"] ?? "";
this.refreshOnScreen();
},
},
executeSave: {
async value(value) {
if (updateCard(this.record, "cc-number", value)) {
this.executeCancel();
}
},
},
});
this.#expirationPrototype = this.prototypeDataLine({
label: { value: strings.expirationLabel },
value: {
get() {
return `${this.record["cc-exp-month"]}/${this.record["cc-exp-year"]}`;
},
},
commands: {
value: [copyCommand, editCommand, "-", deleteCommand],
},
});
this.#holderNamePrototype = this.prototypeDataLine({
label: { value: strings.holderLabel },
end: { value: true },
value: {
get() {
return this.editingValue ?? this.record["cc-name"];
},
},
commands: {
value: [copyCommand, editCommand, "-", deleteCommand],
},
executeEdit: {
value() {
this.editingValue = this.record["cc-name"] ?? "";
this.refreshOnScreen();
},
},
executeSave: {
async value(value) {
if (updateCard(this.record, "cc-name", value)) {
this.executeCancel();
}
},
},
});
Services.obs.addObserver(this, "formautofill-storage-changed");
Services.prefs.addObserver(
"extensions.formautofill.creditCards.enabled",
this
);
this.#reloadDataSource();
});
}
/**
* Enumerate all the lines provided by this data source.
*
* @param {string} searchText used to filter data
*/
*enumerateLines(searchText) {
if (this.#enabled === undefined) {
// Async Fluent API makes it possible to have data source waiting
// for the localized strings, which can be detected by undefined in #enabled.
return;
}
yield this.#header;
if (this.#header.collapsed || !this.#enabled) {
return;
}
const stats = { count: 0, total: 0 };
searchText = searchText.toUpperCase();
yield* this.enumerateLinesForMatchingRecords(
searchText,
stats,
card =>
(card["cc-number-decrypted"] || card["cc-number"])
.toUpperCase()
.includes(searchText) ||
`${card["cc-exp-month"]}/${card["cc-exp-year"]}`
.toUpperCase()
.includes(searchText) ||
card["cc-name"]?.toUpperCase().includes(searchText)
);
this.formatMessages({
id:
stats.count == stats.total
? "payments-count"
: "payments-filtered-count",
args: stats,
}).then(([headerLabel]) => {
this.#header.value = headerLabel;
});
}
/**
* Sync lines array with the actual data source.
* This function reads all cards from the storage, adds or updates lines and
* removes lines for the removed cards.
*/
async #reloadDataSource() {
this.#enabled = Services.prefs.getBoolPref(
"extensions.formautofill.creditCards.enabled"
);
if (!this.#enabled) {
this.#reloadEmptyDataSource();
return;
}
await formAutofillStorage.initialize();
const cards = await formAutofillStorage.creditCards.getAll();
this.beforeReloadingDataSource();
cards.forEach(card => {
const lineId = `${card["cc-name"]}:${card.guid}`;
this.addOrUpdateLine(card, lineId + "0", this.#cardNumberPrototype);
this.addOrUpdateLine(card, lineId + "1", this.#expirationPrototype);
this.addOrUpdateLine(card, lineId + "2", this.#holderNamePrototype);
});
this.afterReloadingDataSource();
}
#reloadEmptyDataSource() {
this.lines.length = 0;
//todo: user can enable credit cards by activating header line
this.#header.value = this.#cardsDisabledMessage;
this.refreshAllLinesOnScreen();
}
observe(_subj, topic, message) {
if (
topic == "formautofill-storage-changed" ||
message == "extensions.formautofill.creditCards.enabled"
) {
this.#reloadDataSource();
}
}
}
|