Backed out 5 changesets (bug 1309272) for OSX static build failures a=backout
authorWes Kocher <wkocher@mozilla.com>
Mon, 28 Nov 2016 13:49:09 -0800
changeset 324491 bc39d8cde9ab7f7615a0098f17396691c7354e86
parent 324490 30798ceb7d632bbb8ec8a0a6901e55a1ef473024
child 324492 67cb3a7529115dad9be5a8a3d4e49176b8775c3b
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersbackout
bugs1309272
milestone53.0a1
backs out5ecb0db80f1531e80fd5b70eceed0e4514da09fd
d2046ba274866207e92400089d249cd47f552a48
31fdab31ab5128a693c0f58fc1b3a2ec86ccb698
a9f6271115de9f3cb7e3bea8f4fcbb04d41ace08
125e6fb37319d4767df61d82af7014094163dccb
Backed out 5 changesets (bug 1309272) for OSX static build failures a=backout Backed out changeset 5ecb0db80f15 (bug 1309272) Backed out changeset d2046ba27486 (bug 1309272) Backed out changeset 31fdab31ab51 (bug 1309272) Backed out changeset a9f6271115de (bug 1309272) Backed out changeset 125e6fb37319 (bug 1309272)
gfx/src/nsDeviceContext.cpp
gfx/src/nsDeviceContext.h
gfx/thebes/PrintTarget.h
gfx/thebes/PrintTargetCG.cpp
gfx/thebes/PrintTargetCG.h
gfx/thebes/PrintTargetCG.mm
gfx/thebes/PrintTargetPS.cpp
gfx/thebes/PrintTargetPS.h
gfx/thebes/PrintTargetSkPDF.cpp
gfx/thebes/PrintTargetSkPDF.h
gfx/thebes/PrintTargetThebes.cpp
gfx/thebes/PrintTargetThebes.h
gfx/thebes/PrintTargetWindows.cpp
gfx/thebes/PrintTargetWindows.h
gfx/thebes/moz.build
widget/cocoa/moz.build
widget/cocoa/nsDeviceContextSpecX.h
widget/cocoa/nsDeviceContextSpecX.mm
--- a/gfx/src/nsDeviceContext.cpp
+++ b/gfx/src/nsDeviceContext.cpp
@@ -235,17 +235,21 @@ nsDeviceContext::FontMetricsDeleted(cons
         mFontCache->FontMetricsDeleted(aFontMetrics);
     }
     return NS_OK;
 }
 
 bool
 nsDeviceContext::IsPrinterContext()
 {
-  return mPrintTarget != nullptr;
+  return mPrintTarget != nullptr
+#ifdef XP_MACOSX
+         || mCachedPrintTarget != nullptr
+#endif
+         ;
 }
 
 void
 nsDeviceContext::SetDPI(double* aScale)
 {
     float dpi = -1.0f;
 
     // Use the printing DC to determine DPI values, if we have one.
@@ -338,52 +342,60 @@ nsDeviceContext::CreateReferenceRenderin
 }
 
 already_AddRefed<gfxContext>
 nsDeviceContext::CreateRenderingContextCommon(bool aWantReferenceContext)
 {
     MOZ_ASSERT(IsPrinterContext());
     MOZ_ASSERT(mWidth > 0 && mHeight > 0);
 
+    RefPtr<PrintTarget> printingTarget = mPrintTarget;
+#ifdef XP_MACOSX
+    // CreateRenderingContext() can be called (on reflow) after EndPage()
+    // but before BeginPage().  On OS X (and only there) mPrintTarget
+    // will in this case be null, because OS X printing surfaces are
+    // per-page, and therefore only truly valid between calls to BeginPage()
+    // and EndPage().  But we can get away with fudging things here, if need
+    // be, by using a cached copy.
+    if (!printingTarget) {
+      printingTarget = mCachedPrintTarget;
+    }
+#endif
+
     // This will usually be null, depending on the pref print.print_via_parent.
     RefPtr<DrawEventRecorder> recorder;
     mDeviceContextSpec->GetDrawEventRecorder(getter_AddRefs(recorder));
 
     RefPtr<gfx::DrawTarget> dt;
     if (aWantReferenceContext) {
-      dt = mPrintTarget->GetReferenceDrawTarget(recorder);
+      dt = printingTarget->GetReferenceDrawTarget(recorder);
     } else {
-      dt = mPrintTarget->MakeDrawTarget(gfx::IntSize(mWidth, mHeight), recorder);
+      dt = printingTarget->MakeDrawTarget(gfx::IntSize(mWidth, mHeight), recorder);
     }
 
     if (!dt || !dt->IsValid()) {
       gfxCriticalNote
         << "Failed to create draw target in device context sized "
-        << mWidth << "x" << mHeight << " and pointer "
-        << hexa(mPrintTarget);
+        << mWidth << "x" << mHeight << " and pointers "
+        << hexa(mPrintTarget) << " and " << hexa(printingTarget);
       return nullptr;
     }
 
 #ifdef XP_MACOSX
-    // The CGContextRef provided by PMSessionGetCGGraphicsContext is
-    // write-only, so we need to prevent gfxContext::PushGroupAndCopyBackground
-    // trying to read from it or else we'll crash.
-    // XXXjwatt Consider adding a MakeDrawTarget override to PrintTargetCG and
-    // moving this AddUserData call there.
     dt->AddUserData(&gfxContext::sDontUseAsSourceKey, dt, nullptr);
 #endif
     dt->AddUserData(&sDisablePixelSnapping, (void*)0x1, nullptr);
 
     RefPtr<gfxContext> pContext = gfxContext::CreateOrNull(dt);
     MOZ_ASSERT(pContext); // already checked draw target above
 
     gfxMatrix transform;
-    if (mPrintTarget->RotateNeededForLandscape()) {
+    if (printingTarget->RotateNeededForLandscape()) {
       // Rotate page 90 degrees to draw landscape page on portrait paper
-      IntSize size = mPrintTarget->GetSize();
+      IntSize size = printingTarget->GetSize();
       transform.Translate(gfxPoint(0, size.width));
       gfxMatrix rotate(0, -1,
                        1,  0,
                        0,  0);
       transform = rotate * transform;
     }
     transform.Scale(mPrintingScale, mPrintingScale);
 
@@ -476,79 +488,95 @@ nsDeviceContext::InitForPrinting(nsIDevi
 }
 
 nsresult
 nsDeviceContext::BeginDocument(const nsAString& aTitle,
                                const nsAString& aPrintToFileName,
                                int32_t          aStartPage,
                                int32_t          aEndPage)
 {
-    nsresult rv = mPrintTarget->BeginPrinting(aTitle, aPrintToFileName,
-                                              aStartPage, aEndPage);
+    nsresult rv = mPrintTarget->BeginPrinting(aTitle, aPrintToFileName);
 
     if (NS_SUCCEEDED(rv) && mDeviceContextSpec) {
       rv = mDeviceContextSpec->BeginDocument(aTitle, aPrintToFileName,
                                              aStartPage, aEndPage);
     }
 
     return rv;
 }
 
 
 nsresult
 nsDeviceContext::EndDocument(void)
 {
     nsresult rv = NS_OK;
 
-    rv = mPrintTarget->EndPrinting();
-    if (NS_SUCCEEDED(rv)) {
-        mPrintTarget->Finish();
+    if (mPrintTarget) {
+        rv = mPrintTarget->EndPrinting();
+        if (NS_SUCCEEDED(rv))
+            mPrintTarget->Finish();
     }
 
     if (mDeviceContextSpec)
         mDeviceContextSpec->EndDocument();
 
-    mPrintTarget = nullptr;
-
     return rv;
 }
 
 
 nsresult
 nsDeviceContext::AbortDocument(void)
 {
     nsresult rv = mPrintTarget->AbortPrinting();
 
     if (mDeviceContextSpec)
         mDeviceContextSpec->EndDocument();
 
-    mPrintTarget = nullptr;
-
     return rv;
 }
 
 
 nsresult
 nsDeviceContext::BeginPage(void)
 {
     nsresult rv = NS_OK;
 
     if (mDeviceContextSpec)
         rv = mDeviceContextSpec->BeginPage();
 
     if (NS_FAILED(rv)) return rv;
 
-    return mPrintTarget->BeginPage();
+#ifdef XP_MACOSX
+    // We need to get a new surface for each page on the Mac, as the
+    // CGContextRefs are only good for one page.
+    mPrintTarget = mDeviceContextSpec->MakePrintTarget();
+#endif
+
+    rv = mPrintTarget->BeginPage();
+
+    return rv;
 }
 
 nsresult
 nsDeviceContext::EndPage(void)
 {
     nsresult rv = mPrintTarget->EndPage();
 
+#ifdef XP_MACOSX
+    // We need to release the CGContextRef in the surface here, plus it's
+    // not something you would want anyway, as these CGContextRefs are only
+    // good for one page.  But we need to keep a cached reference to it, since
+    // CreateRenderingContext() may try to access it when mPrintTarget
+    // would normally be null.  See bug 665218.  If we just stop nulling out
+    // mPrintTarget here (and thereby make that our cached copy), we'll
+    // break all our null checks on mPrintTarget.  See bug 684622.
+    mCachedPrintTarget = mPrintTarget;
+    mPrintTarget = nullptr;
+#endif
+
     if (mDeviceContextSpec)
         mDeviceContextSpec->EndPage();
 
     return rv;
 }
 
 void
 nsDeviceContext::ComputeClientRectUsingScreen(nsRect* outRect)
@@ -628,16 +656,20 @@ nsDeviceContext::FindScreen(nsIScreen** 
     if (!(*outScreen)) {
         mScreenManager->GetPrimaryScreen(outScreen);
     }
 }
 
 bool
 nsDeviceContext::CalcPrintingSize()
 {
+    if (!mPrintTarget) {
+        return (mWidth > 0 && mHeight > 0);
+    }
+
     gfxSize size = mPrintTarget->GetSize();
     // For printing, CSS inches and physical inches are identical
     // so it doesn't matter which we use here
     mWidth = NSToCoordRound(size.width * AppUnitsPerPhysicalInch()
                             / POINTS_PER_INCH_FLOAT);
     mHeight = NSToCoordRound(size.height * AppUnitsPerPhysicalInch()
                              / POINTS_PER_INCH_FLOAT);
 
--- a/gfx/src/nsDeviceContext.h
+++ b/gfx/src/nsDeviceContext.h
@@ -295,14 +295,17 @@ private:
     float    mFullZoom;
     float    mPrintingScale;
 
     RefPtr<nsFontCache>            mFontCache;
     nsCOMPtr<nsIWidget>            mWidget;
     nsCOMPtr<nsIScreenManager>     mScreenManager;
     nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
     RefPtr<PrintTarget>            mPrintTarget;
+#ifdef XP_MACOSX
+    RefPtr<PrintTarget>            mCachedPrintTarget;
+#endif
 #ifdef DEBUG
     bool mIsInitialized;
 #endif
 };
 
 #endif /* _NS_DEVICECONTEXT_H_ */
--- a/gfx/thebes/PrintTarget.h
+++ b/gfx/thebes/PrintTarget.h
@@ -25,19 +25,17 @@ class DrawEventRecorder;
  */
 class PrintTarget {
 public:
 
   NS_INLINE_DECL_REFCOUNTING(PrintTarget);
 
   /// Must be matched 1:1 by an EndPrinting/AbortPrinting call.
   virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) {
+                                 const nsAString& aPrintToFileName) {
     return NS_OK;
   }
   virtual nsresult EndPrinting() {
     return NS_OK;
   }
   virtual nsresult AbortPrinting() {
 #ifdef DEBUG
     mHasActivePage = false;
rename from gfx/thebes/PrintTargetCG.mm
rename to gfx/thebes/PrintTargetCG.cpp
--- a/gfx/thebes/PrintTargetCG.mm
+++ b/gfx/thebes/PrintTargetCG.cpp
@@ -3,62 +3,71 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "PrintTargetCG.h"
 
 #include "cairo.h"
 #include "cairo-quartz.h"
 #include "mozilla/gfx/HelpersCairo.h"
-#include "nsObjCExceptions.h"
 
 namespace mozilla {
 namespace gfx {
 
-PrintTargetCG::PrintTargetCG(PMPrintSession aPrintSession,
-                             PMPageFormat aPageFormat,
-                             PMPrintSettings aPrintSettings,
+PrintTargetCG::PrintTargetCG(cairo_surface_t* aCairoSurface,
                              const IntSize& aSize)
-  : PrintTarget(/* aCairoSurface */ nullptr, aSize)
-  , mPrintSession(aPrintSession)
-  , mPageFormat(aPageFormat)
-  , mPrintSettings(aPrintSettings)
+  : PrintTarget(aCairoSurface, aSize)
 {
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
-
-  ::PMRetain(mPrintSession);
-
   // TODO: Add memory reporting like gfxQuartzSurface.
   //RecordMemoryUsed(mSize.height * 4 + sizeof(gfxQuartzSurface));
-
-  NS_OBJC_END_TRY_ABORT_BLOCK;
-}
-
-PrintTargetCG::~PrintTargetCG()
-{
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
-
-  if (mPrintSession)
-    ::PMRelease(mPrintSession);
-
-  NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 /* static */ already_AddRefed<PrintTargetCG>
-PrintTargetCG::CreateOrNull(PMPrintSession aPrintSession,
-                            PMPageFormat aPageFormat,
-                            PMPrintSettings aPrintSettings,
-                            const IntSize& aSize)
+PrintTargetCG::CreateOrNull(const IntSize& aSize, gfxImageFormat aFormat)
 {
   if (!Factory::CheckSurfaceSize(aSize)) {
     return nullptr;
   }
 
-  RefPtr<PrintTargetCG> target = new PrintTargetCG(aPrintSession, aPageFormat,
-                                                   aPrintSettings, aSize);
+  unsigned int width = static_cast<unsigned int>(aSize.width);
+  unsigned int height = static_cast<unsigned int>(aSize.height);
+
+  cairo_format_t cformat = GfxFormatToCairoFormat(aFormat);
+  cairo_surface_t* surface =
+    cairo_quartz_surface_create(cformat, width, height);
+
+  if (cairo_surface_status(surface)) {
+    return nullptr;
+  }
+
+  // The new object takes ownership of our surface reference.
+  RefPtr<PrintTargetCG> target = new PrintTargetCG(surface, aSize);
+
+  return target.forget();
+}
+
+/* static */ already_AddRefed<PrintTargetCG>
+PrintTargetCG::CreateOrNull(CGContextRef aContext, const IntSize& aSize)
+{
+  if (!Factory::CheckSurfaceSize(aSize)) {
+    return nullptr;
+  }
+
+  unsigned int width = static_cast<unsigned int>(aSize.width);
+  unsigned int height = static_cast<unsigned int>(aSize.height);
+
+  cairo_surface_t* surface =
+    cairo_quartz_surface_create_for_cg_context(aContext, width, height);
+
+  if (cairo_surface_status(surface)) {
+    return nullptr;
+  }
+
+  // The new object takes ownership of our surface reference.
+  RefPtr<PrintTargetCG> target = new PrintTargetCG(surface, aSize);
 
   return target.forget();
 }
 
 static size_t
 PutBytesNull(void* info, const void* buffer, size_t count)
 {
   return count;
@@ -102,111 +111,10 @@ PrintTargetCG::GetReferenceDrawTarget(Dr
       }
     }
 
     mRefDT = dt.forget();
   }
   return do_AddRef(mRefDT);
 }
 
-nsresult
-PrintTargetCG::BeginPrinting(const nsAString& aTitle,
-                             const nsAString& aPrintToFileName,
-                             int32_t aStartPage,
-                             int32_t aEndPage)
-{
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-  OSStatus status;
-  status = ::PMSetFirstPage(mPrintSettings, aStartPage, false);
-  NS_ASSERTION(status == noErr, "PMSetFirstPage failed");
-  status = ::PMSetLastPage(mPrintSettings, aEndPage, false);
-  NS_ASSERTION(status == noErr, "PMSetLastPage failed");
-
-  status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat);
-
-  return status == noErr ? NS_OK : NS_ERROR_ABORT;
-
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
-}
-
-nsresult
-PrintTargetCG::EndPrinting()
-{
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-  ::PMSessionEndDocumentNoDialog(mPrintSession);
-  return NS_OK;
-
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
-}
-
-nsresult
-PrintTargetCG::AbortPrinting()
-{
-#ifdef DEBUG
-  mHasActivePage = false;
-#endif
-  return EndPrinting();
-}
-
-nsresult
-PrintTargetCG::BeginPage()
-{
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-  PMSessionError(mPrintSession);
-  OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL);
-  if (status != noErr) {
-    return NS_ERROR_ABORT;
-  }
-
-  CGContextRef context;
-  // This call will fail if it wasn't called between the PMSessionBeginPage/
-  // PMSessionEndPage calls:
-  ::PMSessionGetCGGraphicsContext(mPrintSession, &context);
-
-  if (!context) {
-    return NS_ERROR_FAILURE;
-  }
-
-  unsigned int width = static_cast<unsigned int>(mSize.width);
-  unsigned int height = static_cast<unsigned int>(mSize.height);
-
-  // Initially, origin is at bottom-left corner of the paper.
-  // Here, we translate it to top-left corner of the paper.
-  CGContextTranslateCTM(context, 0, height);
-  CGContextScaleCTM(context, 1.0, -1.0);
-
-  cairo_surface_t* surface =
-    cairo_quartz_surface_create_for_cg_context(context, width, height);
-
-  if (cairo_surface_status(surface)) {
-    return NS_ERROR_FAILURE;
-  }
-
-  mCairoSurface = surface;
-
-  return PrintTarget::BeginPage();
-
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
-}
-
-nsresult
-PrintTargetCG::EndPage()
-{
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-  cairo_surface_finish(mCairoSurface);
-  mCairoSurface = nullptr;
-
-  OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
-  if (status != noErr) {
-    return NS_ERROR_ABORT;
-  }
-
-  return PrintTarget::EndPage();
-
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
-}
-
 } // namespace gfx
 } // namespace mozilla
--- a/gfx/thebes/PrintTargetCG.h
+++ b/gfx/thebes/PrintTargetCG.h
@@ -9,46 +9,34 @@
 #include <Carbon/Carbon.h>
 #include "PrintTarget.h"
 
 namespace mozilla {
 namespace gfx {
 
 /**
  * CoreGraphics printing target.
+ *
+ * Note that a CGContextRef obtained from PMSessionGetCGGraphicsContext is
+ * valid only for the current page.  As a consequence instances of this class
+ * should only be used to print a single page.
  */
 class PrintTargetCG final : public PrintTarget
 {
 public:
   static already_AddRefed<PrintTargetCG>
-  CreateOrNull(PMPrintSession aPrintSession,
-               PMPageFormat aPageFormat,
-               PMPrintSettings aPrintSettings,
-               const IntSize& aSize);
+  CreateOrNull(const IntSize& aSize, gfxImageFormat aFormat);
 
-  virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) final;
-  virtual nsresult EndPrinting() final;
-  virtual nsresult AbortPrinting() final;
-  virtual nsresult BeginPage() final;
-  virtual nsresult EndPage() final;
+  static already_AddRefed<PrintTargetCG>
+  CreateOrNull(CGContextRef aContext, const IntSize& aSize);
 
   virtual already_AddRefed<DrawTarget>
   GetReferenceDrawTarget(DrawEventRecorder* aRecorder) final;
 
 private:
-  PrintTargetCG(PMPrintSession aPrintSession,
-                PMPageFormat aPageFormat,
-                PMPrintSettings aPrintSettings,
+  PrintTargetCG(cairo_surface_t* aCairoSurface,
                 const IntSize& aSize);
-  ~PrintTargetCG();
-
-  PMPrintSession mPrintSession;
-  PMPageFormat mPageFormat;
-  PMPrintSettings mPrintSettings;
 };
 
 } // namespace gfx
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_PRINTTARGETCG_H */
--- a/gfx/thebes/PrintTargetPS.cpp
+++ b/gfx/thebes/PrintTargetPS.cpp
@@ -74,19 +74,17 @@ PrintTargetPS::CreateOrNull(nsIOutputStr
   // The new object takes ownership of our surface reference.
   RefPtr<PrintTargetPS> target = new PrintTargetPS(surface, aSizeInPoints,
                                                    aStream, aOrientation);
   return target.forget();
 }
 
 nsresult
 PrintTargetPS::BeginPrinting(const nsAString& aTitle,
-                             const nsAString& aPrintToFileName,
-                             int32_t aStartPage,
-                             int32_t aEndPage)
+                             const nsAString& aPrintToFileName)
 {
   if (mOrientation == PORTRAIT) {
     cairo_ps_surface_dsc_comment(mCairoSurface, "%%Orientation: Portrait");
   } else {
     cairo_ps_surface_dsc_comment(mCairoSurface, "%%Orientation: Landscape");
   }
   return NS_OK;
 }
--- a/gfx/thebes/PrintTargetPS.h
+++ b/gfx/thebes/PrintTargetPS.h
@@ -24,19 +24,17 @@ public:
   };
 
   static already_AddRefed<PrintTargetPS>
   CreateOrNull(nsIOutputStream *aStream,
                IntSize aSizeInPoints,
                PageOrientation aOrientation);
 
   virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) override;
