Bug 821633 - Alarm API - .getAll() need to wrap the objects respecting to the content window (part 1, ObjectWrapper.jsm and Cu.createDateIn()). r=sicking
authorGene Lian <clian@mozilla.com>
Mon, 17 Dec 2012 13:29:00 +0800
changeset 125440 be80d621f9bb9a084378841a50672b3bd1c39cee
parent 125439 86e0432487a7c91b557448cb265a95f7db1afc75
child 125441 c4332c6f071d492118174f49a56f4d57f01dcbfe
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs821633
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 821633 - Alarm API - .getAll() need to wrap the objects respecting to the content window (part 1, ObjectWrapper.jsm and Cu.createDateIn()). r=sicking
dom/base/ObjectWrapper.jsm
js/xpconnect/idl/xpccomponents.idl
js/xpconnect/src/XPCComponents.cpp
--- a/dom/base/ObjectWrapper.jsm
+++ b/dom/base/ObjectWrapper.jsm
@@ -17,16 +17,18 @@ this.ObjectWrapper = {
     if (aObject === null || aObject === undefined) {
       return "primitive";
     } else if (Array.isArray(aObject)) {
       return "array";
     } else if (aObject instanceof Ci.nsIDOMFile) {
       return "file";
     } else if (aObject instanceof Ci.nsIDOMBlob) {
       return "blob";
+    } else if (aObject instanceof Date) {
+      return "date";
     } else if (typeof aObject == "object") {
       return "object";
     } else {
       return "primitive";
     }
   },
 
   wrap: function objWrapper_wrap(aObject, aCtxt) {
@@ -39,16 +41,18 @@ this.ObjectWrapper = {
       }, this);
       return res;
     } else if (kind == "file") {
       return new aCtxt.File(aObject,
                             { name: aObject.name,
                               type: aObject.type });
     } else if (kind == "blob") {
       return new aCtxt.Blob([aObject], { type: aObject.type });
+    } else if (kind == "date") {
+      return Cu.createDateIn(aCtxt, aObject.getTime());
     } else if (kind == "primitive") {
       return aObject;
     }
 
     // Fall-through, we now have a dictionnary object.
     let res = Cu.createObjectIn(aCtxt);
     let propList = { };
     for (let prop in aObject) {
--- a/js/xpconnect/idl/xpccomponents.idl
+++ b/js/xpconnect/idl/xpccomponents.idl
@@ -113,17 +113,17 @@ interface nsIXPCComponents_utils_Sandbox
 interface ScheduledGCCallback : nsISupports
 {
     void callback();
 };
 
 /**
 * interface of Components.utils
 */
-[scriptable, uuid(b3290d8e-b010-4693-a261-10bbf006b8f0)]
+[scriptable, uuid(50799877-9ddc-4784-8a58-da7366c1368f)]
 interface nsIXPCComponents_Utils : nsISupports
 {
 
     /* reportError is designed to be called from JavaScript only.
      *
      * It will report a JS Error object to the JS console, and return. It
      * is meant for use in exception handler blocks which want to "eat"
      * an exception, but still want to report it to the console.
@@ -298,16 +298,24 @@ interface nsIXPCComponents_Utils : nsISu
      * Returns an array created in |vobj|'s compartment.
      */
     [implicit_jscontext]
     jsval createArrayIn(in jsval vobj);
 
     /*
      * To be called from JS only.
      *
+     * Returns a date given timestamp |msec| created in |vobj|'s compartment.
+     */
+    [implicit_jscontext]
+    jsval createDateIn(in jsval vobj, in long long msec);
+
+    /*
+     * To be called from JS only.
+     *
      * Ensures that all functions come from vobj's scope (and aren't cross
      * compartment wrappers).
      */
     [implicit_jscontext]
     void makeObjectPropsNormal(in jsval vobj);
 
     /**
      * Determines whether this object is backed by a DeadObjectProxy.
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -4219,16 +4219,44 @@ nsXPCComponents_Utils::CreateArrayIn(con
     }
 
     if (!JS_WrapObject(cx, &obj))
         return NS_ERROR_FAILURE;
     *rval = OBJECT_TO_JSVAL(obj);
     return NS_OK;
 }
 
+/* jsval createDateIn(in jsval vobj, in long long msec); */
+NS_IMETHODIMP
+nsXPCComponents_Utils::CreateDateIn(const jsval &vobj,
+                                    int64_t msec,
+                                    JSContext *cx, jsval *rval)
+{
+    if (!cx)
+        return NS_ERROR_FAILURE;
+
+    // first argument must be an object
+    if (JSVAL_IS_PRIMITIVE(vobj))
+        return NS_ERROR_XPC_BAD_CONVERT_JS;
+
+    JSObject *scope = js::UnwrapObject(JSVAL_TO_OBJECT(vobj));
+    JSObject *obj;
+    {
+        JSAutoCompartment ac(cx, scope);
+        obj =  JS_NewDateObjectMsec(cx, msec);
+        if (!obj)
+            return NS_ERROR_FAILURE;
+    }
+
+    if (!JS_WrapObject(cx, &obj))
+        return NS_ERROR_FAILURE;
+    *rval = OBJECT_TO_JSVAL(obj);
+    return NS_OK;
+}
+
 JSBool
 FunctionWrapper(JSContext *cx, unsigned argc, jsval *vp)
 {
     JSObject *callee = &JS_CALLEE(cx, vp).toObject();
     JS::Value v = js::GetFunctionNativeReserved(callee, 0);
     NS_ASSERTION(v.isObject(), "weird function");
 
     JSObject *obj = JS_THIS_OBJECT(cx, vp);