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 "nsIPrintSession.h"
20 #include "nsIPrintSettings.h"
21 #include "private/pprio.h"
23 using mozilla::Unused
;
25 using namespace mozilla
;
26 using namespace mozilla::gfx
;
28 NS_IMPL_ISUPPORTS(nsDeviceContextSpecProxy
, nsIDeviceContextSpec
)
31 nsDeviceContextSpecProxy::Init(nsIWidget
* aWidget
,
32 nsIPrintSettings
* aPrintSettings
,
33 bool aIsPrintPreview
) {
35 mRealDeviceContextSpec
=
36 do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv
);
37 if (NS_WARN_IF(NS_FAILED(rv
))) {
41 mRealDeviceContextSpec
->Init(nullptr, aPrintSettings
, aIsPrintPreview
);
42 if (NS_WARN_IF(NS_FAILED(rv
))) {
43 mRealDeviceContextSpec
= nullptr;
47 mPrintSettings
= aPrintSettings
;
49 if (aIsPrintPreview
) {
53 // nsIPrintSettings only has a weak reference to nsIPrintSession, so we hold
54 // it to make sure it's available for the lifetime of the print.
55 rv
= mPrintSettings
->GetPrintSession(getter_AddRefs(mPrintSession
));
56 if (NS_FAILED(rv
) || !mPrintSession
) {
57 NS_WARNING("We can't print via the parent without an nsIPrintSession.");
58 return NS_ERROR_FAILURE
;
61 mRemotePrintJob
= mPrintSession
->GetRemotePrintJob();
62 if (!mRemotePrintJob
) {
63 NS_WARNING("We can't print via the parent without a RemotePrintJobChild.");
64 return NS_ERROR_FAILURE
;
70 already_AddRefed
<PrintTarget
> nsDeviceContextSpecProxy::MakePrintTarget() {
71 MOZ_ASSERT(mRealDeviceContextSpec
);
74 nsresult rv
= mPrintSettings
->GetEffectivePageSize(&width
, &height
);
75 if (NS_WARN_IF(NS_FAILED(rv
)) || width
<= 0 || height
<= 0) {
79 // convert twips to points
80 width
/= TWIPS_PER_POINT_FLOAT
;
81 height
/= TWIPS_PER_POINT_FLOAT
;
83 RefPtr
<gfxASurface
> surface
=
84 gfxPlatform::GetPlatform()->CreateOffscreenSurface(
85 mozilla::gfx::IntSize::Truncate(width
, height
),
86 mozilla::gfx::SurfaceFormat::A8R8G8B8_UINT32
);
91 // The type of PrintTarget that we return here shouldn't really matter since
92 // our implementation of GetDrawEventRecorder returns an object, which means
93 // the DrawTarget returned by the PrintTarget will be a
94 // DrawTargetWrapAndRecord. The recording will be serialized and sent over to
95 // the parent process where PrintTranslator::TranslateRecording will call
96 // MakePrintTarget (indirectly via PrintTranslator::CreateDrawTarget) on
97 // whatever type of nsIDeviceContextSpecProxy is created for the platform that
98 // we are running on. It is that DrawTarget that the recording will be
99 // replayed on to print.
100 // XXX(jwatt): The above isn't quite true. We do want to use a
101 // PrintTargetRecording here, but we can't until bug 1280324 is figured out
102 // and fixed otherwise we will cause bug 1280181 to happen again.
103 RefPtr
<PrintTarget
> target
= PrintTargetThebes::CreateOrNull(surface
);
105 return target
.forget();
109 nsDeviceContextSpecProxy::GetDrawEventRecorder(
110 mozilla::gfx::DrawEventRecorder
** aDrawEventRecorder
) {
111 MOZ_ASSERT(aDrawEventRecorder
);
112 RefPtr
<mozilla::gfx::DrawEventRecorder
> result
= mRecorder
;
113 result
.forget(aDrawEventRecorder
);
117 float nsDeviceContextSpecProxy::GetDPI() {
118 MOZ_ASSERT(mRealDeviceContextSpec
);
120 return mRealDeviceContextSpec
->GetDPI();
123 float nsDeviceContextSpecProxy::GetPrintingScale() {
124 MOZ_ASSERT(mRealDeviceContextSpec
);
126 return mRealDeviceContextSpec
->GetPrintingScale();
129 gfxPoint
nsDeviceContextSpecProxy::GetPrintingTranslate() {
130 MOZ_ASSERT(mRealDeviceContextSpec
);
132 return mRealDeviceContextSpec
->GetPrintingTranslate();
136 nsDeviceContextSpecProxy::BeginDocument(const nsAString
& aTitle
,
137 const nsAString
& aPrintToFileName
,
138 int32_t aStartPage
, int32_t aEndPage
) {
139 mRecorder
= new mozilla::layout::DrawEventRecorderPRFileDesc();
140 nsresult rv
= mRemotePrintJob
->InitializePrint(
141 nsString(aTitle
), nsString(aPrintToFileName
), aStartPage
, aEndPage
);
143 // The parent process will send a 'delete' message to tell this process to
144 // delete our RemotePrintJobChild. As soon as we return to the event loop
145 // and evaluate that message we will crash if we try to access
146 // mRemotePrintJob. We must not try to use it again.
147 mRemotePrintJob
= nullptr;
153 nsDeviceContextSpecProxy::EndDocument() {
154 if (mRemotePrintJob
) {
155 Unused
<< mRemotePrintJob
->SendFinalizePrint();
161 nsDeviceContextSpecProxy::AbortDocument() {
162 if (mRemotePrintJob
) {
163 Unused
<< mRemotePrintJob
->SendAbortPrint(NS_OK
);
169 nsDeviceContextSpecProxy::BeginPage() {
170 mRecorder
->OpenFD(mRemotePrintJob
->GetNextPageFD());
176 nsDeviceContextSpecProxy::EndPage() {
177 // Send the page recording to the parent.
179 mRemotePrintJob
->ProcessPage();