Skip to content

Commit

Permalink
PDF: Cleanup more code now that it is completely out of process.
Browse files Browse the repository at this point in the history
BUG=303491

Review URL: https://codereview.chromium.org/1155963004

Cr-Commit-Position: refs/heads/master@{#331920}
  • Loading branch information
leizleiz authored and Commit bot committed May 29, 2015
1 parent 9d16592 commit 488102f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 51 deletions.
31 changes: 20 additions & 11 deletions pdf/document_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand Down
24 changes: 9 additions & 15 deletions pdf/out_of_process_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 2 additions & 6 deletions pdf/out_of_process_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pdf/pdf_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 7 additions & 13 deletions pdf/pdfium/pdfium_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,8 @@ FPDF_FILEHANDLER* PDFiumEngine::Form_OpenFile(FPDF_FORMFILLINFO* param,
url_str =
base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions pdf/pdfium/pdfium_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 488102f

Please sign in to comment.