Bug 621944, find and replace doesn't wrap in mail compose, use the correct focused frame, r=neil
authorNeil Deakin <neil@mozilla.com>
Thu, 18 Oct 2012 09:18:30 -0400
changeset 110795 56d0d35f515a0355a8345897f11358e160439146
parent 110794 c228ab000f4f52d2d12c9280960e90528373a103
child 110796 c9fe407b3492765e28b1a0fbc1ec93729bf75048
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersneil
bugs621944
milestone19.0a1
Bug 621944, find and replace doesn't wrap in mail compose, use the correct focused frame, r=neil
docshell/base/nsDocShell.cpp
embedding/components/find/src/nsWebBrowserFind.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -11357,36 +11357,24 @@ NS_IMETHODIMP nsDocShell::EnsureFind()
     // we promise that the nsIWebBrowserFind that we return has been set
     // up to point to the focused, or content window, so we have to
     // set that up each time.
 
     nsIScriptGlobalObject* scriptGO = GetScriptGlobalObject();
     NS_ENSURE_TRUE(scriptGO, NS_ERROR_UNEXPECTED);
 
     // default to our window
-    nsCOMPtr<nsIDOMWindow> windowToSearch(do_QueryInterface(mScriptGlobal));
-
-    nsCOMPtr<nsIDocShellTreeItem> root;
-    GetRootTreeItem(getter_AddRefs(root));
-
-    // if the active window is the same window that this docshell is in,
-    // use the currently focused frame
-    nsCOMPtr<nsIDOMWindow> rootWindow = do_GetInterface(root);
-    nsCOMPtr<nsIFocusManager> fm = do_GetService(FOCUSMANAGER_CONTRACTID);
-    if (fm) {
-      nsCOMPtr<nsIDOMWindow> activeWindow;
-      fm->GetActiveWindow(getter_AddRefs(activeWindow));
-      if (activeWindow == rootWindow)
-        fm->GetFocusedWindow(getter_AddRefs(windowToSearch));
-    }
+    nsCOMPtr<nsPIDOMWindow> ourWindow = do_QueryInterface(scriptGO);
+    nsCOMPtr<nsPIDOMWindow> windowToSearch;
+    nsFocusManager::GetFocusedDescendant(ourWindow, true, getter_AddRefs(windowToSearch));
 
     nsCOMPtr<nsIWebBrowserFindInFrames> findInFrames = do_QueryInterface(mFind);
     if (!findInFrames) return NS_ERROR_NO_INTERFACE;
     
-    rv = findInFrames->SetRootSearchFrame(rootWindow);
+    rv = findInFrames->SetRootSearchFrame(ourWindow);
     if (NS_FAILED(rv)) return rv;
     rv = findInFrames->SetCurrentSearchFrame(windowToSearch);
     if (NS_FAILED(rv)) return rv;
     
     return NS_OK;
 }
 
 bool
--- a/embedding/components/find/src/nsWebBrowserFind.cpp
+++ b/embedding/components/find/src/nsWebBrowserFind.cpp
@@ -201,30 +201,30 @@ NS_IMETHODIMP nsWebBrowserFind::FindNext
     while (NS_SUCCEEDED(docShellEnumerator->HasMoreElements(&hasMore)) && hasMore)
     {
         nsCOMPtr<nsISupports> curSupports;
         rv = docShellEnumerator->GetNext(getter_AddRefs(curSupports));
         if (NS_FAILED(rv)) break;
         curItem = do_QueryInterface(curSupports, &rv);
         if (NS_FAILED(rv)) break;
 
+        searchFrame = do_GetInterface(curItem, &rv);
+        if (NS_FAILED(rv)) break;
+
         if (curItem.get() == startingItem.get())
         {
             // Beware! This may flush notifications via synchronous
             // ScrollSelectionIntoView.
             rv = SearchInFrame(searchFrame, true, outDidFind);
             if (NS_FAILED(rv)) return rv;
             if (*outDidFind)
                 return OnFind(searchFrame);        // we are done
             break;
         }
 
-        searchFrame = do_GetInterface(curItem, &rv);
-        if (NS_FAILED(rv)) break;
-
         OnStartSearchFrame(searchFrame);
 
         // Beware! This may flush notifications via synchronous
         // ScrollSelectionIntoView.
         rv = SearchInFrame(searchFrame, false, outDidFind);
         if (NS_FAILED(rv)) return rv;
         if (*outDidFind)
             return OnFind(searchFrame);        // we are done
@@ -796,28 +796,23 @@ nsWebBrowserFind::GetFrameSelection(nsID
     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
     nsIPresShell* presShell = doc->GetShell();
     if (!presShell) return;
 
     // text input controls have their independent selection controllers
     // that we must use when they have focus.
     nsPresContext *presContext = presShell->GetPresContext();
 
-    nsIFrame *frame = nullptr;
-    nsCOMPtr<nsIFocusManager> fm = do_GetService(FOCUSMANAGER_CONTRACTID);
-    if (fm) {
-      nsCOMPtr<nsIDOMElement> focusedElement;
-      fm->GetFocusedElement(getter_AddRefs(focusedElement));
-      nsCOMPtr<nsIContent> focusedContent(do_QueryInterface(focusedElement));
-      if (focusedContent) {
-        frame = focusedContent->GetPrimaryFrame();
-        if (frame && frame->PresContext() != presContext)
-          frame = nullptr;
-      }
-    }
+    nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
+
+    nsCOMPtr<nsPIDOMWindow> focusedWindow;
+    nsCOMPtr<nsIContent> focusedContent =
+      nsFocusManager::GetFocusedDescendant(window, false, getter_AddRefs(focusedWindow));
+
+    nsIFrame *frame = focusedContent ? focusedContent->GetPrimaryFrame() : nullptr;
 
     nsCOMPtr<nsISelectionController> selCon;
     if (frame) {
         frame->GetSelectionController(presContext, getter_AddRefs(selCon));
         selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, aSel);
         if (*aSel) {
             int32_t count = -1;
             (*aSel)->GetRangeCount(&count);