Bug 990729 - No longer store addonId in sandbox metadata (r=bholley)
authorBill McCloskey <wmccloskey@mozilla.com>
Sat, 21 Jun 2014 11:54:37 -0700
changeset 189946 ec2967398c6230daedf7ab4a23ceba1bab56d2fb
parent 189945 1b5f05eeebd558b593eb042cb05cc0edb8ef15bf
child 189947 53bfc7d490291a8d79b7a0df01920faabb0cbde6
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbholley
bugs990729
milestone33.0a1
Bug 990729 - No longer store addonId in sandbox metadata (r=bholley)
js/xpconnect/idl/xpccomponents.idl
js/xpconnect/src/Sandbox.cpp
js/xpconnect/src/XPCComponents.cpp
js/xpconnect/src/xpcprivate.h
js/xpconnect/tests/unit/test_sandbox_metadata.js
toolkit/mozapps/extensions/internal/XPIProvider.jsm
--- a/js/xpconnect/idl/xpccomponents.idl
+++ b/js/xpconnect/idl/xpccomponents.idl
@@ -153,16 +153,29 @@ interface nsIXPCComponents_Utils : nsISu
      */
     [implicit_jscontext,optional_argc]
     jsval evalInSandbox(in AString source, in jsval sandbox,
                         [optional] in jsval version,
                         [optional] in AUTF8String filename,
                         [optional] in long lineNo);
 
     /*
+     * getSandboxAddonId is designed to be called from JavaScript only.
+     *
+     * getSandboxAddonId retrieves the add-on ID associated with
+     * a sandbox object. It will return undefined if there
+     * is no add-on ID attached to the sandbox.
+     *
+     * var s = C.u.Sandbox(..., { addonId: "123" });
+     * var id = C.u.getSandboxAddonId(s);
+     */
+    [implicit_jscontext]
+    jsval getSandboxAddonId(in jsval sandbox);
+
+    /*
      * getSandboxMetadata is designed to be called from JavaScript only.
      *
      * getSandboxMetadata retrieves the metadata associated with
      * a sandbox object. It will return undefined if there
      * is no metadata attached to the sandbox.
      *
      * var s = C.u.Sandbox(..., { metadata: "metadata" });
      * var metadata = C.u.getSandboxMetadata(s);
--- a/js/xpconnect/src/Sandbox.cpp
+++ b/js/xpconnect/src/Sandbox.cpp
@@ -1985,16 +1985,35 @@ xpc::NewFunctionForwarder(JSContext *cx,
     RootedId emptyId(cx);
     RootedValue emptyStringValue(cx, JS_GetEmptyStringValue(cx));
     if (!JS_ValueToId(cx, emptyStringValue, &emptyId))
         return false;
 
     return NewFunctionForwarder(cx, emptyId, callable, doclone, vp);
 }
 
+nsresult
+xpc::GetSandboxAddonId(JSContext *cx, HandleObject sandbox, MutableHandleValue rval)
+{
+    MOZ_ASSERT(NS_IsMainThread());
+    MOZ_ASSERT(IsSandbox(sandbox));
+
+    JSAddonId *id = JS::AddonIdOfObject(sandbox);
+    if (!id) {
+        rval.setNull();
+        return NS_OK;
+    }
+
+    JS::RootedValue idStr(cx, StringValue(JS::StringOfAddonId(id)));
+    if (!JS_WrapValue(cx, &idStr))
+        return NS_ERROR_UNEXPECTED;
+
+    rval.set(idStr);
+    return NS_OK;
+}
 
 nsresult
 xpc::GetSandboxMetadata(JSContext *cx, HandleObject sandbox, MutableHandleValue rval)
 {
     MOZ_ASSERT(NS_IsMainThread());
     MOZ_ASSERT(IsSandbox(sandbox));
 
     RootedValue metadata(cx);
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -2670,16 +2670,31 @@ nsXPCComponents_Utils::EvalInSandbox(con
         }
     }
 
     return xpc::EvalInSandbox(cx, sandbox, source, filename, lineNo,
                               jsVersion, false, retval);
 }
 
 NS_IMETHODIMP
+nsXPCComponents_Utils::GetSandboxAddonId(HandleValue sandboxVal,
+                                         JSContext *cx, MutableHandleValue rval)
+{
+    if (!sandboxVal.isObject())
+        return NS_ERROR_INVALID_ARG;
+
+    RootedObject sandbox(cx, &sandboxVal.toObject());
+    sandbox = js::CheckedUnwrap(sandbox);
+    if (!sandbox || !xpc::IsSandbox(sandbox))
+        return NS_ERROR_INVALID_ARG;
+
+    return xpc::GetSandboxAddonId(cx, sandbox, rval);
+}
+
+NS_IMETHODIMP
 nsXPCComponents_Utils::GetSandboxMetadata(HandleValue sandboxVal,
                                           JSContext *cx, MutableHandleValue rval)
 {
     if (!sandboxVal.isObject())
         return NS_ERROR_INVALID_ARG;
 
     RootedObject sandbox(cx, &sandboxVal.toObject());
     sandbox = js::CheckedUnwrap(sandbox);
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -3427,16 +3427,20 @@ CreateSandboxObject(JSContext *cx, JS::M
 // an exception to a string, evalInSandbox will return an NS_ERROR_*
 // result, and cx->exception will be empty.
 nsresult
 EvalInSandbox(JSContext *cx, JS::HandleObject sandbox, const nsAString& source,
               const nsACString& filename, int32_t lineNo,
               JSVersion jsVersion, bool returnStringOnly,
               JS::MutableHandleValue rval);
 
+nsresult
+GetSandboxAddonId(JSContext *cx, JS::HandleObject sandboxArg,
+                  JS::MutableHandleValue rval);
+
 // Helper for retrieving metadata stored in a reserved slot. The metadata
 // is set during the sandbox creation using the "metadata" option.
 nsresult
 GetSandboxMetadata(JSContext *cx, JS::HandleObject sandboxArg,
                    JS::MutableHandleValue rval);
 
 nsresult
 SetSandboxMetadata(JSContext *cx, JS::HandleObject sandboxArg,
--- a/js/xpconnect/tests/unit/test_sandbox_metadata.js
+++ b/js/xpconnect/tests/unit/test_sandbox_metadata.js
@@ -2,20 +2,22 @@
  * 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/. */
 
 /* See https://bugzilla.mozilla.org/show_bug.cgi?id=898559 */
 
 function run_test()
 {
   let sandbox = Components.utils.Sandbox("http://www.blah.com", {
-    metadata: "test metadata"
+    metadata: "test metadata",
+    addonId: "12345"
   });
 
   do_check_eq(Components.utils.getSandboxMetadata(sandbox), "test metadata");
+  do_check_eq(Components.utils.getSandboxAddonId(sandbox), "12345");
 
   let sandbox = Components.utils.Sandbox("http://www.blah.com", {
     metadata: { foopy: { bar: 2 }, baz: "hi" }
   });
 
   let metadata = Components.utils.getSandboxMetadata(sandbox);
   do_check_eq(metadata.baz, "hi");
   do_check_eq(metadata.foopy.bar, 2);
--- a/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+++ b/toolkit/mozapps/extensions/internal/XPIProvider.jsm
@@ -4136,28 +4136,30 @@ this.XPIProvider = {
 
     let principal = Cc["@mozilla.org/systemprincipal;1"].
                     createInstance(Ci.nsIPrincipal);
 
     if (!aFile.exists()) {
       this.bootstrapScopes[aId] =
         new Cu.Sandbox(principal, { sandboxName: aFile.path,
                                     wantGlobalProperties: ["indexedDB"],
+                                    addonId: aId,
                                     metadata: { addonID: aId } });
       logger.error("Attempted to load bootstrap scope from missing directory " + aFile.path);
       return;
     }
 
     let uri = getURIForResourceInFile(aFile, "bootstrap.js").spec;
     if (aType == "dictionary")
       uri = "resource://gre/modules/addons/SpellCheckDictionaryBootstrap.js"
 
     this.bootstrapScopes[aId] =
       new Cu.Sandbox(principal, { sandboxName: uri,
                                   wantGlobalProperties: ["indexedDB"],
+                                  addonId: aId,
                                   metadata: { addonID: aId, URI: uri } });
 
     let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
                  createInstance(Ci.mozIJSSubScriptLoader);
 
     try {
       // Copy the reason values from the global object into the bootstrap scope.
       for (let name in BOOTSTRAP_REASONS)