Chromium Code Reviews| Index: pdf/pdfium/pdfium_engine.cc |
| diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc |
| index f3ae32c109488f52f10438a9ddf030c339d2c431..c845f3b45b16099ea086ff73dcf24a361968cadc 100644 |
| --- a/pdf/pdfium/pdfium_engine.cc |
| +++ b/pdf/pdfium/pdfium_engine.cc |
| @@ -17,6 +17,7 @@ |
| #include "base/lazy_instance.h" |
| #include "base/logging.h" |
| #include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| #include "base/numerics/safe_conversions.h" |
| #include "base/stl_util.h" |
| #include "base/strings/string_number_conversions.h" |
| @@ -30,6 +31,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 +534,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 +560,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 +840,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* engine = static_cast<PDFiumEngine*>(param); |
| + return engine->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 +990,30 @@ void PDFiumEngine::PostPaint() { |
| bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { |
| password_tries_remaining_ = kMaxPasswordTries; |
| - return doc_loader_.Init(loader, url_, headers_); |
| + 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; |
| } |
| 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(), |
|
Lei Zhang
2016/10/21 09:33:09
Can you assign instead of using reset(), perhaps w
snake
2016/10/21 15:13:15
Done.
|
| + client_->CreateURLLoader())); |
| + |
| + return result; |
| } |
| void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { |
| @@ -1022,35 +1038,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 (!process_when_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. |
| + process_when_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 +1085,51 @@ 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 (!fpdf_availability_) { |
| - fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
| - DCHECK(fpdf_availability_); |
| - } |
| - LoadDocument(); |
| - return; |
| + 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_); |
| + } |
| + LoadDocument(); |
| +} |
| - FinishLoadingDocument(); |
| +void PDFiumEngine::OnDocumentCanceled() { |
| + OnDocumentComplete(); |
| +} |
| + |
| +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()); |