Index: pdf/pdfium/pdfium_engine.cc |
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc |
index f0ba08923cffe37077cd37b2bc7dfb4826c3be0a..35c28964955714d9612abb991075277b904480e0 100644 |
--- a/pdf/pdfium/pdfium_engine.cc |
+++ b/pdf/pdfium/pdfium_engine.cc |
@@ -32,7 +32,6 @@ |
#include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" |
#include "pdf/pdfium/pdfium_mem_buffer_file_read.h" |
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h" |
-#include "pdf/url_loader_wrapper_impl.h" |
#include "ppapi/c/pp_errors.h" |
#include "ppapi/c/pp_input_event.h" |
#include "ppapi/c/ppb_core.h" |
@@ -669,6 +668,7 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) |
: client_(client), |
current_zoom_(1.0), |
current_rotation_(0), |
+ doc_loader_(this), |
password_tries_remaining_(0), |
doc_(nullptr), |
form_(nullptr), |
@@ -695,15 +695,15 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) |
file_access_.m_FileLen = 0; |
file_access_.m_GetBlock = &GetBlock; |
- file_access_.m_Param = this; |
+ file_access_.m_Param = &doc_loader_; |
file_availability_.version = 1; |
file_availability_.IsDataAvail = &IsDataAvail; |
- file_availability_.engine = this; |
+ file_availability_.loader = &doc_loader_; |
download_hints_.version = 1; |
download_hints_.AddSegment = &AddSegment; |
- download_hints_.engine = this; |
+ download_hints_.loader = &doc_loader_; |
// Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct |
// allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in |
@@ -973,22 +973,22 @@ int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param, |
int PDFiumEngine::GetBlock(void* param, unsigned long position, |
unsigned char* buffer, unsigned long size) { |
- PDFiumEngine* engine = static_cast<PDFiumEngine*>(param); |
- return engine->doc_loader_->GetBlock(position, size, buffer); |
+ DocumentLoader* loader = static_cast<DocumentLoader*>(param); |
+ return loader->GetBlock(position, size, buffer); |
} |
FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, |
size_t offset, size_t size) { |
PDFiumEngine::FileAvail* file_avail = |
static_cast<PDFiumEngine::FileAvail*>(param); |
- return file_avail->engine->doc_loader_->IsDataAvailable(offset, size); |
+ return file_avail->loader->IsDataAvailable(offset, size); |
} |
void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, |
size_t offset, size_t size) { |
PDFiumEngine::DownloadHints* download_hints = |
static_cast<PDFiumEngine::DownloadHints*>(param); |
- return download_hints->engine->doc_loader_->RequestData(offset, size); |
+ return download_hints->loader->RequestData(offset, size); |
} |
bool PDFiumEngine::New(const char* url, |
@@ -1123,27 +1123,15 @@ void PDFiumEngine::PostPaint() { |
bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { |
password_tries_remaining_ = kMaxPasswordTries; |
- process_when_pending_request_complete_ = true; |
- auto loader_wrapper = |
- base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), loader); |
- loader_wrapper->SetResponseHeaders(headers_); |
- |
- doc_loader_ = base::MakeUnique<DocumentLoader>(this); |
- if (doc_loader_->Init(std::move(loader_wrapper), url_)) { |
- // request initial data. |
- doc_loader_->RequestData(0, 1); |
- return true; |
- } |
- return false; |
+ return doc_loader_.Init(loader, url_, headers_); |
} |
pp::Instance* PDFiumEngine::GetPluginInstance() { |
return client_->GetPluginInstance(); |
} |
-std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() { |
- return base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), |
- client_->CreateURLLoader()); |
+pp::URLLoader PDFiumEngine::CreateURLLoader() { |
+ return client_->CreateURLLoader(); |
} |
void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { |
@@ -1168,32 +1156,35 @@ void PDFiumEngine::SetScrollPosition(const pp::Point& position) { |
} |
#endif |
+bool PDFiumEngine::IsProgressiveLoad() { |
+ return doc_loader_.is_partial_document(); |
+} |
+ |
std::string PDFiumEngine::GetMetadata(const std::string& key) { |
return GetDocumentMetadata(doc(), key); |
} |
-void PDFiumEngine::OnPendingRequestComplete() { |
- if (!process_when_pending_request_complete_) |
- return; |
+void PDFiumEngine::OnPartialDocumentLoaded() { |
+ file_access_.m_FileLen = doc_loader_.document_size(); |
if (!fpdf_availability_) { |
- file_access_.m_FileLen = doc_loader_->GetDocumentSize(); |
fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
DCHECK(fpdf_availability_); |
- // Currently engine does not deal efficiently with some non-linearized |
- // files. |
- // See http://code.google.com/p/chromium/issues/detail?id=59400 |
- // To improve user experience we download entire file for non-linearized |
- // PDF. |
- if (FPDFAvail_IsLinearized(fpdf_availability_) != PDF_LINEARIZED) { |
- // Wait complete document. |
- process_when_pending_request_complete_ = false; |
- FPDFAvail_Destroy(fpdf_availability_); |
- fpdf_availability_ = nullptr; |
- return; |
- } |
} |
- if (!doc_) { |
+ // Currently engine does not deal efficiently with some non-linearized files. |
+ // See http://code.google.com/p/chromium/issues/detail?id=59400 |
+ // To improve user experience we download entire file for non-linearized PDF. |
+ if (!FPDFAvail_IsLinearized(fpdf_availability_)) { |
+ doc_loader_.RequestData(0, doc_loader_.document_size()); |
+ return; |
+ } |
+ |
+ LoadDocument(); |
+} |
+ |
+void PDFiumEngine::OnPendingRequestComplete() { |
+ if (!doc_ || !form_) { |
+ DCHECK(fpdf_availability_); |
LoadDocument(); |
return; |
} |
@@ -1215,57 +1206,30 @@ void PDFiumEngine::OnPendingRequestComplete() { |
} |
void PDFiumEngine::OnNewDataAvailable() { |
- if (!doc_loader_->GetDocumentSize()) { |
- client_->DocumentLoadProgress(doc_loader_->count_of_bytes_received(), 0); |
- return; |
- } |
+ client_->DocumentLoadProgress(doc_loader_.GetAvailableData(), |
+ doc_loader_.document_size()); |
+} |
- const float progress = doc_loader_->GetProgress(); |
- DCHECK_GE(progress, 0.0); |
- DCHECK_LE(progress, 1.0); |
- client_->DocumentLoadProgress(progress * 10000, 10000); |
+void PDFiumEngine::OnDocumentFailed() { |
+ client_->DocumentLoadFailed(); |
} |
void PDFiumEngine::OnDocumentComplete() { |
- if (doc_) { |
- return FinishLoadingDocument(); |
- } |
- file_access_.m_FileLen = doc_loader_->GetDocumentSize(); |
- if (!fpdf_availability_) { |
- fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
- DCHECK(fpdf_availability_); |
+ if (!doc_ || !form_) { |
+ file_access_.m_FileLen = doc_loader_.document_size(); |
+ if (!fpdf_availability_) { |
+ fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
+ DCHECK(fpdf_availability_); |
+ } |
+ LoadDocument(); |
+ return; |
} |
- LoadDocument(); |
-} |
- |
-void PDFiumEngine::OnDocumentCanceled() { |
- if (visible_pages_.empty()) |
- client_->DocumentLoadFailed(); |
- else |
- OnDocumentComplete(); |
-} |
-void PDFiumEngine::CancelBrowserDownload() { |
- client_->CancelBrowserDownload(); |
+ FinishLoadingDocument(); |
} |
void PDFiumEngine::FinishLoadingDocument() { |
- DCHECK(doc_loader_->IsDocumentComplete() && doc_); |
- |
- if (!form_) { |
- int form_status = |
- FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); |
- if (form_status != PDF_FORM_NOTAVAIL) { |
- form_ = FPDFDOC_InitFormFillEnvironment( |
- doc_, static_cast<FPDF_FORMFILLINFO*>(this)); |
-#if defined(PDF_ENABLE_XFA) |
- FPDF_LoadXFA(doc_); |
-#endif |
- |
- FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor); |
- FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha); |
- } |
- } |
+ DCHECK(doc_loader_.IsDocumentComplete() && doc_); |
bool need_update = false; |
for (size_t i = 0; i < pages_.size(); ++i) { |
@@ -1477,7 +1441,7 @@ pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( |
return pp::Buffer_Dev(); |
// If document is not downloaded yet, disable printing. |
- if (doc_ && !doc_loader_->IsDocumentComplete()) |
+ if (doc_ && !doc_loader_.IsDocumentComplete()) |
return pp::Buffer_Dev(); |
FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); |
@@ -2620,7 +2584,7 @@ void PDFiumEngine::AppendBlankPages(int num_pages) { |
void PDFiumEngine::LoadDocument() { |
// Check if the document is ready for loading. If it isn't just bail for now, |
// we will call LoadDocument() again later. |
- if (!doc_ && !doc_loader_->IsDocumentComplete() && |
+ if (!doc_ && !doc_loader_.IsDocumentComplete() && |
!FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { |
return; |
} |
@@ -2659,12 +2623,13 @@ bool PDFiumEngine::TryLoadingDoc(const std::string& password, |
password_cstr = password.c_str(); |
password_tries_remaining_--; |
} |
- if (doc_loader_->IsDocumentComplete() && |
+ if (doc_loader_.IsDocumentComplete() && |
!FPDFAvail_IsLinearized(fpdf_availability_)) { |
doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); |
} else { |
doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); |
} |
+ |
if (!doc_) { |
if (FPDF_GetLastError() == FPDF_ERR_PASSWORD) |
*needs_password = true; |
@@ -2705,7 +2670,6 @@ void PDFiumEngine::ContinueLoadingDocument(const std::string& password) { |
GetPasswordAndLoad(); |
return; |
} |
- |
if (!doc_) { |
client_->DocumentLoadFailed(); |
return; |
@@ -2717,7 +2681,26 @@ void PDFiumEngine::ContinueLoadingDocument(const std::string& password) { |
permissions_ = FPDF_GetDocPermissions(doc_); |
permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_); |
- if (!doc_loader_->IsDocumentComplete()) { |
+ if (!form_) { |
+ int form_status = |
+ FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); |
+ bool doc_complete = doc_loader_.IsDocumentComplete(); |
+ // Try again if the data is not available and the document hasn't finished |
+ // downloading. |
+ if (form_status == PDF_FORM_NOTAVAIL && !doc_complete) |
+ return; |
+ |
+ form_ = FPDFDOC_InitFormFillEnvironment( |
+ doc_, static_cast<FPDF_FORMFILLINFO*>(this)); |
+#if defined(PDF_ENABLE_XFA) |
+ FPDF_LoadXFA(doc_); |
+#endif |
+ |
+ FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor); |
+ FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha); |
+ } |
+ |
+ if (!doc_loader_.IsDocumentComplete()) { |
// Check if the first page is available. In a linearized PDF, that is not |
// always page 0. Doing this gives us the default page size, since when the |
// document is available, the first page is available as well. |
@@ -2726,19 +2709,17 @@ void PDFiumEngine::ContinueLoadingDocument(const std::string& password) { |
LoadPageInfo(false); |
- if (doc_loader_->IsDocumentComplete()) |
+ if (doc_loader_.IsDocumentComplete()) |
FinishLoadingDocument(); |
} |
void PDFiumEngine::LoadPageInfo(bool reload) { |
- if (!doc_loader_) |
- return; |
pending_pages_.clear(); |
pp::Size old_document_size = document_size_; |
document_size_ = pp::Size(); |
std::vector<pp::Rect> page_rects; |
int page_count = FPDF_GetPageCount(doc_); |
- bool doc_complete = doc_loader_->IsDocumentComplete(); |
+ bool doc_complete = doc_loader_.IsDocumentComplete(); |
bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED; |
for (int i = 0; i < page_count; ++i) { |
if (i != 0) { |
@@ -2795,12 +2776,10 @@ void PDFiumEngine::LoadPageInfo(bool reload) { |
} |
void PDFiumEngine::CalculateVisiblePages() { |
- if (!doc_loader_) |
- return; |
// Clear pending requests queue, since it may contain requests to the pages |
// that are already invisible (after scrolling for example). |
pending_pages_.clear(); |
- doc_loader_->ClearPendingRequests(); |
+ doc_loader_.ClearPendingRequests(); |
visible_pages_.clear(); |
pp::Rect visible_rect(plugin_size_); |
@@ -2861,7 +2840,7 @@ void PDFiumEngine::ScrollToPage(int page) { |
} |
bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { |
- if (!doc_) |
+ if (!doc_ || !form_) |
return false; |
const int num_pages = static_cast<int>(pages_.size()); |