Bug 892203 - Minor cleanups in sandbox.cpp. r=bholley
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Wed, 04 Sep 2013 12:15:50 +0200
changeset 145768 f5743e7da43667013c15cb3796cffebd5c723fe0
parent 145767 bb1d06db46ebc7a1f9e114c11f99172ffb4a5fe8
child 145769 365f2aca45cf51fc85184c09ed844744ee68ab46
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersbholley
bugs892203
milestone26.0a1
Bug 892203 - Minor cleanups in sandbox.cpp. r=bholley
js/xpconnect/src/Sandbox.cpp
js/xpconnect/src/XPCWrappedNativeScope.cpp
--- a/js/xpconnect/src/Sandbox.cpp
+++ b/js/xpconnect/src/Sandbox.cpp
@@ -955,24 +955,24 @@ xpc::CreateSandboxObject(JSContext *cx, 
         }
 
         nsCOMPtr<nsIScriptObjectPrincipal> sbp =
             new SandboxPrivate(principal, sandbox);
 
         // Pass on ownership of sbp to |sandbox|.
         JS_SetPrivate(sandbox, sbp.forget().get());
 
-      bool allowComponents = nsContentUtils::IsSystemPrincipal(principal) ||
-                             nsContentUtils::IsExpandedPrincipal(principal);
-      if (options.wantComponents && allowComponents &&
-          !nsXPCComponents::AttachComponentsObject(cx, GetObjectScope(sandbox)))
-          return NS_ERROR_XPC_UNEXPECTED;
+        bool allowComponents = nsContentUtils::IsSystemPrincipal(principal) ||
+                               nsContentUtils::IsExpandedPrincipal(principal);
+        if (options.wantComponents && allowComponents &&
+            !nsXPCComponents::AttachComponentsObject(cx, GetObjectScope(sandbox)))
+            return NS_ERROR_XPC_UNEXPECTED;
 
-      if (!XPCNativeWrapper::AttachNewConstructorObject(cx, sandbox))
-          return NS_ERROR_XPC_UNEXPECTED;
+        if (!XPCNativeWrapper::AttachNewConstructorObject(cx, sandbox))
+            return NS_ERROR_XPC_UNEXPECTED;
 
         if (!JS_DefineFunctions(cx, sandbox, SandboxFunctions))
             return NS_ERROR_XPC_UNEXPECTED;
 
         if (options.wantXHRConstructor &&
             !JS_DefineFunction(cx, sandbox, "XMLHttpRequest", CreateXMLHttpRequest, 0, JSFUN_CONSTRUCTOR))
             return NS_ERROR_XPC_UNEXPECTED;
 
@@ -1032,17 +1032,17 @@ nsXPCComponents_utils_Sandbox::Construct
     RootedObject obj(cx, objArg);
     return CallOrConstruct(wrapper, cx, obj, args, _retval);
 }
 
 /*
  * For sandbox constructor the first argument can be a URI string in which case
  * we use the related Codebase Principal for the sandbox.
  */
