2012-08-19 19:00:19 +00: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 18:45:14 +00:00
|
|
|
const Cu = Components.utils;
|
2012-08-19 19:00:19 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["ObjectWrapper"];
|
2012-08-19 19:00:19 +00:00
|
|
|
|
|
|
|
// Makes sure that we expose correctly chrome JS objects to content.
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.ObjectWrapper = {
|
2012-12-11 08:30:53 +00:00
|
|
|
getObjectKind: function objWrapper_getObjectKind(aObject) {
|
2012-12-12 22:58:11 +00:00
|
|
|
if (aObject === null || aObject === undefined) {
|
|
|
|
return "primitive";
|
|
|
|
} else if (Array.isArray(aObject)) {
|
2012-10-25 18:45:14 +00:00
|
|
|
return "array";
|
2012-12-11 08:30:53 +00:00
|
|
|
} else if (aObject instanceof Ci.nsIDOMFile) {
|
|
|
|
return "file";
|
|
|
|
} else if (aObject instanceof Ci.nsIDOMBlob) {
|
2012-10-25 18:45:14 +00:00
|
|
|
return "blob";
|
2012-12-17 05:29:00 +00:00
|
|
|
} else if (aObject instanceof Date) {
|
|
|
|
return "date";
|
2012-10-25 18:45:14 +00:00
|
|
|
} else if (typeof aObject == "object") {
|
|
|
|
return "object";
|
|
|
|
} else {
|
|
|
|
return "primitive";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-19 19:00:19 +00:00
|
|
|
wrap: function objWrapper_wrap(aObject, aCtxt) {
|
2012-10-25 18:45:14 +00:00
|
|
|
// First check wich kind of object we have.
|
|
|
|
let kind = this.getObjectKind(aObject);
|
2012-12-12 22:58:11 +00:00
|
|
|
if (kind == "array") {
|
2012-10-25 18:45:14 +00:00
|
|
|
let res = Cu.createArrayIn(aCtxt);
|
|
|
|
aObject.forEach(function(aObj) {
|
|
|
|
res.push(this.wrap(aObj, aCtxt));
|
|
|
|
}, this);
|
|
|
|
return res;
|
2012-12-11 08:30:53 +00:00
|
|
|
} else if (kind == "file") {
|
|
|
|
return new aCtxt.File(aObject,
|
|
|
|
{ name: aObject.name,
|
|
|
|
type: aObject.type });
|
2012-10-25 18:45:14 +00:00
|
|
|
} else if (kind == "blob") {
|
2012-12-15 10:09:02 +00:00
|
|
|
return new aCtxt.Blob([aObject], { type: aObject.type });
|
2012-12-17 05:29:00 +00:00
|
|
|
} else if (kind == "date") {
|
|
|
|
return Cu.createDateIn(aCtxt, aObject.getTime());
|
2012-10-25 18:45:14 +00:00
|
|
|
} else if (kind == "primitive") {
|
|
|
|
return aObject;
|
|
|
|
}
|
|
|
|
|
2012-12-11 08:30:53 +00:00
|
|
|
// Fall-through, we now have a dictionnary object.
|
2012-08-19 19:00:19 +00:00
|
|
|
let res = Cu.createObjectIn(aCtxt);
|
|
|
|
let propList = { };
|
|
|
|
for (let prop in aObject) {
|
|
|
|
propList[prop] = {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
2012-12-11 08:30:53 +00:00
|
|
|
value: this.wrap(aObject[prop], aCtxt)
|
2012-08-19 19:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Object.defineProperties(res, propList);
|
|
|
|
Cu.makeObjectPropsNormal(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|