0

Merge PDFEngineExports into PDFiumEngineExports

As part of PDFEngine removal, first remove PDFEngineExports. Merge the
relevant parts into its only implementation.

Bug: 40128715
Change-Id: Ica44bcbb328196332fb10ae85c291fa052236d3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5714800
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Alan Screen <awscreen@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1329803}
This commit is contained in:
Lei Zhang
2024-07-18 20:58:29 +00:00
committed by Chromium LUCI CQ
parent 1de2f0fb6e
commit 181c665d77
5 changed files with 122 additions and 175 deletions

@ -12,9 +12,9 @@
#include "base/check.h"
#include "base/feature_list.h"
#include "build/build_config.h"
#include "pdf/pdf_engine.h"
#include "pdf/pdf_features.h"
#include "pdf/pdf_init.h"
#include "pdf/pdfium/pdfium_engine_exports.h"
#include "services/screen_ai/buildflags/buildflags.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_f.h"
@ -64,7 +64,7 @@ void SetUseSkiaRendererPolicy(bool use_skia) {
std::optional<FlattenPdfResult> CreateFlattenedPdf(
base::span<const uint8_t> input_buffer) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
return PDFEngineExports::Get()->CreateFlattenedPdf(input_buffer);
return PDFiumEngineExports::Get()->CreateFlattenedPdf(input_buffer);
}
#endif // BUILDFLAG(IS_CHROMEOS)
@ -85,8 +85,8 @@ bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
bool autorotate,
bool use_color) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings(
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
PDFiumEngineExports::RenderingSettings settings(
gfx::Size(dpi_x, dpi_y),
gfx::Rect(bounds_origin_x, bounds_origin_y, bounds_width, bounds_height),
fit_to_bounds, stretch_to_bounds, keep_aspect_ratio, center_in_bounds,
@ -96,7 +96,7 @@ bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
}
void SetPDFUsePrintMode(int mode) {
PDFEngineExports::Get()->SetPDFUsePrintMode(mode);
PDFiumEngineExports::Get()->SetPDFUsePrintMode(mode);
}
#endif // BUILDFLAG(IS_WIN)
@ -104,33 +104,33 @@ bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
float* max_page_width) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->GetPDFDocInfo(pdf_buffer, page_count, max_page_width);
}
std::optional<DocumentMetadata> GetPDFDocMetadata(
base::span<const uint8_t> pdf_buffer) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->GetPDFDocMetadata(pdf_buffer);
}
std::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->IsPDFDocTagged(pdf_buffer);
}
base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
int page_index) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->GetPDFStructTreeForPage(pdf_buffer, page_index);
}
std::optional<bool> PDFDocHasOutline(base::span<const uint8_t> pdf_buffer) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->PDFDocHasOutline(pdf_buffer);
}
@ -138,8 +138,8 @@ std::optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
int page_index) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
chrome_pdf::PDFEngineExports* engine_exports =
chrome_pdf::PDFEngineExports::Get();
chrome_pdf::PDFiumEngineExports* engine_exports =
chrome_pdf::PDFiumEngineExports::Get();
return engine_exports->GetPDFPageSizeByIndex(pdf_buffer, page_index);
}
@ -150,8 +150,8 @@ bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
const gfx::Size& dpi,
const RenderOptions& options) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/true);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFEngineExports::RenderingSettings settings(
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
PDFiumEngineExports::RenderingSettings settings(
dpi, gfx::Rect(bitmap_size),
/*fit_to_bounds=*/true, options.stretch_to_bounds,
options.keep_aspect_ratio,
@ -167,7 +167,7 @@ std::vector<uint8_t> ConvertPdfPagesToNupPdf(
const gfx::Size& page_size,
const gfx::Rect& printable_area) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->ConvertPdfPagesToNupPdf(
std::move(input_buffers), pages_per_sheet, page_size, printable_area);
}
@ -178,7 +178,7 @@ std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
const gfx::Size& page_size,
const gfx::Rect& printable_area) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->ConvertPdfDocumentToNupPdf(
input_buffer, pages_per_sheet, page_size, printable_area);
}
@ -189,12 +189,12 @@ std::vector<uint8_t> Searchify(
base::RepeatingCallback<screen_ai::mojom::VisualAnnotationPtr(
const SkBitmap& bitmap)> perform_ocr_callback) {
ScopedSdkInitializer scoped_sdk_initializer(/*enable_v8=*/false);
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->Searchify(pdf_buffer, std::move(perform_ocr_callback));
}
std::unique_ptr<PdfProgressiveSearchifier> CreateProgressiveSearchifier() {
PDFEngineExports* engine_exports = PDFEngineExports::Get();
PDFiumEngineExports* engine_exports = PDFiumEngineExports::Get();
return engine_exports->CreateProgressiveSearchifier();
}
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)

