curDoc = parentDoc;
static void CheckForFocus(nsPIDOMWindow* aOurWindow,                          nsIFocusController* aFocusController,                          nsIDocument* aDocument){  // Now that we have a root frame, we can set focus on the presshell.  // We do this only if our DOM window is currently focused or is an  // an ancestor of a previously focused window.  if (!aFocusController)    return;  nsCOMPtr<nsIDOMWindowInternal> ourWin = do_QueryInterface(aOurWindow);  nsCOMPtr<nsIDOMWindowInternal> focusedWindow;  aFocusController->GetFocusedWindow(getter_AddRefs(focusedWindow));  if (!focusedWindow) {    // This should never really happen, but if it does, assume    // we can focus ourself to keep the window from being keydead.    focusedWindow = ourWin;  }  // Walk up the document chain, starting with focusedWindow's document.  // We stop walking when we find a document that has a null DOMWindow  // (meaning that the DOMWindow has a new document now) or find ourWin  // as the document's window.  We also stop if we hit aDocument, since  // that means there is a child document which loaded before us that's  // already been given focus.  nsCOMPtr<nsIDOMDocument> focusedDOMDoc;  focusedWindow->GetDocument(getter_AddRefs(focusedDOMDoc));  nsCOMPtr<nsIDocument> curDoc = do_QueryInterface(focusedDOMDoc);  if (!curDoc) {    // This can happen if the previously focused DOM window has been    // unhooked from its document during document teardown.  We don't    // really have any other information to help us determine where    // focusedWindow fits into the DOM window hierarchy.  For now, we'll    // go ahead and allow this window to take focus, so that something    // ends up focused.    curDoc = aDocument;  }  while (curDoc) {    nsCOMPtr<nsIScriptGlobalObject> globalObject;    curDoc->GetScriptGlobalObject(getter_AddRefs(globalObject));    nsCOMPtr<nsIDOMWindowInternal> curWin = do_QueryInterface(globalObject);    if (curWin == ourWin || !curWin)      break;    nsCOMPtr<nsIDocument> parentDoc;    curDoc->GetParentDocument(getter_AddRefs(parentDoc));    if (parentDoc == aDocument)      return;    curDoc = parentDoc;  }  if (!curDoc) {    // We reached the top of the document chain, and did not encounter ourWin    // or a windowless document. So, focus should be unaffected by this    // document load.    return;  }  PRBool active;  aFocusController->GetActive(&active);  if (active)    ourWin->Focus();  // We need to ensure that the focus controller is updated, since it may be  // suppressed when this function is called.  aFocusController->SetFocusedWindow(ourWin);}