Bug 500882 part 6. Switch remaining modules to using the new GetPrimaryFrame API. r=smaug,roc
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 24 Dec 2009 16:20:06 -0500
changeset 36657 a071473caf241410c47e05d3e4d5e7a48f9ade41
parent 36656 8b6f32659aa66634e8417d15952b8f2d6c9299f9
child 36658 e9921507713bb38e7d1da3532288ffe6722401f1
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerssmaug, roc
bugs500882
milestone1.9.3a1pre
Bug 500882 part 6. Switch remaining modules to using the new GetPrimaryFrame API. r=smaug,roc
docshell/base/nsDocShell.cpp
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMWindowUtils.cpp
dom/base/nsFocusManager.cpp
dom/base/nsGlobalWindow.cpp
editor/libeditor/base/nsEditor.cpp
editor/libeditor/html/nsHTMLEditor.cpp
editor/libeditor/html/nsTableEditor.cpp
embedding/browser/gtk/src/EmbedContextMenuInfo.cpp
embedding/components/find/src/nsFind.cpp
embedding/components/find/src/nsWebBrowserFind.cpp
toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp
widget/src/beos/nsDragService.cpp
widget/src/xpwidgets/nsBaseDragService.cpp
widget/tests/TestWinTSF.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -4492,17 +4492,17 @@ nsDocShell::GetVisibility(PRBool * aVisi
             NS_NOTREACHED("parent docshell has null pres shell");
             return NS_OK;
         }
 
         nsIContent *shellContent =
             pPresShell->GetDocument()->FindContentForSubDocument(presShell->GetDocument());
         NS_ASSERTION(shellContent, "subshell not in the map");
 
