if (!global) {
if (!window) {
DocumentViewerImpl::PermitUnload(PRBool *aPermitUnload){  *aPermitUnload = PR_TRUE;  if (!mDocument || mInPermitUnload) {    return NS_OK;  }  // First, get the script global object from the document...  nsIScriptGlobalObject *global = mDocument->GetScriptGlobalObject();  if (!global) {    // This is odd, but not fatal    NS_WARNING("nsIScriptGlobalObject not set for document!");    return NS_OK;  }  // Now, fire an BeforeUnload event to the document and see if it's ok  // to unload...  nsEventStatus status = nsEventStatus_eIgnore;  nsBeforePageUnloadEvent event(PR_TRUE, NS_BEFORE_PAGE_UNLOAD);  nsresult rv = NS_OK;  {    // Never permit popups from the beforeunload handler, no matter    // how we get here.    nsAutoPopupStatePusher popupStatePusher(openAbused, PR_TRUE);    // In evil cases we might be destroyed while handling the    // onbeforeunload event, don't let that happen.    nsRefPtr<DocumentViewerImpl> kungFuDeathGrip(this);    mInPermitUnload = PR_TRUE;    rv = global->HandleDOMEvent(mPresContext, &event, nsnull,                                NS_EVENT_FLAG_INIT, &status);    mInPermitUnload = PR_FALSE;  }  nsCOMPtr<nsIDocShellTreeNode> docShellNode(do_QueryReferent(mContainer));  if (NS_SUCCEEDED(rv) && (event.flags & NS_EVENT_FLAG_NO_DEFAULT ||                           !event.text.IsEmpty())) {    // Ask the user if it's ok to unload the current page    nsCOMPtr<nsIPrompt> prompt = do_GetInterface(docShellNode);    if (prompt) {      nsXPIDLString preMsg, postMsg;      rv = nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,                                              "OnBeforeUnloadPreMessage",                                              preMsg);      rv |= nsContentUtils::GetLocalizedString(nsContentUtils::eDOM_PROPERTIES,                                               "OnBeforeUnloadPostMessage",                                               postMsg);      // GetStringFromName can succeed, yet give NULL strings back.      if (NS_FAILED(rv) || preMsg.IsEmpty() || postMsg.IsEmpty()) {        NS_ERROR("Failed to get strings from dom.properties!");        return NS_OK;      }      // Limit the length of the text the page can inject into this      // dialogue to 1024 characters.      PRInt32 len = PR_MIN(event.text.Length(), 1024);      nsAutoString msg(preMsg + NS_LITERAL_STRING("\n\n") +                       StringHead(event.text, len) +                       NS_LITERAL_STRING("\n\n") + postMsg);      // This doesn't pass a title, which makes the title be      // "Confirm", is that ok, or do we want a localizable title for      // this dialogue?      if (NS_FAILED(prompt->Confirm(nsnull, msg.get(), aPermitUnload))) {        *aPermitUnload = PR_TRUE;      }    }  }  if (docShellNode) {    PRInt32 childCount;    docShellNode->GetChildCount(&childCount);    for (PRInt32 i = 0; i < childCount && *aPermitUnload; ++i) {      nsCOMPtr<nsIDocShellTreeItem> item;      docShellNode->GetChildAt(i, getter_AddRefs(item));      nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(item));      nsCOMPtr<nsIContentViewer> cv;      docShell->GetContentViewer(getter_AddRefs(cv));      if (cv) {        cv->PermitUnload(aPermitUnload);      }    }  }  return NS_OK;}