2014-08-21 07:38:20 +00:00
|
|
|
// Copyright (c) 2012 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/print_job.h"
|
|
|
|
|
2017-01-31 08:38:05 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2014-08-21 07:38:20 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/bind_helpers.h"
|
2017-01-31 08:38:05 +00:00
|
|
|
#include "base/location.h"
|
|
|
|
#include "base/memory/ptr_util.h"
|
2014-08-21 07:38:20 +00:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2016-11-30 07:30:03 +00:00
|
|
|
#include "base/run_loop.h"
|
2017-12-19 00:06:42 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
2017-01-31 08:38:05 +00:00
|
|
|
#include "base/single_thread_task_runner.h"
|
2017-04-13 10:11:16 +00:00
|
|
|
#include "base/threading/sequenced_worker_pool.h"
|
2014-08-21 07:38:20 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2016-11-30 07:30:03 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2017-01-31 08:38:05 +00:00
|
|
|
#include "build/build_config.h"
|
2014-08-21 07:38:20 +00:00
|
|
|
#include "chrome/browser/chrome_notification_types.h"
|
|
|
|
#include "chrome/browser/printing/print_job_worker.h"
|
|
|
|
#include "content/public/browser/notification_service.h"
|
|
|
|
#include "printing/printed_document.h"
|
|
|
|
#include "printing/printed_page.h"
|
|
|
|
|
2015-04-30 03:31:17 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "chrome/browser/printing/pdf_to_emf_converter.h"
|
|
|
|
#include "printing/pdf_render_settings.h"
|
|
|
|
#endif
|
|
|
|
|
2014-08-21 07:38:20 +00:00
|
|
|
using base::TimeDelta;
|
|
|
|
|
2017-01-31 08:38:05 +00:00
|
|
|
namespace printing {
|
|
|
|
|
2014-08-21 07:38:20 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper function to ensure |owner| is valid until at least |callback| returns.
|
2017-01-31 08:38:05 +00:00
|
|
|
void HoldRefCallback(const scoped_refptr<PrintJobWorkerOwner>& owner,
|
2014-08-21 07:38:20 +00:00
|
|
|
const base::Closure& callback) {
|
|
|
|
callback.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PrintJob::PrintJob()
|
2017-12-18 02:36:07 +00:00
|
|
|
: is_job_pending_(false),
|
2014-08-21 07:38:20 +00:00
|
|
|
is_canceling_(false),
|
|
|
|
quit_factory_(this) {
|
|
|
|
// This is normally a UI message loop, but in unit tests, the message loop is
|
|
|
|
// of the 'default' type.
|
|
|
|
DCHECK(base::MessageLoopForUI::IsCurrent() ||
|
2014-12-07 14:55:40 +00:00
|
|
|
base::MessageLoop::current()->type() ==
|
|
|
|
base::MessageLoop::TYPE_DEFAULT);
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PrintJob::~PrintJob() {
|
|
|
|
// The job should be finished (or at least canceled) when it is destroyed.
|
|
|
|
DCHECK(!is_job_pending_);
|
|
|
|
DCHECK(!is_canceling_);
|
2014-12-07 14:55:40 +00:00
|
|
|
DCHECK(!worker_ || !worker_->IsRunning());
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::Initialize(PrintJobWorkerOwner* job,
|
2017-12-18 02:36:07 +00:00
|
|
|
const base::string16& name,
|
2014-08-21 07:38:20 +00:00
|
|
|
int page_count) {
|
2017-01-31 08:38:05 +00:00
|
|
|
DCHECK(!worker_);
|
2014-08-21 07:38:20 +00:00
|
|
|
DCHECK(!is_job_pending_);
|
|
|
|
DCHECK(!is_canceling_);
|
|
|
|
DCHECK(!document_.get());
|
2017-01-31 08:38:05 +00:00
|
|
|
worker_ = job->DetachWorker(this);
|
2014-08-21 07:38:20 +00:00
|
|
|
settings_ = job->settings();
|
|
|
|
|
|
|
|
PrintedDocument* new_doc =
|
2017-12-18 02:36:07 +00:00
|
|
|
new PrintedDocument(settings_, name, job->cookie());
|
2014-08-21 07:38:20 +00:00
|
|
|
new_doc->set_page_count(page_count);
|
|
|
|
UpdatePrintedDocument(new_doc);
|
|
|
|
|
|
|
|
// Don't forget to register to our own messages.
|
|
|
|
registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::Observe(int type,
|
|
|
|
const content::NotificationSource& source,
|
|
|
|
const content::NotificationDetails& details) {
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2017-01-31 08:38:05 +00:00
|
|
|
DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_EVENT, type);
|
|
|
|
|
|
|
|
OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::GetSettingsDone(const PrintSettings& new_settings,
|
|
|
|
PrintingContext::Result result) {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2017-01-31 08:38:05 +00:00
|
|
|
std::unique_ptr<PrintJobWorker> PrintJob::DetachWorker(
|
|
|
|
PrintJobWorkerOwner* new_owner) {
|
2014-08-21 07:38:20 +00:00
|
|
|
NOTREACHED();
|
2017-01-31 08:38:05 +00:00
|
|
|
return nullptr;
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PrintSettings& PrintJob::settings() const {
|
|
|
|
return settings_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PrintJob::cookie() const {
|
2017-01-31 08:38:05 +00:00
|
|
|
// Always use an invalid cookie in this case.
|
2014-08-21 07:38:20 +00:00
|
|
|
if (!document_.get())
|
|
|
|
return 0;
|
|
|
|
return document_->cookie();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::StartPrinting() {
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2017-01-31 08:38:05 +00:00
|
|
|
if (!worker_->IsRunning() || is_job_pending_) {
|
|
|
|
NOTREACHED();
|
2014-08-21 07:38:20 +00:00
|
|
|
return;
|
2017-01-31 08:38:05 +00:00
|
|
|
}
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
// Real work is done in PrintJobWorker::StartPrinting().
|
2014-12-07 14:55:40 +00:00
|
|
|
worker_->PostTask(FROM_HERE,
|
2017-12-18 00:23:02 +00:00
|
|
|
base::Bind(&HoldRefCallback, WrapRefCounted(this),
|
2014-12-07 14:55:40 +00:00
|
|
|
base::Bind(&PrintJobWorker::StartPrinting,
|
|
|
|
base::Unretained(worker_.get()),
|
2016-05-23 03:28:59 +00:00
|
|
|
base::RetainedRef(document_))));
|
2014-08-21 07:38:20 +00:00
|
|
|
// Set the flag right now.
|
|
|
|
is_job_pending_ = true;
|
|
|
|
|
|
|
|
// Tell everyone!
|
|
|
|
scoped_refptr<JobEventDetails> details(
|
2017-01-31 08:38:05 +00:00
|
|
|
new JobEventDetails(JobEventDetails::NEW_DOC, document_.get(), nullptr));
|
2014-08-21 07:38:20 +00:00
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(this),
|
|
|
|
content::Details<JobEventDetails>(details.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::Stop() {
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
if (quit_factory_.HasWeakPtrs()) {
|
|
|
|
// In case we're running a nested message loop to wait for a job to finish,
|
|
|
|
// and we finished before the timeout, quit the nested loop right away.
|
|
|
|
Quit();
|
|
|
|
quit_factory_.InvalidateWeakPtrs();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure to live long enough.
|
|
|
|
scoped_refptr<PrintJob> handle(this);
|
|
|
|
|
2014-12-07 14:55:40 +00:00
|
|
|
if (worker_->IsRunning()) {
|
2014-08-21 07:38:20 +00:00
|
|
|
ControlledWorkerShutdown();
|
|
|
|
} else {
|
|
|
|
// Flush the cached document.
|
2017-01-31 08:38:05 +00:00
|
|
|
is_job_pending_ = false;
|
|
|
|
UpdatePrintedDocument(nullptr);
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::Cancel() {
|
|
|
|
if (is_canceling_)
|
|
|
|
return;
|
|
|
|
is_canceling_ = true;
|
|
|
|
|
|
|
|
// Be sure to live long enough.
|
|
|
|
scoped_refptr<PrintJob> handle(this);
|
|
|
|
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2014-12-07 14:55:40 +00:00
|
|
|
if (worker_ && worker_->IsRunning()) {
|
2014-08-21 07:38:20 +00:00
|
|
|
// Call this right now so it renders the context invalid. Do not use
|
|
|
|
// InvokeLater since it would take too much time.
|
|
|
|
worker_->Cancel();
|
|
|
|
}
|
|
|
|
// Make sure a Cancel() is broadcast.
|
|
|
|
scoped_refptr<JobEventDetails> details(
|
2017-01-31 08:38:05 +00:00
|
|
|
new JobEventDetails(JobEventDetails::FAILED, nullptr, nullptr));
|
2014-08-21 07:38:20 +00:00
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(this),
|
|
|
|
content::Details<JobEventDetails>(details.get()));
|
|
|
|
Stop();
|
|
|
|
is_canceling_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintJob::FlushJob(base::TimeDelta timeout) {
|
|
|
|
// Make sure the object outlive this message loop.
|
|
|
|
scoped_refptr<PrintJob> handle(this);
|
|
|
|
|
2016-11-30 07:30:03 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
|
|
|
FROM_HERE, base::Bind(&PrintJob::Quit, quit_factory_.GetWeakPtr()),
|
|
|
|
timeout);
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
base::MessageLoop::ScopedNestableTaskAllower allow(
|
|
|
|
base::MessageLoop::current());
|
2016-11-30 07:30:03 +00:00
|
|
|
base::RunLoop().Run();
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintJob::is_job_pending() const {
|
|
|
|
return is_job_pending_;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintedDocument* PrintJob::document() const {
|
|
|
|
return document_.get();
|
|
|
|
}
|
|
|
|
|
2015-04-30 03:31:17 +00:00
|
|
|
#if defined(OS_WIN)
|
2017-04-17 10:42:12 +00:00
|
|
|
class PrintJob::PdfConversionState {
|
2015-04-30 03:31:17 +00:00
|
|
|
public:
|
2017-04-17 10:42:12 +00:00
|
|
|
PdfConversionState(gfx::Size page_size, gfx::Rect content_area)
|
2015-04-30 03:31:17 +00:00
|
|
|
: page_count_(0),
|
|
|
|
current_page_(0),
|
|
|
|
pages_in_progress_(0),
|
|
|
|
page_size_(page_size),
|
2017-04-17 10:42:12 +00:00
|
|
|
content_area_(content_area) {}
|
2015-04-30 03:31:17 +00:00
|
|
|
|
|
|
|
void Start(const scoped_refptr<base::RefCountedMemory>& data,
|
|
|
|
const PdfRenderSettings& conversion_settings,
|
2017-04-17 10:42:12 +00:00
|
|
|
const PdfConverter::StartCallback& start_callback) {
|
|
|
|
converter_ = PdfConverter::StartPdfConverter(
|
|
|
|
data, conversion_settings, start_callback);
|
2015-04-30 03:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
void GetMorePages(const PdfConverter::GetPageCallback& get_page_callback) {
|
2015-04-30 03:31:17 +00:00
|
|
|
const int kMaxNumberOfTempFilesPerDocument = 3;
|
|
|
|
while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument &&
|
|
|
|
current_page_ < page_count_) {
|
|
|
|
++pages_in_progress_;
|
|
|
|
converter_->GetPage(current_page_++, get_page_callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
void OnPageProcessed(const PdfConverter::GetPageCallback& get_page_callback) {
|
2015-04-30 03:31:17 +00:00
|
|
|
--pages_in_progress_;
|
|
|
|
GetMorePages(get_page_callback);
|
|
|
|
// Release converter if we don't need this any more.
|
|
|
|
if (!pages_in_progress_ && current_page_ >= page_count_)
|
|
|
|
converter_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_page_count(int page_count) { page_count_ = page_count; }
|
|
|
|
gfx::Size page_size() const { return page_size_; }
|
|
|
|
gfx::Rect content_area() const { return content_area_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int page_count_;
|
|
|
|
int current_page_;
|
|
|
|
int pages_in_progress_;
|
|
|
|
gfx::Size page_size_;
|
|
|
|
gfx::Rect content_area_;
|
2017-04-17 10:42:12 +00:00
|
|
|
std::unique_ptr<PdfConverter> converter_;
|
2015-04-30 03:31:17 +00:00
|
|
|
};
|
|
|
|
|
2017-01-31 08:38:05 +00:00
|
|
|
void PrintJob::AppendPrintedPage(int page_number) {
|
|
|
|
pdf_page_mapping_.push_back(page_number);
|
|
|
|
}
|
|
|
|
|
2015-04-30 03:31:17 +00:00
|
|
|
void PrintJob::StartPdfToEmfConversion(
|
|
|
|
const scoped_refptr<base::RefCountedMemory>& bytes,
|
|
|
|
const gfx::Size& page_size,
|
2017-01-31 08:38:05 +00:00
|
|
|
const gfx::Rect& content_area,
|
|
|
|
bool print_text_with_gdi) {
|
2017-04-17 10:42:12 +00:00
|
|
|
DCHECK(!pdf_conversion_state_);
|
|
|
|
pdf_conversion_state_ =
|
2018-04-12 12:48:32 +00:00
|
|
|
std::make_unique<PdfConversionState>(page_size, content_area);
|
2015-04-30 03:31:17 +00:00
|
|
|
const int kPrinterDpi = settings().dpi();
|
2017-04-17 10:42:12 +00:00
|
|
|
PdfRenderSettings settings(
|
|
|
|
content_area, gfx::Point(0, 0), kPrinterDpi, /*autorotate=*/true,
|
|
|
|
print_text_with_gdi ? PdfRenderSettings::Mode::GDI_TEXT
|
|
|
|
: PdfRenderSettings::Mode::NORMAL);
|
|
|
|
pdf_conversion_state_->Start(
|
|
|
|
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
|
2015-04-30 03:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
void PrintJob::OnPdfConversionStarted(int page_count) {
|
2015-04-30 03:31:17 +00:00
|
|
|
if (page_count <= 0) {
|
2017-04-17 10:42:12 +00:00
|
|
|
pdf_conversion_state_.reset();
|
2015-04-30 03:31:17 +00:00
|
|
|
Cancel();
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 10:42:12 +00:00
|
|
|
pdf_conversion_state_->set_page_count(page_count);
|
|
|
|
pdf_conversion_state_->GetMorePages(
|
|
|
|
base::Bind(&PrintJob::OnPdfPageConverted, this));
|
2015-04-30 03:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
void PrintJob::OnPdfPageConverted(int page_number,
|
|
|
|
float scale_factor,
|
|
|
|
std::unique_ptr<MetafilePlayer> metafile) {
|
|
|
|
DCHECK(pdf_conversion_state_);
|
|
|
|
if (!document_.get() || !metafile || page_number < 0 ||
|
2017-01-31 08:38:05 +00:00
|
|
|
static_cast<size_t>(page_number) >= pdf_page_mapping_.size()) {
|
2017-04-17 10:42:12 +00:00
|
|
|
pdf_conversion_state_.reset();
|
2015-04-30 03:31:17 +00:00
|
|
|
Cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the rendered document. It will send notifications to the listener.
|
2017-04-17 10:42:12 +00:00
|
|
|
document_->SetPage(pdf_page_mapping_[page_number], std::move(metafile),
|
|
|
|
scale_factor, pdf_conversion_state_->page_size(),
|
|
|
|
pdf_conversion_state_->content_area());
|
2015-04-30 03:31:17 +00:00
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
pdf_conversion_state_->GetMorePages(
|
|
|
|
base::Bind(&PrintJob::OnPdfPageConverted, this));
|
2015-04-30 03:31:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:42:12 +00:00
|
|
|
void PrintJob::StartPdfToPostScriptConversion(
|
|
|
|
const scoped_refptr<base::RefCountedMemory>& bytes,
|
|
|
|
const gfx::Rect& content_area,
|
|
|
|
const gfx::Point& physical_offsets,
|
|
|
|
bool ps_level2) {
|
|
|
|
DCHECK(!pdf_conversion_state_);
|
2018-04-12 12:48:32 +00:00
|
|
|
pdf_conversion_state_ = std::make_unique<PdfConversionState>(
|
2017-04-17 10:42:12 +00:00
|
|
|
gfx::Size(), gfx::Rect());
|
|
|
|
const int kPrinterDpi = settings().dpi();
|
|
|
|
PdfRenderSettings settings(
|
|
|
|
content_area, physical_offsets, kPrinterDpi, true /* autorotate? */,
|
|
|
|
ps_level2 ? PdfRenderSettings::Mode::POSTSCRIPT_LEVEL2
|
|
|
|
: PdfRenderSettings::Mode::POSTSCRIPT_LEVEL3);
|
|
|
|
pdf_conversion_state_->Start(
|
|
|
|
bytes, settings, base::Bind(&PrintJob::OnPdfConversionStarted, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(OS_WIN)
|
2015-04-30 03:31:17 +00:00
|
|
|
|
2015-09-01 09:48:43 +00:00
|
|
|
void PrintJob::UpdatePrintedDocument(PrintedDocument* new_document) {
|
|
|
|
if (document_.get() == new_document)
|
|
|
|
return;
|
|
|
|
|
|
|
|
document_ = new_document;
|
|
|
|
|
2017-01-31 08:38:05 +00:00
|
|
|
if (document_.get())
|
2015-09-01 09:48:43 +00:00
|
|
|
settings_ = document_->settings();
|
|
|
|
|
|
|
|
if (worker_) {
|
|
|
|
DCHECK(!is_job_pending_);
|
|
|
|
// Sync the document with the worker.
|
|
|
|
worker_->PostTask(FROM_HERE,
|
2017-12-18 00:23:02 +00:00
|
|
|
base::Bind(&HoldRefCallback, WrapRefCounted(this),
|
2015-09-01 09:48:43 +00:00
|
|
|
base::Bind(&PrintJobWorker::OnDocumentChanged,
|
|
|
|
base::Unretained(worker_.get()),
|
2016-05-23 03:28:59 +00:00
|
|
|
base::RetainedRef(document_))));
|
2015-09-01 09:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::OnNotifyPrintJobEvent(const JobEventDetails& event_details) {
|
|
|
|
switch (event_details.type()) {
|
|
|
|
case JobEventDetails::FAILED: {
|
|
|
|
settings_.Clear();
|
|
|
|
// No need to cancel since the worker already canceled itself.
|
|
|
|
Stop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::USER_INIT_DONE:
|
|
|
|
case JobEventDetails::DEFAULT_INIT_DONE:
|
|
|
|
case JobEventDetails::USER_INIT_CANCELED: {
|
|
|
|
DCHECK_EQ(event_details.document(), document_.get());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::NEW_DOC:
|
|
|
|
case JobEventDetails::NEW_PAGE:
|
|
|
|
case JobEventDetails::JOB_DONE:
|
|
|
|
case JobEventDetails::ALL_PAGES_REQUESTED: {
|
|
|
|
// Don't care.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::DOC_DONE: {
|
|
|
|
// This will call Stop() and broadcast a JOB_DONE message.
|
2016-11-30 07:30:03 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostTask(
|
2015-09-01 09:48:43 +00:00
|
|
|
FROM_HERE, base::Bind(&PrintJob::OnDocumentDone, this));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::PAGE_DONE:
|
|
|
|
#if defined(OS_WIN)
|
2017-04-17 10:42:12 +00:00
|
|
|
if (pdf_conversion_state_) {
|
|
|
|
pdf_conversion_state_->OnPageProcessed(
|
|
|
|
base::Bind(&PrintJob::OnPdfPageConverted, this));
|
|
|
|
}
|
2017-01-31 08:38:05 +00:00
|
|
|
#endif // defined(OS_WIN)
|
2015-09-01 09:48:43 +00:00
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
NOTREACHED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 07:38:20 +00:00
|
|
|
void PrintJob::OnDocumentDone() {
|
|
|
|
// Be sure to live long enough. The instance could be destroyed by the
|
|
|
|
// JOB_DONE broadcast.
|
|
|
|
scoped_refptr<PrintJob> handle(this);
|
|
|
|
|
|
|
|
// Stop the worker thread.
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
scoped_refptr<JobEventDetails> details(
|
2017-01-31 08:38:05 +00:00
|
|
|
new JobEventDetails(JobEventDetails::JOB_DONE, document_.get(), nullptr));
|
2014-08-21 07:38:20 +00:00
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(this),
|
|
|
|
content::Details<JobEventDetails>(details.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::ControlledWorkerShutdown() {
|
2017-08-07 21:14:35 +00:00
|
|
|
DCHECK(RunsTasksInCurrentSequence());
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
// The deadlock this code works around is specific to window messaging on
|
|
|
|
// Windows, so we aren't likely to need it on any other platforms.
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// We could easily get into a deadlock case if worker_->Stop() is used; the
|
|
|
|
// printer driver created a window as a child of the browser window. By
|
|
|
|
// canceling the job, the printer driver initiated dialog box is destroyed,
|
|
|
|
// which sends a blocking message to its parent window. If the browser window
|
|
|
|
// thread is not processing messages, a deadlock occurs.
|
|
|
|
//
|
|
|
|
// This function ensures that the dialog box will be destroyed in a timely
|
|
|
|
// manner by the mere fact that the thread will terminate. So the potential
|
|
|
|
// deadlock is eliminated.
|
|
|
|
worker_->StopSoon();
|
|
|
|
|
|
|
|
// Delay shutdown until the worker terminates. We want this code path
|
|
|
|
// to wait on the thread to quit before continuing.
|
|
|
|
if (worker_->IsRunning()) {
|
2017-01-26 07:10:28 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
2017-01-31 08:38:05 +00:00
|
|
|
FROM_HERE, base::Bind(&PrintJob::ControlledWorkerShutdown, this),
|
2014-08-21 07:38:20 +00:00
|
|
|
base::TimeDelta::FromMilliseconds(100));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// Now make sure the thread object is cleaned up. Do this on a worker
|
|
|
|
// thread because it may block.
|
2017-12-19 00:06:42 +00:00
|
|
|
base::PostTaskAndReply(
|
2014-08-21 07:38:20 +00:00
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&PrintJobWorker::Stop, base::Unretained(worker_.get())),
|
2017-12-19 00:06:42 +00:00
|
|
|
base::Bind(&PrintJob::HoldUntilStopIsCalled, this));
|
2014-08-21 07:38:20 +00:00
|
|
|
|
|
|
|
is_job_pending_ = false;
|
|
|
|
registrar_.RemoveAll();
|
2017-01-31 08:38:05 +00:00
|
|
|
UpdatePrintedDocument(nullptr);
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::HoldUntilStopIsCalled() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintJob::Quit() {
|
2017-11-23 14:14:01 +00:00
|
|
|
base::RunLoop::QuitCurrentWhenIdleDeprecated();
|
2014-08-21 07:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Takes settings_ ownership and will be deleted in the receiving thread.
|
|
|
|
JobEventDetails::JobEventDetails(Type type,
|
|
|
|
PrintedDocument* document,
|
|
|
|
PrintedPage* page)
|
|
|
|
: document_(document),
|
|
|
|
page_(page),
|
|
|
|
type_(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
JobEventDetails::~JobEventDetails() {
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintedDocument* JobEventDetails::document() const { return document_.get(); }
|
|
|
|
|
|
|
|
PrintedPage* JobEventDetails::page() const { return page_.get(); }
|
|
|
|
|
|
|
|
} // namespace printing
|