Bug 1685801: Part 1 - Move objectToPropBag and propBagToObject out of BrowserUtils. r=mccr8

Note that PromptUtils.objectToPropBag and the new propBagToObject function in
prompt_common.js do not do the recursive conversion that the BrowserUtils
versions attempted, but that code was completely broken, and would have failed
if it encountered a value which triggered recursion.

Differential Revision: https://phabricator.services.mozilla.com/D101481
This commit is contained in:
Kris Maglione 2021-01-28 20:58:29 +00:00
Родитель 2e368fc8fb
Коммит 63ac85dd8e
3 изменённых файлов: 16 добавлений и 69 удалений

Просмотреть файл

@ -8,9 +8,6 @@ const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { objectToPropBag } = ChromeUtils.import(
"resource://gre/modules/BrowserUtils.jsm"
).BrowserUtils;
// This is redefined below, for strange and unfortunate reasons.
var { PromptUtils } = ChromeUtils.import(
"resource://gre/modules/SharedPromptUtils.jsm"
@ -1204,7 +1201,7 @@ class ModalPrompter {
if (!(taskResult instanceof Object)) {
throw new Error("task must return object");
}
return objectToPropBag(taskResult);
return PromptUtils.objectToPropBag(taskResult);
}
/*

Просмотреть файл

@ -1,8 +1,20 @@
const { Cc, Ci, Cu: ChromeUtils } = SpecialPowers;
const { propBagToObject } = ChromeUtils.import(
"resource://gre/modules/BrowserUtils.jsm"
).BrowserUtils;
/**
* Converts a property bag to object.
* @param {nsIPropertyBag} bag - The property bag to convert
* @returns {Object} - The object representation of the nsIPropertyBag
*/
function propBagToObject(bag) {
if (!(bag instanceof Ci.nsIPropertyBag)) {
throw new TypeError("Not a property bag");
}
let result = {};
for (let { name, value } of bag.enumerator) {
result[name] = value;
}
return result;
}
var modalType;
var tabSubDialogsEnabled = SpecialPowers.Services.prefs.getBoolPref(

Просмотреть файл

@ -1014,68 +1014,6 @@ var BrowserUtils = {
Services.prefs.setBoolPref(FISSION_EXPERIMENT_PREF_QUALIFIED, false);
}
},
/**
* Converts a property bag to object.
* @param {nsIPropertyBag} bag - The property bag to convert
* @returns {Object} - The object representation of the nsIPropertyBag
*/
propBagToObject(bag) {
function toValue(property) {
if (typeof property != "object") {
return property;
}
if (Array.isArray(property)) {
return property.map(this.toValue, this);
}
if (property && property instanceof Ci.nsIPropertyBag) {
return this.propBagToObject(property);
}
return property;
}
if (!(bag instanceof Ci.nsIPropertyBag)) {
throw new TypeError("Not a property bag");
}
let result = {};
for (let { name, value: property } of bag.enumerator) {
let value = toValue(property);
result[name] = value;
}
return result;
},
/**
* Converts an object to a property bag.
* @param {Object} obj - The object to convert.
* @returns {nsIPropertyBag} - The property bag representation of the object.
*/
objectToPropBag(obj) {
function fromValue(value) {
if (typeof value == "function") {
return null; // Emulating the behavior of JSON.stringify with functions
}
if (Array.isArray(value)) {
return value.map(this.fromValue, this);
}
if (value == null || typeof value != "object") {
// Auto-converted to nsIVariant
return value;
}
return this.objectToPropBag(value);
}
if (obj == null || typeof obj != "object") {
throw new TypeError("Invalid object: " + obj);
}
let bag = Cc["@mozilla.org/hash-property-bag;1"].createInstance(
Ci.nsIWritablePropertyBag
);
for (let k of Object.keys(obj)) {
let value = fromValue(obj[k]);
bag.setProperty(k, value);
}
return bag;
},
};
XPCOMUtils.defineLazyPreferenceGetter(