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