layout/printing/nsPrintObject.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsPrintObject.h"
     7 #include "nsIContentViewer.h"
     8 #include "nsIDOMDocument.h"
     9 #include "nsContentUtils.h" // for nsAutoScriptBlocker
    10 #include "nsIInterfaceRequestorUtils.h"
    11 #include "nsPIDOMWindow.h"
    12 #include "nsGkAtoms.h"
    13 #include "nsComponentManagerUtils.h"
    14 #include "nsIDocShellTreeItem.h"
    15 #include "nsIBaseWindow.h"
    16 #include "nsIDocument.h"
    18 //---------------------------------------------------
    19 //-- nsPrintObject Class Impl
    20 //---------------------------------------------------
    21 nsPrintObject::nsPrintObject() :
    22   mContent(nullptr), mFrameType(eFrame), mParent(nullptr),
    23   mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false),
    24   mInvisible(false), mDidCreateDocShell(false),
    25   mShrinkRatio(1.0), mZoomRatio(1.0)
    26 {
    27   MOZ_COUNT_CTOR(nsPrintObject);
    28 }
    31 nsPrintObject::~nsPrintObject()
    32 {
    33   MOZ_COUNT_DTOR(nsPrintObject);
    34   for (uint32_t i=0;i<mKids.Length();i++) {
    35     nsPrintObject* po = mKids[i];
    36     delete po;
    37   }
    39   DestroyPresentation();
    40   if (mDidCreateDocShell && mDocShell) {
    41     nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
    42     if (baseWin) {
    43       baseWin->Destroy();
    44     }
    45   }                            
    46   mDocShell = nullptr;
    47   mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell; 
    48 }
    50 //------------------------------------------------------------------
    51 nsresult 
    52 nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc,
    53                     bool aPrintPreview)
    54 {
    55   mPrintPreview = aPrintPreview;
    57   if (mPrintPreview || mParent) {
    58     mDocShell = aDocShell;
    59   } else {
    60     mTreeOwner = do_GetInterface(aDocShell);
    61     // Create a container docshell for printing.
    62     mDocShell = do_CreateInstance("@mozilla.org/docshell;1");
    63     NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
    64     mDidCreateDocShell = true;
    65     mDocShell->SetItemType(aDocShell->ItemType());
    66     mDocShell->SetTreeOwner(mTreeOwner);
    67   }
    68   NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
    70   nsCOMPtr<nsIDOMDocument> dummy = do_GetInterface(mDocShell);
    71   nsCOMPtr<nsIContentViewer> viewer;
    72   mDocShell->GetContentViewer(getter_AddRefs(viewer));
    73   NS_ENSURE_STATE(viewer);
    75   nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
    76   NS_ENSURE_STATE(doc);
    78   if (mParent) {
    79     nsCOMPtr<nsPIDOMWindow> window = doc->GetWindow();
    80     if (window) {
    81       mContent = do_QueryInterface(window->GetFrameElementInternal());
    82     }
    83     mDocument = doc;
    84     return NS_OK;
    85   }
    87   mDocument = doc->CreateStaticClone(mDocShell);
    88   nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument);
    89   NS_ENSURE_STATE(clonedDOMDoc);
    91   viewer->SetDOMDocument(clonedDOMDoc);
    92   return NS_OK;
    93 }
    95 //------------------------------------------------------------------
    96 // Resets PO by destroying the presentation
    97 void 
    98 nsPrintObject::DestroyPresentation()
    99 {
   100   if (mPresShell) {
   101     mPresShell->EndObservingDocument();
   102     nsAutoScriptBlocker scriptBlocker;
   103     nsCOMPtr<nsIPresShell> shell = mPresShell;
   104     mPresShell = nullptr;
   105     shell->Destroy();
   106   }
   107   mPresContext = nullptr;
   108   mViewManager = nullptr;
   109 }

mercurial