Introduce ::getPropertyInternal method to avoid duplication

Document.cpp implements "getter" methods for some of the
Document object properties.
Some of the body of such methods are identical.

Patch introduces a ::getPropertyInternal private method
that gets rid of this duplication.

Namely the following properties' getters are cleaned up:
- "author", "creationDate", "creator", "keywords",
- "modDate", "producer", "subject" and "title"

No behavior change.

Review-Url: https://codereview.chromium.org/2202283002
diff --git a/AUTHORS b/AUTHORS
index 7336dfd..1425768 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -9,6 +9,7 @@
 # See python fnmatch module documentation for more information.
 
 Andrey Khalyavin <halyavin@chromium.org>
+Antonio Gomes <tonikitoo@igalia.com>
 Brett Wilson <brettw@chromium.org>
 Bruce Dawson <brucedawson@chromium.org>
 Chery Cherian <cherycherian@gmail.com>
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 56b0b84..a4c639f 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -714,23 +714,7 @@
 FX_BOOL Document::author(IJS_Context* cc,
                          CJS_PropValue& vp,
                          CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Author");
-    return TRUE;
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString csAuthor;
-    vp >> csAuthor;
-    pDictionary->SetAtString("Author", PDF_EncodeText(csAuthor));
-    m_pDocument->SetChangeMark();
-    return TRUE;
-  }
+  return getPropertyInternal(cc, vp, "Author", sError);
 }
 
 FX_BOOL Document::info(IJS_Context* cc,
@@ -785,46 +769,38 @@
   return TRUE;
 }
 
-FX_BOOL Document::creationDate(IJS_Context* cc,
-                               CJS_PropValue& vp,
-                               CFX_WideString& sError) {
+FX_BOOL Document::getPropertyInternal(IJS_Context* cc,
+                                      CJS_PropValue& vp,
+                                      const CFX_ByteString& propName,
+                                      CFX_WideString& sError) {
   CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
   if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("CreationDate");
+    vp << pDictionary->GetUnicodeTextBy(propName);
   } else {
     if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
       return FALSE;
 
-    CFX_WideString csCreationDate;
-    vp >> csCreationDate;
-    pDictionary->SetAtString("CreationDate", PDF_EncodeText(csCreationDate));
+    CFX_WideString csProperty;
+    vp >> csProperty;
+    pDictionary->SetAtString(propName, PDF_EncodeText(csProperty));
     m_pDocument->SetChangeMark();
   }
   return TRUE;
 }
 
+FX_BOOL Document::creationDate(IJS_Context* cc,
+                               CJS_PropValue& vp,
+                               CFX_WideString& sError) {
+  return getPropertyInternal(cc, vp, "CreationDate", sError);
+}
+
 FX_BOOL Document::creator(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Creator");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString csCreator;
-    vp >> csCreator;
-    pDictionary->SetAtString("Creator", PDF_EncodeText(csCreator));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "Creator", sError);
 }
 
 FX_BOOL Document::delay(IJS_Context* cc,
@@ -852,85 +828,25 @@
 FX_BOOL Document::keywords(IJS_Context* cc,
                            CJS_PropValue& vp,
                            CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Keywords");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString csKeywords;
-    vp >> csKeywords;
-    pDictionary->SetAtString("Keywords", PDF_EncodeText(csKeywords));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "Keywords", sError);
 }
 
 FX_BOOL Document::modDate(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("ModDate");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString csmodDate;
-    vp >> csmodDate;
-    pDictionary->SetAtString("ModDate", PDF_EncodeText(csmodDate));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "ModDate", sError);
 }
 
 FX_BOOL Document::producer(IJS_Context* cc,
                            CJS_PropValue& vp,
                            CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Producer");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString csproducer;
-    vp >> csproducer;
-    pDictionary->SetAtString("Producer", PDF_EncodeText(csproducer));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "Producer", sError);
 }
 
 FX_BOOL Document::subject(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Subject");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString cssubject;
-    vp >> cssubject;
-    pDictionary->SetAtString("Subject", PDF_EncodeText(cssubject));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "Subject", sError);
 }
 
 FX_BOOL Document::title(IJS_Context* cc,
@@ -939,22 +855,7 @@
   if (!m_pDocument || !m_pDocument->GetUnderlyingDocument())
     return FALSE;
 
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
-  if (!pDictionary)
-    return FALSE;
-
-  if (vp.IsGetting()) {
-    vp << pDictionary->GetUnicodeTextBy("Title");
-  } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY))
-      return FALSE;
-
-    CFX_WideString cstitle;
-    vp >> cstitle;
-    pDictionary->SetAtString("Title", PDF_EncodeText(cstitle));
-    m_pDocument->SetChangeMark();
-  }
-  return TRUE;
+  return getPropertyInternal(cc, vp, "Title", sError);
 }
 
 FX_BOOL Document::numPages(IJS_Context* cc,
diff --git a/fpdfsdk/javascript/Document.h b/fpdfsdk/javascript/Document.h
index c0ef840..7e5d6df 100644
--- a/fpdfsdk/javascript/Document.h
+++ b/fpdfsdk/javascript/Document.h
@@ -272,6 +272,11 @@
   int CountWords(CPDF_TextObject* pTextObj);
   CFX_WideString GetObjWordStr(CPDF_TextObject* pTextObj, int nWordIndex);
 
+  FX_BOOL getPropertyInternal(IJS_Context* cc,
+                              CJS_PropValue& vp,
+                              const CFX_ByteString& propName,
+                              CFX_WideString& sError);
+
   v8::Isolate* m_isolate;
   std::list<std::unique_ptr<IconElement>> m_IconList;
   CPDFSDK_Document* m_pDocument;