2013-10-23 21:30:47 +02:00
|
|
|
// 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 "libcef/browser/printing/print_view_manager_base.h"
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
#include <memory>
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2015-08-14 16:41:08 +02:00
|
|
|
#include "base/auto_reset.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "base/bind.h"
|
2017-03-03 23:37:23 +01:00
|
|
|
#include "base/feature_list.h"
|
2015-08-14 16:41:08 +02:00
|
|
|
#include "base/location.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "base/memory/ptr_util.h"
|
2017-05-31 17:33:30 +02:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2016-07-06 21:34:09 +02:00
|
|
|
#include "base/run_loop.h"
|
2015-08-14 16:41:08 +02:00
|
|
|
#include "base/single_thread_task_runner.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "base/timer/timer.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "build/build_config.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "chrome/browser/browser_process.h"
|
|
|
|
#include "chrome/browser/chrome_notification_types.h"
|
|
|
|
#include "chrome/browser/printing/print_job.h"
|
|
|
|
#include "chrome/browser/printing/print_job_manager.h"
|
|
|
|
#include "chrome/browser/printing/printer_query.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "chrome/browser/profiles/profile.h"
|
2017-03-03 23:37:23 +01:00
|
|
|
#include "chrome/common/chrome_features.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "chrome/common/pref_names.h"
|
2014-09-04 19:53:40 +02:00
|
|
|
#include "chrome/grit/generated_resources.h"
|
2016-02-05 01:49:19 +01:00
|
|
|
#include "components/prefs/pref_service.h"
|
2015-02-04 19:10:14 +01:00
|
|
|
#include "components/printing/common/print_messages.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/notification_details.h"
|
|
|
|
#include "content/public/browser/notification_service.h"
|
|
|
|
#include "content/public/browser/notification_source.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "content/public/browser/render_view_host.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "printing/features/features.h"
|
2014-09-27 01:48:19 +02:00
|
|
|
#include "printing/pdf_metafile_skia.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "printing/printed_document.h"
|
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "chrome/common/chrome_switches.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-23 21:30:47 +02:00
|
|
|
using base::TimeDelta;
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
2014-07-02 20:25:22 +02:00
|
|
|
namespace printing {
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefPrintViewManagerBase::CefPrintViewManagerBase(
|
|
|
|
content::WebContents* web_contents)
|
2015-08-14 16:41:08 +02:00
|
|
|
: PrintManager(web_contents),
|
2016-11-23 21:54:29 +01:00
|
|
|
printing_rfh_(nullptr),
|
2013-10-23 21:30:47 +02:00
|
|
|
printing_succeeded_(false),
|
|
|
|
inside_inner_message_loop_(false),
|
2015-10-09 17:23:12 +02:00
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
expecting_first_page_(true),
|
|
|
|
#endif
|
2013-10-23 21:30:47 +02:00
|
|
|
queue_(g_browser_process->print_job_manager()->queue()) {
|
2014-09-04 19:53:40 +02:00
|
|
|
DCHECK(queue_.get());
|
2016-11-23 21:54:29 +01:00
|
|
|
Profile* profile =
|
|
|
|
Profile::FromBrowserContext(web_contents->GetBrowserContext());
|
2013-10-23 21:30:47 +02:00
|
|
|
printing_enabled_.Init(
|
2016-11-23 21:54:29 +01:00
|
|
|
prefs::kPrintingEnabled, profile->GetPrefs(),
|
|
|
|
base::Bind(&CefPrintViewManagerBase::UpdatePrintingEnabled,
|
2013-10-23 21:30:47 +02:00
|
|
|
base::Unretained(this)));
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefPrintViewManagerBase::~CefPrintViewManagerBase() {
|
2013-10-23 21:30:47 +02:00
|
|
|
ReleasePrinterQuery();
|
|
|
|
DisconnectFromCurrentPrintJob();
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
#if BUILDFLAG(ENABLE_BASIC_PRINTING)
|
|
|
|
bool CefPrintViewManagerBase::PrintNow(content::RenderFrameHost* rfh) {
|
|
|
|
DisconnectFromCurrentPrintJob();
|
|
|
|
|
|
|
|
SetPrintingRFH(rfh);
|
|
|
|
int32_t id = rfh->GetRoutingID();
|
|
|
|
return PrintNowInternal(rfh, base::MakeUnique<PrintMsg_PrintPages>(id));
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
2015-10-09 17:23:12 +02:00
|
|
|
#endif
|
2013-10-23 21:30:47 +02:00
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
void CefPrintViewManagerBase::UpdatePrintingEnabled() {
|
|
|
|
web_contents()->ForEachFrame(
|
|
|
|
base::Bind(&CefPrintViewManagerBase::SendPrintingEnabled,
|
|
|
|
base::Unretained(this), printing_enabled_.GetValue()));
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::NavigationStopped() {
|
2013-10-23 21:30:47 +02:00
|
|
|
// Cancel the current job, wait for the worker to finish.
|
|
|
|
TerminatePrintJob(true);
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
base::string16 CefPrintViewManagerBase::RenderSourceName() {
|
2013-12-17 23:04:35 +01:00
|
|
|
base::string16 name(web_contents()->GetTitle());
|
2013-10-23 21:30:47 +02:00
|
|
|
if (name.empty())
|
|
|
|
name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie,
|
2017-05-17 11:29:28 +02:00
|
|
|
int number_pages) {
|
2015-08-14 16:41:08 +02:00
|
|
|
PrintManager::OnDidGetPrintedPagesCount(cookie, number_pages);
|
2013-10-23 21:30:47 +02:00
|
|
|
OpportunisticallyCreatePrintJob(cookie);
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::OnDidPrintPage(
|
2017-05-17 11:29:28 +02:00
|
|
|
const PrintHostMsg_DidPrintPage_Params& params) {
|
2013-10-23 21:30:47 +02:00
|
|
|
if (!OpportunisticallyCreatePrintJob(params.document_cookie))
|
|
|
|
return;
|
|
|
|
|
|
|
|
PrintedDocument* document = print_job_->document();
|
|
|
|
if (!document || params.document_cookie != document->cookie()) {
|
|
|
|
// Out of sync. It may happen since we are completely asynchronous. Old
|
|
|
|
// spurious messages can be received if one of the processes is overloaded.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-27 01:48:19 +02:00
|
|
|
#if defined(OS_MACOSX)
|
2013-10-23 21:30:47 +02:00
|
|
|
const bool metafile_must_be_valid = true;
|
2014-09-27 01:48:19 +02:00
|
|
|
#else
|
2013-10-23 21:30:47 +02:00
|
|
|
const bool metafile_must_be_valid = expecting_first_page_;
|
|
|
|
expecting_first_page_ = false;
|
2015-10-09 17:23:12 +02:00
|
|
|
#endif
|
2013-10-23 21:30:47 +02:00
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
// Only used when |metafile_must_be_valid| is true.
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<base::SharedMemory> shared_buf;
|
2013-10-23 21:30:47 +02:00
|
|
|
if (metafile_must_be_valid) {
|
2015-10-09 17:23:12 +02:00
|
|
|
if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) {
|
|
|
|
NOTREACHED() << "invalid memory handle";
|
|
|
|
web_contents()->Stop();
|
|
|
|
return;
|
|
|
|
}
|
2016-10-21 21:52:29 +02:00
|
|
|
shared_buf =
|
|
|
|
base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true);
|
2015-10-09 17:23:12 +02:00
|
|
|
if (!shared_buf->Map(params.data_size)) {
|
2013-10-23 21:30:47 +02:00
|
|
|
NOTREACHED() << "couldn't map";
|
|
|
|
web_contents()->Stop();
|
|
|
|
return;
|
|
|
|
}
|
2015-10-09 17:23:12 +02:00
|
|
|
} else {
|
|
|
|
if (base::SharedMemory::IsHandleValid(params.metafile_data_handle)) {
|
|
|
|
NOTREACHED() << "unexpected valid memory handle";
|
|
|
|
web_contents()->Stop();
|
|
|
|
base::SharedMemory::CloseHandle(params.metafile_data_handle);
|
|
|
|
return;
|
|
|
|
}
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:59:23 +02:00
|
|
|
std::unique_ptr<PdfMetafileSkia> metafile(
|
2017-09-06 23:40:58 +02:00
|
|
|
new PdfMetafileSkia(SkiaDocumentType::PDF));
|
2013-10-23 21:30:47 +02:00
|
|
|
if (metafile_must_be_valid) {
|
2015-10-09 17:23:12 +02:00
|
|
|
if (!metafile->InitFromData(shared_buf->memory(), params.data_size)) {
|
2013-10-23 21:30:47 +02:00
|
|
|
NOTREACHED() << "Invalid metafile header";
|
|
|
|
web_contents()->Stop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:23:12 +02:00
|
|
|
#if defined(OS_WIN)
|
2016-08-31 13:25:56 +02:00
|
|
|
print_job_->AppendPrintedPage(params.page_number);
|
2014-07-03 20:34:58 +02:00
|
|
|
if (metafile_must_be_valid) {
|
|
|
|
scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes(
|
2015-10-09 17:23:12 +02:00
|
|
|
reinterpret_cast<const unsigned char*>(shared_buf->memory()),
|
2014-07-03 20:34:58 +02:00
|
|
|
params.data_size);
|
|
|
|
|
2014-12-13 21:18:31 +01:00
|
|
|
document->DebugDumpData(bytes.get(), FILE_PATH_LITERAL(".pdf"));
|
2017-03-03 23:37:23 +01:00
|
|
|
|
|
|
|
const auto& settings = document->settings();
|
2017-07-27 01:19:27 +02:00
|
|
|
if (settings.printer_is_textonly()) {
|
|
|
|
print_job_->StartPdfToTextConversion(bytes, params.page_size);
|
|
|
|
} else if ((settings.printer_is_ps2() || settings.printer_is_ps3()) &&
|
|
|
|
!base::FeatureList::IsEnabled(
|
|
|
|
features::kDisablePostScriptPrinting)) {
|
2017-03-03 23:37:23 +01:00
|
|
|
print_job_->StartPdfToPostScriptConversion(bytes, params.content_area,
|
|
|
|
params.physical_offsets,
|
|
|
|
settings.printer_is_ps2());
|
|
|
|
} else {
|
|
|
|
// TODO(thestig): Figure out why rendering text with GDI results in random
|
|
|
|
// missing characters for some users. https://crbug.com/658606
|
|
|
|
// Update : The missing letters seem to have been caused by the same
|
|
|
|
// problem as https://crbug.com/659604 which was resolved. GDI printing
|
|
|
|
// seems to work with the fix for this bug applied.
|
2017-05-17 11:29:28 +02:00
|
|
|
bool print_text_with_gdi =
|
|
|
|
settings.print_text_with_gdi() && !settings.printer_is_xps() &&
|
|
|
|
base::FeatureList::IsEnabled(features::kGdiTextPrinting);
|
2017-03-03 23:37:23 +01:00
|
|
|
print_job_->StartPdfToEmfConversion(
|
|
|
|
bytes, params.page_size, params.content_area, print_text_with_gdi);
|
|
|
|
}
|
2014-07-03 20:34:58 +02:00
|
|
|
}
|
2015-10-09 17:23:12 +02:00
|
|
|
#else
|
|
|
|
// Update the rendered document. It will send notifications to the listener.
|
2017-05-17 11:29:28 +02:00
|
|
|
document->SetPage(params.page_number, std::move(metafile),
|
2017-03-03 23:37:23 +01:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
0.0f /* dummy shrink_factor */,
|
|
|
|
#endif
|
2017-05-17 11:29:28 +02:00
|
|
|
params.page_size, params.content_area);
|
2015-10-09 17:23:12 +02:00
|
|
|
|
|
|
|
ShouldQuitFromInnerMessageLoop();
|
|
|
|
#endif
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::OnPrintingFailed(int cookie) {
|
2015-08-14 16:41:08 +02:00
|
|
|
PrintManager::OnPrintingFailed(cookie);
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
ReleasePrinterQuery();
|
|
|
|
|
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_RELEASED,
|
|
|
|
content::Source<content::WebContents>(web_contents()),
|
|
|
|
content::NotificationService::NoDetails());
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefPrintViewManagerBase::OnShowInvalidPrinterSettingsError() {}
|
2014-09-27 01:48:19 +02:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::DidStartLoading() {
|
2016-11-23 21:54:29 +01:00
|
|
|
UpdatePrintingEnabled();
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
void CefPrintViewManagerBase::RenderFrameDeleted(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
// Terminates or cancels the print job if one was pending.
|
|
|
|
if (render_frame_host != printing_rfh_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printing_rfh_ = nullptr;
|
|
|
|
|
|
|
|
PrintManager::PrintingRenderFrameDeleted();
|
|
|
|
ReleasePrinterQuery();
|
|
|
|
|
|
|
|
if (!print_job_.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
scoped_refptr<PrintedDocument> document(print_job_->document());
|
|
|
|
if (document.get()) {
|
|
|
|
// If IsComplete() returns false, the document isn't completely rendered.
|
|
|
|
// Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
|
|
|
|
// the print job may finish without problem.
|
|
|
|
TerminatePrintJob(!document->IsComplete());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefPrintViewManagerBase::OnMessageReceived(
|
|
|
|
const IPC::Message& message,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
2013-10-23 21:30:47 +02:00
|
|
|
bool handled = true;
|
2016-07-14 03:35:07 +02:00
|
|
|
IPC_BEGIN_MESSAGE_MAP(CefPrintViewManagerBase, message)
|
2013-10-23 21:30:47 +02:00
|
|
|
IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
|
2014-09-27 01:48:19 +02:00
|
|
|
IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError,
|
2016-11-23 21:54:29 +01:00
|
|
|
OnShowInvalidPrinterSettingsError)
|
2013-10-23 21:30:47 +02:00
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
2016-11-23 21:54:29 +01:00
|
|
|
return handled || PrintManager::OnMessageReceived(message, render_frame_host);
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::Observe(
|
2013-10-23 21:30:47 +02:00
|
|
|
int type,
|
|
|
|
const content::NotificationSource& source,
|
|
|
|
const content::NotificationDetails& details) {
|
2016-06-21 00:59:23 +02:00
|
|
|
DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_EVENT, type);
|
|
|
|
OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::OnNotifyPrintJobEvent(
|
2013-10-23 21:30:47 +02:00
|
|
|
const JobEventDetails& event_details) {
|
|
|
|
switch (event_details.type()) {
|
|
|
|
case JobEventDetails::FAILED: {
|
|
|
|
TerminatePrintJob(true);
|
|
|
|
|
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_RELEASED,
|
|
|
|
content::Source<content::WebContents>(web_contents()),
|
|
|
|
content::NotificationService::NoDetails());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::USER_INIT_DONE:
|
|
|
|
case JobEventDetails::DEFAULT_INIT_DONE:
|
|
|
|
case JobEventDetails::USER_INIT_CANCELED: {
|
|
|
|
NOTREACHED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::ALL_PAGES_REQUESTED: {
|
|
|
|
ShouldQuitFromInnerMessageLoop();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::NEW_DOC:
|
|
|
|
case JobEventDetails::NEW_PAGE:
|
|
|
|
case JobEventDetails::PAGE_DONE:
|
|
|
|
case JobEventDetails::DOC_DONE: {
|
|
|
|
// Don't care about the actual printing process.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JobEventDetails::JOB_DONE: {
|
|
|
|
// Printing is done, we don't need it anymore.
|
|
|
|
// print_job_->is_job_pending() may still be true, depending on the order
|
|
|
|
// of object registration.
|
|
|
|
printing_succeeded_ = true;
|
|
|
|
ReleasePrintJob();
|
|
|
|
|
|
|
|
content::NotificationService::current()->Notify(
|
|
|
|
chrome::NOTIFICATION_PRINT_JOB_RELEASED,
|
|
|
|
content::Source<content::WebContents>(web_contents()),
|
|
|
|
content::NotificationService::NoDetails());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
NOTREACHED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefPrintViewManagerBase::RenderAllMissingPagesNow() {
|
2013-10-23 21:30:47 +02:00
|
|
|
if (!print_job_.get() || !print_job_->is_job_pending())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We can't print if there is no renderer.
|
2017-05-17 11:29:28 +02:00
|
|
|
if (!web_contents() || !web_contents()->GetRenderViewHost() ||
|
2013-10-23 21:30:47 +02:00
|
|
|
!web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the document already complete?
|
|
|
|
if (print_job_->document() && print_job_->document()->IsComplete()) {
|
|
|
|
printing_succeeded_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebContents is either dying or a second consecutive request to print
|
|
|
|
// happened before the first had time to finish. We need to render all the
|
|
|
|
// pages in an hurry if a print_job_ is still pending. No need to wait for it
|
|
|
|
// to actually spool the pages, only to have the renderer generate them. Run
|
|
|
|
// a message loop until we get our signal that the print job is satisfied.
|
|
|
|
// PrintJob will send a ALL_PAGES_REQUESTED after having received all the
|
2017-09-06 23:40:58 +02:00
|
|
|
// pages it needs. RunLoop::QuitCurrentWhenIdleDeprecated() will be called as
|
2015-11-10 21:18:16 +01:00
|
|
|
// soon as print_job_->document()->IsComplete() is true on either
|
|
|
|
// ALL_PAGES_REQUESTED or in DidPrintPage(). The check is done in
|
2013-10-23 21:30:47 +02:00
|
|
|
// ShouldQuitFromInnerMessageLoop().
|
|
|
|
// BLOCKS until all the pages are received. (Need to enable recursive task)
|
|
|
|
if (!RunInnerMessageLoop()) {
|
|
|
|
// This function is always called from DisconnectFromCurrentPrintJob() so we
|
|
|
|
// know that the job will be stopped/canceled in any case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::ShouldQuitFromInnerMessageLoop() {
|
2013-10-23 21:30:47 +02:00
|
|
|
// Look at the reason.
|
|
|
|
DCHECK(print_job_->document());
|
2017-05-17 11:29:28 +02:00
|
|
|
if (print_job_->document() && print_job_->document()->IsComplete() &&
|
2013-10-23 21:30:47 +02:00
|
|
|
inside_inner_message_loop_) {
|
|
|
|
// We are in a message loop created by RenderAllMissingPagesNow. Quit from
|
|
|
|
// it.
|
2017-09-06 23:40:58 +02:00
|
|
|
base::RunLoop::QuitCurrentWhenIdleDeprecated();
|
2013-10-23 21:30:47 +02:00
|
|
|
inside_inner_message_loop_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefPrintViewManagerBase::CreateNewPrintJob(PrintJobWorkerOwner* job) {
|
2013-10-23 21:30:47 +02:00
|
|
|
DCHECK(!inside_inner_message_loop_);
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
// Disconnect the current |print_job_|.
|
2013-10-23 21:30:47 +02:00
|
|
|
DisconnectFromCurrentPrintJob();
|
|
|
|
|
|
|
|
// We can't print if there is no renderer.
|
|
|
|
if (!web_contents()->GetRenderViewHost() ||
|
|
|
|
!web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the renderer to generate the print preview, create the print preview
|
|
|
|
// view and switch to it, initialize the printer and show the print dialog.
|
|
|
|
DCHECK(!print_job_.get());
|
|
|
|
DCHECK(job);
|
|
|
|
if (!job)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
print_job_ = new PrintJob();
|
|
|
|
print_job_->Initialize(job, this, number_pages_);
|
|
|
|
registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(print_job_.get()));
|
|
|
|
printing_succeeded_ = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::DisconnectFromCurrentPrintJob() {
|
2013-10-23 21:30:47 +02:00
|
|
|
// Make sure all the necessary rendered page are done. Don't bother with the
|
|
|
|
// return value.
|
|
|
|
bool result = RenderAllMissingPagesNow();
|
|
|
|
|
|
|
|
// Verify that assertion.
|
2017-05-17 11:29:28 +02:00
|
|
|
if (print_job_.get() && print_job_->document() &&
|
2013-10-23 21:30:47 +02:00
|
|
|
!print_job_->document()->IsComplete()) {
|
|
|
|
DCHECK(!result);
|
|
|
|
// That failed.
|
|
|
|
TerminatePrintJob(true);
|
|
|
|
} else {
|
|
|
|
// DO NOT wait for the job to finish.
|
|
|
|
ReleasePrintJob();
|
|
|
|
}
|
2014-09-27 01:48:19 +02:00
|
|
|
#if !defined(OS_MACOSX)
|
2013-10-23 21:30:47 +02:00
|
|
|
expecting_first_page_ = true;
|
2015-10-09 17:23:12 +02:00
|
|
|
#endif
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::TerminatePrintJob(bool cancel) {
|
2013-10-23 21:30:47 +02:00
|
|
|
if (!print_job_.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (cancel) {
|
|
|
|
// We don't need the metafile data anymore because the printing is canceled.
|
|
|
|
print_job_->Cancel();
|
|
|
|
inside_inner_message_loop_ = false;
|
|
|
|
} else {
|
|
|
|
DCHECK(!inside_inner_message_loop_);
|
|
|
|
DCHECK(!print_job_->document() || print_job_->document()->IsComplete());
|
|
|
|
|
|
|
|
// WebContents is either dying or navigating elsewhere. We need to render
|
|
|
|
// all the pages in an hurry if a print job is still pending. This does the
|
|
|
|
// trick since it runs a blocking message loop:
|
|
|
|
print_job_->Stop();
|
|
|
|
}
|
|
|
|
ReleasePrintJob();
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::ReleasePrintJob() {
|
2016-11-23 21:54:29 +01:00
|
|
|
content::RenderFrameHost* rfh = printing_rfh_;
|
|
|
|
printing_rfh_ = nullptr;
|
|
|
|
|
2013-10-23 21:30:47 +02:00
|
|
|
if (!print_job_.get())
|
|
|
|
return;
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
if (rfh) {
|
|
|
|
auto msg = base::MakeUnique<PrintMsg_PrintingDone>(rfh->GetRoutingID(),
|
|
|
|
printing_succeeded_);
|
|
|
|
rfh->Send(msg.release());
|
|
|
|
}
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
|
|
|
|
content::Source<PrintJob>(print_job_.get()));
|
|
|
|
print_job_->DisconnectSource();
|
|
|
|
// Don't close the worker thread.
|
2016-11-23 21:54:29 +01:00
|
|
|
print_job_ = nullptr;
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefPrintViewManagerBase::RunInnerMessageLoop() {
|
2013-10-23 21:30:47 +02:00
|
|
|
// This value may actually be too low:
|
|
|
|
//
|
|
|
|
// - If we're looping because of printer settings initialization, the premise
|
|
|
|
// here is that some poor users have their print server away on a VPN over a
|
|
|
|
// slow connection. In this situation, the simple fact of opening the printer
|
|
|
|
// can be dead slow. On the other side, we don't want to die infinitely for a
|
|
|
|
// real network error. Give the printer 60 seconds to comply.
|
|
|
|
//
|
|
|
|
// - If we're looping because of renderer page generation, the renderer could
|
|
|
|
// be CPU bound, the page overly complex/large or the system just
|
|
|
|
// memory-bound.
|
|
|
|
static const int kPrinterSettingsTimeout = 60000;
|
2015-10-09 17:23:12 +02:00
|
|
|
base::OneShotTimer quit_timer;
|
2017-09-06 23:40:58 +02:00
|
|
|
base::RunLoop run_loop;
|
|
|
|
quit_timer.Start(FROM_HERE,
|
|
|
|
TimeDelta::FromMilliseconds(kPrinterSettingsTimeout),
|
|
|
|
run_loop.QuitWhenIdleClosure());
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
inside_inner_message_loop_ = true;
|
|
|
|
|
|
|
|
// Need to enable recursive task.
|
|
|
|
{
|
|
|
|
base::MessageLoop::ScopedNestableTaskAllower allow(
|
|
|
|
base::MessageLoop::current());
|
2017-09-06 23:40:58 +02:00
|
|
|
run_loop.Run();
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
if (inside_inner_message_loop_) {
|
|
|
|
// Ok we timed out. That's sad.
|
|
|
|
inside_inner_message_loop_ = false;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
bool CefPrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
|
2013-10-23 21:30:47 +02:00
|
|
|
if (print_job_.get())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!cookie) {
|
|
|
|
// Out of sync. It may happens since we are completely asynchronous. Old
|
|
|
|
// spurious message can happen if one of the processes is overloaded.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The job was initiated by a script. Time to get the corresponding worker
|
|
|
|
// thread.
|
|
|
|
scoped_refptr<PrinterQuery> queued_query = queue_->PopPrinterQuery(cookie);
|
2014-09-04 19:53:40 +02:00
|
|
|
if (!queued_query.get()) {
|
2013-10-23 21:30:47 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:53:40 +02:00
|
|
|
if (!CreateNewPrintJob(queued_query.get())) {
|
2013-10-23 21:30:47 +02:00
|
|
|
// Don't kill anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settings are already loaded. Go ahead. This will set
|
|
|
|
// print_job_->is_job_pending() to true.
|
|
|
|
print_job_->StartPrinting();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
bool CefPrintViewManagerBase::PrintNowInternal(
|
|
|
|
content::RenderFrameHost* rfh,
|
|
|
|
std::unique_ptr<IPC::Message> message) {
|
2015-11-10 21:18:16 +01:00
|
|
|
// Don't print / print preview interstitials or crashed tabs.
|
2016-11-23 21:54:29 +01:00
|
|
|
if (web_contents()->ShowingInterstitialPage() || web_contents()->IsCrashed())
|
2013-10-23 21:30:47 +02:00
|
|
|
return false;
|
2016-11-23 21:54:29 +01:00
|
|
|
return rfh->Send(message.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintViewManagerBase::SetPrintingRFH(content::RenderFrameHost* rfh) {
|
|
|
|
DCHECK(!printing_rfh_);
|
|
|
|
printing_rfh_ = rfh;
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManagerBase::ReleasePrinterQuery() {
|
2013-10-23 21:30:47 +02:00
|
|
|
if (!cookie_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int cookie = cookie_;
|
|
|
|
cookie_ = 0;
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
PrintJobManager* print_job_manager = g_browser_process->print_job_manager();
|
2013-10-23 21:30:47 +02:00
|
|
|
// May be NULL in tests.
|
|
|
|
if (!print_job_manager)
|
|
|
|
return;
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
scoped_refptr<PrinterQuery> printer_query;
|
2013-10-23 21:30:47 +02:00
|
|
|
printer_query = queue_->PopPrinterQuery(cookie);
|
2014-09-04 19:53:40 +02:00
|
|
|
if (!printer_query.get())
|
2013-10-23 21:30:47 +02:00
|
|
|
return;
|
2017-05-31 17:33:30 +02:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::BindOnce(&PrinterQuery::StopWorker, printer_query));
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
void CefPrintViewManagerBase::SendPrintingEnabled(
|
|
|
|
bool enabled,
|
|
|
|
content::RenderFrameHost* rfh) {
|
|
|
|
rfh->Send(new PrintMsg_SetPrintingEnabled(rfh->GetRoutingID(), enabled));
|
|
|
|
}
|
|
|
|
|
2013-10-23 21:30:47 +02:00
|
|
|
} // namespace printing
|