Commit 0a43ea3e authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Chromium LUCI CQ

Trim whitespace from PDF metadata

Guarantee that every PDF metadata field is trimmed of whitespace
characters, so users of the metadata do not have to worry about checking
themselves.

Consequently, change PDFiumEngine::GetMetadataByField() to
PDFiumEngine::GetTrimmedMetadataByField().

Bug: 93619
Change-Id: Ie51c7099d147e50e4e507e43fcca3a17fbba183a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2609655
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#841712}
parent 8deb9f9f
...@@ -2374,7 +2374,7 @@ void OutOfProcessInstance::SendMetadata() { ...@@ -2374,7 +2374,7 @@ void OutOfProcessInstance::SendMetadata() {
metadata_message.Set(pp::Var(kType), pp::Var(kJSMetadataType)); metadata_message.Set(pp::Var(kType), pp::Var(kJSMetadataType));
const std::string& title = engine()->GetDocumentMetadata().title; const std::string& title = engine()->GetDocumentMetadata().title;
if (!base::TrimWhitespace(base::UTF8ToUTF16(title), base::TRIM_ALL).empty()) if (!title.empty())
metadata_message.Set(pp::Var(kJSTitle), pp::Var(title)); metadata_message.Set(pp::Var(kJSTitle), pp::Var(title));
metadata_message.Set( metadata_message.Set(
......
...@@ -3950,19 +3950,22 @@ void PDFiumEngine::LoadDocumentMetadata() { ...@@ -3950,19 +3950,22 @@ void PDFiumEngine::LoadDocumentMetadata() {
// Document information dictionary entries // Document information dictionary entries
doc_metadata_.version = GetDocumentVersion(); doc_metadata_.version = GetDocumentVersion();
doc_metadata_.title = GetMetadataByField("Title"); doc_metadata_.title = GetTrimmedMetadataByField("Title");
doc_metadata_.author = GetMetadataByField("Author"); doc_metadata_.author = GetTrimmedMetadataByField("Author");
doc_metadata_.subject = GetMetadataByField("Subject"); doc_metadata_.subject = GetTrimmedMetadataByField("Subject");
doc_metadata_.creator = GetMetadataByField("Creator"); doc_metadata_.creator = GetTrimmedMetadataByField("Creator");
doc_metadata_.producer = GetMetadataByField("Producer"); doc_metadata_.producer = GetTrimmedMetadataByField("Producer");
} }
std::string PDFiumEngine::GetMetadataByField(FPDF_BYTESTRING field) const { std::string PDFiumEngine::GetTrimmedMetadataByField(
FPDF_BYTESTRING field) const {
DCHECK(doc()); DCHECK(doc());
return base::UTF16ToUTF8(CallPDFiumWideStringBufferApi( base::string16 metadata = CallPDFiumWideStringBufferApi(
base::BindRepeating(&FPDF_GetMetaText, doc(), field), base::BindRepeating(&FPDF_GetMetaText, doc(), field),
/*check_expected_size=*/false)); /*check_expected_size=*/false);
return base::UTF16ToUTF8(base::TrimWhitespace(metadata, base::TRIM_ALL));
} }
PdfVersion PDFiumEngine::GetDocumentVersion() const { PdfVersion PDFiumEngine::GetDocumentVersion() const {
......
...@@ -608,9 +608,9 @@ class PDFiumEngine : public PDFEngine, ...@@ -608,9 +608,9 @@ class PDFiumEngine : public PDFEngine,
// document is loaded. // document is loaded.
void LoadDocumentMetadata(); void LoadDocumentMetadata();
// Retrieves the unparsed value of |field| in the document information // Retrieves the value of |field| in the document information dictionary.
// dictionary. // Trims whitespace characters from the retrieved value.
std::string GetMetadataByField(FPDF_BYTESTRING field) const; std::string GetTrimmedMetadataByField(FPDF_BYTESTRING field) const;
// Retrieves the version of the PDF (e.g. 1.4 or 2.0) as an enum. // Retrieves the version of the PDF (e.g. 1.4 or 2.0) as an enum.
PdfVersion GetDocumentVersion() const; PdfVersion GetDocumentVersion() const;
......
...@@ -18,13 +18,13 @@ endobj ...@@ -18,13 +18,13 @@ endobj
endobj endobj
{{object 4 0}} << {{object 4 0}} <<
/Author (Chromium Authors) /Author (Chromium Authors)
/CreationDate (D:20200205153912+00'00') /CreationDate (D:20200205153912+00'00' )
/Creator (Your Preferred Text Editor) /Creator (Your Preferred Text Editor)
/Keywords (testing,chromium,pdfium,document,info) /Keywords (testing,chromium,pdfium,document,info)
/ModDate (D:20200206094234+00'00') /ModDate (D:20200206094234+00'00')
/Producer (fixup_pdf_template.py) /Producer (fixup_pdf_template.py)
/Subject (Testing) /Subject (Testing)
/Title (Sample PDF Document Info) /Title ( Sample PDF Document Info )
>> >>
endobj endobj
{{xref}} {{xref}}
......
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment