1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "RemotePrintJobParent.h"
#include <fstream>
#include "gfxContext.h"
#include "mozilla/Attributes.h"
#include "mozilla/Unused.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsComponentManagerUtils.h"
#include "nsDirectoryServiceUtils.h"
#include "nsDeviceContext.h"
#include "nsIDeviceContextSpec.h"
#include "nsIPrintSettings.h"
#include "nsIWebProgressListener.h"
namespace mozilla {
namespace layout {
RemotePrintJobParent::RemotePrintJobParent(nsIPrintSettings* aPrintSettings)
: mPrintSettings(aPrintSettings)
{
MOZ_COUNT_CTOR(RemotePrintJobParent);
}
bool
RemotePrintJobParent::RecvInitializePrint(const nsString& aDocumentTitle,
const nsString& aPrintToFile,
const int32_t& aStartPage,
const int32_t& aEndPage)
{
nsresult rv = InitializePrintDevice(aDocumentTitle, aPrintToFile, aStartPage,
aEndPage);
if (NS_FAILED(rv)) {
Unused << SendPrintInitializationResult(rv);
Unused << Send__delete__(this);
return true;
}
mPrintTranslator.reset(new PrintTranslator(mPrintDeviceContext));
Unused << SendPrintInitializationResult(NS_OK);
return true;
}
nsresult
RemotePrintJobParent::InitializePrintDevice(const nsString& aDocumentTitle,
const nsString& aPrintToFile,
const int32_t& aStartPage,
const int32_t& aEndPage)
{
nsresult rv;
nsCOMPtr<nsIDeviceContextSpec> deviceContextSpec =
do_CreateInstance("@mozilla.org/gfx/devicecontextspec;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = deviceContextSpec->Init(nullptr, mPrintSettings, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mPrintDeviceContext = new nsDeviceContext();
rv = mPrintDeviceContext->InitForPrinting(deviceContextSpec);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mPrintDeviceContext->BeginDocument(aDocumentTitle, aPrintToFile,
aStartPage, aEndPage);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
bool
RemotePrintJobParent::RecvProcessPage(const nsCString& aPageFileName)
{
nsresult rv = PrintPage(aPageFileName);
if (NS_FAILED(rv)) {
Unused << SendAbortPrint(rv);
} else {
Unused << SendPageProcessed();
}
return true;
}
nsresult
RemotePrintJobParent::PrintPage(const nsCString& aPageFileName)
{
MOZ_ASSERT(mPrintDeviceContext);
nsresult rv = mPrintDeviceContext->BeginPage();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsIFile> recordingFile;
rv = NS_GetSpecialDirectory(NS_APP_CONTENT_PROCESS_TEMP_DIR,
getter_AddRefs(recordingFile));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = recordingFile->AppendNative(aPageFileName);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsAutoCString recordingPath;
rv = recordingFile->GetNativePath(recordingPath);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
std::ifstream recording(recordingPath.get(), std::ifstream::binary);
if (!mPrintTranslator->TranslateRecording(recording)) {
return NS_ERROR_FAILURE;
}
rv = mPrintDeviceContext->EndPage();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
recording.close();
rv = recordingFile->Remove(/* recursive= */ false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
bool
RemotePrintJobParent::RecvFinalizePrint()
{
// EndDocument is sometimes called in the child even when BeginDocument has
// not been called. See bug 1223332.
if (mPrintDeviceContext) {
DebugOnly<nsresult> rv = mPrintDeviceContext->EndDocument();
// Too late to abort the child just log.
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EndDocument failed");
}
Unused << Send__delete__(this);
return true;
}
bool
RemotePrintJobParent::RecvAbortPrint(const nsresult& aRv)
{
if (mPrintDeviceContext) {
Unused << mPrintDeviceContext->AbortDocument();
}
Unused << Send__delete__(this);
return true;
}
bool
RemotePrintJobParent::RecvStateChange(const long& aStateFlags,
const nsresult& aStatus)
{
uint32_t numberOfListeners = mPrintProgressListeners.Length();
for (uint32_t i = 0; i < numberOfListeners; ++i) {
nsIWebProgressListener* listener = mPrintProgressListeners.SafeElementAt(i);
listener->OnStateChange(nullptr, nullptr, aStateFlags, aStatus);
}
return true;
}
bool
RemotePrintJobParent::RecvProgressChange(const long& aCurSelfProgress,
const long& aMaxSelfProgress,
const long& aCurTotalProgress,
const long& aMaxTotalProgress)
{
uint32_t numberOfListeners = mPrintProgressListeners.Length();
for (uint32_t i = 0; i < numberOfListeners; ++i) {
nsIWebProgressListener* listener = mPrintProgressListeners.SafeElementAt(i);
listener->OnProgressChange(nullptr, nullptr,
aCurSelfProgress, aMaxSelfProgress,
aCurTotalProgress, aMaxTotalProgress);
}
return true;
}
bool
RemotePrintJobParent::RecvStatusChange(const nsresult& aStatus)
{
uint32_t numberOfListeners = mPrintProgressListeners.Length();
for (uint32_t i = 0; i < numberOfListeners; ++i) {
nsIWebProgressListener* listener = mPrintProgressListeners.SafeElementAt(i);
listener->OnStatusChange(nullptr, nullptr, aStatus, nullptr);
}
return true;
}
void
RemotePrintJobParent::RegisterListener(nsIWebProgressListener* aListener)
{
MOZ_ASSERT(aListener);
mPrintProgressListeners.AppendElement(aListener);
}
already_AddRefed<nsIPrintSettings>
RemotePrintJobParent::GetPrintSettings()
{
nsCOMPtr<nsIPrintSettings> printSettings = mPrintSettings;
return printSettings.forget();
}
RemotePrintJobParent::~RemotePrintJobParent()
{
MOZ_COUNT_DTOR(RemotePrintJobParent);
}
void
RemotePrintJobParent::ActorDestroy(ActorDestroyReason aWhy)
{
}
} // namespace layout
} // namespace mozilla
|