Bug 765468 - Use a dictionary for the argument to the XMLHttpRequest constructor; r=bz
authorMs2ger <ms2ger@gmail.com>
Thu, 21 Jun 2012 09:21:55 +0200
changeset 99855 d341aed9218c2dfd9b8c90a2829f15e1e9245834
parent 99854 829ce8dbcf65a943c008b464bb52a48787ff3c04
child 99856 f02a28f83eddf3ad5f64e0ef970477c0e6c15f62
push idunknown
push userunknown
push dateunknown
reviewersbz
bugs765468
milestone16.0a1
Bug 765468 - Use a dictionary for the argument to the XMLHttpRequest constructor; r=bz
content/base/src/nsXMLHttpRequest.cpp
content/base/src/nsXMLHttpRequest.h
content/base/test/test_XHR_parameters.html
dom/webidl/XMLHttpRequest.webidl
dom/workers/XMLHttpRequest.cpp
dom/workers/XMLHttpRequest.h
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -548,38 +548,46 @@ nsXMLHttpRequest::Initialize(nsISupports
 
 nsresult
 nsXMLHttpRequest::InitParameters(JSContext* aCx, const jsval* aParams)
 {
   XMLHttpRequestParameters params;
   nsresult rv = params.Init(aCx, aParams);
   NS_ENSURE_SUCCESS(rv, rv);
 
+  InitParameters(params.mozAnon, params.mozSystem);
+  return NS_OK;
+}
+
+void
+nsXMLHttpRequest::InitParameters(bool aAnon, bool aSystem)
+{
   // Check for permissions.
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(GetOwner());
-  NS_ENSURE_TRUE(window && window->GetDocShell(), NS_OK);
+  if (!window || !window->GetDocShell()) {
+    return;
+  }
 
   // Chrome is always allowed access, so do the permission check only
   // for non-chrome pages.
   if (!nsContentUtils::IsCallerChrome()) {
     nsCOMPtr<nsIDocument> doc = do_QueryInterface(window->GetExtantDocument());
-    NS_ENSURE_TRUE(doc, NS_OK);
+    if (!doc) {
+      return;
+    }
 
     nsCOMPtr<nsIURI> uri;
     doc->NodePrincipal()->GetURI(getter_AddRefs(uri));
-
     if (!nsContentUtils::URIIsChromeOrInPref(uri, "dom.systemXHR.whitelist")) {
-      return NS_OK;
+      return;
     }
   }
 
-  mIsAnon = params.mozAnon;
-  mIsSystem = params.mozSystem;
-
-  return NS_OK;
+  mIsAnon = aAnon;
+  mIsSystem = aSystem;
 }
 
 void
 nsXMLHttpRequest::ResetResponse()
 {
   mResponseXML = nsnull;
   mResponseBody.Truncate();
   mResponseText.Truncate();
--- a/content/base/src/nsXMLHttpRequest.h
+++ b/content/base/src/nsXMLHttpRequest.h
@@ -177,34 +177,31 @@ public:
   {
     return GetOwner();
   }
 
   // The WebIDL constructor.
   static already_AddRefed<nsXMLHttpRequest>
   Constructor(JSContext* aCx,
               nsISupports* aGlobal,
-              const mozilla::dom::Optional<jsval>& aParams,
+              const mozilla::dom::Nullable<mozilla::dom::MozXMLHttpRequestParameters>& aParams,
               ErrorResult& aRv)
   {
     nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal);
     nsCOMPtr<nsIScriptObjectPrincipal> principal = do_QueryInterface(aGlobal);
     if (!window || ! principal) {
       aRv.Throw(NS_ERROR_FAILURE);
       return NULL;
     }
 
     nsRefPtr<nsXMLHttpRequest> req = new nsXMLHttpRequest();
     req->Construct(principal->GetPrincipal(), window);
-    if (aParams.WasPassed()) {
-      nsresult rv = req->InitParameters(aCx, &aParams.Value());
-      if (NS_FAILED(rv)) {
-        aRv.Throw(rv);
-        return req.forget();
-      }
+    if (!aParams.IsNull()) {
+      const mozilla::dom::MozXMLHttpRequestParameters& params = aParams.Value();
+      req->InitParameters(params.mozAnon, params.mozSystem);
     }
     return req.forget();
   }
 
   void Construct(nsIPrincipal* aPrincipal,
                  nsPIDOMWindow* aOwnerWindow,
                  nsIURI* aBaseURI = NULL)
   {
@@ -212,16 +209,17 @@ public:
     MOZ_ASSERT_IF(aOwnerWindow, aOwnerWindow->IsInnerWindow());
     mPrincipal = aPrincipal;
     BindToOwner(aOwnerWindow);
     mBaseURI = aBaseURI;
   }
 
   // Initialize XMLHttpRequestParameter object.
   nsresult InitParameters(JSContext* aCx, const jsval* aParams);
+  void InitParameters(bool aAnon, bool aSystem);
 
   NS_DECL_ISUPPORTS_INHERITED
 
   // nsIXMLHttpRequest
   NS_DECL_NSIXMLHTTPREQUEST
 
   // nsIJSXMLHttpRequest
   NS_IMETHOD GetOnuploadprogress(nsIDOMEventListener** aOnuploadprogress);
--- a/content/base/test/test_XHR_parameters.html
+++ b/content/base/test/test_XHR_parameters.html
@@ -16,27 +16,33 @@
 </div>
 <pre id="test">
 <script class="testbody" type="application/javascript;version=1.8">
 
 function runTests() {
   SimpleTest.waitForExplicitFinish();
 
   let validParameters = [
+    undefined,
+    null,
     {},
     {mozSystem: ""},
     {mozSystem: 0},
     {mozAnon: 1},
     {mozAnon: []},
     {get mozAnon() { return true; }},
   ];
 
   let invalidParameters = [
-    undefined,
-    null,
+    0,
+    7,
+    Math.PI,
+    "string",
+    true,
+    false,
     {get mozSystem() { throw "Bla"; } },
   ];
 
   let havePrivileges = false;
 
   function testValidParameter(value) {
     let xhr;
     try {
--- a/dom/webidl/XMLHttpRequest.webidl
+++ b/dom/webidl/XMLHttpRequest.webidl
@@ -26,17 +26,37 @@ enum XMLHttpRequestResponseType {
   "text",
 
   // Mozilla-specific stuff
   "moz-chunked-text",
   "moz-chunked-arraybuffer",
   "moz-blob"
 };
 
-[Constructor(optional any params)]
+/**
+ * Parameters for instantiating an XMLHttpRequest. They are passed as an
+ * optional argument to the constructor:
+ *
+ *  new XMLHttpRequest({anon: true, system: true});
+ */
+dictionary MozXMLHttpRequestParameters
+{
+  /**
+   * If true, the request will be sent without cookie and authentication
+   * headers.
+   */
+  boolean mozAnon = false;
+
+  /**
+   * If true, the same origin policy will not be enforced on the request.
+   */
+  boolean mozSystem = false;
+};
+
+[Constructor(optional MozXMLHttpRequestParameters? params = null)]
 interface XMLHttpRequest : XMLHttpRequestEventTarget {
   // event handler
   [TreatNonCallableAsNull] attribute Function? onreadystatechange;
 
   // states
   const unsigned short UNSENT = 0;
   const unsigned short OPENED = 1;
   const unsigned short HEADERS_RECEIVED = 2;
--- a/dom/workers/XMLHttpRequest.cpp
+++ b/dom/workers/XMLHttpRequest.cpp
@@ -1461,17 +1461,17 @@ XMLHttpRequest::_finalize(JSFreeOp* aFop
   ReleaseProxy(XHRIsGoingAway);
   XMLHttpRequestEventTarget::_finalize(aFop);
 }
 
 // static
 XMLHttpRequest*
 XMLHttpRequest::Constructor(JSContext* aCx,
                             JSObject* aGlobal,
-                            const Optional<jsval>& aParams,
+                            const Nullable<MozXMLHttpRequestParametersWorkers>& aParams,
                             ErrorResult& aRv)
 {
   WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
   MOZ_ASSERT(workerPrivate);
 
   nsRefPtr<XMLHttpRequest> xhr = new XMLHttpRequest(aCx, workerPrivate);
 
   if (!Wrap(aCx, aGlobal, xhr)) {
--- a/dom/workers/XMLHttpRequest.h
+++ b/dom/workers/XMLHttpRequest.h
@@ -67,17 +67,18 @@ public:
   virtual void
   _trace(JSTracer* aTrc) MOZ_OVERRIDE;
 
   virtual void
   _finalize(JSFreeOp* aFop) MOZ_OVERRIDE;
 
   static XMLHttpRequest*
   Constructor(JSContext* aCx, JSObject* aGlobal,
-              const Optional<jsval>& aParams, ErrorResult& aRv);
+              const Nullable<MozXMLHttpRequestParametersWorkers>& aParams,
+              ErrorResult& aRv);
   void
   Unpin();
 
   bool
   Notify(JSContext* aCx, Status aStatus) MOZ_OVERRIDE;
 
 #define IMPL_GETTER_AND_SETTER(_type)                                          \
   JSObject*                                                                    \