nsCOMPtr<nsIScriptGlobalObject> global = do_GetInterface(container); if (global) { mDocument->SetScriptGlobalObject(global); global->SetNewDocument(aDocument, PR_TRUE, PR_TRUE);
nsCOMPtr<nsIScriptGlobalObject> global = do_GetInterface(container); if (global) { global->SetNewDocument(aDocument, PR_TRUE, PR_TRUE); } }
DocumentViewerImpl::SetDOMDocument(nsIDOMDocument *aDocument){  // Assumptions:  //  // 1) this document viewer has been initialized with a call to Init().  // 2) the stylesheets associated with the document have been added  // to the document.  // XXX Right now, this method assumes that the layout of the current  // document hasn't started yet.  More cleanup will probably be  // necessary to make this method work for the case when layout *has*  // occurred for the current document.  // That work can happen when and if it is needed.  nsresult rv;  if (!aDocument)    return NS_ERROR_NULL_POINTER;  nsCOMPtr<nsIDocument> newDoc = do_QueryInterface(aDocument, &rv);  if (NS_FAILED(rv)) return rv;  // Set new container  nsCOMPtr<nsISupports> container = do_QueryReferent(mContainer);  newDoc->SetContainer(container);  // Replace the old document with the new one  mDocument = newDoc;  // Set the script global object on the new document  nsCOMPtr<nsIScriptGlobalObject> global = do_GetInterface(container);  if (global) {    mDocument->SetScriptGlobalObject(global);    global->SetNewDocument(aDocument, PR_TRUE, PR_TRUE);    rv = SyncParentSubDocMap();    NS_ENSURE_SUCCESS(rv, rv);  }  // Replace the current pres shell with a new shell for the new document  nsCOMPtr<nsILinkHandler> linkHandler;  if (mPresShell) {    if (mPresContext) {      // Save the linkhandler (nsPresShell::Destroy removes it from      // mPresContext).      linkHandler = mPresContext->GetLinkHandler();    }    mPresShell->EndObservingDocument();    mPresShell->Destroy();    mPresShell = nsnull;  }  // And if we're already given a prescontext...  if (mPresContext) {    // If we had a linkHandler and it got removed, put it back.    if (linkHandler) {      mPresContext->SetLinkHandler(linkHandler);    }    // Create a new style set for the document    nsStyleSet *styleSet;    rv = CreateStyleSet(mDocument, &styleSet);    NS_ENSURE_SUCCESS(rv, rv);    rv = newDoc->CreateShell(mPresContext, mViewManager, styleSet,                             getter_AddRefs(mPresShell));    if (NS_FAILED(rv)) {      delete styleSet;      return rv;    }    // We're done creating the style set    styleSet->EndUpdate();    // The pres shell owns the style set now.    mPresShell->BeginObservingDocument();    // Register the focus listener on the new document    nsCOMPtr<nsIDOMEventReceiver> erP = do_QueryInterface(mDocument, &rv);    NS_WARN_IF_FALSE(erP, "No event receiver in document!");    if (erP) {      rv = erP->AddEventListenerByIID(mFocusListener,                                      NS_GET_IID(nsIDOMFocusListener));      NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener");    }  }  return rv;}