1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/printing/print_job.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/threading/worker_pool.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/printing/print_job_worker.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_service.h"
17 #include "printing/printed_document.h"
18 #include "printing/printed_page.h"
21 #include "chrome/browser/printing/pdf_to_emf_converter.h"
22 #include "printing/pdf_render_settings.h"
25 using base::TimeDelta
;
29 // Helper function to ensure |owner| is valid until at least |callback| returns.
30 void HoldRefCallback(const scoped_refptr
<printing::PrintJobWorkerOwner
>& owner
,
31 const base::Closure
& callback
) {
43 is_job_pending_(false),
46 // This is normally a UI message loop, but in unit tests, the message loop is
47 // of the 'default' type.
48 DCHECK(base::MessageLoopForUI::IsCurrent() ||
49 base::MessageLoop::current()->type() ==
50 base::MessageLoop::TYPE_DEFAULT
);
53 PrintJob::~PrintJob() {
54 // The job should be finished (or at least canceled) when it is destroyed.
55 DCHECK(!is_job_pending_
);
56 DCHECK(!is_canceling_
);
57 DCHECK(!worker_
|| !worker_
->IsRunning());
58 DCHECK(RunsTasksOnCurrentThread());
61 void PrintJob::Initialize(PrintJobWorkerOwner
* job
,
62 PrintedPagesSource
* source
,
65 DCHECK(!worker_
.get());
66 DCHECK(!is_job_pending_
);
67 DCHECK(!is_canceling_
);
68 DCHECK(!document_
.get());
70 worker_
.reset(job
->DetachWorker(this));
71 settings_
= job
->settings();
73 PrintedDocument
* new_doc
=
74 new PrintedDocument(settings_
,
77 content::BrowserThread::GetBlockingPool());
78 new_doc
->set_page_count(page_count
);
79 UpdatePrintedDocument(new_doc
);
81 // Don't forget to register to our own messages.
82 registrar_
.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT
,
83 content::Source
<PrintJob
>(this));
86 void PrintJob::Observe(int type
,
87 const content::NotificationSource
& source
,
88 const content::NotificationDetails
& details
) {
89 DCHECK(RunsTasksOnCurrentThread());
91 case chrome::NOTIFICATION_PRINT_JOB_EVENT
: {
92 OnNotifyPrintJobEvent(*content::Details
<JobEventDetails
>(details
).ptr());
101 void PrintJob::GetSettingsDone(const PrintSettings
& new_settings
,
102 PrintingContext::Result result
) {
106 PrintJobWorker
* PrintJob::DetachWorker(PrintJobWorkerOwner
* new_owner
) {
111 const PrintSettings
& PrintJob::settings() const {
115 int PrintJob::cookie() const {
116 if (!document_
.get())
117 // Always use an invalid cookie in this case.
119 return document_
->cookie();
122 void PrintJob::StartPrinting() {
123 DCHECK(RunsTasksOnCurrentThread());
124 DCHECK(worker_
->IsRunning());
125 DCHECK(!is_job_pending_
);
126 if (!worker_
->IsRunning() || is_job_pending_
)
129 // Real work is done in PrintJobWorker::StartPrinting().
130 worker_
->PostTask(FROM_HERE
,
131 base::Bind(&HoldRefCallback
,
132 make_scoped_refptr(this),
133 base::Bind(&PrintJobWorker::StartPrinting
,
134 base::Unretained(worker_
.get()),
136 // Set the flag right now.
137 is_job_pending_
= true;
140 scoped_refptr
<JobEventDetails
> details(
141 new JobEventDetails(JobEventDetails::NEW_DOC
, document_
.get(), NULL
));
142 content::NotificationService::current()->Notify(
143 chrome::NOTIFICATION_PRINT_JOB_EVENT
,
144 content::Source
<PrintJob
>(this),
145 content::Details
<JobEventDetails
>(details
.get()));
148 void PrintJob::Stop() {
149 DCHECK(RunsTasksOnCurrentThread());
151 if (quit_factory_
.HasWeakPtrs()) {
152 // In case we're running a nested message loop to wait for a job to finish,
153 // and we finished before the timeout, quit the nested loop right away.
155 quit_factory_
.InvalidateWeakPtrs();
158 // Be sure to live long enough.
159 scoped_refptr
<PrintJob
> handle(this);
161 if (worker_
->IsRunning()) {
162 ControlledWorkerShutdown();
164 // Flush the cached document.
165 UpdatePrintedDocument(NULL
);
169 void PrintJob::Cancel() {
172 is_canceling_
= true;
174 // Be sure to live long enough.
175 scoped_refptr
<PrintJob
> handle(this);
177 DCHECK(RunsTasksOnCurrentThread());
178 if (worker_
&& worker_
->IsRunning()) {
179 // Call this right now so it renders the context invalid. Do not use
180 // InvokeLater since it would take too much time.
183 // Make sure a Cancel() is broadcast.
184 scoped_refptr
<JobEventDetails
> details(
185 new JobEventDetails(JobEventDetails::FAILED
, NULL
, NULL
));
186 content::NotificationService::current()->Notify(
187 chrome::NOTIFICATION_PRINT_JOB_EVENT
,
188 content::Source
<PrintJob
>(this),
189 content::Details
<JobEventDetails
>(details
.get()));
191 is_canceling_
= false;
194 bool PrintJob::FlushJob(base::TimeDelta timeout
) {
195 // Make sure the object outlive this message loop.
196 scoped_refptr
<PrintJob
> handle(this);
198 base::MessageLoop::current()->PostDelayedTask(FROM_HERE
,
199 base::Bind(&PrintJob::Quit
, quit_factory_
.GetWeakPtr()), timeout
);
201 base::MessageLoop::ScopedNestableTaskAllower
allow(
202 base::MessageLoop::current());
203 base::MessageLoop::current()->Run();
208 void PrintJob::DisconnectSource() {
211 document_
->DisconnectSource();
214 bool PrintJob::is_job_pending() const {
215 return is_job_pending_
;
218 PrintedDocument
* PrintJob::document() const {
219 return document_
.get();
224 class PrintJob::PdfToEmfState
{
226 PdfToEmfState(const gfx::Size
& page_size
, const gfx::Rect
& content_area
)
229 pages_in_progress_(0),
230 page_size_(page_size
),
231 content_area_(content_area
),
232 converter_(PdfToEmfConverter::CreateDefault()) {}
234 void Start(const scoped_refptr
<base::RefCountedMemory
>& data
,
235 const PdfRenderSettings
& conversion_settings
,
236 const PdfToEmfConverter::StartCallback
& start_callback
) {
237 converter_
->Start(data
, conversion_settings
, start_callback
);
241 const PdfToEmfConverter::GetPageCallback
& get_page_callback
) {
242 const int kMaxNumberOfTempFilesPerDocument
= 3;
243 while (pages_in_progress_
< kMaxNumberOfTempFilesPerDocument
&&
244 current_page_
< page_count_
) {
245 ++pages_in_progress_
;
246 converter_
->GetPage(current_page_
++, get_page_callback
);
250 void OnPageProcessed(
251 const PdfToEmfConverter::GetPageCallback
& get_page_callback
) {
252 --pages_in_progress_
;
253 GetMorePages(get_page_callback
);
254 // Release converter if we don't need this any more.
255 if (!pages_in_progress_
&& current_page_
>= page_count_
)
259 void set_page_count(int page_count
) { page_count_
= page_count
; }
260 gfx::Size
page_size() const { return page_size_
; }
261 gfx::Rect
content_area() const { return content_area_
; }
266 int pages_in_progress_
;
267 gfx::Size page_size_
;
268 gfx::Rect content_area_
;
269 scoped_ptr
<PdfToEmfConverter
> converter_
;
272 void PrintJob::StartPdfToEmfConversion(
273 const scoped_refptr
<base::RefCountedMemory
>& bytes
,
274 const gfx::Size
& page_size
,
275 const gfx::Rect
& content_area
) {
276 DCHECK(!ptd_to_emf_state_
.get());
277 ptd_to_emf_state_
.reset(new PdfToEmfState(page_size
, content_area
));
278 const int kPrinterDpi
= settings().dpi();
279 ptd_to_emf_state_
->Start(
281 printing::PdfRenderSettings(content_area
, kPrinterDpi
, true),
282 base::Bind(&PrintJob::OnPdfToEmfStarted
, this));
285 void PrintJob::OnPdfToEmfStarted(int page_count
) {
286 if (page_count
<= 0) {
287 ptd_to_emf_state_
.reset();
291 ptd_to_emf_state_
->set_page_count(page_count
);
292 ptd_to_emf_state_
->GetMorePages(
293 base::Bind(&PrintJob::OnPdfToEmfPageConverted
, this));
296 void PrintJob::OnPdfToEmfPageConverted(int page_number
,
298 scoped_ptr
<MetafilePlayer
> emf
) {
299 DCHECK(ptd_to_emf_state_
);
300 if (!document_
.get() || !emf
) {
301 ptd_to_emf_state_
.reset();
306 // Update the rendered document. It will send notifications to the listener.
307 document_
->SetPage(page_number
,
310 ptd_to_emf_state_
->page_size(),
311 ptd_to_emf_state_
->content_area());
313 ptd_to_emf_state_
->GetMorePages(
314 base::Bind(&PrintJob::OnPdfToEmfPageConverted
, this));
319 void PrintJob::UpdatePrintedDocument(PrintedDocument
* new_document
) {
320 if (document_
.get() == new_document
)
323 document_
= new_document
;
325 if (document_
.get()) {
326 settings_
= document_
->settings();
330 DCHECK(!is_job_pending_
);
331 // Sync the document with the worker.
332 worker_
->PostTask(FROM_HERE
,
333 base::Bind(&HoldRefCallback
,
334 make_scoped_refptr(this),
335 base::Bind(&PrintJobWorker::OnDocumentChanged
,
336 base::Unretained(worker_
.get()),
341 void PrintJob::OnNotifyPrintJobEvent(const JobEventDetails
& event_details
) {
342 switch (event_details
.type()) {
343 case JobEventDetails::FAILED
: {
345 // No need to cancel since the worker already canceled itself.
349 case JobEventDetails::USER_INIT_DONE
:
350 case JobEventDetails::DEFAULT_INIT_DONE
:
351 case JobEventDetails::USER_INIT_CANCELED
: {
352 DCHECK_EQ(event_details
.document(), document_
.get());
355 case JobEventDetails::NEW_DOC
:
356 case JobEventDetails::NEW_PAGE
:
357 case JobEventDetails::JOB_DONE
:
358 case JobEventDetails::ALL_PAGES_REQUESTED
: {
362 case JobEventDetails::DOC_DONE
: {
363 // This will call Stop() and broadcast a JOB_DONE message.
364 base::MessageLoop::current()->PostTask(
365 FROM_HERE
, base::Bind(&PrintJob::OnDocumentDone
, this));
368 case JobEventDetails::PAGE_DONE
:
370 ptd_to_emf_state_
->OnPageProcessed(
371 base::Bind(&PrintJob::OnPdfToEmfPageConverted
, this));
381 void PrintJob::OnDocumentDone() {
382 // Be sure to live long enough. The instance could be destroyed by the
383 // JOB_DONE broadcast.
384 scoped_refptr
<PrintJob
> handle(this);
386 // Stop the worker thread.
389 scoped_refptr
<JobEventDetails
> details(
390 new JobEventDetails(JobEventDetails::JOB_DONE
, document_
.get(), NULL
));
391 content::NotificationService::current()->Notify(
392 chrome::NOTIFICATION_PRINT_JOB_EVENT
,
393 content::Source
<PrintJob
>(this),
394 content::Details
<JobEventDetails
>(details
.get()));
397 void PrintJob::ControlledWorkerShutdown() {
398 DCHECK(RunsTasksOnCurrentThread());
400 // The deadlock this code works around is specific to window messaging on
401 // Windows, so we aren't likely to need it on any other platforms.
403 // We could easily get into a deadlock case if worker_->Stop() is used; the
404 // printer driver created a window as a child of the browser window. By
405 // canceling the job, the printer driver initiated dialog box is destroyed,
406 // which sends a blocking message to its parent window. If the browser window
407 // thread is not processing messages, a deadlock occurs.
409 // This function ensures that the dialog box will be destroyed in a timely
410 // manner by the mere fact that the thread will terminate. So the potential
411 // deadlock is eliminated.
414 // Delay shutdown until the worker terminates. We want this code path
415 // to wait on the thread to quit before continuing.
416 if (worker_
->IsRunning()) {
417 base::MessageLoop::current()->PostDelayedTask(
419 base::Bind(&PrintJob::ControlledWorkerShutdown
, this),
420 base::TimeDelta::FromMilliseconds(100));
426 // Now make sure the thread object is cleaned up. Do this on a worker
427 // thread because it may block.
428 base::WorkerPool::PostTaskAndReply(
430 base::Bind(&PrintJobWorker::Stop
, base::Unretained(worker_
.get())),
431 base::Bind(&PrintJob::HoldUntilStopIsCalled
, this),
434 is_job_pending_
= false;
435 registrar_
.RemoveAll();
436 UpdatePrintedDocument(NULL
);
439 void PrintJob::HoldUntilStopIsCalled() {
442 void PrintJob::Quit() {
443 base::MessageLoop::current()->Quit();
446 // Takes settings_ ownership and will be deleted in the receiving thread.
447 JobEventDetails::JobEventDetails(Type type
,
448 PrintedDocument
* document
,
450 : document_(document
),
455 JobEventDetails::~JobEventDetails() {
458 PrintedDocument
* JobEventDetails::document() const { return document_
.get(); }
460 PrintedPage
* JobEventDetails::page() const { return page_
.get(); }
462 } // namespace printing