Bug 1478171 - [1.0] Forward channel redirect to nsILoadURIDelegate to allow external handling. r=snorp,smaug, a=RyanVM
authorEugen Sawin <esawin@mozilla.com>
Wed, 31 Oct 2018 16:16:28 -0500
changeset 501032 4c7bbc4c6fbc8a4233590f01cd74ae44fad5f24c
parent 501031 309fbb6fbc62cd2fa09b4a2d6fc3d5b92b1f8b55
child 501033 1041bb5b5556cd21989d2af036f0acf1f323345b
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp, smaug, RyanVM
bugs1478171
milestone64.0
Bug 1478171 - [1.0] Forward channel redirect to nsILoadURIDelegate to allow external handling. r=snorp,smaug, a=RyanVM
mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/NavigationDelegateTest.kt
uriloader/base/nsDocLoader.cpp
--- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/NavigationDelegateTest.kt
+++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/NavigationDelegateTest.kt
@@ -200,17 +200,16 @@ class NavigationDelegateTest : BaseSessi
                 object : Callbacks.TrackingProtectionDelegate {
             @AssertCalled(false)
             override fun onTrackerBlocked(session: GeckoSession, uri: String,
                                           categories: Int) {
             }
         })
     }
 
-    @Ignore
     @Test fun redirectLoad() {
         val redirectUri = if (sessionRule.env.isAutomation) {
             "http://example.org/tests/robocop/robocop_blank_02.html"
         } else {
             "http://jigsaw.w3.org/HTTP/300/Overview.html"
         }
         val uri = if (sessionRule.env.isAutomation) {
             "http://example.org/tests/robocop/simple_redirect.sjs?$redirectUri"
--- a/uriloader/base/nsDocLoader.cpp
+++ b/uriloader/base/nsDocLoader.cpp
@@ -29,16 +29,18 @@
 #include "nsIScriptSecurityManager.h"
 
 #include "nsITransport.h"
 #include "nsISocketTransport.h"
 #include "nsIDocShell.h"
 #include "nsIDocument.h"
 #include "nsPresContext.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
+#include "nsILoadURIDelegate.h"
+#include "nsIBrowserDOMWindow.h"
 
 using mozilla::dom::ContentBlockingLog;
 using mozilla::DebugOnly;
 using mozilla::LogLevel;
 
 //
 // Log module for nsIDocumentLoader logging...
 //
@@ -1419,16 +1421,46 @@ int64_t nsDocLoader::CalculateMaxProgres
   return max;
 }
 
 NS_IMETHODIMP nsDocLoader::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
                                                   nsIChannel *aNewChannel,
                                                   uint32_t aFlags,
                                                   nsIAsyncVerifyRedirectCallback *cb)
 {
+  if (aFlags &
+      (nsIChannelEventSink::REDIRECT_TEMPORARY |
+       nsIChannelEventSink::REDIRECT_PERMANENT)) {
+    nsCOMPtr<nsIDocShell> docShell =
+      do_QueryInterface(static_cast<nsIRequestObserver*>(this));
+
+    nsCOMPtr<nsILoadURIDelegate> delegate;
+    if (docShell) {
+      docShell->GetLoadURIDelegate(getter_AddRefs(delegate));
+    }
+
+    nsCOMPtr<nsIURI> newURI;
+    if (delegate) {
+      // No point in getting the URI if we don't have a LoadURIDelegate.
+      aNewChannel->GetURI(getter_AddRefs(newURI));
+    }
+
+    if (newURI) {
+      const int where = nsIBrowserDOMWindow::OPEN_CURRENTWINDOW;
+      bool loadURIHandled = false;
+      nsresult rv = delegate->LoadURI(newURI, where, /* flags */ 0,
+                                      /* triggering principal */ nullptr,
+                                      &loadURIHandled);
+      if (NS_SUCCEEDED(rv) && loadURIHandled) {
+        cb->OnRedirectVerifyCallback(NS_OK);
+        return NS_OK;
+      }
+    }
+  }
+
   if (aOldChannel)
   {
     nsLoadFlags loadFlags = 0;
     int32_t stateFlags = nsIWebProgressListener::STATE_REDIRECTING |
                          nsIWebProgressListener::STATE_IS_REQUEST;
 
     aOldChannel->GetLoadFlags(&loadFlags);
     // If the document channel is being redirected, then indicate that the