nsCOMPtr<nsIEventStateManager> manager; nsresult rv = mPresContext->GetEventStateManager(getter_AddRefs(manager)); if (NS_FAILED(rv)) return rv; if (!manager) return NS_ERROR_FAILURE;
nsCOMPtr<nsIContent> content; nsCOMPtr<nsPIDOMWindow> ourWindow = do_QueryInterface(mDocument->GetScriptGlobalObject()); if (ourWindow) { nsCOMPtr<nsIFocusController> focusController; ourWindow->GetRootFocusController(getter_AddRefs(focusController)); if (focusController) { nsCOMPtr<nsIDOMElement> focusedElement; focusController->GetFocusedElement(getter_AddRefs(focusedElement)); content = do_QueryInterface(focusedElement); } }
PresShell::GetSelectionForCopy(nsISelection** outSelection){  *outSelection = nsnull;  nsCOMPtr<nsIDocument> doc;  GetDocument(getter_AddRefs(doc));  if (!doc) return NS_ERROR_FAILURE;  nsCOMPtr<nsIEventStateManager> manager;  nsresult rv = mPresContext->GetEventStateManager(getter_AddRefs(manager));  if (NS_FAILED(rv)) return rv;  if (!manager) return NS_ERROR_FAILURE;  nsCOMPtr<nsISelection> sel;  nsCOMPtr<nsIContent> content;  rv = manager->GetFocusedContent(getter_AddRefs(content));  if (NS_SUCCEEDED(rv) && content)  {    //check to see if we need to get selection from frame    //optimization that MAY need to be expanded as more things implement their own "selection"    nsCOMPtr<nsIDOMNSHTMLInputElement>    htmlInputElement(do_QueryInterface(content));    nsCOMPtr<nsIDOMNSHTMLTextAreaElement> htmlTextAreaElement(do_QueryInterface(content));    if (htmlInputElement || htmlTextAreaElement)    {      nsIFrame *htmlInputFrame;      rv = GetPrimaryFrameFor(content, &htmlInputFrame);      if (NS_FAILED(rv))  return rv;      if (!htmlInputFrame) return NS_ERROR_FAILURE;      nsCOMPtr<nsISelectionController> selCon;      rv = htmlInputFrame->GetSelectionController(mPresContext,getter_AddRefs(selCon));      if (NS_FAILED(rv)) return rv;      if (!selCon) return NS_ERROR_FAILURE;      rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(sel));    }  }  if (!sel) //get selection from this PresShell    rv = GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(sel));     *outSelection = sel;  NS_IF_ADDREF(*outSelection);  return rv;}