#if defined(UNSAFE_BUFFERS_BUILD)
#pragma allow_unsafe_buffers
#endif
#include "pdf/pdfium/pdfium_engine.h"
#include <math.h>
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/types/optional_util.h"
#include "build/build_config.h"
#include "gin/array_buffer.h"
#include "gin/public/gin_embedders.h"
#include "gin/public/isolate_holder.h"
#include "gin/public/v8_platform.h"
#include "pdf/accessibility_structs.h"
#include "pdf/draw_utils/coordinates.h"
#include "pdf/draw_utils/shadow.h"
#include "pdf/input_utils.h"
#include "pdf/loader/document_loader_impl.h"
#include "pdf/loader/url_loader.h"
#include "pdf/loader/url_loader_wrapper_impl.h"
#include "pdf/pdf_features.h"
#include "pdf/pdf_transform.h"
#include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
#include "pdf/pdfium/pdfium_document.h"
#include "pdf/pdfium/pdfium_document_metadata.h"
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
#include "pdf/pdfium/pdfium_permissions.h"
#include "pdf/pdfium/pdfium_unsupported_features.h"
#include "printing/mojom/print.mojom-shared.h"
#include "printing/units.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/input/web_pointer_properties.h"
#include "third_party/blink/public/common/input/web_touch_event.h"
#include "third_party/blink/public/common/input/web_touch_point.h"
#include "third_party/blink/public/web/web_print_params.h"
#include "third_party/pdfium/public/cpp/fpdf_scopers.h"
#include "third_party/pdfium/public/fpdf_annot.h"
#include "third_party/pdfium/public/fpdf_attachment.h"
#include "third_party/pdfium/public/fpdf_catalog.h"
#include "third_party/pdfium/public/fpdf_ext.h"
#include "third_party/pdfium/public/fpdf_fwlevent.h"
#include "third_party/pdfium/public/fpdf_ppo.h"
#include "third_party/pdfium/public/fpdf_searchex.h"
#include "third_party/pdfium/public/fpdfview.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"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
#include "v8/include/v8.h"
#if defined(PDF_ENABLE_XFA)
#include "gin/public/cppgc.h"
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include "pdf/pdfium/pdfium_font_linux.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "pdf/pdfium/pdfium_font_win.h"
#endif
ConvertUnit;
ConvertUnitFloat;
kPixelsPerInch;
kPointsPerInch;
namespace chrome_pdf {
FocusFieldType;
namespace …
void InitializeSDK(bool enable_v8,
bool use_skia,
FontMappingMode font_mapping_mode) { … }
void ShutdownSDK() { … }
PDFiumEngine::PDFiumEngine(PDFiumEngineClient* client,
PDFiumFormFiller::ScriptOption script_option)
: … { … }
PDFiumEngine::~PDFiumEngine() { … }
void PDFiumEngine::SetDocumentLoaderForTesting(
std::unique_ptr<DocumentLoader> loader) { … }
FontMappingMode PDFiumEngine::GetFontMappingMode() { … }
void PDFiumEngine::PageOffsetUpdated(const gfx::Vector2d& page_offset) { … }
void PDFiumEngine::PluginSizeUpdated(const gfx::Size& size) { … }
void PDFiumEngine::ScrolledToXPosition(int position) { … }
void PDFiumEngine::ScrolledToYPosition(int position) { … }
void PDFiumEngine::PrePaint() { … }
void PDFiumEngine::Paint(const gfx::Rect& rect,
SkBitmap& image_data,
std::vector<gfx::Rect>& ready,
std::vector<gfx::Rect>& pending) { … }
void PDFiumEngine::PostPaint() { … }
bool PDFiumEngine::HandleDocumentLoad(std::unique_ptr<UrlLoader> loader,
const std::string& original_url) { … }
std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() { … }
void PDFiumEngine::AppendPage(PDFiumEngine* engine, int index) { … }
std::vector<uint8_t> PDFiumEngine::GetSaveData() { … }
void PDFiumEngine::OnPendingRequestComplete() { … }
void PDFiumEngine::OnNewDataReceived() { … }
void PDFiumEngine::OnDocumentComplete() { … }
void PDFiumEngine::OnDocumentCanceled() { … }
void PDFiumEngine::FinishLoadingDocument() { … }
void PDFiumEngine::UnsupportedFeature(const std::string& feature) { … }
FPDF_AVAIL PDFiumEngine::fpdf_availability() const { … }
FPDF_DOCUMENT PDFiumEngine::doc() const { … }
FPDF_FORMHANDLE PDFiumEngine::form() const { … }
PDFiumPage* PDFiumEngine::GetPage(size_t index) { … }
bool PDFiumEngine::IsValidLink(const std::string& url) { … }
void PDFiumEngine::ContinueFind(bool case_sensitive) { … }
bool PDFiumEngine::HandleInputEvent(const blink::WebInputEvent& event) { … }
void PDFiumEngine::PrintBegin() { … }
std::vector<uint8_t> PDFiumEngine::PrintPages(
const std::vector<int>& page_indices,
const blink::WebPrintParams& print_params) { … }
std::vector<uint8_t> PDFiumEngine::PrintPagesAsRasterPdf(
const std::vector<int>& page_indices,
const blink::WebPrintParams& print_params) { … }
std::vector<uint8_t> PDFiumEngine::PrintPagesAsPdf(
const std::vector<int>& page_indices,
const blink::WebPrintParams& print_params) { … }
void PDFiumEngine::KillFormFocus() { … }
void PDFiumEngine::UpdateFocus(bool has_focus) { … }
AccessibilityFocusInfo PDFiumEngine::GetFocusInfo() { … }
bool PDFiumEngine::IsPDFDocTagged() { … }
uint32_t PDFiumEngine::GetLoadedByteSize() { … }
bool PDFiumEngine::ReadLoadedBytes(uint32_t length, void* buffer) { … }
void PDFiumEngine::SetFormSelectedText(FPDF_FORMHANDLE form_handle,
FPDF_PAGE page) { … }
void PDFiumEngine::PrintEnd() { … }
PDFiumPage::Area PDFiumEngine::GetCharIndex(const gfx::Point& point,
int* page_index,
int* char_index,
int* form_type,
PDFiumPage::LinkTarget* target) { … }
bool PDFiumEngine::OnMouseDown(const blink::WebMouseEvent& event) { … }
void PDFiumEngine::OnSingleClick(int page_index, int char_index) { … }
void PDFiumEngine::OnMultipleClick(int click_count,
int page_index,
int char_index) { … }
bool PDFiumEngine::OnLeftMouseDown(const blink::WebMouseEvent& event) { … }
bool PDFiumEngine::OnMiddleMouseDown(const blink::WebMouseEvent& event) { … }
bool PDFiumEngine::OnRightMouseDown(const blink::WebMouseEvent& event) { … }
bool PDFiumEngine::NavigateToLinkDestination(
PDFiumPage::Area area,
const PDFiumPage::LinkTarget& target,
WindowOpenDisposition disposition) { … }
bool PDFiumEngine::OnMouseUp(const blink::WebMouseEvent& event) { … }
bool PDFiumEngine::OnMouseMove(const blink::WebMouseEvent& event) { … }
ui::mojom::CursorType PDFiumEngine::DetermineCursorType(PDFiumPage::Area area,
int form_type) const { … }
void PDFiumEngine::OnMouseEnter(const blink::WebMouseEvent& event) { … }
bool PDFiumEngine::ExtendSelection(int page_index, int char_index) { … }
bool PDFiumEngine::OnKeyDown(const blink::WebKeyboardEvent& event) { … }
bool PDFiumEngine::OnKeyUp(const blink::WebKeyboardEvent& event) { … }
bool PDFiumEngine::OnChar(const blink::WebKeyboardEvent& event) { … }
void PDFiumEngine::StartFind(const std::u16string& text, bool case_sensitive) { … }
void PDFiumEngine::SearchUsingPDFium(const std::u16string& term,
bool case_sensitive,
bool first_search,
int character_to_start_searching_from,
int current_page) { … }
void PDFiumEngine::SearchUsingICU(const std::u16string& term,
bool case_sensitive,
bool first_search,
int character_to_start_searching_from,
int current_page) { … }
void PDFiumEngine::AddFindResult(const PDFiumRange& result) { … }
bool PDFiumEngine::SelectFindResult(bool forward) { … }
void PDFiumEngine::StopFind() { … }
std::vector<gfx::Rect> PDFiumEngine::GetAllScreenRectsUnion(
const std::vector<PDFiumRange>& rect_range,
const gfx::Point& point) const { … }
void PDFiumEngine::UpdateTickMarks() { … }
void PDFiumEngine::ZoomUpdated(double new_zoom_level) { … }
void PDFiumEngine::RotateClockwise() { … }
void PDFiumEngine::RotateCounterclockwise() { … }
bool PDFiumEngine::IsReadOnly() const { … }
void PDFiumEngine::SetReadOnly(bool enable) { … }
void PDFiumEngine::SetDocumentLayout(DocumentLayout::PageSpread page_spread) { … }
void PDFiumEngine::DisplayAnnotations(bool display) { … }
void PDFiumEngine::InvalidateAllPages() { … }
std::string PDFiumEngine::GetSelectedText() { … }
bool PDFiumEngine::CanEditText() const { … }
bool PDFiumEngine::HasEditableText() const { … }
void PDFiumEngine::ReplaceSelection(const std::string& text) { … }
bool PDFiumEngine::CanUndo() const { … }
bool PDFiumEngine::CanRedo() const { … }
void PDFiumEngine::Undo() { … }
void PDFiumEngine::Redo() { … }
void PDFiumEngine::HandleAccessibilityAction(
const AccessibilityActionData& action_data) { … }
std::string PDFiumEngine::GetLinkAtPosition(const gfx::Point& point) { … }
bool PDFiumEngine::HasPermission(DocumentPermission permission) const { … }
void PDFiumEngine::SelectAll() { … }
const std::vector<DocumentAttachmentInfo>&
PDFiumEngine::GetDocumentAttachmentInfoList() const { … }
std::vector<uint8_t> PDFiumEngine::GetAttachmentData(size_t index) { … }
const DocumentMetadata& PDFiumEngine::GetDocumentMetadata() const { … }
int PDFiumEngine::GetNumberOfPages() const { … }
base::Value::List PDFiumEngine::GetBookmarks() { … }
base::Value::Dict PDFiumEngine::TraverseBookmarks(FPDF_BOOKMARK bookmark,
unsigned int depth) { … }
void PDFiumEngine::ScrollBasedOnScrollAlignment(
const gfx::Rect& scroll_rect,
const AccessibilityScrollAlignment& horizontal_scroll_alignment,
const AccessibilityScrollAlignment& vertical_scroll_alignment) { … }
void PDFiumEngine::ScrollToGlobalPoint(const gfx::Rect& target_rect,
const gfx::Point& global_point) { … }
std::optional<PDFiumEngine::NamedDestination> PDFiumEngine::GetNamedDestination(
const std::string& destination) { … }
int PDFiumEngine::GetMostVisiblePage() { … }
gfx::Rect PDFiumEngine::GetPageBoundsRect(int index) { … }
gfx::Rect PDFiumEngine::GetPageContentsRect(int index) { … }
void PDFiumEngine::SetGrayscale(bool grayscale) { … }
void PDFiumEngine::HandleLongPress(const blink::WebTouchEvent& event) { … }
int PDFiumEngine::GetCharCount(int page_index) { … }
gfx::RectF PDFiumEngine::GetCharBounds(int page_index, int char_index) { … }
uint32_t PDFiumEngine::GetCharUnicode(int page_index, int char_index) { … }
std::optional<AccessibilityTextRunInfo> PDFiumEngine::GetTextRunInfo(
int page_index,
int start_char_index) { … }
std::vector<AccessibilityLinkInfo> PDFiumEngine::GetLinkInfo(
int page_index,
const std::vector<AccessibilityTextRunInfo>& text_runs) { … }
std::vector<AccessibilityImageInfo> PDFiumEngine::GetImageInfo(
int page_index,
uint32_t text_run_count) { … }
SkBitmap PDFiumEngine::GetImageForOcr(int page_index, int image_index) { … }
std::vector<AccessibilityHighlightInfo> PDFiumEngine::GetHighlightInfo(
int page_index,
const std::vector<AccessibilityTextRunInfo>& text_runs) { … }
std::vector<AccessibilityTextFieldInfo> PDFiumEngine::GetTextFieldInfo(
int page_index,
uint32_t text_run_count) { … }
bool PDFiumEngine::GetPrintScaling() { … }
int PDFiumEngine::GetCopiesToPrint() { … }
printing::mojom::DuplexMode PDFiumEngine::GetDuplexMode() { … }
std::optional<gfx::Size> PDFiumEngine::GetUniformPageSizePoints() { … }
void PDFiumEngine::AppendBlankPages(size_t num_pages) { … }
gfx::Size PDFiumEngine::plugin_size() const { … }
void PDFiumEngine::LoadDocument() { … }
bool PDFiumEngine::TryLoadingDoc(const std::string& password,
bool* needs_password) { … }
void PDFiumEngine::GetPasswordAndLoad() { … }
void PDFiumEngine::OnGetPasswordComplete(const std::string& password) { … }
void PDFiumEngine::ContinueLoadingDocument(const std::string& password) { … }
void PDFiumEngine::LoadPageInfo() { … }
void PDFiumEngine::RefreshCurrentDocumentLayout() { … }
void PDFiumEngine::ProposeNextDocumentLayout() { … }
void PDFiumEngine::UpdateDocumentLayout(DocumentLayout* layout) { … }
std::vector<gfx::Size> PDFiumEngine::LoadPageSizes(
const DocumentLayout::Options& layout_options) { … }
void PDFiumEngine::LoadBody() { … }
void PDFiumEngine::LoadPages() { … }
void PDFiumEngine::LoadForm() { … }
bool PDFiumEngine::IsLinearized() { … }
void PDFiumEngine::CalculateVisiblePages() { … }
bool PDFiumEngine::IsPageVisible(int page_index) const { … }
PageOrientation PDFiumEngine::GetCurrentOrientation() const { … }
void PDFiumEngine::ScrollToPage(int page) { … }
bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { … }
gfx::Size PDFiumEngine::GetPageSize(int index) { … }
gfx::Size PDFiumEngine::GetPageSizeForLayout(
int index,
const DocumentLayout::Options& layout_options) { … }
gfx::Insets PDFiumEngine::GetInsets(
const DocumentLayout::Options& layout_options,
size_t page_index,
size_t num_of_pages) const { … }
std::optional<size_t> PDFiumEngine::GetAdjacentPageIndexForTwoUpView(
size_t page_index,
size_t num_of_pages) const { … }
int PDFiumEngine::StartPaint(int page_index, const gfx::Rect& dirty) { … }
bool PDFiumEngine::ContinuePaint(int progressive_index, SkBitmap& image_data) { … }
void PDFiumEngine::FinishPaint(int progressive_index, SkBitmap& image_data) { … }
void PDFiumEngine::CancelPaints() { … }
void PDFiumEngine::FillPageSides(int progressive_index) { … }
void PDFiumEngine::PaintPageShadow(int progressive_index,
SkBitmap& image_data) { … }
void PDFiumEngine::DrawSelections(int progressive_index,
SkBitmap& image_data) const { … }
void PDFiumEngine::PaintUnavailablePage(int page_index,
const gfx::Rect& dirty,
SkBitmap& image_data) { … }
int PDFiumEngine::GetProgressiveIndex(int page_index) const { … }
ScopedFPDFBitmap PDFiumEngine::CreateBitmap(const gfx::Rect& rect,
bool has_alpha,
SkBitmap& image_data) const { … }
void PDFiumEngine::GetPDFiumRect(int page_index,
const gfx::Rect& rect,
int* start_x,
int* start_y,
int* size_x,
int* size_y) const { … }
int PDFiumEngine::GetRenderingFlags() const { … }
gfx::Rect PDFiumEngine::GetVisibleRect() const { … }
gfx::Rect PDFiumEngine::GetPageScreenRect(int page_index) const { … }
gfx::Rect PDFiumEngine::GetScreenRect(const gfx::Rect& rect) const { … }
gfx::RectF PDFiumEngine::GetPageBoundingBox(int page_index) { … }
void PDFiumEngine::Highlight(const RegionData& region,
const gfx::Rect& rect,
SkColor color,
std::vector<gfx::Rect>& highlighted_rects) const { … }
PDFiumEngine::SelectionChangeInvalidator::SelectionChangeInvalidator(
PDFiumEngine* engine)
: … { … }
PDFiumEngine::SelectionChangeInvalidator::~SelectionChangeInvalidator() { … }
std::vector<gfx::Rect>
PDFiumEngine::SelectionChangeInvalidator::GetVisibleSelections() const { … }
void PDFiumEngine::SelectionChangeInvalidator::Invalidate(
const gfx::Rect& selection) { … }
PDFiumEngine::MouseDownState::MouseDownState(
const PDFiumPage::Area& area,
const PDFiumPage::LinkTarget& target)
: … { … }
PDFiumEngine::MouseDownState::~MouseDownState() = default;
void PDFiumEngine::MouseDownState::Set(const PDFiumPage::Area& area,
const PDFiumPage::LinkTarget& target) { … }
void PDFiumEngine::MouseDownState::Reset() { … }
bool PDFiumEngine::MouseDownState::Matches(
const PDFiumPage::Area& area,
const PDFiumPage::LinkTarget& target) const { … }
PDFiumEngine::RegionData::RegionData(base::span<uint8_t> buffer, size_t stride)
: … { … }
PDFiumEngine::RegionData::RegionData(RegionData&&) noexcept = default;
PDFiumEngine::RegionData& PDFiumEngine::RegionData::operator=(
RegionData&&) noexcept = default;
PDFiumEngine::RegionData::~RegionData() = default;
void PDFiumEngine::DeviceToPage(int page_index,
const gfx::Point& device_point,
double* page_x,
double* page_y) { … }
gfx::Point PDFiumEngine::DeviceToScreen(const gfx::Point& device_point) const { … }
int PDFiumEngine::GetVisiblePageIndex(FPDF_PAGE page) { … }
void PDFiumEngine::SetCurrentPage(int index) { … }
void PDFiumEngine::DrawPageShadow(const gfx::Rect& page_rc,
const gfx::Rect& shadow_rc,
const gfx::Rect& clip_rc,
SkBitmap& image_data) { … }
std::optional<PDFiumEngine::RegionData> PDFiumEngine::GetRegion(
const gfx::Point& location,
SkBitmap& image_data) const { … }
void PDFiumEngine::OnSelectionTextChanged() { … }
void PDFiumEngine::OnSelectionPositionChanged() { … }
gfx::Size PDFiumEngine::ApplyDocumentLayout(
const DocumentLayout::Options& options) { … }
void PDFiumEngine::SetSelecting(bool selecting) { … }
void PDFiumEngine::EnteredEditMode() { … }
void PDFiumEngine::SetFieldFocus(PDFiumEngineClient::FocusFieldType type) { … }
void PDFiumEngine::SetMouseLeftButtonDown(bool is_mouse_left_button_down) { … }
bool PDFiumEngine::IsAnnotationAnEditableFormTextArea(FPDF_ANNOTATION annot,
int form_type) const { … }
void PDFiumEngine::ScheduleTouchTimer(const blink::WebTouchEvent& event) { … }
void PDFiumEngine::KillTouchTimer() { … }
bool PDFiumEngine::PageIndexInBounds(int index) const { … }
bool PDFiumEngine::IsPageCharacterIndexInBounds(
const PageCharacterIndex& index) const { … }
FPDF_BOOL PDFiumEngine::Pause_NeedToPauseNow(IFSDK_PAUSE* param) { … }
void PDFiumEngine::SetSelection(const PageCharacterIndex& selection_start_index,
const PageCharacterIndex& selection_end_index) { … }
void PDFiumEngine::SaveSelection() { … }
void PDFiumEngine::RestoreSelection() { … }
void PDFiumEngine::ScrollFocusedAnnotationIntoView() { … }
void PDFiumEngine::ScrollAnnotationIntoView(FPDF_ANNOTATION annot,
int page_index) { … }
void PDFiumEngine::OnFocusedAnnotationUpdated(FPDF_ANNOTATION annot,
int page_index) { … }
void PDFiumEngine::SetCaretPosition(const gfx::Point& position) { … }
void PDFiumEngine::MoveRangeSelectionExtent(const gfx::Point& extent) { … }
void PDFiumEngine::SetSelectionBounds(const gfx::Point& base,
const gfx::Point& extent) { … }
void PDFiumEngine::GetSelection(uint32_t* selection_start_page_index,
uint32_t* selection_start_char_index,
uint32_t* selection_end_page_index,
uint32_t* selection_end_char_index) { … }
void PDFiumEngine::LoadDocumentAttachmentInfoList() { … }
void PDFiumEngine::LoadDocumentMetadata() { … }
bool PDFiumEngine::HandleTabEvent(int modifiers) { … }
bool PDFiumEngine::HandleTabEventWithModifiers(int modifiers) { … }
bool PDFiumEngine::HandleTabForward(int modifiers) { … }
bool PDFiumEngine::HandleTabBackward(int modifiers) { … }
void PDFiumEngine::UpdateFocusElementType(FocusElementType focus_element_type) { … }
#if defined(PDF_ENABLE_XFA)
void PDFiumEngine::UpdatePageCount() { … }
#endif
void PDFiumEngine::UpdateLinkUnderCursor(const std::string& target_url) { … }
void PDFiumEngine::SetLinkUnderCursorForAnnotation(FPDF_ANNOTATION annot,
int page_index) { … }
void PDFiumEngine::RequestThumbnail(int page_index,
float device_pixel_ratio,
SendThumbnailCallback send_callback) { … }
void PDFiumEngine::MaybeRequestPendingThumbnail(int page_index) { … }
PDFiumEngine::ProgressivePaint::ProgressivePaint(int index,
const gfx::Rect& rect)
: … { … }
PDFiumEngine::ProgressivePaint::ProgressivePaint(
ProgressivePaint&& that) noexcept = default;
PDFiumEngine::ProgressivePaint& PDFiumEngine::ProgressivePaint::operator=(
ProgressivePaint&& that) noexcept = default;
PDFiumEngine::ProgressivePaint::~ProgressivePaint() = default;
void PDFiumEngine::ProgressivePaint::SetBitmapAndImageData(
ScopedFPDFBitmap bitmap,
SkBitmap image_data) { … }
PDFiumEngine::PendingThumbnail::PendingThumbnail() = default;
PDFiumEngine::PendingThumbnail::PendingThumbnail(
PendingThumbnail&& that) noexcept = default;
PDFiumEngine::PendingThumbnail& PDFiumEngine::PendingThumbnail::operator=(
PendingThumbnail&& that) noexcept = default;
PDFiumEngine::PendingThumbnail::~PendingThumbnail() = default;
}