if (NS_FAILED(rv)) { nsCOMPtr<nsIDOMWindowInternal> domWindow (do_GetInterface(originalWindowContext)); return InvokeUnknownContentHandler(aChannel, contentType, domWindow); }
nsresult nsDocumentOpenInfo::DispatchContent(nsIChannel * aChannel, nsISupports * aCtxt){  nsresult rv;  nsXPIDLCString contentType;  nsCOMPtr<nsISupports> originalWindowContext = m_originalContext; // local variable to keep track of this.  nsCOMPtr<nsIStreamListener> contentStreamListener;  rv = aChannel->GetContentType(getter_Copies(contentType));  if (NS_FAILED(rv)) return rv;   // go to the uri dispatcher and give them our stuff...  NS_WITH_SERVICE(nsIURILoader, pURILoader, kURILoaderCID, &rv);  if (NS_SUCCEEDED(rv))  {    nsCOMPtr<nsIURIContentListener> contentListener;    nsXPIDLCString desiredContentType;    //    // First step:  See if any nsIURIContentListener prefers to handle this    //              content type.    //    PRBool abortDispatch = PR_FALSE;    rv = pURILoader->DispatchContent(contentType, mCommand, m_windowTarget,                                      aChannel, aCtxt,                                      m_contentListener,                                      m_originalContext,                                     getter_Copies(desiredContentType),                                      getter_AddRefs(contentListener),                                     &abortDispatch);      // if the uri loader says to abort the dispatch then someone    // else must have stepped in and taken over for us...so stop..    if (abortDispatch) return NS_OK;    //    // Second step:  If no listener prefers this type, see if any stream    //               decoders exist to transform this content type into    //               some other.    //    if (!contentListener)     {      rv = RetargetOutput(aChannel, contentType, "*/*", nsnull);      if (m_targetStreamListener)        return NS_OK;    }        //    // Step 3:    //    // BIG TIME HACK ALERT!!!!! WE NEED THIS HACK IN PLACE UNTIL OUR NEW UNKNOWN CONTENT    // HANDLER COMES ONLINE!!!     // Until that day, if we couldn't find a handler for the content type, then go back to the listener who    // originated the url request and force them to handle the content....this forces us through the old code    // path for unknown content types which brings up the file save as dialog...    if (!contentListener) {      contentListener = m_contentListener;    }    //    // Good news!  Some content listener can handle this content type.    //    if (contentListener)    {      PRBool bAbortProcess = PR_FALSE;           nsCAutoString contentTypeToUse;      if (desiredContentType)        contentTypeToUse.Assign(desiredContentType);      else        contentTypeToUse.Assign(contentType);      // We need to first figure out if we are retargeting the load to a content listener      // that is different from the one that originated the request....if so, set      // LOAD_RETARGETED_DOCUMENT_URI on the channel.       if (contentListener.get() != m_contentListener.get())      {         // we must be retargeting...so set an appropriate flag on the channel        nsLoadFlags loadAttribs = 0;        aChannel->GetLoadAttributes(&loadAttribs);        loadAttribs |= nsIChannel::LOAD_RETARGETED_DOCUMENT_URI;        aChannel->SetLoadAttributes(loadAttribs);      }      rv = contentListener->DoContent(contentTypeToUse, mCommand, m_windowTarget,                                     aChannel, getter_AddRefs(contentStreamListener),                                    &bAbortProcess);      // the listener is doing all the work from here...we are done!!!      if (bAbortProcess) return rv;      // BEFORE we fail and bring up the unknown content handler dialog...      // try to detect if there is a helper application we an use instead...      if (/* mCommand == nsIURILoader::viewUserClick && */ !contentStreamListener)      {        nsCOMPtr<nsIURI> uri;        PRBool abortProcess = PR_FALSE;        aChannel->GetURI(getter_AddRefs(uri));        nsCOMPtr<nsIStreamListener> contentStreamListener;        nsCOMPtr<nsIExternalHelperAppService> helperAppService (do_GetService(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID));        if (helperAppService)        {            rv = helperAppService->DoContent(contentType, uri, m_originalContext, &abortProcess, getter_AddRefs(contentStreamListener));            if (NS_SUCCEEDED(rv) && contentStreamListener)              return RetargetOutput(aChannel, contentType, contentType, contentStreamListener);        }        rv = NS_ERROR_FAILURE; // this will cause us to bring up the unknown content handler dialog.      }      if (NS_FAILED(rv))      {        nsCOMPtr<nsIDOMWindowInternal> domWindow (do_GetInterface(originalWindowContext));        return InvokeUnknownContentHandler(aChannel, contentType, domWindow);      }      // okay, all registered listeners have had a chance to handle this content...      // did one of them give us a stream listener back? if so, let's start reading data      // into it...      rv = RetargetOutput(aChannel, contentType, desiredContentType, contentStreamListener);      m_originalContext = nsnull; // we don't need this anymore....    }   }  return rv;}