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

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

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