if (NS_SUCCEEDED(rv)) {
if (NS_SUCCEEDED(rv) && !aWindowTarget) {
nsWebShell::DoLoadURL(nsIURI * aUri,                      const char* aCommand,                      nsIInputStream* aPostDataStream,                      nsLoadFlags aType,                      const PRUint32 aLocalIP,                      const PRUnichar* aReferrer,                      const char * aWindowTarget,                      PRBool aKickOffLoad){  if (!aUri)    return NS_ERROR_NULL_POINTER;  nsXPIDLCString urlSpec;  nsresult rv = NS_OK;  rv = aUri->GetSpec(getter_Copies(urlSpec));  if (NS_FAILED(rv)) return rv;  // If it's a normal reload that uses the cache, look at the destination anchor  // and see if it's an element within the current document  // We don't have a reload loadtype yet in necko. So, check for just history  // loadtype  if ((aType == nsISessionHistory::LOAD_HISTORY || aType == nsIChannel::LOAD_NORMAL) && (nsnull != mContentViewer) &&      (nsnull == aPostDataStream))   {    nsCOMPtr<nsIDocumentViewer> docViewer;    if (NS_SUCCEEDED(mContentViewer->QueryInterface(kIDocumentViewerIID,                                                    getter_AddRefs(docViewer))))      {      // Get the document object      nsCOMPtr<nsIDocument> doc;      docViewer->GetDocument(*getter_AddRefs(doc));      // Get the URL for the document      nsCOMPtr<nsIURI>  docURL = getter_AddRefs(doc->GetDocumentURL());      if (aUri && docURL && EqualBaseURLs(docURL, aUri))         {         // See if there's a destination anchor         nsXPIDLCString ref;         nsCOMPtr<nsIURL> aUrl = do_QueryInterface(aUri);         if (aUrl)            rv = aUrl->GetRef(getter_Copies(ref));         nsCOMPtr<nsIPresShell> presShell;         rv = docViewer->GetPresShell(*getter_AddRefs(presShell));         if (NS_SUCCEEDED(rv) && presShell)            {            /* Pass OnStartDocument notifications to the docloaderobserver            * so that urlbar, forward/back buttons will            * behave properly when going to named anchors            */            nsIInterfaceRequestor * interfaceRequestor = NS_STATIC_CAST(nsIInterfaceRequestor *, this);            nsCOMPtr<nsIChannel> dummyChannel;            // creating a channel is expensive...don't create it unless we know we have to            // so move the creation down into each of the if clauses...            if (nsnull != (const char *) ref)               {               rv = NS_OpenURI(getter_AddRefs(dummyChannel), aUri, nsnull, interfaceRequestor);               if (NS_FAILED(rv)) return rv;               if (!mProcessedEndDocumentLoad)                  rv = OnStartDocumentLoad(mDocLoader, aUri, "load");               // Go to the anchor in the current document               rv = presShell->GoToAnchor(nsAutoString(ref));                                     // Set the URL & referrer if the anchor was successfully visited               if (NS_SUCCEEDED(rv))                  {                  SetCurrentURI(aUri);                  SetReferrer(aReferrer);                  }               // Pass on status of scrolling/anchor visit to docloaderobserver               if (!mProcessedEndDocumentLoad)                  {                  rv = OnEndDocumentLoad(mDocLoader, dummyChannel, rv);                  }               return rv;                      }            else if (aType == nsISessionHistory::LOAD_HISTORY)               {               rv = NS_OpenURI(getter_AddRefs(dummyChannel), aUri, nsnull, interfaceRequestor);               if (NS_FAILED(rv)) return rv;               rv = OnStartDocumentLoad(mDocLoader, aUri, "load");               // Go to the top of the current document               nsCOMPtr<nsIViewManager> viewMgr;               rv = presShell->GetViewManager(getter_AddRefs(viewMgr));               if (NS_SUCCEEDED(rv) && viewMgr)                  {                  nsIScrollableView* view;                  rv = viewMgr->GetRootScrollableView(&view);                  if (NS_SUCCEEDED(rv) && view)                  rv = view->ScrollTo(0, 0, NS_VMREFRESH_IMMEDIATE);                  if(NS_SUCCEEDED(rv))                     {                     SetCurrentURI(aUri);                     SetReferrer(aReferrer);                     }                  mProcessedEndDocumentLoad = PR_FALSE;                  // Pass on status of scrolling/anchor visit to docloaderobserver                  rv = OnEndDocumentLoad(mDocLoader, dummyChannel, rv);                  return rv;                         }               }#if 0                          mProcessedEndDocumentLoad = PR_FALSE;            // Pass on status of scrolling/anchor visit to docloaderobserver            rv = OnEndDocumentLoad(mDocLoader, dummyChannel, rv);            return rv;      #endif /* 0 */            }  // NS_SUCCEEDED(rv) && presShell         } // EqualBaseURLs(docURL, url)      }   }  // Stop loading the current document (if any...).  This call may result in  // firing an EndLoadURL notification for the old document...  if (aKickOffLoad)    StopLoad();    // Tell web-shell-container we are loading a new url  if (nsnull != mContainer) {    nsAutoString uniSpec (urlSpec);    rv = mContainer->BeginLoadURL(this, uniSpec.GetUnicode());    if (NS_FAILED(rv)) {      return rv;    }  }  mProcessedEndDocumentLoad = PR_FALSE; /* WebShell was primarily passing the buck when it came to streamObserver.  * So, pass on the observer which is already a streamObserver to DocLoder.  */  if (aKickOffLoad) {#ifdef MOZ_PERF_METRICS    {       char* url;       nsresult rv = NS_OK;       rv = aUri->GetSpec(&url);       if (NS_SUCCEEDED(rv)) {         MOZ_TIMER_LOG(("*** Timing layout processes on url: '%s', webshell: %p\n", url, this));         delete [] url;       }    }      MOZ_TIMER_DEBUGLOG(("Reset and start: nsWebShell::DoLoadURL(), this=%p\n", this));    MOZ_TIMER_RESET(mTotalTime);    MOZ_TIMER_START(mTotalTime);#endif    nsCOMPtr<nsIURILoader> pURILoader = do_GetService(NS_URI_LOADER_PROGID, &rv);    if (NS_FAILED(rv)) return rv;    nsXPIDLCString aUrlScheme;    aUri->GetScheme(getter_Copies(aUrlScheme));    nsCOMPtr<nsILoadGroup> loadGroup;    pURILoader->GetLoadGroupForContext(NS_STATIC_CAST(nsISupports *, (nsIWebShell *) this), getter_AddRefs(loadGroup));    // first, create a channel for the protocol....    nsCOMPtr<nsIIOService> pNetService = do_GetService(kIOServiceCID, &rv);    if (NS_SUCCEEDED(rv)) {      nsCOMPtr<nsIChannel> pChannel;      nsCOMPtr<nsIInterfaceRequestor> requestor (do_QueryInterface(NS_STATIC_CAST(nsIContentViewerContainer*, (nsIWebShell*)this)));      // Create a referrer URI      nsCOMPtr<nsIURI> referrer;      if (aReferrer) {        nsAutoString tempReferrer(aReferrer);        char* referrerStr = tempReferrer.ToNewCString();        pNetService->NewURI(referrerStr, nsnull, getter_AddRefs(referrer));        Recycle(referrerStr);      }      rv = pNetService->NewChannelFromURI(aCommand, aUri, loadGroup, requestor,                                          aType, referrer /* referring uri */,                                           0, 0,                                          getter_AddRefs(pChannel));      if (NS_FAILED(rv)) {      if (rv == NS_ERROR_DOM_RETVAL_UNDEFINED) // if causing the channel changed the         return NS_OK;                        // dom and there is nothing else to do      else          return rv; // uhoh we were unable to get a channel to handle the url!!!    }      // Mark the channel as being a document URI...      nsLoadFlags loadAttribs = 0;      pChannel->GetLoadAttributes(&loadAttribs);      loadAttribs |= nsIChannel::LOAD_DOCUMENT_URI;      pChannel->SetLoadAttributes(loadAttribs);            nsCOMPtr<nsIHTTPChannel> httpChannel(do_QueryInterface(pChannel));      if (httpChannel)      {          // figure out if we need to set the post data stream on the channel...          // right now, this is only done for http channels.....          if (aPostDataStream)          {            httpChannel->SetRequestMethod(HM_POST);            httpChannel->SetPostDataStream(aPostDataStream);          }          // Set the referrer explicitly          if (referrer)          {              // Referrer is currenly only set for link clicks here.               httpChannel->SetReferrer(referrer,                       nsIHTTPChannel::REFERRER_LINK_CLICK);          }      }      // now let's pass the channel into the uri loader      nsURILoadCommand loadCmd = nsIURILoader::viewNormal;      if (nsCRT::strcasecmp(aCommand, "view-link-click") == 0)        loadCmd = nsIURILoader::viewUserClick;      else if (nsCRT::strcasecmp(aCommand, "view-source") == 0)        loadCmd = nsIURILoader::viewSource;      rv = pURILoader->OpenURI(pChannel, loadCmd, aWindowTarget /* window target */,                                NS_STATIC_CAST(nsIContentViewerContainer*, (nsIWebShell*)this));    }  }  // Fix for bug 1646.  Change the notion of current url and referrer only after  // the document load succeeds.  if (NS_SUCCEEDED(rv)) {    SetCurrentURI(aUri);    SetReferrer(aReferrer);  }  return rv;}