nsCOMPtr<nsIDocShellTreeNode> rootNode(do_QueryInterface(root));
if (depth >= MAX_DEPTH_CONTENT_FRAMES) { NS_WARNING("Too many nested content frames so giving up");
nsFrameLoader::EnsureDocShell(){  if (mDocShell) {    return NS_OK;  }  nsCOMPtr<nsIPresContext> presContext;  GetPresContext(getter_AddRefs(presContext));  NS_ENSURE_TRUE(presContext, NS_ERROR_UNEXPECTED);  nsCOMPtr<nsISupports> parentAsSupports;  presContext->GetContainer(getter_AddRefs(parentAsSupports));  // bug98158:count the children under the root docshell.  // if the total number of children under the root docshell  // beyond the limit,return a error.  if (parentAsSupports) {    nsCOMPtr<nsIDocShellTreeItem> parentAsItem = do_QueryInterface(parentAsSupports);    nsCOMPtr<nsIDocShellTreeItem> root;    parentAsItem->GetSameTypeRootTreeItem(getter_AddRefs(root));    nsCOMPtr<nsIDocShellTreeNode> rootNode(do_QueryInterface(root));    PRInt32  childrenCount;    childrenCount = GetDocShellChildCount(rootNode);    if(childrenCount >= MAX_NUMBER_DOCSHELLS) {      NS_WARNING("Too many docshell (recursion?) so giving up");      return NS_ERROR_FAILURE;    }  }  // Create the docshell...  mDocShell = do_CreateInstance("@mozilla.org/webshell;1");  NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);  // Get the frame name and tell the docshell about it.  nsCOMPtr<nsIDocShellTreeItem> docShellAsItem(do_QueryInterface(mDocShell));  NS_ENSURE_TRUE(docShellAsItem, NS_ERROR_FAILURE);  nsAutoString frameName;  mOwnerContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::name, frameName);  if (!frameName.IsEmpty()) {    docShellAsItem->SetName(frameName.get());  }  // If our container is a web-shell, inform it that it has a new  // child. If it's not a web-shell then some things will not operate  // properly.  nsCOMPtr<nsISupports> container;  presContext->GetContainer(getter_AddRefs(container));  nsCOMPtr<nsIDocShellTreeNode> parentAsNode(do_QueryInterface(container));  if (parentAsNode) {    nsCOMPtr<nsIDocShellTreeItem> parentAsItem =      do_QueryInterface(parentAsNode);    PRInt32 parentType;    parentAsItem->GetItemType(&parentType);    nsAutoString value;    PRBool isContent = PR_FALSE;    if (mOwnerContent->IsContentOfType(nsIContent::eXUL)) {      mOwnerContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::type, value);    }    // we accept "content" and "content-xxx" values.    // at time of writing, we expect "xxx" to be "primary", but    // someday it might be an integer expressing priority    if (value.Length() >= 7) {      // Lowercase the value, ContentShellAdded() further down relies      // on it being lowercased.      ToLowerCase(value);      nsAutoString::const_char_iterator start, end;      value.BeginReading(start);      value.EndReading(end);      nsAutoString::const_char_iterator iter(start + 7);      isContent = Substring(start, iter) == NS_LITERAL_STRING("content") &&                  (iter == end || *iter == '-');    }    if (isContent) {      // The web shell's type is content.      docShellAsItem->SetItemType(nsIDocShellTreeItem::typeContent);    } else {      // Inherit our type from our parent webshell.  If it is      // chrome, we'll be chrome.  If it is content, we'll be      // content.      docShellAsItem->SetItemType(parentType);    }    parentAsNode->AddChild(docShellAsItem);    if (isContent) {      nsCOMPtr<nsIDocShellTreeOwner> parentTreeOwner;      parentAsItem->GetTreeOwner(getter_AddRefs(parentTreeOwner));      if(parentTreeOwner) {        PRBool is_primary = parentType == nsIDocShellTreeItem::typeChrome &&                            value == NS_LITERAL_STRING("content-primary");        parentTreeOwner->ContentShellAdded(docShellAsItem, is_primary,                                           value.get());      }    }    // connect the container...    nsCOMPtr<nsIWebShell> webShell(do_QueryInterface(mDocShell));    nsCOMPtr<nsIWebShellContainer> outerContainer =      do_QueryInterface(container);    if (outerContainer) {      webShell->SetContainer(outerContainer);    }    // Make sure all shells have links back to the content element    // in the nearest enclosing chrome shell.    nsCOMPtr<nsIChromeEventHandler> chromeEventHandler;    if (parentType == nsIDocShellTreeItem::typeChrome) {      // Our parent shell is a chrome shell. It is therefore our nearest      // enclosing chrome shell.      chromeEventHandler = do_QueryInterface(mOwnerContent);      NS_WARN_IF_FALSE(chromeEventHandler,                       "This mContent should implement this.");    } else {      nsCOMPtr<nsIDocShell> parentShell(do_QueryInterface(parentAsNode));      // Our parent shell is a content shell. Get the chrome event      // handler from it and use that for our shell as well.      parentShell->GetChromeEventHandler(getter_AddRefs(chromeEventHandler));    }    mDocShell->SetChromeEventHandler(chromeEventHandler);  }  // This is nasty, this code (the do_GetInterface(mDocShell) below)  // *must* come *after* the above call to  // mDocShell->SetChromeEventHandler() for the global window to get  // the right chrome event handler.  // Tell the window about the frame that hosts it.  nsCOMPtr<nsIDOMElement> frame_element(do_QueryInterface(mOwnerContent));  NS_ASSERTION(frame_element, "frame loader owner element not a DOM element!");  nsCOMPtr<nsIDOMWindow> win(do_GetInterface(mDocShell));  nsCOMPtr<nsPIDOMWindow> win_private(do_QueryInterface(win));  NS_ENSURE_TRUE(win_private, NS_ERROR_UNEXPECTED);  win_private->SetFrameElementInternal(frame_element);  nsCOMPtr<nsIBaseWindow> base_win(do_QueryInterface(mDocShell));  NS_ENSURE_TRUE(base_win, NS_ERROR_UNEXPECTED);  // This is kinda whacky, this call doesn't really create anything,  // but it must be called to make sure things are properly  // initialized  base_win->Create();  return NS_OK;}