Bug 1309272, part 7 - Restructure the PMSessionBeginCGDocumentNoDialog related code to live in PrintTargetCG. r=lsalzman
☠☠ backed out by bc39d8cde9ab ☠ ☠
authorJonathan Watt <jwatt@jwatt.org>
Tue, 22 Nov 2016 16:39:08 +0000
changeset 324469 d2046ba274866207e92400089d249cd47f552a48
parent 324468 31fdab31ab5128a693c0f58fc1b3a2ec86ccb698
child 324470 5ecb0db80f1531e80fd5b70eceed0e4514da09fd
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerslsalzman
bugs1309272
milestone53.0a1
Bug 1309272, part 7 - Restructure the PMSessionBeginCGDocumentNoDialog related code to live in PrintTargetCG. r=lsalzman
gfx/thebes/PrintTargetCG.h
gfx/thebes/PrintTargetCG.mm
widget/cocoa/nsDeviceContextSpecX.h
widget/cocoa/nsDeviceContextSpecX.mm
--- a/gfx/thebes/PrintTargetCG.h
+++ b/gfx/thebes/PrintTargetCG.h
@@ -14,28 +14,41 @@ namespace gfx {
 
 /**
  * CoreGraphics printing target.
  */
 class PrintTargetCG final : public PrintTarget
 {
 public:
   static already_AddRefed<PrintTargetCG>
-  CreateOrNull(PMPrintSession aPrintSession, const IntSize& aSize);
+  CreateOrNull(PMPrintSession aPrintSession,
+               PMPageFormat aPageFormat,
+               PMPrintSettings aPrintSettings,
+               const IntSize& aSize);
 
+  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;
 
   virtual already_AddRefed<DrawTarget>
   GetReferenceDrawTarget(DrawEventRecorder* aRecorder) final;
 
 private:
   PrintTargetCG(PMPrintSession aPrintSession,
+                PMPageFormat aPageFormat,
+                PMPrintSettings aPrintSettings,
                 const IntSize& aSize);
   ~PrintTargetCG();
 
   PMPrintSession mPrintSession;
+  PMPageFormat mPageFormat;
+  PMPrintSettings mPrintSettings;
 };
 
 } // namespace gfx
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_PRINTTARGETCG_H */
--- a/gfx/thebes/PrintTargetCG.mm
+++ b/gfx/thebes/PrintTargetCG.mm
@@ -9,19 +9,23 @@
 #include "cairo-quartz.h"
 #include "mozilla/gfx/HelpersCairo.h"
 #include "nsObjCExceptions.h"
 
 namespace mozilla {
 namespace gfx {
 
 PrintTargetCG::PrintTargetCG(PMPrintSession aPrintSession,
+                             PMPageFormat aPageFormat,
+                             PMPrintSettings aPrintSettings,
                              const IntSize& aSize)
   : PrintTarget(/* aCairoSurface */ nullptr, aSize)
   , mPrintSession(aPrintSession)
+  , mPageFormat(aPageFormat)
+  , mPrintSettings(aPrintSettings)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   ::PMRetain(mPrintSession);
 
   // TODO: Add memory reporting like gfxQuartzSurface.
   //RecordMemoryUsed(mSize.height * 4 + sizeof(gfxQuartzSurface));
 
@@ -34,23 +38,27 @@ PrintTargetCG::~PrintTargetCG()
 
   if (mPrintSession)
     ::PMRelease(mPrintSession);
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 /* static */ already_AddRefed<PrintTargetCG>
-PrintTargetCG::CreateOrNull(PMPrintSession aPrintSession, const IntSize& aSize)
+PrintTargetCG::CreateOrNull(PMPrintSession aPrintSession,
+                            PMPageFormat aPageFormat,
+                            PMPrintSettings aPrintSettings,
+                            const IntSize& aSize)
 {
   if (!Factory::CheckSurfaceSize(aSize)) {
     return nullptr;
   }
 
-  RefPtr<PrintTargetCG> target = new PrintTargetCG(aPrintSession, aSize);
+  RefPtr<PrintTargetCG> target = new PrintTargetCG(aPrintSession, aPageFormat,
+                                                   aPrintSettings, aSize);
 
   return target.forget();
 }
 
 static size_t
 PutBytesNull(void* info, const void* buffer, size_t count)
 {
   return count;
@@ -95,22 +103,69 @@ 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 we are not called between the PMSessionBeginPage/
+  // 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);
@@ -133,15 +188,25 @@ PrintTargetCG::BeginPage()
   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/widget/cocoa/nsDeviceContextSpecX.h
+++ b/widget/cocoa/nsDeviceContextSpecX.h
@@ -19,18 +19,22 @@ 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;
-    NS_IMETHOD EndPage() override;
+    NS_IMETHOD BeginPage() override {
+      return NS_OK;
+    };
+    NS_IMETHOD EndPage() override {
+      return NS_OK;
+    };
 
     void GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight);
 
 protected:
     virtual ~nsDeviceContextSpecX();
 
 protected:
     PMPrintSession    mPrintSession;              // printing context.
--- a/widget/cocoa/nsDeviceContextSpecX.mm
+++ b/widget/cocoa/nsDeviceContextSpecX.mm
@@ -71,62 +71,24 @@ 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;
-
-    ::PMSessionEndDocumentNoDialog(mPrintSession);
-    return NS_OK;
-
-    NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
-}
-
-NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
-{
-    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-    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;
-}
-
-NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
-{
-    NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
-
-    OSStatus status = ::PMSessionEndPageNoDialog(mPrintSession);
-    if (status != noErr) return NS_ERROR_ABORT;
-    return NS_OK;
-
-    NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
+  return NS_OK;
 }
 
 void nsDeviceContextSpecX::GetPaperRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
 {
     NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
     PMRect paperRect;
     ::PMGetAdjustedPaperRect(mPageFormat, &paperRect);
@@ -140,10 +102,11 @@ void nsDeviceContextSpecX::GetPaperRect(
 already_AddRefed<PrintTarget> nsDeviceContextSpecX::MakePrintTarget()
 {
     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);
 
-    return PrintTargetCG::CreateOrNull(mPrintSession, size);
+    return PrintTargetCG::CreateOrNull(mPrintSession, mPageFormat,
+                                       mPrintSettings, size);
 }