Cleanup parts of CPDFSDK_AnnotIterator and CPDFSDK_PageView.

R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/1411203007 .
diff --git a/fpdfsdk/include/fsdk_annothandler.h b/fpdfsdk/include/fsdk_annothandler.h
index 369a080..8970545 100644
--- a/fpdfsdk/include/fsdk_annothandler.h
+++ b/fpdfsdk/include/fsdk_annothandler.h
@@ -8,6 +8,7 @@
 #define FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_
 
 #include <map>
+#include <vector>
 
 #include "../../core/include/fxcrt/fx_basic.h"
 
@@ -122,7 +123,7 @@
 
 class CPDFSDK_BFAnnotHandler : public IPDFSDK_AnnotHandler {
  public:
-  CPDFSDK_BFAnnotHandler(CPDFDoc_Environment* pApp)
+  explicit CPDFSDK_BFAnnotHandler(CPDFDoc_Environment* pApp)
       : m_pApp(pApp), m_pFormFiller(NULL) {}
   ~CPDFSDK_BFAnnotHandler() override {}
 
@@ -303,38 +304,20 @@
   CPDFDoc_Environment* m_pApp;
 };
 
-typedef int (*AI_COMPARE)(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2);
-
 class CPDFSDK_AnnotIterator {
  public:
-  CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
-                        FX_BOOL bReverse,
-                        FX_BOOL bIgnoreTopmost = FALSE,
-                        FX_BOOL bCircle = FALSE,
-                        CFX_PtrArray* pList = NULL);
-  virtual ~CPDFSDK_AnnotIterator() {}
+  CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView, bool bReverse);
+  ~CPDFSDK_AnnotIterator();
 
-  virtual CPDFSDK_Annot* Next(const CPDFSDK_Annot* pCurrent);
-  virtual CPDFSDK_Annot* Prev(const CPDFSDK_Annot* pCurrent);
-  virtual CPDFSDK_Annot* Next(int& index);
-  virtual CPDFSDK_Annot* Prev(int& index);
-  virtual int Count() { return m_pIteratorAnnotList.GetSize(); }
+  CPDFSDK_Annot* Next();
 
-  virtual FX_BOOL InitIteratorAnnotList(CPDFSDK_PageView* pPageView,
-                                        CFX_PtrArray* pList = NULL);
+ private:
+  CPDFSDK_Annot* NextAnnot();
+  CPDFSDK_Annot* PrevAnnot();
 
-  void InsertSort(CFX_PtrArray& arrayList, AI_COMPARE pCompare);
-
- protected:
-  CPDFSDK_Annot* NextAnnot(const CPDFSDK_Annot* pCurrent);
-  CPDFSDK_Annot* PrevAnnot(const CPDFSDK_Annot* pCurrent);
-  CPDFSDK_Annot* NextAnnot(int& index);
-  CPDFSDK_Annot* PrevAnnot(int& index);
-
-  CFX_PtrArray m_pIteratorAnnotList;
-  FX_BOOL m_bReverse;
-  FX_BOOL m_bIgnoreTopmost;
-  FX_BOOL m_bCircle;
+  std::vector<CPDFSDK_Annot*> m_iteratorAnnotList;
+  const bool m_bReverse;
+  size_t m_pos;
 };
 
 #endif  // FPDFSDK_INCLUDE_FSDK_ANNOTHANDLER_H_
diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h
index 068731e..fa2860a 100644
--- a/fpdfsdk/include/fsdk_mgr.h
+++ b/fpdfsdk/include/fsdk_mgr.h
@@ -12,6 +12,7 @@
 #include "../../core/include/fpdftext/fpdf_text.h"
 #include "../../public/fpdf_formfill.h"
 #include "../../public/fpdf_fwlevent.h"  // cross platform keycode and events define.
+#include "../../third_party/base/nonstd_unique_ptr.h"
 #include "fsdk_common.h"
 #include "fsdk_define.h"
 #include "fx_systemhandler.h"
@@ -307,8 +308,8 @@
   CPDFSDK_Annot* AddAnnot(const FX_CHAR* lpSubType, CPDF_Dictionary* pDict);
   CPDFSDK_Annot* AddAnnot(CPDF_Annot* pPDFAnnot);
   FX_BOOL DeleteAnnot(CPDFSDK_Annot* pAnnot);