+                                 const nsAString& aPrintToFileName) override;
   virtual nsresult EndPage() override;
   virtual void Finish() override;
 
   virtual bool GetRotateForLandscape() {
     return (mOrientation == LANDSCAPE);
   }
 
 private:
deleted file mode 100644
--- a/gfx/thebes/PrintTargetSkPDF.cpp
+++ /dev/null
@@ -1,159 +0,0 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#include "PrintTargetSkPDF.h"
-
-#include "mozilla/gfx/2D.h"
-#include "nsString.h"
-#include <vector>
-
-namespace mozilla {
-namespace gfx {
-
-PrintTargetSkPDF::PrintTargetSkPDF(const IntSize& aSize,
-                                   UniquePtr<SkWStream> aStream)
-  : PrintTarget(/* not using cairo_surface_t */ nullptr, aSize)
-  , mOStream(Move(aStream))
-  , mPageCanvas(nullptr)
-{
-}
-
-PrintTargetSkPDF::~PrintTargetSkPDF()
-{
-  Finish(); // ensure stream is flushed
-
-  // Make sure mPDFDoc and mRefPDFDoc are destroyed before our member streams
-  // (which they wrap) are destroyed:
-  mPDFDoc = nullptr;
-  mRefPDFDoc = nullptr;
-}
-
-/* static */ already_AddRefed<PrintTargetSkPDF>
-PrintTargetSkPDF::CreateOrNull(UniquePtr<SkWStream> aStream,
-                               const IntSize& aSizeInPoints)
-{
-  return do_AddRef(new PrintTargetSkPDF(aSizeInPoints, Move(aStream)));
-}
-
-nsresult
-PrintTargetSkPDF::BeginPrinting(const nsAString& aTitle,
-                                const nsAString& aPrintToFileName,
-                                int32_t aStartPage,
-                                int32_t aEndPage)
-{
-  // We need to create the SkPDFDocument here rather than in CreateOrNull
-  // because it's only now that we are given aTitle which we want for the
-  // PDF metadata.
-
-  SkDocument::PDFMetadata metadata;
-  metadata.fTitle = NS_ConvertUTF16toUTF8(aTitle).get();
-  metadata.fCreator = "Firefox";
-  SkTime::DateTime now;
-  SkTime::GetDateTime(&now);
-  metadata.fCreation.fEnabled = true;
-  metadata.fCreation.fDateTime = now;
-  metadata.fModified.fEnabled = true;
-  metadata.fModified.fDateTime = now;
-
-  // SkDocument stores a non-owning raw pointer to aStream
-  mPDFDoc = SkDocument::MakePDF(mOStream.get(), SK_ScalarDefaultRasterDPI,
-                                metadata, /*jpegEncoder*/ nullptr, true);
-
-  return mPDFDoc ? NS_OK : NS_ERROR_FAILURE;
-}
-
-nsresult
-PrintTargetSkPDF::BeginPage()
-{
-  mPageCanvas = sk_ref_sp(mPDFDoc->beginPage(mSize.width, mSize.height));
-
-  return !mPageCanvas ? NS_ERROR_FAILURE : PrintTarget::BeginPage();
-}
-
-nsresult
-PrintTargetSkPDF::EndPage()
-{
-  mPageCanvas = nullptr;
-  mPageDT = nullptr;
-  return PrintTarget::EndPage();
-}
-
-nsresult
-PrintTargetSkPDF::EndPrinting()
-{
-  mPDFDoc->close();
-  if (mRefPDFDoc) {
-    mRefPDFDoc->close();
-  }
-  mPageCanvas = nullptr;
-  mPageDT = nullptr;
-  return NS_OK;
-}
-
-void
-PrintTargetSkPDF::Finish()
-{
-  if (mIsFinished) {
-    return;
-  }
-  mOStream->flush();
-  PrintTarget::Finish();
-}
-
-already_AddRefed<DrawTarget>
-PrintTargetSkPDF::MakeDrawTarget(const IntSize& aSize,
-                                 DrawEventRecorder* aRecorder)
-{
-  if (aRecorder) {
-    return PrintTarget::MakeDrawTarget(aSize, aRecorder);
-  }
-  //MOZ_ASSERT(aSize == mSize, "Should mPageCanvas size match?");
-  if (!mPageCanvas) {
-    return nullptr;
-  }
-  mPageDT = Factory::CreateDrawTargetWithSkCanvas(mPageCanvas.get());
-  if (!mPageDT) {
-    mPageCanvas = nullptr;
-    return nullptr;
-  }
-  return do_AddRef(mPageDT);
-}
-
-already_AddRefed<DrawTarget>
-PrintTargetSkPDF::GetReferenceDrawTarget(DrawEventRecorder* aRecorder)
-{
-  if (!mRefDT) {
-    SkDocument::PDFMetadata metadata;
-    // SkDocument stores a non-owning raw pointer to aStream
-    mRefPDFDoc = SkDocument::MakePDF(&mRefOStream,
-                                     SK_ScalarDefaultRasterDPI,
-                                     metadata, nullptr, true);
-    if (!mRefPDFDoc) {
-      return nullptr;
-    }
-    mRefCanvas = sk_ref_sp(mRefPDFDoc->beginPage(mSize.width, mSize.height));
-    if (!mRefCanvas) {
-      return nullptr;
-    }
-    RefPtr<DrawTarget> dt =
-      Factory::CreateDrawTargetWithSkCanvas(mRefCanvas.get());
-    if (!dt) {
-      return nullptr;
-    }
-
-    if (aRecorder) {
-      dt = CreateRecordingDrawTarget(aRecorder, dt);
-      if (!dt || !dt->IsValid()) {
-        return nullptr;
-      }
-    }
-
-    mRefDT = dt.forget();
-  }
-  return do_AddRef(mRefDT);
-}
-
-} // namespace gfx
-} // namespace mozilla
deleted file mode 100644
--- a/gfx/thebes/PrintTargetSkPDF.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-#ifndef MOZILLA_GFX_PRINTTARGETSKPDF_H
-#define MOZILLA_GFX_PRINTTARGETSKPDF_H
-
-#include "mozilla/UniquePtr.h"
-#include "nsCOMPtr.h"
-#include "PrintTarget.h"
-#include "SkCanvas.h"
-#include "SkDocument.h"
-#include "SkStream.h"
-
-namespace mozilla {
-namespace gfx {
-
-/**
- * Skia PDF printing target.
- */
-class PrintTargetSkPDF final : public PrintTarget
-{
-public:
-  // The returned PrintTargetSkPDF keeps a raw pointer to the passed SkWStream
-  // but does not own it.  Callers are responsible for ensuring that passed
-  // stream outlives the returned PrintTarget.
-  static already_AddRefed<PrintTargetSkPDF>
-  CreateOrNull(UniquePtr<SkWStream> aStream,
-               const IntSize& aSizeInPoints);
-
-  virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) override;
-  virtual nsresult EndPrinting() override;
-  virtual void Finish() override;
-
-  virtual nsresult BeginPage() override;
-  virtual nsresult EndPage() override;
-
-  virtual already_AddRefed<DrawTarget>
-  MakeDrawTarget(const IntSize& aSize,
-                 DrawEventRecorder* aRecorder = nullptr) final;
-
-  virtual already_AddRefed<DrawTarget>
-  GetReferenceDrawTarget(DrawEventRecorder* aRecorder) override final;
-
-private:
-  PrintTargetSkPDF(const IntSize& aSize,
-                   UniquePtr<SkWStream> aStream);
-  virtual ~PrintTargetSkPDF();
-
-  // Do not hand out references to this object.  It holds a non-owning
-  // reference to mOStreame, so must not outlive mOStream.
-  sk_sp<SkDocument> mPDFDoc;
-
-  // The stream that the SkDocument outputs to.
-  UniquePtr<SkWStream> mOStream;
-
-  // The current page's SkCanvas and its wrapping DrawTarget:
-  sk_sp<SkCanvas> mPageCanvas;
-  RefPtr<DrawTarget> mPageDT;
-
-  // Members needed to provide a reference DrawTarget:
-  sk_sp<SkDocument> mRefPDFDoc;
-  sk_sp<SkCanvas> mRefCanvas;
-  SkDynamicMemoryWStream mRefOStream;
-  RefPtr<DrawTarget> mRefDT;
-};
-
-} // namespace gfx
-} // namespace mozilla
-
-#endif /* MOZILLA_GFX_PRINTTARGETSKPDF_H */
--- a/gfx/thebes/PrintTargetThebes.cpp
+++ b/gfx/thebes/PrintTargetThebes.cpp
@@ -73,19 +73,17 @@ PrintTargetThebes::GetReferenceDrawTarge
     }
     mRefDT = dt->CreateSimilarDrawTarget(IntSize(1,1), dt->GetFormat());
   }
   return do_AddRef(mRefDT);
 }
 
 nsresult
 PrintTargetThebes::BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage)
