Add support to Document::getAnnots method

Although notably, the parameters handling support is not
complete, CL intends to be the first step towards a more
complete implementation of this API.

TEST=testing/resources/javascript/bug_492_1.in

BUG=pdfium:492

Review-Url: https://codereview.chromium.org/2281273002
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 8fb9ce6..c1381f5 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -1090,7 +1090,48 @@
                             const std::vector<CJS_Value>& params,
                             CJS_Value& vRet,
                             CFX_WideString& sError) {
-  vRet.SetNull(CJS_Runtime::FromContext(cc));
+  CJS_Context* pContext = static_cast<CJS_Context*>(cc);
+
+  // TODO(tonikitoo): Add support supported parameters as per
+  // the PDF spec.
+
+  CJS_Runtime* pRuntime = pContext->GetJSRuntime();
+
+  int nPageNo = m_pDocument->GetPageCount();
+  CJS_Array annots;
+
+  for (int i = 0; i < nPageNo; ++i) {
+    CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(i);
+    if (!pPageView)
+      return FALSE;
+
+    CPDFSDK_AnnotIterator annotIterator(pPageView, false);
+    while (CPDFSDK_Annot* pSDKAnnotCur = annotIterator.Next()) {
+      CPDFSDK_BAAnnot* pSDKBAAnnot =
+          static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur);
+      if (!pSDKBAAnnot)
+        return FALSE;
+
+      v8::Local<v8::Object> pObj =
+          pRuntime->NewFxDynamicObj(CJS_Annot::g_nObjDefnID);
+      if (pObj.IsEmpty())
+        return FALSE;
+
+      CJS_Annot* pJS_Annot =
+          static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
+      if (!pJS_Annot)
+        return FALSE;
+
+      Annot* pAnnot = static_cast<Annot*>(pJS_Annot->GetEmbedObject());
+      if (!pAnnot)
+        return FALSE;
+
+      pAnnot->SetSDKAnnot(pSDKBAAnnot);
+      annots.SetElement(pRuntime, i, CJS_Value(pRuntime, pJS_Annot));
+    }
+  }
+
+  vRet = CJS_Value(pRuntime, annots);
   return TRUE;
 }
 
diff --git a/testing/resources/javascript/bug_492_1.in b/testing/resources/javascript/bug_492_1.in
new file mode 100644
index 0000000..f2b03b0
--- /dev/null
+++ b/testing/resources/javascript/bug_492_1.in
@@ -0,0 +1,95 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+  /OpenAction 20 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 2
+  /Kids [
+    10 0 R
+    11 0 R
+  ]
+>>
+endobj
+% Page number 0.
+{{object 10 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Resources <<
+    /Font <</F1 15 0 R>>
+  >>
+  /MediaBox [0 0 612 792]
+  /Annots [
+    22 0 R
+  ]
+  /Tabs /R
+>>
+endobj
+% Page number 1.
+{{object 11 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /Resources <<
+    /Font <</F1 15 0 R>>
+  >>
+  /MediaBox [0 0 612 792]
+  /Annots [
+    22 0 R
+  ]
+  /Tabs /C
+>>
+endobj
+
+% OpenAction action
+{{object 20 0}} <<
+  /Type /Action
+  /S /JavaScript
+  /JS 21 0 R
+>>
+endobj
+% JS program to exexute
+{{object 21 0}} <<
+>>
+stream
+  var annots = this.getAnnots();
+  for (var i = 0; i < annots.length; i++)
+    app.alert(annots[i].name);
+endstream
+endobj
+
+{{object 22 0}} <<
+  /Type /Annot
+  /Subtype  /Highlight
+  /QuadPoints [
+    115.80264
+    718.9139232
+    157.211172
+    718.9139232
+    115.80264
+    706.26441
+    6
+    157.211172
+    706.264416
+  ]
+  /Rect [ 115.75062 706.328568 157.001868 719.2715904 ]
+  /F 4
+  /Border [ 0 0 1 ]
+  /C [ 1 1 0 ]
+  /CA 1
+  /Contents <feff>
+  /M (D:20160712221733)
+  /NM (annot_s_name)
+  /P 9 0 R
+  /T <feff004a006100650020004800790075006e0020005000610072006b>
+>>
+endobj
+
+{{xref}}
+trailer <<
+  /Root 1 0 R
+>>
+{{startxref}}
+%%EOF
diff --git a/testing/resources/javascript/bug_492_1_expected.txt b/testing/resources/javascript/bug_492_1_expected.txt
new file mode 100644
index 0000000..2862e44
--- /dev/null
+++ b/testing/resources/javascript/bug_492_1_expected.txt
@@ -0,0 +1,2 @@
+Alert: annot_s_name
+Alert: annot_s_name
diff --git a/testing/resources/javascript/document_methods.in b/testing/resources/javascript/document_methods.in
index cdbd684..8c5a14a 100644
--- a/testing/resources/javascript/document_methods.in
+++ b/testing/resources/javascript/document_methods.in
@@ -147,6 +147,13 @@
    // TODO(tonikitoo): test success cases.
 }
 
+function testGetAnnots() {
+   // Method is present.
+   expect('typeof this.getAnnots', 'function');
+
+   // TODO(tonikitoo): test success cases.
+}
+
 function testGetField() {
    // Method is present.
    expect('typeof this.getField', 'function');
@@ -302,7 +309,6 @@
   testUnsupported('this.exportAsXFDF');
   testUnsupported('this.extractPages');
   testUnsupported('this.getAnnot3D');
-  testUnsupported('this.getAnnots');
   testUnsupported('this.getLinks');
   testUnsupported('this.getOCGs');
   testUnsupported('this.getPageBox');
@@ -320,6 +326,7 @@
   testAddIcon();
   testCalculateNow();
   testGetAnnot();
+  testGetAnnots();
   testGetField();
   testGetIcon();
   testGetNthFieldName();
diff --git a/testing/resources/javascript/document_methods_expected.txt b/testing/resources/javascript/document_methods_expected.txt
index 70a7481..4a2c4d7 100644
--- a/testing/resources/javascript/document_methods_expected.txt
+++ b/testing/resources/javascript/document_methods_expected.txt
@@ -32,9 +32,6 @@
 Alert: PASS: typeof this.getAnnot3D = function
 Alert: PASS: this.getAnnot3D() = undefined
 Alert: PASS: this.getAnnot3D(1, 2, "clams", [1, 2, 3]) = undefined
-Alert: PASS: typeof this.getAnnots = function
-Alert: PASS: this.getAnnots() = undefined
-Alert: PASS: this.getAnnots(1, 2, "clams", [1, 2, 3]) = undefined
 Alert: PASS: typeof this.getLinks = function
 Alert: PASS: this.getLinks() = undefined
 Alert: PASS: this.getLinks(1, 2, "clams", [1, 2, 3]) = undefined
@@ -83,6 +80,7 @@
 Alert: PASS: this.getAnnot() threw error Document.getAnnot: Incorrect number of parameters passed to function.
 Alert: PASS: this.getAnnot(0) threw error Document.getAnnot: Incorrect number of parameters passed to function.
 Alert: PASS: this.getAnnot(0, "test", 0) threw error Document.getAnnot: Incorrect number of parameters passed to function.
+Alert: PASS: typeof this.getAnnots = function
 Alert: PASS: typeof this.getField = function
 Alert: PASS: this.getField() threw error Document.getField: Incorrect number of parameters passed to function.
 Alert: PASS: typeof this.getIcon = function