-  int CountAnnots();
-  CPDFSDK_Annot* GetAnnot(int nIndex);
+  int CountAnnots() const;
+  CPDFSDK_Annot* GetAnnot(size_t nIndex);
   CPDFSDK_Annot* GetAnnotByDict(CPDF_Dictionary* pDict);
   CPDF_Page* GetPDFPage() { return m_page; }
   CPDF_Document* GetPDFDocument();
@@ -324,11 +325,13 @@
                        double deltaY,
                        const CPDF_Point& point,
                        int nFlag);
-  FX_BOOL IsValidAnnot(void* p);
+  FX_BOOL IsValidAnnot(CPDF_Annot* p) const;
   void GetCurrentMatrix(CPDF_Matrix& matrix) { matrix = m_curMatrix; }
   void UpdateRects(CFX_RectArray& rects);
   void UpdateView(CPDFSDK_Annot* pAnnot);
-  CFX_PtrArray* GetAnnotList() { return &m_fxAnnotArray; }
+  const std::vector<CPDFSDK_Annot*>& GetAnnotList() const {
+    return m_fxAnnotArray;
+  }
 
   int GetPageIndex();
   void LoadFXAnnots();
@@ -341,11 +344,11 @@
  private:
   void PageView_OnHighlightFormFields(CFX_RenderDevice* pDevice,
                                       CPDFSDK_Widget* pWidget);
+
   CPDF_Matrix m_curMatrix;
   CPDF_Page* m_page;
-  CPDF_AnnotList* m_pAnnotList;
-  // CPDFSDK_Annot* m_pFocusAnnot;
-  CFX_PtrArray m_fxAnnotArray;
+  nonstd::unique_ptr<CPDF_AnnotList> m_pAnnotList;
+  std::vector<CPDFSDK_Annot*> m_fxAnnotArray;
   CPDFSDK_Document* m_pSDKDoc;
   CPDFSDK_Widget* m_CaptureWidget;
   FX_BOOL m_bEnterWidget;
diff --git a/fpdfsdk/src/fsdk_annothandler.cpp b/fpdfsdk/src/fsdk_annothandler.cpp
index bb999e1..b1b2e2c 100644
--- a/fpdfsdk/src/fsdk_annothandler.cpp
+++ b/fpdfsdk/src/fsdk_annothandler.cpp
@@ -4,6 +4,8 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include <algorithm>
+
 #include "../include/fsdk_define.h"
 #include "../include/fsdk_mgr.h"
 #include "../include/formfiller/FFL_FormFiller.h"
@@ -649,164 +651,45 @@
   return rect.Contains(point.x, point.y);
 }
 
-// CReader_AnnotIteratorEx
-
 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView,
