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

Side by Side Diff: pdf/pdfium/pdfium_engine.cc

Issue 2455403002: Reland of Improve linearized pdf load/show time. (Closed)
Patch Set: Fix review issues Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | pdf/preview_mode_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "pdf/pdfium/pdfium_engine.h" 5 #include "pdf/pdfium/pdfium_engine.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "base/strings/string_piece.h" 24 #include "base/strings/string_piece.h"
25 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h" 26 #include "base/strings/utf_string_conversions.h"
27 #include "gin/array_buffer.h" 27 #include "gin/array_buffer.h"
28 #include "gin/public/gin_embedders.h" 28 #include "gin/public/gin_embedders.h"
29 #include "gin/public/isolate_holder.h" 29 #include "gin/public/isolate_holder.h"
30 #include "pdf/draw_utils.h" 30 #include "pdf/draw_utils.h"
31 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" 31 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
32 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h" 32 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h"
33 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h" 33 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
34 #include "pdf/url_loader_wrapper_impl.h"
34 #include "ppapi/c/pp_errors.h" 35 #include "ppapi/c/pp_errors.h"
35 #include "ppapi/c/pp_input_event.h" 36 #include "ppapi/c/pp_input_event.h"
36 #include "ppapi/c/ppb_core.h" 37 #include "ppapi/c/ppb_core.h"
37 #include "ppapi/c/private/ppb_pdf.h" 38 #include "ppapi/c/private/ppb_pdf.h"
38 #include "ppapi/cpp/dev/memory_dev.h" 39 #include "ppapi/cpp/dev/memory_dev.h"
39 #include "ppapi/cpp/input_event.h" 40 #include "ppapi/cpp/input_event.h"
40 #include "ppapi/cpp/instance.h" 41 #include "ppapi/cpp/instance.h"
41 #include "ppapi/cpp/module.h" 42 #include "ppapi/cpp/module.h"
42 #include "ppapi/cpp/private/pdf.h" 43 #include "ppapi/cpp/private/pdf.h"
43 #include "ppapi/cpp/trusted/browser_font_trusted.h" 44 #include "ppapi/cpp/trusted/browser_font_trusted.h"
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 } 532 }
532 533
533 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { 534 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) {
534 return new PDFiumEngine(client); 535 return new PDFiumEngine(client);
535 } 536 }
536 537
537 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) 538 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
538 : client_(client), 539 : client_(client),
539 current_zoom_(1.0), 540 current_zoom_(1.0),
540 current_rotation_(0), 541 current_rotation_(0),
541 doc_loader_(this),
542 password_tries_remaining_(0), 542 password_tries_remaining_(0),
543 doc_(nullptr), 543 doc_(nullptr),
544 form_(nullptr), 544 form_(nullptr),
545 defer_page_unload_(false), 545 defer_page_unload_(false),
546 selecting_(false), 546 selecting_(false),
547 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA, 547 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA,
548 PDFiumPage::LinkTarget()), 548 PDFiumPage::LinkTarget()),
549 next_page_to_search_(-1), 549 next_page_to_search_(-1),
550 last_page_to_search_(-1), 550 last_page_to_search_(-1),
551 last_character_index_to_search_(-1), 551 last_character_index_to_search_(-1),
552 permissions_(0), 552 permissions_(0),
553 permissions_handler_revision_(-1), 553 permissions_handler_revision_(-1),
554 fpdf_availability_(nullptr), 554 fpdf_availability_(nullptr),
555 next_timer_id_(0), 555 next_timer_id_(0),
556 last_page_mouse_down_(-1), 556 last_page_mouse_down_(-1),
557 most_visible_page_(-1), 557 most_visible_page_(-1),
558 called_do_document_action_(false), 558 called_do_document_action_(false),
559 render_grayscale_(false), 559 render_grayscale_(false),
560 render_annots_(true), 560 render_annots_(true),
561 progressive_paint_timeout_(0), 561 progressive_paint_timeout_(0),
562 getting_password_(false) { 562 getting_password_(false) {
563 find_factory_.Initialize(this); 563 find_factory_.Initialize(this);
564 password_factory_.Initialize(this); 564 password_factory_.Initialize(this);
565 565
566 file_access_.m_FileLen = 0; 566 file_access_.m_FileLen = 0;
567 file_access_.m_GetBlock = &GetBlock; 567 file_access_.m_GetBlock = &GetBlock;
568 file_access_.m_Param = &doc_loader_; 568 file_access_.m_Param = this;
569 569
570 file_availability_.version = 1; 570 file_availability_.version = 1;
571 file_availability_.IsDataAvail = &IsDataAvail; 571 file_availability_.IsDataAvail = &IsDataAvail;
572 file_availability_.loader = &doc_loader_; 572 file_availability_.engine = this;
573 573
574 download_hints_.version = 1; 574 download_hints_.version = 1;
575 download_hints_.AddSegment = &AddSegment; 575 download_hints_.AddSegment = &AddSegment;
576 download_hints_.loader = &doc_loader_; 576 download_hints_.engine = this;
577 577
578 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct 578 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct
579 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in 579 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in
580 // callbacks to ourself instead of maintaining a map of them to 580 // callbacks to ourself instead of maintaining a map of them to
581 // PDFiumEngine. 581 // PDFiumEngine.
582 FPDF_FORMFILLINFO::version = 1; 582 FPDF_FORMFILLINFO::version = 1;
583 FPDF_FORMFILLINFO::m_pJsPlatform = this; 583 FPDF_FORMFILLINFO::m_pJsPlatform = this;
584 FPDF_FORMFILLINFO::Release = nullptr; 584 FPDF_FORMFILLINFO::Release = nullptr;
585 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate; 585 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate;
586 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect; 586 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param, 836 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param,
837 void* language, 837 void* language,
838 int length) { 838 int length) {
839 return 0; 839 return 0;
840 } 840 }
841 841
842 #endif // defined(PDF_ENABLE_XFA) 842 #endif // defined(PDF_ENABLE_XFA)
843 843
844 int PDFiumEngine::GetBlock(void* param, unsigned long position, 844 int PDFiumEngine::GetBlock(void* param, unsigned long position,
845 unsigned char* buffer, unsigned long size) { 845 unsigned char* buffer, unsigned long size) {
846 DocumentLoader* loader = static_cast<DocumentLoader*>(param); 846 PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
847 return loader->GetBlock(position, size, buffer); 847 return engine->doc_loader_->GetBlock(position, size, buffer);
848 } 848 }
849 849
850 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, 850 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param,
851 size_t offset, size_t size) { 851 size_t offset, size_t size) {
852 PDFiumEngine::FileAvail* file_avail = 852 PDFiumEngine::FileAvail* file_avail =
853 static_cast<PDFiumEngine::FileAvail*>(param); 853 static_cast<PDFiumEngine::FileAvail*>(param);
854 return file_avail->loader->IsDataAvailable(offset, size); 854 return file_avail->engine->doc_loader_->IsDataAvailable(offset, size);
855 } 855 }
856 856
857 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, 857 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param,
858 size_t offset, size_t size) { 858 size_t offset, size_t size) {
859 PDFiumEngine::DownloadHints* download_hints = 859 PDFiumEngine::DownloadHints* download_hints =
860 static_cast<PDFiumEngine::DownloadHints*>(param); 860 static_cast<PDFiumEngine::DownloadHints*>(param);
861 return download_hints->loader->RequestData(offset, size); 861 return download_hints->engine->doc_loader_->RequestData(offset, size);
862 } 862 }
863 863
864 bool PDFiumEngine::New(const char* url, 864 bool PDFiumEngine::New(const char* url,
865 const char* headers) { 865 const char* headers) {
866 url_ = url; 866 url_ = url;
867 if (headers) 867 if (headers)
868 headers_ = headers; 868 headers_ = headers;
869 else 869 else
870 headers_.clear(); 870 headers_.clear();
871 return true; 871 return true;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 FPDF_RenderPage_Close( 986 FPDF_RenderPage_Close(
987 pages_[progressive_paints_[i].page_index]->GetPage()); 987 pages_[progressive_paints_[i].page_index]->GetPage());
988 FPDFBitmap_Destroy(progressive_paints_[i].bitmap); 988 FPDFBitmap_Destroy(progressive_paints_[i].bitmap);
989 progressive_paints_.erase(progressive_paints_.begin() + i); 989 progressive_paints_.erase(progressive_paints_.begin() + i);
990 --i; 990 --i;
991 } 991 }
992 } 992 }
993 993
994 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { 994 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) {
995 password_tries_remaining_ = kMaxPasswordTries; 995 password_tries_remaining_ = kMaxPasswordTries;
996 return doc_loader_.Init(loader, url_, headers_); 996 process_when_pending_request_complete_ = true;
997 auto loader_wrapper =
998 base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), loader);
999 loader_wrapper->SetResponseHeaders(headers_);
1000
1001 doc_loader_ = base::MakeUnique<DocumentLoader>(this);
1002 if (doc_loader_->Init(std::move(loader_wrapper), url_)) {
1003 // request initial data.
1004 doc_loader_->RequestData(0, 1);
1005 return true;
1006 }
1007 return false;
997 } 1008 }
998 1009
999 pp::Instance* PDFiumEngine::GetPluginInstance() { 1010 pp::Instance* PDFiumEngine::GetPluginInstance() {
1000 return client_->GetPluginInstance(); 1011 return client_->GetPluginInstance();
1001 } 1012 }
1002 1013
1003 pp::URLLoader PDFiumEngine::CreateURLLoader() { 1014 std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() {
1004 return client_->CreateURLLoader(); 1015 return base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(),
1016 client_->CreateURLLoader());
1005 } 1017 }
1006 1018
1007 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { 1019 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) {
1008 // Unload and delete the blank page before appending. 1020 // Unload and delete the blank page before appending.
1009 pages_[index]->Unload(); 1021 pages_[index]->Unload();
1010 pages_[index]->set_calculated_links(false); 1022 pages_[index]->set_calculated_links(false);
1011 pp::Size curr_page_size = GetPageSize(index); 1023 pp::Size curr_page_size = GetPageSize(index);
1012 FPDFPage_Delete(doc_, index); 1024 FPDFPage_Delete(doc_, index);
1013 FPDF_ImportPages(doc_, 1025 FPDF_ImportPages(doc_,
1014 static_cast<PDFiumEngine*>(engine)->doc(), 1026 static_cast<PDFiumEngine*>(engine)->doc(),
1015 "1", 1027 "1",
1016 index); 1028 index);
1017 pp::Size new_page_size = GetPageSize(index); 1029 pp::Size new_page_size = GetPageSize(index);
1018 if (curr_page_size != new_page_size) 1030 if (curr_page_size != new_page_size)
1019 LoadPageInfo(true); 1031 LoadPageInfo(true);
1020 client_->Invalidate(GetPageScreenRect(index)); 1032 client_->Invalidate(GetPageScreenRect(index));
1021 } 1033 }
1022 1034
1023 #if defined(PDF_ENABLE_XFA) 1035 #if defined(PDF_ENABLE_XFA)
1024 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { 1036 void PDFiumEngine::SetScrollPosition(const pp::Point& position) {
1025 position_ = position; 1037 position_ = position;
1026 } 1038 }
1027 #endif 1039 #endif
1028 1040
1029 bool PDFiumEngine::IsProgressiveLoad() {
1030 return doc_loader_.is_partial_document();
1031 }
1032
1033 std::string PDFiumEngine::GetMetadata(const std::string& key) { 1041 std::string PDFiumEngine::GetMetadata(const std::string& key) {
1034 return GetDocumentMetadata(doc(), key); 1042 return GetDocumentMetadata(doc(), key);
1035 } 1043 }
1036 1044
1037 void PDFiumEngine::OnPartialDocumentLoaded() { 1045 void PDFiumEngine::OnPendingRequestComplete() {
1038 file_access_.m_FileLen = doc_loader_.document_size(); 1046 if (!process_when_pending_request_complete_)
1047 return;
1039 if (!fpdf_availability_) { 1048 if (!fpdf_availability_) {
1049 file_access_.m_FileLen = doc_loader_->GetDocumentSize();
1040 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); 1050 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
1041 DCHECK(fpdf_availability_); 1051 DCHECK(fpdf_availability_);
1052 // Currently engine does not deal efficiently with some non-linearized
1053 // files.
1054 // See http://code.google.com/p/chromium/issues/detail?id=59400
1055 // To improve user experience we download entire file for non-linearized
1056 // PDF.
1057 if (FPDFAvail_IsLinearized(fpdf_availability_) != PDF_LINEARIZED) {
1058 // Wait complete document.
1059 process_when_pending_request_complete_ = false;
1060 FPDFAvail_Destroy(fpdf_availability_);
1061 fpdf_availability_ = nullptr;
1062 return;
1063 }
1042 } 1064 }
1043 1065
1044 // Currently engine does not deal efficiently with some non-linearized files. 1066 if (!doc_) {
1045 // See http://code.google.com/p/chromium/issues/detail?id=59400
1046 // To improve user experience we download entire file for non-linearized PDF.
1047 if (!FPDFAvail_IsLinearized(fpdf_availability_)) {
1048 doc_loader_.RequestData(0, doc_loader_.document_size());
1049 return;
1050 }
1051
1052 LoadDocument();
1053 }
1054
1055 void PDFiumEngine::OnPendingRequestComplete() {
1056 if (!doc_ || !form_) {
1057 DCHECK(fpdf_availability_);
1058 LoadDocument(); 1067 LoadDocument();
1059 return; 1068 return;
1060 } 1069 }
1061 1070
1062 // LoadDocument() will result in |pending_pages_| being reset so there's no 1071 // LoadDocument() will result in |pending_pages_| being reset so there's no
1063 // need to run the code below in that case. 1072 // need to run the code below in that case.
1064 bool update_pages = false; 1073 bool update_pages = false;
1065 std::vector<int> still_pending; 1074 std::vector<int> still_pending;
1066 for (int pending_page : pending_pages_) { 1075 for (int pending_page : pending_pages_) {
1067 if (CheckPageAvailable(pending_page, &still_pending)) { 1076 if (CheckPageAvailable(pending_page, &still_pending)) {
1068 update_pages = true; 1077 update_pages = true;
1069 if (IsPageVisible(pending_page)) 1078 if (IsPageVisible(pending_page))
1070 client_->Invalidate(GetPageScreenRect(pending_page)); 1079 client_->Invalidate(GetPageScreenRect(pending_page));
1071 } 1080 }
1072 } 1081 }
1073 pending_pages_.swap(still_pending); 1082 pending_pages_.swap(still_pending);
1074 if (update_pages) 1083 if (update_pages)
1075 LoadPageInfo(true); 1084 LoadPageInfo(true);
1076 } 1085 }
1077 1086
1078 void PDFiumEngine::OnNewDataAvailable() { 1087 void PDFiumEngine::OnNewDataAvailable() {
1079 client_->DocumentLoadProgress(doc_loader_.GetAvailableData(), 1088 const float progress = doc_loader_->GetProgress();
1080 doc_loader_.document_size()); 1089 if (progress < 0.001) {
1090 client_->DocumentLoadProgress(0, 0);
1091 } else {
1092 client_->DocumentLoadProgress(progress * 10000, 10000);
1093 }
1081 } 1094 }
1082 1095
1083 void PDFiumEngine::OnDocumentComplete() { 1096 void PDFiumEngine::OnDocumentComplete() {
1084 if (!doc_ || !form_) { 1097 if (doc_) {
1085 file_access_.m_FileLen = doc_loader_.document_size(); 1098 return FinishLoadingDocument();
1086 if (!fpdf_availability_) {
1087 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
1088 DCHECK(fpdf_availability_);
1089 }
1090 LoadDocument();
1091 return;
1092 } 1099 }
1100 file_access_.m_FileLen = doc_loader_->GetDocumentSize();
1101 if (!fpdf_availability_) {
1102 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
1103 DCHECK(fpdf_availability_);
1104 }
1105 LoadDocument();
1106 }
1093 1107
1094 FinishLoadingDocument(); 1108 void PDFiumEngine::OnDocumentCanceled() {
1109 OnDocumentComplete();
1110 }
1111
1112 void PDFiumEngine::CancelBrowserDownload() {
1113 client_->CancelBrowserDownload();
1095 } 1114 }
1096 1115
1097 void PDFiumEngine::FinishLoadingDocument() { 1116 void PDFiumEngine::FinishLoadingDocument() {
1098 DCHECK(doc_loader_.IsDocumentComplete() && doc_); 1117 DCHECK(doc_loader_->IsDocumentComplete() && doc_);
1118
1119 if (!form_) {
1120 int form_status =
1121 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_);
1122 if (form_status != PDF_FORM_NOTAVAIL) {
1123 form_ = FPDFDOC_InitFormFillEnvironment(
1124 doc_, static_cast<FPDF_FORMFILLINFO*>(this));
1125 #if defined(PDF_ENABLE_XFA)
1126 FPDF_LoadXFA(doc_);
1127 #endif
1128
1129 FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor);
1130 FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha);
1131 }
1132 }
1099 1133
1100 bool need_update = false; 1134 bool need_update = false;
1101 for (size_t i = 0; i < pages_.size(); ++i) { 1135 for (size_t i = 0; i < pages_.size(); ++i) {
1102 if (pages_[i]->available()) 1136 if (pages_[i]->available())
1103 continue; 1137 continue;
1104 1138
1105 pages_[i]->set_available(true); 1139 pages_[i]->set_available(true);
1106 // We still need to call IsPageAvail() even if the whole document is 1140 // We still need to call IsPageAvail() even if the whole document is
1107 // already downloaded. 1141 // already downloaded.
1108 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_); 1142 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_);
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 return temp_doc; 1334 return temp_doc;
1301 } 1335 }
1302 1336
1303 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( 1337 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF(
1304 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1338 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1305 const PP_PrintSettings_Dev& print_settings) { 1339 const PP_PrintSettings_Dev& print_settings) {
1306 if (!page_range_count) 1340 if (!page_range_count)
1307 return pp::Buffer_Dev(); 1341 return pp::Buffer_Dev();
1308 1342
1309 // If document is not downloaded yet, disable printing. 1343 // If document is not downloaded yet, disable printing.
1310 if (doc_ && !doc_loader_.IsDocumentComplete()) 1344 if (doc_ && !doc_loader_->IsDocumentComplete())
1311 return pp::Buffer_Dev(); 1345 return pp::Buffer_Dev();
1312 1346
1313 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); 1347 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
1314 if (!output_doc) 1348 if (!output_doc)
1315 return pp::Buffer_Dev(); 1349 return pp::Buffer_Dev();
1316 1350
1317 SaveSelectedFormForPrint(); 1351 SaveSelectedFormForPrint();
1318 1352
1319 std::vector<PDFiumPage> pages_to_print; 1353 std::vector<PDFiumPage> pages_to_print;
1320 // width and height of source PDF pages. 1354 // width and height of source PDF pages.
(...skipping 1123 matching lines...) Expand 10 before | Expand all | Expand 10 after
2444 } 2478 }
2445 2479
2446 CalculateVisiblePages(); 2480 CalculateVisiblePages();
2447 if (document_size_ != old_document_size) 2481 if (document_size_ != old_document_size)
2448 client_->DocumentSizeUpdated(document_size_); 2482 client_->DocumentSizeUpdated(document_size_);
2449 } 2483 }
2450 2484
2451 void PDFiumEngine::LoadDocument() { 2485 void PDFiumEngine::LoadDocument() {
2452 // Check if the document is ready for loading. If it isn't just bail for now, 2486 // Check if the document is ready for loading. If it isn't just bail for now,
2453 // we will call LoadDocument() again later. 2487 // we will call LoadDocument() again later.
2454 if (!doc_ && !doc_loader_.IsDocumentComplete() && 2488 if (!doc_ && !doc_loader_->IsDocumentComplete() &&
2455 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { 2489 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) {
2456 return; 2490 return;
2457 } 2491 }
2458 2492
2459 // If we're in the middle of getting a password, just return. We will retry 2493 // If we're in the middle of getting a password, just return. We will retry
2460 // loading the document after we get the password anyway. 2494 // loading the document after we get the password anyway.
2461 if (getting_password_) 2495 if (getting_password_)
2462 return; 2496 return;
2463 2497
2464 ScopedUnsupportedFeature scoped_unsupported_feature(this); 2498 ScopedUnsupportedFeature scoped_unsupported_feature(this);
(...skipping 18 matching lines...) Expand all
2483 // call may have failed, so call it again for good measure. 2517 // call may have failed, so call it again for good measure.
2484 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); 2518 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_);
2485 return true; 2519 return true;
2486 } 2520 }
2487 2521
2488 const char* password_cstr = nullptr; 2522 const char* password_cstr = nullptr;
2489 if (!password.empty()) { 2523 if (!password.empty()) {
2490 password_cstr = password.c_str(); 2524 password_cstr = password.c_str();
2491 password_tries_remaining_--; 2525 password_tries_remaining_--;
2492 } 2526 }
2493 if (doc_loader_.IsDocumentComplete() && 2527 if (doc_loader_->IsDocumentComplete() &&
2494 !FPDFAvail_IsLinearized(fpdf_availability_)) { 2528 !FPDFAvail_IsLinearized(fpdf_availability_)) {
2495 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); 2529 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr);
2496 } else { 2530 } else {
2497 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); 2531 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr);
2498 } 2532 }
2499 if (!doc_) { 2533 if (!doc_) {
2500 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD) 2534 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD)
2501 *needs_password = true; 2535 *needs_password = true;
2502 return false; 2536 return false;
2503 } 2537 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2541 client_->DocumentLoadFailed(); 2575 client_->DocumentLoadFailed();
2542 return; 2576 return;
2543 } 2577 }
2544 2578
2545 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES) 2579 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES)
2546 client_->DocumentHasUnsupportedFeature("Bookmarks"); 2580 client_->DocumentHasUnsupportedFeature("Bookmarks");
2547 2581
2548 permissions_ = FPDF_GetDocPermissions(doc_); 2582 permissions_ = FPDF_GetDocPermissions(doc_);
2549 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_); 2583 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_);
2550 2584
2551 if (!form_) { 2585 if (!doc_loader_->IsDocumentComplete()) {
2552 int form_status =
2553 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_);
2554 bool doc_complete = doc_loader_.IsDocumentComplete();
2555 // Try again if the data is not available and the document hasn't finished
2556 // downloading.
2557 if (form_status == PDF_FORM_NOTAVAIL && !doc_complete)
2558 return;
2559
2560 form_ = FPDFDOC_InitFormFillEnvironment(
2561 doc_, static_cast<FPDF_FORMFILLINFO*>(this));
2562 #if defined(PDF_ENABLE_XFA)
2563 FPDF_LoadXFA(doc_);
2564 #endif
2565
2566 FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor);
2567 FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha);
2568 }
2569
2570 if (!doc_loader_.IsDocumentComplete()) {
2571 // Check if the first page is available. In a linearized PDF, that is not 2586 // Check if the first page is available. In a linearized PDF, that is not
2572 // always page 0. Doing this gives us the default page size, since when the 2587 // always page 0. Doing this gives us the default page size, since when the
2573 // document is available, the first page is available as well. 2588 // document is available, the first page is available as well.
2574 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_); 2589 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_);
2575 } 2590 }
2576 2591
2577 LoadPageInfo(false); 2592 LoadPageInfo(false);
2578 2593
2579 if (doc_loader_.IsDocumentComplete()) 2594 if (doc_loader_->IsDocumentComplete())
2580 FinishLoadingDocument(); 2595 FinishLoadingDocument();
2581 } 2596 }
2582 2597
2583 void PDFiumEngine::LoadPageInfo(bool reload) { 2598 void PDFiumEngine::LoadPageInfo(bool reload) {
2584 pending_pages_.clear(); 2599 pending_pages_.clear();
2585 pp::Size old_document_size = document_size_; 2600 pp::Size old_document_size = document_size_;
2586 document_size_ = pp::Size(); 2601 document_size_ = pp::Size();
2587 std::vector<pp::Rect> page_rects; 2602 std::vector<pp::Rect> page_rects;
2588 int page_count = FPDF_GetPageCount(doc_); 2603 int page_count = FPDF_GetPageCount(doc_);
2589 bool doc_complete = doc_loader_.IsDocumentComplete(); 2604 bool doc_complete = doc_loader_->IsDocumentComplete();
2590 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED; 2605 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED;
2591 for (int i = 0; i < page_count; ++i) { 2606 for (int i = 0; i < page_count; ++i) {
2592 if (i != 0) { 2607 if (i != 0) {
2593 // Add space for horizontal separator. 2608 // Add space for horizontal separator.
2594 document_size_.Enlarge(0, kPageSeparatorThickness); 2609 document_size_.Enlarge(0, kPageSeparatorThickness);
2595 } 2610 }
2596 2611
2597 // Get page availability. If |reload| == true, then the document has been 2612 // Get page availability. If |reload| == true, then the document has been
2598 // constructed already. Get page availability flag from already existing 2613 // constructed already. Get page availability flag from already existing
2599 // PDFiumPage class. 2614 // PDFiumPage class.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2636 pages_.push_back(base::MakeUnique<PDFiumPage>(this, i, page_rect, false)); 2651 pages_.push_back(base::MakeUnique<PDFiumPage>(this, i, page_rect, false));
2637 } 2652 }
2638 } 2653 }
2639 2654
2640 CalculateVisiblePages(); 2655 CalculateVisiblePages();
2641 if (document_size_ != old_document_size) 2656 if (document_size_ != old_document_size)
2642 client_->DocumentSizeUpdated(document_size_); 2657 client_->DocumentSizeUpdated(document_size_);
2643 } 2658 }
2644 2659
2645 void PDFiumEngine::CalculateVisiblePages() { 2660 void PDFiumEngine::CalculateVisiblePages() {
2661 if (!doc_loader_)
2662 return;
2646 // Clear pending requests queue, since it may contain requests to the pages 2663 // Clear pending requests queue, since it may contain requests to the pages
2647 // that are already invisible (after scrolling for example). 2664 // that are already invisible (after scrolling for example).
2648 pending_pages_.clear(); 2665 pending_pages_.clear();
2649 doc_loader_.ClearPendingRequests(); 2666 doc_loader_->ClearPendingRequests();
2650 2667
2651 visible_pages_.clear(); 2668 visible_pages_.clear();
2652 pp::Rect visible_rect(plugin_size_); 2669 pp::Rect visible_rect(plugin_size_);
2653 for (size_t i = 0; i < pages_.size(); ++i) { 2670 for (size_t i = 0; i < pages_.size(); ++i) {
2654 // Check an entire PageScreenRect, since we might need to repaint side 2671 // Check an entire PageScreenRect, since we might need to repaint side
2655 // borders and shadows even if the page itself is not visible. 2672 // borders and shadows even if the page itself is not visible.
2656 // For example, when user use pdf with different page sizes and zoomed in 2673 // For example, when user use pdf with different page sizes and zoomed in
2657 // outside page area. 2674 // outside page area.
2658 if (visible_rect.Intersects(GetPageScreenRect(i))) { 2675 if (visible_rect.Intersects(GetPageScreenRect(i))) {
2659 visible_pages_.push_back(i); 2676 visible_pages_.push_back(i);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2700 2717
2701 void PDFiumEngine::ScrollToPage(int page) { 2718 void PDFiumEngine::ScrollToPage(int page) {
2702 if (!PageIndexInBounds(page)) 2719 if (!PageIndexInBounds(page))
2703 return; 2720 return;
2704 2721
2705 in_flight_visible_page_ = page; 2722 in_flight_visible_page_ = page;
2706 client_->ScrollToPage(page); 2723 client_->ScrollToPage(page);
2707 } 2724 }
2708 2725
2709 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { 2726 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) {
2710 if (!doc_ || !form_) 2727 if (!doc_)
2711 return false; 2728 return false;
2712 2729
2713 const int num_pages = static_cast<int>(pages_.size()); 2730 const int num_pages = static_cast<int>(pages_.size());
2714 if (index < num_pages && pages_[index]->available()) 2731 if (index < num_pages && pages_[index]->available())
2715 return true; 2732 return true;
2716 2733
2717 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) { 2734 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) {
2718 if (!base::ContainsValue(*pending, index)) 2735 if (!base::ContainsValue(*pending, index))
2719 pending->push_back(index); 2736 pending->push_back(index);
2720 return false; 2737 return false;
(...skipping 1285 matching lines...) Expand 10 before | Expand all | Expand 10 after
4006 FPDF_DOCUMENT doc = 4023 FPDF_DOCUMENT doc =
4007 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4024 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4008 if (!doc) 4025 if (!doc)
4009 return false; 4026 return false;
4010 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4027 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4011 FPDF_CloseDocument(doc); 4028 FPDF_CloseDocument(doc);
4012 return success; 4029 return success;
4013 } 4030 }
4014 4031
4015 } // namespace chrome_pdf 4032 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | pdf/preview_mode_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698