0

[unseasoned-pdf] Move printing implementations to PdfViewPluginBase

Move shareable implementations of PrintBegin(), PrintPages(), and
PrintEnd() from OutOfProcessInstance to PdfViewPluginBase so that
PdfViewWebPlugin will be able to use them.

Bug: 1200000
Change-Id: I28c2f30e8a14993e0861839d898424a51f7d0403
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2953966
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#892381}
This commit is contained in:
Daniel Hosseinian
2021-06-15 01:56:56 +00:00
committed by Chromium LUCI CQ
parent e2a253d94a
commit 24f54223e7
6 changed files with 63 additions and 40 deletions

@ -323,7 +323,6 @@ if (enable_pdf) {
"//ppapi/cpp:objects",
"//ppapi/cpp/private:internal_module",
"//skia",
"//third_party/blink/public:blink_headers",
"//third_party/blink/public/common:headers",
"//ui/base",
"//ui/base/cursor/mojom:cursor_type",

@ -726,20 +726,8 @@ void OutOfProcessInstance::Redo() {
int32_t OutOfProcessInstance::PdfPrintBegin(
const PP_PrintSettings_Dev* print_settings,
const PP_PdfPrintSettings_Dev* pdf_print_settings) {
// For us num_pages is always equal to the number of pages in the PDF
// document irrespective of the printable area.
int32_t ret = engine()->GetNumberOfPages();
if (!ret)
return 0;
uint32_t supported_formats = QuerySupportedPrintOutputFormats();
if ((print_settings->format & supported_formats) == 0)
return 0;
print_params_ =
WebPrintParamsFromPPPrintSettings(*print_settings, *pdf_print_settings);
engine()->PrintBegin();
return ret;
return PdfViewPluginBase::PrintBegin(
WebPrintParamsFromPPPrintSettings(*print_settings, *pdf_print_settings));
}
uint32_t OutOfProcessInstance::QuerySupportedPrintOutputFormats() {
@ -760,18 +748,8 @@ int32_t OutOfProcessInstance::PrintBegin(
pp::Resource OutOfProcessInstance::PrintPages(
const PP_PrintPageNumberRange_Dev* page_ranges,
uint32_t page_range_count) {
if (!print_params_.has_value())
return pp::Resource();
print_pages_called_ = true;
if (page_range_count == 0)
return pp::Resource();
const std::vector<int> page_numbers =
PageNumbersFromPPPrintPageNumberRange(page_ranges, page_range_count);
const std::vector<uint8_t> pdf_data =
engine()->PrintPages(page_numbers, print_params_.value());
const std::vector<uint8_t> pdf_data = PdfViewPluginBase::PrintPages(
PageNumbersFromPPPrintPageNumberRange(page_ranges, page_range_count));
// Convert buffer to Pepper type.
pp::Buffer_Dev buffer;
@ -784,11 +762,7 @@ pp::Resource OutOfProcessInstance::PrintPages(
}
void OutOfProcessInstance::PrintEnd() {
if (print_pages_called_)
UserMetricsRecordAction("PDF.PrintPage");
print_pages_called_ = false;
print_params_.reset();
engine()->PrintEnd();
PdfViewPluginBase::PrintEnd();
}
bool OutOfProcessInstance::IsPrintScalingDisabled() {

@ -24,7 +24,6 @@
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/private/find_private.h"
#include "third_party/blink/public/web/web_print_params.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/rect.h"
@ -193,12 +192,6 @@ class OutOfProcessInstance : public PdfViewPluginBase,
// The Pepper image data that is in sync with mutable_image_data().
pp::ImageData pepper_image_data_;
// Assigned a value only between `PdfPrintBegin()` and `PrintEnd()` calls.
absl::optional<blink::WebPrintParams> print_params_;
// For identifying actual print operations to avoid double logging of UMA.
bool print_pages_called_;
// The PreviewModeClient used for print preview. Will be passed to
// `preview_engine_`.
std::unique_ptr<PreviewModeClient> preview_client_;

@ -561,6 +561,40 @@ void PdfViewPluginBase::OnGeometryChanged(double old_zoom,
PrepareAndSetAccessibilityViewportInfo();
}
int PdfViewPluginBase::PrintBegin(const blink::WebPrintParams& print_params) {
// The returned value is always equal to the number of pages in the PDF
// document irrespective of the printable area.
int32_t ret = engine()->GetNumberOfPages();
if (!ret)
return 0;
const bool can_print =
engine()->HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) ||
(print_params.rasterize_pdf &&
engine()->HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY));
if (!can_print)
return 0;
print_params_ = print_params;
engine()->PrintBegin();
return ret;
}
std::vector<uint8_t> PdfViewPluginBase::PrintPages(
const std::vector<int>& page_numbers) {
print_pages_called_ = true;
return engine()->PrintPages(page_numbers, print_params_.value());
}
void PdfViewPluginBase::PrintEnd() {
if (print_pages_called_)
UserMetricsRecordAction("PDF.PrintPage");
print_pages_called_ = false;
print_params_.reset();
engine_->PrintEnd();
}
void PdfViewPluginBase::UpdateGeometryOnViewChanged(
const gfx::Rect& new_view_rect,
float new_device_scale) {

@ -16,6 +16,8 @@
#include "pdf/paint_manager.h"
#include "pdf/pdf_engine.h"
#include "pdf/pdfium/pdfium_form_filler.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/web/web_print_params.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
@ -254,6 +256,20 @@ class PdfViewPluginBase : public PDFEngine::Client,
virtual void SetAccessibilityViewportInfo(
const AccessibilityViewportInfo& viewport_info) = 0;
// Begins a print session with the given `print_params`. A call to
// `PrintPages()` can only be made after after a successful call to
// `PrintBegin()`. Returns the number of pages required for the print output.
// A returned value of 0 indicates failure.
int PrintBegin(const blink::WebPrintParams& print_params);
// Prints the pages specified by `page_numbers` using the parameters passed to
// `PrintBegin()` Returns a vector of bytes containing the printed output. An
// empty returned value indicates failure.
std::vector<uint8_t> PrintPages(const std::vector<int>& page_numbers);
// Ends the print session. Further calls to `PrintPages()` will fail.
void PrintEnd();
static constexpr bool IsSaveDataSizeValid(size_t size) {
return size > 0 && size <= kMaximumSavedFileSize;
}
@ -483,6 +499,12 @@ class PdfViewPluginBase : public PDFEngine::Client,
// Whether the document is in edit mode.
bool edit_mode_ = false;
// Assigned a value only between `PrintBegin()` and `PrintEnd()` calls.
absl::optional<blink::WebPrintParams> print_params_;
// For identifying actual print operations to avoid double logging of UMA.
bool print_pages_called_;
};
} // namespace chrome_pdf

@ -969,7 +969,8 @@ void PDFiumEngine::PrintBegin() {
std::vector<uint8_t> PDFiumEngine::PrintPages(
const std::vector<int>& page_numbers,
const blink::WebPrintParams& print_params) {
DCHECK(!page_numbers.empty());
if (page_numbers.empty())
return std::vector<uint8_t>();
return print_params.rasterize_pdf
? PrintPagesAsRasterPdf(page_numbers, print_params)