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

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

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