nsPrintObject.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/. */
  5. #include "nsPrintObject.h"
  6. #include "nsIContentViewer.h"
  7. #include "nsIDOMDocument.h"
  8. #include "nsIDOMElement.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"
  17. #include "nsIWidget.h"
  18. #include "mozilla/Unused.h"
  19. #include "mozilla/dom/Element.h"
  20. //---------------------------------------------------
  21. //-- nsPrintObject Class Impl
  22. //---------------------------------------------------
  23. nsPrintObject::nsPrintObject() :
  24. mContent(nullptr), mFrameType(eFrame), mParent(nullptr),
  25. mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false),
  26. mInvisible(false), mDidCreateDocShell(false),
  27. mShrinkRatio(1.0), mZoomRatio(1.0)
  28. {
  29. MOZ_COUNT_CTOR(nsPrintObject);
  30. }
  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. }
  38. DestroyPresentation();
  39. if (mDidCreateDocShell && mDocShell) {
  40. nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
  41. if (baseWin) {
  42. baseWin->Destroy();
  43. }
  44. }
  45. mDocShell = nullptr;
  46. mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
  47. }
  48. //------------------------------------------------------------------
  49. nsresult
  50. nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc,
  51. bool aPrintPreview)
  52. {
  53. mPrintPreview = aPrintPreview;
  54. if (mPrintPreview || mParent) {
  55. mDocShell = aDocShell;
  56. } else {
  57. mTreeOwner = do_GetInterface(aDocShell);
  58. // Create a container docshell for printing.
  59. mDocShell = do_CreateInstance("@mozilla.org/docshell;1");
  60. NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
  61. mDidCreateDocShell = true;
  62. mDocShell->SetItemType(aDocShell->ItemType());
  63. mDocShell->SetTreeOwner(mTreeOwner);
  64. }
  65. NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
  66. // Keep the document related to this docshell alive
  67. nsCOMPtr<nsIDOMDocument> dummy = do_GetInterface(mDocShell);
  68. mozilla::Unused << dummy;
  69. nsCOMPtr<nsIContentViewer> viewer;
  70. mDocShell->GetContentViewer(getter_AddRefs(viewer));
  71. NS_ENSURE_STATE(viewer);
  72. nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
  73. NS_ENSURE_STATE(doc);
  74. if (mParent) {
  75. nsCOMPtr<nsPIDOMWindowOuter> window = doc->GetWindow();
  76. if (window) {
  77. mContent = window->GetFrameElementInternal();
  78. }
  79. mDocument = doc;
  80. return NS_OK;
  81. }
  82. mDocument = doc->CreateStaticClone(mDocShell);
  83. nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument);
  84. NS_ENSURE_STATE(clonedDOMDoc);
  85. viewer->SetDOMDocument(clonedDOMDoc);
  86. return NS_OK;
  87. }
  88. //------------------------------------------------------------------
  89. // Resets PO by destroying the presentation
  90. void
  91. nsPrintObject::DestroyPresentation()
  92. {
  93. if (mPresShell) {
  94. mPresShell->EndObservingDocument();
  95. nsAutoScriptBlocker scriptBlocker;
  96. nsCOMPtr<nsIPresShell> shell = mPresShell;
  97. mPresShell = nullptr;
  98. shell->Destroy();
  99. }
  100. mPresContext = nullptr;
  101. mViewManager = nullptr;
  102. }