Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope. r=kmag

MozReview-Commit-ID: Bx95Cgx0EuH

--HG--
extra : rebase_source : bdc82cee761ab389ee17235ca115e3a964a55797
This commit is contained in:
Luca Greco 2016-11-28 20:11:06 +01:00
Родитель 1dd5ea0353
Коммит ce1cef68ba
1 изменённых файлов: 16 добавлений и 3 удалений

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

@ -18,7 +18,20 @@ XPCOMUtils.defineLazyModuleGetter(this, "OS",
XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
"resource://gre/modules/AsyncShutdown.jsm");
function jsonReplacer(key, value) {
/**
* Helper function used to sanitize the objects that have to be saved in the ExtensionStorage.
*
* @param {BaseContext} context
* The current extension context.
* @param {string} key
* The key of the current JSON property.
* @param {any} value
* The value of the current JSON property.
*
* @returns {any}
* The sanitized value of the property.
*/
function jsonReplacer(context, key, value) {
switch (typeof(value)) {
// Serialize primitive types as-is.
case "string":
@ -49,7 +62,7 @@ function jsonReplacer(key, value) {
if (!key) {
// If this is the root object, and we can't serialize it, serialize
// the value to an empty object.
return {};
return new context.cloneScope.Object();
}
// Everything else, omit entirely.
@ -72,7 +85,7 @@ this.ExtensionStorage = {
* The sanitized value.
*/
sanitize(value, context) {
let json = context.jsonStringify(value, jsonReplacer);
let json = context.jsonStringify(value, jsonReplacer.bind(null, context));
return JSON.parse(json);
},