nsCOMPtr<nsIStreamListener> listener; if (NS_FAILED(rv = xmlDoc->StartDocumentLoad("view", channel, nsnull, nsnull, getter_AddRefs(listener)))) { NS_ERROR("Failure to init XBL doc prior to load."); return rv;
if (ni && (ni->Equals(nsXULAtoms::scrollbar, kNameSpaceID_XUL) || ni->Equals(nsXULAtoms::thumb, kNameSpaceID_XUL) || (ni->Equals(nsHTMLAtoms::select) && aBoundElement->IsContentOfType(nsIContent::eHTML))) || IsResourceURI(aDocumentURI)) aForceSyncLoad = PR_TRUE;  if(!aForceSyncLoad) {  nsCOMPtr<nsIDocument> doc = do_CreateInstance(kXMLDocumentCID, &rv); NS_ENSURE_SUCCESS(rv, rv);  nsCOMPtr<nsIChannel> channel; rv = NS_NewChannel(getter_AddRefs(channel), aDocumentURI, nsnull, loadGroup); if (NS_FAILED(rv)) return rv;  nsCOMPtr<nsIStreamListener> listener; nsCOMPtr<nsIXMLContentSink> xblSink; NS_NewXBLContentSink(getter_AddRefs(xblSink), doc, aDocumentURI, nsnull); if (!xblSink) return NS_ERROR_FAILURE;  if (NS_FAILED(rv = doc->StartDocumentLoad("loadAsInteractiveData", channel, loadGroup, nsnull, getter_AddRefs(listener), PR_TRUE, xblSink))) { NS_ERROR("Failure to init XBL doc prior to load."); return rv; }   nsXBLStreamListener* xblListener = new nsXBLStreamListener(this, listener, aBoundDocument, doc); NS_ENSURE_TRUE(xblListener,NS_ERROR_OUT_OF_MEMORY);  nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(doc)); rec->AddEventListener(NS_LITERAL_STRING("load"), (nsIDOMLoadListener*)xblListener, PR_FALSE);   nsIBindingManager *bindingManager; if (aBoundDocument) bindingManager = aBoundDocument->GetBindingManager(); else bindingManager = nsnull;  if (bindingManager) bindingManager->PutLoadingDocListener(aDocumentURI, xblListener);   nsXBLBindingRequest* req = nsXBLBindingRequest::Create(mPool, aBindingURL, aBoundElement); xblListener->AddRequest(req);   channel->AsyncOpen(xblListener, nsnull); return NS_OK;
nsXBLService::FetchBindingDocument(nsIURI* aURI, nsIDocument** aResult){  // Initialize our out pointer to nsnull  *aResult = nsnull;  // Create the XML document  nsCOMPtr<nsIDocument> doc;  nsresult rv = nsComponentManager::CreateInstance(kXMLDocumentCID, nsnull,                                                   NS_GET_IID(nsIDocument),                                                   getter_AddRefs(doc));  if (NS_FAILED(rv)) return rv;  // XXX This is evil, but we're living in layout, so I'm  // just going to do it.  nsXMLDocument* xmlDoc = (nsXMLDocument*)(doc.get());  // Now we have to synchronously load the binding file.  // Create an XML content sink and a parser.   nsCOMPtr<nsIChannel> channel;  rv = NS_OpenURI(getter_AddRefs(channel), aURI, nsnull);  if (NS_FAILED(rv)) return rv;  // Call StartDocumentLoad  nsCOMPtr<nsIStreamListener> listener;  if (NS_FAILED(rv = xmlDoc->StartDocumentLoad("view", channel,                                                nsnull, nsnull, getter_AddRefs(listener)))) {    NS_ERROR("Failure to init XBL doc prior to load.");    return rv;  }  // Now do a blocking synchronous parse of the file.  nsCOMPtr<nsIInputStream> in;  PRUint32 sourceOffset = 0;  rv = channel->OpenInputStream(0, -1, getter_AddRefs(in));  // If we couldn't open the channel, then just return.  if (NS_FAILED(rv)) return NS_OK;  NS_ASSERTION(in != nsnull, "no input stream");  if (! in) return NS_ERROR_FAILURE;  rv = NS_ERROR_OUT_OF_MEMORY;  nsProxyStream* proxy = new nsProxyStream();  if (! proxy)    return NS_ERROR_FAILURE;  listener->OnStartRequest(channel, nsnull);  while (PR_TRUE) {    char buf[1024];    PRUint32 readCount;    if (NS_FAILED(rv = in->Read(buf, sizeof(buf), &readCount)))        break; // error    if (readCount == 0)        break; // eof    proxy->SetBuffer(buf, readCount);    rv = listener->OnDataAvailable(channel, nsnull, proxy, sourceOffset, readCount);    sourceOffset += readCount;    if (NS_FAILED(rv))        break;  }  listener->OnStopRequest(channel, nsnull, NS_OK, nsnull);  // don't leak proxy!  proxy->Close();  delete proxy;  // The document is parsed. We now have a prototype document.  // Everything worked, so we can just hand this back now.  *aResult = doc;  NS_IF_ADDREF(*aResult);  // The XML content sink produces a ridiculous # of content nodes.  // It generates text nodes even for whitespace.  The following  // call walks the generated document tree and trims out these  // nodes.  nsCOMPtr<nsIContent> root = getter_AddRefs(doc->GetRootContent());  if (root)    StripWhitespaceNodes(root);  return NS_OK;}