-nsresult
+static nsresult
 GetPrincipalFromString(JSContext *cx, HandleString codebase, nsIPrincipal **principal)
 {
     MOZ_ASSERT(principal);
     MOZ_ASSERT(codebase);
     nsCOMPtr<nsIURI> uri;
     nsDependentJSString codebaseStr;
     NS_ENSURE_TRUE(codebaseStr.init(cx, codebase), NS_ERROR_FAILURE);
     nsresult rv = NS_NewURI(getter_AddRefs(uri), codebaseStr);
@@ -1058,20 +1058,20 @@ GetPrincipalFromString(JSContext *cx, Ha
     rv = secman->GetNoAppCodebasePrincipal(uri, principal);
     NS_ENSURE_SUCCESS(rv, rv);
     NS_ENSURE_TRUE(*principal, NS_ERROR_FAILURE);
 
     return NS_OK;
 }
 
 /*
- * For sandbox constructor  the first argument can be a principal object or
+ * For sandbox constructor the first argument can be a principal object or
  * a script object principal (Document, Window).
  */
-nsresult
+static nsresult
 GetPrincipalOrSOP(JSContext *cx, HandleObject from, nsISupports **out)
 {
     MOZ_ASSERT(out);
     *out = NULL;
 
     nsXPConnect* xpc = nsXPConnect::XPConnect();
     nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
     xpc->GetWrappedNativeOfJSObject(cx, from,
@@ -1090,17 +1090,17 @@ GetPrincipalOrSOP(JSContext *cx, HandleO
 
     return NS_OK;
 }
 
 /*
  * The first parameter of the sandbox constructor might be an array of principals, either in string
  * format or actual objects (see GetPrincipalOrSOP)
  */
-nsresult
+static nsresult
 GetExpandedPrincipal(JSContext *cx, HandleObject arrayObj, nsIExpandedPrincipal **out)
 {
     MOZ_ASSERT(out);
     uint32_t length;
 
     if (!JS_IsArrayObject(cx, arrayObj) ||
         !JS_GetArrayLength(cx, arrayObj, &length) ||
         !length)
@@ -1154,82 +1154,81 @@ GetExpandedPrincipal(JSContext *cx, Hand
   nsCOMPtr<nsIExpandedPrincipal> result = new nsExpandedPrincipal(allowedDomains);
   result.forget(out);
   return NS_OK;
 }
 
 /*
  * Helper that tries to get a property form the options object.
  */
-nsresult
+static nsresult
 GetPropFromOptions(JSContext *cx, HandleObject from, const char *name, MutableHandleValue prop,
                    bool *found)
 {
-    if (!JS_HasProperty(cx, from, name, found))
-        return NS_ERROR_INVALID_ARG;
+    MOZ_ASSERT(found);
+    bool ok = JS_HasProperty(cx, from, name, found);
+    NS_ENSURE_TRUE(ok, NS_ERROR_INVALID_ARG);
 
-    if (*found && !JS_GetProperty(cx, from, name, prop))
-        return NS_ERROR_INVALID_ARG;
+    if (!*found)
+        return NS_OK;
 
+    ok = JS_GetProperty(cx, from, name, prop);
+    NS_ENSURE_TRUE(ok, NS_ERROR_INVALID_ARG);
     return NS_OK;
 }
 
 /*
  * Helper that tries to get a boolean property form the options object.
  */
-nsresult
+static nsresult
 GetBoolPropFromOptions(JSContext *cx, HandleObject from, const char *name, bool *prop)
 {
     MOZ_ASSERT(prop);
 
-
     RootedValue value(cx);
     bool found;
-    if (NS_FAILED(GetPropFromOptions(cx, from, name, &value, &found)))
-        return NS_ERROR_INVALID_ARG;
+    nsresult rv = GetPropFromOptions(cx, from, name, &value, &found);
+    NS_ENSURE_SUCCESS(rv, rv);
 
     if (!found)
         return NS_OK;
 
-    if (!value.isBoolean())
-        return NS_ERROR_INVALID_ARG;
+    NS_ENSURE_TRUE(value.isBoolean(), NS_ERROR_INVALID_ARG);
 
     *prop = value.toBoolean();
     return NS_OK;
 }
 
 /*
  * Helper that tries to get an object property form the options object.
  */
-nsresult
+static nsresult
 GetObjPropFromOptions(JSContext *cx, HandleObject from, const char *name, JSObject **prop)
 {
     MOZ_ASSERT(prop);
 
     RootedValue value(cx);
     bool found;
-    if (NS_FAILED(GetPropFromOptions(cx, from, name, &value, &found)))
-        return NS_ERROR_INVALID_ARG;
+    nsresult rv = GetPropFromOptions(cx, from, name, &value, &found);
+    NS_ENSURE_SUCCESS(rv, rv);
 
     if (!found) {
         *prop = NULL;
         return NS_OK;
     }
 
-    if (!value.isObject())
-        return NS_ERROR_INVALID_ARG;
-
+    NS_ENSURE_TRUE(value.isObject(), NS_ERROR_INVALID_ARG);
     *prop = &value.toObject();
     return NS_OK;
 }
 
 /*
  * Helper that tries to get a string property form the options object.
  */
-nsresult
+static nsresult
 GetStringPropFromOptions(JSContext *cx, HandleObject from, const char *name, nsCString &prop)
 {
     RootedValue value(cx);
     bool found;
     nsresult rv = GetPropFromOptions(cx, from, name, &value, &found);
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (!found)
@@ -1241,17 +1240,17 @@ GetStringPropFromOptions(JSContext *cx, 
     NS_ENSURE_TRUE(tmp, NS_ERROR_INVALID_ARG);
     prop.Adopt(tmp, strlen(tmp));
     return NS_OK;
 }
 
 /*
  * Helper that parsing the sandbox options object (from) and sets the fields of the incoming options struct (options).
  */
-nsresult
+static nsresult
 ParseOptionsObject(JSContext *cx, jsval from, SandboxOptions &options)
 {
     NS_ENSURE_TRUE(from.isObject(), NS_ERROR_INVALID_ARG);
     RootedObject optionsObject(cx, &from.toObject());
     nsresult rv = GetObjPropFromOptions(cx, optionsObject,
                                         "sandboxPrototype", options.proto.address());
     NS_ENSURE_SUCCESS(rv, rv);
 
--- a/js/xpconnect/src/XPCWrappedNativeScope.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeScope.cpp
@@ -237,17 +237,16 @@ XPCWrappedNativeScope::EnsureXBLScope(JS
     //
     // NB: One would think that wantXrays wouldn't make a difference here.
     // However, wantXrays lives a secret double life, and one of its other
     // hobbies is to waive Xray on the returned sandbox when set to false.
     // So make sure to keep this set to true, here.
     SandboxOptions options(cx);
     options.wantXrays = true;
     options.wantComponents = true;
-    options.wantXHRConstructor = false;
     options.proto = global;
     options.sameZoneAs = global;
 
     // Use an nsExpandedPrincipal to create asymmetric security.
     nsIPrincipal *principal = GetPrincipal();
     nsCOMPtr<nsIExpandedPrincipal> ep;
     MOZ_ASSERT(!(ep = do_QueryInterface(principal)));
     nsTArray< nsCOMPtr<nsIPrincipal> > principalAsArray(1);