Commit 0cb6b59e authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Handle FFI_PageEvent from PDFium in PDF Viewer.

Add or remove pages from the viewer in response to these events.

Bug: pdfium:401,chromium:872903,chromium:867135
Change-Id: Iddc88c5a90370213619a0f3f2587f215d0dafbc8
Reviewed-on: https://chromium-review.googlesource.com/1169691
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584515}
parent 2741204b
......@@ -2606,22 +2606,24 @@ void PDFiumEngine::LoadPageInfo(bool reload) {
pp::Size old_document_size = document_size_;
document_size_ = pp::Size();
std::vector<pp::Rect> page_rects;
int page_count = FPDF_GetPageCount(doc());
size_t new_page_count = FPDF_GetPageCount(doc());
bool doc_complete = doc_loader_->IsDocumentComplete();
bool is_linear =
FPDFAvail_IsLinearized(fpdf_availability()) == PDF_LINEARIZED;
for (int i = 0; i < page_count; ++i) {
for (size_t i = 0; i < new_page_count; ++i) {
if (i != 0) {
// Add space for horizontal separator.
document_size_.Enlarge(0, kPageSeparatorThickness);
}
// Get page availability. If |reload| == true, then the document has been
// constructed already. Get page availability flag from already existing
// PDFiumPage class.
// If |reload| == false, then the document may not be fully loaded yet.
// Get page availability. If |reload| == true and the page is not new,
// then the page has been constructed already. Get page availability flag
// from already existing PDFiumPage object.
// If |reload| == false or the page is new, then the page may not be fully
// loaded yet.
bool page_available;
if (reload) {
if (reload && i < pages_.size()) {
page_available = pages_[i]->available();
} else if (is_linear) {
FX_DOWNLOADHINTS& download_hints = document_->download_hints();
......@@ -2644,22 +2646,34 @@ void PDFiumEngine::LoadPageInfo(bool reload) {
document_size_.Enlarge(0, size.height());
}
for (int i = 0; i < page_count; ++i) {
for (size_t i = 0; i < new_page_count; ++i) {
// Center pages relative to the entire document.
page_rects[i].set_x((document_size_.width() - page_rects[i].width()) / 2);
pp::Rect page_rect(page_rects[i]);
page_rect.Inset(kPageShadowLeft, kPageShadowTop, kPageShadowRight,
kPageShadowBottom);
if (reload) {
pages_[i]->set_rect(page_rect);
} else {
if (!reload) {
// The page is marked as not being available even if |doc_complete| is
// true because FPDFAvail_IsPageAvail() still has to be called for this
// page, which will be done in FinishLoadingDocument().
pages_.push_back(std::make_unique<PDFiumPage>(this, i, page_rect, false));
} else if (i < pages_.size()) {
pages_[i]->set_rect(page_rect);
} else {
bool available = FPDFAvail_IsPageAvail(fpdf_availability(), i, nullptr);
pages_.push_back(
std::make_unique<PDFiumPage>(this, i, page_rect, available));
}
}
// Remove pages that do not exist anymore.
if (pages_.size() > new_page_count) {
for (size_t i = new_page_count; i < pages_.size(); ++i)
pages_[i]->Unload();
pages_.resize(new_page_count);
}
CalculateVisiblePages();
if (document_size_ != old_document_size)
client_->DocumentSizeUpdated(document_size_);
......@@ -3593,6 +3607,12 @@ void PDFiumEngine::GetSelection(uint32_t* selection_start_page_index,
}
}
#if defined(PDF_ENABLE_XFA)
void PDFiumEngine::UpdatePageCount() {
InvalidateAllPages();
}
#endif // defined(PDF_ENABLE_XFA)
PDFiumEngine::ProgressivePaint::ProgressivePaint(int index,
const pp::Rect& rect)
: page_index_(index), rect_(rect) {}
......
......@@ -138,6 +138,10 @@ class PDFiumEngine : public PDFEngine,
void CancelBrowserDownload() override;
void KillFormFocus() override;
#if defined(PDF_ENABLE_XFA)
void UpdatePageCount();
#endif // defined(PDF_ENABLE_XFA)
void UnsupportedFeature(const std::string& feature);
void FontSubstituted();
......
......@@ -52,7 +52,7 @@ PDFiumFormFiller::PDFiumFormFiller(PDFiumEngine* engine, bool enable_javascript)
FPDF_FORMFILLINFO::FFI_GetCurrentPageIndex = Form_GetCurrentPageIndex;
FPDF_FORMFILLINFO::FFI_GetPageViewRect = Form_GetPageViewRect;
FPDF_FORMFILLINFO::FFI_GetPlatform = Form_GetPlatform;
FPDF_FORMFILLINFO::FFI_PageEvent = nullptr;
FPDF_FORMFILLINFO::FFI_PageEvent = Form_PageEvent;
FPDF_FORMFILLINFO::FFI_PopupMenu = Form_PopupMenu;
FPDF_FORMFILLINFO::FFI_PostRequestURL = Form_PostRequestURL;
FPDF_FORMFILLINFO::FFI_PutRequestURL = Form_PutRequestURL;
......@@ -393,6 +393,18 @@ int PDFiumFormFiller::Form_GetPlatform(FPDF_FORMFILLINFO* param,
return platform_flag;
}
// static
void PDFiumFormFiller::Form_PageEvent(FPDF_FORMFILLINFO* param,
int page_count,
unsigned long event_type) {
DCHECK(page_count != 0);
DCHECK(event_type == FXFA_PAGEVIEWEVENT_POSTADDED ||
event_type == FXFA_PAGEVIEWEVENT_POSTREMOVED);
PDFiumEngine* engine = GetEngine(param);
engine->UpdatePageCount();
}
// static
FPDF_BOOL PDFiumFormFiller::Form_PopupMenu(FPDF_FORMFILLINFO* param,
FPDF_PAGE page,
......
......@@ -91,6 +91,9 @@ class PDFiumFormFiller : public FPDF_FORMFILLINFO, public IPDF_JSPLATFORM {
static int Form_GetPlatform(FPDF_FORMFILLINFO* param,
void* platform,
int length);
static void Form_PageEvent(FPDF_FORMFILLINFO* param,
int page_count,
unsigned long event_type);
static FPDF_BOOL Form_PopupMenu(FPDF_FORMFILLINFO* param,
FPDF_PAGE page,
FPDF_WIDGET widget,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment