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/. */
7 #include "nsDeviceContextSpecProxy.h"
9 #include "gfxASurface.h"
10 #include "gfxPlatform.h"
11 #include "mozilla/gfx/DrawEventRecorder.h"
12 #include "mozilla/gfx/PrintTargetThebes.h"
13 #include "mozilla/layout/RemotePrintJobChild.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/Unused.h"
16 #include "nsComponentManagerUtils.h"
17 #include "nsAppDirectoryServiceDefs.h"
18 #include "nsDirectoryServiceUtils.h"
19 #include "nsIPrintSettings.h"
20 #include "private/pprio.h"
22 using mozilla::Unused
;
24 using namespace mozilla
;
25 using namespace mozilla::gfx
;
27 NS_IMPL_ISUPPORTS(nsDeviceContextSpecProxy
, nsIDeviceContextSpec
)
29 nsDeviceContextSpecProxy::nsDeviceContextSpecProxy(
30 RemotePrintJobChild
* aRemotePrintJob
)
31 : mRemotePrintJob(aRemotePrintJob
) {}
32 nsDeviceContextSpecProxy::~nsDeviceContextSpecProxy() = default;
35 nsDeviceContextSpecProxy::Init(nsIPrintSettings
* aPrintSettings
,
36 bool aIsPrintPreview
) {
37 mPrintSettings
= aPrintSettings
;
39 if (aIsPrintPreview
) {
43 if (!mRemotePrintJob
) {
44 NS_WARNING("We can't print via the parent without a RemotePrintJobChild.");
45 return NS_ERROR_FAILURE
;
51 already_AddRefed
<PrintTarget
> nsDeviceContextSpecProxy::MakePrintTarget() {
53 mPrintSettings
->GetEffectiveSheetSize(&width
, &height
);
54 if (width
<= 0 || height
<= 0) {
58 // convert twips to points
59 width
/= TWIPS_PER_POINT_FLOAT
;
60 height
/= TWIPS_PER_POINT_FLOAT
;
62 RefPtr
<gfxASurface
> surface
=
63 gfxPlatform::GetPlatform()->CreateOffscreenSurface(
64 mozilla::gfx::IntSize::Ceil(width
, height
),
65 mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32
);
70 // The type of PrintTarget that we return here shouldn't really matter since
71 // our implementation of GetDrawEventRecorder returns an object, which means
72 // the DrawTarget returned by the PrintTarget will be a
73 // DrawTargetWrapAndRecord. The recording will be serialized and sent over to
74 // the parent process where PrintTranslator::TranslateRecording will call
75 // MakePrintTarget (indirectly via PrintTranslator::CreateDrawTarget) on
76 // whatever type of nsIDeviceContextSpecProxy is created for the platform that
77 // we are running on. It is that DrawTarget that the recording will be
78 // replayed on to print.
79 // XXX(jwatt): The above isn't quite true. We do want to use a
80 // PrintTargetRecording here, but we can't until bug 1280324 is figured out
81 // and fixed otherwise we will cause bug 1280181 to happen again.
82 RefPtr
<PrintTarget
> target
= PrintTargetThebes::CreateOrNull(surface
);
84 return target
.forget();
88 nsDeviceContextSpecProxy::GetDrawEventRecorder(
89 mozilla::gfx::DrawEventRecorder
** aDrawEventRecorder
) {
90 MOZ_ASSERT(aDrawEventRecorder
);
91 RefPtr
<mozilla::gfx::DrawEventRecorder
> result
= mRecorder
;
92 result
.forget(aDrawEventRecorder
);
97 nsDeviceContextSpecProxy::BeginDocument(const nsAString
& aTitle
,
98 const nsAString
& aPrintToFileName
,
99 int32_t aStartPage
, int32_t aEndPage
) {
100 if (!mRemotePrintJob
|| mRemotePrintJob
->IsDestroyed()) {
101 mRemotePrintJob
= nullptr;
102 return NS_ERROR_NOT_AVAILABLE
;
105 mRecorder
= new mozilla::layout::DrawEventRecorderPRFileDesc();
107 mRemotePrintJob
->InitializePrint(nsString(aTitle
), aStartPage
, aEndPage
);
109 // The parent process will send a 'delete' message to tell this process to
110 // delete our RemotePrintJobChild. As soon as we return to the event loop
111 // and evaluate that message we will crash if we try to access
112 // mRemotePrintJob. We must not try to use it again.
113 mRemotePrintJob
= nullptr;
119 nsDeviceContextSpecProxy::EndDocument() {
120 if (!mRemotePrintJob
|| mRemotePrintJob
->IsDestroyed()) {
121 mRemotePrintJob
= nullptr;
122 return NS_ERROR_NOT_AVAILABLE
;
125 Unused
<< mRemotePrintJob
->SendFinalizePrint();
131 nsDeviceContextSpecProxy::AbortDocument() {
132 if (!mRemotePrintJob
|| mRemotePrintJob
->IsDestroyed()) {
133 mRemotePrintJob
= nullptr;
134 return NS_ERROR_NOT_AVAILABLE
;
137 Unused
<< mRemotePrintJob
->SendAbortPrint(NS_OK
);
143 nsDeviceContextSpecProxy::BeginPage() {
144 if (!mRemotePrintJob
|| mRemotePrintJob
->IsDestroyed()) {
145 mRemotePrintJob
= nullptr;
146 return NS_ERROR_NOT_AVAILABLE
;
149 mRecorder
->OpenFD(mRemotePrintJob
->GetNextPageFD());
155 nsDeviceContextSpecProxy::EndPage() {
156 if (!mRemotePrintJob
|| mRemotePrintJob
->IsDestroyed()) {
157 mRemotePrintJob
= nullptr;
158 return NS_ERROR_NOT_AVAILABLE
;
161 // Send the page recording to the parent.
163 mRemotePrintJob
->ProcessPage(std::move(mRecorder
->TakeDependentSurfaces()));