diff --git a/pdf/document_loader.cc b/pdf/document_loader.cc index 03e82edbf4582b..165fffe88682e7 100644 --- a/pdf/document_loader.cc +++ b/pdf/document_loader.cc @@ -67,6 +67,15 @@ std::string GetMultiPartBoundary(const std::string& headers) { return std::string(); } +bool IsValidContentType(const std::string& type) { + return (EndsWith(type, "/pdf", false) || + EndsWith(type, ".pdf", false) || + EndsWith(type, "/x-pdf", false) || + EndsWith(type, "/*", false) || + EndsWith(type, "/acrobat", false) || + EndsWith(type, "/unknown", false)); +} + } // namespace DocumentLoader::Client::~Client() { @@ -106,7 +115,15 @@ bool DocumentLoader::Init(const pp::URLLoader& loader, uint32_t content_length = 0; std::string type; std::string disposition; - if (!response_headers.empty()) { + + // This happens for PDFs not loaded from http(s) sources. + if (response_headers == "Content-Type: text/plain") { + if (!StartsWithASCII(url, "http://", false) && + !StartsWithASCII(url, "https://", false)) { + type = "application/pdf"; + } + } + if (type.empty() && !response_headers.empty()) { net::HttpUtil::HeadersIterator it(response_headers.begin(), response_headers.end(), "\n"); while (it.GetNext()) { @@ -128,18 +145,10 @@ bool DocumentLoader::Init(const pp::URLLoader& loader, } } } - if (!type.empty() && - !EndsWith(type, "/pdf", false) && - !EndsWith(type, ".pdf", false) && - !EndsWith(type, "/x-pdf", false) && - !EndsWith(type, "/*", false) && - !EndsWith(type, "/acrobat", false) && - !EndsWith(type, "/unknown", false)) { + if (!type.empty() && !IsValidContentType(type)) return false; - } - if (StartsWithASCII(disposition, "attachment", false)) { + if (StartsWithASCII(disposition, "attachment", false)) return false; - } if (content_length > 0) chunk_stream_.Preallocate(content_length); diff --git a/pdf/out_of_process_instance.cc b/pdf/out_of_process_instance.cc index c75d04af27f160..b457b648dc400b 100644 --- a/pdf/out_of_process_instance.cc +++ b/pdf/out_of_process_instance.cc @@ -342,9 +342,9 @@ bool OutOfProcessInstance::Init(uint32_t argc, text_input_.reset(new pp::TextInput_Dev(this)); - const char* stream_url = NULL; - const char* original_url = NULL; - const char* headers = NULL; + const char* stream_url = nullptr; + const char* original_url = nullptr; + const char* headers = nullptr; bool is_material = false; for (uint32_t i = 0; i < argc; ++i) { if (strcmp(argn[i], "src") == 0) @@ -362,12 +362,6 @@ bool OutOfProcessInstance::Init(uint32_t argc, else background_color_ = kBackgroundColor; - // TODO(raymes): This is a hack to ensure that if no headers are passed in - // then we get the right MIME type. When the in process plugin is removed we - // can fix the document loader properly and remove this hack. - if (!headers || strcmp(headers, "") == 0) - headers = "content-type: application/pdf"; - if (!original_url) return false; @@ -443,7 +437,7 @@ void OutOfProcessInstance::HandleMessage(const pp::Var& message) { preview_engine_.reset(); engine_.reset(PDFEngine::Create(this)); engine_->SetGrayscale(dict.Get(pp::Var(kJSPrintPreviewGrayscale)).AsBool()); - engine_->New(url_.c_str()); + engine_->New(url_.c_str(), nullptr /* empty header */); print_preview_page_count_ = std::max(dict.Get(pp::Var(kJSPrintPreviewPageCount)).AsInt(), 0); @@ -908,7 +902,7 @@ void OutOfProcessInstance::UpdateCursor(PP_CursorType_Dev cursor) { } cursor_interface->SetCursor( - pp_instance(), cursor_, pp::ImageData().pp_resource(), NULL); + pp_instance(), cursor_, pp::ImageData().pp_resource(), nullptr); } void OutOfProcessInstance::UpdateTickMarks( @@ -1170,7 +1164,7 @@ void OutOfProcessInstance::PreviewDocumentLoadComplete() { if (print_preview_page_count_ == 0) return; - if (preview_pages_info_.size()) + if (!preview_pages_info_.empty()) LoadAvailablePreviewPage(); } @@ -1203,7 +1197,7 @@ void OutOfProcessInstance::PreviewDocumentLoadFailed() { preview_document_load_state_ = LOAD_STATE_FAILED; preview_pages_info_.pop(); - if (preview_pages_info_.size()) + if (!preview_pages_info_.empty()) LoadAvailablePreviewPage(); } @@ -1353,7 +1347,7 @@ void OutOfProcessInstance::AppendBlankPrintPreviewPages() { if (print_preview_page_count_ == 0) return; engine_->AppendBlankPages(print_preview_page_count_); - if (preview_pages_info_.size() > 0) + if (!preview_pages_info_.empty()) LoadAvailablePreviewPage(); } @@ -1386,7 +1380,7 @@ void OutOfProcessInstance::ProcessPreviewPageInfo(const std::string& url, } void OutOfProcessInstance::LoadAvailablePreviewPage() { - if (preview_pages_info_.size() <= 0 || + if (preview_pages_info_.empty() || document_load_state_ != LOAD_STATE_COMPLETE) { return; } diff --git a/pdf/out_of_process_instance.h b/pdf/out_of_process_instance.h index a458a924f87e28..8a3de66e2e7ba4 100644 --- a/pdf/out_of_process_instance.h +++ b/pdf/out_of_process_instance.h @@ -167,12 +167,6 @@ class OutOfProcessInstance : public pp::Instance, // frame's origin. pp::URLLoader CreateURLLoaderInternal(); - // Figure out the initial page to display based on #page=N and #nameddest=foo - // in the |url_|. - // Returns -1 if there is no valid fragment. The returned value is 0-based, - // whereas page=N is 1-based. - int GetInitialPage(const std::string& url); - void FormDidOpen(int32_t result); std::string GetLocalizedString(PP_ResourceString id); @@ -338,6 +332,8 @@ class OutOfProcessInstance : public pp::Instance, // The background color of the PDF viewer. uint32 background_color_; + + DISALLOW_COPY_AND_ASSIGN(OutOfProcessInstance); }; } // namespace chrome_pdf diff --git a/pdf/pdf_engine.h b/pdf/pdf_engine.h index e99ab47ed878b6..857db88728e910 100644 --- a/pdf/pdf_engine.h +++ b/pdf/pdf_engine.h @@ -184,11 +184,10 @@ class PDFEngine { static PDFEngine* Create(Client* client); virtual ~PDFEngine() {} + // Most of these functions are similar to the Pepper functions of the same // name, so not repeating the description here unless it's different. - virtual bool New(const char* url) = 0; - virtual bool New(const char* url, - const char* headers) = 0; + virtual bool New(const char* url, const char* headers) = 0; virtual void PageOffsetUpdated(const pp::Point& page_offset) = 0; virtual void PluginSizeUpdated(const pp::Size& size) = 0; virtual void ScrolledToXPosition(int position) = 0; diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc index f9feec8e0a7ed2..4b279462043f0e 100644 --- a/pdf/pdfium/pdfium_engine.cc +++ b/pdf/pdfium/pdfium_engine.cc @@ -941,8 +941,8 @@ FPDF_FILEHANDLER* PDFiumEngine::Form_OpenFile(FPDF_FORMFILLINFO* param, url_str = base::UTF16ToUTF8(reinterpret_cast(url)); } - // TODO: need to implement open file from the url - // Use a file path for the ease of testing + // TODO: need to implement open file from the url + // Use a file path for the ease of testing FILE* file = fopen(XFA_TESTFILE("tem.txt"), mode); FPDF_FILE* file_wrapper = new FPDF_FILE; file_wrapper->file = file; @@ -992,17 +992,11 @@ void PDFiumEngine::AddSegment(FX_DOWNLOADHINTS* param, return download_hints->loader->RequestData(offset, size); } -bool PDFiumEngine::New(const char* url) { - url_ = url; - headers_ = std::string(); - return true; -} - bool PDFiumEngine::New(const char* url, const char* headers) { url_ = url; if (!headers) - headers_ = std::string(); + headers_.clear(); else headers_ = headers; return true; @@ -1243,7 +1237,7 @@ void PDFiumEngine::FinishLoadingDocument() { FORM_DoPageAAction(new_page, form_, FPDFPAGE_AACTION_OPEN); } - if (doc_) // This can only happen if loading |doc_| fails. + if (doc_) // This can only happen if loading |doc_| fails. client_->DocumentLoadComplete(pages_.size()); } @@ -2003,8 +1997,8 @@ void PDFiumEngine::StartFind(const char* text, bool case_sensitive) { if (pages_[current_page]->available()) { base::string16 str = base::UTF8ToUTF16(text); - // Don't use PDFium to search for now, since it doesn't support unicode text. - // Leave the code for now to avoid bit-rot, in case it's fixed later. + // Don't use PDFium to search for now, since it doesn't support unicode + // text. Leave the code for now to avoid bit-rot, in case it's fixed later. if (0) { SearchUsingPDFium( str, case_sensitive, first_search, character_to_start_searching_from, @@ -2483,7 +2477,7 @@ bool PDFiumEngine::GetPageSizeAndUniformity(pp::Size* size) { } void PDFiumEngine::AppendBlankPages(int num_pages) { - DCHECK(num_pages != 0); + DCHECK_NE(num_pages, 0); if (!doc_) return; diff --git a/pdf/pdfium/pdfium_engine.h b/pdf/pdfium/pdfium_engine.h index 0c9ecd92637494..480a6e8e94abdc 100644 --- a/pdf/pdfium/pdfium_engine.h +++ b/pdf/pdfium/pdfium_engine.h @@ -46,9 +46,7 @@ class PDFiumEngine : public PDFEngine, virtual ~PDFiumEngine(); // PDFEngine implementation. - virtual bool New(const char* url); - virtual bool New(const char* url, - const char* headers); + virtual bool New(const char* url, const char* headers); virtual void PageOffsetUpdated(const pp::Point& page_offset); virtual void PluginSizeUpdated(const pp::Size& size); virtual void ScrolledToXPosition(int position);