tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsPrintObject.cpp (2807B)


      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 
      9 #include "mozilla/PresShell.h"
     10 #include "mozilla/dom/BrowsingContext.h"
     11 #include "mozilla/dom/Document.h"
     12 #include "mozilla/dom/Element.h"
     13 #include "nsComponentManagerUtils.h"
     14 #include "nsContentUtils.h"  // for nsAutoScriptBlocker
     15 #include "nsDocShell.h"
     16 #include "nsGkAtoms.h"
     17 #include "nsIBaseWindow.h"
     18 #include "nsIDocumentViewer.h"
     19 #include "nsIInterfaceRequestorUtils.h"
     20 #include "nsPIDOMWindow.h"
     21 #include "nsPresContext.h"
     22 
     23 using namespace mozilla;
     24 using mozilla::dom::BrowsingContext;
     25 using mozilla::dom::Document;
     26 using mozilla::dom::Element;
     27 using mozilla::dom::Selection;
     28 
     29 //---------------------------------------------------
     30 //-- nsPrintObject Class Impl
     31 //---------------------------------------------------
     32 nsPrintObject::nsPrintObject(nsIDocShell& aDocShell, Document& aDoc,
     33                             nsPrintObject* aParent)
     34    : mDocShell(&aDocShell), mDocument(&aDoc), mParent(aParent) {
     35  MOZ_COUNT_CTOR(nsPrintObject);
     36  MOZ_ASSERT(aDoc.IsStaticDocument());
     37 
     38  if (!aParent) {
     39    // We are a root nsPrintObject.
     40    // Ensure the document has no presentation.
     41    DestroyPresentation();
     42  } else {
     43    // We are a nested nsPrintObject.
     44    nsCOMPtr<nsPIDOMWindowOuter> window = aDoc.GetWindow();
     45    mContent = window->GetFrameElementInternal();
     46  }
     47 }
     48 
     49 nsPrintObject::~nsPrintObject() {
     50  MOZ_COUNT_DTOR(nsPrintObject);
     51 
     52  DestroyPresentation();
     53  mDocShell = nullptr;
     54  mTreeOwner = nullptr;  // mTreeOwner must be released after mDocShell;
     55 }
     56 
     57 //------------------------------------------------------------------
     58 // Resets PO by destroying the presentation
     59 void nsPrintObject::DestroyPresentation() {
     60  if (mDocument) {
     61    if (RefPtr<PresShell> ps = mDocument->GetPresShell()) {
     62      MOZ_DIAGNOSTIC_ASSERT(!mPresShell || ps == mPresShell);
     63      mPresShell = nullptr;
     64      nsAutoScriptBlocker scriptBlocker;
     65      ps->EndObservingDocument();
     66      ps->Destroy();
     67    }
     68  }
     69  mPresShell = nullptr;
     70  mPresContext = nullptr;
     71 }
     72 
     73 void nsPrintObject::EnablePrinting(bool aEnable) {
     74  mPrintingIsEnabled = aEnable;
     75 
     76  for (const UniquePtr<nsPrintObject>& kid : mKids) {
     77    kid->EnablePrinting(aEnable);
     78  }
     79 }
     80 
     81 bool nsPrintObject::HasSelection() const {
     82  return mDocument && mDocument->GetProperty(nsGkAtoms::printselectionranges);
     83 }
     84 
     85 void nsPrintObject::EnablePrintingSelectionOnly() {
     86  mPrintingIsEnabled = HasSelection();
     87 
     88  for (const UniquePtr<nsPrintObject>& kid : mKids) {
     89    kid->EnablePrintingSelectionOnly();
     90  }
     91 }