SetScrolling(-1, PR_FALSE);
InitFrameData(PR_FALSE);
NS_IMETHODIMP nsWebShell::SetDocument(nsIDOMDocument *aDOMDoc,    nsIDOMElement *aRootNode){  // The tricky part is bypassing the normal load process and just putting a document into  // the webshell.  This is particularly nasty, since webshells don't normally even know  // about their documents  // (1) Create a document viewer   nsCOMPtr<nsIContentViewer> documentViewer;  nsCOMPtr<nsIDocumentLoaderFactory> docFactory;  static NS_DEFINE_CID(kLayoutDocumentLoaderFactoryCID, NS_LAYOUT_DOCUMENT_LOADER_FACTORY_CID);  NS_ENSURE_SUCCESS(nsComponentManager::CreateInstance(kLayoutDocumentLoaderFactoryCID, nsnull,                                                        NS_GET_IID(nsIDocumentLoaderFactory),                                                       (void**)getter_AddRefs(docFactory)),                    NS_ERROR_FAILURE);  nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDOMDoc);  if (!doc) { return NS_ERROR_NULL_POINTER; }  NS_ENSURE_SUCCESS(docFactory->CreateInstanceForDocument(NS_STATIC_CAST(nsIContentViewerContainer*, (nsIWebShell*)this),                                                          doc,                                                          "view",                                                          getter_AddRefs(documentViewer)),                    NS_ERROR_FAILURE);   // (2) Feed the webshell to the content viewer  NS_ENSURE_SUCCESS(documentViewer->SetContainer((nsIWebShell*)this), NS_ERROR_FAILURE);  // (3) Tell the content viewer container to embed the content viewer.  //     (This step causes everything to be set up for an initial flow.)  NS_ENSURE_SUCCESS(Embed(documentViewer, "view", nsnull), NS_ERROR_FAILURE);  // XXX: It would be great to get rid of this dummy channel!  const nsAutoString uriString = "about:blank";  nsCOMPtr<nsIURI> uri;  NS_ENSURE_SUCCESS(NS_NewURI(getter_AddRefs(uri), uriString), NS_ERROR_FAILURE);  if (!uri) { return NS_ERROR_OUT_OF_MEMORY; }  nsCOMPtr<nsIChannel> dummyChannel;  NS_ENSURE_SUCCESS(NS_OpenURI(getter_AddRefs(dummyChannel), uri, nsnull), NS_ERROR_FAILURE);  // (4) fire start document load notification  nsCOMPtr<nsIStreamListener> outStreamListener;  // a valid pointer is required for the returned stream listener    // XXX: warning: magic cookie!  should get string "view delayedContentLoad"    //      from somewhere, maybe nsIHTMLDocument?  NS_ENSURE_SUCCESS(doc->StartDocumentLoad("view delayedContentLoad", dummyChannel, nsnull, NS_STATIC_CAST(nsIContentViewerContainer*, (nsIWebShell*)this),                                           getter_AddRefs(outStreamListener)),                     NS_ERROR_FAILURE);  NS_ENSURE_SUCCESS(OnStartDocumentLoad(mDocLoader, uri, "load"), NS_ERROR_FAILURE);  // (5) hook up the document and its content  nsCOMPtr<nsIContent> rootContent = do_QueryInterface(aRootNode);  if (!doc) { return NS_ERROR_OUT_OF_MEMORY; }  NS_ENSURE_SUCCESS(rootContent->SetDocument(doc, PR_FALSE), NS_ERROR_FAILURE);  doc->SetRootContent(rootContent);  rootContent->SetDocument(doc, PR_TRUE);  // (6) reflow the document  SetScrolling(-1, PR_FALSE);  PRInt32 i;  PRInt32 ns = doc->GetNumberOfShells();  for (i = 0; i < ns; i++)   {    nsCOMPtr<nsIPresShell> shell(dont_AddRef(doc->GetShellAt(i)));    if (shell)     {      // Make shell an observer for next time      NS_ENSURE_SUCCESS(shell->BeginObservingDocument(), NS_ERROR_FAILURE);      // Resize-reflow this time      nsCOMPtr<nsIDocumentViewer> docViewer = do_QueryInterface(documentViewer);      if (!docViewer) { return NS_ERROR_OUT_OF_MEMORY; }      nsCOMPtr<nsIPresContext> presContext;      NS_ENSURE_SUCCESS(docViewer->GetPresContext(*(getter_AddRefs(presContext))), NS_ERROR_FAILURE);      if (!presContext) { return NS_ERROR_OUT_OF_MEMORY; }      float p2t;      presContext->GetScaledPixelsToTwips(&p2t);      nsRect r;      GetPositionAndSize(&r.x, &r.y, &r.width, &r.height);      NS_ENSURE_SUCCESS(shell->InitialReflow(NSToCoordRound(r.width * p2t), NSToCoordRound(r.height * p2t)), NS_ERROR_FAILURE);      // Now trigger a refresh      nsCOMPtr<nsIViewManager> vm;      NS_ENSURE_SUCCESS(shell->GetViewManager(getter_AddRefs(vm)), NS_ERROR_FAILURE);      if (vm)       {        PRBool enabled;        documentViewer->GetEnableRendering(&enabled);        if (enabled) {          vm->EnableRefresh(NS_VMREFRESH_IMMEDIATE);        }        NS_ENSURE_SUCCESS(vm->SetWindowDimensions(NSToCoordRound(r.width * p2t),                                                   NSToCoordRound(r.height * p2t)),                           NS_ERROR_FAILURE);      }    }  }  // (7) fire end document load notification  mProcessedEndDocumentLoad = PR_FALSE;  nsresult rv = NS_OK;  NS_ENSURE_SUCCESS(OnEndDocumentLoad(mDocLoader, dummyChannel, rv), NS_ERROR_FAILURE);  NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);  // test the resulting out-param separately  return NS_OK;}