@ -24,14 +24,6 @@
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include "pdf/flatten_pdf_result.h"
#endif
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
#include "services/screen_ai/public/mojom/screen_ai_service.mojom-forward.h"
#endif
@ -46,7 +38,6 @@ struct WebPrintParams;
namespace gfx {
class Point;
class Size;
class SizeF;
class Vector2d;
} // namespace gfx
@ -79,10 +70,6 @@ enum class DocumentPermission {
kPrintHighQuality,
};
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
class PdfProgressiveSearchifier;
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
// Do one time initialization of the SDK.
// If `enable_v8` is false, then the PDFEngine will not be able to run
// JavaScript.
@ -321,118 +308,6 @@ class PDFEngine {
SendThumbnailCallback send_callback) = 0;
};
// Interface for exports that wrap the PDF engine.
class PDFEngineExports {
public:
struct RenderingSettings {
RenderingSettings(const gfx::Size& dpi,
const gfx::Rect& bounds,
bool fit_to_bounds,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool center_in_bounds,
bool autorotate,
bool use_color,
bool render_for_printing);
RenderingSettings(const RenderingSettings& that);
gfx::Size dpi;
gfx::Rect bounds;
bool fit_to_bounds;
bool stretch_to_bounds;
bool keep_aspect_ratio;
bool center_in_bounds;
bool autorotate;
bool use_color;
bool render_for_printing;
};
PDFEngineExports() {}
virtual ~PDFEngineExports() {}
static PDFEngineExports* Get();
#if BUILDFLAG(IS_CHROMEOS)
// See the definition of CreateFlattenedPdf in pdf.cc for details.
virtual std::optional<FlattenPdfResult> CreateFlattenedPdf(
base::span<const uint8_t> input_buffer) = 0;
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_WIN)
// See the definition of RenderPDFPageToDC in pdf.cc for details.
virtual bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int page_index,
const RenderingSettings& settings,
HDC dc) = 0;
virtual void SetPDFUsePrintMode(int mode) = 0;
#endif // BUILDFLAG(IS_WIN)
// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
virtual bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int page_index,
const RenderingSettings& settings,
void* bitmap_buffer) = 0;
// See the definition of ConvertPdfPagesToNupPdf in pdf.cc for details.
virtual std::vector<uint8_t> ConvertPdfPagesToNupPdf(
std::vector<base::span<const uint8_t>> input_buffers,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area) = 0;
// See the definition of ConvertPdfDocumentToNupPdf in pdf.cc for details.
virtual std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
base::span<const uint8_t> input_buffer,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area) = 0;
virtual bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
float* max_page_width) = 0;
// Gets the PDF document metadata (see section 14.3.3 "Document Information
// Dictionary" of the ISO 32000-1:2008 spec).
virtual std::optional<DocumentMetadata> GetPDFDocMetadata(
base::span<const uint8_t> pdf_buffer) = 0;
// Whether the PDF is Tagged (see ISO 32000-1:2008 14.8 "Tagged PDF").
// Returns true if it's a tagged (accessible) PDF, false if it's a valid
// PDF but untagged, and nullopt if the PDF can't be parsed.
virtual std::optional<bool> IsPDFDocTagged(
base::span<const uint8_t> pdf_buffer) = 0;
// Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
// the structure tree for a given page as a hierarchical tree of base::Values.
virtual base::Value GetPDFStructTreeForPage(
base::span<const uint8_t> pdf_buffer,
int page_index) = 0;
// Whether the PDF has a Document Outline (see ISO 32000-1:2008 12.3.3
// "Document Outline"). Returns true if the PDF has an outline, false if it's
// a valid PDF without an outline, and nullopt if the PDF can't be parsed.
virtual std::optional<bool> PDFDocHasOutline(
base::span<const uint8_t> pdf_buffer) = 0;
// See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
virtual std::optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
int page_index) = 0;
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
// Converts an inaccessible PDF to a searchable PDF. See `Searchify` in pdf.h
// for more details.
virtual std::vector<uint8_t> Searchify(
base::span<const uint8_t> pdf_buffer,
base::RepeatingCallback<screen_ai::mojom::VisualAnnotationPtr(
const SkBitmap& bitmap)> perform_ocr_callback) = 0;
// Creates a PDF searchifier for future operations, such as adding and
// deleting pages, and saving PDFs.
virtual std::unique_ptr<PdfProgressiveSearchifier>
CreateProgressiveSearchifier() = 0;
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
};
} // namespace chrome_pdf
#endif // PDF_PDF_ENGINE_H_

