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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/printing/pwg_raster_converter.h"
#include <algorithm>
#include <utility>
#include "base/bind_helpers.h"
#include "base/cancelable_callback.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "chrome/common/chrome_utility_messages.h"
#include "chrome/common/chrome_utility_printing_messages.h"
#include "chrome/grit/generated_resources.h"
#include "components/cloud_devices/common/cloud_device_description.h"
#include "components/cloud_devices/common/printer_description.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/utility_process_host.h"
#include "content/public/browser/utility_process_host_client.h"
#include "printing/pdf_render_settings.h"
#include "printing/pwg_raster_settings.h"
#include "printing/units.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace printing {
namespace {
using content::BrowserThread;
class FileHandlers {
public:
FileHandlers() {}
~FileHandlers() {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
}
void Init(base::RefCountedMemory* data);
bool IsValid();
base::FilePath GetPwgPath() const {
return temp_dir_.path().AppendASCII("output.pwg");
}
base::FilePath GetPdfPath() const {
return temp_dir_.path().AppendASCII("input.pdf");
}
IPC::PlatformFileForTransit GetPdfForProcess(base::ProcessHandle process) {
DCHECK(pdf_file_.IsValid());
IPC::PlatformFileForTransit transit =
IPC::TakeFileHandleForProcess(std::move(pdf_file_), process);
return transit;
}
IPC::PlatformFileForTransit GetPwgForProcess(base::ProcessHandle process) {
DCHECK(pwg_file_.IsValid());
IPC::PlatformFileForTransit transit =
IPC::TakeFileHandleForProcess(std::move(pwg_file_), process);
return transit;
}
private:
base::ScopedTempDir temp_dir_;
base::File pdf_file_;
base::File pwg_file_;
};
void FileHandlers::Init(base::RefCountedMemory* data) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
if (!temp_dir_.CreateUniqueTempDir()) {
return;
}
if (static_cast<int>(data->size()) !=
base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) {
return;
}
// Reopen in read only mode.
pdf_file_.Initialize(GetPdfPath(),
base::File::FLAG_OPEN | base::File::FLAG_READ);
pwg_file_.Initialize(GetPwgPath(),
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
}
bool FileHandlers::IsValid() {
return pdf_file_.IsValid() && pwg_file_.IsValid();
}
// Converts PDF into PWG raster.
// Class uses 3 threads: UI, IO and FILE.
// Internal workflow is following:
// 1. Create instance on the UI thread. (files_, settings_,)
// 2. Create file on the FILE thread.
// 3. Start utility process and start conversion on the IO thread.
// 4. Run result callback on the UI thread.
// 5. Instance is destroyed from any thread that has the last reference.
// 6. FileHandlers destroyed on the FILE thread.
// This step posts |FileHandlers| to be destroyed on the FILE thread.
// All these steps work sequentially, so no data should be accessed
// simultaneously by several threads.
class PwgUtilityProcessHostClient : public content::UtilityProcessHostClient {
public:
PwgUtilityProcessHostClient(
const PdfRenderSettings& settings,
const PwgRasterSettings& bitmap_settings);
void Convert(base::RefCountedMemory* data,
const PWGRasterConverter::ResultCallback& callback);
// UtilityProcessHostClient implementation.
void OnProcessCrashed(int exit_code) override;
bool OnMessageReceived(const IPC::Message& message) override;
private:
~PwgUtilityProcessHostClient() override;
// Message handlers.
void OnProcessStarted();
void OnSucceeded();
void OnFailed();
void RunCallback(bool success);
void StartProcessOnIOThread();
void RunCallbackOnUIThread(bool success);
void OnFilesReadyOnUIThread();
scoped_ptr<FileHandlers, BrowserThread::DeleteOnFileThread> files_;
PdfRenderSettings settings_;
PwgRasterSettings bitmap_settings_;
PWGRasterConverter::ResultCallback callback_;
base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient);
};
PwgUtilityProcessHostClient::PwgUtilityProcessHostClient(
const printing::PdfRenderSettings& settings,
const printing::PwgRasterSettings& bitmap_settings)
: settings_(settings), bitmap_settings_(bitmap_settings) {}
PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() {
}
void PwgUtilityProcessHostClient::Convert(
base::RefCountedMemory* data,
const PWGRasterConverter::ResultCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
callback_ = callback;
CHECK(!files_);
files_.reset(new FileHandlers());
BrowserThread::PostTaskAndReply(
BrowserThread::FILE, FROM_HERE,
base::Bind(&FileHandlers::Init, base::Unretained(files_.get()),
make_scoped_refptr(data)),
base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this));
}
void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
OnFailed();
}
bool PwgUtilityProcessHostClient::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted)
IPC_MESSAGE_HANDLER(
ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded)
IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed,
OnFailed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PwgUtilityProcessHostClient::OnProcessStarted() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!utility_process_host_) {
RunCallbackOnUIThread(false);
return;
}
base::ProcessHandle process = utility_process_host_->GetData().handle;
utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster(
files_->GetPdfForProcess(process),
settings_,
bitmap_settings_,
files_->GetPwgForProcess(process)));
utility_process_host_.reset();
}
void PwgUtilityProcessHostClient::OnSucceeded() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunCallback(true);
}
void PwgUtilityProcessHostClient::OnFailed() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RunCallback(false);
}
void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!files_->IsValid()) {
RunCallbackOnUIThread(false);
return;
}
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this));
}
void PwgUtilityProcessHostClient::StartProcessOnIOThread() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
utility_process_host_ =
content::UtilityProcessHost::Create(
this, base::MessageLoop::current()->task_runner())->AsWeakPtr();
utility_process_host_->SetName(l10n_util::GetStringUTF16(
IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME));
utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
}
void PwgUtilityProcessHostClient::RunCallback(bool success) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this,
success));
}
void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!callback_.is_null()) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(callback_, success,
files_->GetPwgPath()));
callback_.Reset();
}
}
class PWGRasterConverterImpl : public PWGRasterConverter {
public:
PWGRasterConverterImpl();
~PWGRasterConverterImpl() override;
void Start(base::RefCountedMemory* data,
const printing::PdfRenderSettings& conversion_settings,
const printing::PwgRasterSettings& bitmap_settings,
const ResultCallback& callback) override;
private:
scoped_refptr<PwgUtilityProcessHostClient> utility_client_;
base::CancelableCallback<ResultCallback::RunType> callback_;
DISALLOW_COPY_AND_ASSIGN(PWGRasterConverterImpl);
};
PWGRasterConverterImpl::PWGRasterConverterImpl() {
}
PWGRasterConverterImpl::~PWGRasterConverterImpl() {
}
void PWGRasterConverterImpl::Start(
base::RefCountedMemory* data,
const printing::PdfRenderSettings& conversion_settings,
const printing::PwgRasterSettings& bitmap_settings,
const ResultCallback& callback) {
// Rebind cancelable callback to avoid calling callback if
// PWGRasterConverterImpl is destroyed.
callback_.Reset(callback);
utility_client_ =
new PwgUtilityProcessHostClient(conversion_settings, bitmap_settings);
utility_client_->Convert(data, callback_.callback());
}
} // namespace
// static
scoped_ptr<PWGRasterConverter> PWGRasterConverter::CreateDefault() {
return scoped_ptr<PWGRasterConverter>(new PWGRasterConverterImpl());
}
// static
printing::PdfRenderSettings PWGRasterConverter::GetConversionSettings(
const cloud_devices::CloudDeviceDescription& printer_capabilities,
const gfx::Size& page_size) {
int dpi = printing::kDefaultPdfDpi;
cloud_devices::printer::DpiCapability dpis;
if (dpis.LoadFrom(printer_capabilities))
dpi = std::max(dpis.GetDefault().horizontal, dpis.GetDefault().vertical);
double scale = dpi;
scale /= printing::kPointsPerInch;
// Make vertical rectangle to optimize streaming to printer. Fix orientation
// by autorotate.
gfx::Rect area(std::min(page_size.width(), page_size.height()) * scale,
std::max(page_size.width(), page_size.height()) * scale);
return printing::PdfRenderSettings(area, dpi, true /* autorotate */);
}
// static
printing::PwgRasterSettings PWGRasterConverter::GetBitmapSettings(
const cloud_devices::CloudDeviceDescription& printer_capabilities,
const cloud_devices::CloudDeviceDescription& ticket) {
printing::PwgRasterSettings result;
cloud_devices::printer::PwgRasterConfigCapability raster_capability;
// If the raster capability fails to load, raster_capability will contain
// the default value.
raster_capability.LoadFrom(printer_capabilities);
cloud_devices::printer::DuplexTicketItem duplex_item;
cloud_devices::printer::DuplexType duplex_value =
cloud_devices::printer::NO_DUPLEX;
cloud_devices::printer::DocumentSheetBack document_sheet_back =
raster_capability.value().document_sheet_back;
if (duplex_item.LoadFrom(ticket)) {
duplex_value = duplex_item.value();
}
result.odd_page_transform = printing::TRANSFORM_NORMAL;
switch (duplex_value) {
case cloud_devices::printer::NO_DUPLEX:
result.odd_page_transform = printing::TRANSFORM_NORMAL;
break;
case cloud_devices::printer::LONG_EDGE:
if (document_sheet_back == cloud_devices::printer::ROTATED) {
result.odd_page_transform = printing::TRANSFORM_ROTATE_180;
} else if (document_sheet_back == cloud_devices::printer::FLIPPED) {
result.odd_page_transform = printing::TRANSFORM_FLIP_VERTICAL;
}
break;
case cloud_devices::printer::SHORT_EDGE:
if (document_sheet_back == cloud_devices::printer::MANUAL_TUMBLE) {
result.odd_page_transform = printing::TRANSFORM_ROTATE_180;
} else if (document_sheet_back == cloud_devices::printer::FLIPPED) {
result.odd_page_transform = printing::TRANSFORM_FLIP_HORIZONTAL;
}
}
result.rotate_all_pages = raster_capability.value().rotate_all_pages;
result.reverse_page_order = raster_capability.value().reverse_order_streaming;
return result;
}
} // namespace printing
|