diff options
Diffstat (limited to 'src/core/printing')
-rw-r--r-- | src/core/printing/pdf_document_helper_client_qt.cpp | 33 | ||||
-rw-r--r-- | src/core/printing/pdf_document_helper_client_qt.h | 27 | ||||
-rw-r--r-- | src/core/printing/pdf_stream_delegate_qt.cpp | 99 | ||||
-rw-r--r-- | src/core/printing/pdf_stream_delegate_qt.h | 23 | ||||
-rw-r--r-- | src/core/printing/pdfium_document_wrapper_qt.cpp | 40 | ||||
-rw-r--r-- | src/core/printing/pdfium_document_wrapper_qt.h | 42 | ||||
-rw-r--r-- | src/core/printing/print_view_manager_base_qt.cpp | 465 | ||||
-rw-r--r-- | src/core/printing/print_view_manager_base_qt.h | 178 | ||||
-rw-r--r-- | src/core/printing/print_view_manager_qt.cpp | 443 | ||||
-rw-r--r-- | src/core/printing/print_view_manager_qt.h | 130 | ||||
-rw-r--r-- | src/core/printing/printer_worker.cpp | 87 | ||||
-rw-r--r-- | src/core/printing/printer_worker.h | 58 | ||||
-rw-r--r-- | src/core/printing/printing_message_filter_qt.cpp | 234 | ||||
-rw-r--r-- | src/core/printing/printing_message_filter_qt.h | 126 |
14 files changed, 897 insertions, 1088 deletions
diff --git a/src/core/printing/pdf_document_helper_client_qt.cpp b/src/core/printing/pdf_document_helper_client_qt.cpp new file mode 100644 index 000000000..be1cc2e4c --- /dev/null +++ b/src/core/printing/pdf_document_helper_client_qt.cpp @@ -0,0 +1,33 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/ui/pdf/chrome_pdf_document_helper_client.cc: + +#include "pdf_document_helper_client_qt.h" + +#include "content/public/browser/render_process_host.h" +#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" +#include "extensions/common/constants.h" + +#include "pdf_util_qt.h" + +PDFDocumentHelperClientQt::PDFDocumentHelperClientQt() = default; +PDFDocumentHelperClientQt::~PDFDocumentHelperClientQt() = default; + +content::RenderFrameHost *PDFDocumentHelperClientQt::FindPdfFrame(content::WebContents *contents) +{ + content::RenderFrameHost *main_frame = contents->GetPrimaryMainFrame(); + content::RenderFrameHost *pdf_frame = QtWebEngineCore::FindPdfChildFrame(main_frame); + return pdf_frame ? pdf_frame : main_frame; +} + +void PDFDocumentHelperClientQt::SetPluginCanSave(content::RenderFrameHost *render_frame_host, bool can_save) +{ + auto *guest_view = extensions::MimeHandlerViewGuest::FromRenderFrameHost(render_frame_host); + if (guest_view) + guest_view->SetPluginCanSave(can_save); +} + +void PDFDocumentHelperClientQt::UpdateContentRestrictions(content::RenderFrameHost *, int) +{ +} diff --git a/src/core/printing/pdf_document_helper_client_qt.h b/src/core/printing/pdf_document_helper_client_qt.h new file mode 100644 index 000000000..af58c7794 --- /dev/null +++ b/src/core/printing/pdf_document_helper_client_qt.h @@ -0,0 +1,27 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef PDF_DOCUMENT_HELPER_CLIENT_QT_H +#define PDF_DOCUMENT_HELPER_CLIENT_QT_H + +#include "components/pdf/browser/pdf_document_helper_client.h" + +// based on chrome/browser/ui/pdf/chrome_pdf_document_helper_client.h: +class PDFDocumentHelperClientQt : public pdf::PDFDocumentHelperClient // FIXME: rename +{ +public: + PDFDocumentHelperClientQt(); + PDFDocumentHelperClientQt(const PDFDocumentHelperClientQt&) = delete; + PDFDocumentHelperClientQt& operator=(const PDFDocumentHelperClientQt&) = delete; + ~PDFDocumentHelperClientQt() override; + +private: + // pdf::PDFDocumentHelperClient: + content::RenderFrameHost* FindPdfFrame(content::WebContents *contents) override; + void OnPDFHasUnsupportedFeature(content::WebContents *contents) override {} + void OnSaveURL(content::WebContents *contents) override {} + void SetPluginCanSave(content::RenderFrameHost *render_frame_host, bool can_save) override; + void UpdateContentRestrictions(content::RenderFrameHost *, int) override; +}; + +#endif // PDF_DOCUMENT_HELPER_CLIENT_QT_H diff --git a/src/core/printing/pdf_stream_delegate_qt.cpp b/src/core/printing/pdf_stream_delegate_qt.cpp new file mode 100644 index 000000000..8677c82bd --- /dev/null +++ b/src/core/printing/pdf_stream_delegate_qt.cpp @@ -0,0 +1,99 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// based on chrome/browser/pdf/chrome_pdf_stream_delegate.cc: + +#include "pdf_stream_delegate_qt.h" + +#include "base/no_destructor.h" +#include "chrome/grit/pdf_resources.h" +#include "content/public/browser/document_user_data.h" +#include "content/public/browser/navigation_handle.h" +#include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h" +#include "extensions/common/constants.h" +#include "ui/base/resource/resource_bundle.h" + +// Associates a `pdf::PdfStreamDelegate::StreamInfo` with the PDF extension's +// or Print Preview's `blink::Document`. +// `ChromePdfStreamDelegate::MapToOriginalUrl()` initializes this in +// `PdfNavigationThrottle`, and then `ChromePdfStreamDelegate::GetStreamInfo()` +// returns the stashed result to `PdfURLLoaderRequestInterceptor`. +class StreamInfoHelper : public content::DocumentUserData<StreamInfoHelper> +{ +public: + absl::optional<pdf::PdfStreamDelegate::StreamInfo> TakeStreamInfo() + { return std::move(stream_info_); } + +private: + friend class content::DocumentUserData<StreamInfoHelper>; + DOCUMENT_USER_DATA_KEY_DECL(); + + StreamInfoHelper(content::RenderFrameHost* embedder_frame, + pdf::PdfStreamDelegate::StreamInfo stream_info) + : content::DocumentUserData<StreamInfoHelper>(embedder_frame), + stream_info_(std::move(stream_info)) {} + + absl::optional<pdf::PdfStreamDelegate::StreamInfo> stream_info_; +}; + +DOCUMENT_USER_DATA_KEY_IMPL(StreamInfoHelper); + +PdfStreamDelegateQt::PdfStreamDelegateQt() = default; +PdfStreamDelegateQt::~PdfStreamDelegateQt() = default; + +absl::optional<GURL> PdfStreamDelegateQt::MapToOriginalUrl(content::NavigationHandle &navigation_handle) +{ + content::RenderFrameHost *embedder_frame = navigation_handle.GetParentFrame(); + StreamInfoHelper *helper = StreamInfoHelper::GetForCurrentDocument(embedder_frame); + if (helper) { + // PDF viewer and Print Preview only do this once per WebContents. + return absl::nullopt; + } + + GURL original_url; + StreamInfo info; + + content::WebContents* contents = navigation_handle.GetWebContents(); + extensions::MimeHandlerViewGuest *guest = + extensions::MimeHandlerViewGuest::FromWebContents(contents); + if (guest) { + base::WeakPtr<extensions::StreamContainer> stream = guest->GetStreamWeakPtr(); + if (!stream || stream->extension_id() != extension_misc::kPdfExtensionId || + stream->stream_url() != navigation_handle.GetURL() || + !stream->pdf_plugin_attributes()) { + return absl::nullopt; + } + + original_url = stream->original_url(); + info.background_color = base::checked_cast<SkColor>(stream->pdf_plugin_attributes()->background_color); + info.full_frame = !stream->embedded(); + info.allow_javascript = stream->pdf_plugin_attributes()->allow_javascript; + } else { + return absl::nullopt; + } + + static const base::NoDestructor<std::string> injected_script( + ui::ResourceBundle::GetSharedInstance().LoadDataResourceString( + IDR_PDF_PDF_INTERNAL_PLUGIN_WRAPPER_ROLLUP_JS)); + + info.stream_url = navigation_handle.GetURL(); + info.original_url = original_url; + info.injected_script = injected_script.get(); + StreamInfoHelper::CreateForCurrentDocument(embedder_frame, std::move(info)); + return original_url; +} + +absl::optional<pdf::PdfStreamDelegate::StreamInfo> +PdfStreamDelegateQt::GetStreamInfo(content::RenderFrameHost* embedder_frame) +{ + if (!embedder_frame) + return absl::nullopt; + + StreamInfoHelper *helper = StreamInfoHelper::GetForCurrentDocument(embedder_frame); + if (!helper) + return absl::nullopt; + + // Only the call immediately following `MapToOriginalUrl()` requires a valid + // `StreamInfo`; subsequent calls should just get nothing. + return helper->TakeStreamInfo(); +} diff --git a/src/core/printing/pdf_stream_delegate_qt.h b/src/core/printing/pdf_stream_delegate_qt.h new file mode 100644 index 000000000..fd279af72 --- /dev/null +++ b/src/core/printing/pdf_stream_delegate_qt.h @@ -0,0 +1,23 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef PDF_STREAM_DELEGATE_QT_H +#define PDF_STREAM_DELEGATE_QT_H + +#include "components/pdf/browser/pdf_stream_delegate.h" + +// based on chrome/browser/pdf/chrome_pdf_stream_delegate.h: +class PdfStreamDelegateQt : public pdf::PdfStreamDelegate +{ +public: + PdfStreamDelegateQt(); + PdfStreamDelegateQt(const PdfStreamDelegateQt &) = delete; + PdfStreamDelegateQt operator=(const PdfStreamDelegateQt &) = delete; + ~PdfStreamDelegateQt() override; + + // pdf::PdfStreamDelegate: + absl::optional<GURL> MapToOriginalUrl(content::NavigationHandle &navigation_handle) override; + absl::optional<StreamInfo> GetStreamInfo(content::RenderFrameHost *embedder_frame) override; +}; + +#endif // PDF_STREAM_DELEGATE_QT_H diff --git a/src/core/printing/pdfium_document_wrapper_qt.cpp b/src/core/printing/pdfium_document_wrapper_qt.cpp index dae6ec44b..56588620d 100644 --- a/src/core/printing/pdfium_document_wrapper_qt.cpp +++ b/src/core/printing/pdfium_document_wrapper_qt.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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$ -** -****************************************************************************/ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "pdfium_document_wrapper_qt.h" #include <QtCore/qhash.h> diff --git a/src/core/printing/pdfium_document_wrapper_qt.h b/src/core/printing/pdfium_document_wrapper_qt.h index e0778c32b..feb34e36a 100644 --- a/src/core/printing/pdfium_document_wrapper_qt.h +++ b/src/core/printing/pdfium_document_wrapper_qt.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2017 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$ -** -****************************************************************************/ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // // W A R N I N G @@ -57,7 +21,7 @@ namespace QtWebEngineCore { -class Q_WEBENGINECORE_PRIVATE_EXPORT PdfiumDocumentWrapperQt +class Q_WEBENGINECORE_EXPORT PdfiumDocumentWrapperQt { public: PdfiumDocumentWrapperQt(const void *pdfData, size_t size, const char *password = nullptr); diff --git a/src/core/printing/print_view_manager_base_qt.cpp b/src/core/printing/print_view_manager_base_qt.cpp index 34c86e506..a691bc2b6 100644 --- a/src/core/printing/print_view_manager_base_qt.cpp +++ b/src/core/printing/print_view_manager_base_qt.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // This is based on chrome/browser/printing/print_view_manager_base.cc: // Copyright 2013 The Chromium Authors. All rights reserved. @@ -48,21 +12,20 @@ #include "web_engine_context.h" #include "base/memory/ref_counted_memory.h" -#include "base/message_loop/message_loop.h" -#include "base/message_loop/message_loop_current.h" #include "base/run_loop.h" -#include "base/single_thread_task_runner.h" -#include "base/task/post_task.h" +#include "base/task/current_thread.h" +#include "base/task/thread_pool.h" #include "base/timer/timer.h" #include "base/values.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" -#include "components/printing/common/print_messages.h" +#include "components/printing/browser/print_manager_utils.h" +#include "components/printing/common/print.mojom.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 "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_types.h" @@ -72,16 +35,90 @@ namespace QtWebEngineCore { +namespace { + +// Runs |callback| with |params| to reply to +// mojom::PrintManagerHost::GetDefaultPrintSettings. +void GetDefaultPrintSettingsReply(printing::mojom::PrintManagerHost::GetDefaultPrintSettingsCallback callback, + printing::mojom::PrintParamsPtr params) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + std::move(callback).Run(std::move(params)); +} + +void OnDidGetDefaultPrintSettings(scoped_refptr<printing::PrintQueriesQueue> queue, + std::unique_ptr<printing::PrinterQuery> printer_query, + printing::mojom::PrintManagerHost::GetDefaultPrintSettingsCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + printing::mojom::PrintParamsPtr params = printing::mojom::PrintParams::New(); + if (printer_query && printer_query->last_status() == printing::mojom::ResultCode::kSuccess) { + RenderParamsFromPrintSettings(printer_query->settings(), params.get()); + params->document_cookie = printer_query->cookie(); + } + + GetDefaultPrintSettingsReply(std::move(callback), std::move(params)); + + // If printing was enabled. + if (printer_query) { + // If user hasn't cancelled. + if (printer_query->cookie() && printer_query->settings().dpi()) { + queue->QueuePrinterQuery(std::move(printer_query)); + } + } +} + +void OnDidUpdatePrintSettings(scoped_refptr<printing::PrintQueriesQueue> queue, + std::unique_ptr<printing::PrinterQuery> printer_query, + printing::mojom::PrintManagerHost::UpdatePrintSettingsCallback callback, + int process_id, int routing_id) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + DCHECK(printer_query); + auto params = printing::mojom::PrintPagesParams::New(); + params->params = printing::mojom::PrintParams::New(); + if (printer_query->last_status() == printing::mojom::ResultCode::kSuccess) { + RenderParamsFromPrintSettings(printer_query->settings(), params->params.get()); + params->params->document_cookie = printer_query->cookie(); + params->pages = printer_query->settings().ranges(); + } + + std::move(callback).Run(std::move(params)); + + if (printer_query->cookie() && printer_query->settings().dpi()) { + queue->QueuePrinterQuery(std::move(printer_query)); + } +} + +void OnDidScriptedPrint(scoped_refptr<printing::PrintQueriesQueue> queue, + std::unique_ptr<printing::PrinterQuery> printer_query, + printing::mojom::PrintManagerHost::ScriptedPrintCallback callback) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + auto params = printing::mojom::PrintPagesParams::New(); + params->params = printing::mojom::PrintParams::New(); + if (printer_query->last_status() == printing::mojom::ResultCode::kSuccess && printer_query->settings().dpi()) { + RenderParamsFromPrintSettings(printer_query->settings(), params->params.get()); + params->params->document_cookie = printer_query->cookie(); + params->pages = printer_query->settings().ranges(); + } + bool has_valid_cookie = params->params->document_cookie; + bool has_dpi = !params->params->dpi.IsEmpty(); + std::move(callback).Run(std::move(params)); + + if (has_dpi && has_valid_cookie) { + queue->QueuePrinterQuery(std::move(printer_query)); + } +} + +} // namespace + PrintViewManagerBaseQt::PrintViewManagerBaseQt(content::WebContents *contents) : printing::PrintManager(contents) - , m_isInsideInnerMessageLoop(false) + , m_printingRFH(nullptr) , m_didPrintingSucceed(false) , m_printerQueriesQueue(WebEngineContext::current()->getPrintJobManager()->queue()) - , m_printingRFH(nullptr) { - // FIXME: Check if this needs to be executed async: - // TODO: Add isEnabled to profile - PrintViewManagerBaseQt::UpdatePrintingEnabled(); } PrintViewManagerBaseQt::~PrintViewManagerBaseQt() @@ -96,16 +133,23 @@ void PrintViewManagerBaseQt::SetPrintingRFH(content::RenderFrameHost *rfh) m_printingRFH = rfh; } -void PrintViewManagerBaseQt::UpdatePrintingEnabled() -{ +void PrintViewManagerBaseQt::ScriptedPrintReply(ScriptedPrintCallback callback, + int process_id, + printing::mojom::PrintPagesParamsPtr params) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - bool enabled = false; -#if QT_CONFIG(webengine_printing_and_pdf) - enabled = true; + +#if BUILDFLAG(ENABLE_OOP_PRINTING) + // Finished getting all settings (defaults and from user), no further need + // to be registered as a system print client. + UnregisterSystemPrintClient(); #endif - web_contents()->ForEachFrame( - base::Bind(&PrintViewManagerBaseQt::SendPrintingEnabled, - base::Unretained(this), enabled)); + if (!content::RenderProcessHost::FromID(process_id)) { + // Early return if the renderer is not alive. + return; + } + +// set_cookie(params->params->document_cookie); + std::move(callback).Run(std::move(params)); } void PrintViewManagerBaseQt::NavigationStopped() @@ -114,53 +158,56 @@ void PrintViewManagerBaseQt::NavigationStopped() TerminatePrintJob(true); } -base::string16 PrintViewManagerBaseQt::RenderSourceName() +std::u16string PrintViewManagerBaseQt::RenderSourceName() { return toString16(QLatin1String("")); } -void PrintViewManagerBaseQt::PrintDocument(printing::PrintedDocument *document, - const scoped_refptr<base::RefCountedMemory> &print_data, +void PrintViewManagerBaseQt::PrintDocument(scoped_refptr<base::RefCountedMemory> print_data, const gfx::Size &page_size, const gfx::Rect &content_area, const gfx::Point &offsets) { std::unique_ptr<printing::MetafileSkia> metafile = std::make_unique<printing::MetafileSkia>(); - CHECK(metafile->InitFromData(print_data->front(), print_data->size())); + CHECK(metafile->InitFromData(*print_data)); // Update the rendered document. It will send notifications to the listener. - document->SetDocument(std::move(metafile), page_size, content_area); + printing::PrintedDocument* document = m_printJob->document(); + document->SetDocument(std::move(metafile)); ShouldQuitFromInnerMessageLoop(); } -printing::PrintedDocument *PrintViewManagerBaseQt::GetDocument(int cookie) +void PrintViewManagerBaseQt::DidGetPrintedPagesCount(int32_t cookie, uint32_t number_pages) +{ + PrintManager::DidGetPrintedPagesCount(cookie, number_pages); + OpportunisticallyCreatePrintJob(cookie); +} + +bool PrintViewManagerBaseQt::PrintJobHasDocument(int cookie) { if (!OpportunisticallyCreatePrintJob(cookie)) - return nullptr; + return false; + // These checks may fail since we are completely asynchronous. Old spurious + // messages can be received if one of the processes is overloaded. printing::PrintedDocument* document = m_printJob->document(); - if (!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 nullptr; - } - return document; + return document && document->cookie() == cookie; } -// IPC handlers -void PrintViewManagerBaseQt::OnDidPrintDocument(content::RenderFrameHost* /*render_frame_host*/, - const PrintHostMsg_DidPrintDocument_Params ¶ms, - std::unique_ptr<DelayedFrameDispatchHelper> helper) +void PrintViewManagerBaseQt::DidPrintDocument(printing::mojom::DidPrintDocumentParamsPtr params, + DidPrintDocumentCallback callback) { - printing::PrintedDocument *document = GetDocument(params.document_cookie); - if (!document) + if (!PrintJobHasDocument(params->document_cookie)) { + std::move(callback).Run(false); return; + } - const PrintHostMsg_DidPrintContent_Params &content = params.content; + const printing::mojom::DidPrintContentParams &content = *params->content; if (!content.metafile_data_region.IsValid()) { NOTREACHED() << "invalid memory handle"; web_contents()->Stop(); + std::move(callback).Run(false); return; } @@ -168,41 +215,91 @@ void PrintViewManagerBaseQt::OnDidPrintDocument(content::RenderFrameHost* /*rend if (!data) { NOTREACHED() << "couldn't map"; web_contents()->Stop(); + std::move(callback).Run(false); return; } - PrintDocument(document, data, params.page_size, params.content_area, - params.physical_offsets); - if (helper) - helper->SendCompleted(); + PrintDocument(data, params->page_size, params->content_area, + params->physical_offsets); + std::move(callback).Run(true); } -void PrintViewManagerBaseQt::OnGetDefaultPrintSettings(content::RenderFrameHost *render_frame_host, - IPC::Message *reply_msg) +void PrintViewManagerBaseQt::GetDefaultPrintSettings(GetDefaultPrintSettingsCallback callback) { - NOTREACHED() << "should be handled by printing::PrintingMessageFilter"; + content::RenderFrameHost *render_frame_host = + print_manager_host_receivers_.GetCurrentTargetFrame(); + content::RenderProcessHost *render_process_host = + render_frame_host->GetProcess(); + + auto callback_wrapper = + base::BindOnce(&GetDefaultPrintSettingsReply, std::move(callback)); + std::unique_ptr<printing::PrinterQuery> printer_query = m_printerQueriesQueue->PopPrinterQuery(0); + if (!printer_query) + printer_query = m_printerQueriesQueue->CreatePrinterQuery(render_frame_host->GetGlobalId()); + + // Loads default settings. This is asynchronous, only the mojo message sender + // will hang until the settings are retrieved. + auto *printer_query_ptr = printer_query.get(); + printer_query_ptr->GetDefaultSettings( + base::BindOnce(&OnDidGetDefaultPrintSettings, m_printerQueriesQueue, + std::move(printer_query), std::move(callback_wrapper)), + false, + !render_process_host->IsPdf()); } -void PrintViewManagerBaseQt::OnScriptedPrint(content::RenderFrameHost *render_frame_host, - const PrintHostMsg_ScriptedPrint_Params ¶ms, - IPC::Message *reply_msg) +void PrintViewManagerBaseQt::PrintingFailed(int32_t cookie, printing::mojom::PrintFailureReason reason) { - NOTREACHED() << "should be handled by printing::PrintingMessageFilter"; -} + // Note: Not redundant with cookie checks in the same method in other parts of + // the class hierarchy. + if (!IsValidCookie(cookie)) + return; -void PrintViewManagerBaseQt::OnShowInvalidPrinterSettingsError() + PrintManager::PrintingFailed(cookie, reason); + + ReleasePrinterQuery(); +} +void PrintViewManagerBaseQt::IsPrintingEnabled(IsPrintingEnabledCallback callback) { + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + bool enabled = false; +#if QT_CONFIG(webengine_printing_and_pdf) + enabled = true; +#endif + std::move(callback).Run(enabled); } -void PrintViewManagerBaseQt::DidStartLoading() +void PrintViewManagerBaseQt::ScriptedPrint(printing::mojom::ScriptedPrintParamsPtr params, + printing::mojom::PrintManagerHost::ScriptedPrintCallback callback) { - UpdatePrintingEnabled(); + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + content::RenderFrameHost *render_frame_host = + print_manager_host_receivers_.GetCurrentTargetFrame(); + content::RenderProcessHost *render_process_host = + render_frame_host->GetProcess(); + + auto callback_wrapper = base::BindOnce( + &PrintViewManagerBaseQt::ScriptedPrintReply, weak_ptr_factory_.GetWeakPtr(), + std::move(callback), render_process_host->GetID()); + + std::unique_ptr<printing::PrinterQuery> printer_query = + m_printerQueriesQueue->PopPrinterQuery(params->cookie); + if (!printer_query) + printer_query = m_printerQueriesQueue->CreatePrinterQuery(render_frame_host->GetGlobalId()); + + auto *printer_query_ptr = printer_query.get(); + printer_query_ptr->GetSettingsFromUser( + params->expected_pages_count, params->has_selection, params->margin_type, + params->is_scripted, !render_process_host->IsPdf(), + base::BindOnce(&OnDidScriptedPrint, m_printerQueriesQueue, std::move(printer_query), std::move(callback_wrapper))); } -// Note: In PrintViewManagerQt we always initiate printing with PrintMsg_InitiatePrintPreview +// Note: In PrintViewManagerQt we always initiate printing with +// printing::mojom::PrintRenderFrame::InitiatePrintPreview() // so m_printingRFH is never set and used at the moment. void PrintViewManagerBaseQt::RenderFrameDeleted(content::RenderFrameHost *render_frame_host) { + PrintManager::RenderFrameDeleted(render_frame_host); + // Terminates or cancels the print job if one was pending. if (render_frame_host != m_printingRFH) return; @@ -216,7 +313,7 @@ void PrintViewManagerBaseQt::RenderFrameDeleted(content::RenderFrameHost *render return; scoped_refptr<printing::PrintedDocument> document(m_printJob->document()); - if (document.get()) { + if (document) { // 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. @@ -224,73 +321,22 @@ void PrintViewManagerBaseQt::RenderFrameDeleted(content::RenderFrameHost *render } } -bool PrintViewManagerBaseQt::OnMessageReceived(const IPC::Message& message, - content::RenderFrameHost* render_frame_host) +void PrintViewManagerBaseQt::OnDocDone(int /*job_id*/, printing::PrintedDocument * /*document*/) { - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBaseQt, message) - IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, - OnShowInvalidPrinterSettingsError); - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled || PrintManager::OnMessageReceived(message, render_frame_host); } -void PrintViewManagerBaseQt::Observe(int type, - const content::NotificationSource& /*source*/, - const content::NotificationDetails& details) +void PrintViewManagerBaseQt::OnJobDone() { - DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_EVENT, type); - OnNotifyPrintJobEvent(*content::Details<printing::JobEventDetails>(details).ptr()); + // 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. + m_didPrintingSucceed = true; + ReleasePrintJob(); } -void PrintViewManagerBaseQt::OnNotifyPrintJobEvent(const printing::JobEventDetails& event_details) +void PrintViewManagerBaseQt::OnFailed() { - switch (event_details.type()) { - case printing::JobEventDetails::FAILED: { - TerminatePrintJob(true); - - content::NotificationService::current()->Notify( - chrome::NOTIFICATION_PRINT_JOB_RELEASED, - content::Source<content::WebContents>(web_contents()), - content::NotificationService::NoDetails()); - break; - } - case printing::JobEventDetails::USER_INIT_DONE: - case printing::JobEventDetails::DEFAULT_INIT_DONE: - case printing::JobEventDetails::USER_INIT_CANCELED: { - NOTREACHED(); - break; - } - case printing::JobEventDetails::ALL_PAGES_REQUESTED: { - ShouldQuitFromInnerMessageLoop(); - break; - } - case printing::JobEventDetails::NEW_DOC: -#if defined(OS_WIN) - case printing::JobEventDetails::PAGE_DONE: -#endif - case printing::JobEventDetails::DOC_DONE: { - // Don't care about the actual printing process. - break; - } - case printing::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. - m_didPrintingSucceed = true; - ReleasePrintJob(); - - content::NotificationService::current()->Notify( - chrome::NOTIFICATION_PRINT_JOB_RELEASED, - content::Source<content::WebContents>(web_contents()), - content::NotificationService::NoDetails()); - break; - } - default: - NOTREACHED(); - break; - } + TerminatePrintJob(true); } // Requests the RenderView to render all the missing pages for the print job. @@ -309,8 +355,8 @@ bool PrintViewManagerBaseQt::RenderAllMissingPagesNow() // We can't print if there is no renderer. if (!web_contents() || - !web_contents()->GetRenderViewHost() || - !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { + !web_contents()->GetPrimaryMainFrame() || + !web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive()) { return false; } @@ -319,11 +365,9 @@ bool PrintViewManagerBaseQt::RenderAllMissingPagesNow() // 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 - // pages it needs. MessageLoop::current()->Quit() will be called as soon as - // print_job_->document()->IsComplete() is true on either ALL_PAGES_REQUESTED - // or in DidPrintPage(). The check is done in - // ShouldQuitFromInnerMessageLoop(). + // |quit_inner_loop_| will be called as soon as + // print_job_->document()->IsComplete() is true in DidPrintDocument(). The + // check is done in ShouldQuitFromInnerMessageLoop(). // BLOCKS until all the pages are received. (Need to enable recursive task) if (!RunInnerMessageLoop()) { // This function is always called from DisconnectFromCurrentPrintJob() so we @@ -333,36 +377,32 @@ bool PrintViewManagerBaseQt::RenderAllMissingPagesNow() return true; } -// Quits the current message loop if these conditions hold true: a document is -// loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This -// function is called in DidPrintPage() or on ALL_PAGES_REQUESTED -// notification. The inner message loop is created was created by -// RenderAllMissingPagesNow(). void PrintViewManagerBaseQt::ShouldQuitFromInnerMessageLoop() { // Look at the reason. DCHECK(m_printJob->document()); - if (m_printJob->document() && - m_printJob->document()->IsComplete() && - m_isInsideInnerMessageLoop) { - // We are in a message loop created by RenderAllMissingPagesNow. Quit from - // it. - base::RunLoop::QuitCurrentWhenIdleDeprecated(); - m_isInsideInnerMessageLoop = false; + if (m_printJob->document() && m_printJob->document()->IsComplete() && m_quitInnerLoop) { + // We are in a message loop created by RenderAllMissingPagesNow. Quit from + // it. + std::move(m_quitInnerLoop).Run(); } } bool PrintViewManagerBaseQt::CreateNewPrintJob(std::unique_ptr<printing::PrinterQuery> query) { - DCHECK(!m_isInsideInnerMessageLoop); + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + DCHECK(!m_quitInnerLoop); DCHECK(query); // Disconnect the current |m_printJob|. + auto weak_this = weak_ptr_factory_.GetWeakPtr(); DisconnectFromCurrentPrintJob(); + if (!weak_this) + return false; // We can't print if there is no renderer. - if (!web_contents()->GetRenderViewHost() || - !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { + if (!web_contents()->GetPrimaryMainFrame() || + !web_contents()->GetPrimaryMainFrame()->IsRenderFrameLive()) { return false; } @@ -370,10 +410,9 @@ bool PrintViewManagerBaseQt::CreateNewPrintJob(std::unique_ptr<printing::Printer // view and switch to it, initialize the printer and show the print dialog. DCHECK(!m_printJob.get()); - m_printJob = base::MakeRefCounted<printing::PrintJob>(); + m_printJob = base::MakeRefCounted<printing::PrintJob>(nullptr /*g_browser_process->print_job_manager()*/); m_printJob->Initialize(std::move(query), RenderSourceName(), number_pages_); - m_registrar.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, - content::Source<printing::PrintJob>(m_printJob.get())); + m_printJob->AddObserver(*this); m_didPrintingSucceed = false; return true; } @@ -405,9 +444,9 @@ void PrintViewManagerBaseQt::TerminatePrintJob(bool cancel) if (cancel) { // We don't need the metafile data anymore because the printing is canceled. m_printJob->Cancel(); - m_isInsideInnerMessageLoop = false; + m_quitInnerLoop.Reset(); } else { - DCHECK(!m_isInsideInnerMessageLoop); + DCHECK(!m_quitInnerLoop); DCHECK(!m_printJob->document() || m_printJob->document()->IsComplete()); // WebContents is either dying or navigating elsewhere. We need to render @@ -429,13 +468,14 @@ void PrintViewManagerBaseQt::ReleasePrintJob() if (rfh) GetPrintRenderFrame(rfh)->PrintingDone(m_didPrintingSucceed); - m_registrar.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, - content::Source<printing::PrintJob>(m_printJob.get())); + m_printJob->RemoveObserver(*this); + // Don't close the worker thread. m_printJob = nullptr; } -bool PrintViewManagerBaseQt::RunInnerMessageLoop() { +bool PrintViewManagerBaseQt::RunInnerMessageLoop() +{ // This value may actually be too low: // // - If we're looping because of printer settings initialization, the premise @@ -451,23 +491,18 @@ bool PrintViewManagerBaseQt::RunInnerMessageLoop() { base::OneShotTimer quit_timer; base::RunLoop run_loop; quit_timer.Start(FROM_HERE, - base::TimeDelta::FromMilliseconds(kPrinterSettingsTimeout), + base::Milliseconds(kPrinterSettingsTimeout), run_loop.QuitWhenIdleClosure()); - m_isInsideInnerMessageLoop = true; + m_quitInnerLoop = run_loop.QuitClosure(); - // Need to enable recursive task. - { - base::MessageLoopCurrent::ScopedNestableTaskAllower allow; - run_loop.Run(); - } + auto weak_this = weak_ptr_factory_.GetWeakPtr(); + run_loop.Run(); + if (!weak_this) + return false; - bool success = true; - if (m_isInsideInnerMessageLoop) { - // Ok we timed out. That's sad. - m_isInsideInnerMessageLoop = false; - success = false; - } + bool success = !m_quitInnerLoop; + m_quitInnerLoop.Reset(); return success; } @@ -515,12 +550,8 @@ void PrintViewManagerBaseQt::ReleasePrinterQuery() if (!printJobManager) return; - std::unique_ptr<printing::PrinterQuery> printerQuery; - printerQuery = m_printerQueriesQueue->PopPrinterQuery(cookie); - if (!printerQuery) - return; - base::PostTask(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&printing::PrinterQuery::StopWorker, std::move(printerQuery))); + std::unique_ptr<printing::PrinterQuery> printerQuery = + m_printerQueriesQueue->PopPrinterQuery(cookie); } // Originally from print_preview_message_handler.cc: @@ -528,17 +559,31 @@ void PrintViewManagerBaseQt::StopWorker(int documentCookie) { if (documentCookie <= 0) return; - std::unique_ptr<printing::PrinterQuery> printer_query = + std::unique_ptr<printing::PrinterQuery> printerQuery = m_printerQueriesQueue->PopPrinterQuery(documentCookie); - if (printer_query.get()) { - base::PostTask(FROM_HERE, {content::BrowserThread::IO}, - base::BindOnce(&printing::PrinterQuery::StopWorker, std::move(printer_query))); - } } -void PrintViewManagerBaseQt::SendPrintingEnabled(bool enabled, content::RenderFrameHost* rfh) +void PrintViewManagerBaseQt::UpdatePrintSettings(base::Value::Dict job_settings, + UpdatePrintSettingsCallback callback) { - GetPrintRenderFrame(rfh)->SetPrintingEnabled(enabled); + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + + if (!job_settings.FindInt(printing::kSettingPrinterType)) { + std::move(callback).Run(nullptr); + return; + } + + content::RenderFrameHost *render_frame_host = + print_manager_host_receivers_.GetCurrentTargetFrame(); + std::unique_ptr<printing::PrinterQuery> printer_query = + m_printerQueriesQueue->CreatePrinterQuery(content::GlobalRenderFrameHostId()); + + auto *printer_query_ptr = printer_query.get(); + printer_query_ptr->SetSettings( + std::move(job_settings), + base::BindOnce(&OnDidUpdatePrintSettings, + m_printerQueriesQueue, std::move(printer_query), std::move(callback), + render_frame_host->GetProcess()->GetID(), render_frame_host->GetRoutingID())); } } // namespace QtWebEngineCore diff --git a/src/core/printing/print_view_manager_base_qt.h b/src/core/printing/print_view_manager_base_qt.h index 1217e8c11..d4b5bfe82 100644 --- a/src/core/printing/print_view_manager_base_qt.h +++ b/src/core/printing/print_view_manager_base_qt.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be @@ -45,134 +9,138 @@ #define PRINT_VIEW_MANAGER_BASE_QT_H #include "base/memory/ref_counted_memory.h" -#include "base/strings/string16.h" +#include "chrome/browser/printing/print_job.h" #include "components/prefs/pref_member.h" #include "components/printing/browser/print_manager.h" +#include "components/printing/common/print.mojom-forward.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" -struct PrintHostMsg_DidPrintDocument_Params; - namespace base { class RefCountedBytes; } namespace content { class RenderFrameHost; -class RenderViewHost; } namespace printing { -class JobEventDetails; -class MetafilePlayer; -class PrintJob; -class PrintJobWorkerOwner; class PrintQueriesQueue; -class PrintedDocument; class PrinterQuery; } namespace QtWebEngineCore { -class PrintViewManagerBaseQt : public content::NotificationObserver - , public printing::PrintManager +class PrintViewManagerBaseQt : public printing::PrintManager + , public printing::PrintJob::Observer { public: ~PrintViewManagerBaseQt() override; - // Whether printing is enabled or not. - void UpdatePrintingEnabled(); + std::u16string RenderSourceName(); - virtual base::string16 RenderSourceName(); + // mojom::PrintManagerHost: + void DidGetPrintedPagesCount(int32_t cookie, uint32_t number_pages) override; + void DidPrintDocument(printing::mojom::DidPrintDocumentParamsPtr params, + DidPrintDocumentCallback callback) override; + void GetDefaultPrintSettings(GetDefaultPrintSettingsCallback callback) override; + void UpdatePrintSettings(base::Value::Dict, UpdatePrintSettingsCallback) override; + void IsPrintingEnabled(IsPrintingEnabledCallback callback) override; + void ScriptedPrint(printing::mojom::ScriptedPrintParamsPtr, + printing::mojom::PrintManagerHost::ScriptedPrintCallback) override; + void PrintingFailed(int32_t cookie, + printing::mojom::PrintFailureReason reason) override; protected: explicit PrintViewManagerBaseQt(content::WebContents*); void SetPrintingRFH(content::RenderFrameHost* rfh); - // content::WebContentsObserver implementation. // Cancels the print job. void NavigationStopped() override; // content::WebContentsObserver implementation. void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; - bool OnMessageReceived(const IPC::Message& message, - content::RenderFrameHost* render_frame_host) override; - - // printing::PrintManager implementation: - void OnDidPrintDocument(content::RenderFrameHost *render_frame_host, - const PrintHostMsg_DidPrintDocument_Params ¶ms, - std::unique_ptr<DelayedFrameDispatchHelper> helper) override; - void OnGetDefaultPrintSettings(content::RenderFrameHost* render_frame_host, - IPC::Message* reply_msg) override; - void OnScriptedPrint(content::RenderFrameHost* render_frame_host, - const PrintHostMsg_ScriptedPrint_Params& params, - IPC::Message* reply_msg) override; - - void OnShowInvalidPrinterSettingsError(); - - // Processes a NOTIFY_PRINT_JOB_EVENT notification. - void OnNotifyPrintJobEvent(const printing::JobEventDetails& event_details); - - // content::NotificationObserver implementation. - void Observe(int, - const content::NotificationSource&, - const content::NotificationDetails&) override; - void StopWorker(int document_cookie); - // In the case of Scripted Printing, where the renderer is controlling the - // control flow, print_job_ is initialized whenever possible. No-op is - // print_job_ is initialized. - bool OpportunisticallyCreatePrintJob(int cookie); + // Creates a new empty print job. It has no settings loaded. If there is + // currently a print job, safely disconnect from it. Returns false if it is + // impossible to safely disconnect from the current print job or it is + // impossible to create a new print job. + virtual bool CreateNewPrintJob(std::unique_ptr<printing::PrinterQuery> query); + + // Makes sure the current print_job_ has all its data before continuing, and + // disconnect from it. + void DisconnectFromCurrentPrintJob(); + // PrintJob::Observer overrides: + void OnDocDone(int job_id, printing::PrintedDocument *document) override; + void OnJobDone() override; + void OnFailed() override; + + void StopWorker(int documentCookie); + +private: // Requests the RenderView to render all the missing pages for the print job. // No-op if no print job is pending. Returns true if at least one page has // been requested to the renderer. bool RenderAllMissingPagesNow(); - // Checks that synchronization is correct and a print query exists for - // |cookie|. If so, returns the document associated with the cookie. - printing::PrintedDocument* GetDocument(int cookie); + // Runs `callback` with `params` to reply to ScriptedPrint(). + void ScriptedPrintReply(ScriptedPrintCallback callback, + int process_id, + printing::mojom::PrintPagesParamsPtr params); + + // Checks that synchronization is correct with |print_job_| based on |cookie|. + bool PrintJobHasDocument(int cookie); - // Starts printing a document with data given in |print_data|. |print_data| - // must successfully initialize a metafile. |document| is the printed - // document associated with the print job. Returns true if successful. - void PrintDocument(printing::PrintedDocument *document, - const scoped_refptr<base::RefCountedMemory> &print_data, + // Starts printing the |document| in |print_job_| with the given |print_data|. + // This method assumes PrintJobHasDocument() has been called, and |print_data| + // contains valid data. + void PrintDocument(scoped_refptr<base::RefCountedMemory> print_data, const gfx::Size &page_size, const gfx::Rect &content_area, const gfx::Point &offsets); // Quits the current message loop if these conditions hold true: a document is // loaded and is complete and waiting_for_pages_to_be_rendered_ is true. This - // function is called in DidPrintDocument() or on ALL_PAGES_REQUESTED - // notification. The inner message loop is created was created by - // RenderAllMissingPagesNow(). + // function is called in DidPrintDocument(). The inner message loop was + // created by RenderAllMissingPagesNow(). void ShouldQuitFromInnerMessageLoop(); - bool RunInnerMessageLoop(); - + // Terminates the print job. No-op if no print job has been created. If + // |cancel| is true, cancel it instead of waiting for the job to finish. Will + // call ReleasePrintJob(). void TerminatePrintJob(bool cancel); - void DisconnectFromCurrentPrintJob(); - bool CreateNewPrintJob(std::unique_ptr<printing::PrinterQuery> query); + // Releases print_job_. Correctly deregisters from notifications. No-op if + // no print job has been created. void ReleasePrintJob(); - void ReleasePrinterQuery(); -private: - // Helper method for UpdatePrintingEnabled(). - void SendPrintingEnabled(bool enabled, content::RenderFrameHost* rfh); - // content::WebContentsObserver implementation. - void DidStartLoading() override; + // Runs an inner message loop. It will set inside_inner_message_loop_ to true + // while the blocking inner message loop is running. This is useful in cases + // where the RenderView is about to be destroyed while a printing job isn't + // finished. + bool RunInnerMessageLoop(); + + // In the case of Scripted Printing, where the renderer is controlling the + // control flow, print_job_ is initialized whenever possible. No-op is + // print_job_ is initialized. + bool OpportunisticallyCreatePrintJob(int cookie); + + // Release the PrinterQuery associated with our |cookie_|. + void ReleasePrinterQuery(); private: content::NotificationRegistrar m_registrar; scoped_refptr<printing::PrintJob> m_printJob; - bool m_isInsideInnerMessageLoop; + content::RenderFrameHost *m_printingRFH; bool m_didPrintingSucceed; + // Set while running an inner message loop inside RenderAllMissingPagesNow(). + // This means we are _blocking_ until all the necessary pages have been + // rendered or the print settings are being loaded. + base::OnceClosure m_quitInnerLoop; scoped_refptr<printing::PrintQueriesQueue> m_printerQueriesQueue; - // The current RFH that is printing with a system printing dialog. - content::RenderFrameHost *m_printingRFH; - DISALLOW_COPY_AND_ASSIGN(PrintViewManagerBaseQt); + + base::WeakPtrFactory<PrintViewManagerBaseQt> weak_ptr_factory_{this}; }; } // namespace QtWebEngineCore diff --git a/src/core/printing/print_view_manager_qt.cpp b/src/core/printing/print_view_manager_qt.cpp index 79e92fd6a..42bade52b 100644 --- a/src/core/printing/print_view_manager_qt.cpp +++ b/src/core/printing/print_view_manager_qt.cpp @@ -1,69 +1,37 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// 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 "pdf_util_qt.h" #include "type_conversion.h" #include "web_contents_adapter_client.h" +#include "web_contents_adapter.h" #include "web_contents_view_qt.h" #include "web_engine_context.h" #include <QtGui/qpagelayout.h> +#include <QtGui/qpageranges.h> #include <QtGui/qpagesize.h> #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_messages.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 "content/public/common/web_preferences.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" @@ -97,57 +65,57 @@ GetBytesFromHandle(const base::ReadOnlySharedMemoryRegion &handle) // Write the PDF file to disk. static void SavePdfFile(scoped_refptr<base::RefCountedBytes> data, const base::FilePath &path, - const QtWebEngineCore::PrintViewManagerQt::PrintToPDFFileCallback &saveCallback) + QtWebEngineCore::PrintViewManagerQt::PrintToPDFFileCallback saveCallback) { DCHECK_GT(data->size(), 0U); printing::MetafileSkia metafile; - metafile.InitFromData(static_cast<const void*>(data->front()), data->size()); + 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(saveCallback, success)); + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(saveCallback), success)); } -static base::DictionaryValue *createPrintSettings() +static base::Value::Dict createPrintSettings() { - base::DictionaryValue *printSettings = new base::DictionaryValue(); + base::Value::Dict printSettings; // 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); + printSettings.Set(printing::kIsFirstRequest, internalRequestId++ == 0); + printSettings.Set(printing::kPreviewRequestID, internalRequestId); // The following are standard settings that Chromium expects to be set. - printSettings->SetInteger(printing::kSettingPrinterType, printing::kPdfPrinter); + printSettings.Set(printing::kSettingPrinterType, static_cast<int>(printing::mojom::PrinterType::kPdf)); - printSettings->SetInteger(printing::kSettingDpiHorizontal, printing::kPointsPerInch); - printSettings->SetInteger(printing::kSettingDpiVertical, printing::kPointsPerInch); + printSettings.Set(printing::kSettingDpiHorizontal, printing::kPointsPerInch); + printSettings.Set(printing::kSettingDpiVertical, printing::kPointsPerInch); - printSettings->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); - printSettings->SetInteger(printing::kSettingCopies, 1); - printSettings->SetInteger(printing::kSettingPagesPerSheet, 1); - printSettings->SetBoolean(printing::kSettingCollate, false); + printSettings.Set(printing::kSettingDuplexMode, static_cast<int>(printing::mojom::DuplexMode::kSimplex)); + printSettings.Set(printing::kSettingCopies, 1); + printSettings.Set(printing::kSettingPagesPerSheet, 1); + printSettings.Set(printing::kSettingCollate, false); // printSettings->SetBoolean(printing::kSettingGenerateDraftData, false); - printSettings->SetBoolean(printing::kSettingPreviewModifiable, false); + printSettings.Set(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); + printSettings.Set(printing::kSettingShouldPrintSelectionOnly, base::Value(false)); + printSettings.Set(printing::kSettingShouldPrintBackgrounds, base::Value(true)); + printSettings.Set(printing::kSettingHeaderFooterEnabled, base::Value(false)); + printSettings.Set(printing::kSettingRasterizePdf, base::Value(false)); + printSettings.Set(printing::kSettingScaleFactor, 100); + printSettings.Set(printing::kSettingDeviceName, ""); + printSettings.Set(printing::kPreviewUIID, 12345678); return printSettings; } -static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayout &pageLayout, +static base::Value::Dict createPrintSettingsFromQPageLayout(const QPageLayout &pageLayout, bool useCustomMargins) { - base::DictionaryValue *printSettings = createPrintSettings(); + base::Value::Dict printSettings = createPrintSettings(); QRectF pageSizeInMillimeter; if (useCustomMargins) { @@ -155,185 +123,164 @@ static base::DictionaryValue *createPrintSettingsFromQPageLayout(const QPageLayo pageSizeInMillimeter = pageLayout.pageSize().rect(QPageSize::Millimeter); QMargins pageMarginsInPoints = pageLayout.marginsPoints(); - std::unique_ptr<base::DictionaryValue> 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()); + base::Value::Dict marginsDict; + marginsDict.Set(printing::kSettingMarginTop, pageMarginsInPoints.top()); + marginsDict.Set(printing::kSettingMarginBottom, pageMarginsInPoints.bottom()); + marginsDict.Set(printing::kSettingMarginLeft, pageMarginsInPoints.left()); + marginsDict.Set(printing::kSettingMarginRight, pageMarginsInPoints.right()); - printSettings->Set(printing::kSettingMarginsCustom, std::move(marginsDict)); - printSettings->SetInteger(printing::kSettingMarginsType, printing::CUSTOM_MARGINS); + printSettings.Set(printing::kSettingMarginsCustom, std::move(marginsDict)); + printSettings.Set(printing::kSettingMarginsType, (int)printing::mojom::MarginType::kCustomMargins); // pageSizeInMillimeter is in portrait orientation. Transpose it if necessary. - printSettings->SetBoolean(printing::kSettingLandscape, pageLayout.orientation() == QPageLayout::Landscape); + printSettings.Set(printing::kSettingLandscape, pageLayout.orientation() == QPageLayout::Landscape); } else { // QPrinter will handle margins pageSizeInMillimeter = pageLayout.paintRect(QPageLayout::Millimeter); - printSettings->SetInteger(printing::kSettingMarginsType, printing::NO_MARGINS); + printSettings.Set(printing::kSettingMarginsType, (int)printing::mojom::MarginType::kNoMargins); // pageSizeInMillimeter already contains the orientation. - printSettings->SetBoolean(printing::kSettingLandscape, false); + printSettings.Set(printing::kSettingLandscape, false); } //Set page size attributes, chromium expects these in micrometers - std::unique_ptr<base::DictionaryValue> 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)); + base::Value::Dict sizeDict; + sizeDict.Set(printing::kSettingMediaSizeWidthMicrons, int(pageSizeInMillimeter.width() * kMicronsToMillimeter)); + sizeDict.Set(printing::kSettingMediaSizeHeightMicrons, int(pageSizeInMillimeter.height() * kMicronsToMillimeter)); + printSettings.Set(printing::kSettingMediaSize, std::move(sizeDict)); return printSettings; } +static base::Value::List createPageRangeSettings(const QList<QPageRanges::Range> &ranges) +{ + base::Value::List pageRangeArray; + for (int i = 0; i < ranges.count(); i++) { + base::Value::Dict pageRange; + pageRange.Set(printing::kSettingPageRangeFrom, ranges.at(i).from); + pageRange.Set(printing::kSettingPageRangeTo, ranges.at(i).to); + pageRangeArray.Append(std::move(pageRange)); + } + return pageRangeArray; +} + } // namespace namespace QtWebEngineCore { -struct PrintViewManagerQt::FrameDispatchHelper { - PrintViewManagerQt* m_manager; - content::RenderFrameHost* m_renderFrameHost; - - bool Send(IPC::Message* msg) { - return m_renderFrameHost->Send(msg); - } - - void OnSetupScriptedPrintPreview(IPC::Message* reply_msg) { - m_manager->OnSetupScriptedPrintPreview(m_renderFrameHost, reply_msg); - } -}; - PrintViewManagerQt::~PrintViewManagerQt() { } void PrintViewManagerQt::PrintToPDFFileWithCallback(const QPageLayout &pageLayout, - bool printInColor, - const QString &filePath, - const PrintToPDFFileCallback& callback) + const QPageRanges &pageRanges, + bool printInColor, const QString &filePath, + quint64 frameId, + PrintToPDFFileCallback callback) { if (callback.is_null()) return; - if (m_printSettings || !filePath.length()) { - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(callback, false)); + if (!m_printSettings.empty() || !filePath.length()) { + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(callback), false)); return; } m_pdfOutputPath = toFilePath(filePath); - m_pdfSaveCallback = callback; - if (!PrintToPDFInternal(pageLayout, printInColor)) { - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(callback, false)); + m_pdfSaveCallback = std::move(callback); + if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor, /*useCustomMargins*/ true, + frameId)) { + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(m_pdfSaveCallback), false)); resetPdfState(); } } void PrintViewManagerQt::PrintToPDFWithCallback(const QPageLayout &pageLayout, - bool printInColor, - bool useCustomMargins, - const PrintToPDFCallback& callback) + const QPageRanges &pageRanges, bool printInColor, + bool useCustomMargins, quint64 frameId, + 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(callback, QSharedPointer<QByteArray>())); + if (!m_printSettings.empty()) { + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(callback), QSharedPointer<QByteArray>())); return; } - m_pdfPrintCallback = callback; - if (!PrintToPDFInternal(pageLayout, printInColor, useCustomMargins)) { - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(callback, QSharedPointer<QByteArray>())); + m_pdfPrintCallback = std::move(callback); + if (!PrintToPDFInternal(pageLayout, pageRanges, printInColor, useCustomMargins, frameId)) { + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer<QByteArray>())); resetPdfState(); } } bool PrintViewManagerQt::PrintToPDFInternal(const QPageLayout &pageLayout, - const bool printInColor, - const bool useCustomMargins) + const QPageRanges &pageRanges, const bool printInColor, + const bool useCustomMargins, quint64 frameId) { if (!pageLayout.isValid()) return false; - m_printSettings.reset(createPrintSettingsFromQPageLayout(pageLayout, useCustomMargins)); - m_printSettings->SetBoolean(printing::kSettingShouldPrintBackgrounds, - web_contents()->GetRenderViewHost()-> - GetWebkitPreferences().should_print_backgrounds); - m_printSettings->SetInteger(printing::kSettingColor, - printInColor ? printing::COLOR : printing::GRAYSCALE); + m_printSettings = createPrintSettingsFromQPageLayout(pageLayout, useCustomMargins); + m_printSettings.Set(printing::kSettingShouldPrintBackgrounds, + web_contents()->GetOrCreateWebPreferences().should_print_backgrounds); + m_printSettings.Set(printing::kSettingColor, + int(printInColor ? printing::mojom::ColorModel::kColor : printing::mojom::ColorModel::kGrayscale)); + if (!pageRanges.isEmpty()) + m_printSettings.Set(printing::kSettingPageRange, createPageRangeSettings(pageRanges.toRangeList())); - if (web_contents()->ShowingInterstitialPage() || web_contents()->IsCrashed()) + if (web_contents()->IsCrashed()) return false; - content::RenderFrameHost* rfh = web_contents()->GetMainFrame(); - GetPrintRenderFrame(rfh)->InitiatePrintPreview(mojo::PendingAssociatedRemote<printing::mojom::PrintRenderer>(), false); + content::RenderFrameHost *rfh = nullptr; + if (frameId == WebContentsAdapter::kInvalidFrameId) { + return false; + } else if (frameId == WebContentsAdapter::kUseMainFrameId) { + rfh = web_contents()->GetPrimaryMainFrame(); + // Use the plugin frame for printing if web_contents() is a PDF viewer guest + content::RenderFrameHost *full_page_plugin = GetFullPagePlugin(web_contents()); + if (content::RenderFrameHost *pdf_rfh = + FindPdfChildFrame(full_page_plugin ? full_page_plugin : rfh)) + rfh = pdf_rfh; + } else { + auto *ftn = content::FrameTreeNode::GloballyFindByID(static_cast<int>(frameId)); + if (!ftn) + return false; + rfh = ftn->current_frame_host(); + } + GetPrintRenderFrame(rfh)->InitiatePrintPreview(false); DCHECK(!m_printPreviewRfh); m_printPreviewRfh = rfh; return true; } -// PrintedPagesSource implementation. -base::string16 PrintViewManagerQt::RenderSourceName() -{ - return base::string16(); -} - PrintViewManagerQt::PrintViewManagerQt(content::WebContents *contents) : PrintViewManagerBaseQt(contents) + , content::WebContentsUserData<PrintViewManagerQt>(*contents) , m_printPreviewRfh(nullptr) { } -// content::WebContentsObserver implementation. -bool PrintViewManagerQt::OnMessageReceived(const IPC::Message& message, - content::RenderFrameHost* render_frame_host) +// static +void PrintViewManagerQt::BindPrintManagerHost(mojo::PendingAssociatedReceiver<printing::mojom::PrintManagerHost> receiver, + content::RenderFrameHost *rfh) { - FrameDispatchHelper helper = {this, render_frame_host}; - bool handled = true; - IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManagerQt, message, render_frame_host); - IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) - IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, OnRequestPrintPreview) - IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, OnMetafileReadyForPrinting); - IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) - IPC_MESSAGE_FORWARD_DELAY_REPLY( - PrintHostMsg_SetupScriptedPrintPreview, &helper, - FrameDispatchHelper::OnSetupScriptedPrintPreview) - IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, - OnShowScriptedPrintPreview) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled || PrintViewManagerBaseQt::OnMessageReceived(message, render_frame_host); -} - -void PrintViewManagerQt::RenderFrameDeleted(content::RenderFrameHost *render_frame_host) -{ - if (render_frame_host == m_printPreviewRfh) - PrintPreviewDone(); - PrintViewManagerBaseQt::RenderFrameDeleted(render_frame_host); - m_printRenderFrames.erase(render_frame_host); -} - -const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> &PrintViewManagerQt::GetPrintRenderFrame(content::RenderFrameHost *rfh) -{ - auto it = m_printRenderFrames.find(rfh); - if (it == m_printRenderFrames.end()) { - mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> remote; - rfh->GetRemoteAssociatedInterfaces()->GetInterface(&remote); - it = m_printRenderFrames.insert(std::make_pair(rfh, std::move(remote))).first; - } else if (it->second.is_bound() && !it->second.is_connected()) { - // When print preview is closed, the remote is disconnected from the - // receiver. Reset and bind the remote before using it again. - it->second.reset(); - rfh->GetRemoteAssociatedInterfaces()->GetInterface(&it->second); - } - - return it->second; + 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() @@ -341,50 +288,14 @@ void PrintViewManagerQt::resetPdfState() m_pdfOutputPath.clear(); m_pdfPrintCallback.Reset(); m_pdfSaveCallback.Reset(); - m_printSettings.reset(); -} - -// IPC handlers - -void PrintViewManagerQt::OnRequestPrintPreview( - const PrintHostMsg_RequestPrintPreview_Params &/*params*/) -{ - m_printPreviewRfh->Send(new PrintMsg_PrintPreview(m_printPreviewRfh->GetRoutingID(), - *m_printSettings)); - PrintPreviewDone(); + m_printSettings.clear(); } -void PrintViewManagerQt::OnMetafileReadyForPrinting(content::RenderFrameHost* rfh, - const PrintHostMsg_DidPreviewDocument_Params& params, - const PrintHostMsg_PreviewIds &ids) -{ - 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<QByteArray> data_array = GetStdVectorFromHandle(params.content.metafile_data_region); - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(pdf_print_callback, data_array)); - } else { - scoped_refptr<base::RefCountedBytes> data_bytes = GetBytesFromHandle(params.content.metafile_data_region); - base::PostTask(FROM_HERE, {base::ThreadPool(), base::MayBlock()}, - base::BindOnce(&SavePdfFile, data_bytes, pdfOutputPath, pdf_save_callback)); - } -} - -void PrintViewManagerQt::OnDidShowPrintDialog() -{ -} - -// content::WebContentsObserver implementation. -void PrintViewManagerQt::DidStartLoading() +void PrintViewManagerQt::PrintPreviewDone() { + if (m_printPreviewRfh->IsRenderFrameLive() && IsPrintRenderFrameConnected(m_printPreviewRfh)) + GetPrintRenderFrame(m_printPreviewRfh)->OnPrintPreviewDialogClosed(); + m_printPreviewRfh = nullptr; } // content::WebContentsObserver implementation. @@ -392,59 +303,119 @@ void PrintViewManagerQt::DidStartLoading() void PrintViewManagerQt::NavigationStopped() { if (!m_pdfPrintCallback.is_null()) { - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(m_pdfPrintCallback, QSharedPointer<QByteArray>())); + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer<QByteArray>())); } resetPdfState(); PrintViewManagerBaseQt::NavigationStopped(); } -void PrintViewManagerQt::RenderProcessGone(base::TerminationStatus status) +void PrintViewManagerQt::PrimaryMainFrameRenderProcessGone(base::TerminationStatus status) { - PrintViewManagerBaseQt::RenderProcessGone(status); + PrintViewManagerBaseQt::PrimaryMainFrameRenderProcessGone(status); if (!m_pdfPrintCallback.is_null()) { - base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(m_pdfPrintCallback, QSharedPointer<QByteArray>())); + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(m_pdfPrintCallback), QSharedPointer<QByteArray>())); } resetPdfState(); } -void PrintViewManagerQt::OnDidPreviewPage(content::RenderFrameHost* rfh, - const PrintHostMsg_DidPreviewPage_Params& params, - const PrintHostMsg_PreviewIds& ids) +void PrintViewManagerQt::RenderFrameDeleted(content::RenderFrameHost *render_frame_host) { - // just consume the message, this is just for sending 'page-preview-ready' for webui + if (render_frame_host == m_printPreviewRfh) + PrintPreviewDone(); + PrintViewManagerBaseQt::RenderFrameDeleted(render_frame_host); } -void PrintViewManagerQt::OnSetupScriptedPrintPreview(content::RenderFrameHost* rfh, - IPC::Message* reply_msg) +// mojom::PrintManagerHost: +void PrintViewManagerQt::SetupScriptedPrintPreview(SetupScriptedPrintPreviewCallback callback) { // ignore the scripted print - rfh->Send(reply_msg); + std::move(callback).Run(); content::WebContentsView *view = static_cast<content::WebContentsImpl*>(web_contents())->GetView(); WebContentsAdapterClient *client = WebContentsViewQt::from(view)->client(); - + content::RenderFrameHost *rfh = + print_manager_host_receivers_.GetCurrentTargetFrame(); if (!client) return; // close preview - GetPrintRenderFrame(rfh)->OnPrintPreviewDialogClosed(); + if (rfh) + GetPrintRenderFrame(rfh)->OnPrintPreviewDialogClosed(); - client->printRequested(); + if (web_contents()->GetPrimaryMainFrame() == rfh) + client->printRequested(); + else + client->printRequestedByFrame(static_cast<quint64>(rfh->GetFrameTreeNodeId())); } -void PrintViewManagerQt::OnShowScriptedPrintPreview(content::RenderFrameHost* rfh, - bool source_is_modifiable) +void PrintViewManagerQt::ShowScriptedPrintPreview(bool /*source_is_modifiable*/) { // ignore for now } -void PrintViewManagerQt::PrintPreviewDone() { - GetPrintRenderFrame(m_printPreviewRfh)->OnPrintPreviewDialogClosed(); - m_printPreviewRfh = nullptr; +void PrintViewManagerQt::RequestPrintPreview(printing::mojom::RequestPrintPreviewParamsPtr params) +{ + if (!m_printPreviewRfh && params->webnode_only) { + // The preview was requested by the print button of PDF viewer plugin. The code path ends up here, because + // Chromium automatically initiated a preview generation. We don't want that, just notify our embedder + // like we do in SetupScriptedPrintPreview() after window.print() and let them decide what to do. + content::WebContentsView *view = static_cast<content::WebContentsImpl*>(web_contents()->GetOutermostWebContents())->GetView(); + if (WebContentsAdapterClient *client = WebContentsViewQt::from(view)->client()) + client->printRequested(); + return; + } + + if (m_printSettings.empty()) { + PrintPreviewDone(); + return; + } + + mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> 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<QByteArray> data_array = GetStdVectorFromHandle(params->content->metafile_data_region); + content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE, + base::BindOnce(std::move(pdf_print_callback), data_array)); + } else { + scoped_refptr<base::RefCountedBytes> 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) +WEB_CONTENTS_USER_DATA_KEY_IMPL(PrintViewManagerQt); } // namespace QtWebEngineCore diff --git a/src/core/printing/print_view_manager_qt.h b/src/core/printing/print_view_manager_qt.h index 06c2f47ea..879a89ef0 100644 --- a/src/core/printing/print_view_manager_qt.h +++ b/src/core/printing/print_view_manager_qt.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only // Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be @@ -49,35 +13,16 @@ #include "qtwebenginecoreglobal_p.h" #include "base/memory/ref_counted.h" -#include "base/strings/string16.h" #include "components/prefs/pref_member.h" -#include "components/printing/browser/print_manager.h" #include "components/printing/common/print.mojom.h" -#include "components/printing/common/print_messages.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" #include "content/public/browser/web_contents_user_data.h" #include "mojo/public/cpp/bindings/associated_remote.h" #include <QSharedPointer> -struct PrintHostMsg_RequestPrintPreview_Params; -struct PrintHostMsg_DidPreviewDocument_Params; - -namespace content { -class RenderViewHost; -} - -namespace printing { -class JobEventDetails; -class MetafilePlayer; -class PrintJob; -class PrintJobWorkerOwner; -class PrintQueriesQueue; -} - QT_BEGIN_NAMESPACE class QPageLayout; +class QPageRanges; class QString; QT_END_NAMESPACE @@ -88,60 +33,49 @@ class PrintViewManagerQt { public: ~PrintViewManagerQt() override; - typedef base::Callback<void(QSharedPointer<QByteArray> result)> PrintToPDFCallback; - typedef base::Callback<void(bool success)> PrintToPDFFileCallback; - // Method to print a page to a Pdf document with page size \a pageSize in location \a filePath. - void PrintToPDFFileWithCallback(const QPageLayout &pageLayout, - bool printInColor, - const QString &filePath, - const PrintToPDFFileCallback& callback); - void PrintToPDFWithCallback(const QPageLayout &pageLayout, - bool printInColor, - bool useCustomMargins, - const PrintToPDFCallback &callback); + static void BindPrintManagerHost(mojo::PendingAssociatedReceiver<printing::mojom::PrintManagerHost> receiver, + content::RenderFrameHost *rfh); + + typedef base::OnceCallback<void(QSharedPointer<QByteArray> result)> PrintToPDFCallback; + typedef base::OnceCallback<void(bool success)> PrintToPDFFileCallback; - base::string16 RenderSourceName() override; + // Method to print a page to a Pdf document with page size \a pageSize in location \a filePath. + void PrintToPDFFileWithCallback(const QPageLayout &pageLayout, const QPageRanges &pageRanges, + bool printInColor, const QString &filePath, quint64 frameId, + PrintToPDFFileCallback callback); + void PrintToPDFWithCallback(const QPageLayout &pageLayout, const QPageRanges &pageRanges, + bool printInColor, bool useCustomMargins, quint64 frameId, + PrintToPDFCallback callback); protected: explicit PrintViewManagerQt(content::WebContents*); + bool PrintToPDFInternal(const QPageLayout &, const QPageRanges &, bool printInColor, + bool useCustomMargins, quint64 frameId); + // content::WebContentsObserver implementation. // Cancels the print job. void NavigationStopped() override; // Terminates or cancels the print job if one was pending. - void RenderProcessGone(base::TerminationStatus status) override; + void PrimaryMainFrameRenderProcessGone(base::TerminationStatus status) override; - // content::WebContentsObserver implementation. - bool OnMessageReceived(const IPC::Message& message, - content::RenderFrameHost* render_frame_host) override; void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; - // IPC handlers - void OnDidShowPrintDialog(); - void OnRequestPrintPreview(const PrintHostMsg_RequestPrintPreview_Params&); - void OnMetafileReadyForPrinting(content::RenderFrameHost* rfh, - const PrintHostMsg_DidPreviewDocument_Params& params, - const PrintHostMsg_PreviewIds &ids); - void OnSetupScriptedPrintPreview(content::RenderFrameHost* rfh, - IPC::Message* reply_msg); - void OnDidPreviewPage(content::RenderFrameHost* rfh, - const PrintHostMsg_DidPreviewPage_Params& params, - const PrintHostMsg_PreviewIds& ids); - void OnShowScriptedPrintPreview(content::RenderFrameHost* rfh, - bool source_is_modifiable); - bool PrintToPDFInternal(const QPageLayout &, bool printInColor, bool useCustomMargins = true); - + // mojom::PrintManagerHost: + void SetupScriptedPrintPreview(SetupScriptedPrintPreviewCallback callback) override; + void ShowScriptedPrintPreview(bool source_is_modifiable) override; + void RequestPrintPreview(printing::mojom::RequestPrintPreviewParamsPtr params) override; + void CheckForCancel(int32_t preview_ui_id, + int32_t request_id, + CheckForCancelCallback callback) override; + void MetafileReadyForPrinting(printing::mojom::DidPreviewDocumentParamsPtr params, + int32_t preview_ui_id) override; + void SetAccessibilityTree(int32_t, const ui::AXTreeUpdate &) override; private: void resetPdfState(); - // Helper method to fetch the PrintRenderFrame associated remote interface - // pointer. - const mojo::AssociatedRemote<printing::mojom::PrintRenderFrame> &GetPrintRenderFrame(content::RenderFrameHost *rfh); - - // content::WebContentsObserver implementation. - void DidStartLoading() override; void PrintPreviewDone(); private: @@ -150,13 +84,9 @@ private: base::FilePath m_pdfOutputPath; PrintToPDFCallback m_pdfPrintCallback; PrintToPDFFileCallback m_pdfSaveCallback; - std::unique_ptr<base::DictionaryValue> m_printSettings; - - std::map<content::RenderFrameHost*,mojo::AssociatedRemote<printing::mojom::PrintRenderFrame>> m_printRenderFrames; + base::Value::Dict m_printSettings; friend class content::WebContentsUserData<PrintViewManagerQt>; - DISALLOW_COPY_AND_ASSIGN(PrintViewManagerQt); - struct FrameDispatchHelper; }; } // namespace QtWebEngineCore diff --git a/src/core/printing/printer_worker.cpp b/src/core/printing/printer_worker.cpp new file mode 100644 index 000000000..6032f2211 --- /dev/null +++ b/src/core/printing/printer_worker.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2018 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "printer_worker.h" + +#include "printing/pdfium_document_wrapper_qt.h" + +#include <QPainter> +#include <QPagedPaintDevice> + +namespace QtWebEngineCore { + +PrinterWorker::PrinterWorker(QSharedPointer<QByteArray> data, QPagedPaintDevice *device) + : m_data(data), m_device(device) +{ +} + +PrinterWorker::~PrinterWorker() { } + +void PrinterWorker::print() +{ + if (!m_data->size()) { + qWarning("Failed to print: Print result data is empty."); + Q_EMIT resultReady(false); + return; + } + + PdfiumDocumentWrapperQt pdfiumWrapper(m_data->constData(), m_data->size()); + + const int fromPage = m_firstPageFirst ? 0 : pdfiumWrapper.pageCount() - 1; + const int toPage = m_firstPageFirst ? pdfiumWrapper.pageCount() : -1; + + int pageCopies = 1; + if (m_collateCopies) { + pageCopies = m_documentCopies; + m_documentCopies = 1; + } + + qreal resolution = m_deviceResolution / 72.0; // pdfium uses points so 1/72 inch + + QPainter painter; + + for (int printedDocuments = 0; printedDocuments < m_documentCopies; printedDocuments++) { + if (printedDocuments > 0) + m_device->newPage(); + + for (int i = fromPage; i != toPage; m_firstPageFirst ? i++ : i--) { + QSizeF documentSize = (pdfiumWrapper.pageSize(i) * resolution); + bool isLandscape = documentSize.width() > documentSize.height(); + m_device->setPageOrientation(isLandscape ? QPageLayout::Landscape + : QPageLayout::Portrait); + QRectF paintRect = m_device->pageLayout().paintRectPixels(m_deviceResolution); + documentSize = documentSize.scaled(paintRect.size(), Qt::KeepAspectRatio); + + // setPageOrientation has to be called before qpainter.begin() or before + // qprinter.newPage() so correct metrics is used, therefore call begin now for only + // first page + if (!painter.isActive() && !painter.begin(m_device)) { + qWarning("Failure to print on device: Could not open printer for painting."); + Q_EMIT resultReady(false); + return; + } + + if (i != fromPage) + m_device->newPage(); + + for (int printedPages = 0; printedPages < pageCopies; printedPages++) { + if (printedPages > 0) + m_device->newPage(); + + QImage currentImage = + pdfiumWrapper.pageAsQImage(i, documentSize.width(), documentSize.height()); + if (currentImage.isNull()) { + Q_EMIT resultReady(false); + return; + } + painter.drawImage(0, 0, currentImage); + } + } + } + painter.end(); + + Q_EMIT resultReady(true); + return; +} + +} // namespace QtWebEngineCore diff --git a/src/core/printing/printer_worker.h b/src/core/printing/printer_worker.h new file mode 100644 index 000000000..0d2454fa0 --- /dev/null +++ b/src/core/printing/printer_worker.h @@ -0,0 +1,58 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef PRINTER_WORKER_H +#define PRINTER_WORKER_H + +#include <QtWebEngineCore/private/qtwebenginecoreglobal_p.h> + +#include <QtCore/qobject.h> +#include <QtCore/qsharedpointer.h> + +QT_BEGIN_NAMESPACE +class QPagedPaintDevice; +QT_END_NAMESPACE + +namespace QtWebEngineCore { + +class Q_WEBENGINECORE_EXPORT PrinterWorker : public QObject +{ + Q_OBJECT +public: + PrinterWorker(QSharedPointer<QByteArray> data, QPagedPaintDevice *device); + virtual ~PrinterWorker(); + + int m_deviceResolution; + bool m_firstPageFirst; + int m_documentCopies; + bool m_collateCopies; + +public Q_SLOTS: + void print(); + +Q_SIGNALS: + void resultReady(bool success); + +private: + Q_DISABLE_COPY(PrinterWorker) + + QSharedPointer<QByteArray> m_data; + QPagedPaintDevice *m_device; +}; + +} // namespace QtWebEngineCore + +Q_DECLARE_METATYPE(QtWebEngineCore::PrinterWorker *) + +#endif // PRINTER_WORKER_H diff --git a/src/core/printing/printing_message_filter_qt.cpp b/src/core/printing/printing_message_filter_qt.cpp deleted file mode 100644 index 5b9228d20..000000000 --- a/src/core/printing/printing_message_filter_qt.cpp +++ /dev/null @@ -1,234 +0,0 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ - -// Based on chrome/browser/printing/printing_message_filter.cc: -// 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.Chromium file. - -#include "printing_message_filter_qt.h" - -#include "web_engine_context.h" - -#include <string> - -#include "base/bind.h" -#include "chrome/browser/printing/print_job_manager.h" -#include "chrome/browser/printing/printer_query.h" -#include "components/printing/browser/print_manager_utils.h" -#include "components/printing/common/print_messages.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/render_view_host.h" -#include "content/public/browser/web_contents.h" -#include "content/public/common/child_process_host.h" - -using content::BrowserThread; - -namespace QtWebEngineCore { - -PrintingMessageFilterQt::PrintingMessageFilterQt(int render_process_id) - : BrowserMessageFilter(PrintMsgStart), - render_process_id_(render_process_id), - queue_(WebEngineContext::current()->getPrintJobManager()->queue()) { - DCHECK(queue_.get()); -} - -PrintingMessageFilterQt::~PrintingMessageFilterQt() { -} - -void PrintingMessageFilterQt::OverrideThreadForMessage( - const IPC::Message& message, BrowserThread::ID* thread) { -} - -bool PrintingMessageFilterQt::OnMessageReceived(const IPC::Message& message) { - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(PrintingMessageFilterQt, message) - IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, - OnGetDefaultPrintSettings) - IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) - IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, - OnUpdatePrintSettings) - IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled; -} - -void PrintingMessageFilterQt::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { - DCHECK_CURRENTLY_ON(BrowserThread::IO); - std::unique_ptr<printing::PrinterQuery> printer_query; - - printer_query = queue_->PopPrinterQuery(0); - if (!printer_query) { - printer_query = - queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id()); - } - - // Loads default settings. This is asynchronous, only the IPC message sender - // will hang until the settings are retrieved. - printer_query->GetSettings( - printing::PrinterQuery::GetSettingsAskParam::DEFAULTS, - 0, - false, - printing::DEFAULT_MARGINS, - false, - false, - base::BindOnce(&PrintingMessageFilterQt::OnGetDefaultPrintSettingsReply, - this, - std::move(printer_query), - reply_msg)); -} - -void PrintingMessageFilterQt::OnGetDefaultPrintSettingsReply( - std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg) { - PrintMsg_Print_Params params; - if (!printer_query.get() || - printer_query->last_status() != printing::PrintingContext::OK) { - params.Reset(); - } else { - RenderParamsFromPrintSettings(printer_query->settings(), ¶ms); - params.document_cookie = printer_query->cookie(); - } - PrintHostMsg_GetDefaultPrintSettings::WriteReplyParams(reply_msg, params); - Send(reply_msg); - // If printing was enabled. - if (printer_query.get()) { - // If user hasn't cancelled. - if (printer_query->cookie() && printer_query->settings().dpi()) { - queue_->QueuePrinterQuery(std::move(printer_query)); - } else { - printer_query->StopWorker(); - } - } -} - -void PrintingMessageFilterQt::OnScriptedPrint( - const PrintHostMsg_ScriptedPrint_Params& params, - IPC::Message* reply_msg) { - std::unique_ptr<printing::PrinterQuery> printer_query = - queue_->PopPrinterQuery(params.cookie); - if (!printer_query.get()) { - printer_query = - queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id()); - } - printer_query->GetSettings( - printing::PrinterQuery::GetSettingsAskParam::ASK_USER, - params.expected_pages_count, - params.has_selection, - params.margin_type, - params.is_scripted, - params.is_modifiable, - base::BindOnce(&PrintingMessageFilterQt::OnScriptedPrintReply, - this, - std::move(printer_query), - reply_msg)); -} - -void PrintingMessageFilterQt::OnScriptedPrintReply( - std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg) { - PrintMsg_PrintPages_Params params; - - if (printer_query->last_status() != printing::PrintingContext::OK || - !printer_query->settings().dpi()) { - params.Reset(); - } else { - RenderParamsFromPrintSettings(printer_query->settings(), ¶ms.params); - params.params.document_cookie = printer_query->cookie(); - params.pages = printing::PageRange::GetPages(printer_query->settings().ranges()); - } - PrintHostMsg_ScriptedPrint::WriteReplyParams(reply_msg, params); - Send(reply_msg); - if (!params.params.dpi.IsEmpty() && params.params.document_cookie) { - queue_->QueuePrinterQuery(std::move(printer_query)); - } else { - printer_query->StopWorker(); - } -} - -void PrintingMessageFilterQt::OnUpdatePrintSettings(int document_cookie, - base::Value job_settings, - IPC::Message* reply_msg) { - std::unique_ptr<printing::PrinterQuery> printer_query; - printer_query = queue_->PopPrinterQuery(document_cookie); - if (!printer_query.get()) { - printer_query = queue_->CreatePrinterQuery( - content::ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); - } - printer_query->SetSettings( - std::move(job_settings), - base::BindOnce(&PrintingMessageFilterQt::OnUpdatePrintSettingsReply, this, - std::move(printer_query), reply_msg)); -} - -void PrintingMessageFilterQt::OnUpdatePrintSettingsReply(std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg) { - PrintMsg_PrintPages_Params params; - if (!printer_query.get() || - printer_query->last_status() != printing::PrintingContext::OK) { - params.Reset(); - } else { - RenderParamsFromPrintSettings(printer_query->settings(), ¶ms.params); - params.params.document_cookie = printer_query->cookie(); - params.pages = printing::PageRange::GetPages(printer_query->settings().ranges()); - } - - PrintHostMsg_UpdatePrintSettings::WriteReplyParams( - reply_msg, - params, - printer_query.get() && - (printer_query->last_status() == printing::PrintingContext::CANCEL)); - Send(reply_msg); - // If user hasn't cancelled. - if (printer_query) { - if (printer_query->cookie() && printer_query->settings().dpi()) { - queue_->QueuePrinterQuery(std::move(printer_query)); - } else { - printer_query->StopWorker(); - } - } -} - -void PrintingMessageFilterQt::OnCheckForCancel(const PrintHostMsg_PreviewIds& ids, - bool* cancel) { - *cancel = false; -} - -} // namespace printing diff --git a/src/core/printing/printing_message_filter_qt.h b/src/core/printing/printing_message_filter_qt.h deleted file mode 100644 index 89e473dcf..000000000 --- a/src/core/printing/printing_message_filter_qt.h +++ /dev/null @@ -1,126 +0,0 @@ -/**************************************************************************** -** -** 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$ -** -****************************************************************************/ - -// 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.Chromium file. - -#ifndef PRINTING_PRINTING_MESSAGE_FILTER_QT_H_ -#define PRINTING_PRINTING_MESSAGE_FILTER_QT_H_ - -#include <string> - -#include "base/compiler_specific.h" -#include "components/prefs/pref_member.h" -#include "content/public/browser/browser_message_filter.h" - -struct PrintHostMsg_PreviewIds; -struct PrintHostMsg_ScriptedPrint_Params; - -namespace base { -class DictionaryValue; -class FilePath; -} - -namespace content { -class WebContents; -} - -namespace printing { -class PrintJobManager; -class PrintQueriesQueue; -class PrinterQuery; -} - -namespace QtWebEngineCore { -// This class filters out incoming printing related IPC messages for the -// renderer process on the IPC thread. -class PrintingMessageFilterQt : public content::BrowserMessageFilter { - public: - PrintingMessageFilterQt(int render_process_id); - - // content::BrowserMessageFilter methods. - void OverrideThreadForMessage(const IPC::Message& message, - content::BrowserThread::ID* thread) override; - bool OnMessageReceived(const IPC::Message& message) override; - - private: - ~PrintingMessageFilterQt() override; - - // GetPrintSettingsForRenderView must be called via PostTask and - // base::Bind. Collapse the settings-specific params into a - // struct to avoid running into issues with too many params - // to base::Bind. - struct GetPrintSettingsForRenderViewParams; - - // Get the default print setting. - void OnGetDefaultPrintSettings(IPC::Message* reply_msg); - void OnGetDefaultPrintSettingsReply(std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg); - - // The renderer host have to show to the user the print dialog and returns - // the selected print settings. The task is handled by the print worker - // thread and the UI thread. The reply occurs on the IO thread. - void OnScriptedPrint(const PrintHostMsg_ScriptedPrint_Params& params, - IPC::Message* reply_msg); - void OnScriptedPrintReply(std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg); - - // Modify the current print settings based on |job_settings|. The task is - // handled by the print worker thread and the UI thread. The reply occurs on - // the IO thread. - void OnUpdatePrintSettings(int document_cookie, - base::Value job_settings, - IPC::Message* reply_msg); - void OnUpdatePrintSettingsReply(std::unique_ptr<printing::PrinterQuery> printer_query, - IPC::Message* reply_msg); - - // Check to see if print preview has been cancelled. - void OnCheckForCancel(const PrintHostMsg_PreviewIds& ids, bool* cancel); - - const int render_process_id_; - - scoped_refptr<printing::PrintQueriesQueue> queue_; - - DISALLOW_COPY_AND_ASSIGN(PrintingMessageFilterQt); -}; - -} // namespace printing - -#endif // PRINTING_PRINTING_MESSAGE_FILTER_QT_H_ |