+                                 const nsAString& aPrintToFileName)
 {
   return mGfxSurface->BeginPrinting(aTitle, aPrintToFileName);
 }
 
 nsresult
 PrintTargetThebes::EndPrinting()
 {
   return mGfxSurface->EndPrinting();
--- a/gfx/thebes/PrintTargetThebes.h
+++ b/gfx/thebes/PrintTargetThebes.h
@@ -25,19 +25,17 @@ namespace gfx {
  */
 class PrintTargetThebes final : public PrintTarget {
 public:
 
   static already_AddRefed<PrintTargetThebes>
   CreateOrNull(gfxASurface* aSurface);
 
   virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) override;
+                                 const nsAString& aPrintToFileName) override;
   virtual nsresult EndPrinting() override;
   virtual nsresult AbortPrinting() override;
   virtual nsresult BeginPage() override;
   virtual nsresult EndPage() override;
   virtual void Finish() override;
 
   virtual already_AddRefed<DrawTarget>
   MakeDrawTarget(const IntSize& aSize,
--- a/gfx/thebes/PrintTargetWindows.cpp
+++ b/gfx/thebes/PrintTargetWindows.cpp
@@ -50,19 +50,17 @@ PrintTargetWindows::CreateOrNull(HDC aDC
   RefPtr<PrintTargetWindows> target =
     new PrintTargetWindows(surface, size, aDC);
 
   return target.forget();
 }
 
 nsresult
 PrintTargetWindows::BeginPrinting(const nsAString& aTitle,
-                                  const nsAString& aPrintToFileName,
-                                  int32_t aStartPage,
-                                  int32_t aEndPage)
+                                  const nsAString& aPrintToFileName)
 {
   const uint32_t DOC_TITLE_LENGTH = MAX_PATH - 1;
 
   DOCINFOW docinfo;
 
   nsString titleStr(aTitle);
   if (titleStr.Length() > DOC_TITLE_LENGTH) {
     titleStr.SetLength(DOC_TITLE_LENGTH - 3);
--- a/gfx/thebes/PrintTargetWindows.h
+++ b/gfx/thebes/PrintTargetWindows.h
@@ -19,19 +19,17 @@ namespace gfx {
  */
 class PrintTargetWindows final : public PrintTarget
 {
 public:
   static already_AddRefed<PrintTargetWindows>
   CreateOrNull(HDC aDC);
 
   virtual nsresult BeginPrinting(const nsAString& aTitle,
-                                 const nsAString& aPrintToFileName,
-                                 int32_t aStartPage,
-                                 int32_t aEndPage) override;
+                                 const nsAString& aPrintToFileName) override;
   virtual nsresult EndPrinting() override;
   virtual nsresult AbortPrinting() override;
   virtual nsresult BeginPage() override;
   virtual nsresult EndPage() override;
 
 private:
   PrintTargetWindows(cairo_surface_t* aCairoSurface,
                      const IntSize& aSize,
--- a/gfx/thebes/moz.build
+++ b/gfx/thebes/moz.build
@@ -84,17 +84,17 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'co
         'PrintTargetCG.h',
     ]
     SOURCES += [
         'gfxCoreTextShaper.cpp',
         'gfxMacFont.cpp',
         'gfxPlatformMac.cpp',
         'gfxQuartzNativeDrawing.cpp',
         'gfxQuartzSurface.cpp',
-        'PrintTargetCG.mm',
+        'PrintTargetCG.cpp',
     ]
 elif 'gtk' in CONFIG['MOZ_WIDGET_TOOLKIT']:
     EXPORTS += [
         'gfxFontconfigFonts.h',
         'gfxFT2FontBase.h',
         'gfxGdkNativeRenderer.h',
         'gfxPlatformGtk.h',
     ]
@@ -218,24 +218,16 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'coco
         'gfxMacPlatformFontList.mm',
     ]
 elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'windows':
     UNIFIED_SOURCES += [
         'D3D11Checks.cpp',
         'DeviceManagerDx.cpp',
     ]
 
-if CONFIG['MOZ_ENABLE_SKIA_PDF']:
-    EXPORTS.mozilla.gfx += [
-        'PrintTargetSkPDF.h',
-    ]
-    SOURCES += [
-        'PrintTargetSkPDF.cpp',
-    ]
-
 # We prefer to use ICU for normalization functions, but currently it is only
 # available if we're building with the Intl API enabled:
 if CONFIG['ENABLE_INTL_API']:
     USE_LIBS += [
         'icu',
     ]
 
 include('/ipc/chromium/chromium-config.mozbuild')
--- a/widget/cocoa/moz.build
+++ b/widget/cocoa/moz.build
@@ -86,24 +86,16 @@ FINAL_LIBRARY = 'xul'
 LOCAL_INCLUDES += [
     '/layout/forms',
     '/layout/generic',
     '/layout/style',
     '/layout/xul',
     '/widget',
 ]
 
-if CONFIG['MOZ_ENABLE_SKIA_PDF']:
-    LOCAL_INCLUDES += [
-        # Skia includes because widget code includes PrintTargetSkPDF.h, and that
-        # includes skia headers.
-        '/gfx/skia/skia/include/config',
-        '/gfx/skia/skia/include/core',
-    ]
-
 RESOURCE_FILES.cursors += [
     'cursors/arrowN.png',
     'cursors/arrowN@2x.png',
     'cursors/arrowS.png',
     'cursors/arrowS@2x.png',
     'cursors/cell.png',
     'cursors/cell@2x.png',
     'cursors/colResize.png',
--- a/widget/cocoa/nsDeviceContextSpecX.h
+++ b/widget/cocoa/nsDeviceContextSpecX.h
@@ -19,31 +19,23 @@ public:
 
     NS_IMETHOD Init(nsIWidget *aWidget, nsIPrintSettings* aPS, bool aIsPrintPreview) override;
     virtual already_AddRefed<PrintTarget> MakePrintTarget() final;
     NS_IMETHOD BeginDocument(const nsAString& aTitle,
                              const nsAString& aPrintToFileName,
                              int32_t          aStartPage,
                              int32_t          aEndPage) override;
     NS_IMETHOD EndDocument() override;
-    NS_IMETHOD BeginPage() override {
-      return NS_OK;
-    };
-    NS_IMETHOD EndPage() override {
-      return NS_OK;
-    };
+    NS_IMETHOD BeginPage() override;
+    NS_IMETHOD EndPage() override;
 
     void GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight);
 
 protected:
     virtual ~nsDeviceContextSpecX();
 
 protected:
     PMPrintSession    mPrintSession;              // printing context.
     PMPageFormat      mPageFormat;                // page format.
     PMPrintSettings   mPrintSettings;             // print settings.
-#ifdef MOZ_ENABLE_SKIA_PDF
-    nsCOMPtr<nsIFile> mTempFile;                  // file "print" output is generated to if printing via PDF
-    bool              mPrintViaSkPDF;
-#endif
 };
 
 #endif //nsDeviceContextSpecX_h_
--- a/widget/cocoa/nsDeviceContextSpecX.mm
+++ b/widget/cocoa/nsDeviceContextSpecX.mm
@@ -1,49 +1,34 @@
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsDeviceContextSpecX.h"
 
 #include "mozilla/gfx/PrintTargetCG.h"
-#ifdef MOZ_ENABLE_SKIA_PDF
-#include "mozilla/gfx/PrintTargetSkPDF.h"
-#endif
-#include "mozilla/Preferences.h"
 #include "mozilla/RefPtr.h"
 #include "nsCRT.h"
-#include "nsDirectoryServiceDefs.h"
-#include "nsILocalFileMac.h"
 #include <unistd.h>
 
 #include "nsQueryObject.h"
 #include "nsIServiceManager.h"
 #include "nsPrintSettingsX.h"
 
 // This must be the last include:
 #include "nsObjCExceptions.h"
 
 using namespace mozilla;
-using mozilla::gfx::IntSize;
-using mozilla::gfx::PrintTarget;
-using mozilla::gfx::PrintTargetCG;
-#ifdef MOZ_ENABLE_SKIA_PDF
-using mozilla::gfx::PrintTargetSkPDF;
-#endif
-using mozilla::gfx::SurfaceFormat;
+using namespace mozilla::gfx;
 
 nsDeviceContextSpecX::nsDeviceContextSpecX()
 : mPrintSession(NULL)
 , mPageFormat(kPMNoPageFormat)
 , mPrintSettings(kPMNoPrintSettings)
-#ifdef MOZ_ENABLE_SKIA_PDF
-, mPrintViaSkPDF(false)
-#endif
 {
 }
 
 nsDeviceContextSpecX::~nsDeviceContextSpecX()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   if (mPrintSession)
@@ -64,55 +49,16 @@ NS_IMETHODIMP nsDeviceContextSpecX::Init
   if (!settings)
     return NS_ERROR_NO_INTERFACE;
 
   mPrintSession = settings->GetPMPrintSession();
   ::PMRetain(mPrintSession);
   mPageFormat = settings->GetPMPageFormat();
   mPrintSettings = settings->GetPMPrintSettings();
 
-#ifdef MOZ_ENABLE_SKIA_PDF
-  const nsAdoptingString& printViaPdf =
-    mozilla::Preferences::GetString("print.print_via_pdf_encoder");
-  if (printViaPdf == NS_LITERAL_STRING("skia-pdf")) {
-    // Annoyingly, PMPrinterPrintWithFile does not pay attention to the
-    // kPMDestination* value set in the PMPrintSession; it always sends the PDF
-    // to the specified printer.  This means that if we create the PDF using
-    // SkPDF then we need to manually handle user actions like "Open PDF in
-    // Preview" and "Save as PDF...".
-    // TODO: Currently we do not support using SkPDF for kPMDestinationFax or
-    // kPMDestinationProcessPDF ("Add PDF to iBooks, etc.), and we only support
-    // it for kPMDestinationFile if the destination file is a PDF.
-    // XXX Could PMWorkflowSubmitPDFWithSettings/PMPrinterPrintWithProvider help?
-    OSStatus status = noErr;
-    PMDestinationType destination;
-    status = ::PMSessionGetDestinationType(mPrintSession, mPrintSettings,
-                                           &destination);
-    if (status == noErr) {
-      if (destination == kPMDestinationPrinter ||
-          destination == kPMDestinationPreview){
-        mPrintViaSkPDF = true;
-      } else if (destination == kPMDestinationFile) {
-        CFURLRef destURL;
-        status = ::PMSessionCopyDestinationLocation(mPrintSession,
-                                                    mPrintSettings, &destURL);
-        if (status == noErr) {
-          CFStringRef destPathRef =
-            CFURLCopyFileSystemPath(destURL, kCFURLPOSIXPathStyle);
-          NSString* destPath = (NSString*) destPathRef;
-          NSString* destPathExt = [destPath pathExtension];
-          if ([destPathExt isEqualToString: @"pdf"]) {
-            mPrintViaSkPDF = true;
-          }
-        }
-      }
-    }
-  }
-#endif
-
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(const nsAString& aTitle, 
                                                   const nsAString& aPrintToFileName,
                                                   int32_t          aStartPage, 
@@ -125,107 +71,62 @@ NS_IMETHODIMP nsDeviceContextSpecX::Begi
         ::CFStringCreateWithCharacters(NULL, reinterpret_cast<const UniChar*>(aTitle.BeginReading()),
                                              aTitle.Length());
       if (cfString) {
         ::PMPrintSettingsSetJobName(mPrintSettings, cfString);
         ::CFRelease(cfString);
       }
     }
 
+    OSStatus status;
+    status = ::PMSetFirstPage(mPrintSettings, aStartPage, false);
+    NS_ASSERTION(status == noErr, "PMSetFirstPage failed");
+    status = ::PMSetLastPage(mPrintSettings, aEndPage, false);
+    NS_ASSERTION(status == noErr, "PMSetLastPage failed");
+
+    status = ::PMSessionBeginCGDocumentNoDialog(mPrintSession, mPrintSettings, mPageFormat);
+    if (status != noErr)
+      return NS_ERROR_ABORT;
+
     return NS_OK;
 
     NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 NS_IMETHODIMP nsDeviceContextSpecX::EndDocument()
 {
-  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
-#ifdef MOZ_ENABLE_SKIA_PDF
-  if (mPrintViaSkPDF) {
-    OSStatus status = noErr;
+    ::PMSessionEndDocumentNoDialog(mPrintSession);
+    return NS_OK;
 
-    nsCOMPtr<nsILocalFileMac> tmpPDFFile = do_QueryInterface(mTempFile);
-    if (!tmpPDFFile) {
-      return NS_ERROR_FAILURE;
-    }
-    CFURLRef pdfURL;
-    nsresult rv = tmpPDFFile->GetCFURL(&pdfURL);
-    NS_ENSURE_SUCCESS(rv, rv);
+    NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
 
-    PMDestinationType destination;
-    status = ::PMSessionGetDestinationType(mPrintSession, mPrintSettings,
-                                           &destination);
+NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
+{
+    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
-    switch (destination) {
-    case kPMDestinationPrinter: {
-      PMPrinter currentPrinter = NULL;
-      status = ::PMSessionGetCurrentPrinter(mPrintSession, &currentPrinter);
-      if (status != noErr) {
-        return NS_ERROR_FAILURE;
-      }
-      CFStringRef mimeType = CFSTR("application/pdf");
-      status = ::PMPrinterPrintWithFile(currentPrinter, mPrintSettings,
-                                        mPageFormat, mimeType, pdfURL);
-      break;
-    }
-    case kPMDestinationPreview: {
-      // XXXjwatt Or should we use CocoaFileUtils::RevealFileInFinder(pdfURL);
-      CFStringRef pdfPath = CFURLCopyFileSystemPath(pdfURL,
-                                                    kCFURLPOSIXPathStyle);
-      NSString* path = (NSString*) pdfPath;
-      NSWorkspace* ws = [NSWorkspace sharedWorkspace];
-      [ws openFile: path];
-      break;
-    }
-    case kPMDestinationFile: {
-      CFURLRef destURL;
-      status = ::PMSessionCopyDestinationLocation(mPrintSession,
-                                                  mPrintSettings, &destURL);
-      if (status == noErr) {
-        CFStringRef sourcePathRef =
-          CFURLCopyFileSystemPath(pdfURL, kCFURLPOSIXPathStyle);
-        CFStringRef destPathRef =
-          CFURLCopyFileSystemPath(destURL, kCFURLPOSIXPathStyle);
-        NSString* sourcePath = (NSString*) sourcePathRef;
-        NSString* destPath = (NSString*) destPathRef;
-#ifdef DEBUG
-        NSString* destPathExt = [destPath pathExtension];
-        MOZ_ASSERT([destPathExt isEqualToString: @"pdf"],
-                   "nsDeviceContextSpecX::Init only allows '.pdf' for now");
-        // We could use /usr/sbin/cupsfilter to convert the PDF to PS, but
-        // currently we don't.
-#endif
-        NSFileManager* fileManager = [NSFileManager defaultManager];
-        if ([fileManager fileExistsAtPath:sourcePath]) {
-          NSURL* src = static_cast<NSURL*>(pdfURL);
-          NSURL* dest = static_cast<NSURL*>(destURL);
-          bool ok = [fileManager replaceItemAtURL:dest withItemAtURL:src
-                                 backupItemName:nil
-                                 options:NSFileManagerItemReplacementUsingNewMetadataOnly
-                                 resultingItemURL:nil error:nil];
-          if (!ok) {
-            return NS_ERROR_FAILURE;
-          }
-        }
-      }
-      break;
-    }
-    default:
-      MOZ_ASSERT_UNREACHABLE("nsDeviceContextSpecX::Init doesn't set "
-                             "mPrintViaSkPDF for other values");
-    }
+    PMSessionError(mPrintSession);
+    OSStatus status = ::PMSessionBeginPageNoDialog(mPrintSession, mPageFormat, NULL);
+    if (status != noErr) return NS_ERROR_ABORT;
+    return NS_OK;
+
+    NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+}
 
-    return (status == noErr) ? NS_OK : NS_ERROR_FAILURE;
-  }
-#endif
+NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
+{
+    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
 
-  return NS_OK;
+    OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
+    if (status != noErr) return NS_ERROR_ABORT;
+    return NS_OK;
 
-  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+    NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 void nsDeviceContextSpecX::GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
 {
     NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
     PMRect paperRect;
     ::PMGetAdjustedPaperRect(mPageFormat, &paperRect);
@@ -233,32 +134,32 @@ void nsDeviceContextSpecX::GetPaperRect(
     *aTop = paperRect.top, *aLeft = paperRect.left;
     *aBottom = paperRect.bottom, *aRight = paperRect.right;
 
     NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 already_AddRefed<PrintTarget> nsDeviceContextSpecX::MakePrintTarget()
 {
+    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
+
     double top, left, bottom, right;
     GetPaperRect(&top, &left, &bottom, &right);
     const double width = right - left;
     const double height = bottom - top;
     IntSize size = IntSize::Floor(width, height);
 
-#ifdef MOZ_ENABLE_SKIA_PDF
-    if (mPrintViaSkPDF) {
-      nsresult rv =
-        NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mTempFile));
-      NS_ENSURE_SUCCESS(rv, nullptr);
-      nsAutoCString tempPath("tmp-printing.pdf");
-      mTempFile->AppendNative(tempPath);
-      rv = mTempFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
-      NS_ENSURE_SUCCESS(rv, nullptr);
-      mTempFile->GetNativePath(tempPath);
-      auto stream = MakeUnique<SkFILEWStream>(tempPath.get());
-      return PrintTargetSkPDF::CreateOrNull(Move(stream), size);
+    CGContextRef context;
+    ::PMSessionGetCGGraphicsContext(mPrintSession, &context);
+
+    if (context) {
+        // Initially, origin is at bottom-left corner of the paper.
+        // Here, we translate it to top-left corner of the paper.
+        CGContextTranslateCTM(context, 0, height);
+        CGContextScaleCTM(context, 1.0, -1.0);
+        return PrintTargetCG::CreateOrNull(context, size);
     }
-#endif
 
-    return PrintTargetCG::CreateOrNull(mPrintSession, mPageFormat,
-                                       mPrintSettings, size);
+    // Apparently we do need this branch - bug 368933.
+    return PrintTargetCG::CreateOrNull(size, SurfaceFormat::A8R8G8B8_UINT32);
+
+    NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL;
 }