-                                             FX_BOOL bReverse,
-                                             FX_BOOL bIgnoreTopmost /*=FALSE*/,
-                                             FX_BOOL bCircle /*=FALSE*/,
-                                             CFX_PtrArray* pList /*=NULL*/) {
-  ASSERT(pPageView);
-  m_bReverse = bReverse;
-  m_bIgnoreTopmost = bIgnoreTopmost;
-  m_bCircle = bCircle;
-  m_pIteratorAnnotList.RemoveAll();
-  InitIteratorAnnotList(pPageView, pList);
-}
+                                             bool bReverse)
+    : m_bReverse(bReverse), m_pos(0) {
+  const std::vector<CPDFSDK_Annot*>& annots = pPageView->GetAnnotList();
+  m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), annots.rbegin(),
+                             annots.rend());
+  std::stable_sort(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
+                   [](CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
+                     return p1->GetLayoutOrder() < p2->GetLayoutOrder();
+                   });
 
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(const CPDFSDK_Annot* pCurrent) {
-  int index = -1;
-  int nCount = m_pIteratorAnnotList.GetSize();
-  if (pCurrent) {
-    for (int i = 0; i < nCount; i++) {
-      CPDFSDK_Annot* pReaderAnnot =
-          (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
-      if (pReaderAnnot == pCurrent) {
-        index = i;
-        break;
-      }
-    }
-  }
-  return NextAnnot(index);
-}
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(const CPDFSDK_Annot* pCurrent) {
-  int index = -1;
-  int nCount = m_pIteratorAnnotList.GetSize();
-  if (pCurrent) {
-    for (int i = 0; i < nCount; i++) {
-      CPDFSDK_Annot* pReaderAnnot =
-          (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
-      if (pReaderAnnot == pCurrent) {
-        index = i;
-        break;
-      }
-    }
-  }
-  return PrevAnnot(index);
-}
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot(int& index) {
-  int nCount = m_pIteratorAnnotList.GetSize();
-  if (nCount <= 0)
-    index = -1;
-  else {
-    if (index < 0) {
-      index = 0;
-    } else {
-      if (m_bCircle) {
-        index = (index < nCount - 1) ? (index + 1) : 0;
-      } else {
-        index = (index < nCount - 1) ? (index + 1) : -1;
-      }
-    }
-  }
-  return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
-}
+  CPDFSDK_Annot* pTopMostAnnot = pPageView->GetFocusAnnot();
+  if (!pTopMostAnnot)
+    return;
 
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot(int& index) {
-  int nCount = m_pIteratorAnnotList.GetSize();
-  if (nCount <= 0)
-    index = -1;
-  else {
-    if (index < 0) {
-      index = nCount - 1;
-    } else {
-      if (m_bCircle) {
-        index = (index > 0) ? (index - 1) : nCount - 1;
-      } else {
-        index = (index > 0) ? (index - 1) : -1;
-      }
-    }
-  }
-  return (index < 0) ? NULL : (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(index);
-}
-
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(const CPDFSDK_Annot* pCurrent) {
-  return (m_bReverse) ? PrevAnnot(pCurrent) : NextAnnot(pCurrent);
-}
-
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(const CPDFSDK_Annot* pCurrent) {
-  return (m_bReverse) ? NextAnnot(pCurrent) : PrevAnnot(pCurrent);
-}
-
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next(int& index) {
-  return (m_bReverse) ? PrevAnnot(index) : NextAnnot(index);
-}
-
-CPDFSDK_Annot* CPDFSDK_AnnotIterator::Prev(int& index) {
-  return (m_bReverse) ? NextAnnot(index) : PrevAnnot(index);
-}
-
-void CPDFSDK_AnnotIterator::InsertSort(CFX_PtrArray& arrayList,
-                                       AI_COMPARE pCompare) {
-  for (int i = 1; i < arrayList.GetSize(); i++) {
-    if (pCompare((CPDFSDK_Annot*)(arrayList[i]),
-                 (CPDFSDK_Annot*)(arrayList[i - 1])) < 0) {
-      int j = i - 1;
-      CPDFSDK_Annot* pTemp = (CPDFSDK_Annot*)arrayList[i];
-
-      do {
-        arrayList[j + 1] = arrayList[j];
-      } while (--j >= 0 && pCompare(pTemp, (CPDFSDK_Annot*)arrayList[j]) < 0);
-
-      arrayList[j + 1] = pTemp;
-    }
+  auto it = std::find(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(),
+                      pTopMostAnnot);
+  if (it != m_iteratorAnnotList.end()) {
+    CPDFSDK_Annot* pReaderAnnot = *it;
+    m_iteratorAnnotList.erase(it);
+    m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), pReaderAnnot);
   }
 }
 
-int LyOrderCompare(CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) {
-  if (p1->GetLayoutOrder() < p2->GetLayoutOrder())
-    return -1;
-  if (p1->GetLayoutOrder() > p2->GetLayoutOrder())
-    return 1;
-  return 0;
+CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() {
 }
 
-FX_BOOL CPDFSDK_AnnotIterator::InitIteratorAnnotList(
-    CPDFSDK_PageView* pPageView,
-    CFX_PtrArray* pAnnotList) {
-  ASSERT(pPageView);
+CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot() {
+  if (m_pos < m_iteratorAnnotList.size())
+    return m_iteratorAnnotList[m_pos++];
+  return nullptr;
+}
 
-  if (pAnnotList == NULL) {
-    pAnnotList = pPageView->GetAnnotList();
-  }
+CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot() {
+  if (m_pos < m_iteratorAnnotList.size())
+    return m_iteratorAnnotList[m_iteratorAnnotList.size() - ++m_pos];
+  return nullptr;
+}
 
-  m_pIteratorAnnotList.RemoveAll();
-  if (!pAnnotList)
-    return FALSE;
-
-  CPDFSDK_Annot* pTopMostAnnot =
-      (m_bIgnoreTopmost) ? NULL : pPageView->GetFocusAnnot();
-
-  int nCount = pAnnotList->GetSize();
-
-  for (int i = nCount - 1; i >= 0; i--) {
-    CPDFSDK_Annot* pReaderAnnot = (CPDFSDK_Annot*)pAnnotList->GetAt(i);
-    m_pIteratorAnnotList.Add(pReaderAnnot);
-  }
-
-  InsertSort(m_pIteratorAnnotList, &LyOrderCompare);
-
-  if (pTopMostAnnot) {
-    for (int i = 0; i < nCount; i++) {
-      CPDFSDK_Annot* pReaderAnnot =
-          (CPDFSDK_Annot*)m_pIteratorAnnotList.GetAt(i);
-      if (pReaderAnnot == pTopMostAnnot) {
-        m_pIteratorAnnotList.RemoveAt(i);
-        m_pIteratorAnnotList.InsertAt(0, pReaderAnnot);
-        break;
-      }
-    }
-  }
-
-  return TRUE;
+CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next() {
+  return m_bReverse ? PrevAnnot() : NextAnnot();
 }
diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp
index e083220..fad28e7 100644
--- a/fpdfsdk/src/fsdk_baseform.cpp
+++ b/fpdfsdk/src/fsdk_baseform.cpp
@@ -2431,7 +2431,7 @@
 
   switch (m_nTabs) {
     case BAI_STRUCTURE: {
-      for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
+      for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
         CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
         ASSERT(pAnnot != NULL);
 
@@ -2443,7 +2443,7 @@
       CPDFSDK_SortAnnots sa;
 
       {
-        for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
+        for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
           CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
           ASSERT(pAnnot != NULL);
 
@@ -2522,7 +2522,7 @@
       CPDFSDK_SortAnnots sa;
 
       {
-        for (int i = 0, sz = m_pPageView->CountAnnots(); i < sz; i++) {
+        for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) {
           CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i);
           ASSERT(pAnnot != NULL);
 
diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp
index f43a45b..53a774e 100644
--- a/fpdfsdk/src/fsdk_mgr.cpp
+++ b/fpdfsdk/src/fsdk_mgr.cpp
@@ -4,6 +4,8 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include <algorithm>
+
 #include "../../public/fpdf_ext.h"
 #include "../../third_party/base/nonstd_unique_ptr.h"
 #include "../include/formfiller/FFL_FormFiller.h"
@@ -19,7 +21,8 @@
 
 class CFX_SystemHandler : public IFX_SystemHandler {
  public:
-  CFX_SystemHandler(CPDFDoc_Environment* pEnv) : m_pEnv(pEnv), m_nCharSet(-1) {}
+  explicit CFX_SystemHandler(CPDFDoc_Environment* pEnv)
+      : m_pEnv(pEnv), m_nCharSet(-1) {}
   ~CFX_SystemHandler() override {}
 
  public:
@@ -606,47 +609,39 @@
 }
 
 CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc, CPDF_Page* page)
-    : m_page(page), m_pSDKDoc(pSDKDoc) {
+    : m_page(page),
+      m_pSDKDoc(pSDKDoc),
+      m_CaptureWidget(nullptr),
+      m_bEnterWidget(FALSE),
+      m_bExitWidget(FALSE),
+      m_bOnWidget(FALSE),
+      m_bValid(FALSE),
+      m_bLocked(FALSE),
+      m_bTakeOverPage(FALSE) {
   CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
   if (pInterForm) {
     CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
     pPDFInterForm->FixPageFields(page);
   }
-  m_page->SetPrivateData((void*)m_page, (void*)this, NULL);
-  m_fxAnnotArray.RemoveAll();
-
-  m_bEnterWidget = FALSE;
-  m_bExitWidget = FALSE;
-  m_bOnWidget = FALSE;
-  m_CaptureWidget = NULL;
-  m_bValid = FALSE;
-  m_bLocked = FALSE;
-  m_bTakeOverPage = FALSE;
+  m_page->SetPrivateData((void*)m_page, (void*)this, nullptr);
 }
 
 CPDFSDK_PageView::~CPDFSDK_PageView() {
   // if there is a focused annot on the page, we should kill the focus first.
   if (CPDFSDK_Annot* focusedAnnot = m_pSDKDoc->GetFocusAnnot()) {
-    for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
-      CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
-      if (pAnnot == focusedAnnot) {
-        KillFocusAnnot();
-        break;
-      }
-    }
+    auto it =
+        std::find(m_fxAnnotArray.begin(), m_fxAnnotArray.end(), focusedAnnot);
+    if (it != m_fxAnnotArray.end())
+      KillFocusAnnot();
   }
 
   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
   CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
-  ASSERT(pAnnotHandlerMgr);
-  for (int i = 0, count = m_fxAnnotArray.GetSize(); i < count; i++) {
-    CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
+  for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray)
     pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
-  }
-  m_fxAnnotArray.RemoveAll();
+  m_fxAnnotArray.clear();
 
-  delete m_pAnnotList;
-  m_pAnnotList = NULL;
+  m_pAnnotList.reset();
 
   m_page->RemovePrivateData((void*)m_page);
   if (m_bTakeOverPage) {
@@ -660,9 +655,8 @@
   m_curMatrix = *pUser2Device;
 
   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotIterator annotIterator(this, TRUE);
-  int index = -1;
-  while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next(index)) {
+  CPDFSDK_AnnotIterator annotIterator(this, true);
+  while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
     CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
     pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device, 0);
   }
@@ -670,7 +664,7 @@
 
 CPDF_Annot* CPDFSDK_PageView::GetPDFAnnotAtPoint(FX_FLOAT pageX,
                                                  FX_FLOAT pageY) {
-  int nCount = m_pAnnotList->Count();
+  int nCount = CountAnnots();
   for (int i = 0; i < nCount; i++) {
     CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
     CFX_FloatRect annotRect;
@@ -678,13 +672,13 @@
     if (annotRect.Contains(pageX, pageY))
       return pAnnot;
   }
-  return NULL;
+  return nullptr;
 }
 
 CPDF_Annot* CPDFSDK_PageView::GetPDFWidgetAtPoint(FX_FLOAT pageX,
                                                   FX_FLOAT pageY) {
-  int nCount = m_pAnnotList->Count();
-  for (int i = 0; i < nCount; i++) {
+  int nCount = CountAnnots();
+  for (int i = 0; i < nCount; ++i) {
     CPDF_Annot* pAnnot = m_pAnnotList->GetAt(i);
     if (pAnnot->GetSubType() == "Widget") {
       CFX_FloatRect annotRect;
@@ -693,33 +687,29 @@
         return pAnnot;
     }
   }
-  return NULL;
+  return nullptr;
 }
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
                                                    FX_FLOAT pageY) {
-  CPDFSDK_AnnotIterator annotIterator(this, FALSE);
   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
   CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
-  CPDFSDK_Annot* pSDKAnnot = NULL;
-  int index = -1;
-  while ((pSDKAnnot = annotIterator.Next(index))) {
+  CPDFSDK_AnnotIterator annotIterator(this, false);
+  while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
     CPDF_Rect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
     if (rc.Contains(pageX, pageY))
       return pSDKAnnot;
   }
 
-  return NULL;
+  return nullptr;
 }
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
                                                     FX_FLOAT pageY) {
-  CPDFSDK_AnnotIterator annotIterator(this, FALSE);
   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
   CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
-  CPDFSDK_Annot* pSDKAnnot = NULL;
-  int index = -1;
-  while ((pSDKAnnot = annotIterator.Next(index))) {
+  CPDFSDK_AnnotIterator annotIterator(this, false);
+  while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
     if (pSDKAnnot->GetType() == "Widget") {
       pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
       CPDF_Point point(pageX, pageY);
@@ -728,7 +718,7 @@
     }
   }
 
-  return NULL;
+  return nullptr;
 }
 
 FX_BOOL CPDFSDK_PageView::Annot_HasAppearance(CPDF_Annot* pAnnot) {
@@ -742,21 +732,15 @@
   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
   ASSERT(pEnv);
   CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
+  if (!pAnnotHandler)
+    return nullptr;
 
-  CPDFSDK_Annot* pSDKAnnot = NULL;
-
-  if (pAnnotHandler) {
-    pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
-  }
+  CPDFSDK_Annot* pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
   if (!pSDKAnnot)
-    return NULL;
+    return nullptr;
 
-  m_fxAnnotArray.Add(pSDKAnnot);
-
-  if (pAnnotHandler) {
-    pAnnotHandler->Annot_OnCreate(pSDKAnnot);
-  }
-
+  m_fxAnnotArray.push_back(pSDKAnnot);
+  pAnnotHandler->Annot_OnCreate(pSDKAnnot);
   return pSDKAnnot;
 }
 
@@ -780,27 +764,20 @@
   return NULL;
 }
 
-int CPDFSDK_PageView::CountAnnots() {
+int CPDFSDK_PageView::CountAnnots() const {
   return m_pAnnotList->Count();
 }
 
-CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(int nIndex) {
-  int nCount = m_fxAnnotArray.GetSize();
-  if (nIndex < 0 || nIndex >= nCount) {
-    return NULL;
-  }
-
-  return (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(nIndex);
+CPDFSDK_Annot* CPDFSDK_PageView::GetAnnot(size_t nIndex) {
+  return nIndex < m_fxAnnotArray.size() ? m_fxAnnotArray[nIndex] : nullptr;
 }
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetAnnotByDict(CPDF_Dictionary* pDict) {
-  int nCount = m_fxAnnotArray.GetSize();
-  for (int i = 0; i < nCount; i++) {
-    CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
-    if (pDict == pAnnot->GetPDFAnnot()->GetAnnotDict())
+  for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
+    if (pAnnot->GetPDFAnnot()->GetAnnotDict() == pDict)
       return pAnnot;
   }
-  return NULL;
+  return nullptr;
 }
 
 FX_BOOL CPDFSDK_PageView::OnLButtonDown(const CPDF_Point& point,
@@ -920,11 +897,11 @@
   FX_BOOL enableAPUpdate = CPDF_InterForm::UpdatingAPEnabled();
   // Disable the default AP construction.
   CPDF_InterForm::EnableUpdateAP(FALSE);
-  m_pAnnotList = new CPDF_AnnotList(m_page);
+  m_pAnnotList.reset(new CPDF_AnnotList(m_page));
   CPDF_InterForm::EnableUpdateAP(enableAPUpdate);
-  int nCount = m_pAnnotList->Count();
+  int nCount = CountAnnots();
   SetLock(TRUE);
-  for (int i = 0; i < nCount; i++) {
+  for (int i = 0; i < nCount; ++i) {
     CPDF_Annot* pPDFAnnot = m_pAnnotList->GetAt(i);
     CPDF_Document* pDoc = GetPDFDocument();
 
@@ -937,7 +914,7 @@
       CPDFSDK_Annot* pAnnot = pAnnotHandlerMgr->NewAnnot(pPDFAnnot, this);
       if (!pAnnot)
         continue;
-      m_fxAnnotArray.Add(pAnnot);
+      m_fxAnnotArray.push_back(pAnnot);
 
       pAnnotHandlerMgr->Annot_OnLoad(pAnnot);
     }
@@ -971,11 +948,12 @@
   return -1;
 }
 
-FX_BOOL CPDFSDK_PageView::IsValidAnnot(void* p) {
-  if (p == NULL)
+FX_BOOL CPDFSDK_PageView::IsValidAnnot(CPDF_Annot* p) const {
+  if (!p)
     return FALSE;
-  int iCount = m_pAnnotList->Count();
-  for (int i = 0; i < iCount; i++) {
+
+  int nCount = CountAnnots();
+  for (int i = 0; i < nCount; ++i) {
     if (m_pAnnotList->GetAt(i) == p)
       return TRUE;
   }
@@ -985,12 +963,11 @@
 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
   CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
   if (!pFocusAnnot)
-    return NULL;
+    return nullptr;
 
-  for (int i = 0; i < m_fxAnnotArray.GetSize(); i++) {
-    CPDFSDK_Annot* pAnnot = (CPDFSDK_Annot*)m_fxAnnotArray.GetAt(i);
+  for (CPDFSDK_Annot* pAnnot : m_fxAnnotArray) {
     if (pAnnot == pFocusAnnot)
       return pAnnot;
   }
-  return NULL;
+  return nullptr;
 }