-        nsIFrame* frame = pPresShell->GetPrimaryFrameFor(shellContent);
+        nsIFrame* frame = shellContent->GetPrimaryFrame();
         PRBool isDocShellOffScreen = PR_FALSE;
         docShell->GetIsOffScreenBrowser(&isDocShellOffScreen);
         if (frame && !frame->AreAncestorViewsVisible() && !isDocShellOffScreen)
             return NS_OK;
 
         treeItem = parentItem;
         treeItem->GetParent(getter_AddRefs(parentItem));
     }
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -7735,20 +7735,17 @@ nsEventTargetSH::PreserveWrapper(nsISupp
 static PRBool
 GetBindingURL(nsIContent *aContent, nsIDocument *aDocument,
               nsCSSValue::URL **aResult)
 {
   // If we have a frame the frame has already loaded the binding.  And
   // otherwise, don't do anything else here unless we're dealing with
   // XUL.
   nsIPresShell *shell = aDocument->GetPrimaryShell();
-  nsIFrame *frame;
-  if (!shell ||
-      (frame = shell->GetPrimaryFrameFor(aContent)) ||
-      !aContent->IsXUL()) {
+  if (!shell || aContent->GetPrimaryFrame() || !aContent->IsXUL()) {
     *aResult = nsnull;
 
     return PR_TRUE;
   }
 
   // Get the computed -moz-binding directly from the style context
   nsPresContext *pctx = shell->GetPresContext();
   NS_ENSURE_TRUE(pctx, PR_FALSE);
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -469,17 +469,17 @@ nsDOMWindowUtils::GetWidgetForElement(ns
   if (!aElement)
     return GetWidget();
 
   nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
   nsIDocument* doc = content->GetCurrentDoc();
   nsIPresShell* presShell = doc ? doc->GetPrimaryShell() : nsnull;
 
   if (presShell) {
-    nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+    nsIFrame* frame = content->GetPrimaryFrame();
     if (!frame) {
       frame = presShell->GetRootFrame();
     }
     if (frame)
       return frame->GetWindow();
   }
 
   return nsnull;
--- a/dom/base/nsFocusManager.cpp
+++ b/dom/base/nsFocusManager.cpp
@@ -1301,23 +1301,23 @@ nsFocusManager::CheckIfFocusable(nsICont
   if (aContent == doc->GetRootContent())
     return aContent;
 
   // cannot focus content in print preview mode. Only the root can be focused.
   nsPresContext* presContext = shell->GetPresContext();
   if (presContext && presContext->Type() == nsPresContext::eContext_PrintPreview)
     return nsnull;
 
-  nsIFrame* frame = shell->GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
   if (!frame)
     return nsnull;
 
   if (aContent->Tag() == nsGkAtoms::area && aContent->IsHTML()) {
     // HTML areas do not have their own frame, and the img frame we get from
-    // GetPrimaryFrameFor() is not relevant as to whether it is focusable or
+    // GetPrimaryFrame() is not relevant as to whether it is focusable or
     // not, so we have to do all the relevant checks manually for them.
     return frame->AreAncestorViewsVisible() &&
            frame->GetStyleVisibility()->IsVisible() &&
            aContent->IsFocusable() ? aContent : nsnull;
   }
 
   // if this is a child frame content node, check if it is visible and
   // call the content node's IsFocusable method instead of the frame's
@@ -1404,17 +1404,17 @@ nsFocusManager::Blur(nsPIDOMWindow* aWin
       presContext->EventStateManager()->SetContentState(content, NS_EVENT_STATE_FOCUS);  
     }
 
     // if an object/plug-in is being blurred, move the system focus to the
     // parent window, otherwise events will still get fired at the plugin.
     // But don't do this if we are blurring due to the window being lowered,
     // otherwise, the parent window can get raised again.
     if (mActiveWindow) {
-      nsIFrame* contentFrame = presShell->GetPrimaryFrameFor(content);
+      nsIFrame* contentFrame = content->GetPrimaryFrame();
       nsIObjectFrame* objectFrame = do_QueryFrame(contentFrame);
       if (objectFrame) {
         // note that the presshell's widget is being retrieved here, not the one
         // for the object frame.
         nsIViewManager* vm = presShell->GetViewManager();
         if (vm) {
           nsCOMPtr<nsIWidget> widget;
           vm->GetRootWidget(getter_AddRefs(widget));
@@ -1597,23 +1597,27 @@ nsFocusManager::Focus(nsPIDOMWindow* aWi
       if (aFocusChanged)
         ScrollIntoView(presShell, aContent, aFlags);
 
       // inform the EventStateManager so that content state change notifications
       // are made.
       nsPresContext* presContext = presShell->GetPresContext();
       presContext->EventStateManager()->SetContentState(aContent, NS_EVENT_STATE_FOCUS);  
 
-      // if this is an object/plug-in, focus the plugin's widget
-      nsIFrame* contentFrame = presShell->GetPrimaryFrameFor(aContent);
-      nsIObjectFrame* objectFrame = do_QueryFrame(contentFrame);
-      if (objectFrame) {
-        nsIWidget* widget = objectFrame->GetWidget();
-        if (widget)
-          widget->SetFocus(PR_FALSE);
+      // if this is an object/plug-in, focus the plugin's widget.  Note that we might
+      // no longer be in the same document, due to the events we fired above when
+      // aIsNewDocument.
+      if (presShell->GetDocument() == aContent->GetDocument()) {
+        nsIFrame* contentFrame = aContent->GetPrimaryFrame();
+        nsIObjectFrame* objectFrame = do_QueryFrame(contentFrame);
+        if (objectFrame) {
+          nsIWidget* widget = objectFrame->GetWidget();
+          if (widget)
+            widget->SetFocus(PR_FALSE);
+        }
       }
 
       nsIMEStateManager::OnChangeFocus(presContext, aContent);
 
       // as long as this focus wasn't because a window was raised, update the
       // commands
       // XXXndeakin P2 someone could adjust the focus during the update
       if (!aWindowRaised)
@@ -1901,17 +1905,19 @@ nsFocusManager::SetCaretVisible(nsIPresS
 
   PRBool caretVisible = PR_FALSE;
   caret->GetCaretVisible(&caretVisible);
   if (!aVisible && !caretVisible)
     return NS_OK;
 
   nsCOMPtr<nsFrameSelection> frameSelection;
   if (aContent) {
-    nsIFrame *focusFrame = aPresShell->GetPrimaryFrameFor(aContent);
+    NS_ASSERTION(aContent->GetDocument() == aPresShell->GetDocument(),
+                 "Wrong document?");
+    nsIFrame *focusFrame = aContent->GetPrimaryFrame();
     if (focusFrame)
       frameSelection = focusFrame->GetFrameSelection();
   }
 
   nsCOMPtr<nsFrameSelection> docFrameSelection = aPresShell->FrameSelection();
 
   if (docFrameSelection && caret &&
      (frameSelection == docFrameSelection || !aContent)) {
@@ -1996,17 +2002,17 @@ nsFocusManager::GetSelectionLocation(nsI
     }
   }
   else {
     rv = NS_ERROR_INVALID_ARG;
   }
 
   nsIFrame *startFrame = nsnull;
   if (startContent) {
-    startFrame = aPresShell->GetPrimaryFrameFor(startContent);
+    startFrame = startContent->GetPrimaryFrame();
     if (isCollapsed) {
       // Next check to see if our caret is at the very end of a node
       // If so, the caret is actually sitting in front of the next
       // logical frame's primary node - so for this case we need to
       // change caretContent to that node.
 
       nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(startContent));
       PRUint16 nodeType;
@@ -2147,17 +2153,17 @@ nsFocusManager::DetermineElementToMoveFo
   PRBool ignoreTabIndex = PR_FALSE;
   // when a popup is open, we want to ensure that tab navigation occurs only
   // within the most recently opened panel. If a popup is open, its frame will
   // be stored in popupFrame.
   nsIFrame* popupFrame = nsnull;
 
   PRInt32 tabIndex = forward ? 1 : 0;
   if (startContent) {
-    nsIFrame* frame = presShell->GetPrimaryFrameFor(startContent);
+    nsIFrame* frame = startContent->GetPrimaryFrame();
     if (startContent->Tag() == nsGkAtoms::area &&
         startContent->IsHTML())
       startContent->IsFocusable(&tabIndex);
     else if (frame)
       frame->IsFocusable(&tabIndex, 0);
     else
       startContent->IsFocusable(&tabIndex);
 
@@ -2328,17 +2334,17 @@ nsFocusManager::DetermineElementToMoveFo
       doc = do_QueryInterface(piParentWindow->GetExtantDocument());
       NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
 
       presShell = doc->GetPrimaryShell();
 
       rootContent = doc->GetRootContent();
       startContent = do_QueryInterface(piWindow->GetFrameElementInternal());
       if (startContent) {
-        nsIFrame* frame = presShell->GetPrimaryFrameFor(startContent);
+        nsIFrame* frame = startContent->GetPrimaryFrame();
         if (!frame)
           return NS_OK;
 
         frame->IsFocusable(&tabIndex, 0);
         if (tabIndex < 0) {
           tabIndex = 1;
           ignoreTabIndex = PR_TRUE;
         }
@@ -2410,17 +2416,17 @@ nsFocusManager::GetNextTabbableContent(n
   printf(" tabindex: %d\n", aCurrentTabIndex);
 #endif
 
   nsPresContext* presContext = aPresShell->GetPresContext();
 
   PRBool getNextFrame = PR_TRUE;
   nsCOMPtr<nsIContent> iterStartContent = aStartContent;
   while (1) {
-    nsIFrame* startFrame = aPresShell->GetPrimaryFrameFor(iterStartContent);
+    nsIFrame* startFrame = iterStartContent->GetPrimaryFrame();
     // if there is no frame, look for another content node that has a frame
     if (!startFrame) {
       // if the root content doesn't have a frame, just return
       if (iterStartContent == aRootContent)
         return NS_OK;
 
       // look for the next or previous content node in tree order
       nsTreeWalker walker(aRootContent, nsIDOMNodeFilter::SHOW_ALL, nsnull, PR_TRUE);
@@ -2725,17 +2731,17 @@ nsFocusManager::GetRootForFocus(nsPIDOMW
 
   if (aCheckVisibility && !IsWindowVisible(aWindow))
     return nsnull;
 
   nsIContent *rootContent = aDocument->GetRootContent();
   if (rootContent) {
     if (aCheckVisibility) {
       nsIPresShell* presShell = aDocument->GetPrimaryShell();
-      if (!presShell || !presShell->GetPrimaryFrameFor(rootContent))
+      if (!presShell || !rootContent->GetPrimaryFrame())
         return nsnull;
     }
 
     // Finally, check if this is a frameset
     nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(aDocument);
     if (htmlDoc) {
       PRUint32 childCount = rootContent->GetChildCount();
       for (PRUint32 i = 0; i < childCount; ++i) {
@@ -2923,17 +2929,17 @@ nsFocusManager::GetFocusInSelection(nsPI
 
   nsCOMPtr<nsIContent> currentFocus = aWindow->GetFocusedNode();
 
   // We now have the correct start node in selectionContent!
   // Search for focusable elements, starting with selectionContent
 
   // Method #1: Keep going up while we look - an ancestor might be focusable
   // We could end the loop earlier, such as when we're no longer
-  // in the same frame, by comparing getPrimaryFrameFor(selectionContent)
+  // in the same frame, by comparing selectionContent->GetPrimaryFrame()
   // with a variable holding the starting selectionContent
   while (testContent) {
     // Keep testing while selectionContent is equal to something,
     // eventually we'll run out of ancestors
 
     nsCOMPtr<nsIURI> uri;
     if (testContent == currentFocus ||
         testContent->IsLink(getter_AddRefs(uri))) {
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -7014,17 +7014,17 @@ nsGlobalWindow::UpdateCanvasFocus(PRBool
   if (!presShell || !mDocument)
     return;
 
   nsCOMPtr<nsIDocument> doc(do_QueryInterface(mDocument));
   nsIContent *rootContent = doc->GetRootContent();
   if (rootContent) {
       if ((mHasFocus || aFocusChanged) &&
           (mFocusedNode == rootContent || aNewContent == rootContent)) {
-          nsIFrame* frame = presShell->GetPrimaryFrameFor(rootContent);
+          nsIFrame* frame = rootContent->GetPrimaryFrame();
           if (frame) {
               frame = frame->GetParent();
               nsCanvasFrame* canvasFrame = do_QueryFrame(frame);
               if (canvasFrame) {
                   canvasFrame->SetHasFocus(mHasFocus && rootContent == aNewContent);
               }
           }
       }
@@ -9142,21 +9142,17 @@ nsGlobalChromeWindow::NotifyDefaultButto
   nsresult rv = xulControl->GetDisabled(&disabled);
   NS_ENSURE_SUCCESS(rv, rv);
   if (disabled)
     return NS_OK;
 
   // Get the button rect in screen coordinates.
   nsCOMPtr<nsIContent> content(do_QueryInterface(aDefaultButton));
   NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
-  nsIDocument *doc = content->GetCurrentDoc();
-  NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
-  nsIPresShell *shell = doc->GetPrimaryShell();
-  NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
-  nsIFrame *frame = shell->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
   nsIntRect buttonRect = frame->GetScreenRect();
 
   // Get the widget rect in screen coordinates.
   nsIWidget *widget = GetNearestWidget();
   NS_ENSURE_TRUE(widget, NS_ERROR_FAILURE);
   nsIntRect widgetRect;
   rv = widget->GetScreenBounds(widgetRect);
--- a/editor/libeditor/base/nsEditor.cpp
+++ b/editor/libeditor/base/nsEditor.cpp
@@ -2115,30 +2115,30 @@ nsEditor::GetPhonetic(nsAString& aPhonet
   else
     aPhonetic.Truncate(0);
 
   return NS_OK;
 }
 
 
 static nsresult
-GetEditorContentWindow(nsIPresShell *aPresShell, nsIDOMElement *aRoot, nsIWidget **aResult)
+GetEditorContentWindow(nsIDOMElement *aRoot, nsIWidget **aResult)
 {
-  if (!aPresShell || !aRoot || !aResult)
+  if (!aRoot || !aResult)
     return NS_ERROR_NULL_POINTER;
 
   *aResult = 0;
 
   nsCOMPtr<nsIContent> content = do_QueryInterface(aRoot);
 
   if (!content)
     return NS_ERROR_FAILURE;
 
   // Not ref counted
-  nsIFrame *frame = aPresShell->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
 
   if (!frame)
     return NS_ERROR_FAILURE;
 
   *aResult = frame->GetWindow();
   if (!*aResult)
     return NS_ERROR_FAILURE;
 
@@ -2147,27 +2147,19 @@ GetEditorContentWindow(nsIPresShell *aPr
 }
 
 nsresult
 nsEditor::GetWidget(nsIWidget **aWidget)
 {
   if (!aWidget)
     return NS_ERROR_NULL_POINTER;
   *aWidget = nsnull;
-  nsCOMPtr<nsIPresShell> shell;
-  nsresult res = GetPresShell(getter_AddRefs(shell));
-
-  if (NS_FAILED(res))
-    return res;
-
-  if (!shell)
-    return NS_ERROR_FAILURE;
 
   nsCOMPtr<nsIWidget> widget;
-  res = GetEditorContentWindow(shell, GetRoot(), getter_AddRefs(widget));
+  nsresult res = GetEditorContentWindow(GetRoot(), getter_AddRefs(widget));
   if (NS_FAILED(res))
     return res;
   if (!widget)
     return NS_ERROR_NOT_AVAILABLE;
 
   NS_ADDREF(*aWidget = widget);
 
   return NS_OK;
@@ -2217,24 +2209,20 @@ nsEditor::GetPreferredIMEState(PRUint32 
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (flags & (nsIPlaintextEditor::eEditorReadonlyMask |
                nsIPlaintextEditor::eEditorDisabledMask)) {
     *aState = nsIContent::IME_STATUS_DISABLE;
     return NS_OK;
   }
 
-  nsCOMPtr<nsIPresShell> presShell;
-  rv = GetPresShell(getter_AddRefs(presShell));
-  NS_ENSURE_SUCCESS(rv, rv);
-
   nsCOMPtr<nsIContent> content = do_QueryInterface(GetRoot());
   NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
 
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame* frame = content->GetPrimaryFrame();
   NS_ENSURE_TRUE(frame, NS_ERROR_FAILURE);
 
   switch (frame->GetStyleUIReset()->mIMEMode) {
     case NS_STYLE_IME_MODE_AUTO:
       if (flags & (nsIPlaintextEditor::eEditorPasswordMask))
         *aState = nsIContent::IME_STATUS_PASSWORD;
       break;
     case NS_STYLE_IME_MODE_DISABLED:
@@ -3802,28 +3790,25 @@ nsEditor::IsTextInDirtyFrameVisible(nsID
 
   return PR_TRUE;
 }
 
 PRBool 
 nsEditor::IsEditable(nsIDOMNode *aNode)
 {
   if (!aNode) return PR_FALSE;
-  nsCOMPtr<nsIPresShell> shell;
-  GetPresShell(getter_AddRefs(shell));
-  if (!shell)  return PR_FALSE;
 
   if (IsMozEditorBogusNode(aNode) || !IsModifiableNode(aNode)) return PR_FALSE;
 
   // see if it has a frame.  If so, we'll edit it.
   // special case for textnodes: frame must have width.
   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
   if (content)
   {
-    nsIFrame *resultFrame = shell->GetPrimaryFrameFor(content);
+    nsIFrame *resultFrame = content->GetPrimaryFrame();
     if (!resultFrame)   // if it has no frame, it is not editable
       return PR_FALSE;
     NS_ASSERTION(content->IsNodeOfType(nsINode::eTEXT) ||
                  content->IsNodeOfType(nsINode::eELEMENT),
                  "frame for non element-or-text?");
     if (!content->IsNodeOfType(nsINode::eTEXT))
       return PR_TRUE;  // not a text node; has a frame
 
@@ -4151,17 +4136,17 @@ nsEditor::IsPreformatted(nsIDOMNode *aNo
 {
   nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
   
   if (!aResult || !content) return NS_ERROR_NULL_POINTER;
   
   nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
   if (!ps) return NS_ERROR_NOT_INITIALIZED;
   
-  nsIFrame *frame = ps->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
 
   NS_ASSERTION(frame, "no frame, see bug #188946");
   if (!frame)
   {
     // Consider nodes without a style context to be NOT preformatted:
     // For instance, this is true of JS tags inside the body (which show
     // up as #text nodes but have no style context).
     *aResult = PR_FALSE;
@@ -5302,22 +5287,17 @@ nsEditor::SwitchTextDirection()
   // Get the current root direction from its frame
   nsIDOMElement *rootElement = GetRoot();
 
   nsresult rv;
   nsCOMPtr<nsIContent> content = do_QueryInterface(rootElement, &rv);
   if (NS_FAILED(rv))
     return rv;
 
-  nsCOMPtr<nsIPresShell> presShell;
-  rv = GetPresShell(getter_AddRefs(presShell));
-  if (NS_FAILED(rv))
-    return rv;  
-
-  nsIFrame *frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
   if (!frame)
     return NS_ERROR_FAILURE; 
 
   // Apply the opposite direction
   if (frame->GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL)
     rv = rootElement->SetAttribute(NS_LITERAL_STRING("dir"), NS_LITERAL_STRING("ltr"));
   else
     rv = rootElement->SetAttribute(NS_LITERAL_STRING("dir"), NS_LITERAL_STRING("rtl"));
--- a/editor/libeditor/html/nsHTMLEditor.cpp
+++ b/editor/libeditor/html/nsHTMLEditor.cpp
@@ -5516,17 +5516,17 @@ nsHTMLEditor::GetElementOrigin(nsIDOMEle
   aX = 0;
   aY = 0;
 
   if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
   nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
   if (!ps) return NS_ERROR_NOT_INITIALIZED;
 
   nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
-  nsIFrame *frame = ps->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
 
   nsIFrame *container = ps->GetAbsoluteContainingBlock(frame);
   if (!frame) return NS_OK;
   nsPoint off = frame->GetOffsetTo(container);
   aX = nsPresContext::AppUnitsToIntCSSPixels(off.x);
   aY = nsPresContext::AppUnitsToIntCSSPixels(off.y);
 
   return NS_OK;
--- a/editor/libeditor/html/nsTableEditor.cpp
+++ b/editor/libeditor/html/nsTableEditor.cpp
@@ -2645,17 +2645,17 @@ nsHTMLEditor::GetCellIndexes(nsIDOMEleme
 
   if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
   nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
   if (!ps) return NS_ERROR_NOT_INITIALIZED;
 
   nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aCell) );
   if (!nodeAsContent) return NS_ERROR_FAILURE;
   // frames are not ref counted, so don't use an nsCOMPtr
-  nsIFrame *layoutObject = ps->GetPrimaryFrameFor(nodeAsContent);
+  nsIFrame *layoutObject = nodeAsContent->GetPrimaryFrame();
   if (!layoutObject)  return NS_ERROR_FAILURE;
 
   nsITableCellLayout *cellLayoutObject = do_QueryFrame(layoutObject);
   if (!cellLayoutObject)  return NS_ERROR_FAILURE;
   return cellLayoutObject->GetCellIndexes(*aRowIndex, *aColIndex);
 }
 
 NS_IMETHODIMP
@@ -2665,17 +2665,17 @@ nsHTMLEditor::GetTableLayoutObject(nsIDO
   if (!aTable) return NS_ERROR_NOT_INITIALIZED;
   if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
   nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
   if (!ps) return NS_ERROR_NOT_INITIALIZED;
 
   nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aTable) );
   if (!nodeAsContent) return NS_ERROR_FAILURE;
   // frames are not ref counted, so don't use an nsCOMPtr
-  nsIFrame *layoutObject = ps->GetPrimaryFrameFor(nodeAsContent);
+  nsIFrame *layoutObject = nodeAsContent->GetPrimaryFrame();
   if (!layoutObject)  return NS_ERROR_FAILURE;
 
   *tableLayoutObject = do_QueryFrame(layoutObject);
   return *tableLayoutObject ? NS_OK : NS_NOINTERFACE;
 }
 
 //Return actual number of cells (a cell with colspan > 1 counts as just 1)
 PRBool nsHTMLEditor::GetNumberOfCellsInRow(nsIDOMElement* aTable, PRInt32 rowIndex)
--- a/embedding/browser/gtk/src/EmbedContextMenuInfo.cpp
+++ b/embedding/browser/gtk/src/EmbedContextMenuInfo.cpp
@@ -160,19 +160,20 @@ EmbedContextMenuInfo::GetFormControlType
     mCtxDocument = domDoc;
     nsCOMPtr<nsIDocument> doc = do_QueryInterface(mCtxDocument);
     if (!doc)
       return NS_OK;
     nsIPresShell *presShell = doc->GetPrimaryShell();
     if (!presShell)
       return NS_OK;
     nsCOMPtr<nsIContent> tgContent = do_QueryInterface(mEventTarget);
-	nsIFrame* frame = nsnull;
+    nsIFrame* frame = nsnull;
 #if defined(FIXED_BUG347731) || !defined(MOZ_ENABLE_LIBXUL)
-    frame = presShell->GetPrimaryFrameFor(tgContent);
+    frame = tgContent->GetDocument() == presShell->GetDocument() ?
+      tgContent->GetPrimaryFrame() : nsnull;
     if (frame)
       mFormRect = frame->GetScreenRectExternal();
 #endif
     return NS_OK;
   }
   return NS_ERROR_FAILURE;
 }
 
@@ -590,18 +591,18 @@ EmbedContextMenuInfo::UpdateContextData(
   nsIPresShell *presShell = doc->GetPrimaryShell();
   if (!presShell)
     return NS_OK;
   nsCOMPtr<nsIContent> tgContent = do_QueryInterface(mEventTarget);
   nsIFrame* frame = nsnull;
 #if defined(FIXED_BUG347731) || !defined(MOZ_ENABLE_LIBXUL)
   if (mEmbedCtxType & GTK_MOZ_EMBED_CTX_RICHEDIT)
     frame = presShell->GetRootFrame();
-  else {
-    frame = presShell->GetPrimaryFrameFor(tgContent);
+  else if (tgContent->GetDocument() == presShell->GetDocument()) {
+    frame = tgContent->GetPrimaryFrame();
   }
   if (frame) {
     mFormRect = frame->GetScreenRectExternal();
   }
 #endif
   if (NS_SUCCEEDED(SetFormControlType(mEventTarget))) {
     return NS_OK;
   }
--- a/embedding/components/find/src/nsFind.cpp
+++ b/embedding/components/find/src/nsFind.cpp
@@ -371,26 +371,17 @@ nsFindContentIterator::MaybeSetupInnerIt
 void
 nsFindContentIterator::SetupInnerIterator(nsIContent* aContent)
 {
   if (!aContent) {
     return;
   }
   NS_ASSERTION(!aContent->IsRootOfNativeAnonymousSubtree(), "invalid call");
 
-  nsIDocument* doc = aContent->GetDocument();
-  nsIPresShell* shell = doc ? doc->GetPrimaryShell() : nsnull;
-  if (!shell)
-    return;
-
-  nsIFrame* frame = shell->GetPrimaryFrameFor(aContent);
-  if (!frame)
-    return;
-
-  nsITextControlFrame* tcFrame = do_QueryFrame(frame);
+  nsITextControlFrame* tcFrame = do_QueryFrame(aContent->GetPrimaryFrame());
   if (!tcFrame)
     return;
 
   nsCOMPtr<nsIEditor> editor;
   tcFrame->GetEditor(getter_AddRefs(editor));
   if (!editor)
     return;
 
@@ -822,25 +813,17 @@ PRBool nsFind::IsTextNode(nsIDOMNode* aN
 }
 
 PRBool nsFind::IsVisibleNode(nsIDOMNode *aDOMNode)
 {
   nsCOMPtr<nsIContent> content(do_QueryInterface(aDOMNode));
   if (!content)
     return PR_FALSE;
 
-  nsCOMPtr<nsIDocument> doc = content->GetDocument();
-  if (!doc)
-    return PR_FALSE;
-
-  nsIPresShell *presShell = doc->GetPrimaryShell();
-  if (!presShell)
-    return PR_FALSE;
-
-  nsIFrame *frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
   if (!frame) {
     // No frame! Not visible then.
     return PR_FALSE;
   }
 
   return frame->GetStyleVisibility()->IsVisible();
 }
 
--- a/embedding/components/find/src/nsWebBrowserFind.cpp
+++ b/embedding/components/find/src/nsWebBrowserFind.cpp
@@ -395,29 +395,29 @@ void nsWebBrowserFind::SetSelectionAndSc
 
   nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
   nsIPresShell* presShell = doc->GetPrimaryShell();
   if (!presShell) return;
 
   nsCOMPtr<nsIDOMNode> node;
   aRange->GetStartContainer(getter_AddRefs(node));
   nsCOMPtr<nsIContent> content(do_QueryInterface(node));
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame* frame = content->GetPrimaryFrame();
   if (!frame)
       return;
   nsCOMPtr<nsISelectionController> selCon;
   frame->GetSelectionController(presShell->GetPresContext(),
                                 getter_AddRefs(selCon));
   
   // since the match could be an anonymous textnode inside a
   // <textarea> or text <input>, we need to get the outer frame
   nsITextControlFrame *tcFrame = nsnull;
   for ( ; content; content = content->GetParent()) {
     if (!IsInNativeAnonymousSubtree(content)) {
-      nsIFrame* f = presShell->GetPrimaryFrameFor(content);
+      nsIFrame* f = content->GetPrimaryFrame();
       if (!f)
         return;
       tcFrame = do_QueryFrame(f);
       break;
     }
   }
 
   nsCOMPtr<nsISelection> selection;
@@ -842,17 +842,17 @@ nsWebBrowserFind::GetFrameSelection(nsID
 
     nsIFrame *frame = nsnull;
     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 = presShell->GetPrimaryFrameFor(focusedContent);
+        frame = focusedContent->GetPrimaryFrame();
         if (frame && frame->PresContext() != presContext)
           frame = nsnull;
       }
     }
 
     nsCOMPtr<nsISelectionController> selCon;
     if (frame) {
         frame->GetSelectionController(presContext, getter_AddRefs(selCon));
--- a/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp
+++ b/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp
@@ -1091,17 +1091,17 @@ nsTypeAheadFind::IsRangeVisible(nsIPresS
   aRange->CloneRange(aFirstVisibleRange);
   nsCOMPtr<nsIDOMNode> node;
   aRange->GetStartContainer(getter_AddRefs(node));
 
   nsCOMPtr<nsIContent> content(do_QueryInterface(node));
   if (!content)
     return PR_FALSE;
 
-  nsIFrame *frame = aPresShell->GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
   if (!frame)    
     return PR_FALSE;  // No frame! Not visible then.
 
   if (!frame->GetStyleVisibility()->IsVisible())
     return PR_FALSE;
 
   // Detect if we are _inside_ a text control, or something else with its own
   // selection controller.
--- a/widget/src/beos/nsDragService.cpp
+++ b/widget/src/beos/nsDragService.cpp
@@ -72,23 +72,17 @@ static PRLogModuleInfo *sDragLm = NULL;
 
 static nsIFrame*
 GetPrimaryFrameFor(nsIDOMNode *aDOMNode)
 {
     nsCOMPtr<nsIContent> aContent = do_QueryInterface(aDOMNode);
     if (nsnull == aContent)
         return nsnull;
 
-    nsIDocument* doc = aContent->GetCurrentDoc();
-    if (nsnull == doc)
-        return nsnull;
-    nsIPresShell* presShell = doc->GetPrimaryShell();
-    if ( nsnull == presShell) 
-        return nsnull;
-    return presShell->GetPrimaryFrameFor(aContent);
+    return aContent->GetPrimaryFrame();
 }
 
 static bool 
 IsInternalDrag(BMessage * aMsg)
 {
     BString orig;
     // We started this drag if originater is 'BeZilla'
     return (nsnull != aMsg && B_OK == aMsg->FindString("be:originator", &orig) &&
--- a/widget/src/xpwidgets/nsBaseDragService.cpp
+++ b/widget/src/xpwidgets/nsBaseDragService.cpp
@@ -465,17 +465,17 @@ nsBaseDragService::DrawDrag(nsIDOMNode* 
         aScreenDragRect->SetRect(screenRect.x + dragRect.x, screenRect.y + dragRect.y,
                                  dragRect.width, dragRect.height);
       }
     }
     else {
       // otherwise, there was no region so just set the rectangle to
       // the size of the primary frame of the content.
       nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
-      nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+      nsIFrame* frame = content->GetPrimaryFrame();
       if (frame) {
         nsIntRect screenRect = frame->GetScreenRectExternal();
         aScreenDragRect->SetRect(screenRect.x, screenRect.y,
                                  screenRect.width, screenRect.height);
       }
     }
 
     return NS_OK;
--- a/widget/tests/TestWinTSF.cpp
+++ b/widget/tests/TestWinTSF.cpp
@@ -3065,18 +3065,18 @@ nsresult
 TestApp::GetSelCon(nsISelectionController** aSelCon)
 {
   nsCOMPtr<nsIDocShell> docShell;
   nsresult nsr = mWindow->GetDocShell(getter_AddRefs(docShell));
   if (NS_SUCCEEDED(nsr) && docShell) {
     nsCOMPtr<nsIPresShell> presShell;
     nsr = docShell->GetPresShell(getter_AddRefs(presShell));
     if (NS_SUCCEEDED(nsr) && presShell) {
-      nsIFrame* frame = presShell->GetPrimaryFrameFor(
-          nsCOMPtr<nsIContent>(do_QueryInterface(mCurrentNode)));
+      nsIFrame* frame = 
+        nsCOMPtr<nsIContent>(do_QueryInterface(mCurrentNode))->GetPrimaryFrame();
       if (frame) {
         nsPresContext* presContext = presShell->GetPresContext();
         if (presContext) {
           nsr = frame->GetSelectionController(presContext, aSelCon);
         }
       }
     }
   }