2013-10-23 21:30:47 +02: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 "libcef/browser/printing/print_view_manager.h"
|
2017-05-19 11:06:00 +02:00
|
|
|
#include "include/internal/cef_types_wrappers.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
#include <map>
|
2016-01-06 20:20:54 +01:00
|
|
|
#include <utility>
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/lazy_instance.h"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "base/memory/ptr_util.h"
|
2016-10-17 20:14:44 +02:00
|
|
|
#include "base/memory/ref_counted_memory.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "chrome/browser/browser_process.h"
|
|
|
|
#include "chrome/browser/printing/print_job_manager.h"
|
2015-03-24 16:40:08 +01:00
|
|
|
#include "chrome/browser/printing/printer_query.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"
|
2016-11-23 21:54:29 +01:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2015-03-24 16:40:08 +01:00
|
|
|
#include "printing/pdf_metafile_skia.h"
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::CefPrintViewManager);
|
2013-10-23 21:30:47 +02:00
|
|
|
|
|
|
|
namespace printing {
|
|
|
|
|
2015-03-24 16:40:08 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const int PREVIEW_UIID = 12345678;
|
|
|
|
|
|
|
|
// Convert CefPdfPrintSettings into base::DictionaryValue.
|
|
|
|
void FillInDictionaryFromPdfPrintSettings(
|
|
|
|
const CefPdfPrintSettings& pdf_settings,
|
|
|
|
int request_id,
|
|
|
|
base::DictionaryValue& print_settings) {
|
|
|
|
// Fixed settings.
|
|
|
|
print_settings.SetBoolean(kSettingPrintToPDF, true);
|
|
|
|
print_settings.SetBoolean(kSettingCloudPrintDialog, false);
|
|
|
|
print_settings.SetBoolean(kSettingPrintWithPrivet, false);
|
|
|
|
print_settings.SetBoolean(kSettingPrintWithExtension, false);
|
|
|
|
print_settings.SetInteger(kSettingColor, GRAY);
|
|
|
|
print_settings.SetInteger(kSettingDuplexMode, SIMPLEX);
|
|
|
|
print_settings.SetInteger(kSettingCopies, 1);
|
|
|
|
print_settings.SetBoolean(kSettingCollate, false);
|
|
|
|
print_settings.SetString(kSettingDeviceName, "");
|
2017-04-06 17:20:07 +02:00
|
|
|
print_settings.SetBoolean(kSettingRasterizePdf, false);
|
2015-03-24 16:40:08 +01:00
|
|
|
print_settings.SetBoolean(kSettingGenerateDraftData, false);
|
|
|
|
print_settings.SetBoolean(kSettingPreviewModifiable, false);
|
2017-04-20 21:28:17 +02:00
|
|
|
print_settings.SetInteger(kSettingDpiHorizontal, 0);
|
|
|
|
print_settings.SetInteger(kSettingDpiVertical, 0);
|
2015-03-24 16:40:08 +01:00
|
|
|
|
|
|
|
// User defined settings.
|
|
|
|
print_settings.SetBoolean(kSettingLandscape, !!pdf_settings.landscape);
|
|
|
|
print_settings.SetBoolean(kSettingShouldPrintSelectionOnly,
|
|
|
|
!!pdf_settings.selection_only);
|
|
|
|
print_settings.SetBoolean(kSettingShouldPrintBackgrounds,
|
|
|
|
!!pdf_settings.backgrounds_enabled);
|
|
|
|
print_settings.SetBoolean(kSettingHeaderFooterEnabled,
|
|
|
|
!!pdf_settings.header_footer_enabled);
|
2017-05-17 11:29:28 +02:00
|
|
|
print_settings.SetInteger(kSettingScaleFactor, pdf_settings.scale_factor > 0
|
|
|
|
? pdf_settings.scale_factor
|
|
|
|
: 100);
|
2015-03-24 16:40:08 +01:00
|
|
|
|
|
|
|
if (pdf_settings.header_footer_enabled) {
|
2017-05-17 11:29:28 +02:00
|
|
|
print_settings.SetString(
|
|
|
|
kSettingHeaderFooterTitle,
|
2015-03-24 16:40:08 +01:00
|
|
|
CefString(&pdf_settings.header_footer_title).ToString16());
|
2017-05-17 11:29:28 +02:00
|
|
|
print_settings.SetString(
|
|
|
|
kSettingHeaderFooterURL,
|
2015-03-24 16:40:08 +01:00
|
|
|
CefString(&pdf_settings.header_footer_url).ToString16());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pdf_settings.page_width > 0 && pdf_settings.page_height > 0) {
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
|
2015-03-24 16:40:08 +01:00
|
|
|
dict->SetInteger(kSettingMediaSizeWidthMicrons, pdf_settings.page_width);
|
|
|
|
dict->SetInteger(kSettingMediaSizeHeightMicrons, pdf_settings.page_height);
|
2016-01-06 20:20:54 +01:00
|
|
|
print_settings.Set(kSettingMediaSize, std::move(dict));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int margin_type = DEFAULT_MARGINS;
|
|
|
|
switch (pdf_settings.margin_type) {
|
|
|
|
case PDF_PRINT_MARGIN_NONE:
|
|
|
|
margin_type = NO_MARGINS;
|
|
|
|
break;
|
|
|
|
case PDF_PRINT_MARGIN_MINIMUM:
|
|
|
|
margin_type = PRINTABLE_AREA_MARGINS;
|
|
|
|
break;
|
|
|
|
case PDF_PRINT_MARGIN_CUSTOM:
|
|
|
|
margin_type = CUSTOM_MARGINS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
print_settings.SetInteger(kSettingMarginsType, margin_type);
|
|
|
|
if (margin_type == CUSTOM_MARGINS) {
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
|
2015-03-24 16:40:08 +01:00
|
|
|
dict->SetDouble(kSettingMarginTop, pdf_settings.margin_top);
|
|
|
|
dict->SetDouble(kSettingMarginRight, pdf_settings.margin_right);
|
|
|
|
dict->SetDouble(kSettingMarginBottom, pdf_settings.margin_bottom);
|
|
|
|
dict->SetDouble(kSettingMarginLeft, pdf_settings.margin_left);
|
2016-01-06 20:20:54 +01:00
|
|
|
print_settings.Set(kSettingMarginsCustom, std::move(dict));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Service settings.
|
|
|
|
print_settings.SetInteger(kPreviewUIID, PREVIEW_UIID);
|
|
|
|
print_settings.SetInteger(kPreviewRequestID, request_id);
|
|
|
|
print_settings.SetBoolean(kIsFirstRequest, request_id != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopWorker(int document_cookie) {
|
|
|
|
if (document_cookie <= 0)
|
|
|
|
return;
|
|
|
|
scoped_refptr<PrintQueriesQueue> queue =
|
|
|
|
g_browser_process->print_job_manager()->queue();
|
|
|
|
scoped_refptr<PrinterQuery> printer_query =
|
|
|
|
queue->PopPrinterQuery(document_cookie);
|
|
|
|
if (printer_query.get()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(&PrinterQuery::StopWorker, printer_query));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
scoped_refptr<base::RefCountedBytes> GetDataFromHandle(
|
|
|
|
base::SharedMemoryHandle handle,
|
|
|
|
uint32_t data_size) {
|
2016-11-23 21:54:29 +01:00
|
|
|
std::unique_ptr<base::SharedMemory> shared_buf =
|
|
|
|
base::MakeUnique<base::SharedMemory>(handle, true);
|
2015-03-24 16:40:08 +01:00
|
|
|
|
|
|
|
if (!shared_buf->Map(data_size)) {
|
|
|
|
NOTREACHED();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* data = static_cast<unsigned char*>(shared_buf->memory());
|
|
|
|
std::vector<unsigned char> dataVector(data, data + data_size);
|
|
|
|
return base::RefCountedBytes::TakeVector(&dataVector);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the PDF file to disk.
|
|
|
|
void SavePdfFile(scoped_refptr<base::RefCountedBytes> data,
|
|
|
|
const base::FilePath& path,
|
2016-07-14 03:35:07 +02:00
|
|
|
const CefPrintViewManager::PdfPrintCallback& callback) {
|
2015-03-24 16:40:08 +01:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
|
|
|
|
DCHECK_GT(data->size(), 0U);
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
PdfMetafileSkia metafile(SkiaDocumentType::PDF);
|
2015-03-24 16:40:08 +01:00
|
|
|
metafile.InitFromData(static_cast<const void*>(data->front()), data->size());
|
|
|
|
|
|
|
|
base::File file(path,
|
|
|
|
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
|
|
|
bool ok = file.IsValid() && metafile.SaveTo(&file);
|
|
|
|
|
|
|
|
if (!callback.is_null()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
2015-03-24 16:40:08 +01:00
|
|
|
base::Bind(callback, ok));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
struct CefPrintViewManager::PdfPrintState {
|
|
|
|
content::RenderFrameHost* printing_rfh_ = nullptr;
|
|
|
|
base::FilePath output_path_;
|
|
|
|
base::DictionaryValue settings_;
|
|
|
|
PdfPrintCallback callback_;
|
|
|
|
};
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefPrintViewManager::CefPrintViewManager(content::WebContents* web_contents)
|
2017-05-17 11:29:28 +02:00
|
|
|
: CefPrintViewManagerBase(web_contents) {}
|
2013-10-23 21:30:47 +02:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
CefPrintViewManager::~CefPrintViewManager() {
|
2015-03-24 16:40:08 +01:00
|
|
|
TerminatePdfPrintJob();
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
bool CefPrintViewManager::PrintToPDF(content::RenderFrameHost* rfh,
|
|
|
|
const base::FilePath& path,
|
|
|
|
const CefPdfPrintSettings& settings,
|
|
|
|
const PdfPrintCallback& callback) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2013-10-23 21:30:47 +02:00
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
// Don't start print again while printing is currently in progress.
|
|
|
|
if (pdf_print_state_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't print interstitials or crashed tabs.
|
|
|
|
if (!web_contents() || web_contents()->ShowingInterstitialPage() ||
|
|
|
|
web_contents()->IsCrashed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-23 21:30:47 +02:00
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
pdf_print_state_.reset(new PdfPrintState);
|
|
|
|
pdf_print_state_->printing_rfh_ = rfh;
|
|
|
|
pdf_print_state_->output_path_ = path;
|
|
|
|
pdf_print_state_->callback_ = callback;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
FillInDictionaryFromPdfPrintSettings(settings, ++next_pdf_request_id_,
|
2016-11-23 21:54:29 +01:00
|
|
|
pdf_print_state_->settings_);
|
|
|
|
|
|
|
|
rfh->Send(new PrintMsg_InitiatePrintPreview(rfh->GetRoutingID(),
|
|
|
|
!!settings.selection_only));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefPrintViewManager::RenderFrameDeleted(
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
if (pdf_print_state_ &&
|
|
|
|
render_frame_host == pdf_print_state_->printing_rfh_) {
|
|
|
|
TerminatePdfPrintJob();
|
|
|
|
}
|
|
|
|
CefPrintViewManagerBase::RenderFrameDeleted(render_frame_host);
|
2013-10-23 21:30:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManager::NavigationStopped() {
|
2015-03-24 16:40:08 +01:00
|
|
|
TerminatePdfPrintJob();
|
2016-11-23 21:54:29 +01:00
|
|
|
CefPrintViewManagerBase::NavigationStopped();
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManager::RenderProcessGone(base::TerminationStatus status) {
|
2015-03-24 16:40:08 +01:00
|
|
|
TerminatePdfPrintJob();
|
2016-11-23 21:54:29 +01:00
|
|
|
CefPrintViewManagerBase::RenderProcessGone(status);
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
bool CefPrintViewManager::OnMessageReceived(
|
|
|
|
const IPC::Message& message,
|
|
|
|
content::RenderFrameHost* render_frame_host) {
|
|
|
|
bool handled = true;
|
|
|
|
IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(CefPrintViewManager, message,
|
|
|
|
render_frame_host)
|
|
|
|
IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
|
2017-05-17 11:29:28 +02:00
|
|
|
IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, OnRequestPrintPreview)
|
2016-11-23 21:54:29 +01:00
|
|
|
IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting,
|
|
|
|
OnMetafileReadyForPrinting)
|
|
|
|
IPC_MESSAGE_UNHANDLED(handled = false)
|
|
|
|
IPC_END_MESSAGE_MAP()
|
2015-03-24 16:40:08 +01:00
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
return handled ||
|
|
|
|
CefPrintViewManagerBase::OnMessageReceived(message, render_frame_host);
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefPrintViewManager::OnDidShowPrintDialog(content::RenderFrameHost* rfh) {}
|
2015-03-24 16:40:08 +01:00
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManager::OnRequestPrintPreview(
|
2015-03-24 16:40:08 +01:00
|
|
|
const PrintHostMsg_RequestPrintPreview_Params&) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2016-11-23 21:54:29 +01:00
|
|
|
if (!pdf_print_state_)
|
2015-03-24 16:40:08 +01:00
|
|
|
return;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
pdf_print_state_->printing_rfh_->Send(
|
|
|
|
new PrintMsg_PrintPreview(pdf_print_state_->printing_rfh_->GetRoutingID(),
|
|
|
|
pdf_print_state_->settings_));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManager::OnMetafileReadyForPrinting(
|
2015-03-24 16:40:08 +01:00
|
|
|
const PrintHostMsg_DidPreviewDocument_Params& params) {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
StopWorker(params.document_cookie);
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
if (!pdf_print_state_)
|
|
|
|
return;
|
|
|
|
|
2015-03-24 16:40:08 +01:00
|
|
|
scoped_refptr<base::RefCountedBytes> data_bytes =
|
|
|
|
GetDataFromHandle(params.metafile_data_handle, params.data_size);
|
|
|
|
if (!data_bytes || !data_bytes->size()) {
|
|
|
|
TerminatePdfPrintJob();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
const base::FilePath output_path = pdf_print_state_->output_path_;
|
|
|
|
const PdfPrintCallback print_callback = pdf_print_state_->callback_;
|
2015-03-24 16:40:08 +01:00
|
|
|
|
|
|
|
// Reset state information.
|
2016-11-23 21:54:29 +01:00
|
|
|
pdf_print_state_.reset();
|
2015-03-24 16:40:08 +01:00
|
|
|
|
|
|
|
// Save the PDF file to disk and then execute the callback.
|
2017-05-17 11:29:28 +02:00
|
|
|
BrowserThread::PostTask(
|
|
|
|
BrowserThread::FILE, FROM_HERE,
|
2016-11-23 21:54:29 +01:00
|
|
|
base::Bind(&SavePdfFile, data_bytes, output_path, print_callback));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 03:35:07 +02:00
|
|
|
void CefPrintViewManager::TerminatePdfPrintJob() {
|
2015-03-24 16:40:08 +01:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
2016-11-23 21:54:29 +01:00
|
|
|
if (!pdf_print_state_)
|
2015-03-24 16:40:08 +01:00
|
|
|
return;
|
|
|
|
|
2016-11-23 21:54:29 +01:00
|
|
|
if (!pdf_print_state_->callback_.is_null()) {
|
2015-03-24 16:40:08 +01:00
|
|
|
// Execute the callback.
|
2017-05-17 11:29:28 +02:00
|
|
|
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(pdf_print_state_->callback_, false));
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset state information.
|
2016-11-23 21:54:29 +01:00
|
|
|
pdf_print_state_.reset();
|
2015-03-24 16:40:08 +01:00
|
|
|
}
|
|
|
|
|
2013-10-23 21:30:47 +02:00
|
|
|
} // namespace printing
|