OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "pdf/pdfium/pdfium_engine.h" | 5 #include "pdf/pdfium/pdfium_engine.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "base/strings/string_piece.h" | 25 #include "base/strings/string_piece.h" |
26 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
27 #include "base/strings/utf_string_conversions.h" | 27 #include "base/strings/utf_string_conversions.h" |
28 #include "gin/array_buffer.h" | 28 #include "gin/array_buffer.h" |
29 #include "gin/public/gin_embedders.h" | 29 #include "gin/public/gin_embedders.h" |
30 #include "gin/public/isolate_holder.h" | 30 #include "gin/public/isolate_holder.h" |
31 #include "pdf/draw_utils.h" | 31 #include "pdf/draw_utils.h" |
32 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" | 32 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" |
33 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h" | 33 #include "pdf/pdfium/pdfium_mem_buffer_file_read.h" |
34 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h" | 34 #include "pdf/pdfium/pdfium_mem_buffer_file_write.h" |
35 #include "pdf/url_loader_wrapper_impl.h" | |
36 #include "ppapi/c/pp_errors.h" | 35 #include "ppapi/c/pp_errors.h" |
37 #include "ppapi/c/pp_input_event.h" | 36 #include "ppapi/c/pp_input_event.h" |
38 #include "ppapi/c/ppb_core.h" | 37 #include "ppapi/c/ppb_core.h" |
39 #include "ppapi/c/private/ppb_pdf.h" | 38 #include "ppapi/c/private/ppb_pdf.h" |
40 #include "ppapi/cpp/dev/memory_dev.h" | 39 #include "ppapi/cpp/dev/memory_dev.h" |
41 #include "ppapi/cpp/input_event.h" | 40 #include "ppapi/cpp/input_event.h" |
42 #include "ppapi/cpp/instance.h" | 41 #include "ppapi/cpp/instance.h" |
43 #include "ppapi/cpp/module.h" | 42 #include "ppapi/cpp/module.h" |
44 #include "ppapi/cpp/private/pdf.h" | 43 #include "ppapi/cpp/private/pdf.h" |
45 #include "ppapi/cpp/trusted/browser_font_trusted.h" | 44 #include "ppapi/cpp/trusted/browser_font_trusted.h" |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 } | 661 } |
663 | 662 |
664 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { | 663 PDFEngine* PDFEngine::Create(PDFEngine::Client* client) { |
665 return new PDFiumEngine(client); | 664 return new PDFiumEngine(client); |
666 } | 665 } |
667 | 666 |
668 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) | 667 PDFiumEngine::PDFiumEngine(PDFEngine::Client* client) |
669 : client_(client), | 668 : client_(client), |
670 current_zoom_(1.0), | 669 current_zoom_(1.0), |
671 current_rotation_(0), | 670 current_rotation_(0), |
| 671 doc_loader_(this), |
672 password_tries_remaining_(0), | 672 password_tries_remaining_(0), |
673 doc_(nullptr), | 673 doc_(nullptr), |
674 form_(nullptr), | 674 form_(nullptr), |
675 defer_page_unload_(false), | 675 defer_page_unload_(false), |
676 selecting_(false), | 676 selecting_(false), |
677 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA, | 677 mouse_down_state_(PDFiumPage::NONSELECTABLE_AREA, |
678 PDFiumPage::LinkTarget()), | 678 PDFiumPage::LinkTarget()), |
679 next_page_to_search_(-1), | 679 next_page_to_search_(-1), |
680 last_page_to_search_(-1), | 680 last_page_to_search_(-1), |
681 last_character_index_to_search_(-1), | 681 last_character_index_to_search_(-1), |
682 permissions_(0), | 682 permissions_(0), |
683 permissions_handler_revision_(-1), | 683 permissions_handler_revision_(-1), |
684 fpdf_availability_(nullptr), | 684 fpdf_availability_(nullptr), |
685 next_timer_id_(0), | 685 next_timer_id_(0), |
686 last_page_mouse_down_(-1), | 686 last_page_mouse_down_(-1), |
687 most_visible_page_(-1), | 687 most_visible_page_(-1), |
688 called_do_document_action_(false), | 688 called_do_document_action_(false), |
689 render_grayscale_(false), | 689 render_grayscale_(false), |
690 render_annots_(true), | 690 render_annots_(true), |
691 progressive_paint_timeout_(0), | 691 progressive_paint_timeout_(0), |
692 getting_password_(false) { | 692 getting_password_(false) { |
693 find_factory_.Initialize(this); | 693 find_factory_.Initialize(this); |
694 password_factory_.Initialize(this); | 694 password_factory_.Initialize(this); |
695 | 695 |
696 file_access_.m_FileLen = 0; | 696 file_access_.m_FileLen = 0; |
697 file_access_.m_GetBlock = &GetBlock; | 697 file_access_.m_GetBlock = &GetBlock; |
698 file_access_.m_Param = this; | 698 file_access_.m_Param = &doc_loader_; |
699 | 699 |
700 file_availability_.version = 1; | 700 file_availability_.version = 1; |
701 file_availability_.IsDataAvail = &IsDataAvail; | 701 file_availability_.IsDataAvail = &IsDataAvail; |
702 file_availability_.engine = this; | 702 file_availability_.loader = &doc_loader_; |
703 | 703 |
704 download_hints_.version = 1; | 704 download_hints_.version = 1; |
705 download_hints_.AddSegment = &AddSegment; | 705 download_hints_.AddSegment = &AddSegment; |
706 download_hints_.engine = this; | 706 download_hints_.loader = &doc_loader_; |
707 | 707 |
708 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct | 708 // Initialize FPDF_FORMFILLINFO member variables. Deriving from this struct |
709 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in | 709 // allows the static callbacks to be able to cast the FPDF_FORMFILLINFO in |
710 // callbacks to ourself instead of maintaining a map of them to | 710 // callbacks to ourself instead of maintaining a map of them to |
711 // PDFiumEngine. | 711 // PDFiumEngine. |
712 FPDF_FORMFILLINFO::version = 1; | 712 FPDF_FORMFILLINFO::version = 1; |
713 FPDF_FORMFILLINFO::m_pJsPlatform = this; | 713 FPDF_FORMFILLINFO::m_pJsPlatform = this; |
714 FPDF_FORMFILLINFO::Release = nullptr; | 714 FPDF_FORMFILLINFO::Release = nullptr; |
715 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate; | 715 FPDF_FORMFILLINFO::FFI_Invalidate = Form_Invalidate; |
716 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect; | 716 FPDF_FORMFILLINFO::FFI_OutputSelectedRect = Form_OutputSelectedRect; |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
966 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param, | 966 int PDFiumEngine::Form_GetLanguage(FPDF_FORMFILLINFO* param, |
967 void* language, | 967 void* language, |
968 int length) { | 968 int length) { |
969 return 0; | 969 return 0; |
970 } | 970 } |
971 | 971 |
972 #endif // defined(PDF_ENABLE_XFA) | 972 #endif // defined(PDF_ENABLE_XFA) |
973 | 973 |
974 int PDFiumEngine::GetBlock(void* param, unsigned long position, | 974 int PDFiumEngine::GetBlock(void* param, unsigned long position, |
975 unsigned char* buffer, unsigned long size) { | 975 unsigned char* buffer, unsigned long size) { |
976 PDFiumEngine* engine = static_cast<PDFiumEngine*>(param); | 976 DocumentLoader* loader = static_cast<DocumentLoader*>(param); |
977 return engine->doc_loader_->GetBlock(position, size, buffer); | 977 return loader->GetBlock(position, size, buffer); |
978 } | 978 } |
979 | 979 |
980 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, | 980 FPDF_BOOL PDFiumEngine::IsDataAvail(FX_FILEAVAIL* param, |
981 size_t offset, size_t size) { | 981 size_t offset, size_t size) { |
982 PDFiumEngine::FileAvail* file_avail = | 982 PDFiumEngine::FileAvail* file_avail = |
983 static_cast<PDFiumEngine::FileAvail*>(param); | 983 static_cast<PDFiumEngine::FileAvail*>(param); |
984 return file_avail->engine->doc_loader_->IsDataAvailable(offset, size); | 984 return file_avail->loader->IsDataAvailable(offset, size); |
985 } | 985 } |
986 | 986 |
987 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, | 987 void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, |
988 size_t offset, size_t size) { | 988 size_t offset, size_t size) { |
989 PDFiumEngine::DownloadHints* download_hints = | 989 PDFiumEngine::DownloadHints* download_hints = |
990 static_cast<PDFiumEngine::DownloadHints*>(param); | 990 static_cast<PDFiumEngine::DownloadHints*>(param); |
991 return download_hints->engine->doc_loader_->RequestData(offset, size); | 991 return download_hints->loader->RequestData(offset, size); |
992 } | 992 } |
993 | 993 |
994 bool PDFiumEngine::New(const char* url, | 994 bool PDFiumEngine::New(const char* url, |
995 const char* headers) { | 995 const char* headers) { |
996 url_ = url; | 996 url_ = url; |
997 if (headers) | 997 if (headers) |
998 headers_ = headers; | 998 headers_ = headers; |
999 else | 999 else |
1000 headers_.clear(); | 1000 headers_.clear(); |
1001 return true; | 1001 return true; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 FPDF_RenderPage_Close( | 1116 FPDF_RenderPage_Close( |
1117 pages_[progressive_paints_[i].page_index]->GetPage()); | 1117 pages_[progressive_paints_[i].page_index]->GetPage()); |
1118 FPDFBitmap_Destroy(progressive_paints_[i].bitmap); | 1118 FPDFBitmap_Destroy(progressive_paints_[i].bitmap); |
1119 progressive_paints_.erase(progressive_paints_.begin() + i); | 1119 progressive_paints_.erase(progressive_paints_.begin() + i); |
1120 --i; | 1120 --i; |
1121 } | 1121 } |
1122 } | 1122 } |
1123 | 1123 |
1124 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { | 1124 bool PDFiumEngine::HandleDocumentLoad(const pp::URLLoader& loader) { |
1125 password_tries_remaining_ = kMaxPasswordTries; | 1125 password_tries_remaining_ = kMaxPasswordTries; |
1126 process_when_pending_request_complete_ = true; | 1126 return doc_loader_.Init(loader, url_, headers_); |
1127 auto loader_wrapper = | |
1128 base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), loader); | |
1129 loader_wrapper->SetResponseHeaders(headers_); | |
1130 | |
1131 doc_loader_ = base::MakeUnique<DocumentLoader>(this); | |
1132 if (doc_loader_->Init(std::move(loader_wrapper), url_)) { | |
1133 // request initial data. | |
1134 doc_loader_->RequestData(0, 1); | |
1135 return true; | |
1136 } | |
1137 return false; | |
1138 } | 1127 } |
1139 | 1128 |
1140 pp::Instance* PDFiumEngine::GetPluginInstance() { | 1129 pp::Instance* PDFiumEngine::GetPluginInstance() { |
1141 return client_->GetPluginInstance(); | 1130 return client_->GetPluginInstance(); |
1142 } | 1131 } |
1143 | 1132 |
1144 std::unique_ptr<URLLoaderWrapper> PDFiumEngine::CreateURLLoader() { | 1133 pp::URLLoader PDFiumEngine::CreateURLLoader() { |
1145 return base::MakeUnique<URLLoaderWrapperImpl>(GetPluginInstance(), | 1134 return client_->CreateURLLoader(); |
1146 client_->CreateURLLoader()); | |
1147 } | 1135 } |
1148 | 1136 |
1149 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { | 1137 void PDFiumEngine::AppendPage(PDFEngine* engine, int index) { |
1150 // Unload and delete the blank page before appending. | 1138 // Unload and delete the blank page before appending. |
1151 pages_[index]->Unload(); | 1139 pages_[index]->Unload(); |
1152 pages_[index]->set_calculated_links(false); | 1140 pages_[index]->set_calculated_links(false); |
1153 pp::Size curr_page_size = GetPageSize(index); | 1141 pp::Size curr_page_size = GetPageSize(index); |
1154 FPDFPage_Delete(doc_, index); | 1142 FPDFPage_Delete(doc_, index); |
1155 FPDF_ImportPages(doc_, | 1143 FPDF_ImportPages(doc_, |
1156 static_cast<PDFiumEngine*>(engine)->doc(), | 1144 static_cast<PDFiumEngine*>(engine)->doc(), |
1157 "1", | 1145 "1", |
1158 index); | 1146 index); |
1159 pp::Size new_page_size = GetPageSize(index); | 1147 pp::Size new_page_size = GetPageSize(index); |
1160 if (curr_page_size != new_page_size) | 1148 if (curr_page_size != new_page_size) |
1161 LoadPageInfo(true); | 1149 LoadPageInfo(true); |
1162 client_->Invalidate(GetPageScreenRect(index)); | 1150 client_->Invalidate(GetPageScreenRect(index)); |
1163 } | 1151 } |
1164 | 1152 |
1165 #if defined(PDF_ENABLE_XFA) | 1153 #if defined(PDF_ENABLE_XFA) |
1166 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { | 1154 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { |
1167 position_ = position; | 1155 position_ = position; |
1168 } | 1156 } |
1169 #endif | 1157 #endif |
1170 | 1158 |
| 1159 bool PDFiumEngine::IsProgressiveLoad() { |
| 1160 return doc_loader_.is_partial_document(); |
| 1161 } |
| 1162 |
1171 std::string PDFiumEngine::GetMetadata(const std::string& key) { | 1163 std::string PDFiumEngine::GetMetadata(const std::string& key) { |
1172 return GetDocumentMetadata(doc(), key); | 1164 return GetDocumentMetadata(doc(), key); |
1173 } | 1165 } |
1174 | 1166 |
1175 void PDFiumEngine::OnPendingRequestComplete() { | 1167 void PDFiumEngine::OnPartialDocumentLoaded() { |
1176 if (!process_when_pending_request_complete_) | 1168 file_access_.m_FileLen = doc_loader_.document_size(); |
1177 return; | |
1178 if (!fpdf_availability_) { | 1169 if (!fpdf_availability_) { |
1179 file_access_.m_FileLen = doc_loader_->GetDocumentSize(); | |
1180 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); | 1170 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
1181 DCHECK(fpdf_availability_); | 1171 DCHECK(fpdf_availability_); |
1182 // Currently engine does not deal efficiently with some non-linearized | |
1183 // files. | |
1184 // See http://code.google.com/p/chromium/issues/detail?id=59400 | |
1185 // To improve user experience we download entire file for non-linearized | |
1186 // PDF. | |
1187 if (FPDFAvail_IsLinearized(fpdf_availability_) != PDF_LINEARIZED) { | |
1188 // Wait complete document. | |
1189 process_when_pending_request_complete_ = false; | |
1190 FPDFAvail_Destroy(fpdf_availability_); | |
1191 fpdf_availability_ = nullptr; | |
1192 return; | |
1193 } | |
1194 } | 1172 } |
1195 | 1173 |
1196 if (!doc_) { | 1174 // Currently engine does not deal efficiently with some non-linearized files. |
| 1175 // See http://code.google.com/p/chromium/issues/detail?id=59400 |
| 1176 // To improve user experience we download entire file for non-linearized PDF. |
| 1177 if (!FPDFAvail_IsLinearized(fpdf_availability_)) { |
| 1178 doc_loader_.RequestData(0, doc_loader_.document_size()); |
| 1179 return; |
| 1180 } |
| 1181 |
| 1182 LoadDocument(); |
| 1183 } |
| 1184 |
| 1185 void PDFiumEngine::OnPendingRequestComplete() { |
| 1186 if (!doc_ || !form_) { |
| 1187 DCHECK(fpdf_availability_); |
1197 LoadDocument(); | 1188 LoadDocument(); |
1198 return; | 1189 return; |
1199 } | 1190 } |
1200 | 1191 |
1201 // LoadDocument() will result in |pending_pages_| being reset so there's no | 1192 // LoadDocument() will result in |pending_pages_| being reset so there's no |
1202 // need to run the code below in that case. | 1193 // need to run the code below in that case. |
1203 bool update_pages = false; | 1194 bool update_pages = false; |
1204 std::vector<int> still_pending; | 1195 std::vector<int> still_pending; |
1205 for (int pending_page : pending_pages_) { | 1196 for (int pending_page : pending_pages_) { |
1206 if (CheckPageAvailable(pending_page, &still_pending)) { | 1197 if (CheckPageAvailable(pending_page, &still_pending)) { |
1207 update_pages = true; | 1198 update_pages = true; |
1208 if (IsPageVisible(pending_page)) | 1199 if (IsPageVisible(pending_page)) |
1209 client_->Invalidate(GetPageScreenRect(pending_page)); | 1200 client_->Invalidate(GetPageScreenRect(pending_page)); |
1210 } | 1201 } |
1211 } | 1202 } |
1212 pending_pages_.swap(still_pending); | 1203 pending_pages_.swap(still_pending); |
1213 if (update_pages) | 1204 if (update_pages) |
1214 LoadPageInfo(true); | 1205 LoadPageInfo(true); |
1215 } | 1206 } |
1216 | 1207 |
1217 void PDFiumEngine::OnNewDataAvailable() { | 1208 void PDFiumEngine::OnNewDataAvailable() { |
1218 if (!doc_loader_->GetDocumentSize()) { | 1209 client_->DocumentLoadProgress(doc_loader_.GetAvailableData(), |
1219 client_->DocumentLoadProgress(doc_loader_->count_of_bytes_received(), 0); | 1210 doc_loader_.document_size()); |
| 1211 } |
| 1212 |
| 1213 void PDFiumEngine::OnDocumentFailed() { |
| 1214 client_->DocumentLoadFailed(); |
| 1215 } |
| 1216 |
| 1217 void PDFiumEngine::OnDocumentComplete() { |
| 1218 if (!doc_ || !form_) { |
| 1219 file_access_.m_FileLen = doc_loader_.document_size(); |
| 1220 if (!fpdf_availability_) { |
| 1221 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
| 1222 DCHECK(fpdf_availability_); |
| 1223 } |
| 1224 LoadDocument(); |
1220 return; | 1225 return; |
1221 } | 1226 } |
1222 | 1227 |
1223 const float progress = doc_loader_->GetProgress(); | 1228 FinishLoadingDocument(); |
1224 DCHECK_GE(progress, 0.0); | |
1225 DCHECK_LE(progress, 1.0); | |
1226 client_->DocumentLoadProgress(progress * 10000, 10000); | |
1227 } | |
1228 | |
1229 void PDFiumEngine::OnDocumentComplete() { | |
1230 if (doc_) { | |
1231 return FinishLoadingDocument(); | |
1232 } | |
1233 file_access_.m_FileLen = doc_loader_->GetDocumentSize(); | |
1234 if (!fpdf_availability_) { | |
1235 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); | |
1236 DCHECK(fpdf_availability_); | |
1237 } | |
1238 LoadDocument(); | |
1239 } | |
1240 | |
1241 void PDFiumEngine::OnDocumentCanceled() { | |
1242 if (visible_pages_.empty()) | |
1243 client_->DocumentLoadFailed(); | |
1244 else | |
1245 OnDocumentComplete(); | |
1246 } | |
1247 | |
1248 void PDFiumEngine::CancelBrowserDownload() { | |
1249 client_->CancelBrowserDownload(); | |
1250 } | 1229 } |
1251 | 1230 |
1252 void PDFiumEngine::FinishLoadingDocument() { | 1231 void PDFiumEngine::FinishLoadingDocument() { |
1253 DCHECK(doc_loader_->IsDocumentComplete() && doc_); | 1232 DCHECK(doc_loader_.IsDocumentComplete() && doc_); |
1254 | |
1255 if (!form_) { | |
1256 int form_status = | |
1257 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); | |
1258 if (form_status != PDF_FORM_NOTAVAIL) { | |
1259 form_ = FPDFDOC_InitFormFillEnvironment( | |
1260 doc_, static_cast<FPDF_FORMFILLINFO*>(this)); | |
1261 #if defined(PDF_ENABLE_XFA) | |
1262 FPDF_LoadXFA(doc_); | |
1263 #endif | |
1264 | |
1265 FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor); | |
1266 FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha); | |
1267 } | |
1268 } | |
1269 | 1233 |
1270 bool need_update = false; | 1234 bool need_update = false; |
1271 for (size_t i = 0; i < pages_.size(); ++i) { | 1235 for (size_t i = 0; i < pages_.size(); ++i) { |
1272 if (pages_[i]->available()) | 1236 if (pages_[i]->available()) |
1273 continue; | 1237 continue; |
1274 | 1238 |
1275 pages_[i]->set_available(true); | 1239 pages_[i]->set_available(true); |
1276 // We still need to call IsPageAvail() even if the whole document is | 1240 // We still need to call IsPageAvail() even if the whole document is |
1277 // already downloaded. | 1241 // already downloaded. |
1278 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_); | 1242 FPDFAvail_IsPageAvail(fpdf_availability_, i, &download_hints_); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1470 return temp_doc; | 1434 return temp_doc; |
1471 } | 1435 } |
1472 | 1436 |
1473 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( | 1437 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( |
1474 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, | 1438 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, |
1475 const PP_PrintSettings_Dev& print_settings) { | 1439 const PP_PrintSettings_Dev& print_settings) { |
1476 if (!page_range_count) | 1440 if (!page_range_count) |
1477 return pp::Buffer_Dev(); | 1441 return pp::Buffer_Dev(); |
1478 | 1442 |
1479 // If document is not downloaded yet, disable printing. | 1443 // If document is not downloaded yet, disable printing. |
1480 if (doc_ && !doc_loader_->IsDocumentComplete()) | 1444 if (doc_ && !doc_loader_.IsDocumentComplete()) |
1481 return pp::Buffer_Dev(); | 1445 return pp::Buffer_Dev(); |
1482 | 1446 |
1483 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); | 1447 FPDF_DOCUMENT output_doc = FPDF_CreateNewDocument(); |
1484 if (!output_doc) | 1448 if (!output_doc) |
1485 return pp::Buffer_Dev(); | 1449 return pp::Buffer_Dev(); |
1486 | 1450 |
1487 SaveSelectedFormForPrint(); | 1451 SaveSelectedFormForPrint(); |
1488 | 1452 |
1489 std::vector<PDFiumPage> pages_to_print; | 1453 std::vector<PDFiumPage> pages_to_print; |
1490 // width and height of source PDF pages. | 1454 // width and height of source PDF pages. |
(...skipping 1122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2613 } | 2577 } |
2614 | 2578 |
2615 CalculateVisiblePages(); | 2579 CalculateVisiblePages(); |
2616 if (document_size_ != old_document_size) | 2580 if (document_size_ != old_document_size) |
2617 client_->DocumentSizeUpdated(document_size_); | 2581 client_->DocumentSizeUpdated(document_size_); |
2618 } | 2582 } |
2619 | 2583 |
2620 void PDFiumEngine::LoadDocument() { | 2584 void PDFiumEngine::LoadDocument() { |
2621 // Check if the document is ready for loading. If it isn't just bail for now, | 2585 // Check if the document is ready for loading. If it isn't just bail for now, |
2622 // we will call LoadDocument() again later. | 2586 // we will call LoadDocument() again later. |
2623 if (!doc_ && !doc_loader_->IsDocumentComplete() && | 2587 if (!doc_ && !doc_loader_.IsDocumentComplete() && |
2624 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { | 2588 !FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_)) { |
2625 return; | 2589 return; |
2626 } | 2590 } |
2627 | 2591 |
2628 // If we're in the middle of getting a password, just return. We will retry | 2592 // If we're in the middle of getting a password, just return. We will retry |
2629 // loading the document after we get the password anyway. | 2593 // loading the document after we get the password anyway. |
2630 if (getting_password_) | 2594 if (getting_password_) |
2631 return; | 2595 return; |
2632 | 2596 |
2633 ScopedUnsupportedFeature scoped_unsupported_feature(this); | 2597 ScopedUnsupportedFeature scoped_unsupported_feature(this); |
(...skipping 18 matching lines...) Expand all Loading... |
2652 // call may have failed, so call it again for good measure. | 2616 // call may have failed, so call it again for good measure. |
2653 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); | 2617 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); |
2654 return true; | 2618 return true; |
2655 } | 2619 } |
2656 | 2620 |
2657 const char* password_cstr = nullptr; | 2621 const char* password_cstr = nullptr; |
2658 if (!password.empty()) { | 2622 if (!password.empty()) { |
2659 password_cstr = password.c_str(); | 2623 password_cstr = password.c_str(); |
2660 password_tries_remaining_--; | 2624 password_tries_remaining_--; |
2661 } | 2625 } |
2662 if (doc_loader_->IsDocumentComplete() && | 2626 if (doc_loader_.IsDocumentComplete() && |
2663 !FPDFAvail_IsLinearized(fpdf_availability_)) { | 2627 !FPDFAvail_IsLinearized(fpdf_availability_)) { |
2664 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); | 2628 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); |
2665 } else { | 2629 } else { |
2666 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); | 2630 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); |
2667 } | 2631 } |
| 2632 |
2668 if (!doc_) { | 2633 if (!doc_) { |
2669 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD) | 2634 if (FPDF_GetLastError() == FPDF_ERR_PASSWORD) |
2670 *needs_password = true; | 2635 *needs_password = true; |
2671 return false; | 2636 return false; |
2672 } | 2637 } |
2673 | 2638 |
2674 // Always call FPDFAvail_IsDocAvail() so PDFium initializes internal data | 2639 // Always call FPDFAvail_IsDocAvail() so PDFium initializes internal data |
2675 // structures. | 2640 // structures. |
2676 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); | 2641 FPDFAvail_IsDocAvail(fpdf_availability_, &download_hints_); |
2677 return true; | 2642 return true; |
(...skipping 20 matching lines...) Expand all Loading... |
2698 ScopedUnsupportedFeature scoped_unsupported_feature(this); | 2663 ScopedUnsupportedFeature scoped_unsupported_feature(this); |
2699 ScopedSubstFont scoped_subst_font(this); | 2664 ScopedSubstFont scoped_subst_font(this); |
2700 | 2665 |
2701 bool needs_password = false; | 2666 bool needs_password = false; |
2702 bool loaded = TryLoadingDoc(password, &needs_password); | 2667 bool loaded = TryLoadingDoc(password, &needs_password); |
2703 bool password_incorrect = !loaded && needs_password && !password.empty(); | 2668 bool password_incorrect = !loaded && needs_password && !password.empty(); |
2704 if (password_incorrect && password_tries_remaining_ > 0) { | 2669 if (password_incorrect && password_tries_remaining_ > 0) { |
2705 GetPasswordAndLoad(); | 2670 GetPasswordAndLoad(); |
2706 return; | 2671 return; |
2707 } | 2672 } |
2708 | |
2709 if (!doc_) { | 2673 if (!doc_) { |
2710 client_->DocumentLoadFailed(); | 2674 client_->DocumentLoadFailed(); |
2711 return; | 2675 return; |
2712 } | 2676 } |
2713 | 2677 |
2714 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES) | 2678 if (FPDFDoc_GetPageMode(doc_) == PAGEMODE_USEOUTLINES) |
2715 client_->DocumentHasUnsupportedFeature("Bookmarks"); | 2679 client_->DocumentHasUnsupportedFeature("Bookmarks"); |
2716 | 2680 |
2717 permissions_ = FPDF_GetDocPermissions(doc_); | 2681 permissions_ = FPDF_GetDocPermissions(doc_); |
2718 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_); | 2682 permissions_handler_revision_ = FPDF_GetSecurityHandlerRevision(doc_); |
2719 | 2683 |
2720 if (!doc_loader_->IsDocumentComplete()) { | 2684 if (!form_) { |
| 2685 int form_status = |
| 2686 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); |
| 2687 bool doc_complete = doc_loader_.IsDocumentComplete(); |
| 2688 // Try again if the data is not available and the document hasn't finished |
| 2689 // downloading. |
| 2690 if (form_status == PDF_FORM_NOTAVAIL && !doc_complete) |
| 2691 return; |
| 2692 |
| 2693 form_ = FPDFDOC_InitFormFillEnvironment( |
| 2694 doc_, static_cast<FPDF_FORMFILLINFO*>(this)); |
| 2695 #if defined(PDF_ENABLE_XFA) |
| 2696 FPDF_LoadXFA(doc_); |
| 2697 #endif |
| 2698 |
| 2699 FPDF_SetFormFieldHighlightColor(form_, 0, kFormHighlightColor); |
| 2700 FPDF_SetFormFieldHighlightAlpha(form_, kFormHighlightAlpha); |
| 2701 } |
| 2702 |
| 2703 if (!doc_loader_.IsDocumentComplete()) { |
2721 // Check if the first page is available. In a linearized PDF, that is not | 2704 // Check if the first page is available. In a linearized PDF, that is not |
2722 // always page 0. Doing this gives us the default page size, since when the | 2705 // always page 0. Doing this gives us the default page size, since when the |
2723 // document is available, the first page is available as well. | 2706 // document is available, the first page is available as well. |
2724 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_); | 2707 CheckPageAvailable(FPDFAvail_GetFirstPageNum(doc_), &pending_pages_); |
2725 } | 2708 } |
2726 | 2709 |
2727 LoadPageInfo(false); | 2710 LoadPageInfo(false); |
2728 | 2711 |
2729 if (doc_loader_->IsDocumentComplete()) | 2712 if (doc_loader_.IsDocumentComplete()) |
2730 FinishLoadingDocument(); | 2713 FinishLoadingDocument(); |
2731 } | 2714 } |
2732 | 2715 |
2733 void PDFiumEngine::LoadPageInfo(bool reload) { | 2716 void PDFiumEngine::LoadPageInfo(bool reload) { |
2734 if (!doc_loader_) | |
2735 return; | |
2736 pending_pages_.clear(); | 2717 pending_pages_.clear(); |
2737 pp::Size old_document_size = document_size_; | 2718 pp::Size old_document_size = document_size_; |
2738 document_size_ = pp::Size(); | 2719 document_size_ = pp::Size(); |
2739 std::vector<pp::Rect> page_rects; | 2720 std::vector<pp::Rect> page_rects; |
2740 int page_count = FPDF_GetPageCount(doc_); | 2721 int page_count = FPDF_GetPageCount(doc_); |
2741 bool doc_complete = doc_loader_->IsDocumentComplete(); | 2722 bool doc_complete = doc_loader_.IsDocumentComplete(); |
2742 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED; | 2723 bool is_linear = FPDFAvail_IsLinearized(fpdf_availability_) == PDF_LINEARIZED; |
2743 for (int i = 0; i < page_count; ++i) { | 2724 for (int i = 0; i < page_count; ++i) { |
2744 if (i != 0) { | 2725 if (i != 0) { |
2745 // Add space for horizontal separator. | 2726 // Add space for horizontal separator. |
2746 document_size_.Enlarge(0, kPageSeparatorThickness); | 2727 document_size_.Enlarge(0, kPageSeparatorThickness); |
2747 } | 2728 } |
2748 | 2729 |
2749 // Get page availability. If |reload| == true, then the document has been | 2730 // Get page availability. If |reload| == true, then the document has been |
2750 // constructed already. Get page availability flag from already existing | 2731 // constructed already. Get page availability flag from already existing |
2751 // PDFiumPage class. | 2732 // PDFiumPage class. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2788 pages_.push_back(base::MakeUnique<PDFiumPage>(this, i, page_rect, false)); | 2769 pages_.push_back(base::MakeUnique<PDFiumPage>(this, i, page_rect, false)); |
2789 } | 2770 } |
2790 } | 2771 } |
2791 | 2772 |
2792 CalculateVisiblePages(); | 2773 CalculateVisiblePages(); |
2793 if (document_size_ != old_document_size) | 2774 if (document_size_ != old_document_size) |
2794 client_->DocumentSizeUpdated(document_size_); | 2775 client_->DocumentSizeUpdated(document_size_); |
2795 } | 2776 } |
2796 | 2777 |
2797 void PDFiumEngine::CalculateVisiblePages() { | 2778 void PDFiumEngine::CalculateVisiblePages() { |
2798 if (!doc_loader_) | |
2799 return; | |
2800 // Clear pending requests queue, since it may contain requests to the pages | 2779 // Clear pending requests queue, since it may contain requests to the pages |
2801 // that are already invisible (after scrolling for example). | 2780 // that are already invisible (after scrolling for example). |
2802 pending_pages_.clear(); | 2781 pending_pages_.clear(); |
2803 doc_loader_->ClearPendingRequests(); | 2782 doc_loader_.ClearPendingRequests(); |
2804 | 2783 |
2805 visible_pages_.clear(); | 2784 visible_pages_.clear(); |
2806 pp::Rect visible_rect(plugin_size_); | 2785 pp::Rect visible_rect(plugin_size_); |
2807 for (size_t i = 0; i < pages_.size(); ++i) { | 2786 for (size_t i = 0; i < pages_.size(); ++i) { |
2808 // Check an entire PageScreenRect, since we might need to repaint side | 2787 // Check an entire PageScreenRect, since we might need to repaint side |
2809 // borders and shadows even if the page itself is not visible. | 2788 // borders and shadows even if the page itself is not visible. |
2810 // For example, when user use pdf with different page sizes and zoomed in | 2789 // For example, when user use pdf with different page sizes and zoomed in |
2811 // outside page area. | 2790 // outside page area. |
2812 if (visible_rect.Intersects(GetPageScreenRect(i))) { | 2791 if (visible_rect.Intersects(GetPageScreenRect(i))) { |
2813 visible_pages_.push_back(i); | 2792 visible_pages_.push_back(i); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2854 | 2833 |
2855 void PDFiumEngine::ScrollToPage(int page) { | 2834 void PDFiumEngine::ScrollToPage(int page) { |
2856 if (!PageIndexInBounds(page)) | 2835 if (!PageIndexInBounds(page)) |
2857 return; | 2836 return; |
2858 | 2837 |
2859 in_flight_visible_page_ = page; | 2838 in_flight_visible_page_ = page; |
2860 client_->ScrollToPage(page); | 2839 client_->ScrollToPage(page); |
2861 } | 2840 } |
2862 | 2841 |
2863 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { | 2842 bool PDFiumEngine::CheckPageAvailable(int index, std::vector<int>* pending) { |
2864 if (!doc_) | 2843 if (!doc_ || !form_) |
2865 return false; | 2844 return false; |
2866 | 2845 |
2867 const int num_pages = static_cast<int>(pages_.size()); | 2846 const int num_pages = static_cast<int>(pages_.size()); |
2868 if (index < num_pages && pages_[index]->available()) | 2847 if (index < num_pages && pages_[index]->available()) |
2869 return true; | 2848 return true; |
2870 | 2849 |
2871 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) { | 2850 if (!FPDFAvail_IsPageAvail(fpdf_availability_, index, &download_hints_)) { |
2872 if (!base::ContainsValue(*pending, index)) | 2851 if (!base::ContainsValue(*pending, index)) |
2873 pending->push_back(index); | 2852 pending->push_back(index); |
2874 return false; | 2853 return false; |
(...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4159 FPDF_DOCUMENT doc = | 4138 FPDF_DOCUMENT doc = |
4160 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4139 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
4161 if (!doc) | 4140 if (!doc) |
4162 return false; | 4141 return false; |
4163 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4142 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4164 FPDF_CloseDocument(doc); | 4143 FPDF_CloseDocument(doc); |
4165 return success; | 4144 return success; |
4166 } | 4145 } |
4167 | 4146 |
4168 } // namespace chrome_pdf | 4147 } // namespace chrome_pdf |
OLD | NEW |