2018-07-08 03:49:34 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["FormAutofill"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2018-07-08 03:49:34 +03:00
|
|
|
|
2020-05-27 23:16:48 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
Region: "resource://gre/modules/Region.jsm",
|
|
|
|
});
|
|
|
|
|
2018-07-08 03:49:34 +03:00
|
|
|
const ADDRESSES_FIRST_TIME_USE_PREF = "extensions.formautofill.firstTimeUse";
|
2022-02-08 22:31:58 +03:00
|
|
|
const AUTOFILL_ADDRESSES_AVAILABLE_PREF =
|
|
|
|
"extensions.formautofill.addresses.supported";
|
|
|
|
// This pref should be refactored after the migration of the old bool pref
|
2018-07-08 03:49:34 +03:00
|
|
|
const AUTOFILL_CREDITCARDS_AVAILABLE_PREF =
|
2022-02-08 22:31:58 +03:00
|
|
|
"extensions.formautofill.creditCards.supported";
|
|
|
|
const BROWSER_SEARCH_REGION_PREF = "browser.search.region";
|
2018-07-08 03:49:34 +03:00
|
|
|
const CREDITCARDS_USED_STATUS_PREF = "extensions.formautofill.creditCards.used";
|
2022-02-08 22:31:58 +03:00
|
|
|
const CREDITCARDS_AUTOFILL_SUPPORTED_COUNTRIES_PREF =
|
|
|
|
"extensions.formautofill.creditCards.supportedCountries";
|
2018-07-08 03:49:34 +03:00
|
|
|
const ENABLED_AUTOFILL_ADDRESSES_PREF =
|
|
|
|
"extensions.formautofill.addresses.enabled";
|
2020-07-21 08:58:30 +03:00
|
|
|
const ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF =
|
|
|
|
"extensions.formautofill.addresses.capture.enabled";
|
2022-02-08 22:31:58 +03:00
|
|
|
const ENABLED_AUTOFILL_ADDRESSES_SUPPORTED_COUNTRIES_PREF =
|
|
|
|
"extensions.formautofill.addresses.supportedCountries";
|
2018-07-08 03:49:34 +03:00
|
|
|
const ENABLED_AUTOFILL_CREDITCARDS_PREF =
|
|
|
|
"extensions.formautofill.creditCards.enabled";
|
2020-06-25 23:35:56 +03:00
|
|
|
const ENABLED_AUTOFILL_CREDITCARDS_REAUTH_PREF =
|
|
|
|
"extensions.formautofill.reauth.enabled";
|
2020-08-08 22:25:31 +03:00
|
|
|
const AUTOFILL_CREDITCARDS_HIDE_UI_PREF =
|
|
|
|
"extensions.formautofill.creditCards.hideui";
|
2022-02-08 22:31:58 +03:00
|
|
|
const FORM_AUTOFILL_SUPPORT_RTL_PREF = "extensions.formautofill.supportRTL";
|
2018-07-08 03:49:34 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
this,
|
|
|
|
"logLevel",
|
|
|
|
"extensions.formautofill.loglevel",
|
|
|
|
"Warn"
|
|
|
|
);
|
|
|
|
|
|
|
|
// A logging helper for debug logging to avoid creating Console objects
|
|
|
|
// or triggering expensive JS -> C++ calls when debug logging is not
|
|
|
|
// enabled.
|
|
|
|
//
|
|
|
|
// Console objects, even natively-implemented ones, can consume a lot of
|
|
|
|
// memory, and since this code may run in every content process, that
|
|
|
|
// memory can add up quickly. And, even when debug-level messages are
|
|
|
|
// being ignored, console.debug() calls can be expensive.
|
|
|
|
//
|
|
|
|
// This helper avoids both of those problems by never touching the
|
|
|
|
// console object unless debug logging is enabled.
|
|
|
|
function debug() {
|
2018-09-05 20:54:48 +03:00
|
|
|
if (logLevel.toLowerCase() == "debug") {
|
2018-07-08 03:49:34 +03:00
|
|
|
this.log.debug(...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var FormAutofill = {
|
|
|
|
ENABLED_AUTOFILL_ADDRESSES_PREF,
|
2020-07-21 08:58:30 +03:00
|
|
|
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF,
|
2018-07-08 03:49:34 +03:00
|
|
|
ENABLED_AUTOFILL_CREDITCARDS_PREF,
|
2020-06-25 23:35:56 +03:00
|
|
|
ENABLED_AUTOFILL_CREDITCARDS_REAUTH_PREF,
|
2018-07-08 03:49:34 +03:00
|
|
|
ADDRESSES_FIRST_TIME_USE_PREF,
|
|
|
|
CREDITCARDS_USED_STATUS_PREF,
|
|
|
|
|
2020-05-27 23:16:48 +03:00
|
|
|
get DEFAULT_REGION() {
|
|
|
|
return Region.home || "US";
|
|
|
|
},
|
2022-02-08 22:31:58 +03:00
|
|
|
/**
|
|
|
|
* Determines if an autofill feature should be enabled based on the "available"
|
|
|
|
* and "supportedCountries" parameters.
|
|
|
|
*
|
|
|
|
* @param {string} available Available can be one of the following: "on", "detect", "off".
|
|
|
|
* "on" forces the particular Form Autofill feature on, while "detect" utilizes the supported countries
|
|
|
|
* to see if the feature should be available.
|
|
|
|
* @param {string[]} supportedCountries
|
|
|
|
* @returns {boolean} `true` if autofill feature is supported in the current browser search region
|
|
|
|
*/
|
|
|
|
_isSupportedRegion(available, supportedCountries) {
|
|
|
|
if (available == "on") {
|
|
|
|
return true;
|
|
|
|
} else if (available == "detect") {
|
|
|
|
if (!FormAutofill.supportRTL && Services.locale.isAppLocaleRTL) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-23 00:21:06 +03:00
|
|
|
// TODO: Bug 1747284. Use Region.home instead of reading "browser.serach.region"
|
|
|
|
// by default. However, Region.home doesn't observe preference change at this point,
|
|
|
|
// we should also fix that issue.
|
|
|
|
let region = Services.prefs.getCharPref(
|
|
|
|
BROWSER_SEARCH_REGION_PREF,
|
|
|
|
this.DEFAULT_REGION
|
|
|
|
);
|
2022-02-08 22:31:58 +03:00
|
|
|
return supportedCountries.includes(region);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
isAutofillAddressesAvailableInCountry(country) {
|
|
|
|
return FormAutofill._addressAutofillSupportedCountries.includes(country);
|
|
|
|
},
|
2018-07-08 03:49:34 +03:00
|
|
|
get isAutofillEnabled() {
|
2022-02-08 22:31:58 +03:00
|
|
|
return this.isAutofillAddressesEnabled || this.isAutofillCreditCardsEnabled;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Determines if the credit card autofill feature is available to use in the browser.
|
|
|
|
* If the feature is not available, then there are no user facing ways to enable it.
|
|
|
|
* @returns {boolean} `true` if credit card autofill is available
|
|
|
|
*/
|
|
|
|
get isAutofillCreditCardsAvailable() {
|
|
|
|
return this._isSupportedRegion(
|
|
|
|
FormAutofill._isAutofillCreditCardsAvailable,
|
|
|
|
FormAutofill._creditCardAutofillSupportedCountries
|
2022-01-26 23:11:41 +03:00
|
|
|
);
|
|
|
|
},
|
2022-02-08 22:31:58 +03:00
|
|
|
/**
|
|
|
|
* Determines if the address autofill feature is available to use in the browser.
|
|
|
|
* If the feature is not available, then there are no user facing ways to enable it.
|
|
|
|
* @returns {boolean} `true` if address autofill is available
|
|
|
|
*/
|
|
|
|
get isAutofillAddressesAvailable() {
|
|
|
|
return this._isSupportedRegion(
|
|
|
|
FormAutofill._isAutofillAddressesAvailable,
|
|
|
|
FormAutofill._addressAutofillSupportedCountries
|
|
|
|
);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Determines if the user has enabled or disabled credit card autofill.
|
|
|
|
* @returns {boolean} `true` if credit card autofill is enabled
|
|
|
|
*/
|
2018-07-08 03:49:34 +03:00
|
|
|
get isAutofillCreditCardsEnabled() {
|
|
|
|
return (
|
2022-02-08 22:31:58 +03:00
|
|
|
this.isAutofillCreditCardsAvailable &&
|
2018-07-08 03:49:34 +03:00
|
|
|
FormAutofill._isAutofillCreditCardsEnabled
|
|
|
|
);
|
|
|
|
},
|
2022-02-08 22:31:58 +03:00
|
|
|
/**
|
|
|
|
* Determines if the user has enabled or disabled address autofill.
|
|
|
|
* @returns {boolean} `true` if address autofill is enabled
|
|
|
|
*/
|
|
|
|
get isAutofillAddressesEnabled() {
|
|
|
|
return (
|
|
|
|
this.isAutofillAddressesAvailable &&
|
|
|
|
FormAutofill._isAutofillAddressesEnabled
|
|
|
|
);
|
|
|
|
},
|
2018-07-08 03:49:34 +03:00
|
|
|
|
|
|
|
defineLazyLogGetter(scope, logPrefix) {
|
|
|
|
scope.debug = debug;
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(scope, "log", () => {
|
2022-03-11 19:41:29 +03:00
|
|
|
let { ConsoleAPI } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/Console.jsm"
|
|
|
|
);
|
2018-07-08 03:49:34 +03:00
|
|
|
return new ConsoleAPI({
|
|
|
|
maxLogLevelPref: "extensions.formautofill.loglevel",
|
|
|
|
prefix: logPrefix,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
2022-02-08 22:31:58 +03:00
|
|
|
"_isAutofillAddressesAvailable",
|
|
|
|
AUTOFILL_ADDRESSES_AVAILABLE_PREF
|
|
|
|
);
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"_isAutofillAddressesEnabled",
|
2018-07-08 03:49:34 +03:00
|
|
|
ENABLED_AUTOFILL_ADDRESSES_PREF
|
|
|
|
);
|
2020-07-21 08:58:30 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"isAutofillAddressesCaptureEnabled",
|
|
|
|
ENABLED_AUTOFILL_ADDRESSES_CAPTURE_PREF
|
|
|
|
);
|
2018-07-08 03:49:34 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
2022-02-08 22:31:58 +03:00
|
|
|
"_isAutofillCreditCardsAvailable",
|
2018-07-08 03:49:34 +03:00
|
|
|
AUTOFILL_CREDITCARDS_AVAILABLE_PREF
|
|
|
|
);
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
2022-02-08 22:31:58 +03:00
|
|
|
"_isAutofillCreditCardsEnabled",
|
2018-07-08 03:49:34 +03:00
|
|
|
ENABLED_AUTOFILL_CREDITCARDS_PREF
|
|
|
|
);
|
2020-08-08 22:25:31 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"isAutofillCreditCardsHideUI",
|
|
|
|
AUTOFILL_CREDITCARDS_HIDE_UI_PREF
|
|
|
|
);
|
2018-07-08 03:49:34 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"isAutofillAddressesFirstTimeUse",
|
|
|
|
ADDRESSES_FIRST_TIME_USE_PREF
|
|
|
|
);
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"AutofillCreditCardsUsedStatus",
|
|
|
|
CREDITCARDS_USED_STATUS_PREF
|
|
|
|
);
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
2022-02-08 22:31:58 +03:00
|
|
|
"_addressAutofillSupportedCountries",
|
|
|
|
ENABLED_AUTOFILL_ADDRESSES_SUPPORTED_COUNTRIES_PREF,
|
|
|
|
null,
|
|
|
|
val => val.split(",")
|
|
|
|
);
|
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"_creditCardAutofillSupportedCountries",
|
|
|
|
CREDITCARDS_AUTOFILL_SUPPORTED_COUNTRIES_PREF,
|
2018-07-08 03:49:34 +03:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
val => val.split(",")
|
|
|
|
);
|
2022-02-08 22:31:58 +03:00
|
|
|
XPCOMUtils.defineLazyPreferenceGetter(
|
|
|
|
FormAutofill,
|
|
|
|
"supportRTL",
|
|
|
|
FORM_AUTOFILL_SUPPORT_RTL_PREF
|
|
|
|
);
|
2018-09-24 09:50:03 +03:00
|
|
|
|
2019-01-11 03:25:52 +03:00
|
|
|
// XXX: This should be invalidated on intl:app-locales-changed.
|
2018-09-24 09:50:03 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(FormAutofill, "countries", () => {
|
2019-01-11 03:25:52 +03:00
|
|
|
let availableRegionCodes = Services.intl.getAvailableLocaleDisplayNames(
|
|
|
|
"region"
|
|
|
|
);
|
|
|
|
let displayNames = Services.intl.getRegionDisplayNames(
|
|
|
|
undefined,
|
|
|
|
availableRegionCodes
|
|
|
|
);
|
|
|
|
let result = new Map();
|
|
|
|
for (let i = 0; i < availableRegionCodes.length; i++) {
|
|
|
|
result.set(availableRegionCodes[i].toUpperCase(), displayNames[i]);
|
|
|
|
}
|
|
|
|
return result;
|
2018-09-24 09:50:03 +03:00
|
|
|
});
|