diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/components/printing | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/components/printing')
14 files changed, 377 insertions, 277 deletions
diff --git a/chromium/components/printing/browser/BUILD.gn b/chromium/components/printing/browser/BUILD.gn index e25da9dfe5d..1b3c83d06c8 100644 --- a/chromium/components/printing/browser/BUILD.gn +++ b/chromium/components/printing/browser/BUILD.gn @@ -40,6 +40,7 @@ static_library("browser") { "print_manager_utils.h", "printer_capabilities.cc", "printer_capabilities.h", + "service_sandbox_type.h", ] public_deps = [ diff --git a/chromium/components/printing/browser/OWNERS b/chromium/components/printing/browser/OWNERS index b1b8445d4ef..ba1476bc785 100644 --- a/chromium/components/printing/browser/OWNERS +++ b/chromium/components/printing/browser/OWNERS @@ -1 +1,5 @@ # COMPONENT: Internals>Printing + +# Service sandbox specialization must be reviewed by SECURITY_OWNERS +per-file service_sandbox_type.h=set noparent +per-file service_sandbox_type.h=file://ipc/SECURITY_OWNERS
\ No newline at end of file diff --git a/chromium/components/printing/browser/print_composite_client.cc b/chromium/components/printing/browser/print_composite_client.cc index ff32a589713..9915fd124dc 100644 --- a/chromium/components/printing/browser/print_composite_client.cc +++ b/chromium/components/printing/browser/print_composite_client.cc @@ -9,9 +9,9 @@ #include "base/bind.h" #include "base/memory/read_only_shared_memory_region.h" #include "base/stl_util.h" -#include "base/task/post_task.h" #include "build/build_config.h" #include "components/discardable_memory/service/discardable_shared_memory_manager.h" +#include "components/printing/browser/service_sandbox_type.h" #include "components/printing/common/print_messages.h" #include "components/services/print_compositor/public/cpp/print_service_mojo_types.h" #include "components/strings/grit/components_strings.h" @@ -20,6 +20,7 @@ #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/service_process_host.h" +#include "printing/common/metafile_utils.h" #include "printing/printing_utils.h" #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" @@ -79,40 +80,41 @@ PrintCompositeClient::~PrintCompositeClient() {} bool PrintCompositeClient::OnMessageReceived( const IPC::Message& message, content::RenderFrameHost* render_frame_host) { +#if BUILDFLAG(ENABLE_TAGGED_PDF) bool handled = true; IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintCompositeClient, message, render_frame_host) - IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintFrameContent, - OnDidPrintFrameContent) -#if BUILDFLAG(ENABLE_TAGGED_PDF) IPC_MESSAGE_HANDLER(PrintHostMsg_AccessibilityTree, OnAccessibilityTree) -#endif IPC_MESSAGE_UNHANDLED(handled = false) IPC_END_MESSAGE_MAP() return handled; +#else + return false; +#endif } void PrintCompositeClient::RenderFrameDeleted( content::RenderFrameHost* render_frame_host) { - uint64_t frame_guid = GenerateFrameGuid(render_frame_host); - auto iter = pending_subframe_cookies_.find(frame_guid); - if (iter != pending_subframe_cookies_.end()) { - // When a subframe we are expecting is deleted, we should notify print + if (document_cookie_ == 0) + return; + + auto iter = pending_subframes_.find(render_frame_host); + if (iter != pending_subframes_.end()) { + // When a subframe we are expecting is deleted, we should notify the print // compositor service. - for (int doc_cookie : iter->second) { - auto* compositor = GetCompositeRequest(doc_cookie); - compositor->NotifyUnavailableSubframe(frame_guid); - } - pending_subframe_cookies_.erase(iter); + auto* compositor = GetCompositeRequest(document_cookie_); + compositor->NotifyUnavailableSubframe(GenerateFrameGuid(render_frame_host)); + pending_subframes_.erase(iter); } print_render_frames_.erase(render_frame_host); } void PrintCompositeClient::OnDidPrintFrameContent( - content::RenderFrameHost* render_frame_host, + int render_process_id, + int render_frame_id, int document_cookie, - const PrintHostMsg_DidPrintContent_Params& params) { + mojom::DidPrintContentParamsPtr params) { auto* outer_contents = web_contents()->GetOuterWebContents(); if (outer_contents) { // When the printed content belongs to an extension or app page, the print @@ -123,26 +125,31 @@ void PrintCompositeClient::OnDidPrintFrameContent( // contents nested in multiple layers. auto* outer_client = PrintCompositeClient::FromWebContents(outer_contents); DCHECK(outer_client); - outer_client->OnDidPrintFrameContent(render_frame_host, document_cookie, - params); + outer_client->OnDidPrintFrameContent(render_process_id, render_frame_id, + document_cookie, std::move(params)); return; } + if (document_cookie_ != document_cookie) + return; + + auto* render_frame_host = + content::RenderFrameHost::FromID(render_process_id, render_frame_id); + if (!render_frame_host) + return; + // Content in |params| is sent from untrusted source; only minimal processing // is done here. Most of it will be directly forwarded to print compositor // service. auto* compositor = GetCompositeRequest(document_cookie); - auto region = params.metafile_data_region.Duplicate(); - uint64_t frame_guid = GenerateFrameGuid(render_frame_host); + auto region = params->metafile_data_region.Duplicate(); compositor->AddSubframeContent( - frame_guid, std::move(region), - ConvertContentInfoMap(render_frame_host, params.subframe_content_info)); + GenerateFrameGuid(render_frame_host), std::move(region), + ConvertContentInfoMap(render_frame_host, params->subframe_content_info)); // Update our internal states about this frame. - pending_subframe_cookies_[frame_guid].erase(document_cookie); - if (pending_subframe_cookies_[frame_guid].empty()) - pending_subframe_cookies_.erase(frame_guid); - printed_subframes_[document_cookie].insert(frame_guid); + pending_subframes_.erase(render_frame_host); + printed_subframes_.insert(render_frame_host); } #if BUILDFLAG(ENABLE_TAGGED_PDF) @@ -159,38 +166,36 @@ void PrintCompositeClient::PrintCrossProcessSubframe( int document_cookie, content::RenderFrameHost* subframe_host) { auto params = mojom::PrintFrameContentParams::New(rect, document_cookie); - uint64_t frame_guid = GenerateFrameGuid(subframe_host); if (!subframe_host->IsRenderFrameLive()) { // When the subframe is dead, no need to send message, // just notify the service. auto* compositor = GetCompositeRequest(document_cookie); - compositor->NotifyUnavailableSubframe(frame_guid); + compositor->NotifyUnavailableSubframe(GenerateFrameGuid(subframe_host)); return; } - auto subframe_iter = printed_subframes_.find(document_cookie); - if (subframe_iter != printed_subframes_.end() && - base::Contains(subframe_iter->second, frame_guid)) { - // If this frame is already printed, no need to print again. - return; - } - - auto cookie_iter = pending_subframe_cookies_.find(frame_guid); - if (cookie_iter != pending_subframe_cookies_.end() && - base::Contains(cookie_iter->second, document_cookie)) { - // If this frame is being printed, no need to print again. + // If this frame is already printed, no need to print again. + if (base::Contains(pending_subframes_, subframe_host) || + base::Contains(printed_subframes_, subframe_host)) { return; } // Send the request to the destination frame. - GetPrintRenderFrame(subframe_host)->PrintFrameContent(std::move(params)); - pending_subframe_cookies_[frame_guid].insert(document_cookie); + int render_process_id = subframe_host->GetProcess()->GetID(); + int render_frame_id = subframe_host->GetRoutingID(); + GetPrintRenderFrame(subframe_host) + ->PrintFrameContent( + std::move(params), + base::BindOnce(&PrintCompositeClient::OnDidPrintFrameContent, + weak_ptr_factory_.GetWeakPtr(), render_process_id, + render_frame_id)); + pending_subframes_.insert(subframe_host); } void PrintCompositeClient::DoCompositePageToPdf( int document_cookie, content::RenderFrameHost* render_frame_host, - const PrintHostMsg_DidPrintContent_Params& content, + const mojom::DidPrintContentParams& content, mojom::PrintCompositor::CompositePageToPdfCallback callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); @@ -209,8 +214,8 @@ void PrintCompositeClient::DoPrepareForDocumentToPdf( DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK(!GetIsDocumentConcurrentlyComposited(document_cookie)); - is_doc_concurrently_composited_set_.insert(document_cookie); - auto* compositor = GetCompositeRequest(document_cookie); + auto* compositor = CreateCompositeRequest(document_cookie); + is_doc_concurrently_composited_ = true; compositor->PrepareForDocumentToPdf( base::BindOnce(&PrintCompositeClient::OnDidPrepareForDocumentToPdf, std::move(callback))); @@ -238,12 +243,12 @@ void PrintCompositeClient::DoCompleteDocumentToPdf( void PrintCompositeClient::DoCompositeDocumentToPdf( int document_cookie, content::RenderFrameHost* render_frame_host, - const PrintHostMsg_DidPrintContent_Params& content, + const mojom::DidPrintContentParams& content, mojom::PrintCompositor::CompositeDocumentToPdfCallback callback) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK(!GetIsDocumentConcurrentlyComposited(document_cookie)); - auto* compositor = GetCompositeRequest(document_cookie); + auto* compositor = CreateCompositeRequest(document_cookie); auto region = content.metafile_data_region.Duplicate(); // Since this class owns compositor, compositor will be gone when this class @@ -271,9 +276,6 @@ void PrintCompositeClient::OnDidCompositeDocumentToPdf( mojom::PrintCompositor::Status status, base::ReadOnlySharedMemoryRegion region) { RemoveCompositeRequest(document_cookie); - // Clear all stored printed subframes. - printed_subframes_.erase(document_cookie); - std::move(callback).Run(status, std::move(region)); } @@ -290,54 +292,61 @@ void PrintCompositeClient::OnDidCompleteDocumentToPdf( mojom::PrintCompositor::Status status, base::ReadOnlySharedMemoryRegion region) { RemoveCompositeRequest(document_cookie); - // Clear all stored printed subframes. - printed_subframes_.erase(document_cookie); - // No longer concurrently compositing this document. - is_doc_concurrently_composited_set_.erase(document_cookie); std::move(callback).Run(status, std::move(region)); } bool PrintCompositeClient::GetIsDocumentConcurrentlyComposited( int cookie) const { - return base::Contains(is_doc_concurrently_composited_set_, cookie); + return is_doc_concurrently_composited_ && document_cookie_ == cookie; } -mojom::PrintCompositor* PrintCompositeClient::GetCompositeRequest(int cookie) { - auto iter = compositor_map_.find(cookie); - if (iter != compositor_map_.end()) { - DCHECK(iter->second.is_bound()); - return iter->second.get(); +mojom::PrintCompositor* PrintCompositeClient::CreateCompositeRequest( + int cookie) { + if (document_cookie_ != 0) { + DCHECK_NE(document_cookie_, cookie); + RemoveCompositeRequest(document_cookie_); } + document_cookie_ = cookie; - iter = compositor_map_.emplace(cookie, CreateCompositeRequest()).first; - return iter->second.get(); -} - -void PrintCompositeClient::RemoveCompositeRequest(int cookie) { - size_t erased = compositor_map_.erase(cookie); - DCHECK_EQ(erased, 1u); -} - -mojo::Remote<mojom::PrintCompositor> -PrintCompositeClient::CreateCompositeRequest() { - auto compositor = content::ServiceProcessHost::Launch<mojom::PrintCompositor>( + compositor_ = content::ServiceProcessHost::Launch<mojom::PrintCompositor>( content::ServiceProcessHost::Options() .WithDisplayName(IDS_PRINT_COMPOSITOR_SERVICE_DISPLAY_NAME) - .WithSandboxType(service_manager::SandboxType::kPrintCompositor) .Pass()); mojo::PendingRemote<discardable_memory::mojom::DiscardableSharedMemoryManager> discardable_memory_manager; - base::PostTask( - FROM_HERE, {content::BrowserThread::IO}, + content::GetIOThreadTaskRunner({})->PostTask( + FROM_HERE, base::BindOnce( &BindDiscardableSharedMemoryManagerOnIOThread, discardable_memory_manager.InitWithNewPipeAndPassReceiver())); - compositor->SetDiscardableSharedMemoryManager( + compositor_->SetDiscardableSharedMemoryManager( std::move(discardable_memory_manager)); - compositor->SetWebContentsURL(web_contents()->GetLastCommittedURL()); - compositor->SetUserAgent(user_agent_); - return compositor; + compositor_->SetWebContentsURL(web_contents()->GetLastCommittedURL()); + compositor_->SetUserAgent(user_agent_); + + return compositor_.get(); +} + +void PrintCompositeClient::RemoveCompositeRequest(int cookie) { + DCHECK_EQ(document_cookie_, cookie); + compositor_.reset(); + document_cookie_ = 0; + + // Clear all stored printed and pending subframes. + pending_subframes_.clear(); + printed_subframes_.clear(); + + // No longer concurrently compositing this document. + is_doc_concurrently_composited_ = false; +} + +mojom::PrintCompositor* PrintCompositeClient::GetCompositeRequest( + int cookie) const { + DCHECK_NE(0, document_cookie_); + DCHECK_EQ(document_cookie_, cookie); + DCHECK(compositor_.is_bound()); + return compositor_.get(); } const mojo::AssociatedRemote<mojom::PrintRenderFrame>& diff --git a/chromium/components/printing/browser/print_composite_client.h b/chromium/components/printing/browser/print_composite_client.h index 95d8376b01a..356af70f5df 100644 --- a/chromium/components/printing/browser/print_composite_client.h +++ b/chromium/components/printing/browser/print_composite_client.h @@ -9,6 +9,7 @@ #include <memory> #include "base/containers/flat_set.h" +#include "base/memory/weak_ptr.h" #include "build/build_config.h" #include "components/printing/common/print.mojom.h" #include "components/services/print_compositor/public/mojom/print_compositor.mojom.h" @@ -19,8 +20,6 @@ #include "printing/buildflags/buildflags.h" #include "ui/accessibility/ax_tree_update_forward.h" -struct PrintHostMsg_DidPrintContent_Params; - namespace printing { // Class to manage print requests and their communication with print compositor @@ -40,10 +39,6 @@ class PrintCompositeClient void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; // IPC message handler. - void OnDidPrintFrameContent( - content::RenderFrameHost* render_frame_host, - int document_cookie, - const PrintHostMsg_DidPrintContent_Params& params); #if BUILDFLAG(ENABLE_TAGGED_PDF) void OnAccessibilityTree(int document_cookie, const ui::AXTreeUpdate& accessibility_tree); @@ -62,7 +57,7 @@ class PrintCompositeClient void DoCompositePageToPdf( int cookie, content::RenderFrameHost* render_frame_host, - const PrintHostMsg_DidPrintContent_Params& content, + const mojom::DidPrintContentParams& content, mojom::PrintCompositor::CompositePageToPdfCallback callback); // Notifies compositor to collect individual pages into a document @@ -84,7 +79,7 @@ class PrintCompositeClient void DoCompositeDocumentToPdf( int cookie, content::RenderFrameHost* render_frame_host, - const PrintHostMsg_DidPrintContent_Params& content, + const mojom::DidPrintContentParams& content, mojom::PrintCompositor::CompositeDocumentToPdfCallback callback); // Get the concurrent composition status for a document. Identifies if the @@ -118,37 +113,47 @@ class PrintCompositeClient mojom::PrintCompositor::Status status, base::ReadOnlySharedMemoryRegion region); - // Get the request or create a new one if none exists. - // Since printed pages always share content with its document, they share the - // same composite request. - mojom::PrintCompositor* GetCompositeRequest(int cookie); + void OnDidPrintFrameContent(int render_process_id, + int render_frame_id, + int document_cookie, + mojom::DidPrintContentParamsPtr params); - // Remove an existing request from |compositor_map_|. + // Creates a new composite request for a given document |cookie|. Since + // printed pages always share content with its document, they share the same + // composite request. Launches the compositor in a separate process. + // If a composite request already exists, it is removed. + // Returns the created composite request. + mojom::PrintCompositor* CreateCompositeRequest(int cookie); + + // Remove the existing composite request. void RemoveCompositeRequest(int cookie); - mojo::Remote<mojom::PrintCompositor> CreateCompositeRequest(); + // Get the composite request of a document. |cookie| must be valid and equal + // to |document_cookie_|. + mojom::PrintCompositor* GetCompositeRequest(int cookie) const; // Helper method to fetch the PrintRenderFrame remote interface pointer // associated with a given subframe. const mojo::AssociatedRemote<mojom::PrintRenderFrame>& GetPrintRenderFrame( content::RenderFrameHost* rfh); - // Stores the mapping between document cookies and their corresponding - // requests. - std::map<int, mojo::Remote<mojom::PrintCompositor>> compositor_map_; + // Stores the message pipe endpoint for making remote calls to the compositor. + mojo::Remote<mojom::PrintCompositor> compositor_; + + // Stores the unique sequential cookie of the document being composited. + // Holds 0 if no document is being composited. + int document_cookie_ = 0; - // Stores the mapping between render frame's global unique id and document - // cookies that requested such frame. - std::map<uint64_t, base::flat_set<int>> pending_subframe_cookies_; + // Stores whether the document is concurrently compositing using individual + // pages, so that no separate composite request with full-document blob is + // required. + bool is_doc_concurrently_composited_ = false; - // Stores the mapping between document cookie and all the printed subframes - // for that document. - std::map<int, base::flat_set<uint64_t>> printed_subframes_; + // Stores the pending subframes for the composited document. + base::flat_set<content::RenderFrameHost*> pending_subframes_; - // Stores the set of cookies for documents that are doing concurrently - // composition using individual pages, so that no separate composite request - // with full-document blob is required. - base::flat_set<int> is_doc_concurrently_composited_set_; + // Stores the printed subframes for the composited document. + base::flat_set<content::RenderFrameHost*> printed_subframes_; std::string user_agent_; @@ -159,6 +164,8 @@ class PrintCompositeClient mojo::AssociatedRemote<mojom::PrintRenderFrame>> print_render_frames_; + base::WeakPtrFactory<PrintCompositeClient> weak_ptr_factory_{this}; + WEB_CONTENTS_USER_DATA_KEY_DECL(); DISALLOW_COPY_AND_ASSIGN(PrintCompositeClient); diff --git a/chromium/components/printing/browser/print_manager_utils.cc b/chromium/components/printing/browser/print_manager_utils.cc index 0d7c4f74679..3988fb6a309 100644 --- a/chromium/components/printing/browser/print_manager_utils.cc +++ b/chromium/components/printing/browser/print_manager_utils.cc @@ -7,6 +7,7 @@ #include "components/printing/browser/print_composite_client.h" #include "components/printing/common/print_messages.h" #include "content/public/browser/site_isolation_policy.h" +#include "printing/mojom/print.mojom.h" #include "printing/print_settings.h" namespace printing { @@ -68,8 +69,8 @@ void RenderParamsFromPrintSettings(const PrintSettings& settings, params->title = settings.title(); params->url = settings.url(); params->printed_doc_type = IsOopifEnabled() && settings.is_modifiable() - ? SkiaDocumentType::MSKP - : SkiaDocumentType::PDF; + ? mojom::SkiaDocumentType::kMSKP + : mojom::SkiaDocumentType::kPDF; params->pages_per_sheet = settings.pages_per_sheet(); } diff --git a/chromium/components/printing/browser/service_sandbox_type.h b/chromium/components/printing/browser/service_sandbox_type.h new file mode 100644 index 00000000000..bc7bb1f146c --- /dev/null +++ b/chromium/components/printing/browser/service_sandbox_type.h @@ -0,0 +1,28 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_PRINTING_BROWSER_SERVICE_SANDBOX_TYPE_H_ +#define COMPONENTS_PRINTING_BROWSER_SERVICE_SANDBOX_TYPE_H_ + +#include "content/public/browser/sandbox_type.h" +#include "content/public/browser/service_process_host.h" + +// This file maps service classes to sandbox types. Services which +// require a non-utility sandbox can be added here. See +// ServiceProcessHost::Launch() for how these templates are consumed. + +// printing::mojom::PrintCompositor +namespace printing { +namespace mojom { +class PrintCompositor; +} +} // namespace printing + +template <> +inline content::SandboxType +content::GetServiceSandboxType<printing::mojom::PrintCompositor>() { + return content::SandboxType::kPrintCompositor; +} + +#endif // COMPONENTS_PRINTING_BROWSER_SERVICE_SANDBOX_TYPE_H_ diff --git a/chromium/components/printing/common/BUILD.gn b/chromium/components/printing/common/BUILD.gn index ce878a58e6d..386f3770eed 100644 --- a/chromium/components/printing/common/BUILD.gn +++ b/chromium/components/printing/common/BUILD.gn @@ -15,6 +15,7 @@ static_library("common") { ] deps = [ + ":mojo_interfaces", "//base", "//components/cloud_devices/common:common", "//ipc", diff --git a/chromium/components/printing/common/print.mojom b/chromium/components/printing/common/print.mojom index 408f5bc1e43..faccd2a6881 100644 --- a/chromium/components/printing/common/print.mojom +++ b/chromium/components/printing/common/print.mojom @@ -29,6 +29,32 @@ struct OptionsFromDocumentParams { DuplexMode duplex; }; +// Holds the printed content information. +// The printed content is in shared memory, and passed as a region. +// A map on out-of-process subframe contents is also included so the printed +// content can be composited as needed. +struct DidPrintContentParams { + // A shared memory region for the metafile data. + mojo_base.mojom.ReadOnlySharedMemoryRegion metafile_data_region; + // Content id to render frame proxy id mapping for out-of-process subframes. + map<uint32, int32> subframe_content_info; +}; + +// Parameters to describe the to-be-rendered preview document. +struct DidStartPreviewParams { + // Total page count for the rendered preview. (Not the number of pages the + // user selected to print.) + int32 page_count; + // The list of 0-based page numbers that will be rendered. + array<int32> pages_to_render; + // number of pages per sheet and should be greater or equal to 1. + int32 pages_per_sheet; + // Physical size of the page, including non-printable margins. + gfx.mojom.Size page_size; + // Scaling % to fit to page + int32 fit_to_page_scaling; +}; + // Interface implemented by a class that desires to render print documents for // Chrome print preview. interface PrintRenderer { @@ -54,6 +80,18 @@ interface PrintPreviewUI { // ensure correctness and |request_id| is the id for the preview request. [EnableIf=enable_print_preview] PrintPreviewFailed(int32 document_cookie, int32 request_id); + + // Tell the browser print preview was cancelled. |document_cookie| is the + // param to ensure correctness and |request_id| is the id for the preview + // request. + [EnableIf=enable_print_preview] + PrintPreviewCancelled(int32 document_cookie, int32 request_id); + + // Tell the browser print preview found the selected printer has invalid + // settings (which typically caused by disconnected network printer or + // printer driver is bogus). + [EnableIf=enable_print_preview] + PrinterSettingsInvalid(int32 document_cookie, int32 request_id); }; // Render process interface exposed to the browser to handle most of the @@ -88,8 +126,10 @@ interface PrintRenderFrame { [EnableIf=enable_print_preview] OnPrintPreviewDialogClosed(); - // Prints the content of an out-of-process subframe. - PrintFrameContent(PrintFrameContentParams params); + // Prints the content of an out-of-process subframe. Replies back to the + // browser the rendered subframe content that was requested. + PrintFrameContent(PrintFrameContentParams params) + => (int32 document_cookie, DidPrintContentParams params); // Tells the RenderFrame whether printing is enabled or not. SetPrintingEnabled(bool enabled); diff --git a/chromium/components/printing/common/print_messages.cc b/chromium/components/printing/common/print_messages.cc index 46b965863e5..1ae067c5268 100644 --- a/chromium/components/printing/common/print_messages.cc +++ b/chromium/components/printing/common/print_messages.cc @@ -66,6 +66,7 @@ namespace IPC { PrintMsg_Print_Params::PrintMsg_Print_Params() : margin_top(0), margin_left(0), + page_orientation(printing::mojom::PageOrientation::kUpright), scale_factor(1.0f), rasterize_pdf(false), document_cookie(0), @@ -74,11 +75,11 @@ PrintMsg_Print_Params::PrintMsg_Print_Params() preview_ui_id(-1), preview_request_id(0), is_first_request(false), - print_scaling_option(blink::kWebPrintScalingOptionSourceSize), + print_scaling_option(printing::mojom::PrintScalingOption::kSourceSize), print_to_pdf(false), display_header_footer(false), should_print_backgrounds(false), - printed_doc_type(printing::SkiaDocumentType::PDF), + printed_doc_type(printing::mojom::SkiaDocumentType::kPDF), prefer_css_page_size(false), pages_per_sheet(1) {} @@ -93,6 +94,7 @@ void PrintMsg_Print_Params::Reset() { printable_area = gfx::Rect(); margin_top = 0; margin_left = 0; + page_orientation = printing::mojom::PageOrientation::kUpright; dpi = gfx::Size(); scale_factor = 1.0f; rasterize_pdf = false; @@ -102,7 +104,7 @@ void PrintMsg_Print_Params::Reset() { preview_ui_id = -1; preview_request_id = 0; is_first_request = false; - print_scaling_option = blink::kWebPrintScalingOptionSourceSize; + print_scaling_option = printing::mojom::PrintScalingOption::kSourceSize; print_to_pdf = false; display_header_footer = false; title = base::string16(); @@ -110,7 +112,7 @@ void PrintMsg_Print_Params::Reset() { header_template = base::string16(); footer_template = base::string16(); should_print_backgrounds = false; - printed_doc_type = printing::SkiaDocumentType::PDF; + printed_doc_type = printing::mojom::SkiaDocumentType::kPDF; prefer_css_page_size = false; pages_per_sheet = 1; } diff --git a/chromium/components/printing/common/print_messages.h b/chromium/components/printing/common/print_messages.h index 18b238f14b7..6b2e5582053 100644 --- a/chromium/components/printing/common/print_messages.h +++ b/chromium/components/printing/common/print_messages.h @@ -14,14 +14,13 @@ #include "base/memory/read_only_shared_memory_region.h" #include "base/values.h" #include "build/build_config.h" +#include "components/printing/common/print.mojom.h" #include "components/printing/common/printing_param_traits_macros.h" #include "ipc/ipc_message_macros.h" #include "printing/buildflags/buildflags.h" -#include "printing/common/metafile_utils.h" +#include "printing/mojom/print.mojom.h" #include "printing/page_range.h" -#include "printing/page_size_margins.h" #include "printing/print_job_constants.h" -#include "third_party/blink/public/web/web_print_scaling_option.h" #include "ui/accessibility/ax_param_traits.h" #include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/rect.h" @@ -44,6 +43,7 @@ struct PrintMsg_Print_Params { gfx::Rect printable_area; int margin_top; int margin_left; + printing::mojom::PageOrientation page_orientation; gfx::Size dpi; double scale_factor; bool rasterize_pdf; @@ -53,7 +53,7 @@ struct PrintMsg_Print_Params { int32_t preview_ui_id; int preview_request_id; bool is_first_request; - blink::WebPrintScalingOption print_scaling_option; + printing::mojom::PrintScalingOption print_scaling_option; bool print_to_pdf; bool display_header_footer; base::string16 title; @@ -61,7 +61,7 @@ struct PrintMsg_Print_Params { base::string16 header_template; base::string16 footer_template; bool should_print_backgrounds; - printing::SkiaDocumentType printed_doc_type; + printing::mojom::SkiaDocumentType printed_doc_type; bool prefer_css_page_size; int pages_per_sheet; }; @@ -103,10 +103,10 @@ struct PrintHostMsg_PreviewIds { #define IPC_MESSAGE_START PrintMsgStart -IPC_ENUM_TRAITS_MAX_VALUE(blink::WebPrintScalingOption, - blink::kWebPrintScalingOptionLast) -IPC_ENUM_TRAITS_MAX_VALUE(printing::SkiaDocumentType, - printing::SkiaDocumentType::MAX) +IPC_ENUM_TRAITS_MAX_VALUE(printing::mojom::PrintScalingOption, + printing::mojom::PrintScalingOption::kMaxValue) +IPC_ENUM_TRAITS_MAX_VALUE(printing::mojom::SkiaDocumentType, + printing::mojom::SkiaDocumentType::kMaxValue) // Parameters for a render request. IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params) @@ -212,7 +212,7 @@ IPC_STRUCT_TRAITS_BEGIN(PrintHostMsg_PreviewIds) IPC_STRUCT_TRAITS_END() #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) -IPC_STRUCT_TRAITS_BEGIN(printing::PageSizeMargins) +IPC_STRUCT_TRAITS_BEGIN(printing::mojom::PageSizeMargins) IPC_STRUCT_TRAITS_MEMBER(content_width) IPC_STRUCT_TRAITS_MEMBER(content_height) IPC_STRUCT_TRAITS_MEMBER(margin_left) @@ -234,38 +234,38 @@ IPC_STRUCT_TRAITS_END() // The printed content is in shared memory, and passed as a region. // A map on out-of-process subframe contents is also included so the printed // content can be composited as needed. -IPC_STRUCT_BEGIN(PrintHostMsg_DidPrintContent_Params) +IPC_STRUCT_TRAITS_BEGIN(printing::mojom::DidPrintContentParams) // A shared memory region for the metafile data. - IPC_STRUCT_MEMBER(base::ReadOnlySharedMemoryRegion, metafile_data_region) + IPC_STRUCT_TRAITS_MEMBER(metafile_data_region) // Content id to render frame proxy id mapping for out-of-process subframes. - IPC_STRUCT_MEMBER(printing::ContentToProxyIdMap, subframe_content_info) -IPC_STRUCT_END() + IPC_STRUCT_TRAITS_MEMBER(subframe_content_info) +IPC_STRUCT_TRAITS_END() #if BUILDFLAG(ENABLE_PRINT_PREVIEW) // Parameters to describe the to-be-rendered preview document. -IPC_STRUCT_BEGIN(PrintHostMsg_DidStartPreview_Params) +IPC_STRUCT_TRAITS_BEGIN(printing::mojom::DidStartPreviewParams) // Total page count for the rendered preview. (Not the number of pages the // user selected to print.) - IPC_STRUCT_MEMBER(int, page_count) + IPC_STRUCT_TRAITS_MEMBER(page_count) // The list of 0-based page numbers that will be rendered. - IPC_STRUCT_MEMBER(std::vector<int>, pages_to_render) + IPC_STRUCT_TRAITS_MEMBER(pages_to_render) // number of pages per sheet and should be greater or equal to 1. - IPC_STRUCT_MEMBER(int, pages_per_sheet) + IPC_STRUCT_TRAITS_MEMBER(pages_per_sheet) // Physical size of the page, including non-printable margins. - IPC_STRUCT_MEMBER(gfx::Size, page_size) + IPC_STRUCT_TRAITS_MEMBER(page_size) // Scaling % to fit to page - IPC_STRUCT_MEMBER(int, fit_to_page_scaling) -IPC_STRUCT_END() + IPC_STRUCT_TRAITS_MEMBER(fit_to_page_scaling) +IPC_STRUCT_TRAITS_END() // Parameters to describe a rendered preview page. IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewPage_Params) // Page's content including metafile data and subframe info. - IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content) + IPC_STRUCT_MEMBER(printing::mojom::DidPrintContentParams, content) // |page_number| is zero-based and should not be negative. IPC_STRUCT_MEMBER(int, page_number) @@ -277,7 +277,7 @@ IPC_STRUCT_END() // Parameters to describe the final rendered preview document. IPC_STRUCT_BEGIN(PrintHostMsg_DidPreviewDocument_Params) // Document's content including metafile data and subframe info. - IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content) + IPC_STRUCT_MEMBER(printing::mojom::DidPrintContentParams, content) // Cookie for the document to ensure correctness. IPC_STRUCT_MEMBER(int, document_cookie) @@ -290,7 +290,7 @@ IPC_STRUCT_END() // Parameters to describe a rendered page. IPC_STRUCT_BEGIN(PrintHostMsg_DidPrintDocument_Params) // Document's content including metafile data and subframe info. - IPC_STRUCT_MEMBER(PrintHostMsg_DidPrintContent_Params, content) + IPC_STRUCT_MEMBER(printing::mojom::DidPrintContentParams, content) // Cookie for the document to ensure correctness. IPC_STRUCT_MEMBER(int, document_cookie) @@ -342,12 +342,6 @@ IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_DidPrintDocument, /* page content */, bool /* completed */) -// Sends back to the browser the rendered subframe content that was -// requested by a PrintMsg_PrintFrameContent message. -IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPrintFrameContent, - int /* rendered document cookie */, - PrintHostMsg_DidPrintContent_Params) - #if BUILDFLAG(ENABLE_TAGGED_PDF) // Sends the accessibility tree corresponding to a document being // printed, needed for a tagged (accessible) PDF. @@ -384,7 +378,7 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_RequestPrintPreview, // Notify the browser the about the to-be-rendered print preview document. IPC_MESSAGE_ROUTED2(PrintHostMsg_DidStartPreview, - PrintHostMsg_DidStartPreview_Params /* params */, + printing::mojom::DidStartPreviewParams /* params */, PrintHostMsg_PreviewIds /* ids */) // Notify the browser of preparing to print the document, for cases where @@ -399,11 +393,12 @@ IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPrepareDocumentForPreview, // |printable_area_in_points| Specifies the printable area in points. // |has_custom_page_size_style| is true when the printing frame has a custom // page size css otherwise false. -IPC_MESSAGE_ROUTED4(PrintHostMsg_DidGetDefaultPageLayout, - printing::PageSizeMargins /* page layout in points */, - gfx::Rect /* printable area in points */, - bool /* has custom page size style */, - PrintHostMsg_PreviewIds /* ids */) +IPC_MESSAGE_ROUTED4( + PrintHostMsg_DidGetDefaultPageLayout, + printing::mojom::PageSizeMargins /* page layout in points */, + gfx::Rect /* printable area in points */, + bool /* has custom page size style */, + PrintHostMsg_PreviewIds /* ids */) // Notify the browser a print preview page has been rendered. IPC_MESSAGE_ROUTED2(PrintHostMsg_DidPreviewPage, @@ -431,18 +426,6 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_PrintingFailed, int /* document cookie */) #if BUILDFLAG(ENABLE_PRINT_PREVIEW) -// Tell the browser print preview was cancelled. -IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewCancelled, - int /* document cookie */, - PrintHostMsg_PreviewIds /* ids */) - -// Tell the browser print preview found the selected printer has invalid -// settings (which typically caused by disconnected network printer or printer -// driver is bogus). -IPC_MESSAGE_ROUTED2(PrintHostMsg_PrintPreviewInvalidPrinterSettings, - int /* document cookie */, - PrintHostMsg_PreviewIds /* ids */) - // Run a nested run loop in the renderer until print preview for // window.print() finishes. IPC_SYNC_MESSAGE_ROUTED0_0(PrintHostMsg_SetupScriptedPrintPreview) diff --git a/chromium/components/printing/renderer/print_render_frame_helper.cc b/chromium/components/printing/renderer/print_render_frame_helper.cc index 8c471289dc7..3797c58b291 100644 --- a/chromium/components/printing/renderer/print_render_frame_helper.cc +++ b/chromium/components/printing/renderer/print_render_frame_helper.cc @@ -31,6 +31,7 @@ #include "base/trace_event/trace_event.h" #include "build/build_config.h" #include "components/grit/components_resources.h" +#include "components/printing/common/print.mojom.h" #include "components/printing/common/print_messages.h" #include "content/public/common/web_preferences.h" #include "content/public/renderer/render_frame.h" @@ -41,9 +42,11 @@ #include "net/base/escape.h" #include "printing/buildflags/buildflags.h" #include "printing/metafile_skia.h" +#include "printing/mojom/print.mojom.h" #include "printing/printing_features.h" #include "printing/units.h" #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h" +#include "third_party/blink/public/common/css/page_orientation.h" #include "third_party/blink/public/mojom/frame/frame_owner_element_type.mojom.h" #include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/web_data.h" @@ -61,8 +64,6 @@ #include "third_party/blink/public/web/web_local_frame_client.h" #include "third_party/blink/public/web/web_navigation_control.h" #include "third_party/blink/public/web/web_plugin.h" -#include "third_party/blink/public/web/web_plugin_container.h" -#include "third_party/blink/public/web/web_plugin_document.h" #include "third_party/blink/public/web/web_print_page_description.h" #include "third_party/blink/public/web/web_print_params.h" #include "third_party/blink/public/web/web_print_preset_options.h" @@ -140,9 +141,21 @@ bool PrintMsg_Print_Params_IsValid(const PrintMsg_Print_Params& params) { } // Helper function to check for fit to page -bool IsWebPrintScalingOptionFitToPage(const PrintMsg_Print_Params& params) { +bool IsPrintScalingOptionFitToPage(const PrintMsg_Print_Params& params) { return params.print_scaling_option == - blink::kWebPrintScalingOptionFitToPrintableArea; + mojom::PrintScalingOption::kFitToPrintableArea; +} + +mojom::PageOrientation FromBlinkPageOrientation( + blink::PageOrientation orientation) { + switch (orientation) { + case blink::PageOrientation::kUpright: + return printing::mojom::PageOrientation::kUpright; + case blink::PageOrientation::kRotateLeft: + return printing::mojom::PageOrientation::kRotateLeft; + case blink::PageOrientation::kRotateRight: + return printing::mojom::PageOrientation::kRotateRight; + } } PrintMsg_Print_Params GetCssPrintParams( @@ -185,6 +198,9 @@ PrintMsg_Print_Params GetCssPrintParams( return page_css_params; } + page_css_params.page_orientation = + FromBlinkPageOrientation(description.orientation); + page_css_params.page_size = gfx::Size(ConvertUnit(description.size.Width(), kPixelsPerInch, dpi), ConvertUnit(description.size.Height(), kPixelsPerInch, dpi)); @@ -239,8 +255,8 @@ double FitPrintParamsToPage(const PrintMsg_Print_Params& page_params, void CalculatePageLayoutFromPrintParams( const PrintMsg_Print_Params& params, double scale_factor, - PageSizeMargins* page_layout_in_points) { - bool fit_to_page = IsWebPrintScalingOptionFitToPage(params); + mojom::PageSizeMargins* page_layout_in_points) { + bool fit_to_page = IsPrintScalingOptionFitToPage(params); int dpi = GetDPI(params); int content_width = params.content_size.width(); int content_height = params.content_size.height(); @@ -338,28 +354,14 @@ void ComputeWebKitPrintParamsInDesiredDpi( webkit_print_params->pages_per_sheet = print_params.pages_per_sheet; } -blink::WebPlugin* GetPlugin(const blink::WebLocalFrame* frame) { - return frame->GetDocument().IsPluginDocument() - ? frame->GetDocument().To<blink::WebPluginDocument>().Plugin() - : nullptr; -} - -bool IsPrintingNodeOrPdfFrame(const blink::WebLocalFrame* frame, +bool IsPrintingNodeOrPdfFrame(blink::WebLocalFrame* frame, const blink::WebNode& node) { - if (!node.IsNull()) - return true; - blink::WebPlugin* plugin = GetPlugin(frame); + blink::WebPlugin* plugin = frame->GetPluginToPrint(node); return plugin && plugin->SupportsPaginatedPrint(); } bool IsPrintingPdf(blink::WebLocalFrame* frame, const blink::WebNode& node) { - blink::WebPlugin* plugin; - if (node.IsNull()) { - plugin = GetPlugin(frame); - } else { - blink::WebPluginContainer* plugin_container = node.PluginContainer(); - plugin = plugin_container ? plugin_container->Plugin() : nullptr; - } + blink::WebPlugin* plugin = frame->GetPluginToPrint(node); return plugin && plugin->IsPdfPlugin(); } @@ -466,29 +468,29 @@ ScalingType ScalingTypeFromJobSettings( // option is disabled for initiator renderer plugin. // // In all other cases, we scale the source page to fit the printable area. -blink::WebPrintScalingOption GetPrintScalingOption( +mojom::PrintScalingOption GetPrintScalingOption( blink::WebLocalFrame* frame, const blink::WebNode& node, bool source_is_html, const base::DictionaryValue& job_settings, const PrintMsg_Print_Params& params) { if (params.print_to_pdf) - return blink::kWebPrintScalingOptionSourceSize; + return mojom::PrintScalingOption::kSourceSize; if (!source_is_html) { ScalingType scaling_type = ScalingTypeFromJobSettings(job_settings); // The following conditions are ordered for an optimization that avoids // calling PDFShouldDisableScaling(), which has to make a call using PPAPI. if (scaling_type == DEFAULT || scaling_type == CUSTOM) - return blink::kWebPrintScalingOptionNone; + return mojom::PrintScalingOption::kNone; if (params.is_first_request && PDFShouldDisableScaling(frame, node, params, true)) { - return blink::kWebPrintScalingOptionNone; + return mojom::PrintScalingOption::kNone; } if (scaling_type == FIT_TO_PAPER) - return blink::kWebPrintScalingOptionFitToPaper; + return mojom::PrintScalingOption::kFitToPaper; } - return blink::kWebPrintScalingOptionFitToPrintableArea; + return mojom::PrintScalingOption::kFitToPrintableArea; } #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) @@ -572,9 +574,8 @@ PrintMsg_Print_Params CalculatePrintParamsForCss( return result_params; } -bool CopyMetafileDataToReadOnlySharedMem( - const MetafileSkia& metafile, - PrintHostMsg_DidPrintContent_Params* params) { +bool CopyMetafileDataToReadOnlySharedMem(const MetafileSkia& metafile, + mojom::DidPrintContentParams* params) { uint32_t buf_size = metafile.GetDataSize(); if (buf_size == 0) return false; @@ -653,7 +654,7 @@ void PrintRenderFrameHelper::PrintHeaderAndFooter( int total_pages, const blink::WebLocalFrame& source_frame, float webkit_scale_factor, - const PageSizeMargins& page_layout, + const mojom::PageSizeMargins& page_layout, const PrintMsg_Print_Params& params) { cc::PaintCanvasAutoRestore auto_restore(canvas, true); canvas->scale(1 / webkit_scale_factor, 1 / webkit_scale_factor); @@ -845,7 +846,7 @@ PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( bool source_is_pdf = IsPrintingNodeOrPdfFrame(frame, node_to_print_); if (!should_print_selection_only_) { bool fit_to_page = - ignore_css_margins && IsWebPrintScalingOptionFitToPage(print_params); + ignore_css_margins && IsPrintScalingOptionFitToPage(print_params); ComputeWebKitPrintParamsInDesiredDpi(params, source_is_pdf, &web_print_params_); frame->PrintBegin(web_print_params_, node_to_print_); @@ -1219,8 +1220,9 @@ void PrintRenderFrameHelper::PrintForSystemDialog() { void PrintRenderFrameHelper::SetPrintPreviewUI( mojo::PendingAssociatedRemote<mojom::PrintPreviewUI> preview) { preview_ui_.Bind(std::move(preview)); - preview_ui_.set_disconnect_handler(base::BindOnce( - &PrintRenderFrameHelper::OnPreviewDisconnect, base::Unretained(this))); + preview_ui_.set_disconnect_handler( + base::BindOnce(&PrintRenderFrameHelper::OnPreviewDisconnect, + weak_ptr_factory_.GetWeakPtr())); } void PrintRenderFrameHelper::InitiatePrintPreview( @@ -1257,10 +1259,10 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) { print_preview_context_.OnPrintPreview(); - base::UmaHistogramEnumeration(print_preview_context_.IsForArc() - ? "Arc.PrintPreview.PreviewEvent" - : "PrintPreview.PreviewEvent", - PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); + if (print_preview_context_.IsForArc()) { + base::UmaHistogramEnumeration("Arc.PrintPreview.PreviewEvent", + PREVIEW_EVENT_REQUESTED, PREVIEW_EVENT_MAX); + } if (!print_preview_context_.source_frame()) { DidFinishPrinting(FAIL_PREVIEW); @@ -1288,7 +1290,7 @@ void PrintRenderFrameHelper::PrintPreview(base::Value settings) { if (print_pages_params_->params.is_first_request && !print_preview_context_.IsModifiable()) { mojom::OptionsFromDocumentParamsPtr options = SetOptionsFromPdfDocument(); - if (options) { + if (options && preview_ui_) { preview_ui_->SetOptionsFromDocument( std::move(options), print_pages_params_->params.preview_request_id); } @@ -1309,7 +1311,8 @@ void PrintRenderFrameHelper::OnPrintPreviewDialogClosed() { #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) void PrintRenderFrameHelper::PrintFrameContent( - mojom::PrintFrameContentParamsPtr params) { + mojom::PrintFrameContentParamsPtr params, + PrintFrameContentCallback callback) { ScopedIPC scoped_ipc(weak_ptr_factory_.GetWeakPtr()); if (ipc_nesting_level_ > 1) return; @@ -1326,12 +1329,13 @@ void PrintRenderFrameHelper::PrintFrameContent( if (!weak_this) return; - MetafileSkia metafile(SkiaDocumentType::MSKP, params->document_cookie); + MetafileSkia metafile(mojom::SkiaDocumentType::kMSKP, + params->document_cookie); gfx::Size area_size = params->printable_area.size(); // Since GetVectorCanvasForNewPage() starts a new recording, it will return // a valid canvas. - cc::PaintCanvas* canvas = - metafile.GetVectorCanvasForNewPage(area_size, gfx::Rect(area_size), 1.0f); + cc::PaintCanvas* canvas = metafile.GetVectorCanvasForNewPage( + area_size, gfx::Rect(area_size), 1.0f, mojom::PageOrientation::kUpright); DCHECK(canvas); canvas->SetPrintingMetafile(&metafile); @@ -1361,14 +1365,16 @@ void PrintRenderFrameHelper::PrintFrameContent( metafile.FinishFrameContent(); // Send the printed result back. - PrintHostMsg_DidPrintContent_Params printed_frame_params; - if (!CopyMetafileDataToReadOnlySharedMem(metafile, &printed_frame_params)) { + mojom::DidPrintContentParamsPtr printed_frame_params = + mojom::DidPrintContentParams::New(); + if (!CopyMetafileDataToReadOnlySharedMem(metafile, + printed_frame_params.get())) { DLOG(ERROR) << "CopyMetafileDataToSharedMem failed"; return; } - Send(new PrintHostMsg_DidPrintFrameContent( - routing_id(), params->document_cookie, printed_frame_params)); + std::move(callback).Run(params->document_cookie, + std::move(printed_frame_params)); if (!render_frame_gone_) frame->DispatchAfterPrintEvent(); @@ -1393,7 +1399,7 @@ void PrintRenderFrameHelper::PrintNodeUnderContextMenu() { } void PrintRenderFrameHelper::GetPageSizeAndContentAreaFromPageLayout( - const PageSizeMargins& page_layout_in_points, + const mojom::PageSizeMargins& page_layout_in_points, gfx::Size* page_size, gfx::Rect* content_area) { *page_size = gfx::Size( @@ -1463,24 +1469,25 @@ PrintRenderFrameHelper::CreatePreviewDocument() { if (!print_pages_params_ || CheckForCancel()) return CREATE_FAIL; - base::UmaHistogramEnumeration( - print_preview_context_.IsForArc() ? "Arc.PrintPreview.PreviewEvent" - : "PrintPreview.PreviewEvent", - PREVIEW_EVENT_CREATE_DOCUMENT, PREVIEW_EVENT_MAX); + if (print_preview_context_.IsForArc()) { + base::UmaHistogramEnumeration("Arc.PrintPreview.PreviewEvent", + PREVIEW_EVENT_CREATE_DOCUMENT, + PREVIEW_EVENT_MAX); + } const PrintMsg_Print_Params& print_params = print_pages_params_->params; const std::vector<int>& pages = print_pages_params_->pages; bool require_document_metafile = print_renderer_ || - print_params.printed_doc_type != SkiaDocumentType::MSKP; + print_params.printed_doc_type != mojom::SkiaDocumentType::kMSKP; if (!print_preview_context_.CreatePreviewDocument( std::move(prep_frame_view_), pages, print_params.printed_doc_type, print_params.document_cookie, require_document_metafile)) { return CREATE_FAIL; } - PageSizeMargins default_page_layout; + mojom::PageSizeMargins default_page_layout; double scale_factor = GetScaleFactor(print_params.scale_factor, !print_preview_context_.IsModifiable()); @@ -1506,14 +1513,15 @@ PrintRenderFrameHelper::CreatePreviewDocument() { routing_id(), default_page_layout, printable_area_in_points, has_page_size_style, ids)); - PrintHostMsg_DidStartPreview_Params params; - params.page_count = print_preview_context_.total_page_count(); - params.pages_to_render = print_preview_context_.pages_to_render(); - params.pages_per_sheet = print_params.pages_per_sheet; - params.page_size = GetPdfPageSize(print_params.page_size, dpi); - params.fit_to_page_scaling = - GetFitToPageScaleFactor(printable_area_in_points); - Send(new PrintHostMsg_DidStartPreview(routing_id(), params, ids)); + Send(new PrintHostMsg_DidStartPreview( + routing_id(), + mojom::DidStartPreviewParams( + print_preview_context_.total_page_count(), + print_preview_context_.pages_to_render(), + print_params.pages_per_sheet, + GetPdfPageSize(print_params.page_size, dpi), + GetFitToPageScaleFactor(printable_area_in_points)), + ids)); if (CheckForCancel()) return CREATE_FAIL; @@ -1529,7 +1537,8 @@ PrintRenderFrameHelper::CreatePreviewDocument() { return CREATE_IN_PROGRESS; } - if (print_pages_params_->params.printed_doc_type == SkiaDocumentType::MSKP) { + if (print_pages_params_->params.printed_doc_type == + mojom::SkiaDocumentType::kMSKP) { // Want modifiable content of MSKP type to be collected into a document // during individual page preview generation (to avoid separate document // version for composition), notify to prepare to do this collection. @@ -1540,6 +1549,15 @@ PrintRenderFrameHelper::CreatePreviewDocument() { while (!print_preview_context_.IsFinalPageRendered()) { int page_number = print_preview_context_.GetNextPageNumber(); DCHECK_GE(page_number, 0); + + blink::WebLocalFrame* frame = print_preview_context_.source_frame(); + if (frame) { + blink::WebPrintPageDescription description; + frame->GetPageDescription(page_number, &description); + print_pages_params_->params.page_orientation = + FromBlinkPageOrientation(description.orientation); + } + if (!RenderPreviewPage(page_number)) return CREATE_FAIL; @@ -1790,7 +1808,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, { // PrintHostMsg_ScriptedPrint in GetPrintSettingsFromUser() will reset // |print_scaling_option|, so save the value here and restore it afterwards. - blink::WebPrintScalingOption scaling_option = + mojom::PrintScalingOption scaling_option = print_pages_params_->params.print_scaling_option; PrintMsg_PrintPages_Params print_settings; @@ -1803,7 +1821,7 @@ void PrintRenderFrameHelper::Print(blink::WebLocalFrame* frame, print_settings.params.print_scaling_option = print_settings.params.prefer_css_page_size - ? blink::kWebPrintScalingOptionSourceSize + ? mojom::PrintScalingOption::kSourceSize : scaling_option; SetPrintPagesParams(print_settings); if (print_settings.params.dpi.IsEmpty() || @@ -1850,17 +1868,18 @@ void PrintRenderFrameHelper::DidFinishPrinting(PrintingResult result) { if (!is_print_ready_metafile_sent_) { if (notify_browser_of_print_failure_) { LOG(ERROR) << "CreatePreviewDocument failed"; - preview_ui_->PrintPreviewFailed(cookie, ids.request_id); + if (preview_ui_) + preview_ui_->PrintPreviewFailed(cookie, ids.request_id); } else { - Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie, - ids)); + if (preview_ui_) + preview_ui_->PrintPreviewCancelled(cookie, ids.request_id); } } print_preview_context_.Failed(notify_browser_of_print_failure_); break; case INVALID_SETTINGS: - Send(new PrintHostMsg_PrintPreviewInvalidPrinterSettings(routing_id(), - cookie, ids)); + if (preview_ui_) + preview_ui_->PrinterSettingsInvalid(cookie, ids.request_id); print_preview_context_.Failed(false); break; #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) @@ -1933,8 +1952,7 @@ bool PrintRenderFrameHelper::PrintPagesNative(blink::WebLocalFrame* frame, std::unique_ptr<content::AXTreeSnapshotter> snapshotter; if (delegate_->ShouldGenerateTaggedPDF()) { snapshotter = render_frame()->CreateAXTreeSnapshotter(); - snapshotter->Snapshot(ui::kAXModeComplete, 0, - &metafile.accessibility_tree()); + snapshotter->Snapshot(ui::AXMode::kPDF, 0, &metafile.accessibility_tree()); } PrintHostMsg_DidPrintDocument_Params page_params; @@ -1986,11 +2004,11 @@ void PrintRenderFrameHelper::ComputePageLayoutInPointsForCss( const PrintMsg_Print_Params& page_params, bool ignore_css_margins, double* scale_factor, - PageSizeMargins* page_layout_in_points) { + mojom::PageSizeMargins* page_layout_in_points) { double input_scale_factor = *scale_factor; PrintMsg_Print_Params params = CalculatePrintParamsForCss( frame, page_index, page_params, ignore_css_margins, - IsWebPrintScalingOptionFitToPage(page_params), scale_factor); + IsPrintScalingOptionFitToPage(page_params), scale_factor); CalculatePageLayoutFromPrintParams(params, input_scale_factor, page_layout_in_points); } @@ -2047,8 +2065,8 @@ bool PrintRenderFrameHelper::InitPrintSettings(bool fit_to_paper_size) { settings.pages.clear(); settings.params.print_scaling_option = - fit_to_paper_size ? blink::kWebPrintScalingOptionFitToPrintableArea - : blink::kWebPrintScalingOptionSourceSize; + fit_to_paper_size ? mojom::PrintScalingOption::kFitToPrintableArea + : mojom::PrintScalingOption::kSourceSize; SetPrintPagesParams(settings); return result; @@ -2227,7 +2245,7 @@ void PrintRenderFrameHelper::PrintPageInternal( // scaling back. Windows uses |page_size_in_dpi| for the actual page size // so requires an accurate value. gfx::Size original_page_size = params.page_size; - PageSizeMargins page_layout_in_points; + mojom::PageSizeMargins page_layout_in_points; ComputePageLayoutInPointsForCss(frame, page_number, params, ignore_css_margins_, &css_scale_factor, &page_layout_in_points); @@ -2259,7 +2277,7 @@ void PrintRenderFrameHelper::PrintPageInternal( #endif cc::PaintCanvas* canvas = metafile->GetVectorCanvasForNewPage( - page_size, canvas_area, final_scale_factor); + page_size, canvas_area, final_scale_factor, params.page_orientation); if (!canvas) return; @@ -2329,7 +2347,7 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) { // 2. PrintHostMsg_ShowScriptedPrintPreview shows preview once the // document has been loaded. is_scripted_preview_delayed_ = true; - if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) { + if (is_loading_ && print_preview_context_.IsPlugin()) { // Wait for DidStopLoading. Plugins may not know the correct // |is_modifiable| value until they are fully loaded, which occurs when // DidStopLoading() is called. Defer showing the preview until then. @@ -2356,7 +2374,7 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) { // Wait for DidStopLoading. Continuing with this function while // |is_loading_| is true will cause print preview to hang when try to // print a PDF document. - if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) { + if (is_loading_ && print_preview_context_.IsPlugin()) { on_stop_loading_closure_ = base::BindOnce(&PrintRenderFrameHelper::RequestPrintPreview, weak_ptr_factory_.GetWeakPtr(), type); @@ -2367,12 +2385,12 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) { } case PRINT_PREVIEW_USER_INITIATED_SELECTION: { DCHECK(has_selection); - DCHECK(!GetPlugin(print_preview_context_.source_frame())); + DCHECK(!print_preview_context_.IsPlugin()); params.selection_only = has_selection; break; } case PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE: { - if (is_loading_ && GetPlugin(print_preview_context_.source_frame())) { + if (is_loading_ && print_preview_context_.IsPlugin()) { on_stop_loading_closure_ = base::BindOnce(&PrintRenderFrameHelper::RequestPrintPreview, weak_ptr_factory_.GetWeakPtr(), type); @@ -2388,10 +2406,10 @@ void PrintRenderFrameHelper::RequestPrintPreview(PrintPreviewRequestType type) { } } - base::UmaHistogramEnumeration(print_preview_context_.IsForArc() - ? "Arc.PrintPreview.PreviewEvent" - : "PrintPreview.PreviewEvent", - PREVIEW_EVENT_INITIATED, PREVIEW_EVENT_MAX); + if (print_preview_context_.IsForArc()) { + base::UmaHistogramEnumeration("Arc.PrintPreview.PreviewEvent", + PREVIEW_EVENT_INITIATED, PREVIEW_EVENT_MAX); + } Send(new PrintHostMsg_RequestPrintPreview(routing_id(), params)); } @@ -2427,7 +2445,7 @@ bool PrintRenderFrameHelper::PreviewPageRendered( // http://crbug.com/1039817 if (snapshotter_ && page_number == 0) { ui::AXTreeUpdate accessibility_tree; - snapshotter_->Snapshot(ui::kAXModeComplete, 0, &accessibility_tree); + snapshotter_->Snapshot(ui::AXMode::kPDF, 0, &accessibility_tree); Send(new PrintHostMsg_AccessibilityTree( routing_id(), print_pages_params_->params.document_cookie, accessibility_tree)); @@ -2469,8 +2487,7 @@ void PrintRenderFrameHelper::PrintPreviewContext::InitWithFrame( state_ = INITIALIZED; source_frame_.Reset(web_frame); source_node_.Reset(); - CalculateIsModifiable(); - CalculateIsPdf(); + CalculatePluginAttributes(); } void PrintRenderFrameHelper::PrintPreviewContext::InitWithNode( @@ -2481,8 +2498,7 @@ void PrintRenderFrameHelper::PrintPreviewContext::InitWithNode( state_ = INITIALIZED; source_frame_.Reset(web_node.GetDocument().GetFrame()); source_node_ = web_node; - CalculateIsModifiable(); - CalculateIsPdf(); + CalculatePluginAttributes(); } void PrintRenderFrameHelper::PrintPreviewContext::OnPrintPreview() { @@ -2493,7 +2509,7 @@ void PrintRenderFrameHelper::PrintPreviewContext::OnPrintPreview() { bool PrintRenderFrameHelper::PrintPreviewContext::CreatePreviewDocument( std::unique_ptr<PrepareFrameAndViewForPrint> prepared_frame, const std::vector<int>& pages, - SkiaDocumentType doc_type, + mojom::SkiaDocumentType doc_type, int document_cookie, bool require_document_metafile) { DCHECK_EQ(INITIALIZED, state_); @@ -2625,6 +2641,11 @@ bool PrintRenderFrameHelper::PrintPreviewContext::IsForArc() const { return is_for_arc_; } +bool PrintRenderFrameHelper::PrintPreviewContext::IsPlugin() const { + DCHECK(state_ != UNINITIALIZED); + return is_plugin_; +} + bool PrintRenderFrameHelper::PrintPreviewContext::IsModifiable() const { DCHECK(state_ != UNINITIALIZED); return is_modifiable_; @@ -2715,11 +2736,9 @@ void PrintRenderFrameHelper::PrintPreviewContext::ClearContext() { error_ = PREVIEW_ERROR_NONE; } -void PrintRenderFrameHelper::PrintPreviewContext::CalculateIsModifiable() { +void PrintRenderFrameHelper::PrintPreviewContext::CalculatePluginAttributes() { + is_plugin_ = !!source_frame()->GetPluginToPrint(source_node_); is_modifiable_ = !IsPrintingNodeOrPdfFrame(source_frame(), source_node_); -} - -void PrintRenderFrameHelper::PrintPreviewContext::CalculateIsPdf() { is_pdf_ = IsPrintingPdf(source_frame(), source_node_); } diff --git a/chromium/components/printing/renderer/print_render_frame_helper.h b/chromium/components/printing/renderer/print_render_frame_helper.h index 23f8c4a97a7..e6d759d06c0 100644 --- a/chromium/components/printing/renderer/print_render_frame_helper.h +++ b/chromium/components/printing/renderer/print_render_frame_helper.h @@ -25,6 +25,7 @@ #include "mojo/public/cpp/bindings/pending_associated_remote.h" #include "printing/buildflags/buildflags.h" #include "printing/common/metafile_utils.h" +#include "printing/mojom/print.mojom-forward.h" #include "third_party/blink/public/web/web_node.h" #include "third_party/blink/public/web/web_print_params.h" #include "ui/gfx/geometry/size.h" @@ -59,7 +60,6 @@ class AXTreeSnapshotter; namespace printing { -struct PageSizeMargins; class MetafileSkia; class PrepareFrameAndViewForPrint; @@ -236,7 +236,8 @@ class PrintRenderFrameHelper void PrintPreview(base::Value settings) override; void OnPrintPreviewDialogClosed() override; #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) - void PrintFrameContent(mojom::PrintFrameContentParamsPtr params) override; + void PrintFrameContent(mojom::PrintFrameContentParamsPtr params, + PrintFrameContentCallback callback) override; void PrintingDone(bool success) override; void SetPrintingEnabled(bool enabled) override; void PrintNodeUnderContextMenu() override; @@ -244,7 +245,7 @@ class PrintRenderFrameHelper // Get |page_size| and |content_area| information from // |page_layout_in_points|. void GetPageSizeAndContentAreaFromPageLayout( - const PageSizeMargins& page_layout_in_points, + const mojom::PageSizeMargins& page_layout_in_points, gfx::Size* page_size, gfx::Rect* content_area); @@ -375,7 +376,7 @@ class PrintRenderFrameHelper const PrintMsg_Print_Params& default_params, bool ignore_css_margins, double* scale_factor, - PageSizeMargins* page_layout_in_points); + mojom::PageSizeMargins* page_layout_in_points); // Return an array of pages to print given the print |params| and an expected // |page_count|. Page numbers are zero-based. @@ -385,13 +386,14 @@ class PrintRenderFrameHelper // Given the |device| and |canvas| to draw on, prints the appropriate headers // and footers using strings from |header_footer_info| on to the canvas. - static void PrintHeaderAndFooter(cc::PaintCanvas* canvas, - int page_number, - int total_pages, - const blink::WebLocalFrame& source_frame, - float webkit_scale_factor, - const PageSizeMargins& page_layout_in_points, - const PrintMsg_Print_Params& params); + static void PrintHeaderAndFooter( + cc::PaintCanvas* canvas, + int page_number, + int total_pages, + const blink::WebLocalFrame& source_frame, + float webkit_scale_factor, + const mojom::PageSizeMargins& page_layout_in_points, + const PrintMsg_Print_Params& params); // Script Initiated Printing ------------------------------------------------ @@ -479,7 +481,7 @@ class PrintRenderFrameHelper bool CreatePreviewDocument( std::unique_ptr<PrepareFrameAndViewForPrint> prepared_frame, const std::vector<int>& pages, - SkiaDocumentType doc_type, + mojom::SkiaDocumentType doc_type, int document_cookie, bool require_document_metafile); @@ -507,6 +509,7 @@ class PrintRenderFrameHelper int GetNextPageNumber(); bool IsRendering() const; bool IsForArc() const; + bool IsPlugin() const; bool IsModifiable() const; bool IsPdf() const; bool HasSelection(); @@ -547,9 +550,7 @@ class PrintRenderFrameHelper // Reset some of the internal rendering context. void ClearContext(); - void CalculateIsModifiable(); - - void CalculateIsPdf(); + void CalculatePluginAttributes(); // Specifies what to render for print preview. FrameReference source_frame_; @@ -569,6 +570,9 @@ class PrintRenderFrameHelper // List of page indices that need to be rendered. std::vector<int> pages_to_render_; + // True, if the document source is a plugin. + bool is_plugin_ = false; + // True, if the document source is modifiable. e.g. HTML and not PDF. bool is_modifiable_ = true; diff --git a/chromium/components/printing/renderer/print_render_frame_helper_mac.mm b/chromium/components/printing/renderer/print_render_frame_helper_mac.mm index b596fa7609e..7ff22e196b9 100644 --- a/chromium/components/printing/renderer/print_render_frame_helper_mac.mm +++ b/chromium/components/printing/renderer/print_render_frame_helper_mac.mm @@ -15,7 +15,7 @@ #include "components/printing/common/print_messages.h" #include "printing/buildflags/buildflags.h" #include "printing/metafile_skia.h" -#include "printing/page_size_margins.h" +#include "printing/mojom/print.mojom.h" #include "third_party/blink/public/web/web_local_frame.h" namespace printing { @@ -30,7 +30,7 @@ void PrintRenderFrameHelper::PrintPageInternal( gfx::Size* page_size_in_dpi, gfx::Rect* content_rect_in_dpi) { double css_scale_factor = scale_factor; - PageSizeMargins page_layout_in_points; + mojom::PageSizeMargins page_layout_in_points; ComputePageLayoutInPointsForCss(frame, page_number, params, ignore_css_margins_, &css_scale_factor, &page_layout_in_points); @@ -53,7 +53,7 @@ void PrintRenderFrameHelper::PrintPageInternal( float final_scale_factor = css_scale_factor * webkit_page_shrink_factor; cc::PaintCanvas* canvas = metafile->GetVectorCanvasForNewPage( - page_size, canvas_area, final_scale_factor); + page_size, canvas_area, final_scale_factor, params.page_orientation); if (!canvas) return; diff --git a/chromium/components/printing/test/BUILD.gn b/chromium/components/printing/test/BUILD.gn index 0e30276c924..8f65aa0f995 100644 --- a/chromium/components/printing/test/BUILD.gn +++ b/chromium/components/printing/test/BUILD.gn @@ -17,6 +17,7 @@ static_library("test_support") { public_deps = [ "//base", "//components/printing/common", + "//components/printing/common:mojo_interfaces", "//components/printing/renderer", "//content/public/common", "//content/public/renderer", |