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 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
6 import { Region } from "resource://gre/modules/Region.sys.mjs";
7 import { AddressMetaDataLoader } from "resource://gre/modules/shared/AddressMetaDataLoader.sys.mjs";
9 const AUTOFILL_ADDRESSES_AVAILABLE_PREF =
10 "extensions.formautofill.addresses.supported";
11 // This pref should be refactored after the migration of the old bool pref
12 const AUTOFILL_CREDITCARDS_AVAILABLE_PREF =
13 "extensions.formautofill.creditCards.supported";
14 const BROWSER_SEARCH_REGION_PREF = "browser.search.region";
15 const CREDITCARDS_AUTOFILL_SUPPORTED_COUNTRIES_PREF =
16 "extensions.formautofill.creditCards.supportedCountries";
17 const ENABLED_AUTOFILL_ADDRESSES_PREF =
18 "extensions.formautofill.addresses.enabled";
19 const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF =
20 "extensions.formautofill.addresses.capture.enabled";
21 const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_REQUIRED_FIELDS_PREF =
22 "extensions.formautofill.addresses.capture.requiredFields";
23 const ENABLED_AUTOFILL_ADDRESSES_SUPPORTED_COUNTRIES_PREF =
24 "extensions.formautofill.addresses.supportedCountries";
25 const ENABLED_AUTOFILL_CREDITCARDS_PREF =
26 "extensions.formautofill.creditCards.enabled";
27 const AUTOFILL_CREDITCARDS_REAUTH_PREF =
28 "extensions.formautofill.creditCards.reauth.optout";
29 const AUTOFILL_CREDITCARDS_HIDE_UI_PREF =
30 "extensions.formautofill.creditCards.hideui";
31 const FORM_AUTOFILL_SUPPORT_RTL_PREF = "extensions.formautofill.supportRTL";
32 const AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF =
33 "extensions.formautofill.creditCards.ignoreAutocompleteOff";
34 const AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF =
35 "extensions.formautofill.addresses.ignoreAutocompleteOff";
36 const ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF =
37 "extensions.formautofill.heuristics.captureOnFormRemoval";
38 const ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF =
39 "extensions.formautofill.heuristics.captureOnPageNavigation";
40 const ENABLED_AUTOFILL_SAME_ORIGIN_WITH_TOP =
41 "extensions.formautofill.heuristics.autofillSameOriginWithTop";
43 export const FormAutofill = {
44 ENABLED_AUTOFILL_ADDRESSES_PREF,
45 ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF,
46 ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF,
47 ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF,
48 ENABLED_AUTOFILL_SAME_ORIGIN_WITH_TOP,
49 ENABLED_AUTOFILL_CREDITCARDS_PREF,
50 AUTOFILL_CREDITCARDS_REAUTH_PREF,
51 AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
52 AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,
56 get DEFAULT_REGION() {
57 return this._region || Region.home || "US";
60 set DEFAULT_REGION(region) {
61 this._region = region;
65 * Determines if an autofill feature should be enabled based on the "available"
66 * and "supportedCountries" parameters.
68 * @param {string} available Available can be one of the following: "on", "detect", "off".
69 * "on" forces the particular Form Autofill feature on, while "detect" utilizes the supported countries
70 * to see if the feature should be available.
71 * @param {string[]} supportedCountries
72 * @returns {boolean} `true` if autofill feature is supported in the current browser search region
74 _isSupportedRegion(available, supportedCountries) {
75 if (available == "on") {
77 } else if (available == "detect") {
78 if (!FormAutofill.supportRTL && Services.locale.isAppLocaleRTL) {
82 return supportedCountries.includes(FormAutofill.browserSearchRegion);
88 * Return true if address autofill is available for a specific country.
90 isAutofillAddressesAvailableInCountry(country) {
91 if (FormAutofill._isAutofillAddressesAvailableInExperiment) {
95 let available = FormAutofill._isAutofillAddressesAvailable;
96 if (country && available == "detect") {
97 return FormAutofill._addressAutofillSupportedCountries.includes(
101 return available == "on";
103 get isAutofillEnabled() {
104 return this.isAutofillAddressesEnabled || this.isAutofillCreditCardsEnabled;
107 * Determines if the credit card autofill feature is available to use in the browser.
108 * If the feature is not available, then there are no user facing ways to enable it.
110 * @returns {boolean} `true` if credit card autofill is available
112 get isAutofillCreditCardsAvailable() {
113 return this._isSupportedRegion(
114 FormAutofill._isAutofillCreditCardsAvailable,
115 FormAutofill._creditCardAutofillSupportedCountries
119 * Determines if the address autofill feature is available to use in the browser.
120 * If the feature is not available, then there are no user facing ways to enable it.
121 * Two conditions must be met for the autofill feature to be considered available:
122 * 1. Address autofill support is confirmed when:
123 * - `extensions.formautofill.addresses.supported` is set to `on`.
124 * - The user is located in a region supported by the feature
125 * (`extensions.formautofill.creditCards.supportedCountries`).
126 * 2. Address autofill is enabled through a Nimbus experiment:
127 * - The experiment pref `extensions.formautofill.addresses.experiments.enabled` is set to true.
129 * @returns {boolean} `true` if address autofill is available
131 get isAutofillAddressesAvailable() {
132 const isUserInSupportedRegion = this._isSupportedRegion(
133 FormAutofill._isAutofillAddressesAvailable,
134 FormAutofill._addressAutofillSupportedCountries
137 isUserInSupportedRegion ||
138 FormAutofill._isAutofillAddressesAvailableInExperiment
142 * Determines if the user has enabled or disabled credit card autofill.
144 * @returns {boolean} `true` if credit card autofill is enabled
146 get isAutofillCreditCardsEnabled() {
148 this.isAutofillCreditCardsAvailable &&
149 FormAutofill._isAutofillCreditCardsEnabled
153 * Determines if credit card autofill is locked by policy.
155 * @returns {boolean} `true` if credit card autofill is locked
157 get isAutofillCreditCardsLocked() {
158 return Services.prefs.prefIsLocked(ENABLED_AUTOFILL_CREDITCARDS_PREF);
161 * Determines if the user has enabled or disabled address autofill.
163 * @returns {boolean} `true` if address autofill is enabled
165 get isAutofillAddressesEnabled() {
167 this.isAutofillAddressesAvailable &&
168 FormAutofill._isAutofillAddressesEnabled
172 * Determines if address autofill is locked by policy.
174 * @returns {boolean} `true` if address autofill is locked
176 get isAutofillAddressesLocked() {
177 return Services.prefs.prefIsLocked(ENABLED_AUTOFILL_ADDRESSES_PREF);
180 defineLogGetter(scope, logPrefix) {
181 // A logging helper for debug logging to avoid creating Console objects
182 // or triggering expensive JS -> C++ calls when debug logging is not
185 // Console objects, even natively-implemented ones, can consume a lot of
186 // memory, and since this code may run in every content process, that
187 // memory can add up quickly. And, even when debug-level messages are
188 // being ignored, console.debug() calls can be expensive.
190 // This helper avoids both of those problems by never touching the
191 // console object unless debug logging is enabled.
192 scope.debug = function debug() {
193 if (FormAutofill.logLevel.toLowerCase() == "debug") {
194 this.log.debug(...arguments);
198 let { ConsoleAPI } = ChromeUtils.importESModule(
199 "resource://gre/modules/Console.sys.mjs"
201 return new ConsoleAPI({
202 maxLogLevelPref: "extensions.formautofill.loglevel",
207 get isMLExperimentEnabled() {
208 return FormAutofill._isMLEnabled && FormAutofill._isMLExperimentEnabled;
212 // TODO: Bug 1747284. Use Region.home instead of reading "browser.serach.region"
213 // by default. However, Region.home doesn't observe preference change at this point,
214 // we should also fix that issue.
215 XPCOMUtils.defineLazyPreferenceGetter(
217 "browserSearchRegion",
218 BROWSER_SEARCH_REGION_PREF,
219 FormAutofill.DEFAULT_REGION
221 XPCOMUtils.defineLazyPreferenceGetter(
224 "extensions.formautofill.loglevel",
228 XPCOMUtils.defineLazyPreferenceGetter(
230 "_isAutofillAddressesAvailable",
231 AUTOFILL_ADDRESSES_AVAILABLE_PREF
233 XPCOMUtils.defineLazyPreferenceGetter(
235 "_isAutofillAddressesEnabled",
236 ENABLED_AUTOFILL_ADDRESSES_PREF
238 XPCOMUtils.defineLazyPreferenceGetter(
240 "isAutofillAddressesCaptureEnabled",
241 ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF
243 XPCOMUtils.defineLazyPreferenceGetter(
245 "_isAutofillCreditCardsAvailable",
246 AUTOFILL_CREDITCARDS_AVAILABLE_PREF
248 XPCOMUtils.defineLazyPreferenceGetter(
250 "_isAutofillCreditCardsEnabled",
251 ENABLED_AUTOFILL_CREDITCARDS_PREF
253 XPCOMUtils.defineLazyPreferenceGetter(
255 "isAutofillCreditCardsHideUI",
256 AUTOFILL_CREDITCARDS_HIDE_UI_PREF
258 XPCOMUtils.defineLazyPreferenceGetter(
260 "_addressAutofillSupportedCountries",
261 ENABLED_AUTOFILL_ADDRESSES_SUPPORTED_COUNTRIES_PREF,
263 val => val.split(",")
265 XPCOMUtils.defineLazyPreferenceGetter(
267 "_creditCardAutofillSupportedCountries",
268 CREDITCARDS_AUTOFILL_SUPPORTED_COUNTRIES_PREF,
271 val => val.split(",")
273 XPCOMUtils.defineLazyPreferenceGetter(
276 FORM_AUTOFILL_SUPPORT_RTL_PREF
278 XPCOMUtils.defineLazyPreferenceGetter(
280 "creditCardsAutocompleteOff",
281 AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF
283 XPCOMUtils.defineLazyPreferenceGetter(
285 "addressesAutocompleteOff",
286 AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF
288 XPCOMUtils.defineLazyPreferenceGetter(
290 "captureOnFormRemoval",
291 ENABLED_AUTOFILL_CAPTURE_ON_FORM_REMOVAL_PREF
293 XPCOMUtils.defineLazyPreferenceGetter(
295 "captureOnPageNavigation",
296 ENABLED_AUTOFILL_CAPTURE_ON_PAGE_NAVIGATION_PREF
298 XPCOMUtils.defineLazyPreferenceGetter(
300 "addressCaptureRequiredFields",
301 ENABLED_AUTOFILL_ADDRESSES_CAPTURE_REQUIRED_FIELDS_PREF,
304 val => val?.split(",").filter(v => !!v)
306 XPCOMUtils.defineLazyPreferenceGetter(
308 "autofillSameOriginWithTop",
309 ENABLED_AUTOFILL_SAME_ORIGIN_WITH_TOP
312 XPCOMUtils.defineLazyPreferenceGetter(
314 "_isAutofillAddressesAvailableInExperiment",
315 "extensions.formautofill.addresses.experiments.enabled"
318 XPCOMUtils.defineLazyPreferenceGetter(
325 XPCOMUtils.defineLazyPreferenceGetter(
327 "_isMLExperimentEnabled",
328 "extensions.formautofill.ml.experiment.enabled",
332 XPCOMUtils.defineLazyPreferenceGetter(
335 "extensions.formautofill.ml.experiment.modelRevision",
339 ChromeUtils.defineLazyGetter(FormAutofill, "countries", () =>
340 AddressMetaDataLoader.getCountries()