2012-08-19 12:00:19 -07: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"
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
const Cu = Components.utils;
|
2012-08-19 12:00:19 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["ObjectWrapper"];
|
2012-08-19 12:00:19 -07:00
|
|
|
|
|
|
|
// Makes sure that we expose correctly chrome JS objects to content.
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.ObjectWrapper = {
|
2012-12-11 05:09:38 -08:00
|
|
|
getObjectKind: function objWrapper_getobjectkind(aObject) {
|
2012-10-25 11:45:14 -07:00
|
|
|
if (!aObject) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(aObject)) {
|
|
|
|
return "array";
|
2012-12-11 05:09:38 -08:00
|
|
|
} else if (aObject.mozSlice && (typeof aObject.mozSlice == "function")) {
|
2012-10-25 11:45:14 -07:00
|
|
|
return "blob";
|
|
|
|
} else if (typeof aObject == "object") {
|
|
|
|
return "object";
|
|
|
|
} else {
|
|
|
|
return "primitive";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-19 12:00:19 -07:00
|
|
|
wrap: function objWrapper_wrap(aObject, aCtxt) {
|
2012-12-11 05:09:38 -08:00
|
|
|
if (!aObject) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-10-25 11:45:14 -07:00
|
|
|
// First check wich kind of object we have.
|
|
|
|
let kind = this.getObjectKind(aObject);
|
2012-12-11 05:09:38 -08:00
|
|
|
if (kind == "array") {
|
2012-10-25 11:45:14 -07:00
|
|
|
let res = Cu.createArrayIn(aCtxt);
|
|
|
|
aObject.forEach(function(aObj) {
|
|
|
|
res.push(this.wrap(aObj, aCtxt));
|
|
|
|
}, this);
|
|
|
|
return res;
|
|
|
|
} else if (kind == "blob") {
|
|
|
|
return new aCtxt.Blob([aObject]);
|
|
|
|
} else if (kind == "primitive") {
|
|
|
|
return aObject;
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:09:38 -08:00
|
|
|
// Fall-through, we now have a dictionnary object.
|
2012-08-19 12:00:19 -07:00
|
|
|
let res = Cu.createObjectIn(aCtxt);
|
|
|
|
let propList = { };
|
|
|
|
for (let prop in aObject) {
|
2012-12-11 05:09:38 -08:00
|
|
|
let value;
|
|
|
|
let objProp = aObject[prop];
|
|
|
|
let propKind = this.getObjectKind(objProp);
|
|
|
|
if (propKind == "array") {
|
|
|
|
value = Cu.createArrayIn(aCtxt);
|
|
|
|
objProp.forEach(function(aObj) {
|
|
|
|
value.push(this.wrap(aObj, aCtxt));
|
|
|
|
}, this);
|
|
|
|
} else if (propKind == "blob") {
|
|
|
|
value = new aCtxt.Blob([objProp]);
|
|
|
|
} else if (propKind == "object") {
|
|
|
|
value = this.wrap(objProp, aCtxt);
|
|
|
|
} else {
|
|
|
|
value = objProp;
|
|
|
|
}
|
2012-08-19 12:00:19 -07:00
|
|
|
propList[prop] = {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2012-12-11 05:09:38 -08:00
|
|
|
value: value
|
2012-08-19 12:00:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperties(res, propList);
|
|
|
|
Cu.makeObjectPropsNormal(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|