Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(942)

Unified Diff: pdf/pdfium/pdfium_engine.cc

Issue 2349753003: Improve linearized pdf load/show time. (Closed)
Patch Set: remove useless code. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pdf/pdfium/pdfium_engine.cc
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index f3ae32c109488f52f10438a9ddf030c339d2c431..6b8f7073a09e5e5a50b32102c877a60944a18f55 100644
--- a/pdf/pdfium/pdfium_engine.cc
+++ b/pdf/pdfium/pdfium_engine.cc
@@ -30,6 +30,7 @@
#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"
@@ -532,7 +533,6 @@ 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),
@@ -559,15 +559,15 @@ PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
file_access_.m_FileLen = 0;
file_access_.m_GetBlock = &GetBlock;
- file_access_.m_Param = &doc_loader_;
+ file_access_.m_Param = this;
file_availability_.version = 1;
file_availability_.IsDataAvail = &IsDataAvail;
- file_availability_.loader = &doc_loader_;
+ file_availability_.engine = this;
download_hints_.version = 1;
download_hints_.AddSegment = &AddSegment;
- download_hints_.loader = &doc_loader_;
+ download_hints_.engine = this;
// Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct
// allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in
@@ -839,22 +839,22 @@ int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param,
int PDFiumEngine::GetBlock(void* param, unsigned long position,
unsigned char* buffer, unsigned long size) {
- DocumentLoader* loader = static_cast<DocumentLoader*>(param);
- return loader->GetBlock(position, size, buffer);
+ PDFiumEngine* p_this = static_cast<PDFiumEngine*>(param);
Lei Zhang 2016/10/05 07:18:08 s/p_this/engine/ ?
snake 2016/10/05 14:14:10 Done.
+ return p_this->doc_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->loader->IsDataAvailable(offset, size);
+ return file_avail->engine->doc_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->loader->RequestData(offset, size);
+ return download_hints->engine->doc_loader_->RequestData(offset, size);
}
bool PDFiumEngine::New(const char* url,
@@ -989,15 +989,30 @@ void PDFiumEngine::PostPaint() {
bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) {
password_tries_remaining_ = kMaxPasswordTries;
- return doc_loader_.Init(loader, url_, headers_);
+ should_process_pending_request_complete_ = true;
+ URLLoaderWrapperImpl* loader_wrapper_ptr =
Lei Zhang 2016/10/05 07:18:07 Why bother with the raw pointer? Just: auto loade
snake 2016/10/05 14:14:10 Done.
+ new URLLoaderWrapperImpl(GetPluginInstance(), loader);
+ loader_wrapper_ptr->SetResponseHeaders(headers_);
+ std::unique_ptr<URLLoaderWrapper> loader_wrapper(loader_wrapper_ptr);
+ doc_loader_.reset(new DocumentLoader(this));
Lei Zhang 2016/10/05 07:18:08 More base::MakeUnique?
snake 2016/10/05 14:14:10 Done.
+ if (doc_loader_->Init(std::move(loader_wrapper), url_)) {
+ // request initial data.
+ doc_loader_->RequestData(0, 1);
+ return true;
+ }
+ return false;
}
pp::Instance* PDFiumEngine::GetPluginInstance() {
return client_->GetPluginInstance();
}
-pp::URLLoader PDFiumEngine::CreateURLLoader() {
- return client_->CreateURLLoader();
+std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() {
+ std::unique_ptr<URLLoaderWrapper> result;
+ result.reset(new URLLoaderWrapperImpl(GetPluginInstance(),
+ client_->CreateURLLoader()));
+
+ return result;
}
void PDFiumEngine::AppendPage(PDFEngine* engine, int index) {
@@ -1022,35 +1037,32 @@ 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::OnPartialDocumentLoaded() {
- file_access_.m_FileLen = doc_loader_.document_size();
+void PDFiumEngine::OnPendingRequestComplete() {
+ if (!should_process_pending_request_complete_)
+ return;
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.
+ should_process_pending_request_complete_ = false;
+ FPDFAvail_Destroy(fpdf_availability_);
+ fpdf_availability_ = nullptr;
+ return;
+ }
}
- // 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_);
+ if (!doc_) {
LoadDocument();
return;
}
@@ -1072,26 +1084,52 @@ void PDFiumEngine::OnPendingRequestComplete() {
}
void PDFiumEngine::OnNewDataAvailable() {
- client_->DocumentLoadProgress(doc_loader_.GetAvailableData(),
- doc_loader_.document_size());
+ const float progress = doc_loader_->GetProgress();
+ if (progress < 0.001) {
+ client_->DocumentLoadProgress(0, 0);
+ } else {
+ client_->DocumentLoadProgress(progress * 10000, 10000);
+ }
}
void PDFiumEngine::OnDocumentComplete() {
- if (!doc_ || !form_) {
- file_access_.m_FileLen = doc_loader_.document_size();
+ if (!doc_) {
Lei Zhang 2016/10/05 07:18:08 In general, if (!foo) { bar(); } else { qux(); } m
snake 2016/10/05 14:14:10 Done.
+ file_access_.m_FileLen = doc_loader_->GetDocumentSize();
if (!fpdf_availability_) {
fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
DCHECK(fpdf_availability_);
}
LoadDocument();
- return;
+ } else {
+ FinishLoadingDocument();
Lei Zhang 2016/10/05 07:18:07 Or how about just: if (doc_) { FinishLoadingDoc
snake 2016/10/05 14:14:10 Done.
}
+}
+
+void PDFiumEngine::OnDocumentCanceled() {
+ OnDocumentComplete();
+}
- FinishLoadingDocument();
+void PDFiumEngine::CancelBrowserDownload() {
+ client_->CancelBrowserDownload();
}
void PDFiumEngine::FinishLoadingDocument() {
- DCHECK(doc_loader_.IsDocumentComplete() && doc_);
+ 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);
+ }
+ }
bool need_update = false;
for (size_t i = 0; i < pages_.size(); ++i) {
@@ -1298,7 +1336,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();
@@ -2436,7 +2474,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;
}
@@ -2474,7 +2512,7 @@ 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 {
@@ -2531,26 +2569,7 @@ void PDFiumEngine::ContinueLoadingDocument(const std::string& password) {
permissions_ = FPDF_GetDocPermissions(doc_);
permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_);
- 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()) {
+ 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.
@@ -2559,7 +2578,7 @@ void PDFiumEngine::ContinueLoadingDocument(const std::string& password) {
LoadPageInfo(false);
- if (doc_loader_.IsDocumentComplete())
+ if (doc_loader_->IsDocumentComplete())
FinishLoadingDocument();
}
@@ -2569,7 +2588,7 @@ void PDFiumEngine::LoadPageInfo(bool reload) {
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) {
@@ -2626,10 +2645,12 @@ 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_);
@@ -2687,7 +2708,7 @@ void PDFiumEngine::ScrollToPage(int page) {
}
bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) {
- if (!doc_ || !form_)
+ if (!doc_)
return false;
const int num_pages = static_cast<int>(pages_.size());

Powered by Google App Engine
This is Rietveld 408576698