/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWebEngine module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 3 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL3 included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 3 requirements ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 2.0 or (at your option) the GNU General ** Public license version 3 or any later version approved by the KDE Free ** Qt Foundation. The licenses are as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-2.0.html and ** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ // Loosely based on print_view_manager.cc and print_preview_message_handler.cc // 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.Chromium file. #include "print_view_manager_qt.h" #include "type_conversion.h" #include "web_contents_adapter_client.h" #include "web_contents_view_qt.h" #include "web_engine_context.h" #include #include #include #include "base/values.h" #include "base/memory/ref_counted_memory.h" #include "base/task/post_task.h" #include "base/task/thread_pool.h" #include "chrome/browser/printing/print_job_manager.h" #include "chrome/browser/printing/printer_query.h" #include "components/printing/common/print.mojom.h" #include "content/browser/renderer_host/render_view_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/render_frame_host.h" #include "printing/metafile_skia.h" #include "printing/mojom/print.mojom-shared.h" #include "printing/print_job_constants.h" #include "printing/units.h" #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" namespace { static const qreal kMicronsToMillimeter = 1000.0f; static QSharedPointer GetStdVectorFromHandle(const base::ReadOnlySharedMemoryRegion &handle) { base::ReadOnlySharedMemoryMapping map = handle.Map(); if (!map.IsValid()) return QSharedPointer(new QByteArray); const char* data = static_cast(map.memory()); return QSharedPointer(new QByteArray(data, map.size())); } static scoped_refptr GetBytesFromHandle(const base::ReadOnlySharedMemoryRegion &handle) { base::ReadOnlySharedMemoryMapping map = handle.Map(); if (!map.IsValid()) return nullptr; const unsigned char* data = static_cast(map.memory()); std::vector dataVector(data, data + map.size()); return base::RefCountedBytes::TakeVector(&dataVector); } // Write the PDF file to disk. static void SavePdfFile(scoped_refptr data, const base::FilePath &path, QtWebEngineCore::PrintViewManagerQt::PrintToPDFFileCallback saveCallback) { DCHECK_GT(data->size(), 0U); printing::MetafileSkia metafile; metafile.InitFromData(base::as_bytes(base::make_span(data->front(), data->size()))); base::File file(path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); bool success = file.IsValid() && metafile.SaveTo(&file); base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(saveCallback), success)); } static base::DictionaryValue *createPrintSettings() { base::DictionaryValue *printSettings = new base::DictionaryValue(); // TO DO: Check if we can use the request ID from Qt here somehow. static int internalRequestId = 0; printSettings->SetBoolean(printing::kIsFirstRequest, internalRequestId++ == 0); printSettings->SetInteger(printing::kPreviewRequestID, internalRequestId); // The following are standard settings that Chromium expects to be set. printSettings->SetInteger(printing::kSettingPrinterType, static_cast(printing::mojom::PrinterType::kPdf)); printSettings->SetInteger(printing::kSettingDpiHorizontal, printing::kPointsPerInch); printSettings->SetInteger(printing::kSettingDpiVertical, printing::kPointsPerInch); printSettings->SetInteger(printing::kSettingDuplexMode, static_cast(printing::mojom::DuplexMode::kSimplex)); printSettings->SetInteger(printing::kSettingCopies, 1); printSettings->SetInteger(printing::kSettingPagesPerSheet, 1); printSettings->SetBoolean(printing::kSettingCollate, false); // printSettings->SetBoolean(printing::kSettingGenerateDraftData, false); printSettings->SetBoolean(printing::kSettingPreviewModifiable, false); printSettings->SetKey(printing::kSettingShouldPrintSelectionOnly, base::Value(false)); printSettings->SetKey(printing::kSettingShouldPrintBackgrounds, base::Value(true)); printSettings->SetKey(printing::kSettingHeaderFooterEnabled, base::Value(false)); printSettings->SetKey(printing::kSettingRasterizePdf, base::Value(false)); printSettings->SetInteger(printing::kSettingScaleFactor, 100); printSettings->SetString(printing::kSettingDeviceName, ""); printSettings->SetInteger(printing::kPreviewUIID, 12345678); return printSettings; } static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayout &pageLayout, bool useCustomMargins) { base::DictionaryValue *printSettings = createPrintSettings(); QRectF pageSizeInMillimeter; if (useCustomMargins) { // Apply page margins when printing to PDF pageSizeInMillimeter = pageLayout.pageSize().rect(QPageSize::Millimeter); QMargins pageMarginsInPoints = pageLayout.marginsPoints(); std::unique_ptr marginsDict(new base::DictionaryValue); marginsDict->SetInteger(printing::kSettingMarginTop, pageMarginsInPoints.top()); marginsDict->SetInteger(printing::kSettingMarginBottom, pageMarginsInPoints.bottom()); marginsDict->SetInteger(printing::kSettingMarginLeft, pageMarginsInPoints.left()); marginsDict->SetInteger(printing::kSettingMarginRight, pageMarginsInPoints.right()); printSettings->Set(printing::kSettingMarginsCustom, std::move(marginsDict)); printSettings->SetInteger(printing::kSettingMarginsType, (int)printing::mojom::MarginType::kCustomMargins); // pageSizeInMillimeter is in portrait orientation. Transpose it if necessary. printSettings->SetBoolean(printing::kSettingLandscape, pageLayout.orientation() == QPageLayout::Landscape); } else { // QPrinter will handle margins pageSizeInMillimeter = pageLayout.paintRect(QPageLayout::Millimeter); printSettings->SetInteger(printing::kSettingMarginsType, (int)printing::mojom::MarginType::kNoMargins); // pageSizeInMillimeter already contains the orientation. printSettings->SetBoolean(printing::kSettingLandscape, false); } //Set page size attributes, chromium expects these in micrometers std::unique_ptr sizeDict(new base::DictionaryValue); sizeDict->SetInteger(printing::kSettingMediaSizeWidthMicrons, pageSizeInMillimeter.width() * kMicronsToMillimeter); sizeDict->SetInteger(printing::kSettingMediaSizeHeightMicrons, pageSizeInMillimeter.height() * kMicronsToMillimeter); printSettings->Set(printing::kSettingMediaSize, std::move(sizeDict)); return printSettings; } static base::ListValue *createPageRangeSettings(const QList &ranges) { base::ListValue *pageRangeArray = new base::ListValue; for (int i = 0; i < ranges.count(); i++) { std::unique_ptr pageRange(new base::DictionaryValue); pageRange->SetInteger(printing::kSettingPageRangeFrom, ranges.at(i).from); pageRange->SetInteger(printing::kSettingPageRangeTo, ranges.at(i).to); pageRangeArray->Append(std::move(pageRange)); } return pageRangeArray; } } // namespace namespace QtWebEngineCore { PrintViewManagerQt::~PrintViewManagerQt() { } void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayout, const QPageRanges &pageRanges, bool printInColor, const QString &filePath, PrintToPDFFileCallback callback) { if (callback.is_null()) return; if (m_printSettings || !filePath.length()) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(callback), false)); return; } m_pdfOutputPath = toFilePath(filePath); m_pdfSaveCallback = std::move(callback); if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(m_pdfSaveCallback), false)); resetPdfState(); } } void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, const QPageRanges &pageRanges, bool printInColor, bool useCustomMargins, PrintToPDFCallback callback) { if (callback.is_null()) return; // If there already is a pending print in progress, don't try starting another one. if (m_printSettings) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(callback), QSharedPointer())); return; } m_pdfPrintCallback = std::move(callback); if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor, useCustomMargins)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer())); resetPdfState(); } } bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, const QPageRanges &pageRanges, const bool printInColor, const bool useCustomMargins) { if (!pageLayout.isValid()) return false; m_printSettings.reset(createPrintSettingsFromQPageLayout(pageLayout, useCustomMargins)); m_printSettings->SetBoolean(printing::kSettingShouldPrintBackgrounds, web_contents()->GetOrCreateWebPreferences().should_print_backgrounds); m_printSettings->SetInteger(printing::kSettingColor, int(printInColor ? printing::mojom::ColorModel::kColor : printing::mojom::ColorModel::kGrayscale)); if (!pageRanges.isEmpty()) m_printSettings->Set(printing::kSettingPageRange, std::unique_ptr(createPageRangeSettings(pageRanges.toRangeList()))); if (web_contents()->IsCrashed()) return false; content::RenderFrameHost* rfh = web_contents()->GetMainFrame(); GetPrintRenderFrame(rfh)->InitiatePrintPreview(mojo::PendingAssociatedRemote(), false); DCHECK(!m_printPreviewRfh); m_printPreviewRfh = rfh; return true; } PrintViewManagerQt::PrintViewManagerQt(content::WebContents *contents) : PrintViewManagerBaseQt(contents) , m_printPreviewRfh(nullptr) { } // static void PrintViewManagerQt::BindPrintManagerHost(mojo::PendingAssociatedReceiver receiver, content::RenderFrameHost *rfh) { auto *web_contents = content::WebContents::FromRenderFrameHost(rfh); if (!web_contents) return; auto *print_manager = PrintViewManagerQt::FromWebContents(web_contents); if (!print_manager) return; print_manager->BindReceiver(std::move(receiver), rfh); } void PrintViewManagerQt::resetPdfState() { m_pdfOutputPath.clear(); m_pdfPrintCallback.Reset(); m_pdfSaveCallback.Reset(); m_printSettings.reset(); } void PrintViewManagerQt::PrintPreviewDone() { if (IsPrintRenderFrameConnected(m_printPreviewRfh)) GetPrintRenderFrame(m_printPreviewRfh)->OnPrintPreviewDialogClosed(); m_printPreviewRfh = nullptr; } // content::WebContentsObserver implementation. // Cancels the print job. void PrintViewManagerQt::NavigationStopped() { if (!m_pdfPrintCallback.is_null()) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer())); } resetPdfState(); PrintViewManagerBaseQt::NavigationStopped(); } void PrintViewManagerQt::RenderProcessGone(base::TerminationStatus status) { PrintViewManagerBaseQt::RenderProcessGone(status); if (!m_pdfPrintCallback.is_null()) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer())); } resetPdfState(); } void PrintViewManagerQt::RenderFrameDeleted(content::RenderFrameHost *render_frame_host) { if (render_frame_host == m_printPreviewRfh) PrintPreviewDone(); PrintViewManagerBaseQt::RenderFrameDeleted(render_frame_host); } // mojom::PrintManagerHost: void PrintViewManagerQt::SetupScriptedPrintPreview(SetupScriptedPrintPreviewCallback callback) { // ignore the scripted print std::move(callback).Run(); content::WebContentsView *view = static_cast(web_contents())->GetView(); WebContentsAdapterClient *client = WebContentsViewQt::from(view)->client(); content::RenderFrameHost *rfh = print_manager_host_receivers_.GetCurrentTargetFrame(); if (!client) return; // close preview if (rfh) GetPrintRenderFrame(rfh)->OnPrintPreviewDialogClosed(); client->printRequested(); } void PrintViewManagerQt::ShowScriptedPrintPreview(bool /*source_is_modifiable*/) { // ignore for now } void PrintViewManagerQt::RequestPrintPreview(printing::mojom::RequestPrintPreviewParamsPtr /*params*/) { mojo::AssociatedRemote printRenderFrame; m_printPreviewRfh->GetRemoteAssociatedInterfaces()->GetInterface(&printRenderFrame); printRenderFrame->PrintPreview(m_printSettings->Clone()); PrintPreviewDone(); } void PrintViewManagerQt::CheckForCancel(int32_t preview_ui_id, int32_t request_id, CheckForCancelCallback callback) { Q_UNUSED(preview_ui_id); Q_UNUSED(request_id); std::move(callback).Run(false); } void PrintViewManagerQt::SetAccessibilityTree(int32_t, const ui::AXTreeUpdate &) { // FIXME! } void PrintViewManagerQt::MetafileReadyForPrinting(printing::mojom::DidPreviewDocumentParamsPtr params, int32_t preview_ui_id) { Q_UNUSED(preview_ui_id); StopWorker(params->document_cookie); // Create local copies so we can reset the state and take a new pdf print job. PrintToPDFCallback pdf_print_callback = std::move(m_pdfPrintCallback); PrintToPDFFileCallback pdf_save_callback = std::move(m_pdfSaveCallback); base::FilePath pdfOutputPath = m_pdfOutputPath; resetPdfState(); if (!pdf_print_callback.is_null()) { QSharedPointer data_array = GetStdVectorFromHandle(params->content->metafile_data_region); base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(std::move(pdf_print_callback), data_array)); } else { scoped_refptr data_bytes = GetBytesFromHandle(params->content->metafile_data_region); base::ThreadPool::PostTask(FROM_HERE, { base::MayBlock() }, base::BindOnce(&SavePdfFile, data_bytes, pdfOutputPath, std::move(pdf_save_callback))); } } WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintViewManagerQt) } // namespace QtWebEngineCore