if (!mXIF) { static NS_DEFINE_CID(kCXIFConverterCID,        NS_XIFFORMATCONVERTER_CID); rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull, NS_GET_IID(nsIFormatConverter), (void **)getter_AddRefs(mXIF)); if (NS_FAILED(rv)) return rv;
if (!mConverter) { static NS_DEFINE_CID(kHTMLConverterCID, NS_HTMLFORMATCONVERTER_CID); mConverter = do_CreateInstance(kHTMLConverterCID); if (!mConverter) return NS_ERROR_FAILURE;
nsAutoCopyService::NotifySelectionChanged(nsIDOMDocument *aDoc, nsISelection *aSel, short aReason){  nsresult rv;  if (!mClipboard) {    static NS_DEFINE_CID(kCClipboardCID,           NS_CLIPBOARD_CID);    mClipboard = do_GetService(kCClipboardCID, &rv);    if (NS_FAILED(rv))      return rv;  }   if (!(aReason & nsISelectionListener::MOUSEUP_REASON))    return NS_OK;//dont care if we are still dragging. or if its not from a mouseup  PRBool collapsed;  if (!aDoc || !aSel || NS_FAILED(aSel->GetIsCollapsed(&collapsed)) || collapsed) {#ifdef DEBUG_CLIPBOARD    fprintf(stderr, "CLIPBOARD: no selection/collapsed selection\n");#endif    /* clear X clipboard? */    return NS_OK;  }  nsCOMPtr<nsIDocument> doc;  doc = do_QueryInterface(NS_REINTERPRET_CAST(nsISupports *,aDoc),&rv);  nsAutoString xifBuffer;  /* nsPresShell::DoCopy thinks that this is infalliable -- do you? */  if (NS_FAILED(doc->CreateXIF(xifBuffer, aSel)))    return NS_ERROR_FAILURE;    /* create a transferable */  static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);  nsCOMPtr<nsITransferable> trans;  rv = nsComponentManager::CreateInstance(kCTransferableCID, nsnull,                                           NS_GET_IID(nsITransferable),                                          (void **)getter_AddRefs(trans));  if (NS_FAILED(rv))    return rv;  if (!mXIF) {    static NS_DEFINE_CID(kCXIFConverterCID,        NS_XIFFORMATCONVERTER_CID);    rv = nsComponentManager::CreateInstance(kCXIFConverterCID, nsnull,                                            NS_GET_IID(nsIFormatConverter),                                            (void **)getter_AddRefs(mXIF));    if (NS_FAILED(rv))      return rv;  }  trans->AddDataFlavor(kXIFMime);  trans->SetConverter(mXIF);    nsCOMPtr<nsISupportsWString> dataWrapper;  rv = nsComponentManager::CreateInstance(NS_SUPPORTS_WSTRING_CONTRACTID, nsnull,                                           NS_GET_IID(nsISupportsWString),                                          getter_AddRefs(dataWrapper));  if (NS_FAILED(rv))    return rv;    dataWrapper->SetData( NS_CONST_CAST(PRUnichar*,xifBuffer.GetUnicode()));    nsCOMPtr<nsISupports> generic(do_QueryInterface(dataWrapper));  /* Length() is in characters, *2 gives bytes. */  trans->SetTransferData(kXIFMime, generic, xifBuffer.Length() * 2);  mClipboard->SetData(trans, nsnull,nsIClipboard::kSelectionClipboard);#ifdef DEBUG_CLIPBOARD  static char *reasons[] = {    "UNKNOWN", "NEW", "REMOVED", "ALTERED",    "BOGUS4", "BOGUS5", "BOGUS6", "BOGUS7", "BOGUS8"  };  nsAutoString str;  aSel->ToString(str);  char *selStr = str.ToNewCString();  fprintf(stderr, "SELECTION: %s, %p, %p [%s]\n", reasons[reason], doc, aSel,          selStr);  nsMemory::Free(selStr);#endif  return NS_OK;}