0

Add new method to grab current visible page from PDF renderer

This CL adds a method to get the current visible PDF page. In a follow
up CL, I'll use the new page index to grab text from the
X pages after the one the user is viewing and include it in the partial
upload request.

Bug: 387306854
Change-Id: Ibc145a5c7cb5f359614814393d671f3fea460299
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6134412
Reviewed-by: Matthew Denton <mpdenton@chromium.org>
Reviewed-by: Ali Stanfield <stanfield@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Duncan Mercer <mercerd@google.com>
Cr-Commit-Position: refs/heads/main@{#1405010}
This commit is contained in:
Duncan Mercer
2025-01-10 14:10:56 -08:00
committed by Chromium LUCI CQ
parent 989be51b7e
commit 090ba9ab51
8 changed files with 61 additions and 6 deletions

@ -1614,7 +1614,22 @@ void LensOverlayController::OnPdfBytesReceived(
std::move(callback).Run(bytes, lens::MimeType::kPdf, page_count);
}
void LensOverlayController::GetPartialPdfText(uint32_t page_count) {
void LensOverlayController::FetchVisiblePageIndexAndGetPartialPdfText(
uint32_t page_count) {
pdf::PDFDocumentHelper* pdf_helper =
pdf::PDFDocumentHelper::MaybeGetForWebContents(tab_->GetContents());
if (!pdf_helper) {
return;
}
pdf_helper->GetMostVisiblePageIndex(
base::BindOnce(&LensOverlayController::GetPartialPdfText,
weak_factory_.GetWeakPtr(), page_count));
}
void LensOverlayController::GetPartialPdfText(
uint32_t page_count,
std::optional<uint32_t> visible_page_index) {
pdf::PDFDocumentHelper* pdf_helper =
pdf::PDFDocumentHelper::MaybeGetForWebContents(tab_->GetContents());
if (!pdf_helper ||
@ -1623,6 +1638,8 @@ void LensOverlayController::GetPartialPdfText(uint32_t page_count) {
return;
}
// TODO(387306854): Add logic to grab page text form the visible page index.
// Fetch the first page of text which will be then recursively fetch following
// pages.
initialization_data_->pdf_pages_text_.clear();
@ -1788,7 +1805,7 @@ void LensOverlayController::UpdatePageContextualization(
// If the new page is a PDF, fetch the text from the page to be used as early
// suggest signals.
if (content_type == lens::MimeType::kPdf) {
GetPartialPdfText(page_count.value_or(0));
FetchVisiblePageIndexAndGetPartialPdfText(page_count.value_or(0));
}
lens_overlay_query_controller_->SendPageContentUpdateRequest(
@ -2025,9 +2042,11 @@ void LensOverlayController::InitializeOverlay(
// If PDF content was extracted from the page, fetch the text from the PDF to
// be used as early suggest signals.
if (initialization_data_->page_content_type_ == lens::MimeType::kPdf) {
if (initialization_data_->page_content_type_ == lens::MimeType::kPdf &&
!initialization_data_->page_content_bytes_.empty()) {
CHECK(initialization_data_->pdf_page_count_.has_value());
GetPartialPdfText(initialization_data_->pdf_page_count_.value());
FetchVisiblePageIndexAndGetPartialPdfText(
initialization_data_->pdf_page_count_.value());
}
// If the StartQueryFlow optimization is enabled, the page contents will not

@ -709,15 +709,21 @@ class LensOverlayController : public LensSearchboxClient,
#if BUILDFLAG(ENABLE_PDF)
// Receives the PDF bytes from the IPC call to the PDF renderer and stores
// them in initialization data.
// them in initialization data. `pdf_page_count` is passed to the partial PDF
// text fetch to be used to determine when to stop fetching.
void OnPdfBytesReceived(PageContentRetrievedCallback callback,
pdf::mojom::PdfListener::GetPdfBytesStatus status,
const std::vector<uint8_t>& bytes,
uint32_t pdf_page_count);
// Fetches the visible page index from the PDF renderer and then starts the
// process of fetching the text from the PDF to be used for suggest signals.
void FetchVisiblePageIndexAndGetPartialPdfText(uint32_t page_count);
// Starts the process of fetching the text from the PDF to be used for suggest
// signals.
void GetPartialPdfText(uint32_t total_page_count);
void GetPartialPdfText(uint32_t page_count,
std::optional<uint32_t> visible_page_index);
// Gets the partial text from the PDF to be used for suggest. Schedules for
// the next page of text to be fetched, from the PDF in page order until

@ -253,6 +253,15 @@ void PDFDocumentHelper::GetPageText(
remote_pdf_client_->GetPageText(page_index, std::move(callback));
}
void PDFDocumentHelper::GetMostVisiblePageIndex(
pdf::mojom::PdfListener::GetMostVisiblePageIndexCallback callback) {
if (!remote_pdf_client_) {
std::move(callback).Run(std::nullopt);
return;
}
remote_pdf_client_->GetMostVisiblePageIndex(std::move(callback));
}
void PDFDocumentHelper::OnSelectionEvent(ui::SelectionEventType event) {
// Should be handled by `TouchSelectionControllerClientAura`.
NOTREACHED();

@ -99,6 +99,8 @@ class PDFDocumentHelper
void GetPageText(int32_t page_index,
pdf::mojom::PdfListener::GetPageTextCallback callback);
void GetMostVisiblePageIndex(
pdf::mojom::PdfListener::GetMostVisiblePageIndexCallback callback);
private:
friend class content::DocumentUserData<PDFDocumentHelper>;

@ -49,6 +49,10 @@ class FakePdfListener : public pdf::mojom::PdfListener {
GetPageText,
(int32_t, GetPageTextCallback callback),
(override));
MOCK_METHOD(void,
GetMostVisiblePageIndex,
(GetMostVisiblePageIndexCallback callback),
(override));
};
class TestPDFDocumentHelperClient : public PDFDocumentHelperClient {

@ -38,6 +38,10 @@ interface PdfListener {
// Get the text contained on the given page of the PDF. `page_index` should be
// the range [0, # of pages).
GetPageText(int32 page_index) => (mojo_base.mojom.String16 text);
// Returns the index of the most visible page. If no page is visible,
// returns nullopt.
GetMostVisiblePageIndex() => (uint32? page_index);
};
// Browser-side interface used by PDF renderers.

@ -1528,6 +1528,16 @@ void PdfViewWebPlugin::GetPdfBytes(uint32_t size_limit,
page_count);
}
void PdfViewWebPlugin::GetMostVisiblePageIndex(
GetMostVisiblePageIndexCallback callback) {
auto page_index = engine_->GetMostVisiblePage();
if (page_index < 0) {
std::move(callback).Run(std::nullopt);
return;
}
std::move(callback).Run(page_index);
}
void PdfViewWebPlugin::GetPageText(int32_t page_index,
GetPageTextCallback callback) {
if (page_index < 0 || page_index >= engine_->GetNumberOfPages()) {

@ -395,6 +395,7 @@ class PdfViewWebPlugin final : public PDFiumEngineClient,
const gfx::PointF& extent) override;
void GetPdfBytes(uint32_t size_limit, GetPdfBytesCallback callback) override;
void GetPageText(int32_t page_index, GetPageTextCallback callback) override;
void GetMostVisiblePageIndex(GetMostVisiblePageIndexCallback callback) override;
// UrlLoader::Client:
bool IsValid() const override;