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

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

Issue 2349753003: Improve linearized pdf load/show time. (Closed)
Patch Set: Fix review issues. Created 4 years, 2 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 unified diff | Download patch
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
11 #include <algorithm> 11 #include <algorithm>
12 #include <memory> 12 #include <memory>
13 #include <set> 13 #include <set>
14 14
15 #include "base/i18n/encoding_detection.h" 15 #include "base/i18n/encoding_detection.h"
16 #include "base/i18n/icu_string_conversions.h" 16 #include "base/i18n/icu_string_conversions.h"
17 #include "base/lazy_instance.h" 17 #include "base/lazy_instance.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ptr_util.h"
20 #include "base/numerics/safe_conversions.h" 21 #include "base/numerics/safe_conversions.h"
21 #include "base/stl_util.h" 22 #include "base/stl_util.h"
22 #include "base/strings/string_number_conversions.h" 23 #include "base/strings/string_number_conversions.h"
23 #include "base/strings/string_piece.h" 24 #include "base/strings/string_piece.h"
24 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h" 26 #include "base/strings/utf_string_conversions.h"
26 #include "gin/array_buffer.h" 27 #include "gin/array_buffer.h"
27 #include "gin/public/gin_embedders.h" 28 #include "gin/public/gin_embedders.h"
28 #include "gin/public/isolate_holder.h" 29 #include "gin/public/isolate_holder.h"
29 #include "pdf/draw_utils.h" 30 #include "pdf/draw_utils.h"
30 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" 31 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
31 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h" 32 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h"
32 #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"
33 #include "ppapi/c/pp_errors.h" 35 #include "ppapi/c/pp_errors.h"
34 #include "ppapi/c/pp_input_event.h" 36 #include "ppapi/c/pp_input_event.h"
35 #include "ppapi/c/ppb_core.h" 37 #include "ppapi/c/ppb_core.h"
36 #include "ppapi/c/private/ppb_pdf.h" 38 #include "ppapi/c/private/ppb_pdf.h"
37 #include "ppapi/cpp/dev/memory_dev.h" 39 #include "ppapi/cpp/dev/memory_dev.h"
38 #include "ppapi/cpp/input_event.h" 40 #include "ppapi/cpp/input_event.h"
39 #include "ppapi/cpp/instance.h" 41 #include "ppapi/cpp/instance.h"
40 #include "ppapi/cpp/module.h" 42 #include "ppapi/cpp/module.h"
41 #include "ppapi/cpp/private/pdf.h" 43 #include "ppapi/cpp/private/pdf.h"
42 #include "ppapi/cpp/trusted/browser_font_trusted.h" 44 #include "ppapi/cpp/trusted/browser_font_trusted.h"
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 } 527 }
526 528
527 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { 529 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) {
528 return new PDFiumEngine(client); 530 return new PDFiumEngine(client);
529 } 531 }
530 532
531 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) 533 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client)
532 : client_(client), 534 : client_(client),
533 current_zoom_(1.0), 535 current_zoom_(1.0),
534 current_rotation_(0), 536 current_rotation_(0),
535 doc_loader_(this),
536 password_tries_remaining_(0), 537 password_tries_remaining_(0),
537 doc_(nullptr), 538 doc_(nullptr),
538 form_(nullptr), 539 form_(nullptr),
539 defer_page_unload_(false), 540 defer_page_unload_(false),
540 selecting_(false), 541 selecting_(false),
541 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA, 542 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA,
542 PDFiumPage::LinkTarget()), 543 PDFiumPage::LinkTarget()),
543 next_page_to_search_(-1), 544 next_page_to_search_(-1),
544 last_page_to_search_(-1), 545 last_page_to_search_(-1),
545 last_character_index_to_search_(-1), 546 last_character_index_to_search_(-1),
546 permissions_(0), 547 permissions_(0),
547 permissions_handler_revision_(-1), 548 permissions_handler_revision_(-1),
548 fpdf_availability_(nullptr), 549 fpdf_availability_(nullptr),
549 next_timer_id_(0), 550 next_timer_id_(0),
550 last_page_mouse_down_(-1), 551 last_page_mouse_down_(-1),
551 most_visible_page_(-1), 552 most_visible_page_(-1),
552 called_do_document_action_(false), 553 called_do_document_action_(false),
553 render_grayscale_(false), 554 render_grayscale_(false),
554 render_annots_(true), 555 render_annots_(true),
555 progressive_paint_timeout_(0), 556 progressive_paint_timeout_(0),
556 getting_password_(false) { 557 getting_password_(false) {
557 find_factory_.Initialize(this); 558 find_factory_.Initialize(this);
558 password_factory_.Initialize(this); 559 password_factory_.Initialize(this);
559 560
560 file_access_.m_FileLen = 0; 561 file_access_.m_FileLen = 0;
561 file_access_.m_GetBlock = &GetBlock; 562 file_access_.m_GetBlock = &GetBlock;
562 file_access_.m_Param = &doc_loader_; 563 file_access_.m_Param = this;
563 564
564 file_availability_.version = 1; 565 file_availability_.version = 1;
565 file_availability_.IsDataAvail = &IsDataAvail; 566 file_availability_.IsDataAvail = &IsDataAvail;
566 file_availability_.loader = &doc_loader_; 567 file_availability_.engine = this;
567 568
568 download_hints_.version = 1; 569 download_hints_.version = 1;
569 download_hints_.AddSegment = &AddSegment; 570 download_hints_.AddSegment = &AddSegment;
570 download_hints_.loader = &doc_loader_; 571 download_hints_.engine = this;
571 572
572 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct 573 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct
573 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in 574 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in
574 // callbacks to ourself instead of maintaining a map of them to 575 // callbacks to ourself instead of maintaining a map of them to
575 // PDFiumEngine. 576 // PDFiumEngine.
576 FPDF_FORMFILLINFO::version = 1; 577 FPDF_FORMFILLINFO::version = 1;
577 FPDF_FORMFILLINFO::m_pJsPlatform = this; 578 FPDF_FORMFILLINFO::m_pJsPlatform = this;
578 FPDF_FORMFILLINFO::Release = nullptr; 579 FPDF_FORMFILLINFO::Release = nullptr;
579 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate; 580 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate;
580 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect; 581 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect;
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param, 833 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param,
833 void* language, 834 void* language,
834 int length) { 835 int length) {
835 return 0; 836 return 0;
836 } 837 }
837 838
838 #endif // defined(PDF_ENABLE_XFA) 839 #endif // defined(PDF_ENABLE_XFA)
839 840
840 int PDFiumEngine::GetBlock(void* param, unsigned long position, 841 int PDFiumEngine::GetBlock(void* param, unsigned long position,
841 unsigned char* buffer, unsigned long size) { 842 unsigned char* buffer, unsigned long size) {
842 DocumentLoader* loader = static_cast<DocumentLoader*>(param); 843 PDFiumEngine* engine = static_cast<PDFiumEngine*>(param);
843 return loader->GetBlock(position, size, buffer); 844 return engine->doc_loader_->GetBlock(position, size, buffer);
844 } 845 }
845 846
846 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, 847 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param,
847 size_t offset, size_t size) { 848 size_t offset, size_t size) {
848 PDFiumEngine::FileAvail* file_avail = 849 PDFiumEngine::FileAvail* file_avail =
849 static_cast<PDFiumEngine::FileAvail*>(param); 850 static_cast<PDFiumEngine::FileAvail*>(param);
850 return file_avail->loader->IsDataAvailable(offset, size); 851 return file_avail->engine->doc_loader_->IsDataAvailable(offset, size);
851 } 852 }
852 853
853 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, 854 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param,
854 size_t offset, size_t size) { 855 size_t offset, size_t size) {
855 PDFiumEngine::DownloadHints* download_hints = 856 PDFiumEngine::DownloadHints* download_hints =
856 static_cast<PDFiumEngine::DownloadHints*>(param); 857 static_cast<PDFiumEngine::DownloadHints*>(param);
857 return download_hints->loader->RequestData(offset, size); 858 return download_hints->engine->doc_loader_->RequestData(offset, size);
858 } 859 }
859 860
860 bool PDFiumEngine::New(const char* url, 861 bool PDFiumEngine::New(const char* url,
861 const char* headers) { 862 const char* headers) {
862 url_ = url; 863 url_ = url;
863 if (headers) 864 if (headers)
864 headers_ = headers; 865 headers_ = headers;
865 else 866 else
866 headers_.clear(); 867 headers_.clear();
867 return true; 868 return true;
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 FPDF_RenderPage_Close( 983 FPDF_RenderPage_Close(
983 pages_[progressive_paints_[i].page_index]->GetPage()); 984 pages_[progressive_paints_[i].page_index]->GetPage());
984 FPDFBitmap_Destroy(progressive_paints_[i].bitmap); 985 FPDFBitmap_Destroy(progressive_paints_[i].bitmap);
985 progressive_paints_.erase(progressive_paints_.begin() + i); 986 progressive_paints_.erase(progressive_paints_.begin() + i);
986 --i; 987 --i;
987 } 988 }
988 } 989 }
989 990
990 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { 991 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) {
991 password_tries_remaining_ = kMaxPasswordTries; 992 password_tries_remaining_ = kMaxPasswordTries;
992 return doc_loader_.Init(loader, url_, headers_); 993 process_when_pending_request_complete_ = true;
994 auto loader_wrapper =
995 base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), loader);
996 loader_wrapper->SetResponseHeaders(headers_);
997
998 doc_loader_ = base::MakeUnique<DocumentLoader>(this);
999 if (doc_loader_->Init(std::move(loader_wrapper), url_)) {
1000 // request initial data.
1001 doc_loader_->RequestData(0, 1);
1002 return true;
1003 }
1004 return false;
993 } 1005 }
994 1006
995 pp::Instance* PDFiumEngine::GetPluginInstance() { 1007 pp::Instance* PDFiumEngine::GetPluginInstance() {
996 return client_->GetPluginInstance(); 1008 return client_->GetPluginInstance();
997 } 1009 }
998 1010
999 pp::URLLoader PDFiumEngine::CreateURLLoader() { 1011 std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() {
1000 return client_->CreateURLLoader(); 1012 std::unique_ptr<URLLoaderWrapper> result;
1013 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.
1014 client_->CreateURLLoader()));
1015
1016 return result;
1001 } 1017 }
1002 1018
1003 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { 1019 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) {
1004 // Unload and delete the blank page before appending. 1020 // Unload and delete the blank page before appending.
1005 pages_[index]->Unload(); 1021 pages_[index]->Unload();
1006 pages_[index]->set_calculated_links(false); 1022 pages_[index]->set_calculated_links(false);
1007 pp::Size curr_page_size = GetPageSize(index); 1023 pp::Size curr_page_size = GetPageSize(index);
1008 FPDFPage_Delete(doc_, index); 1024 FPDFPage_Delete(doc_, index);
1009 FPDF_ImportPages(doc_, 1025 FPDF_ImportPages(doc_,
1010 static_cast<PDFiumEngine*>(engine)->doc(), 1026 static_cast<PDFiumEngine*>(engine)->doc(),
1011 "1", 1027 "1",
1012 index); 1028 index);
1013 pp::Size new_page_size = GetPageSize(index); 1029 pp::Size new_page_size = GetPageSize(index);
1014 if (curr_page_size != new_page_size) 1030 if (curr_page_size != new_page_size)
1015 LoadPageInfo(true); 1031 LoadPageInfo(true);
1016 client_->Invalidate(GetPageScreenRect(index)); 1032 client_->Invalidate(GetPageScreenRect(index));
1017 } 1033 }
1018 1034
1019 #if defined(PDF_ENABLE_XFA) 1035 #if defined(PDF_ENABLE_XFA)
1020 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { 1036 void PDFiumEngine::SetScrollPosition(const pp::Point& position) {
1021 position_ = position; 1037 position_ = position;
1022 } 1038 }
1023 #endif 1039 #endif
1024 1040
1025 bool PDFiumEngine::IsProgressiveLoad() {
1026 return doc_loader_.is_partial_document();
1027 }
1028
1029 std::string PDFiumEngine::GetMetadata(const std::string& key) { 1041 std::string PDFiumEngine::GetMetadata(const std::string& key) {
1030 return GetDocumentMetadata(doc(), key); 1042 return GetDocumentMetadata(doc(), key);
1031 } 1043 }
1032 1044
1033 void PDFiumEngine::OnPartialDocumentLoaded() { 1045 void PDFiumEngine::OnPendingRequestComplete() {
1034 file_access_.m_FileLen = doc_loader_.document_size(); 1046 if (!process_when_pending_request_complete_)
1047 return;
1035 if (!fpdf_availability_) { 1048 if (!fpdf_availability_) {
1049 file_access_.m_FileLen = doc_loader_->GetDocumentSize();
1036 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); 1050 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
1037 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 }
1038 } 1064 }
1039 1065
1040 // Currently engine does not deal efficiently with some non-linearized files. 1066 if (!doc_) {
1041 // See http://code.google.com/p/chromium/issues/detail?id=59400
1042 // To improve user experience we download entire file for non-linearized PDF.
1043 if (!FPDFAvail_IsLinearized(fpdf_availability_)) {
1044 doc_loader_.RequestData(0, doc_loader_.document_size());
1045 return;
1046 }
1047
1048 LoadDocument();
1049 }
1050
1051 void PDFiumEngine::OnPendingRequestComplete() {
1052 if (!doc_ || !form_) {
1053 DCHECK(fpdf_availability_);
1054 LoadDocument(); 1067 LoadDocument();
1055 return; 1068 return;
1056 } 1069 }
1057 1070
1058 // 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
1059 // need to run the code below in that case. 1072 // need to run the code below in that case.
1060 bool update_pages = false; 1073 bool update_pages = false;
1061 std::vector<int> still_pending; 1074 std::vector<int> still_pending;
1062 for (int pending_page : pending_pages_) { 1075 for (int pending_page : pending_pages_) {
1063 if (CheckPageAvailable(pending_page, &still_pending)) { 1076 if (CheckPageAvailable(pending_page, &still_pending)) {
1064 update_pages = true; 1077 update_pages = true;
1065 if (IsPageVisible(pending_page)) 1078 if (IsPageVisible(pending_page))
1066 client_->Invalidate(GetPageScreenRect(pending_page)); 1079 client_->Invalidate(GetPageScreenRect(pending_page));
1067 } 1080 }
1068 } 1081 }
1069 pending_pages_.swap(still_pending); 1082 pending_pages_.swap(still_pending);
1070 if (update_pages) 1083 if (update_pages)
1071 LoadPageInfo(true); 1084 LoadPageInfo(true);
1072 } 1085 }
1073 1086
1074 void PDFiumEngine::OnNewDataAvailable() { 1087 void PDFiumEngine::OnNewDataAvailable() {
1075 client_->DocumentLoadProgress(doc_loader_.GetAvailableData(), 1088 const float progress = doc_loader_->GetProgress();
1076 doc_loader_.document_size()); 1089 if (progress < 0.001) {
1090 client_->DocumentLoadProgress(0, 0);
1091 } else {
1092 client_->DocumentLoadProgress(progress * 10000, 10000);
1093 }
1077 } 1094 }
1078 1095
1079 void PDFiumEngine::OnDocumentComplete() { 1096 void PDFiumEngine::OnDocumentComplete() {
1080 if (!doc_ || !form_) { 1097 if (doc_) {
1081 file_access_.m_FileLen = doc_loader_.document_size(); 1098 return FinishLoadingDocument();
1082 if (!fpdf_availability_) {
1083 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_);
1084 DCHECK(fpdf_availability_);
1085 }
1086 LoadDocument();
1087 return;
1088 } 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 }
1089 1107
1090 FinishLoadingDocument(); 1108 void PDFiumEngine::OnDocumentCanceled() {
1109 OnDocumentComplete();
1110 }
1111
1112 void PDFiumEngine::CancelBrowserDownload() {
1113 client_->CancelBrowserDownload();
1091 } 1114 }
1092 1115
1093 void PDFiumEngine::FinishLoadingDocument() { 1116 void PDFiumEngine::FinishLoadingDocument() {
1094 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 }
1095 1133
1096 bool need_update = false; 1134 bool need_update = false;
1097 for (size_t i = 0; i < pages_.size(); ++i) { 1135 for (size_t i = 0; i < pages_.size(); ++i) {
1098 if (pages_[i]->available()) 1136 if (pages_[i]->available())
1099 continue; 1137 continue;
1100 1138
1101 pages_[i]->set_available(true); 1139 pages_[i]->set_available(true);
1102 // 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
1103 // already downloaded. 1141 // already downloaded.
1104 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_); 1142 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_);
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 return temp_doc; 1329 return temp_doc;
1292 } 1330 }
1293 1331
1294 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( 1332 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF(
1295 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1333 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1296 const PP_PrintSettings_Dev& print_settings) { 1334 const PP_PrintSettings_Dev& print_settings) {
1297 if (!page_range_count) 1335 if (!page_range_count)
1298 return pp::Buffer_Dev(); 1336 return pp::Buffer_Dev();
1299 1337
1300 // If document is not downloaded yet, disable printing. 1338 // If document is not downloaded yet, disable printing.
1301 if (doc_ && !doc_loader_.IsDocumentComplete()) 1339 if (doc_ && !doc_loader_->IsDocumentComplete())
1302 return pp::Buffer_Dev(); 1340 return pp::Buffer_Dev();
1303 1341
1304 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); 1342 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument();
1305 if (!output_doc) 1343 if (!output_doc)
1306 return pp::Buffer_Dev(); 1344 return pp::Buffer_Dev();
1307 1345
1308 SaveSelectedFormForPrint(); 1346 SaveSelectedFormForPrint();
1309 1347
1310 std::vector<PDFiumPage> pages_to_print; 1348 std::vector<PDFiumPage> pages_to_print;
1311 // width and height of source PDF pages. 1349 // width and height of source PDF pages.
(...skipping 1117 matching lines...) Expand 10 before | Expand all | Expand 10 after
2429 } 2467 }
2430 2468
2431 CalculateVisiblePages(); 2469 CalculateVisiblePages();
2432 if (document_size_ != old_document_size) 2470 if (document_size_ != old_document_size)
2433 client_->DocumentSizeUpdated(document_size_); 2471 client_->DocumentSizeUpdated(document_size_);
2434 } 2472 }
2435 2473
2436 void PDFiumEngine::LoadDocument() { 2474 void PDFiumEngine::LoadDocument() {
2437 // Check if the document is ready for loading. If it isn't just bail for now, 2475 // Check if the document is ready for loading. If it isn't just bail for now,
2438 // we will call LoadDocument() again later. 2476 // we will call LoadDocument() again later.
2439 if (!doc_ && !doc_loader_.IsDocumentComplete() && 2477 if (!doc_ && !doc_loader_->IsDocumentComplete() &&
2440 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { 2478 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) {
2441 return; 2479 return;
2442 } 2480 }
2443 2481
2444 // If we're in the middle of getting a password, just return. We will retry 2482 // If we're in the middle of getting a password, just return. We will retry
2445 // loading the document after we get the password anyway. 2483 // loading the document after we get the password anyway.
2446 if (getting_password_) 2484 if (getting_password_)
2447 return; 2485 return;
2448 2486
2449 ScopedUnsupportedFeature scoped_unsupported_feature(this); 2487 ScopedUnsupportedFeature scoped_unsupported_feature(this);
(...skipping 17 matching lines...) Expand all
2467 // call may have failed, so call it again for good measure. 2505 // call may have failed, so call it again for good measure.
2468 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); 2506 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_);
2469 return true; 2507 return true;
2470 } 2508 }
2471 2509
2472 const char* password_cstr = nullptr; 2510 const char* password_cstr = nullptr;
2473 if (!password.empty()) { 2511 if (!password.empty()) {
2474 password_cstr = password.c_str(); 2512 password_cstr = password.c_str();
2475 password_tries_remaining_--; 2513 password_tries_remaining_--;
2476 } 2514 }
2477 if (doc_loader_.IsDocumentComplete() && 2515 if (doc_loader_->IsDocumentComplete() &&
2478 !FPDFAvail_IsLinearized(fpdf_availability_)) { 2516 !FPDFAvail_IsLinearized(fpdf_availability_)) {
2479 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); 2517 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr);
2480 } else { 2518 } else {
2481 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); 2519 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr);
2482 } 2520 }
2483 if (!doc_) { 2521 if (!doc_) {
2484 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD) 2522 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD)
2485 *needs_password = true; 2523 *needs_password = true;
2486 return false; 2524 return false;
2487 } 2525 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2524 client_->DocumentLoadFailed(); 2562 client_->DocumentLoadFailed();
2525 return; 2563 return;
2526 } 2564 }
2527 2565
2528 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES) 2566 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES)
2529 client_->DocumentHasUnsupportedFeature("Bookmarks"); 2567 client_->DocumentHasUnsupportedFeature("Bookmarks");
2530 2568
2531 permissions_ = FPDF_GetDocPermissions(doc_); 2569 permissions_ = FPDF_GetDocPermissions(doc_);
2532 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_); 2570 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_);
2533 2571
2534 if (!form_) { 2572 if (!doc_loader_->IsDocumentComplete()) {
2535 int form_status =
2536 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_);
2537 bool doc_complete = doc_loader_.IsDocumentComplete();
2538 // Try again if the data is not available and the document hasn't finished
2539 // downloading.
2540 if (form_status == PDF_FORM_NOTAVAIL && !doc_complete)
2541 return;
2542
2543 form_ = FPDFDOC_InitFormFillEnvironment(
2544 doc_, static_cast<FPDF_FORMFILLINFO*>(this));
2545 #if defined(PDF_ENABLE_XFA)
2546 FPDF_LoadXFA(doc_);
2547 #endif
2548
2549 FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor);
2550 FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha);
2551 }
2552
2553 if (!doc_loader_.IsDocumentComplete()) {
2554 // Check if the first page is available. In a linearized PDF, that is not 2573 // Check if the first page is available. In a linearized PDF, that is not
2555 // always page 0. Doing this gives us the default page size, since when the 2574 // always page 0. Doing this gives us the default page size, since when the
2556 // document is available, the first page is available as well. 2575 // document is available, the first page is available as well.
2557 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_); 2576 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_);
2558 } 2577 }
2559 2578
2560 LoadPageInfo(false); 2579 LoadPageInfo(false);
2561 2580
2562 if (doc_loader_.IsDocumentComplete()) 2581 if (doc_loader_->IsDocumentComplete())
2563 FinishLoadingDocument(); 2582 FinishLoadingDocument();
2564 } 2583 }
2565 2584
2566 void PDFiumEngine::LoadPageInfo(bool reload) { 2585 void PDFiumEngine::LoadPageInfo(bool reload) {
2567 pending_pages_.clear(); 2586 pending_pages_.clear();
2568 pp::Size old_document_size = document_size_; 2587 pp::Size old_document_size = document_size_;
2569 document_size_ = pp::Size(); 2588 document_size_ = pp::Size();
2570 std::vector<pp::Rect> page_rects; 2589 std::vector<pp::Rect> page_rects;
2571 int page_count = FPDF_GetPageCount(doc_); 2590 int page_count = FPDF_GetPageCount(doc_);
2572 bool doc_complete = doc_loader_.IsDocumentComplete(); 2591 bool doc_complete = doc_loader_->IsDocumentComplete();
2573 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED; 2592 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED;
2574 for (int i = 0; i < page_count; ++i) { 2593 for (int i = 0; i < page_count; ++i) {
2575 if (i != 0) { 2594 if (i != 0) {
2576 // Add space for horizontal separator. 2595 // Add space for horizontal separator.
2577 document_size_.Enlarge(0, kPageSeparatorThickness); 2596 document_size_.Enlarge(0, kPageSeparatorThickness);
2578 } 2597 }
2579 2598
2580 // Get page availability. If |reload| == true, then the document has been 2599 // Get page availability. If |reload| == true, then the document has been
2581 // constructed already. Get page availability flag from already existing 2600 // constructed already. Get page availability flag from already existing
2582 // PDFiumPage class. 2601 // PDFiumPage class.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2619 pages_.push_back(new PDFiumPage(this, i, page_rect, false)); 2638 pages_.push_back(new PDFiumPage(this, i, page_rect, false));
2620 } 2639 }
2621 } 2640 }
2622 2641
2623 CalculateVisiblePages(); 2642 CalculateVisiblePages();
2624 if (document_size_ != old_document_size) 2643 if (document_size_ != old_document_size)
2625 client_->DocumentSizeUpdated(document_size_); 2644 client_->DocumentSizeUpdated(document_size_);
2626 } 2645 }
2627 2646
2628 void PDFiumEngine::CalculateVisiblePages() { 2647 void PDFiumEngine::CalculateVisiblePages() {
2648 if (!doc_loader_)
2649 return;
2629 // Clear pending requests queue, since it may contain requests to the pages 2650 // Clear pending requests queue, since it may contain requests to the pages
2630 // that are already invisible (after scrolling for example). 2651 // that are already invisible (after scrolling for example).
2631 pending_pages_.clear(); 2652 pending_pages_.clear();
2632 doc_loader_.ClearPendingRequests(); 2653 doc_loader_->ClearPendingRequests();
2633 2654
2634 visible_pages_.clear(); 2655 visible_pages_.clear();
2635 pp::Rect visible_rect(plugin_size_); 2656 pp::Rect visible_rect(plugin_size_);
2636 for (size_t i = 0; i < pages_.size(); ++i) { 2657 for (size_t i = 0; i < pages_.size(); ++i) {
2637 // Check an entire PageScreenRect, since we might need to repaint side 2658 // Check an entire PageScreenRect, since we might need to repaint side
2638 // borders and shadows even if the page itself is not visible. 2659 // borders and shadows even if the page itself is not visible.
2639 // For example, when user use pdf with different page sizes and zoomed in 2660 // For example, when user use pdf with different page sizes and zoomed in
2640 // outside page area. 2661 // outside page area.
2641 if (visible_rect.Intersects(GetPageScreenRect(i))) { 2662 if (visible_rect.Intersects(GetPageScreenRect(i))) {
2642 visible_pages_.push_back(i); 2663 visible_pages_.push_back(i);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2680 bool PDFiumEngine::IsPageVisible(int index) const { 2701 bool PDFiumEngine::IsPageVisible(int index) const {
2681 return base::ContainsValue(visible_pages_, index); 2702 return base::ContainsValue(visible_pages_, index);
2682 } 2703 }
2683 2704
2684 void PDFiumEngine::ScrollToPage(int page) { 2705 void PDFiumEngine::ScrollToPage(int page) {
2685 in_flight_visible_page_ = page; 2706 in_flight_visible_page_ = page;
2686 client_->ScrollToPage(page); 2707 client_->ScrollToPage(page);
2687 } 2708 }
2688 2709
2689 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { 2710 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) {
2690 if (!doc_ || !form_) 2711 if (!doc_)
2691 return false; 2712 return false;
2692 2713
2693 const int num_pages = static_cast<int>(pages_.size()); 2714 const int num_pages = static_cast<int>(pages_.size());
2694 if (index < num_pages && pages_[index]->available()) 2715 if (index < num_pages && pages_[index]->available())
2695 return true; 2716 return true;
2696 2717
2697 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) { 2718 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) {
2698 if (!base::ContainsValue(*pending, index)) 2719 if (!base::ContainsValue(*pending, index))
2699 pending->push_back(index); 2720 pending->push_back(index);
2700 return false; 2721 return false;
(...skipping 1274 matching lines...) Expand 10 before | Expand all | Expand 10 after
3975 FPDF_DOCUMENT doc = 3996 FPDF_DOCUMENT doc =
3976 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 3997 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
3977 if (!doc) 3998 if (!doc)
3978 return false; 3999 return false;
3979 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4000 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
3980 FPDF_CloseDocument(doc); 4001 FPDF_CloseDocument(doc);
3981 return success; 4002 return success;
3982 } 4003 }
3983 4004
3984 } // namespace chrome_pdf 4005 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698