nsCOMPtr<nsILoadGroup> loadGroup; aBoundDocument->GetDocumentLoadGroup(getter_AddRefs(loadGroup));
nsXBLService::FetchBindingDocument(nsIContent* aBoundElement, nsIDocument* aBoundDocument,                                   nsIURI* aURI, const nsCString& aRef,                                    PRBool aForceSyncLoad, 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<nsILoadGroup> loadGroup;  aBoundDocument->GetDocumentLoadGroup(getter_AddRefs(loadGroup));    nsCOMPtr<nsIChannel> channel;  rv = NS_OpenURI(getter_AddRefs(channel), aURI, nsnull, loadGroup);  if (NS_FAILED(rv)) return rv;  // Call StartDocumentLoad  nsCOMPtr<nsIStreamListener> listener;  if (NS_FAILED(rv = xmlDoc->StartDocumentLoad("loadAsData", channel,                                                loadGroup, nsnull, getter_AddRefs(listener)))) {    NS_ERROR("Failure to init XBL doc prior to load.");    return rv;  }  nsCOMPtr<nsIAtom> tagName;  if (aBoundElement)    aBoundElement->GetTag(*getter_AddRefs(tagName));   if ((tagName.get() != kScrollbarAtom) && (tagName.get() != kInputAtom) && !aForceSyncLoad) {    // We can be asynchronous    nsXBLStreamListener* xblListener = new nsXBLStreamListener(listener, aBoundDocument, doc);        nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(doc));    rec->AddEventListener(NS_ConvertASCIItoUCS2("load"), (nsIDOMLoadListener*)xblListener, PR_FALSE);    // Add ourselves to the list of loading docs.    nsCOMPtr<nsIBindingManager> bindingManager;    aBoundDocument->GetBindingManager(getter_AddRefs(bindingManager));    nsXPIDLCString uri;    aURI->GetSpec(getter_Copies(uri));    bindingManager->PutLoadingDocListener(nsCAutoString(NS_STATIC_CAST(const char*, uri)), xblListener);    // Add our request.    nsCAutoString bindingURI(uri);    bindingURI += "#";    bindingURI += aRef;    nsXBLBindingRequest* req = new (mPool) nsXBLBindingRequest(bindingURI, aBoundElement);    xblListener->AddRequest(req);    // Now kick off the async read.    channel->AsyncRead(xblListener, nsnull);    return NS_OK;  }  // Now do a blocking synchronous parse of the file.  nsCOMPtr<nsIInputStream> in;  PRUint32 sourceOffset = 0;  rv = channel->OpenInputStream(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;}