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 2385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2396 return; | 2396 return; |
2397 } | 2397 } |
2398 | 2398 |
2399 // If we're in the middle of getting a password, just return. We will retry | 2399 // If we're in the middle of getting a password, just return. We will retry |
2400 // loading the document after we get the password anyway. | 2400 // loading the document after we get the password anyway. |
2401 if (getting_password_) | 2401 if (getting_password_) |
2402 return; | 2402 return; |
2403 | 2403 |
2404 ScopedUnsupportedFeature scoped_unsupported_feature(this); | 2404 ScopedUnsupportedFeature scoped_unsupported_feature(this); |
2405 bool needs_password = false; | 2405 bool needs_password = false; |
2406 if (TryLoadingDoc(false, std::string(), &needs_password)) { | 2406 if (TryLoadingDoc(std::string(), &needs_password)) { |
2407 ContinueLoadingDocument(false, std::string()); | 2407 ContinueLoadingDocument(std::string()); |
2408 return; | 2408 return; |
2409 } | 2409 } |
2410 if (needs_password) | 2410 if (needs_password) |
2411 GetPasswordAndLoad(); | 2411 GetPasswordAndLoad(); |
2412 else | 2412 else |
2413 client_->DocumentLoadFailed(); | 2413 client_->DocumentLoadFailed(); |
2414 } | 2414 } |
2415 | 2415 |
2416 bool PDFiumEngine::TryLoadingDoc(bool with_password, | 2416 bool PDFiumEngine::TryLoadingDoc(const std::string& password, |
2417 const std::string& password, | |
2418 bool* needs_password) { | 2417 bool* needs_password) { |
2419 *needs_password = false; | 2418 *needs_password = false; |
2420 if (doc_) | 2419 if (doc_) |
2421 return true; | 2420 return true; |
2422 | 2421 |
2423 const char* password_cstr = nullptr; | 2422 const char* password_cstr = nullptr; |
2424 if (with_password) { | 2423 if (!password.empty()) { |
2425 password_cstr = password.c_str(); | 2424 password_cstr = password.c_str(); |
2426 password_tries_remaining_--; | 2425 password_tries_remaining_--; |
2427 } | 2426 } |
2428 if (doc_loader_.IsDocumentComplete() && | 2427 if (doc_loader_.IsDocumentComplete() && |
2429 !FPDFAvail_IsLinearized(fpdf_availability_)) { | 2428 !FPDFAvail_IsLinearized(fpdf_availability_)) { |
2430 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); | 2429 doc_ = FPDF_LoadCustomDocument(&file_access_, password_cstr); |
2431 } else { | 2430 } else { |
2432 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); | 2431 doc_ = FPDFAvail_GetDocument(fpdf_availability_, password_cstr); |
2433 } | 2432 } |
2434 | 2433 |
2435 if (!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD) | 2434 if (!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD) |
2436 *needs_password = true; | 2435 *needs_password = true; |
2437 | 2436 |
2438 return !!doc_; | 2437 return !!doc_; |
2439 } | 2438 } |
2440 | 2439 |
2441 void PDFiumEngine::GetPasswordAndLoad() { | 2440 void PDFiumEngine::GetPasswordAndLoad() { |
2442 getting_password_ = true; | 2441 getting_password_ = true; |
2443 DCHECK(!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD); | 2442 DCHECK(!doc_ && FPDF_GetLastError() == FPDF_ERR_PASSWORD); |
2444 client_->GetDocumentPassword(password_factory_.NewCallbackWithOutput( | 2443 client_->GetDocumentPassword(password_factory_.NewCallbackWithOutput( |
2445 &PDFiumEngine::OnGetPasswordComplete)); | 2444 &PDFiumEngine::OnGetPasswordComplete)); |
2446 } | 2445 } |
2447 | 2446 |
2448 void PDFiumEngine::OnGetPasswordComplete(int32_t result, | 2447 void PDFiumEngine::OnGetPasswordComplete(int32_t result, |
2449 const pp::Var& password) { | 2448 const pp::Var& password) { |
2450 getting_password_ = false; | 2449 getting_password_ = false; |
2451 | 2450 |
2452 bool password_given = false; | |
2453 std::string password_text; | 2451 std::string password_text; |
2454 if (result == PP_OK && password.is_string()) { | 2452 if (result == PP_OK && password.is_string()) |
2455 password_text = password.AsString(); | 2453 password_text = password.AsString(); |
2456 if (!password_text.empty()) | 2454 ContinueLoadingDocument(password_text); |
2457 password_given = true; | |
2458 } | |
2459 ContinueLoadingDocument(password_given, password_text); | |
2460 } | 2455 } |
2461 | 2456 |
2462 void PDFiumEngine::ContinueLoadingDocument( | 2457 void PDFiumEngine::ContinueLoadingDocument(const std::string& password) { |
2463 bool has_password, | |
2464 const std::string& password) { | |
2465 ScopedUnsupportedFeature scoped_unsupported_feature(this); | 2458 ScopedUnsupportedFeature scoped_unsupported_feature(this); |
2466 | 2459 |
2467 bool needs_password = false; | 2460 bool needs_password = false; |
2468 bool loaded = TryLoadingDoc(has_password, password, &needs_password); | 2461 bool loaded = TryLoadingDoc(password, &needs_password); |
2469 bool password_incorrect = !loaded && has_password && needs_password; | 2462 bool password_incorrect = !loaded && needs_password && !password.empty(); |
2470 if (password_incorrect && password_tries_remaining_ > 0) { | 2463 if (password_incorrect && password_tries_remaining_ > 0) { |
2471 GetPasswordAndLoad(); | 2464 GetPasswordAndLoad(); |
2472 return; | 2465 return; |
2473 } | 2466 } |
2474 | 2467 |
2475 if (!doc_) { | 2468 if (!doc_) { |
2476 client_->DocumentLoadFailed(); | 2469 client_->DocumentLoadFailed(); |
2477 return; | 2470 return; |
2478 } | 2471 } |
2479 | 2472 |
(...skipping 1397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3877 FPDF_DOCUMENT doc = | 3870 FPDF_DOCUMENT doc = |
3878 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 3871 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
3879 if (!doc) | 3872 if (!doc) |
3880 return false; | 3873 return false; |
3881 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 3874 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
3882 FPDF_CloseDocument(doc); | 3875 FPDF_CloseDocument(doc); |
3883 return success; | 3876 return success; |
3884 } | 3877 } |
3885 | 3878 |
3886 } // namespace chrome_pdf | 3879 } // namespace chrome_pdf |
OLD | NEW |