0

PDF: Return earlier when important tasks in Searchify fail

Skip further tasks whenever:
- The `perform_ocr_callback` passed to `Searchify` returns null.
- FPDFPage_GenerateContent() fails.

Bug: 331592549
Test: pdf_unittests --gtest_filter="PDFiumEngineExportsTest*"
Change-Id: If33bcc050c3527111b437541160b80ba97790567
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5491175
Commit-Queue: Chu-Hsuan Yang <chuhsuan@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1295238}
This commit is contained in:
Chu-Hsuan Yang
2024-05-02 02:31:51 +00:00
committed by Chromium LUCI CQ
parent 065dde8ba2
commit 4e6814c0c7
3 changed files with 21 additions and 4 deletions

@ -222,8 +222,6 @@ std::vector<uint8_t> ConvertPdfDocumentToNupPdf(
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
// Converts an inaccessible PDF to a searchable PDF.
// `pdf_buffer` is the buffer of the inaccessible PDF.
// `searchified_callback` is the callback that is called with the searchified
// PDF when the conversion is done.
// `perform_ocr_callback` is the callback that takes an image and outputs
// the OCR result. It may be called multiple times.
//

@ -234,6 +234,25 @@ TEST_F(PDFiumEngineExportsTest, Searchify) {
testing::HasSubstr(kExpectedText));
}
}
TEST_F(PDFiumEngineExportsTest, SearchifyBroken) {
base::FilePath pdf_path =
pdf_data_dir().Append(FILE_PATH_LITERAL("image_alt_text.pdf"));
std::optional<std::vector<uint8_t>> pdf_buffer =
base::ReadFileToBytes(pdf_path);
ASSERT_TRUE(pdf_buffer.has_value());
auto broken_perform_ocr_callback =
base::BindRepeating([](const SkBitmap& bitmap) {
auto annotation = screen_ai::mojom::VisualAnnotationPtr();
// Simulate the scenarios that fail to set the value of `annotation`.
CHECK(!annotation);
return annotation;
});
std::vector<uint8_t> output_pdf_buffer =
Searchify(*pdf_buffer, std::move(broken_perform_ocr_callback));
EXPECT_TRUE(output_pdf_buffer.empty());
}
#endif // BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
} // namespace chrome_pdf

@ -238,14 +238,14 @@ std::vector<uint8_t> PDFiumSearchify(
auto annotation = perform_ocr_callback.Run(bitmap);
if (!annotation) {
DLOG(ERROR) << "Failed to get OCR annotation on the image";
continue;
return {};
}
AddTextOnImage(document.get(), page.get(), font.get(), image,
std::move(annotation));
}
if (!FPDFPage_GenerateContent(page.get())) {
DLOG(ERROR) << "Failed to generate content";
continue;
return {};
}
}
PDFiumMemBufferFileWrite output_file_write;