Bug 500882 part 5. Switch layout module to using the new GetPrimaryFrame API. r=roc
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 24 Dec 2009 16:20:06 -0500
changeset 36656 8b6f32659aa66634e8417d15952b8f2d6c9299f9
parent 36655 5824db6f9c71d84c20f8207239d7c88153a4ae87
child 36657 a071473caf241410c47e05d3e4d5e7a48f9ade41
push idunknown
push userunknown
push dateunknown
reviewersroc
bugs500882
milestone1.9.3a1pre
Bug 500882 part 5. Switch layout module to using the new GetPrimaryFrame API. r=roc
layout/base/nsCSSFrameConstructor.cpp
layout/base/nsCSSRendering.cpp
layout/base/nsIPresShell.h
layout/base/nsPresShell.cpp
layout/forms/nsIsIndexFrame.cpp
layout/forms/nsListControlFrame.cpp
layout/generic/nsFrame.cpp
layout/generic/nsFrameFrame.cpp
layout/generic/nsGfxScrollFrame.cpp
layout/generic/nsObjectFrame.cpp
layout/generic/nsSelection.cpp
layout/inspector/src/inDOMUtils.cpp
layout/inspector/src/inFlasher.cpp
layout/inspector/src/inLayoutUtils.cpp
layout/inspector/src/inLayoutUtils.h
layout/printing/nsPrintEngine.cpp
layout/style/nsComputedDOMStyle.cpp
layout/style/nsHTMLStyleSheet.cpp
layout/svg/base/src/nsSVGEffects.cpp
layout/svg/base/src/nsSVGOuterSVGFrame.cpp
layout/svg/base/src/nsSVGUtils.cpp
layout/xul/base/src/nsBoxObject.cpp
layout/xul/base/src/nsListBoxBodyFrame.cpp
layout/xul/base/src/nsListBoxObject.cpp
layout/xul/base/src/nsMenuFrame.cpp
layout/xul/base/src/nsMenuPopupFrame.cpp
layout/xul/base/src/nsResizerFrame.cpp
layout/xul/base/src/nsScrollbarFrame.cpp
layout/xul/base/src/nsSliderFrame.cpp
layout/xul/base/src/nsXULPopupManager.cpp
layout/xul/base/src/nsXULTooltipListener.cpp
layout/xul/base/src/tree/src/nsTreeBodyFrame.cpp
layout/xul/base/src/tree/src/nsTreeBoxObject.cpp
layout/xul/base/src/tree/src/nsTreeColumns.cpp
--- a/layout/base/nsCSSFrameConstructor.cpp
+++ b/layout/base/nsCSSFrameConstructor.cpp
@@ -5445,17 +5445,17 @@ nsCSSFrameConstructor::ReconstructDocEle
 {
   return RecreateFramesForContent(mPresShell->GetDocument()->GetRootContent(), PR_FALSE);
 }
 
 nsIFrame*
 nsCSSFrameConstructor::GetFrameFor(nsIContent* aContent)
 {
   // Get the primary frame associated with the content
-  nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
 
   if (!frame)
     return nsnull;
 
   nsIFrame* insertionFrame = frame->GetContentInsertionFrame();
 
   NS_ASSERTION(insertionFrame == frame || !frame->IsLeaf(),
     "The insertion frame is the primary frame or the primary frame isn't a leaf");
@@ -5822,24 +5822,24 @@ nsCSSFrameConstructor::IsValidSibling(ns
 }
 
 nsIFrame*
 nsCSSFrameConstructor::FindFrameForContentSibling(nsIContent* aContent,
                                                   nsIContent* aTargetContent,
                                                   PRUint8& aTargetContentDisplay,
                                                   PRBool aPrevSibling)
 {
-  nsIFrame* sibling = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* sibling = aContent->GetPrimaryFrame();
   if (!sibling || sibling->GetContent() != aContent) {
     // XXX the GetContent() != aContent check is needed due to bug 135040.
     // Remove it once that's fixed.
     return nsnull;
   }
 
-  // If the frame is out-of-flow, GPFF() will have returned the
+  // If the frame is out-of-flow, GetPrimaryFrame() will have returned the
   // out-of-flow frame; we want the placeholder.
   if (sibling->GetStateBits() & NS_FRAME_OUT_OF_FLOW) {
     nsIFrame* placeholderFrame;
     mPresShell->GetPlaceholderFrameFor(sibling, &placeholderFrame);
     NS_ASSERTION(placeholderFrame, "no placeholder for out-of-flow frame");
     sibling = placeholderFrame;
   }
 
@@ -6070,17 +6070,17 @@ nsCSSFrameConstructor::AddTextItemIfNeed
   nsIContent* content = aParentContent->GetChildAt(aContentIndex);
   if (!content->IsNodeOfType(nsINode::eTEXT) ||
       !content->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE)) {
     // Not text, or not suppressed due to being all-whitespace (if it
     // were being suppressed, it would have the
     // NS_CREATE_FRAME_IF_NON_WHITESPACE flag)
     return;
   }
-  NS_ASSERTION(!mPresShell->GetPrimaryFrameFor(content),
+  NS_ASSERTION(!content->GetPrimaryFrame(),
                "Text node has a frame and NS_CREATE_FRAME_IF_NON_WHITESPACE");
   AddFrameConstructionItems(aState, content, aContentIndex, aParentFrame, aItems);
 }
 
 void
 nsCSSFrameConstructor::ReframeTextIfNeeded(nsIContent* aParentContent,
                                            PRInt32 aContentIndex)
 {
@@ -6090,17 +6090,17 @@ nsCSSFrameConstructor::ReframeTextIfNeed
   nsIContent* content = aParentContent->GetChildAt(aContentIndex);
   if (!content->IsNodeOfType(nsINode::eTEXT) ||
       !content->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE)) {
     // Not text, or not suppressed due to being all-whitespace (if it
     // were being suppressed, it would have the
     // NS_CREATE_FRAME_IF_NON_WHITESPACE flag)
     return;
   }
-  NS_ASSERTION(!mPresShell->GetPrimaryFrameFor(content),
+  NS_ASSERTION(!content->GetPrimaryFrame(),
                "Text node has a frame and NS_CREATE_FRAME_IF_NON_WHITESPACE");
   ContentInserted(aParentContent, content, aContentIndex, nsnull);
 }
 
 nsresult
 nsCSSFrameConstructor::ContentAppended(nsIContent*     aContainer,
                                        PRInt32         aNewIndexInContainer)
 {
@@ -6185,17 +6185,17 @@ nsCSSFrameConstructor::ContentAppended(n
       // Now comes the fun part.  For each appended child, make a
       // ContentInserted call as if it had just gotten inserted at the index
       // it's at in aContainer and let ContentInserted handle the mess.  If our
       // insertion point is non-XBL that's the correct index, and otherwise
       // ContentInserted will ignore the passed-in index.
       PRUint32 containerCount = aContainer->GetChildCount();
       for (PRUint32 i = aNewIndexInContainer; i < containerCount; i++) {
         nsIContent* content = aContainer->GetChildAt(i);
-        if ((mPresShell->GetPrimaryFrameFor(content) ||
+        if ((content->GetPrimaryFrame() ||
              mPresShell->FrameManager()->GetUndisplayedContent(content))
 #ifdef MOZ_XUL
             //  Except listboxes suck, so do NOT skip anything here if
             //  we plan to notify a listbox.
             && !MaybeGetListBoxBodyFrame(aContainer, content)
 #endif
             ) {
           // Already have a frame or undisplayed entry for this content; a
@@ -6881,18 +6881,17 @@ nsCSSFrameConstructor::ContentRemoved(ns
   }
 #endif
 
   nsFrameManager *frameManager = mPresShell->FrameManager();
   nsPresContext *presContext = mPresShell->GetPresContext();
   nsresult                  rv = NS_OK;
 
   // Find the child frame that maps the content
-  nsIFrame* childFrame =
-    frameManager->GetPrimaryFrameFor(aChild, aIndexInContainer);
+  nsIFrame* childFrame = aChild->GetPrimaryFrame();
 
   if (!childFrame || childFrame->GetContent() != aChild) {
     // XXXbz the GetContent() != aChild check is needed due to bug 135040.
     // Remove it once that's fixed.
     frameManager->ClearUndisplayedContentIn(aChild, aContainer);
   }
 
 #ifdef MOZ_XUL
@@ -7001,17 +7000,17 @@ nsCSSFrameConstructor::ContentRemoved(ns
 
       // First update the containing blocks structure by removing the
       // existing letter frames. This makes the subsequent logic
       // simpler.
       RemoveLetterFrames(presContext, mPresShell, frameManager,
                          containingBlock);
 
       // Recover childFrame and parentFrame
-      childFrame = mPresShell->GetPrimaryFrameFor(aChild);
+      childFrame = aChild->GetPrimaryFrame();
       if (!childFrame || childFrame->GetContent() != aChild) {
         // XXXbz the GetContent() != aChild check is needed due to bug 135040.
         // Remove it once that's fixed.
         frameManager->ClearUndisplayedContentIn(aChild, aContainer);
         return NS_OK;
       }
       parentFrame = childFrame->GetParent();
       parentType = parentFrame->GetType();
@@ -7355,28 +7354,28 @@ nsCSSFrameConstructor::CharacterDataChan
   AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC);
   nsresult      rv = NS_OK;
 
   if ((aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE) &&
        !aContent->TextIsOnlyWhitespace()) ||
       (aContent->HasFlag(NS_REFRAME_IF_WHITESPACE) &&
        aContent->TextIsOnlyWhitespace())) {
 #ifdef DEBUG
-    nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
+    nsIFrame* frame = aContent->GetPrimaryFrame();
     NS_ASSERTION(!frame || !frame->IsGeneratedContentFrame(),
                  "Bit should never be set on generated content");
 #endif
     LAYOUT_PHASE_TEMP_EXIT();
     nsresult rv = RecreateFramesForContent(aContent, PR_FALSE);
     LAYOUT_PHASE_TEMP_REENTER();
     return rv;
   }
 
   // Find the child frame
-  nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
 
   // Notify the first frame that maps the content. It will generate a reflow
   // command
 
   // It's possible the frame whose content changed isn't inserted into the
   // frame hierarchy yet, or that there is no frame that maps the content
   if (nsnull != frame) {
 #if 0
@@ -7400,17 +7399,17 @@ nsCSSFrameConstructor::CharacterDataChan
     if (block) {
       // See if the block has first-letter style applied to it.
       haveFirstLetterStyle = HasFirstLetterStyle(block);
       if (haveFirstLetterStyle) {
         RemoveLetterFrames(mPresShell->GetPresContext(), mPresShell,
                            mPresShell->FrameManager(), block);
         // Reget |frame|, since we might have killed it.
         // Do we really need to call CharacterDataChanged in this case, though?
-        frame = mPresShell->GetPrimaryFrameFor(aContent);
+        frame = aContent->GetPrimaryFrame();
         NS_ASSERTION(frame, "Should have frame here!");
       }
     }
 
     frame->CharacterDataChanged(aInfo);
 
     if (haveFirstLetterStyle) {
       nsFrameConstructorState state(mPresShell, mFixedContainingBlock,
@@ -7532,17 +7531,17 @@ nsCSSFrameConstructor::ProcessRestyledFr
     if (changeData->mFrame) {
       propTable->DeleteProperty(changeData->mFrame,
                                 nsGkAtoms::changeListProperty);
     }
 
 #ifdef DEBUG
     // reget frame from content since it may have been regenerated...
     if (changeData->mContent) {
-      nsIFrame* frame = mPresShell->GetPrimaryFrameFor(changeData->mContent);
+      nsIFrame* frame = changeData->mContent->GetPrimaryFrame();
       if (frame) {
         mPresShell->FrameManager()->DebugVerifyStyleTree(frame);
       }
     } else {
       NS_WARNING("Unable to test style tree integrity -- no content node");
     }
 #endif
   }
@@ -7551,17 +7550,17 @@ nsCSSFrameConstructor::ProcessRestyledFr
   return NS_OK;
 }
 
 void
 nsCSSFrameConstructor::RestyleElement(nsIContent     *aContent,
                                       nsIFrame       *aPrimaryFrame,
                                       nsChangeHint   aMinHint)
 {
-  NS_ASSERTION(aPrimaryFrame == mPresShell->GetPrimaryFrameFor(aContent),
+  NS_ASSERTION(aPrimaryFrame == aContent->GetPrimaryFrame(),
                "frame/content mismatch");
   if (aPrimaryFrame && aPrimaryFrame->GetContent() != aContent) {
     // XXXbz this is due to image maps messing with the primary frame pointer
     // of <area>s.  See bug 135040.  We can remove this block once that's fixed.
     aPrimaryFrame = nsnull;
   }
   NS_ASSERTION(!aPrimaryFrame || aPrimaryFrame->GetContent() == aContent,
                "frame/content mismatch");
@@ -7588,17 +7587,17 @@ nsCSSFrameConstructor::RestyleLaterSibli
 
   for (PRInt32 index = parent->IndexOf(aContent) + 1,
                index_end = parent->GetChildCount();
        index != index_end; ++index) {
     nsIContent *child = parent->GetChildAt(index);
     if (!child->IsNodeOfType(nsINode::eELEMENT))
       continue;
 
-    nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(child);
+    nsIFrame* primaryFrame = child->GetPrimaryFrame();
     RestyleElement(child, primaryFrame, NS_STYLE_HINT_NONE);
   }
 }
 
 nsresult
 nsCSSFrameConstructor::ContentStatesChanged(nsIContent* aContent1,
                                             nsIContent* aContent2,
                                             PRInt32 aStateMask) 
@@ -7619,17 +7618,17 @@ nsCSSFrameConstructor::DoContentStateCha
   if (aContent) {
     nsChangeHint hint = NS_STYLE_HINT_NONE;
     // Any change to a content state that affects which frames we construct
     // must lead to a frame reconstruct here if we already have a frame.
     // Note that we never decide through non-CSS means to not create frames
     // based on content states, so if we already don't have a frame we don't
     // need to force a reframe -- if it's needed, the HasStateDependentStyle
     // call will handle things.
-    nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(aContent);
+    nsIFrame* primaryFrame = aContent->GetPrimaryFrame();
     if (primaryFrame) {
       // If it's generated content, ignore LOADING/etc state changes on it.
       if (!primaryFrame->IsGeneratedContentFrame() &&
           (aStateMask & (NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED |
                          NS_EVENT_STATE_SUPPRESSED | NS_EVENT_STATE_LOADING))) {
         hint = nsChangeHint_ReconstructFrame;
       } else {          
         PRUint8 app = primaryFrame->GetStyleDisplay()->mAppearance;
@@ -7679,17 +7678,17 @@ nsCSSFrameConstructor::AttributeChanged(
                                         PRInt32 aModType)
 {
   // Hold onto the PresShell to prevent ourselves from being destroyed.
   // XXXbz how, exactly, would this attribute change cause us to be
   // destroyed from inside this function?
   nsCOMPtr<nsIPresShell> shell = mPresShell;
 
   // Get the frame associated with the content which is the highest in the frame tree
-  nsIFrame* primaryFrame = shell->GetPrimaryFrameFor(aContent); 
+  nsIFrame* primaryFrame = aContent->GetPrimaryFrame();
 
 #if 0
   NS_FRAME_LOG(NS_FRAME_TRACE_CALLS,
      ("HTMLStyleSheet::AttributeChanged: content=%p[%s] frame=%p",
       aContent, ContentTag(aContent, 0), frame));
 #endif
 
   // the style tag has its own interpretation based on aHint 
@@ -8310,17 +8309,17 @@ nsCSSFrameConstructor::GetInsertionPoint
     insertionElement = bindingManager->GetSingleInsertionPoint(container,
                                                                &index,
                                                                &multiple);
     if (multiple && aMultiple)
       *aMultiple = multiple; // Record the fact that filters are in use.
   }
 
   if (insertionElement) {
-    nsIFrame* insertionPoint = mPresShell->GetPrimaryFrameFor(insertionElement);
+    nsIFrame* insertionPoint = insertionElement->GetPrimaryFrame();
     if (insertionPoint) {
       // Use the content insertion frame of the insertion point.
       insertionPoint = insertionPoint->GetContentInsertionFrame();
       if (insertionPoint && insertionPoint != aParentFrame) 
         GetInsertionPoint(insertionPoint, aChildContent, aInsertionPoint, aMultiple);
     }
     else {
       // There was no frame created yet for the insertion point.
@@ -8342,17 +8341,17 @@ nsCSSFrameConstructor::GetInsertionPoint
 }
 
 // Capture state for the frame tree rooted at the frame associated with the
 // content object, aContent
 nsresult
 nsCSSFrameConstructor::CaptureStateForFramesOf(nsIContent* aContent,
                                                nsILayoutHistoryState* aHistoryState)
 {
-  nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
   if (frame == mRootElementFrame) {
     frame = mFixedContainingBlock;
   }
   if (frame) {
     CaptureStateFor(frame, aHistoryState);
   }
   return NS_OK;
 }
@@ -8413,17 +8412,17 @@ FindNextNonWhitespaceSibling(nsIFrame* a
 PRBool
 nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval(nsIFrame* aFrame,
                                                              nsresult* aResult)
 {
   NS_PRECONDITION(aFrame, "Must have a frame");
   NS_PRECONDITION(aFrame->GetParent(), "Frame shouldn't be root");
   NS_PRECONDITION(aResult, "Null out param?");
   NS_PRECONDITION(aFrame == aFrame->GetFirstContinuation(),
-                  "aFrame not the result of GetPrimaryFrameFor()?");
+                  "aFrame not the result of GetPrimaryFrame()?");
 
   if (IsFrameSpecial(aFrame)) {
     // The removal functions can't handle removal of an {ib} split directly; we
     // need to rebuild the containing block.
 #ifdef DEBUG
     if (gNoisyContentUpdates) {
       printf("nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval: "
              "frame=");
@@ -8539,23 +8538,23 @@ nsCSSFrameConstructor::RecreateFramesFor
 
   // Is the frame `special'? If so, we need to reframe the containing
   // block *here*, rather than trying to remove and re-insert the
   // content (which would otherwise result in *two* nested reframe
   // containing block from ContentRemoved() and ContentInserted(),
   // below!).  We'd really like to optimize away one of those
   // containing block reframes, hence the code here.
 
-  nsIFrame* frame = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
   if (frame && frame->IsFrameOfType(nsIFrame::eMathML)) {
     // Reframe the topmost MathML element to prevent exponential blowup
     // (see bug 397518)
     while (PR_TRUE) {
       nsIContent* parentContent = aContent->GetParent();
-      nsIFrame* parentContentFrame = mPresShell->GetPrimaryFrameFor(parentContent);
+      nsIFrame* parentContentFrame = parentContent->GetPrimaryFrame();
       if (!parentContentFrame || !parentContentFrame->IsFrameOfType(nsIFrame::eMathML))
         break;
       aContent = parentContent;
       frame = parentContentFrame;
     }
   }
 
   if (frame) {
@@ -8603,17 +8602,17 @@ nsCSSFrameConstructor::RecreateFramesFor
       }
     }
   }
 
 #ifdef ACCESSIBILITY
   if (mPresShell->IsAccessibilityActive()) {
     PRUint32 changeType;
     if (frame) {
-      nsIFrame *newFrame = mPresShell->GetPrimaryFrameFor(aContent);
+      nsIFrame *newFrame = aContent->GetPrimaryFrame();
       changeType = newFrame ? nsIAccessibilityService::FRAME_SIGNIFICANT_CHANGE :
                               nsIAccessibilityService::FRAME_HIDE;
     }
     else {
       changeType = nsIAccessibilityService::FRAME_SHOW;
     }
 
     // A significant enough change occured that this part
@@ -10997,17 +10996,17 @@ nsCSSFrameConstructor::ProcessOneRestyle
   
   if (!aContent->IsInDoc() ||
       aContent->GetCurrentDoc() != mDocument) {
     // Content node has been removed from our document; nothing else
     // to do here
     return;
   }
   
-  nsIFrame* primaryFrame = mPresShell->GetPrimaryFrameFor(aContent);
+  nsIFrame* primaryFrame = aContent->GetPrimaryFrame();
   if (aRestyleHint & eReStyle_Self) {
     RestyleElement(aContent, primaryFrame, aChangeHint);
   } else if (aChangeHint &&
                (primaryFrame ||
                 (aChangeHint & nsChangeHint_ReconstructFrame))) {
     // Don't need to recompute style; just apply the hint
     nsStyleChangeList changeList;
     changeList.AppendChange(primaryFrame, aContent, aChangeHint);
@@ -11227,17 +11226,18 @@ NS_IMETHODIMP nsCSSFrameConstructor::Res
 }
 
 NS_IMETHODIMP
 nsCSSFrameConstructor::LazyGenerateChildrenEvent::Run()
 {
   mPresShell->GetDocument()->FlushPendingNotifications(Flush_Layout);
 
   // this is hard-coded to handle only menu popup frames
-  nsIFrame* frame = mPresShell->GetPrimaryFrameFor(mContent);
+  nsIFrame* frame = mPresShell->GetPresContext() ?
+    mPresShell->GetPresContext()->GetPrimaryFrameFor(mContent) : nsnull;
   if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
     nsWeakFrame weakFrame(frame);
 #ifdef MOZ_XUL
     // it is possible that the frame is different than the one that requested
     // the lazy generation, but as long as it's a popup frame that hasn't
     // generated its children yet, that's OK.
     nsMenuPopupFrame* menuPopupFrame = static_cast<nsMenuPopupFrame *>(frame);
     if (menuPopupFrame->HasGeneratedChildren()) {
--- a/layout/base/nsCSSRendering.cpp
+++ b/layout/base/nsCSSRendering.cpp
@@ -982,18 +982,17 @@ nsCSSRendering::FindBackgroundStyleFrame
         // will reflow a document with no body.  In particular, if a
         // SCRIPT element in the head blocks the parser and then has a
         // SCRIPT that does "document.location.href = 'foo'", then
         // nsParser::Terminate will call |DidBuildModel| methods
         // through to the content sink, which will call |StartLayout|
         // and thus |InitialReflow| on the pres shell.  See bug 119351
         // for the ugly details.
         if (bodyContent) {
-          nsIFrame *bodyFrame = aForFrame->PresContext()->GetPresShell()->
-            GetPrimaryFrameFor(bodyContent);
+          nsIFrame *bodyFrame = bodyContent->GetPrimaryFrame();
           if (bodyFrame) {
             return nsLayoutUtils::GetStyleFrame(bodyFrame);
           }
         }
       }
     }
   }
 
--- a/layout/base/nsIPresShell.h
+++ b/layout/base/nsIPresShell.h
@@ -217,17 +217,17 @@ public:
    * to PopStackMemory.
    *
    * The allocations cannot be for more than 4044 bytes.
    */
   virtual void PushStackMemory() = 0;
   virtual void PopStackMemory() = 0;
   virtual void* AllocateStackMemory(size_t aSize) = 0;
   
-  nsIDocument* GetDocument() { return mDocument; }
+  nsIDocument* GetDocument() const { return mDocument; }
 
   nsPresContext* GetPresContext() { return mPresContext; }
 
   nsIViewManager* GetViewManager() { return mViewManager; }
 
 #ifdef _IMPL_NS_LAYOUT
   nsStyleSet*  StyleSet() { return mStyleSet; }
 
--- a/layout/base/nsPresShell.cpp
+++ b/layout/base/nsPresShell.cpp
@@ -3073,17 +3073,17 @@ NS_IMETHODIMP
 PresShell::CheckVisibility(nsIDOMNode *node, PRInt16 startOffset, PRInt16 EndOffset, PRBool *_retval)
 {
   if (!node || startOffset>EndOffset || !_retval || startOffset<0 || EndOffset<0)
     return NS_ERROR_INVALID_ARG;
   *_retval = PR_FALSE; //initialize return parameter
   nsCOMPtr<nsIContent> content(do_QueryInterface(node));
   if (!content)
     return NS_ERROR_FAILURE;
-  nsIFrame *frame = GetPrimaryFrameFor(content);
+  nsIFrame *frame = content->GetPrimaryFrame();
   if (!frame) //no frame to look at so it must not be visible
     return NS_OK;  
   //start process now to go through all frames to find startOffset. then check chars after that to see 
   //if anything until EndOffset is visible.
   PRBool finished = PR_FALSE;
   frame->CheckVisibility(mPresContext,startOffset,EndOffset,PR_TRUE,&finished, _retval);
   return NS_OK;//dont worry about other return val
 }
@@ -3441,17 +3441,17 @@ PresShell::GetViewToScroll(nsLayoutUtils
       GetSelection(nsISelectionController::SELECTION_NORMAL);
     if (domSelection) {
       nsCOMPtr<nsIDOMNode> focusedNode;
       domSelection->GetFocusNode(getter_AddRefs(focusedNode));
       focusedContent = do_QueryInterface(focusedNode);
     }
   }
   if (focusedContent) {
-    nsIFrame* startFrame = GetPrimaryFrameFor(focusedContent);
+    nsIFrame* startFrame = focusedContent->GetPrimaryFrame();
     if (startFrame) {
       nsIScrollableViewProvider* svp = do_QueryFrame(startFrame);
       // If this very frame provides a scroll view, start there instead of frame's
       // closest view, because the scroll view may be inside a child frame.
       // For example, this happens in the case of overflow:scroll.
       // In that case we still use GetNearestScrollingView() because
       // we need a scrolling view that matches aDirection.
       nsIScrollableView* sv;
@@ -4122,17 +4122,17 @@ PresShell::ScrollContentIntoView(nsICont
   return NS_OK;
 }
 
 void
 PresShell::DoScrollContentIntoView(nsIContent* aContent,
                                    PRIntn      aVPercent,
                                    PRIntn      aHPercent)
 {
-  nsIFrame* frame = GetPrimaryFrameFor(aContent);
+  nsIFrame* frame = aContent->GetPrimaryFrame();
   if (!frame) {
     mContentToScrollTo = nsnull;
     return;
   }
 
   if (frame->GetStateBits() & NS_FRAME_FIRST_REFLOW) {
     // The reflow flush before this scroll got interrupted, and this frame's
     // coords and size are all zero, and it has no content showing anyway.
@@ -4276,17 +4276,17 @@ PresShell::GetSelectionForCopy(nsISelect
   if (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 = GetPrimaryFrameFor(content);
+      nsIFrame *htmlInputFrame = content->GetPrimaryFrame();
       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;
 
@@ -4381,17 +4381,17 @@ PresShell::ClearMouseCapture(nsIView* aV
       nsIDocument* doc = gCaptureInfo.mContent->GetCurrentDoc();
       if (doc) {
         nsIPresShell *shell = doc->GetPrimaryShell();
         if (shell) {
           // not much can happen if frames are being destroyed so just return.
           if (shell->FrameManager()->IsDestroyingFrames())
             return;
 
-          frame = shell->GetPrimaryFrameFor(gCaptureInfo.mContent);
+          frame = gCaptureInfo.mContent->GetPrimaryFrame();
         }
       }
 
       if (frame) {
         nsIView* view = frame->GetClosestView();
         // if there is no view, capturing won't be handled any more, so
         // just release the capture.
         if (view) {
@@ -5147,17 +5147,20 @@ nsIFrame*
 PresShell::GetPrimaryFrameFor(nsIContent* aContent) const
 {
   return FrameManager()->GetPrimaryFrameFor(aContent, -1);
 }
 
 nsIFrame*
 PresShell::GetRealPrimaryFrameFor(nsIContent* aContent) const
 {
-  nsIFrame *primaryFrame = FrameManager()->GetPrimaryFrameFor(aContent, -1);
+  if (aContent->GetDocument() != GetDocument()) {
+    return nsnull;
+  }
+  nsIFrame *primaryFrame = aContent->GetPrimaryFrame();
   if (!primaryFrame)
     return nsnull;
   return nsPlaceholderFrame::GetRealFrameFor(primaryFrame);
 }
 
 NS_IMETHODIMP
 PresShell::GetPlaceholderFrameFor(nsIFrame*  aFrame,
                                   nsIFrame** aResult) const
@@ -5453,17 +5456,17 @@ PresShell::CreateRangePaintInfo(nsIDOMRa
   else {
     nsINode* ancestor = nsContentUtils::GetCommonAncestor(startParent, endParent);
     NS_ASSERTION(!ancestor || ancestor->IsNodeOfType(nsINode::eCONTENT),
                  "common ancestor is not content");
     if (!ancestor || !ancestor->IsNodeOfType(nsINode::eCONTENT))
       return nsnull;
 
     nsIContent* ancestorContent = static_cast<nsIContent*>(ancestor);
-    ancestorFrame = GetPrimaryFrameFor(ancestorContent);
+    ancestorFrame = ancestorContent->GetPrimaryFrame();
 
     // use the nearest ancestor frame that includes all continuations as the
     // root for building the display list
     while (ancestorFrame &&
            nsLayoutUtils::GetNextContinuationOrSpecialSibling(ancestorFrame))
       ancestorFrame = ancestorFrame->GetParent();
   }
 
@@ -5828,17 +5831,17 @@ PresShell::GetCurrentEventFrame()
   }
     
   if (!mCurrentEventFrame && mCurrentEventContent) {
     // Make sure the content still has a document reference. If not,
     // then we assume it is no longer in the content tree and the
     // frame shouldn't get an event, nor should we even assume its
     // safe to try and find the frame.
     if (mCurrentEventContent->GetDocument()) {
-      mCurrentEventFrame = GetPrimaryFrameFor(mCurrentEventContent);
+      mCurrentEventFrame = mCurrentEventContent->GetPrimaryFrame();
     }
   }
 
   return mCurrentEventFrame;
 }
 
 NS_IMETHODIMP
 PresShell::GetEventTargetFrame(nsIFrame** aFrame)
@@ -6147,17 +6150,21 @@ PresShell::HandleEvent(nsIView         *
       }
 #endif
     }
 
     PRBool captureRetarget = PR_FALSE;
     if (capturingContent) {
       captureRetarget = gCaptureInfo.mRetargetToElement;
       if (!captureRetarget) {
-        nsIFrame* captureFrame = GetPrimaryFrameFor(capturingContent);
+        // A check was already done above to ensure that capturingContent is
+        // in this presshell, so GetPrimaryFrame can just be called directly.
+        NS_ASSERTION(capturingContent->GetCurrentDoc() == GetDocument(),
+                     "Unexpected document");
+        nsIFrame* captureFrame = capturingContent->GetPrimaryFrame();
         if (captureFrame) {
           if (capturingContent->Tag() == nsGkAtoms::select &&
               capturingContent->IsHTML()) {
             // a dropdown <select> has a child in its selectPopupList and we should
             // capture on that instead.
             nsIFrame* childFrame = captureFrame->GetChildList(nsGkAtoms::selectPopupList).FirstChild();
             if (childFrame) {
               captureFrame = childFrame;
@@ -6197,18 +6204,20 @@ PresShell::HandleEvent(nsIView         *
     // capture retargeting is being used, no frame was found or the frame's
     // content is not a descendant of the capturing content.
     if (capturingContent &&
         (gCaptureInfo.mRetargetToElement ||
          !targetFrame || !targetFrame->GetContent() ||
          !nsContentUtils::ContentIsCrossDocDescendantOf(targetFrame->GetContent(),
                                                         capturingContent))) {
       // A check was already done above to ensure that capturingContent is
-      // in this presshell, so GetPrimaryFrameFor can just be called directly.
-      nsIFrame* capturingFrame = GetPrimaryFrameFor(capturingContent);
+      // in this presshell, so GetPrimaryFrame can just be called directly.
+      NS_ASSERTION(capturingContent->GetCurrentDoc() == GetDocument(),
+                   "Unexpected document");
+      nsIFrame* capturingFrame = capturingContent->GetPrimaryFrame();
       if (capturingFrame) {
         targetFrame = capturingFrame;
         aView = targetFrame->GetClosestView();
       }
     }
 
     if (targetFrame) {
       PresShell* shell =
@@ -6728,17 +6737,17 @@ PresShell::PrepareToUseCaretPosition(nsI
     //
     // Doing this call first forces the frame to be in view, eliminating the
     // problem. The only difference in the result is that if your cursor is in
     // an edit box below the current view, you'll get the edit box aligned with
     // the top of the window. This is arguably better behavior anyway.
     rv = ScrollContentIntoView(content, NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE,
                                         NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE);
     NS_ENSURE_SUCCESS(rv, PR_FALSE);
-    frame = GetPrimaryFrameFor(content);
+    frame = content->GetPrimaryFrame();
     NS_WARN_IF_FALSE(frame, "No frame for focused content?");
   }
 
   // Actually scroll the selection (ie caret) into view. Note that this must
   // be synchronous since we will be checking the caret position on the screen.
   //
   // Be easy about errors, and just don't scroll in those cases. Better to have
   // the correct menu at a weird place than the wrong menu.
@@ -6831,17 +6840,17 @@ PresShell::GetCurrentItemAndPositionForE
           if (cols) {
             nsCOMPtr<nsITreeColumn> col;
             cols->GetFirstColumn(getter_AddRefs(col));
             if (col) {
               nsCOMPtr<nsIDOMElement> colElement;
               col->GetElement(getter_AddRefs(colElement));
               nsCOMPtr<nsIContent> colContent(do_QueryInterface(colElement));
               if (colContent) {
-                nsIFrame* frame = GetPrimaryFrameFor(colContent);
+                nsIFrame* frame = colContent->GetPrimaryFrame();
                 if (frame) {
                   extraTreeY = frame->GetSize().height;
                 }
               }
             }
           }
         }
         else {
@@ -6861,17 +6870,17 @@ PresShell::GetCurrentItemAndPositionForE
         select->GetSelectedItem(getter_AddRefs(item));
     }
   }
 
   if (item)
     focusedContent = do_QueryInterface(item);
 #endif
 
-  nsIFrame *frame = GetPrimaryFrameFor(focusedContent);
+  nsIFrame *frame = focusedContent->GetPrimaryFrame();
   if (frame) {
     nsPoint frameOrigin(0, 0);
 
     // Get the frame's origin within its view
     nsIView *view = frame->GetClosestView(&frameOrigin);
     NS_ASSERTION(view, "No view for frame");
 
     // View's origin within the view manager tree
@@ -6973,20 +6982,19 @@ PresShell::AddOverrideStyleSheet(nsIStyl
 
 nsresult
 PresShell::RemoveOverrideStyleSheet(nsIStyleSheet *aSheet)
 {
   return mStyleSet->RemoveStyleSheet(nsStyleSet::eOverrideSheet, aSheet);
 }
 
 static void
-FreezeElement(nsIContent *aContent, void *aShell)
-{
-  nsIPresShell* shell = static_cast<nsIPresShell*>(aShell);
-  nsIFrame *frame = shell->FrameManager()->GetPrimaryFrameFor(aContent, -1);
+FreezeElement(nsIContent *aContent, void * /* unused */)
+{
+  nsIFrame *frame = aContent->GetPrimaryFrame();
   nsIObjectFrame *objectFrame = do_QueryFrame(frame);
   if (objectFrame) {
     objectFrame->StopPlugin();
   }
 }
 
 static PRBool
 FreezeSubDocument(nsIDocument *aDocument, void *aData)
@@ -6998,17 +7006,17 @@ FreezeSubDocument(nsIDocument *aDocument
   return PR_TRUE;
 }
 
 void
 PresShell::Freeze()
 {
   MaybeReleaseCapturingContent();
 
-  mDocument->EnumerateFreezableElements(FreezeElement, this);
+  mDocument->EnumerateFreezableElements(FreezeElement, nsnull);
 
   if (mCaret)
     mCaret->SetCaretVisible(PR_FALSE);
 
   mPaintingSuppressed = PR_TRUE;
 
   if (mDocument)
     mDocument->EnumerateSubDocuments(FreezeSubDocument, nsnull);
--- a/layout/forms/nsIsIndexFrame.cpp
+++ b/layout/forms/nsIsIndexFrame.cpp
@@ -141,20 +141,19 @@ nsIsIndexFrame::UpdatePromptLabel(PRBool
   mTextContent->SetText(prompt, aNotify);
 
   return NS_OK;
 }
 
 nsresult
 nsIsIndexFrame::GetInputFrame(nsIFormControlFrame** oFrame)
 {
-  nsIPresShell *presShell = PresContext()->GetPresShell();
   if (!mInputContent) NS_WARNING("null content - cannot restore state");
-  if (presShell && mInputContent) {
-    nsIFrame *frame = presShell->GetPrimaryFrameFor(mInputContent);
+  if (mInputContent) {
+    nsIFrame *frame = mInputContent->GetPrimaryFrame();
     if (frame) {
       *oFrame = do_QueryFrame(frame);
       return *oFrame ? NS_OK : NS_NOINTERFACE;
     }
   }
   return NS_OK;
 }
 
--- a/layout/forms/nsListControlFrame.cpp
+++ b/layout/forms/nsListControlFrame.cpp
@@ -274,19 +274,16 @@ void nsListControlFrame::PaintFocus(nsIR
     focusedIndex = GetSelectedIndex();
   } else {
     focusedIndex = mEndSelectionIndex;
   }
 
   nsPresContext* presContext = PresContext();
   if (!GetScrollableView()) return;
 
-  nsIPresShell *presShell = presContext->GetPresShell();
-  if (!presShell) return;
-
   nsIFrame* containerFrame = GetOptionsContainer();
   if (!containerFrame) return;
 
   nsIFrame * childframe = nsnull;
   nsresult result = NS_ERROR_FAILURE;
 
   nsCOMPtr<nsIContent> focusedContent;
 
@@ -296,17 +293,17 @@ void nsListControlFrame::PaintFocus(nsIR
   nsCOMPtr<nsISelectElement> selectElement(do_QueryInterface(mContent));
   NS_ASSERTION(selectElement, "Can't be null");
 
   // If we have a selected index then get that child frame
   if (focusedIndex != kNothingSelected) {
     focusedContent = GetOptionContent(focusedIndex);
     // otherwise we find the focusedContent's frame and scroll to it
     if (focusedContent) {
-      childframe = presShell->GetPrimaryFrameFor(focusedContent);
+      childframe = focusedContent->GetPrimaryFrame();
     }
   } else {
     nsCOMPtr<nsIDOMHTMLSelectElement> selectHTMLElement(do_QueryInterface(mContent));
     NS_ASSERTION(selectElement, "Can't be null");
 
     // Since there isn't a selected item we need to show a focus ring around the first
     // non-disabled item and skip all the option group elements (nodes)
     nsCOMPtr<nsIDOMNode> node;
@@ -332,17 +329,17 @@ void nsListControlFrame::PaintFocus(nsIR
       if (!node) {
         return;
       }
     }
 
     // if we found a node use it, if not get the first child (this is for empty selects)
     if (node) {
       focusedContent = do_QueryInterface(node);
-      childframe = presShell->GetPrimaryFrameFor(focusedContent);
+      childframe = focusedContent->GetPrimaryFrame();
     }
     if (!childframe) {
       // Failing all else, try the first thing we have, but only if
       // it's an element.  Text frames need not apply.
       childframe = containerFrame->GetFirstChild(nsnull);
       if (childframe &&
           !childframe->GetContent()->IsNodeOfType(nsINode::eELEMENT)) {
         childframe = nsnull;
@@ -471,28 +468,27 @@ GetNumberOfOptionsRecursive(nsIContent* 
     else if (::IsOptGroup(child)) {
       optionCount += ::GetNumberOfOptionsRecursive(child);
     }
   }
   return optionCount;
 }
 
 static nscoord
-GetOptGroupLabelsHeight(nsPresContext* aPresContext,
-                        nsIContent*    aContent,
+GetOptGroupLabelsHeight(nsIContent*    aContent,
                         nscoord        aRowHeight)
 {
   nscoord height = 0;
   const PRUint32 childCount = aContent ? aContent->GetChildCount() : 0;
   for (PRUint32 index = 0; index < childCount; ++index) {
     nsIContent* child = aContent->GetChildAt(index);
     if (::IsOptGroup(child)) {
       PRUint32 numOptions = ::GetNumberOfOptionsRecursive(child);
       nscoord optionsHeight = aRowHeight * numOptions;
-      nsIFrame* frame = aPresContext->GetPresShell()->GetPrimaryFrameFor(child);
+      nsIFrame* frame = child->GetPrimaryFrame();
       nscoord totalHeight = frame ? frame->GetSize().height : 0;
       height += NS_MAX(0, totalHeight - optionsHeight);
     }
   }
   return height;
 }
 
 //-----------------------------------------------------------------
@@ -1928,18 +1924,17 @@ nsListControlFrame::CalcIntrinsicHeight(
 
   // Extra height to tack on to aHeightOfARow * mNumDisplayRows
   nscoord extraHeight = 0;
   
   if (mNumDisplayRows < 1) {
     // When SIZE=0 or unspecified we constrain the height to
     // [2..kMaxDropDownRows] rows.  We add in the height of optgroup labels
     // (within the constraint above), bug 300474.
-    nscoord labelHeight =
-      ::GetOptGroupLabelsHeight(PresContext(), mContent, aHeightOfARow);
+    nscoord labelHeight = ::GetOptGroupLabelsHeight(mContent, aHeightOfARow);
 
     if (GetMultiple()) {
       if (aNumberOfOptions < 2) {
         // Add in 1 aHeightOfARow also when aNumberOfOptions == 0
         mNumDisplayRows = 1;
         extraHeight = NS_MAX(aHeightOfARow, labelHeight);
       }
       else if (aNumberOfOptions * aHeightOfARow + labelHeight >
@@ -2131,42 +2126,41 @@ nsListControlFrame::GetIndexFromDOMEvent
     GetEventTargetContent(nsnull, getter_AddRefs(content));
 
   nsCOMPtr<nsIContent> optionContent = GetOptionFromContent(content);
   if (optionContent) {
     aCurIndex = GetIndexFromContent(optionContent);
     return NS_OK;
   }
 
-  nsIPresShell *presShell = PresContext()->PresShell();
   PRInt32 numOptions = GetNumberOfOptions();
   if (numOptions < 1)
     return NS_ERROR_FAILURE;
 
   nsPoint pt = nsLayoutUtils::GetDOMEventCoordinatesRelativeTo(aMouseEvent, this);
 
   // If the event coordinate is above the first option frame, then target the
   // first option frame
   nsCOMPtr<nsIContent> firstOption = GetOptionContent(0);
   NS_ASSERTION(firstOption, "Can't find first option that's supposed to be there");
-  nsIFrame* optionFrame = presShell->GetPrimaryFrameFor(firstOption);
+  nsIFrame* optionFrame = firstOption->GetPrimaryFrame();
   if (optionFrame) {
     nsPoint ptInOptionFrame = pt - optionFrame->GetOffsetTo(this);
     if (ptInOptionFrame.y < 0 && ptInOptionFrame.x >= 0 &&
         ptInOptionFrame.x < optionFrame->GetSize().width) {
       aCurIndex = 0;
       return NS_OK;
     }
   }
 
   nsCOMPtr<nsIContent> lastOption = GetOptionContent(numOptions - 1);
   // If the event coordinate is below the last option frame, then target the
   // last option frame
   NS_ASSERTION(lastOption, "Can't find last option that's supposed to be there");
-  optionFrame = presShell->GetPrimaryFrameFor(lastOption);
+  optionFrame = lastOption->GetPrimaryFrame();
   if (optionFrame) {
     nsPoint ptInOptionFrame = pt - optionFrame->GetOffsetTo(this);
     if (ptInOptionFrame.y >= optionFrame->GetSize().height && ptInOptionFrame.x >= 0 &&
         ptInOptionFrame.x < optionFrame->GetSize().width) {
       aCurIndex = numOptions - 1;
       return NS_OK;
     }
   }
@@ -2322,20 +2316,19 @@ nsListControlFrame::ScrollToFrame(nsICon
   if (scrollableView) {
     // if null is passed in we scroll to 0,0
     if (nsnull == aOptElement) {
       scrollableView->ScrollTo(0, 0, 0);
       return NS_OK;
     }
   
     // otherwise we find the content's frame and scroll to it
-    nsIPresShell *presShell = PresContext()->PresShell();
     nsIFrame * childframe;
     if (aOptElement) {
-      childframe = presShell->GetPrimaryFrameFor(aOptElement);
+      childframe = aOptElement->GetPrimaryFrame();
     } else {
       return NS_ERROR_FAILURE;
     }
 
     if (childframe) {
       if (scrollableView) {
         nscoord x;
         nscoord y;
@@ -2358,17 +2351,17 @@ nsListControlFrame::ScrollToFrame(nsICon
         // this checks to see the parent is an optgroup
         // and then adds in the parent's y coord
         // XXX this assume only one level of nesting of optgroups
         //   which is all the spec specifies at the moment.
         nsCOMPtr<nsIContent> parentContent = aOptElement->GetParent();
         nsCOMPtr<nsIDOMHTMLOptGroupElement> optGroup(do_QueryInterface(parentContent));
         nsRect optRect(0,0,0,0);
         if (optGroup) {
-          nsIFrame * optFrame = presShell->GetPrimaryFrameFor(parentContent);
+          nsIFrame * optFrame = parentContent->GetPrimaryFrame();
           if (optFrame) {
             optRect = optFrame->GetRect();
           }
         }
         fRect.y += optRect.y;
 
         // See if the selected frame (fRect) is inside the scrolled
         // area (rect). Check only the vertical dimension. Don't
--- a/layout/generic/nsFrame.cpp
+++ b/layout/generic/nsFrame.cpp
@@ -753,23 +753,20 @@ nsFrameList
 nsFrame::GetChildList(nsIAtom* aListName) const
 {
   return nsFrameList::EmptyList();
 }
 
 static nsIFrame*
 GetActiveSelectionFrame(nsPresContext* aPresContext, nsIFrame* aFrame)
 {
-  nsIPresShell* shell = aPresContext->GetPresShell(); 
-  if (shell) {
-    nsIContent* capturingContent = nsIPresShell::GetCapturingContent();
-    if (capturingContent) {
-      nsIFrame* activeFrame = shell->GetPrimaryFrameFor(capturingContent);
-      return activeFrame ? activeFrame : aFrame;
-    }
+  nsIContent* capturingContent = nsIPresShell::GetCapturingContent();
+  if (capturingContent) {
+    nsIFrame* activeFrame = aPresContext->GetPrimaryFrameFor(capturingContent);
+    return activeFrame ? activeFrame : aFrame;
   }
 
   return aFrame;
 }
 
 PRInt16
 nsFrame::DisplaySelection(nsPresContext* aPresContext, PRBool isOkToTurnOn)
 {
--- a/layout/generic/nsFrameFrame.cpp
+++ b/layout/generic/nsFrameFrame.cpp
@@ -561,17 +561,17 @@ nsSubDocumentFrame::Reflow(nsPresContext
   DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
   // printf("OuterFrame::Reflow %X (%d,%d) \n", this, aReflowState.availableWidth, aReflowState.availableHeight);
   NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS,
      ("enter nsSubDocumentFrame::Reflow: maxSize=%d,%d",
       aReflowState.availableWidth, aReflowState.availableHeight));
 
   aStatus = NS_FRAME_COMPLETE;
 
-  NS_ASSERTION(aPresContext->GetPresShell()->GetPrimaryFrameFor(mContent) == this,
+  NS_ASSERTION(mContent->GetPrimaryFrame() == this,
                "Shouldn't happen");
 
   // "offset" is the offset of our content area from our frame's
   // top-left corner.
   nsPoint offset(0, 0);
   
   if (IsInline()) {
     // XUL <iframe> or <browser>, or HTML <iframe>, <object> or <embed>
--- a/layout/generic/nsGfxScrollFrame.cpp
+++ b/layout/generic/nsGfxScrollFrame.cpp
@@ -2147,18 +2147,17 @@ nsGfxScrollFrameInner::IsLTR() const
     nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(document);
     if (htmlDoc) {
       nsIContent *bodyContent = htmlDoc->GetBodyContentExternal();
       if (bodyContent)
         root = bodyContent; // we can trust the document to hold on to it
     }
 
     if (root) {
-      nsIFrame *rootsFrame =
-        presContext->PresShell()->GetPrimaryFrameFor(root);
+      nsIFrame *rootsFrame = root->GetPrimaryFrame();
       if (rootsFrame)
         frame = rootsFrame;
     }
   }
 
   return frame->GetStyleVisibility()->mDirection != NS_STYLE_DIRECTION_RTL;
 }
 
--- a/layout/generic/nsObjectFrame.cpp
+++ b/layout/generic/nsObjectFrame.cpp
@@ -4902,29 +4902,18 @@ DepthOfVisual(const Screen* screen, cons
   return 0;
 }
 #endif
 
 #if defined(MOZ_PLATFORM_HILDON) && defined(MOZ_WIDGET_GTK2)
 
 static GdkWindow* GetClosestWindow(nsIDOMElement *element)
 {
-  nsCOMPtr<nsIDOMDocument> domDocument;
-  element->GetOwnerDocument(getter_AddRefs(domDocument));
-
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDocument);  
-  if (!doc)
-    return nsnull;
-
-  nsIPresShell *presShell = doc->GetPrimaryShell();
-  if (!presShell)
-    return nsnull;
-
   nsCOMPtr<nsIContent> content = do_QueryInterface(element);
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame* frame = content->GetPrimaryFrame();
   if (!frame)
     return nsnull;
 
   nsIWidget* win = frame->GetWindow();
   if (!win)
     return nsnull;
 
   GdkWindow* w = static_cast<GdkWindow*>(win->GetNativeData(NS_NATIVE_WINDOW));
--- a/layout/generic/nsSelection.cpp
+++ b/layout/generic/nsSelection.cpp
@@ -462,17 +462,17 @@ public:
     mDelay = aDelay;
     return NS_OK;
   }
 
   NS_IMETHOD Notify(nsITimer *timer)
   {
     if (mSelection && mPresContext)
     {
-      nsWeakFrame frame = mPresContext->PresShell()->GetPrimaryFrameFor(mContent);
+      nsWeakFrame frame = mPresContext->GetPrimaryFrameFor(mContent);
       mContent = nsnull;
 
       mFrameSelection->HandleDrag(frame, mPoint);
 
       if (!frame.IsAlive())
         return NS_OK;
 
       nsIFrame* checkFrame = frame;
@@ -963,17 +963,17 @@ nsFrameSelection::ConstrainFrameAndPoint
   }
 
   //
   // When we can't find a frame which is under the mouse cursor and has a same
   // selection root as the anchor node's, we should return the selection root
   // frame.
   //
 
-  *aRetFrame = mShell->GetPrimaryFrameFor(anchorRoot);
+  *aRetFrame = anchorRoot->GetPrimaryFrame();
 
   if (!*aRetFrame)
     return NS_ERROR_FAILURE;
 
   //
   // Now make sure that aRetPoint is converted to the same coordinate
   // system used by aRetFrame.
   //
@@ -2085,17 +2085,17 @@ nsFrameSelection::GetFrameForNodeOffset(
           *aReturnOffset = (PRInt32)textLength;
         }
         else
           *aReturnOffset = 0;
       }
     }
   }
   
-  nsIFrame* returnFrame = mShell->GetPrimaryFrameFor(theNode);
+  nsIFrame* returnFrame = theNode->GetPrimaryFrame();
   if (!returnFrame)
     return nsnull;
 
   // find the child frame containing the offset we want
   returnFrame->GetChildFrameContainingOffset(*aReturnOffset, aHint,
                                              &aOffset, &returnFrame);
   return returnFrame;
 }
@@ -2298,35 +2298,27 @@ static PRBool IsCell(nsIContent *aConten
            aContent->Tag() == nsGkAtoms::th) &&
           aContent->IsHTML());
 }
 
 nsITableCellLayout* 
 nsFrameSelection::GetCellLayout(nsIContent *aCellContent) const
 {
   NS_ENSURE_TRUE(mShell, nsnull);
-  // Get frame for cell
-  nsIFrame *cellFrame = mShell->GetPrimaryFrameFor(aCellContent);
-  if (!cellFrame)
-    return nsnull;
-
-  nsITableCellLayout *cellLayoutObject = do_QueryFrame(cellFrame);
+  nsITableCellLayout *cellLayoutObject =
+    do_QueryFrame(aCellContent->GetPrimaryFrame());
   return cellLayoutObject;
 }
 
 nsITableLayout* 
 nsFrameSelection::GetTableLayout(nsIContent *aTableContent) const
 {
   NS_ENSURE_TRUE(mShell, nsnull);
-  // Get frame for table
-  nsIFrame *tableFrame = mShell->GetPrimaryFrameFor(aTableContent);
-  if (!tableFrame)
-    return nsnull;
-
-  nsITableLayout *tableLayoutObject = do_QueryFrame(tableFrame);
+  nsITableLayout *tableLayoutObject =
+    do_QueryFrame(aTableContent->GetPrimaryFrame());
   return tableLayoutObject;
 }
 
 nsresult
 nsFrameSelection::ClearNormalSelection()
 {
   PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
   if (!mDomSelections[index])
@@ -2484,18 +2476,17 @@ printf("HandleTableSelection: Mouse down
         // Check if we have other selected cells
         nsIContent* previousCellNode =
           GetFirstSelectedContent(GetFirstCellRange());
         if (previousCellNode)
         {
           // We have at least 1 other selected cell
 
           // Check if new cell is already selected
-          NS_ENSURE_STATE(mShell);
-          nsIFrame  *cellFrame = mShell->GetPrimaryFrameFor(childContent);
+          nsIFrame  *cellFrame = childContent->GetPrimaryFrame();
           if (!cellFrame) return NS_ERROR_NULL_POINTER;
           result = cellFrame->GetSelected(&isSelected);
           if (NS_FAILED(result)) return result;
         }
         else
         {
           // No cells selected -- remove non-cell selection
           mDomSelections[index]->RemoveAllRanges();
@@ -4195,17 +4186,17 @@ nsTypedSelection::GetPrimaryFrameForRang
     {
       nsIContent *child = content->GetChildAt(aOffset);
       if (!child) //out of bounds?
         return NS_ERROR_FAILURE;
 
       content = child; // releases the focusnode
     }
   }
-  *aReturnFrame = mFrameSelection->GetShell()->GetPrimaryFrameFor(content);
+  *aReturnFrame = content->GetPrimaryFrame();
   return NS_OK;
 }
 #endif
 
 
 NS_IMETHODIMP
 nsTypedSelection::GetPrimaryFrameForAnchorNode(nsIFrame **aReturnFrame)
 {
@@ -4281,17 +4272,17 @@ nsTypedSelection::SelectAllFramesForCont
   nsresult result;
   if (!aInnerIter)
     return NS_ERROR_NULL_POINTER;
   result = aInnerIter->Init(aContent);
   nsIFrame *frame;
   if (NS_SUCCEEDED(result))
   {
     // First select frame of content passed in
-    frame = shell->GetPrimaryFrameFor(aContent);
+    frame = aContent->GetPrimaryFrame();
     if (frame)
     {
       frame->SetSelected(aSelected, mType);
       if (mFrameSelection->GetTableCellSelection())
       {
         nsITableCellLayout *tcl = do_QueryFrame(frame);
         if (tcl)
         {
@@ -4300,17 +4291,17 @@ nsTypedSelection::SelectAllFramesForCont
       }
     }
     // Now iterated through the child frames and set them
     while (!aInnerIter->IsDone())
     {
       nsCOMPtr<nsIContent> innercontent =
         do_QueryInterface(aInnerIter->GetCurrentNode());
 
-      frame = shell->GetPrimaryFrameFor(innercontent);
+      frame = innercontent->GetPrimaryFrame();
       if (frame)
       {
         frame->SetSelected(aSelected, mType);
       }
 
       aInnerIter->Next();
     }
 
@@ -4359,20 +4350,19 @@ nsTypedSelection::selectFrames(nsPresCon
     // loop through the content iterator for each content node
     // for each text node, call SetSelected on it:
     nsCOMPtr<nsIContent> content = do_QueryInterface(aRange->GetStartParent());
 
     // we must call first one explicitly
     if (!content)
       return NS_ERROR_UNEXPECTED;
 
-    nsIFrame *frame;
     if (content->IsNodeOfType(nsINode::eTEXT))
     {
-      frame = presShell->GetPrimaryFrameFor(content);
+      nsIFrame* frame = content->GetPrimaryFrame();
       // The frame could be an SVG text frame, in which case we'll ignore
       // it.
       if (frame && frame->GetType() == nsGkAtoms::textFrame)
       {
         nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
         PRUint32 startOffset = aRange->StartOffset();
         PRUint32 endOffset;
         if (aRange->GetEndParent() == content) {
@@ -4399,17 +4389,17 @@ nsTypedSelection::selectFrames(nsPresCon
     if (aRange->GetEndParent() != aRange->GetStartParent())
     {
       content = do_QueryInterface(aRange->GetEndParent(), &result);
       if (NS_FAILED(result) || !content)
         return result;
 
       if (content->IsNodeOfType(nsINode::eTEXT))
       {
-        frame = presShell->GetPrimaryFrameFor(content);
+        nsIFrame* frame = content->GetPrimaryFrame();
         // The frame could be an SVG text frame, in which case we'll
         // ignore it.
         if (frame && frame->GetType() == nsGkAtoms::textFrame)
         {
           nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
           textFrame->SetSelectedRange(0, aRange->EndOffset(), aFlags, mType);
         }
       }
--- a/layout/inspector/src/inDOMUtils.cpp
+++ b/layout/inspector/src/inDOMUtils.cpp
@@ -95,23 +95,17 @@ inDOMUtils::IsIgnorableWhitespace(nsIDOM
 
   nsCOMPtr<nsIDOMWindowInternal> win = inLayoutUtils::GetWindowFor(aDataNode);
   if (!win) {
     // Hmm.  Things are screwy if we have no window...
     NS_ERROR("No window!");
     return NS_OK;
   }
 
-  nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(win);
-  if (!presShell) {
-    // Display:none iframe or something... Bail out
-    return NS_OK;
-  }
-
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(content);
+  nsIFrame* frame = content->GetPrimaryFrame();
   if (frame) {
     const nsStyleText* text = frame->GetStyleText();
     *aReturn = !text->WhiteSpaceIsSignificant();
   }
   else {
     // empty inter-tag text node without frame, e.g., in between <table>\n<tr>
     *aReturn = PR_TRUE;
   }
--- a/layout/inspector/src/inFlasher.cpp
+++ b/layout/inspector/src/inFlasher.cpp
@@ -128,38 +128,34 @@ inFlasher::SetInvert(PRBool aInvert)
   mInvert = aInvert;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 inFlasher::RepaintElement(nsIDOMElement* aElement)
 {
   NS_ENSURE_ARG_POINTER(aElement);
-  nsCOMPtr<nsIDOMWindowInternal> window = inLayoutUtils::GetWindowFor(aElement);
-  if (!window) return NS_OK;
-  nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window);
-  if (!presShell) return NS_OK;
-  nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement, presShell);
+  nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement);
   if (!frame) return NS_OK;
 
   frame->Invalidate(frame->GetRect());
 
   return NS_OK;
 }
 
 NS_IMETHODIMP 
 inFlasher::DrawElementOutline(nsIDOMElement* aElement)
 {
   NS_ENSURE_ARG_POINTER(aElement);
   nsCOMPtr<nsIDOMWindowInternal> window = inLayoutUtils::GetWindowFor(aElement);
   if (!window) return NS_OK;
   nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window);
   if (!presShell) return NS_OK;
 
-  nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement, presShell);
+  nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement);
 
   PRBool isFirstFrame = PR_TRUE;
 
   while (frame) {
     nsCOMPtr<nsIRenderingContext> rcontext;
     nsresult rv =
       presShell->CreateRenderingContext(frame, getter_AddRefs(rcontext));
     NS_ENSURE_SUCCESS(rv, rv);
--- a/layout/inspector/src/inLayoutUtils.cpp
+++ b/layout/inspector/src/inLayoutUtils.cpp
@@ -79,20 +79,20 @@ inLayoutUtils::GetPresShellFor(nsISuppor
   nsCOMPtr<nsIPresShell> presShell;
   window->GetDocShell()->GetPresShell(getter_AddRefs(presShell));
 
   return presShell;
 }
 
 /*static*/
 nsIFrame*
-inLayoutUtils::GetFrameFor(nsIDOMElement* aElement, nsIPresShell* aShell)
+inLayoutUtils::GetFrameFor(nsIDOMElement* aElement)
 {
   nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
-  return aShell->GetPrimaryFrameFor(content);
+  return content->GetPrimaryFrame();
 }
 
 nsIEventStateManager*
 inLayoutUtils::GetEventStateManagerFor(nsIDOMElement *aElement)
 {
   NS_PRECONDITION(aElement, "Passing in a null element is bad");
 
   nsCOMPtr<nsIDOMDocument> domDoc;
--- a/layout/inspector/src/inLayoutUtils.h
+++ b/layout/inspector/src/inLayoutUtils.h
@@ -49,16 +49,16 @@
 #include "nsBindingManager.h"
 
 class inLayoutUtils
 {
 public:
   static nsIDOMWindowInternal* GetWindowFor(nsIDOMNode* aNode);
   static nsIDOMWindowInternal* GetWindowFor(nsIDOMDocument* aDoc);
   static nsIPresShell* GetPresShellFor(nsISupports* aThing);
-  static nsIFrame* GetFrameFor(nsIDOMElement* aElement, nsIPresShell* aShell);
+  static nsIFrame* GetFrameFor(nsIDOMElement* aElement);
   static nsIEventStateManager* GetEventStateManagerFor(nsIDOMElement *aElement);
   static nsBindingManager* GetBindingManagerFor(nsIDOMNode* aNode);
   static nsIDOMDocument* GetSubDocumentFor(nsIDOMNode* aNode);
   static nsIDOMNode* GetContainerFor(nsIDOMDocument* aDoc);
 };
 
 #endif // __inLayoutUtils_h__
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -1812,18 +1812,17 @@ nsPrintEngine::SetupToPrintContent()
 // all the frame locations of the sub-docs
 nsresult
 nsPrintEngine::ReflowDocList(nsPrintObject* aPO, PRBool aSetPixelScale)
 {
   NS_ENSURE_ARG_POINTER(aPO);
 
   // Check to see if the subdocument's element has been hidden by the parent document
   if (aPO->mParent && aPO->mParent->mPresShell) {
-    nsIFrame * frame =
-      aPO->mParent->mPresShell->GetPrimaryFrameFor(aPO->mContent);
+    nsIFrame * frame = aPO->mContent->GetPrimaryFrame();
     if (frame) {
       if (!frame->GetStyleVisibility()->IsVisible()) {
         aPO->mDontPrint = PR_TRUE;
         aPO->mInvisible = PR_TRUE;
         return NS_OK;
       }
     }
   }
@@ -1867,20 +1866,17 @@ nsPrintEngine::ReflowPrintObject(nsPrint
 
   nsSize adjSize;
   PRBool documentIsTopLevel;
   nsIFrame* frame = nsnull;
   if (!aPO->IsPrintable())
     return NS_OK;
 
   if (aPO->mParent && aPO->mParent->IsPrintable()) {
-    if (aPO->mParent->mPresShell) {
-      frame = aPO->mParent->mPresShell->FrameManager()->
-        GetPrimaryFrameFor(aPO->mContent, -1);
-    }
+    frame = aPO->mContent->GetPrimaryFrame();
     // Without a frame, this document can't be displayed; therefore, there is no
     // point to reflowing it
     if (!frame) {
       aPO->mDontPrint = PR_TRUE;
       return NS_OK;
     }
 
     adjSize = frame->GetContentRect().Size();
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -349,17 +349,17 @@ nsComputedDOMStyle::GetStyleContextForCo
   // If there's no pres shell, get it from the content
   if (!aPresShell) {
     aPresShell = GetPresShellForContent(aContent);
     if (!aPresShell)
       return nsnull;
   }
 
   if (!aPseudo) {
-    nsIFrame* frame = aPresShell->GetPrimaryFrameFor(aContent);
+    nsIFrame* frame = aContent->GetPrimaryFrame();
     if (frame) {
       nsStyleContext* result =
         nsLayoutUtils::GetStyleFrame(frame)->GetStyleContext();
       // Don't use the style context if it was influenced by
       // pseudo-elements, since then it's not the primary style
       // for this element.
       if (!result->HasPseudoElementData()) {
         // this function returns an addrefed style context
@@ -448,17 +448,17 @@ nsComputedDOMStyle::GetPropertyCSSValue(
 #ifdef DEBUG
   mFlushedPendingReflows = propEntry->mNeedsLayoutFlush;
 #endif
 
   mPresShell = document->GetPrimaryShell();
   NS_ENSURE_TRUE(mPresShell && mPresShell->GetPresContext(),
                  NS_ERROR_NOT_AVAILABLE);
 
-  mOuterFrame = mPresShell->GetPrimaryFrameFor(mContent);
+  mOuterFrame = mContent->GetPrimaryFrame();
   mInnerFrame = mOuterFrame;
   if (mOuterFrame && !mPseudo) {
     nsIAtom* type = mOuterFrame->GetType();
     if (type == nsGkAtoms::tableOuterFrame) {
       // If the frame is an outer table frame then we should get the style
       // from the inner table frame.
       mInnerFrame = mOuterFrame->GetFirstChild(nsnull);
       NS_ASSERTION(mInnerFrame, "Outer table must have an inner");
--- a/layout/style/nsHTMLStyleSheet.cpp
+++ b/layout/style/nsHTMLStyleSheet.cpp
@@ -405,17 +405,17 @@ static nsresult GetBodyColor(nsPresConte
 {
   nsIPresShell *shell = aPresContext->PresShell();
   nsCOMPtr<nsIDOMHTMLDocument> domdoc = do_QueryInterface(shell->GetDocument());
   if (!domdoc)
     return NS_ERROR_FAILURE;
   nsCOMPtr<nsIDOMHTMLElement> body;
   domdoc->GetBody(getter_AddRefs(body));
   nsCOMPtr<nsIContent> bodyContent = do_QueryInterface(body);
-  nsIFrame *bodyFrame = shell->GetPrimaryFrameFor(bodyContent);
+  nsIFrame *bodyFrame = bodyContent->GetPrimaryFrame();
   if (!bodyFrame)
     return NS_ERROR_FAILURE;
   *aColor = bodyFrame->GetStyleColor()->mColor;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsHTMLStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
--- a/layout/svg/base/src/nsSVGEffects.cpp
+++ b/layout/svg/base/src/nsSVGEffects.cpp
@@ -85,17 +85,17 @@ nsSVGRenderingObserver::GetReferencedFra
     }
     return mReferencedFrame;
   }
 
   if (mElement.get()) {
     nsIDocument* doc = mElement.get()->GetCurrentDoc();
     nsIPresShell* shell = doc ? doc->GetPrimaryShell() : nsnull;
     if (shell && !shell->FrameManager()->IsDestroyingFrames()) {
-      nsIFrame* frame = shell->GetPrimaryFrameFor(mElement.get());
+      nsIFrame* frame = mElement.get()->GetPrimaryFrame();
       if (frame) {
         mReferencedFrame = frame;
         mReferencedFramePresShell = shell;
         nsSVGEffects::AddRenderingObserver(mReferencedFrame, this);
         return frame;
       }
     }
   }
--- a/layout/svg/base/src/nsSVGOuterSVGFrame.cpp
+++ b/layout/svg/base/src/nsSVGOuterSVGFrame.cpp
@@ -90,17 +90,17 @@ nsSVGMutationObserver::AttributeChanged(
 {
   if (aNameSpaceID != kNameSpaceID_XML || aAttribute != nsGkAtoms::space) {
     return;
   }
 
   nsPresShellIterator iter(aDocument);
   nsCOMPtr<nsIPresShell> shell;
   while ((shell = iter.GetNextShell())) {
-    nsIFrame *frame = shell->GetPrimaryFrameFor(aContent);
+    nsIFrame *frame = aContent->GetPrimaryFrame();
     if (!frame) {
       continue;
     }
 
     // is the content a child of a text element
     nsSVGTextContainerFrame *containerFrame = do_QueryFrame(frame);
     if (containerFrame) {
       containerFrame->NotifyGlyphMetricsChange();
--- a/layout/svg/base/src/nsSVGUtils.cpp
+++ b/layout/svg/base/src/nsSVGUtils.cpp
@@ -552,25 +552,22 @@ nsSVGUtils::GetCTM(nsSVGElement *aElemen
     return matrix * GetCTM(static_cast<nsSVGElement*>(ancestor), PR_TRUE);
   }
   // XXX this does not take into account CSS transform, or that the non-SVG
   // content that we've hit may itself be inside an SVG foreignObject higher up
   float x = 0.0f, y = 0.0f;
   if (currentDoc && element->NodeInfo()->Equals(nsGkAtoms::svg, kNameSpaceID_SVG)) {
     nsIPresShell *presShell = currentDoc->GetPrimaryShell();
     if (presShell) {
-      nsPresContext *context = presShell->GetPresContext();
-      if (context) {
-        nsIFrame* frame = presShell->GetPrimaryFrameFor(element);
-        nsIFrame* ancestorFrame = presShell->GetRootFrame();
-        if (frame && ancestorFrame) {
-          nsPoint point = frame->GetOffsetTo(ancestorFrame);
-          x = nsPresContext::AppUnitsToFloatCSSPixels(point.x);
-          y = nsPresContext::AppUnitsToFloatCSSPixels(point.y);
-        }
+      nsIFrame* frame = element->GetPrimaryFrame();
+      nsIFrame* ancestorFrame = presShell->GetRootFrame();
+      if (frame && ancestorFrame) {
+        nsPoint point = frame->GetOffsetTo(ancestorFrame);
+        x = nsPresContext::AppUnitsToFloatCSSPixels(point.x);
+        y = nsPresContext::AppUnitsToFloatCSSPixels(point.y);
       }
     }
   }
   return matrix * gfxMatrix().Translate(gfxPoint(x, y));
 }
 
 nsSVGDisplayContainerFrame*
 nsSVGUtils::GetNearestSVGViewport(nsIFrame *aFrame)
--- a/layout/xul/base/src/nsBoxObject.cpp
+++ b/layout/xul/base/src/nsBoxObject.cpp
@@ -148,17 +148,22 @@ nsBoxObject::GetFrame(PRBool aFlushLayou
   if (!aFlushLayout) {
     // If we didn't flush layout when getting the presshell, we should at least
     // flush to make sure our frame model is up to date.
     // XXXbz should flush on document, no?  Except people call this from
     // frame code, maybe?
     shell->FlushPendingNotifications(Flush_Frames);
   }
 
-  return shell->GetPrimaryFrameFor(mContent);
+  // The flush might have killed mContent.
+  if (!mContent) {
+    return nsnull;
+  }
+
+  return mContent->GetPrimaryFrame();
 }
 
 nsIPresShell*
 nsBoxObject::GetPresShell(PRBool aFlushLayout)
 {
   if (!mContent) {
     return nsnull;
   }
--- a/layout/xul/base/src/nsListBoxBodyFrame.cpp
+++ b/layout/xul/base/src/nsListBoxBodyFrame.cpp
@@ -1219,18 +1219,17 @@ nsListBoxBodyFrame::GetNextItemBox(nsIBo
       // There is a content node that wants a frame.
       nsIContent *nextContent = parentContent->GetChildAt(i + aOffset + 1);
 
       if (!nextContent->IsXUL() ||
           nextContent->Tag() != nsGkAtoms::listitem)
         return GetNextItemBox(aBox, ++aOffset, aCreated);
 
       nsPresContext* presContext = PresContext();
-      nsIFrame* existingFrame =
-        presContext->GetPresShell()->GetPrimaryFrameFor(nextContent);
+      nsIFrame* existingFrame = nextContent->GetPrimaryFrame();
 
       if (existingFrame && existingFrame->GetParent() != this)
         return GetNextItemBox(aBox, ++aOffset, aCreated);
 
       if (!existingFrame) {
         // Either append the new frame, or insert it after the current frame
         PRBool isAppend = result != mLinkupFrame && mRowsToPrepend <= 0;
         nsIFrame* prevFrame = isAppend ? nsnull : aBox;
@@ -1343,38 +1342,37 @@ nsListBoxBodyFrame::ListBoxInsertFrames(
 // Called by nsCSSFrameConstructor when a new listitem content is inserted.
 //
 void 
 nsListBoxBodyFrame::OnContentInserted(nsPresContext* aPresContext, nsIContent* aChildContent)
 {
   if (mRowCount >= 0)
     ++mRowCount;
 
-  nsIPresShell *shell = aPresContext->PresShell();
   // The RDF content builder will build content nodes such that they are all 
   // ready when OnContentInserted is first called, meaning the first call
   // to CreateRows will create all the frames, but OnContentInserted will
   // still be called again for each content node - so we need to make sure
   // that the frame for each content node hasn't already been created.
-  nsIFrame* childFrame = shell->GetPrimaryFrameFor(aChildContent);
+  nsIFrame* childFrame = aChildContent->GetPrimaryFrame();
   if (childFrame)
     return;
 
   PRInt32 siblingIndex;
   nsCOMPtr<nsIContent> nextSiblingContent;
   GetListItemNextSibling(aChildContent, getter_AddRefs(nextSiblingContent), siblingIndex);
   
   // if we're inserting our item before the first visible content,
   // then we need to shift all rows down by one
   if (siblingIndex >= 0 &&  siblingIndex-1 <= mCurrentIndex) {
     mTopFrame = nsnull;
     mRowsToPrepend = 1;
   } else if (nextSiblingContent) {
     // we may be inserting before a frame that is on screen
-    nsIFrame* nextSiblingFrame = shell->GetPrimaryFrameFor(nextSiblingContent);
+    nsIFrame* nextSiblingFrame = nextSiblingContent->GetPrimaryFrame();
     mLinkupFrame = nextSiblingFrame;
   }
   
   CreateRows();
   PresContext()->PresShell()->
     FrameNeedsReflow(this, nsIPresShell::eTreeChange,
                      NS_FRAME_HAS_DIRTY_CHILDREN);
 }
@@ -1422,18 +1420,17 @@ nsListBoxBodyFrame::OnContentRemoved(nsP
       // down by one, and we will have to insert a new frame at the top.
       
       // if the last content node has a frame, we are scrolled to the bottom
       ChildIterator iter, last;
       ChildIterator::Init(mContent, &iter, &last);
       if (last.position() > 0) {
         iter.seek(last.position() - 1);
         nsIContent *lastChild = *iter;
-        nsIFrame* lastChildFrame = 
-          aPresContext->PresShell()->GetPrimaryFrameFor(lastChild);
+        nsIFrame* lastChildFrame = lastChild->GetPrimaryFrame();
       
         if (lastChildFrame) {
           mTopFrame = nsnull;
           mRowsToPrepend = 1;
           --mCurrentIndex;
           mYPosition = mCurrentIndex*mRowHeight;
           VerticalScroll(mYPosition);
         }
--- a/layout/xul/base/src/nsListBoxObject.cpp
+++ b/layout/xul/base/src/nsListBoxObject.cpp
@@ -189,26 +189,26 @@ nsListBoxObject::GetListBoxBody(PRBool a
 
   nsIPresShell* shell = GetPresShell(PR_FALSE);
   if (!shell) {
     return nsnull;
   }
 
   nsIFrame* frame = aFlush ? 
                       GetFrame(PR_FALSE) /* does Flush_Frames */ :
-                      shell->GetPrimaryFrameFor(mContent);
+                      mContent->GetPrimaryFrame();
   if (!frame)
     return nsnull;
 
   // Iterate over our content model children looking for the body.
   nsCOMPtr<nsIContent> content;
   FindBodyContent(frame->GetContent(), getter_AddRefs(content));
 
   // this frame will be a nsGFXScrollFrame
-  frame = shell->GetPrimaryFrameFor(content);
+  frame = content->GetPrimaryFrame();
   if (!frame)
      return nsnull;
   nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
   if (!scrollFrame)
     return nsnull;
 
   // this frame will be the one we want
   nsIFrame* yeahBaby = scrollFrame->GetScrolledFrame();
--- a/layout/xul/base/src/nsMenuFrame.cpp
+++ b/layout/xul/base/src/nsMenuFrame.cpp
@@ -1323,17 +1323,17 @@ nsMenuFrame::SetActiveChild(nsIDOMElemen
   if (!aChild) {
     // Remove the current selection
     mPopupFrame->ChangeMenuItem(nsnull, PR_FALSE);
     return NS_OK;
   }
 
   nsCOMPtr<nsIContent> child(do_QueryInterface(aChild));
 
-  nsIFrame* kid = PresContext()->PresShell()->GetPrimaryFrameFor(child);
+  nsIFrame* kid = child->GetPrimaryFrame();
   if (kid && kid->GetType() == nsGkAtoms::menuFrame)
     mPopupFrame->ChangeMenuItem(static_cast<nsMenuFrame *>(kid), PR_FALSE);
   return NS_OK;
 }
 
 nsIScrollableView* nsMenuFrame::GetScrollableView()
 {
   if (!mPopupFrame)
--- a/layout/xul/base/src/nsMenuPopupFrame.cpp
+++ b/layout/xul/base/src/nsMenuPopupFrame.cpp
@@ -906,17 +906,17 @@ nsMenuPopupFrame::SetPopupPosition(nsIFr
   if (!aAnchorFrame) {
     if (mAnchorContent) {
       nsCOMPtr<nsIDocument> document = mAnchorContent->GetDocument();
       if (document) {
         nsIPresShell *shell = document->GetPrimaryShell();
         if (!shell)
           return NS_ERROR_FAILURE;
 
-        aAnchorFrame = shell->GetPrimaryFrameFor(mAnchorContent);
+        aAnchorFrame = mAnchorContent->GetPrimaryFrame();
       }
     }
 
     if (!aAnchorFrame) {
       aAnchorFrame = rootFrame;
       if (!aAnchorFrame)
         return NS_OK;
     }
--- a/layout/xul/base/src/nsResizerFrame.cpp
+++ b/layout/xul/base/src/nsResizerFrame.cpp
@@ -97,17 +97,17 @@ nsResizerFrame::HandleEvent(nsPresContex
       if (aEvent->eventStructType == NS_MOUSE_EVENT &&
         static_cast<nsMouseEvent*>(aEvent)->button == nsMouseEvent::eLeftButton)
       {
         nsCOMPtr<nsIBaseWindow> window;
         nsIPresShell* presShell = aPresContext->GetPresShell();
         nsIContent* contentToResize =
           GetContentToResize(presShell, getter_AddRefs(window));
         if (contentToResize) {
-          nsIFrame* frameToResize = presShell->GetPrimaryFrameFor(contentToResize);
+          nsIFrame* frameToResize = contentToResize->GetPrimaryFrame();
           if (!frameToResize)
             break;
 
           mMouseDownRect = frameToResize->GetScreenRect();
         }
         else {
           // ask the widget implementation to begin a resize drag if it can
           Direction direction = GetDirection();
@@ -160,17 +160,17 @@ nsResizerFrame::HandleEvent(nsPresContex
       nsCOMPtr<nsIBaseWindow> window;
       nsIPresShell* presShell = aPresContext->GetPresShell();
       nsCOMPtr<nsIContent> contentToResize =
         GetContentToResize(presShell, getter_AddRefs(window));
 
       // check if the returned content really is a menupopup
       nsMenuPopupFrame* menuPopupFrame = nsnull;
       if (contentToResize) {
-        nsIFrame* frameToResize = presShell->GetPrimaryFrameFor(contentToResize);
+        nsIFrame* frameToResize = contentToResize->GetPrimaryFrame();
         if (frameToResize && frameToResize->GetType() == nsGkAtoms::menuPopupFrame) {
           menuPopupFrame = static_cast<nsMenuPopupFrame *>(frameToResize);
         }
       }
 
       // both MouseMove and direction are negative when pointing to the
       // top and left, and positive when pointing to the bottom and right
 
--- a/layout/xul/base/src/nsScrollbarFrame.cpp
+++ b/layout/xul/base/src/nsScrollbarFrame.cpp
@@ -180,18 +180,17 @@ nsScrollbarFrame::SetScrollbarMediatorCo
   mScrollbarMediator = aMediator;
 }
 
 nsIScrollbarMediator*
 nsScrollbarFrame::GetScrollbarMediator()
 {
   if (!mScrollbarMediator)
     return nsnull;
-  nsIFrame* f =
-    PresContext()->PresShell()->GetPrimaryFrameFor(mScrollbarMediator);
+  nsIFrame* f = mScrollbarMediator->GetPrimaryFrame();
   if (!f)
     return nsnull;
 
   // check if the frame is a scroll frame. If so, get the scrollable frame
   // inside it.
   nsIScrollableFrame* scrollFrame = do_QueryFrame(f);
   if (scrollFrame) {
     f = scrollFrame->GetScrolledFrame();
--- a/layout/xul/base/src/nsSliderFrame.cpp
+++ b/layout/xul/base/src/nsSliderFrame.cpp
@@ -813,23 +813,20 @@ nsSliderFrame::SetCurrentPositionInterna
     // See if we have a mediator.
     nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
     if (mediator) {
       nsRefPtr<nsPresContext> context = PresContext();
       nsCOMPtr<nsIContent> content = GetContent();
       mediator->PositionChanged(scrollbarFrame, GetCurrentPosition(scrollbar), aNewPos);
       // 'mediator' might be dangling now...
       UpdateAttribute(scrollbar, aNewPos, PR_FALSE, aIsSmooth);
-      nsIPresShell* shell = context->GetPresShell();
-      if (shell) {
-        nsIFrame* frame = shell->GetPrimaryFrameFor(content);
-        if (frame && frame->GetType() == nsGkAtoms::sliderFrame) {
-          static_cast<nsSliderFrame*>(frame)->
-            CurrentPositionChanged(frame->PresContext(), aImmediateRedraw);
-        }
+      nsIFrame* frame = content->GetPrimaryFrame();
+      if (frame && frame->GetType() == nsGkAtoms::sliderFrame) {
+        static_cast<nsSliderFrame*>(frame)->
+          CurrentPositionChanged(frame->PresContext(), aImmediateRedraw);
       }
       mUserChanged = PR_FALSE;
       return;
     }
   }
 
   UpdateAttribute(scrollbar, aNewPos, PR_TRUE, aIsSmooth);
   mUserChanged = PR_FALSE;
--- a/layout/xul/base/src/nsXULPopupManager.cpp
+++ b/layout/xul/base/src/nsXULPopupManager.cpp
@@ -282,17 +282,17 @@ nsXULPopupManager::GetFrameOfTypeForCont
 {
   nsIDocument *document = aContent->GetCurrentDoc();
   if (document) {
     nsCOMPtr<nsIPresShell> presShell = document->GetPrimaryShell();
     if (presShell) {
       if (aShouldFlush)
         presShell->FlushPendingNotifications(Flush_Frames);
 
-      nsIFrame* frame = presShell->GetPrimaryFrameFor(aContent);
+      nsIFrame* frame = aContent->GetPrimaryFrame();
       if (frame && frame->GetType() == aFrameType)
         return frame;
     }
   }
 
   return nsnull;
 }
 
@@ -1059,17 +1059,17 @@ nsXULPopupManager::FirePopupShowingEvent
   // the popup, otherwise the new frames will reflow after the popup appears,
   // causing the popup to flicker. Frame code always calls this asynchronously,
   // so this should be safe.
   nsIDocument *document = aPopup->GetCurrentDoc();
   if (document)
     document->FlushPendingNotifications(Flush_Layout);
 
   // get the frame again in case it went away
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(aPopup);
+  nsIFrame* frame = aPopup->GetPrimaryFrame();
   if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
     nsMenuPopupFrame* popupFrame = static_cast<nsMenuPopupFrame *>(frame);
 
     // if the event was cancelled, don't open the popup, and reset it's
     // state back to closed
     if (status == nsEventStatus_eConsumeNoDefault) {
       popupFrame->SetPopupState(ePopupClosed);
     }
@@ -1108,17 +1108,17 @@ nsXULPopupManager::FirePopupHidingEvent(
       if (doc && currentFocus &&
           nsContentUtils::ContentIsCrossDocDescendantOf(currentFocus, aPopup)) {
         fm->ClearFocus(doc->GetWindow());
       }
     }
   }
 
   // get frame again in case it went away
-  nsIFrame* frame = presShell->GetPrimaryFrameFor(aPopup);
+  nsIFrame* frame = aPopup->GetPrimaryFrame();
   if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
     nsMenuPopupFrame* popupFrame = static_cast<nsMenuPopupFrame *>(frame);
 
     // if the event was cancelled, don't hide the popup, and reset it's
     // state back to open. Only popups in chrome shells can prevent a popup
     // from hiding.
     if (status == nsEventStatus_eConsumeNoDefault &&
         !popupFrame->IsInContentShell()) {
--- a/layout/xul/base/src/nsXULTooltipListener.cpp
+++ b/layout/xul/base/src/nsXULTooltipListener.cpp
@@ -691,19 +691,17 @@ nsXULTooltipListener::GetTooltipFor(nsIC
   nsresult rv = FindTooltip(aTarget, getter_AddRefs(tooltip));
   if (NS_FAILED(rv) || !tooltip) {
     return rv;
   }
 
   // Submenus can't be used as tooltips, see bug 288763.
   nsIContent* parent = tooltip->GetParent();
   if (parent) {
-    nsIDocument* doc = parent->GetCurrentDoc();
-    nsIPresShell* presShell = doc ? doc->GetPrimaryShell() : nsnull;
-    nsIFrame* frame = presShell ? presShell->GetPrimaryFrameFor(parent) : nsnull;
+    nsIFrame* frame = parent->GetPrimaryFrame();
     if (frame && frame->GetType() == nsGkAtoms::menuFrame) {
       NS_WARNING("Menu cannot be used as a tooltip");
       return NS_ERROR_FAILURE;
     }
   }
 
   tooltip.swap(*aTooltip);
   return rv;
--- a/layout/xul/base/src/tree/src/nsTreeBodyFrame.cpp
+++ b/layout/xul/base/src/tree/src/nsTreeBodyFrame.cpp
@@ -877,21 +877,20 @@ FindScrollParts(nsIFrame* aCurrFrame, ns
           !aResult->mColumnsScrollableView)) {
     FindScrollParts(child, aResult);
     child = child->GetNextSibling();
   }
 }
 
 nsTreeBodyFrame::ScrollParts nsTreeBodyFrame::GetScrollParts()
 {
-  nsPresContext* presContext = PresContext();
   ScrollParts result = { nsnull, nsnull, nsnull, nsnull, nsnull, nsnull };
   nsIContent* baseElement = GetBaseElement();
   nsIFrame* treeFrame =
-    baseElement ? presContext->PresShell()->GetPrimaryFrameFor(baseElement) : nsnull;
+    baseElement ? baseElement->GetPrimaryFrame() : nsnull;
   if (treeFrame) {
     // The way we do this, searching through the entire frame subtree, is pretty
     // dumb! We should know where these frames are.
     FindScrollParts(treeFrame, &result);
     if (result.mHScrollbar) {
       result.mHScrollbar->SetScrollbarMediatorContent(GetContent());
       nsIFrame* f = do_QueryFrame(result.mHScrollbar);
       result.mHScrollbarContent = f->GetContent();
@@ -1359,17 +1358,17 @@ nsTreeBodyFrame::CheckTextForBidi(nsAuto
 }
 
 void
 nsTreeBodyFrame::AdjustForCellText(nsAutoString& aText,
                                    PRInt32 aRowIndex,  nsTreeColumn* aColumn,
                                    nsIRenderingContext& aRenderingContext,
                                    nsRect& aTextRect)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   nscoord width =
     nsLayoutUtils::GetStringWidth(this, &aRenderingContext, aText.get(), aText.Length());
   nscoord maxWidth = aTextRect.width;
 
   if (aColumn->Overflow()) {
     nsresult rv;
     nsTreeColumn* nextColumn = aColumn->GetNext();
@@ -1517,17 +1516,17 @@ nsTreeBodyFrame::AdjustForCellText(nsAut
     nsLayoutUtils::GetStringWidth(this, &aRenderingContext, aText.get(), aText.Length());
 }
 
 nsIAtom*
 nsTreeBodyFrame::GetItemWithinCellAt(nscoord aX, const nsRect& aCellRect, 
                                      PRInt32 aRowIndex,
                                      nsTreeColumn* aColumn)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   // Obtain the properties for our cell.
   PrefillPropertyArray(aRowIndex, aColumn);
   mView->GetCellProperties(aRowIndex, aColumn, mScratchArray);
 
   // Resolve style for the cell.
   nsStyleContext* cellContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecell);
 
@@ -1961,17 +1960,17 @@ nsTreeBodyFrame::EndUpdateBatch()
   }
 
   return NS_OK;
 }
 
 void
 nsTreeBodyFrame::PrefillPropertyArray(PRInt32 aRowIndex, nsTreeColumn* aCol)
 {
-  NS_PRECONDITION(!aCol || aCol->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(!aCol || aCol->GetFrame(), "invalid column passed");
   mScratchArray->Clear();
   
   // focus
   if (mFocused)
     mScratchArray->AppendElement(nsGkAtoms::focus);
 
   // sort
   PRBool sorted = PR_FALSE;
@@ -2906,17 +2905,17 @@ nsTreeBodyFrame::PaintTreeBody(nsIRender
 
 void
 nsTreeBodyFrame::PaintColumn(nsTreeColumn*        aColumn,
                              const nsRect&        aColumnRect,
                              nsPresContext*      aPresContext,
                              nsIRenderingContext& aRenderingContext,
                              const nsRect&        aDirtyRect)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   // Now obtain the properties for our cell.
   PrefillPropertyArray(-1, aColumn);
   mView->GetColumnProperties(aColumn, mScratchArray);
 
   // Resolve style for the column.  It contains all the info we need to lay ourselves
   // out and to paint.
   nsStyleContext* colContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecolumn);
@@ -3141,17 +3140,17 @@ nsTreeBodyFrame::PaintCell(PRInt32      
                            nsTreeColumn*        aColumn,
                            const nsRect&        aCellRect,
                            nsPresContext*       aPresContext,
                            nsIRenderingContext& aRenderingContext,
                            const nsRect&        aDirtyRect,
                            nscoord&             aCurrX,
                            nsPoint              aPt)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   // Now obtain the properties for our cell.
   // XXX Automatically fill in the following props: open, closed, container, leaf, selected, focused, and the col ID.
   PrefillPropertyArray(aRowIndex, aColumn);
   mView->GetCellProperties(aRowIndex, aColumn, mScratchArray);
 
   // Resolve style for the cell.  It contains all the info we need to lay ourselves
   // out and to paint.
@@ -3320,17 +3319,17 @@ nsTreeBodyFrame::PaintTwisty(PRInt32    
                              nsTreeColumn*        aColumn,
                              const nsRect&        aTwistyRect,
                              nsPresContext*      aPresContext,
                              nsIRenderingContext& aRenderingContext,
                              const nsRect&        aDirtyRect,
                              nscoord&             aRemainingWidth,
                              nscoord&             aCurrX)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   PRBool isRTL = GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
   nscoord rightEdge = aCurrX + aRemainingWidth;
   // Paint the twisty, but only if we are a non-empty container.
   PRBool shouldPaint = PR_FALSE;
   PRBool isContainer = PR_FALSE;
   mView->IsContainer(aRowIndex, &isContainer);
   if (isContainer) {
@@ -3412,17 +3411,17 @@ nsTreeBodyFrame::PaintImage(PRInt32     
                             nsTreeColumn*        aColumn,
                             const nsRect&        aImageRect,
                             nsPresContext*       aPresContext,
                             nsIRenderingContext& aRenderingContext,
                             const nsRect&        aDirtyRect,
                             nscoord&             aRemainingWidth,
                             nscoord&             aCurrX)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   PRBool isRTL = GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
   nscoord rightEdge = aCurrX + aRemainingWidth;
   // Resolve style for the image.
   nsStyleContext* imageContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeimage);
 
   // Obtain the margins for the image and then deflate our rect by that
   // amount.  The image is assumed to be contained within the deflated rect.
@@ -3548,17 +3547,17 @@ nsTreeBodyFrame::PaintText(PRInt32      
                            nsTreeColumn*        aColumn,
                            const nsRect&        aTextRect,
                            nsPresContext*      aPresContext,
                            nsIRenderingContext& aRenderingContext,
                            const nsRect&        aDirtyRect,
                            nscoord&             aCurrX,
                            PRBool               aTextRTL)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   PRBool isRTL = GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
   nscoord rightEdge = aTextRect.XMost();
 
   // Now obtain the text for our cell.
   nsAutoString text;
   mView->GetCellText(aRowIndex, aColumn, text);
   // We're going to paint this text so we need to ensure bidi is enabled if
@@ -3654,17 +3653,17 @@ nsTreeBodyFrame::PaintText(PRInt32      
 void
 nsTreeBodyFrame::PaintCheckbox(PRInt32              aRowIndex,
                                nsTreeColumn*        aColumn,
                                const nsRect&        aCheckboxRect,
                                nsPresContext*      aPresContext,
                                nsIRenderingContext& aRenderingContext,
                                const nsRect&        aDirtyRect)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   // Resolve style for the checkbox.
   nsStyleContext* checkboxContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreecheckbox);
 
   nscoord rightEdge = aCheckboxRect.XMost();
 
   // Obtain the margins for the checkbox and then deflate our rect by that 
   // amount.  The checkbox is assumed to be contained within the deflated rect.
@@ -3716,17 +3715,17 @@ nsTreeBodyFrame::PaintCheckbox(PRInt32  
 void
 nsTreeBodyFrame::PaintProgressMeter(PRInt32              aRowIndex,
                                     nsTreeColumn*        aColumn,
                                     const nsRect&        aProgressMeterRect,
                                     nsPresContext*      aPresContext,
                                     nsIRenderingContext& aRenderingContext,
                                     const nsRect&        aDirtyRect)
 {
-  NS_PRECONDITION(aColumn && aColumn->GetFrame(this), "invalid column passed");
+  NS_PRECONDITION(aColumn && aColumn->GetFrame(), "invalid column passed");
 
   // Resolve style for the progress meter.  It contains all the info we need
   // to lay ourselves out and to paint.
   nsStyleContext* meterContext = GetPseudoStyleContext(nsCSSAnonBoxes::moztreeprogressmeter);
 
   // Obtain the margins for the progress meter and then deflate our rect by that 
   // amount. The progress meter is assumed to be contained within the deflated
   // rect.
--- a/layout/xul/base/src/tree/src/nsTreeBoxObject.cpp
+++ b/layout/xul/base/src/tree/src/nsTreeBoxObject.cpp
@@ -122,23 +122,20 @@ nsTreeBoxObject::GetTreeBody()
 
   nsIFrame* frame = GetFrame(PR_FALSE);
   if (!frame)
     return nsnull;
 
   // Iterate over our content model children looking for the body.
   nsCOMPtr<nsIContent> content;
   FindBodyElement(frame->GetContent(), getter_AddRefs(content));
-
-  nsIPresShell* shell = GetPresShell(PR_FALSE);
-  if (!shell) {
+  if (!content)
     return nsnull;
-  }
 
-  frame = shell->GetPrimaryFrameFor(content);
+  frame = content->GetPrimaryFrame();
   if (!frame)
      return nsnull;
 
   // Make sure that the treebodyframe has a pointer to |this|.
   nsTreeBodyFrame *treeBody = do_QueryFrame(frame);
   NS_ENSURE_TRUE(treeBody && treeBody->GetTreeBoxObject() == this, nsnull);
 
   mTreeBody = treeBody;
--- a/layout/xul/base/src/tree/src/nsTreeColumns.cpp
+++ b/layout/xul/base/src/tree/src/nsTreeColumns.cpp
@@ -88,69 +88,49 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(
     AddRef();
     *aInstancePtr = this;
     return NS_OK;
   }
   else
 NS_INTERFACE_MAP_END
 
 nsIFrame*
-nsTreeColumn::GetFrame(nsTreeBodyFrame* aBodyFrame)
-{
-  NS_PRECONDITION(aBodyFrame, "null frame?");
-
-  nsIPresShell *shell = aBodyFrame->PresContext()->PresShell();
-  if (!shell)
-    return nsnull;
-
-  return shell->GetPrimaryFrameFor(mContent);
-}
-
-nsIFrame*
 nsTreeColumn::GetFrame()
 {
   NS_ENSURE_TRUE(mContent, nsnull);
 
-  nsCOMPtr<nsIDocument> document = mContent->GetDocument();
-  if (!document)
-    return nsnull;
-
-  nsIPresShell *shell = document->GetPrimaryShell();
-  if (!shell)
-    return nsnull;
-
-  return shell->GetPrimaryFrameFor(mContent);
+  return mContent->GetPrimaryFrame();
 }
 
 PRBool
 nsTreeColumn::IsLastVisible(nsTreeBodyFrame* aBodyFrame)
 {
-  NS_ASSERTION(GetFrame(aBodyFrame), "should have checked for this already");
+  NS_ASSERTION(GetFrame(), "should have checked for this already");
 
   // cyclers are fixed width, don't adjust them
   if (IsCycler())
     return PR_FALSE;
 
   // we're certainly not the last visible if we're not visible
-  if (GetFrame(aBodyFrame)->GetRect().width == 0)
+  if (GetFrame()->GetRect().width == 0)
     return PR_FALSE;
 
   // try to find a visible successor
   for (nsTreeColumn *next = GetNext(); next; next = next->GetNext()) {
-    nsIFrame* frame = next->GetFrame(aBodyFrame);
+    nsIFrame* frame = next->GetFrame();
     if (frame && frame->GetRect().width > 0)
       return PR_FALSE;
   }
   return PR_TRUE;
 }
 
 nsresult
 nsTreeColumn::GetRect(nsTreeBodyFrame* aBodyFrame, nscoord aY, nscoord aHeight, nsRect* aResult)
 {
-  nsIFrame* frame = GetFrame(aBodyFrame);
+  nsIFrame* frame = GetFrame();
   if (!frame) {
     *aResult = nsRect();
     return NS_ERROR_FAILURE;
   }
 
   PRBool isRTL = aBodyFrame->GetStyleVisibility()->mDirection == NS_STYLE_DIRECTION_RTL;
   *aResult = frame->GetRect();
   aResult->y = aY;
@@ -160,29 +140,29 @@ nsTreeColumn::GetRect(nsTreeBodyFrame* a
   else if (IsLastVisible(aBodyFrame))
     aResult->width += aBodyFrame->mAdjustWidth;
   return NS_OK;
 }
 
 nsresult
 nsTreeColumn::GetXInTwips(nsTreeBodyFrame* aBodyFrame, nscoord* aResult)
 {
-  nsIFrame* frame = GetFrame(aBodyFrame);
+  nsIFrame* frame = GetFrame();
   if (!frame) {
     *aResult = 0;
     return NS_ERROR_FAILURE;
   }
   *aResult = frame->GetRect().x;
   return NS_OK;
 }
 
 nsresult
 nsTreeColumn::GetWidthInTwips(nsTreeBodyFrame* aBodyFrame, nscoord* aResult)
 {
-  nsIFrame* frame = GetFrame(aBodyFrame);
+  nsIFrame* frame = GetFrame();
   if (!frame) {
     *aResult = 0;
     return NS_ERROR_FAILURE;
   }
   *aResult = frame->GetRect().width;
   if (IsLastVisible(aBodyFrame))
     *aResult += aBodyFrame->mAdjustWidth;
   return NS_OK;
@@ -643,27 +623,22 @@ nsTreeColumns::EnsureColumns()
     boxObject->GetElement(getter_AddRefs(treeElement));
     nsCOMPtr<nsIContent> treeContent = do_QueryInterface(treeElement);
 
     nsIContent* colsContent =
       nsTreeUtils::GetDescendantChild(treeContent, nsGkAtoms::treecols);
     if (!colsContent)
       return;
 
-    nsCOMPtr<nsIDocument> document = treeContent->GetDocument();
-    nsIPresShell *shell = document->GetPrimaryShell();
-    if (!shell)
-      return;
-
     nsIContent* colContent =
       nsTreeUtils::GetDescendantChild(colsContent, nsGkAtoms::treecol);
     if (!colContent)
       return;
 
-    nsIFrame* colFrame = shell->GetPrimaryFrameFor(colContent);
+    nsIFrame* colFrame = colContent->GetPrimaryFrame();
     if (!colFrame)
       return;
 
     colFrame = colFrame->GetParent();
     if (!colFrame)
       return;
 
     colFrame = colFrame->GetFirstChild(nsnull);