nsCOMPtr<nsIWebShell> kungFuDeathGrip(this);
nsWebShell::OnEndDocumentLoad(nsIDocumentLoader* loader,                              nsIChannel* channel,                              nsresult aStatus,                              nsIDocumentLoaderObserver * aWebShell){#ifdef MOZ_PERF_METRICS  MOZ_TIMER_DEBUGLOG(("Stop: nsWebShell::OnEndDocumentLoad(), this=%p\n", this));  MOZ_TIMER_STOP(mTotalTime);  MOZ_TIMER_LOG(("Total (Layout + Page Load) Time (webshell=%p): ", this));  MOZ_TIMER_PRINT(mTotalTime);#endif  nsresult rv = NS_ERROR_FAILURE;  if (!channel) {    return NS_ERROR_NULL_POINTER;  }  nsCOMPtr<nsIURI> aURL;  rv = channel->GetURI(getter_AddRefs(aURL));  if (NS_FAILED(rv)) return rv;//if (!mProcessedEndDocumentLoad) {  if (loader == mDocLoader) {    mProcessedEndDocumentLoad = PR_TRUE;    if (nsnull != mScriptGlobal) {      nsIDocumentViewer* docViewer;      if (nsnull != mContentViewer &&          NS_OK == mContentViewer->QueryInterface(kIDocumentViewerIID, (void**)&docViewer)) {        nsIPresContext *presContext;        if (NS_OK == docViewer->GetPresContext(presContext)) {          nsEventStatus status = nsEventStatus_eIgnore;          nsMouseEvent event;          event.eventStructType = NS_EVENT;          event.message = NS_PAGE_LOAD;          rv = mScriptGlobal->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status);          NS_RELEASE(presContext);        }        NS_RELEASE(docViewer);      }    }    // Fire the EndLoadURL of the web shell container    if (nsnull != aURL) {       nsAutoString urlString;       char* spec;       rv = aURL->GetSpec(&spec);       if (NS_SUCCEEDED(rv)) {         urlString = spec;         if (nsnull != mContainer) {            rv = mContainer->EndLoadURL(this, urlString.GetUnicode(), 0);         }         nsCRT::free(spec);       }    }    nsCOMPtr<nsIDocumentLoaderObserver> dlObserver;    if (!mDocLoaderObserver && mParent) {      /* If this is a frame (in which case it would have a parent && doesn't       * have a documentloaderObserver, get it from the rootWebShell       */      nsCOMPtr<nsIWebShell> root;      nsresult res = GetRootWebShell(*getter_AddRefs(root));      if (NS_SUCCEEDED(res) && root)        root->GetDocLoaderObserver(getter_AddRefs(dlObserver));    }    else    {      /* Take care of the Trailing slash situation */      if (mSHist)        CheckForTrailingSlash(aURL);      dlObserver = do_QueryInterface(mDocLoaderObserver);  // we need this to addref    }    /*     * Fire the OnEndDocumentLoad of the DocLoaderobserver     */    if (dlObserver && (nsnull != aURL)) {       dlObserver->OnEndDocumentLoad(mDocLoader, channel, aStatus, aWebShell);    }    if ( (mDocLoader == loader) && (aStatus == NS_ERROR_UNKNOWN_HOST) ) {        // KEYWORDS        // if a lookup failed, check to see if it was non-qualified.        // if it was, kick it to the keyword server. the keyword server        // now handles the www.*.com trick.        char *host = nsnull;        rv = aURL->GetHost(&host);        if (NS_FAILED(rv)) return rv;        if(!host) return NS_ERROR_OUT_OF_MEMORY;                char *tmp = host;        while (*tmp && (*tmp != '.')) tmp++;        if (!*tmp) {            nsAutoString keywordSpec("keyword:");            keywordSpec.Append(host);            rv = LoadURL(keywordSpec.GetUnicode(), "view");            if (NS_FAILED(rv)) {                // if we couldn't load the keyword munged URL do                // our own www.*.com trick.                nsAllocator::Free(host);                nsCAutoString hostStr;                rv = aURL->GetHost(&host);                if (NS_FAILED(rv)) return rv;                     hostStr.SetString(host);                PRInt32 dotLoc = -1;                dotLoc = hostStr.FindChar('.');                PRBool retry = PR_FALSE;                if (-1 == dotLoc) {                    hostStr.Insert("www.", 0, 4);                    hostStr.Append(".com");                    retry = PR_TRUE;                } else if ( (hostStr.Length() - dotLoc) == 3) {                    hostStr.Insert("www.", 0, 4);                    retry = PR_TRUE;                }                    if (retry) {                    rv = aURL->SetHost(hostStr.GetBuffer());                    if (NS_FAILED(rv)) return rv;                    char *aSpec = nsnull;                    rv = aURL->GetSpec(&aSpec);                    if (NS_FAILED(rv)) return rv;                    nsAutoString newURL(aSpec);                    nsAllocator::Free(aSpec);                    // reload the url                    rv = LoadURL(newURL.GetUnicode(), "view");                } // retry            } // end keyword load failure        } // end !*tmp        nsAllocator::Free(host);    } // unknown host  } //!mProcessedEndDocumentLoad  else {    rv = NS_OK;  }  return rv;}