Bug 649170, use the same nsIWeakReference in DocumentViewer and nsPrintEngine, r=roc
authorOlli Pettay <Olli.Pettay@helsinki.fi>
Tue, 10 May 2011 15:55:26 +0300
changeset 69468 aac394a1bd91f56371b46b420445417d766f3f25
parent 69467 e0f6db50231f8be9c3a52fd395fa8802c0fa911b
child 69469 a16d106105edbdbeb508da20ca8f0fbf6180ab12
push id76
push userbzbarsky@mozilla.com
push dateTue, 05 Jul 2011 17:00:57 +0000
treeherdermozilla-beta@d3a2732c35f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc
bugs649170
milestone6.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 649170, use the same nsIWeakReference in DocumentViewer and nsPrintEngine, r=roc
layout/base/nsDocumentViewer.cpp
layout/printing/nsPrintEngine.cpp
layout/printing/nsPrintEngine.h
--- a/layout/base/nsDocumentViewer.cpp
+++ b/layout/base/nsDocumentViewer.cpp
@@ -3629,17 +3629,17 @@ DocumentViewerImpl::Print(nsIPrintSettin
 #endif
 
   if (!mContainer) {
     PR_PL(("Container was destroyed yet we are still trying to use it!"));
     return NS_ERROR_FAILURE;
   }
 
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mContainer));
-  NS_ASSERTION(docShell, "This has to be a docshell");
+  NS_ENSURE_STATE(docShell);
 
   // Check to see if this document is still busy
   // If it is busy and we aren't already "queued" up to print then
   // Indicate there is a print pending and cache the args for later
   PRUint32 busyFlags = nsIDocShell::BUSY_FLAGS_NONE;
   if ((NS_FAILED(docShell->GetBusyFlags(&busyFlags)) ||
        (busyFlags != nsIDocShell::BUSY_FLAGS_NONE && busyFlags & nsIDocShell::BUSY_FLAGS_PAGE_LOADING)) && 
       !mPrintDocIsFullyLoaded) {
@@ -3677,17 +3677,17 @@ DocumentViewerImpl::Print(nsIPrintSettin
   nsCOMPtr<nsIPluginDocument> pDoc(do_QueryInterface(mDocument));
   if (pDoc)
     return pDoc->Print();
 
   if (!mPrintEngine) {
     mPrintEngine = new nsPrintEngine();
     NS_ENSURE_TRUE(mPrintEngine, NS_ERROR_OUT_OF_MEMORY);
 
-    rv = mPrintEngine->Initialize(this, docShell, mDocument, 
+    rv = mPrintEngine->Initialize(this, mContainer, mDocument, 
                                   float(mDeviceContext->AppUnitsPerCSSInch()) /
                                   float(mDeviceContext->AppUnitsPerDevPixel()) /
                                   mPageZoom,
 #ifdef NS_DEBUG
                                   mDebugFile
 #else
                                   nsnull
 #endif
@@ -3729,32 +3729,31 @@ DocumentViewerImpl::PrintPreview(nsIPrin
   if (xulDoc) {
     nsPrintEngine::CloseProgressDialog(aWebProgressListener);
     nsPrintEngine::ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_NO_XUL, PR_FALSE);
     return NS_ERROR_FAILURE;
   }
 #endif
 
   nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mContainer));
-  NS_ASSERTION(docShell, "This has to be a docshell");
   if (!docShell || !mDeviceContext) {
     PR_PL(("Can't Print Preview without device context and docshell"));
     return NS_ERROR_FAILURE;
   }
 
   if (!mPrintEngine) {
     nsCOMPtr<nsIDOMDocument> domDoc;
     aChildDOMWin->GetDocument(getter_AddRefs(domDoc));
     nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
     NS_ENSURE_STATE(doc);
 
     mPrintEngine = new nsPrintEngine();
     NS_ENSURE_TRUE(mPrintEngine, NS_ERROR_OUT_OF_MEMORY);
 
-    rv = mPrintEngine->Initialize(this, docShell, doc,
+    rv = mPrintEngine->Initialize(this, mContainer, doc,
                                   float(mDeviceContext->AppUnitsPerCSSInch()) /
                                   float(mDeviceContext->AppUnitsPerDevPixel()) /
                                   mPageZoom,
 #ifdef NS_DEBUG
                                   mDebugFile
 #else
                                   nsnull
 #endif
--- a/layout/printing/nsPrintEngine.cpp
+++ b/layout/printing/nsPrintEngine.cpp
@@ -256,17 +256,16 @@ NS_IMPL_ISUPPORTS1(nsPrintEngine, nsIObs
 //---------------------------------------------------
 //-- nsPrintEngine Class Impl
 //---------------------------------------------------
 nsPrintEngine::nsPrintEngine() :
   mIsCreatingPrintPreview(PR_FALSE),
   mIsDoingPrinting(PR_FALSE),
   mIsDoingPrintPreview(PR_FALSE),
   mProgressDialogIsShown(PR_FALSE),
-  mContainer(nsnull),
   mScreenDPI(115.0f),
   mPrt(nsnull),
   mPagePrintTimer(nsnull),
   mPageSeqFrame(nsnull),
   mPrtPreview(nsnull),
   mOldPrtPreview(nsnull),
   mDebugFile(nsnull)
 {
@@ -312,27 +311,27 @@ void nsPrintEngine::DestroyPrintingData(
 }
 
 //---------------------------------------------------------------------------------
 //-- Section: Methods needed by the DocViewer
 //---------------------------------------------------------------------------------
 
 //--------------------------------------------------------
 nsresult nsPrintEngine::Initialize(nsIDocumentViewerPrint* aDocViewerPrint, 
-                                   nsISupports*            aContainer,
+                                   nsIWeakReference*       aContainer,
                                    nsIDocument*            aDocument,
                                    float                   aScreenDPI,
                                    FILE*                   aDebugFile)
 {
   NS_ENSURE_ARG_POINTER(aDocViewerPrint);
   NS_ENSURE_ARG_POINTER(aContainer);
   NS_ENSURE_ARG_POINTER(aDocument);
 
   mDocViewerPrint = aDocViewerPrint;
-  mContainer      = aContainer;      // weak reference
+  mContainer      = aContainer;
   mDocument       = aDocument;
   mScreenDPI      = aScreenDPI;
 
   mDebugFile      = aDebugFile;      // ok to be NULL
 
   return NS_OK;
 }
 
@@ -361,21 +360,21 @@ nsPrintEngine::Cancelled()
 // Install our event listeners on the document to prevent 
 // some events from being processed while in PrintPreview 
 //
 // No return code - if this fails, there isn't much we can do
 void
 nsPrintEngine::InstallPrintPreviewListener()
 {
   if (!mPrt->mPPEventListeners) {
-    nsCOMPtr<nsPIDOMWindow> win(do_GetInterface(mContainer));
-    nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(win->GetFrameElementInternal()));
-    mPrt->mPPEventListeners = new nsPrintPreviewListener(target);
-
-    if (mPrt->mPPEventListeners) {
+    nsCOMPtr<nsIDocShell> docShell = do_QueryReferent(mContainer);
+    nsCOMPtr<nsPIDOMWindow> win(do_GetInterface(docShell));
+    if (win) {
+      nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(win->GetFrameElementInternal()));
+      mPrt->mPPEventListeners = new nsPrintPreviewListener(target);
       mPrt->mPPEventListeners->AddListeners();
     }
   }
 }
 
 //----------------------------------------------------------------------
 nsresult 
 nsPrintEngine::GetSeqFrameAndCountPagesInternal(nsPrintObject*  aPO,
@@ -531,17 +530,17 @@ nsPrintEngine::DoCommonPrint(PRBool     
   // because the Print Dialog will "steal" focus and later when you try
   // to get the currently focused windows it will be NULL
   mPrt->mCurrentFocusWin = FindFocusedDOMWindow();
 
   // Check to see if there is a "regular" selection
   PRBool isSelection = IsThereARangeSelection(mPrt->mCurrentFocusWin);
 
   // Get the docshell for this documentviewer
-  nsCOMPtr<nsIDocShell> webContainer(do_QueryInterface(mContainer, &rv));
+  nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer, &rv));
   NS_ENSURE_SUCCESS(rv, rv);
 
   mPrt->mPrintObject = new nsPrintObject();
   NS_ENSURE_TRUE(mPrt->mPrintObject, NS_ERROR_OUT_OF_MEMORY);
   rv = mPrt->mPrintObject->Init(webContainer, aDoc, aIsPrintPreview);
   NS_ENSURE_SUCCESS(rv, rv);
 
   NS_ENSURE_TRUE(mPrt->mPrintDocList.AppendElement(mPrt->mPrintObject),
@@ -762,18 +761,18 @@ nsPrintEngine::Print(nsIPrintSettings*  
 
 NS_IMETHODIMP
 nsPrintEngine::PrintPreview(nsIPrintSettings* aPrintSettings, 
                                  nsIDOMWindow *aChildDOMWin, 
                                  nsIWebProgressListener* aWebProgressListener)
 {
   // Get the DocShell and see if it is busy
   // (We can't Print Preview this document if it is still busy)
-  nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(mContainer));
-  NS_ASSERTION(docShell, "This has to be a docshell");
+  nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mContainer));
+  NS_ENSURE_STATE(docShell);
 
   PRUint32 busyFlags = nsIDocShell::BUSY_FLAGS_NONE;
   if (NS_FAILED(docShell->GetBusyFlags(&busyFlags)) ||
       busyFlags != nsIDocShell::BUSY_FLAGS_NONE) {
     CloseProgressDialog(aWebProgressListener);
     ShowPrintErrorDialog(NS_ERROR_GFX_PRINTER_DOC_IS_BUSY_PP, PR_FALSE);
     return NS_ERROR_FAILURE;
   }
@@ -787,30 +786,30 @@ nsPrintEngine::PrintPreview(nsIPrintSett
   return CommonPrint(PR_TRUE, aPrintSettings, aWebProgressListener, doc);
 }
 
 //----------------------------------------------------------------------------------
 /* readonly attribute boolean isFramesetDocument; */
 NS_IMETHODIMP
 nsPrintEngine::GetIsFramesetDocument(PRBool *aIsFramesetDocument)
 {
-  nsCOMPtr<nsIDocShell> webContainer(do_QueryInterface(mContainer));
+  nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer));
   *aIsFramesetDocument = IsParentAFrameSet(webContainer);
   return NS_OK;
 }
 
 //----------------------------------------------------------------------------------
 /* readonly attribute boolean isIFrameSelected; */
 NS_IMETHODIMP 
 nsPrintEngine::GetIsIFrameSelected(PRBool *aIsIFrameSelected)
 {
   *aIsIFrameSelected = PR_FALSE;
 
   // Get the docshell for this documentviewer
-  nsCOMPtr<nsIDocShell> webContainer(do_QueryInterface(mContainer));
+  nsCOMPtr<nsIDocShell> webContainer(do_QueryReferent(mContainer));
   // Get the currently focused window
   nsCOMPtr<nsIDOMWindow> currentFocusWin = FindFocusedDOMWindow();
   if (currentFocusWin && webContainer) {
     // Get whether the doc contains a frameset 
     // Also, check to see if the currently focus docshell
     // is a child of this docshell
     PRPackedBool isParentFrameSet;
     *aIsIFrameSelected = IsThereAnIFrameSelected(webContainer, currentFocusWin, isParentFrameSet);
@@ -2844,17 +2843,17 @@ nsPrintEngine::IsWindowsInOurSubTree(nsP
 
   // now check to make sure it is in "our" tree of docshells
   if (window) {
     nsCOMPtr<nsIDocShellTreeItem> docShellAsItem =
       do_QueryInterface(window->GetDocShell());
 
     if (docShellAsItem) {
       // get this DocViewer docshell
-      nsCOMPtr<nsIDocShell> thisDVDocShell(do_QueryInterface(mContainer));
+      nsCOMPtr<nsIDocShell> thisDVDocShell(do_QueryReferent(mContainer));
       while (!found) {
         nsCOMPtr<nsIDocShell> parentDocshell(do_QueryInterface(docShellAsItem));
         if (parentDocshell) {
           if (parentDocshell == thisDVDocShell) {
             found = PR_TRUE;
             break;
           }
         } else {
--- a/layout/printing/nsPrintEngine.h
+++ b/layout/printing/nsPrintEngine.h
@@ -50,16 +50,17 @@
 // Classes
 class nsPagePrintTimer;
 class nsIDocShellTreeNode;
 class nsDeviceContext;
 class nsIDocumentViewerPrint;
 class nsPrintObject;
 class nsIDocShell;
 class nsIPageSequenceFrame;
+class nsIWeakReference;
 
 //------------------------------------------------------------------------
 // nsPrintEngine Class
 //
 //------------------------------------------------------------------------
 class nsPrintEngine : public nsIObserver
 {
 public:
@@ -97,17 +98,17 @@ public:
 
   nsPrintEngine();
   ~nsPrintEngine();
 
   void Destroy();
   void DestroyPrintingData();
 
   nsresult Initialize(nsIDocumentViewerPrint* aDocViewerPrint, 
-                      nsISupports*            aContainer,
+                      nsIWeakReference*       aContainer,
                       nsIDocument*            aDocument,
                       float                   aScreenDPI,
                       FILE*                   aDebugFile);
 
   nsresult GetSeqFrameAndCountPages(nsIFrame*& aSeqFrame, PRInt32& aCount);
 
   //
   // The following three methods are used for printing...
@@ -275,17 +276,17 @@ protected:
 
   // Static member variables
   PRPackedBool mIsCreatingPrintPreview;
   PRPackedBool mIsDoingPrinting;
   PRPackedBool mIsDoingPrintPreview; // per DocumentViewer
   PRPackedBool mProgressDialogIsShown;
 
   nsCOMPtr<nsIDocumentViewerPrint> mDocViewerPrint;
-  nsISupports*            mContainer;      // [WEAK] it owns me!
+  nsWeakPtr               mContainer;
   float                   mScreenDPI;
   
   nsPrintData*            mPrt;
   nsPagePrintTimer*       mPagePrintTimer;
   nsIPageSequenceFrame*   mPageSeqFrame;
 
   // Print Preview
   nsPrintData*            mPrtPreview;