Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/printing/nsPrintObject.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsPrintObject.h"
8
#include "nsIContentViewer.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
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), mPrintPreview(false), mDidCreateDocShell(false),
25
  mShrinkRatio(1.0), mZoomRatio(1.0)
26
0
{
27
0
  MOZ_COUNT_CTOR(nsPrintObject);
28
0
}
29
30
31
nsPrintObject::~nsPrintObject()
32
0
{
33
0
  MOZ_COUNT_DTOR(nsPrintObject);
34
0
35
0
  DestroyPresentation();
36
0
  if (mDidCreateDocShell && mDocShell) {
37
0
    nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
38
0
    if (baseWin) {
39
0
      baseWin->Destroy();
40
0
    }
41
0
  }
42
0
  mDocShell = nullptr;
43
0
  mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
44
0
}
45
46
//------------------------------------------------------------------
47
nsresult
48
nsPrintObject::Init(nsIDocShell* aDocShell, nsIDocument* aDoc,
49
                    bool aPrintPreview)
50
0
{
51
0
  NS_ENSURE_STATE(aDoc);
52
0
53
0
  mPrintPreview = aPrintPreview;
54
0
55
0
  if (mPrintPreview || mParent) {
56
0
    mDocShell = aDocShell;
57
0
  } else {
58
0
    mTreeOwner = do_GetInterface(aDocShell);
59
0
    // Create a container docshell for printing.
60
0
    mDocShell = do_CreateInstance("@mozilla.org/docshell;1");
61
0
    NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
62
0
    mDidCreateDocShell = true;
63
0
    mDocShell->SetItemType(aDocShell->ItemType());
64
0
    mDocShell->SetTreeOwner(mTreeOwner);
65
0
  }
66
0
  NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
67
0
68
0
  // Keep the document related to this docshell alive
69
0
  nsCOMPtr<nsIDocument> dummy = do_GetInterface(mDocShell);
70
0
  mozilla::Unused << dummy;
71
0
72
0
  nsCOMPtr<nsIContentViewer> viewer;
73
0
  mDocShell->GetContentViewer(getter_AddRefs(viewer));
74
0
  NS_ENSURE_STATE(viewer);
75
0
76
0
  if (mParent) {
77
0
    nsCOMPtr<nsPIDOMWindowOuter> window = aDoc->GetWindow();
78
0
    if (window) {
79
0
      mContent = window->GetFrameElementInternal();
80
0
    }
81
0
    mDocument = aDoc;
82
0
    return NS_OK;
83
0
  }
84
0
85
0
  mDocument = aDoc->CreateStaticClone(mDocShell);
86
0
  NS_ENSURE_STATE(mDocument);
87
0
88
0
  viewer->SetDocument(mDocument);
89
0
  return NS_OK;
90
0
}
91
92
//------------------------------------------------------------------
93
// Resets PO by destroying the presentation
94
void
95
nsPrintObject::DestroyPresentation()
96
0
{
97
0
  if (mPresShell) {
98
0
    mPresShell->EndObservingDocument();
99
0
    nsAutoScriptBlocker scriptBlocker;
100
0
    nsCOMPtr<nsIPresShell> shell = mPresShell;
101
0
    mPresShell = nullptr;
102
0
    shell->Destroy();
103
0
  }
104
0
  mPresContext = nullptr;
105
0
  mViewManager = nullptr;
106
0
}
107