Bug 1182537 - Use channel->ascynOpen2 in dom/security/nsCORSListenerProxy (r=sicking)
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Thu, 30 Jul 2015 08:59:20 -0700
changeset 287173 dc7fa51079ad659350332e63cb44e4d6d8d402b8
parent 287172 a110782901107a9c9d7c9a38120554df3a71336b
child 287174 1859af04f2021468f929b00efbf2bc98b26d31c7
child 287175 2d831e32aada68be0aa78b33e1ed9c9a96cf1bb4
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking
bugs1182537
milestone42.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 1182537 - Use channel->ascynOpen2 in dom/security/nsCORSListenerProxy (r=sicking)
dom/security/nsCORSListenerProxy.cpp
--- a/dom/security/nsCORSListenerProxy.cpp
+++ b/dom/security/nsCORSListenerProxy.cpp
@@ -1176,17 +1176,34 @@ nsCORSPreflightListener::OnStartRequest(
   if (NS_SUCCEEDED(rv)) {
     rv = status;
   }
 
   if (NS_SUCCEEDED(rv)) {
     // Everything worked, try to cache and then fire off the actual request.
     AddResultToCache(aRequest);
 
-    rv = mOuterChannel->AsyncOpen(mOuterListener, mOuterContext);
+    nsCOMPtr<nsILoadInfo> loadInfo = mOuterChannel->GetLoadInfo();
+    MOZ_ASSERT(loadInfo, "can not perform CORS preflight without a loadInfo");
+    if (!loadInfo) {
+      return NS_ERROR_FAILURE;
+    }
+    nsSecurityFlags securityMode = loadInfo->GetSecurityMode();
+
+    MOZ_ASSERT(securityMode == 0 ||
+               securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
+               "how did we end up here?");
+
+    if (securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
+      MOZ_ASSERT(!mOuterContext, "AsyncOpen(2) does not take context as a second arg");
+      rv = mOuterChannel->AsyncOpen2(mOuterListener);
+    }
+    else {
+      rv = mOuterChannel->AsyncOpen(mOuterListener, mOuterContext);
+    }
   }
 
   if (NS_FAILED(rv)) {
     mOuterChannel->Cancel(rv);
     mOuterListener->OnStartRequest(mOuterChannel, mOuterContext);
     mOuterListener->OnStopRequest(mOuterChannel, mOuterContext, rv);
     
     return rv;
@@ -1255,60 +1272,59 @@ NS_StartCORSPreflight(nsIChannel* aReque
   nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequestChannel));
   NS_ENSURE_TRUE(httpChannel, NS_ERROR_UNEXPECTED);
   httpChannel->GetRequestMethod(method);
 
   nsCOMPtr<nsIURI> uri;
   nsresult rv = NS_GetFinalChannelURI(aRequestChannel, getter_AddRefs(uri));
   NS_ENSURE_SUCCESS(rv, rv);
 
+  nsCOMPtr<nsILoadInfo> loadInfo = aRequestChannel->GetLoadInfo();
+  MOZ_ASSERT(loadInfo, "can not perform CORS preflight without a loadInfo");
+  if (!loadInfo) {
+    return NS_ERROR_FAILURE;
+  }
+
+  nsSecurityFlags securityMode = loadInfo->GetSecurityMode();
+
+  MOZ_ASSERT(securityMode == 0 ||
+             securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
+             "how did we end up here?");
+
   nsPreflightCache::CacheEntry* entry =
     sPreflightCache ?
     sPreflightCache->GetEntry(uri, aPrincipal, aWithCredentials, false) :
     nullptr;
 
   if (entry && entry->CheckRequest(method, aUnsafeHeaders)) {
     // We have a cached preflight result, just start the original channel
+    if (securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
+      return aRequestChannel->AsyncOpen2(aListener);
+    }
     return aRequestChannel->AsyncOpen(aListener, nullptr);
   }
 
   // Either it wasn't cached or the cached result has expired. Build a
   // channel for the OPTIONS request.
 
   nsCOMPtr<nsILoadGroup> loadGroup;
   rv = aRequestChannel->GetLoadGroup(getter_AddRefs(loadGroup));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsLoadFlags loadFlags;
   rv = aRequestChannel->GetLoadFlags(&loadFlags);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsILoadInfo> loadInfo;
-  rv = aRequestChannel->GetLoadInfo(getter_AddRefs(loadInfo));
-  NS_ENSURE_SUCCESS(rv, rv);
-
   nsCOMPtr<nsIChannel> preflightChannel;
-  if (loadInfo) {
-    rv = NS_NewChannelInternal(getter_AddRefs(preflightChannel),
-                               uri,
-                               loadInfo,
-                               loadGroup,
-                               nullptr,   // aCallbacks
-                               loadFlags);
-  }
-  else {
-    rv = NS_NewChannel(getter_AddRefs(preflightChannel),
-                       uri,
-                       nsContentUtils::GetSystemPrincipal(),
-                       nsILoadInfo::SEC_NORMAL,
-                       nsIContentPolicy::TYPE_OTHER,
-                       loadGroup,
-                       nullptr,   // aCallbacks
-                       loadFlags);
-  }
+  rv = NS_NewChannelInternal(getter_AddRefs(preflightChannel),
+                             uri,
+                             loadInfo,
+                             loadGroup,
+                             nullptr,   // aCallbacks
+                             loadFlags);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIHttpChannel> preHttp = do_QueryInterface(preflightChannel);
   NS_ASSERTION(preHttp, "Failed to QI to nsIHttpChannel!");
 
   rv = preHttp->SetRequestMethod(NS_LITERAL_CSTRING("OPTIONS"));
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -1319,26 +1335,29 @@ NS_StartCORSPreflight(nsIChannel* aReque
   }
   
   // Set up listener which will start the original channel
   nsCOMPtr<nsIStreamListener> preflightListener =
     new nsCORSPreflightListener(aRequestChannel, aListener, nullptr, aPrincipal,
                                 method, aWithCredentials);
   NS_ENSURE_TRUE(preflightListener, NS_ERROR_OUT_OF_MEMORY);
 
-  nsRefPtr<nsCORSListenerProxy> corsListener =
-    new nsCORSListenerProxy(preflightListener, aPrincipal,
-                            aWithCredentials, method,
-                            aUnsafeHeaders);
-  rv = corsListener->Init(preflightChannel, DataURIHandling::Disallow);
-  NS_ENSURE_SUCCESS(rv, rv);
-  preflightListener = corsListener;
-
   // Start preflight
-  rv = preflightChannel->AsyncOpen(preflightListener, nullptr);
+  if (securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
+    rv = preflightChannel->AsyncOpen2(preflightListener);
+  }
+  else {
+    nsRefPtr<nsCORSListenerProxy> corsListener =
+      new nsCORSListenerProxy(preflightListener, aPrincipal,
+                              aWithCredentials, method,
+                              aUnsafeHeaders);
+    rv = corsListener->Init(preflightChannel, DataURIHandling::Disallow);
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = preflightChannel->AsyncOpen(corsListener, nullptr);
+  }
   NS_ENSURE_SUCCESS(rv, rv);
   
   // Return newly created preflight channel
   preflightChannel.forget(aPreflightChannel);
 
   return NS_OK;
 }