@ -249,15 +249,16 @@ base::Value RecursiveGetStructTree(FPDF_STRUCTELEMENT struct_elem) {
} // namespace
PDFEngineExports::RenderingSettings::RenderingSettings(const gfx::Size& dpi,
const gfx::Rect& bounds,
bool fit_to_bounds,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool center_in_bounds,
bool autorotate,
bool use_color,
bool render_for_printing)
PDFiumEngineExports::RenderingSettings::RenderingSettings(
const gfx::Size& dpi,
const gfx::Rect& bounds,
bool fit_to_bounds,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool center_in_bounds,
bool autorotate,
bool use_color,
bool render_for_printing)
: dpi(dpi),
bounds(bounds),
fit_to_bounds(fit_to_bounds),
@ -268,10 +269,10 @@ PDFEngineExports::RenderingSettings::RenderingSettings(const gfx::Size& dpi,
use_color(use_color),
render_for_printing(render_for_printing) {}
PDFEngineExports::RenderingSettings::RenderingSettings(
PDFiumEngineExports::RenderingSettings::RenderingSettings(
const RenderingSettings& that) = default;
PDFEngineExports* PDFEngineExports::Get() {
PDFiumEngineExports* PDFiumEngineExports::Get() {
static base::NoDestructor<PDFiumEngineExports> exports;
return exports.get();
}

@ -8,12 +8,28 @@
#include <stddef.h>
#include <stdint.h>
#include <vector>
#include "base/containers/span.h"
#include "build/build_config.h"
#include "pdf/document_metadata.h"
#include "pdf/pdf_engine.h"
#include "services/screen_ai/buildflags/buildflags.h"
#include "ui/gfx/geometry/size_f.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif
#if BUILDFLAG(IS_CHROMEOS)
#include <optional>
#include "pdf/flatten_pdf_result.h"
#endif
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
#include <memory>
#include "base/functional/callback_forward.h"
#include "services/screen_ai/public/mojom/screen_ai_service.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
@ -25,60 +41,115 @@ namespace chrome_pdf {
class PdfProgressiveSearchifier;
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
class PDFiumEngineExports : public PDFEngineExports {
// Interface for exports that wrap PDFiumEngine.
class PDFiumEngineExports {
public:
struct RenderingSettings {
RenderingSettings(const gfx::Size& dpi,
const gfx::Rect& bounds,
bool fit_to_bounds,
bool stretch_to_bounds,
bool keep_aspect_ratio,
bool center_in_bounds,
bool autorotate,
bool use_color,
bool render_for_printing);
RenderingSettings(const RenderingSettings& that);
gfx::Size dpi;
gfx::Rect bounds;
bool fit_to_bounds;
bool stretch_to_bounds;
bool keep_aspect_ratio;
bool center_in_bounds;
bool autorotate;
bool use_color;
bool render_for_printing;
};
static PDFiumEngineExports* Get();
PDFiumEngineExports();
PDFiumEngineExports(const PDFiumEngineExports&) = delete;
PDFiumEngineExports& operator=(const PDFiumEngineExports&) = delete;
~PDFiumEngineExports() override;
~PDFiumEngineExports();
// PDFEngineExports:
#if BUILDFLAG(IS_CHROMEOS)
// See the definition of CreateFlattenedPdf in pdf.cc for details.
std::optional<FlattenPdfResult> CreateFlattenedPdf(
base::span<const uint8_t> input_buffer) override;
base::span<const uint8_t> input_buffer);
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_WIN)
// See the definition of RenderPDFPageToDC in pdf.cc for details.
bool RenderPDFPageToDC(base::span<const uint8_t> pdf_buffer,
int page_index,
const RenderingSettings& settings,
HDC dc) override;
void SetPDFUsePrintMode(int mode) override;
HDC dc);
void SetPDFUsePrintMode(int mode);
#endif // BUILDFLAG(IS_WIN)
// See the definition of RenderPDFPageToBitmap in pdf.cc for details.
bool RenderPDFPageToBitmap(base::span<const uint8_t> pdf_buffer,
int page_index,
const RenderingSettings& settings,
void* bitmap_buffer) override;
void* bitmap_buffer);
// See the definition of ConvertPdfPagesToNupPdf in pdf.cc for details.
std::vector<uint8_t> ConvertPdfPagesToNupPdf(
std::vector<base::span<const uint8_t>> input_buffers,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area) override;
const gfx::Rect& printable_area);
// See the definition of ConvertPdfDocumentToNupPdf in pdf.cc for details.
std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
base::span<const uint8_t> input_buffer,
size_t pages_per_sheet,
const gfx::Size& page_size,
const gfx::Rect& printable_area) override;
const gfx::Rect& printable_area);
bool GetPDFDocInfo(base::span<const uint8_t> pdf_buffer,
int* page_count,
float* max_page_width) override;
float* max_page_width);
// Gets the PDF document metadata (see section 14.3.3 "Document Information
// Dictionary" of the ISO 32000-1:2008 spec).
std::optional<DocumentMetadata> GetPDFDocMetadata(
base::span<const uint8_t> pdf_buffer) override;
std::optional<bool> IsPDFDocTagged(
base::span<const uint8_t> pdf_buffer) override;
base::span<const uint8_t> pdf_buffer);
// Whether the PDF is Tagged (see ISO 32000-1:2008 14.8 "Tagged PDF").
// Returns true if it's a tagged (accessible) PDF, false if it's a valid
// PDF but untagged, and nullopt if the PDF can't be parsed.
std::optional<bool> IsPDFDocTagged(base::span<const uint8_t> pdf_buffer);
// Given a tagged PDF (see IsPDFDocTagged, above), return the portion of
// the structure tree for a given page as a hierarchical tree of base::Values.
base::Value GetPDFStructTreeForPage(base::span<const uint8_t> pdf_buffer,
int page_index) override;
std::optional<bool> PDFDocHasOutline(
base::span<const uint8_t> pdf_buffer) override;
int page_index);
// Whether the PDF has a Document Outline (see ISO 32000-1:2008 12.3.3
// "Document Outline"). Returns true if the PDF has an outline, false if it's
// a valid PDF without an outline, and nullopt if the PDF can't be parsed.
std::optional<bool> PDFDocHasOutline(base::span<const uint8_t> pdf_buffer);
// See the definition of GetPDFPageSizeByIndex in pdf.cc for details.
std::optional<gfx::SizeF> GetPDFPageSizeByIndex(
base::span<const uint8_t> pdf_buffer,
int page_index) override;
int page_index);
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
// Converts an inaccessible PDF to a searchable PDF. See `Searchify` in pdf.h
// for more details.
std::vector<uint8_t> Searchify(
base::span<const uint8_t> pdf_buffer,
base::RepeatingCallback<screen_ai::mojom::VisualAnnotationPtr(
const SkBitmap& bitmap)> perform_ocr_callback) override;
std::unique_ptr<PdfProgressiveSearchifier> CreateProgressiveSearchifier()
override;
const SkBitmap& bitmap)> perform_ocr_callback);
// Creates a PDF searchifier for future operations, such as adding and
// deleting pages, and saving PDFs.
std::unique_ptr<PdfProgressiveSearchifier> CreateProgressiveSearchifier();
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
};

@ -93,7 +93,7 @@ void CheckPdfRendering(const std::vector<uint8_t>& pdf_data,
SkImageInfo::Make(gfx::SizeToSkISize(page_rect.size()),
kBGRA_8888_SkColorType, kPremul_SkAlphaType));
PDFEngineExports::RenderingSettings settings(
PDFiumEngineExports::RenderingSettings settings(
gfx::Size(printing::kDefaultPdfDpi, printing::kDefaultPdfDpi), page_rect,
/*fit_to_bounds=*/true,
/*stretch_to_bounds=*/false,