Unify CPDF_DataAvail::LoadAllFile.

Use read validator to check whole file availability.

Change-Id: Ic118ccee38557d124ecde3893ee3c18c98c674d5
Reviewed-on: https://pdfium-review.googlesource.com/11712
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp
index 234a5a0..9d6b2f4 100644
--- a/core/fpdfapi/parser/cpdf_data_avail.cpp
+++ b/core/fpdfapi/parser/cpdf_data_avail.cpp
@@ -281,7 +281,7 @@
     case PDF_DATAAVAIL_LOADALLCROSSREF:
       return LoadAllXref(pHints);
     case PDF_DATAAVAIL_LOADALLFILE:
-      return LoadAllFile(pHints);
+      return LoadAllFile();
     case PDF_DATAAVAIL_ROOT:
       return CheckRoot();
     case PDF_DATAAVAIL_INFO:
@@ -298,7 +298,7 @@
       m_docStatus = PDF_DATAAVAIL_PAGE_LATERLOAD;
       return true;
     case PDF_DATAAVAIL_ERROR:
-      return LoadAllFile(pHints);
+      return LoadAllFile();
     case PDF_DATAAVAIL_PAGE_LATERLOAD:
       m_docStatus = PDF_DATAAVAIL_PAGE;
     default:
@@ -307,14 +307,14 @@
   }
 }
 
-bool CPDF_DataAvail::CheckPageStatus(DownloadHints* pHints) {
+bool CPDF_DataAvail::CheckPageStatus() {
   switch (m_docStatus) {
     case PDF_DATAAVAIL_PAGETREE:
       return CheckPages();
     case PDF_DATAAVAIL_PAGE:
       return CheckPage();
     case PDF_DATAAVAIL_ERROR:
-      return LoadAllFile(pHints);
+      return LoadAllFile();
     default:
       m_bPagesTreeLoad = true;
       m_bPagesLoad = true;
@@ -322,13 +322,12 @@
   }
 }
 
-bool CPDF_DataAvail::LoadAllFile(DownloadHints* pHints) {
-  if (m_pFileAvail->IsDataAvail(0, (uint32_t)m_dwFileLen)) {
+bool CPDF_DataAvail::LoadAllFile() {
+  if (GetValidator()->IsWholeFileAvailable()) {
     m_docStatus = PDF_DATAAVAIL_DONE;
     return true;
   }
-
-  pHints->AddSegment(0, (uint32_t)m_dwFileLen);
+  GetValidator()->ScheduleDownloadWholeFile();
   return false;
 }
 
@@ -1001,7 +1000,7 @@
   return true;
 }
 
-bool CPDF_DataAvail::CheckPage(uint32_t dwPage, DownloadHints* pHints) {
+bool CPDF_DataAvail::CheckPage(uint32_t dwPage) {
   while (true) {
     switch (m_docStatus) {
       case PDF_DATAAVAIL_PAGETREE:
@@ -1013,7 +1012,7 @@
           return false;
         break;
       case PDF_DATAAVAIL_ERROR:
-        return LoadAllFile(pHints);
+        return LoadAllFile();
       default:
         m_bPagesTreeLoad = true;
         m_bPagesLoad = true;
@@ -1225,9 +1224,9 @@
   return false;
 }
 
-bool CPDF_DataAvail::LoadPages(DownloadHints* pHints) {
+bool CPDF_DataAvail::LoadPages() {
   while (!m_bPagesTreeLoad) {
-    if (!CheckPageStatus(pHints))
+    if (!CheckPageStatus())
       return false;
   }
 
@@ -1359,22 +1358,21 @@
     }
 
     if (!m_bMainXRefLoadedOK) {
-      if (!LoadAllFile(pHints))
+      if (!LoadAllFile())
         return DataNotAvailable;
       m_pDocument->GetParser()->RebuildCrossRef();
       ResetFirstCheck(dwPage);
       return DataAvailable;
     }
     if (m_bTotalLoadPageTree) {
-      if (!LoadPages(pHints))
+      if (!LoadPages())
         return DataNotAvailable;
     } else {
-      if (!m_bCurPageDictLoadOK && !CheckPage(dwPage, pHints))
+      if (!m_bCurPageDictLoadOK && !CheckPage(dwPage))
         return DataNotAvailable;
     }
   } else {
-    if (!m_bTotalLoadPageTree && !m_bCurPageDictLoadOK &&
-        !CheckPage(dwPage, pHints)) {
+    if (!m_bTotalLoadPageTree && !m_bCurPageDictLoadOK && !CheckPage(dwPage)) {
       return DataNotAvailable;
     }
   }
diff --git a/core/fpdfapi/parser/cpdf_data_avail.h b/core/fpdfapi/parser/cpdf_data_avail.h
index e704e12..57fcc96 100644
--- a/core/fpdfapi/parser/cpdf_data_avail.h
+++ b/core/fpdfapi/parser/cpdf_data_avail.h
@@ -144,7 +144,7 @@
   bool CheckAcroForm();
   bool CheckAcroFormSubObject();
   bool CheckTrailerAppend(DownloadHints* pHints);
-  bool CheckPageStatus(DownloadHints* pHints);
+  bool CheckPageStatus();
 
   bool IsLinearizedFile(uint8_t* pData, uint32_t dwLen);
   void SetStartOffset(FX_FILESIZE dwOffset);
@@ -158,14 +158,14 @@
                                          bool* pExistInFile);
   bool GetPageKids(CPDF_Parser* pParser, CPDF_Object* pPages);
   bool PreparePageItem();
-  bool LoadPages(DownloadHints* pHints);
+  bool LoadPages();
   bool LoadAllXref(DownloadHints* pHints);
-  bool LoadAllFile(DownloadHints* pHints);
+  bool LoadAllFile();
   DocAvailStatus CheckLinearizedData(DownloadHints* pHints);
   bool CheckPageAnnots(uint32_t dwPage);
 
   DocAvailStatus CheckLinearizedFirstPage(uint32_t dwPage);
-  bool CheckPage(uint32_t dwPage, DownloadHints* pHints);
+  bool CheckPage(uint32_t dwPage);
   bool LoadDocPages();
   bool LoadDocPage(uint32_t dwPage);
   bool CheckPageNode(const PageNode& pageNode,
diff --git a/core/fpdfapi/parser/cpdf_read_validator.cpp b/core/fpdfapi/parser/cpdf_read_validator.cpp
index be20f18..d01b3c6 100644
--- a/core/fpdfapi/parser/cpdf_read_validator.cpp
+++ b/core/fpdfapi/parser/cpdf_read_validator.cpp
@@ -84,6 +84,13 @@
 }
 
 void CPDF_ReadValidator::ScheduleDownload(FX_FILESIZE offset, size_t size) {
+  const FX_SAFE_UINT32 safe_size = size;
+  if (safe_size.IsValid())
+    ScheduleDataDownload(offset, safe_size.ValueOrDie());
+}
+
+void CPDF_ReadValidator::ScheduleDataDownload(FX_FILESIZE offset,
+                                              uint32_t size) {
   if (!hints_ || size == 0)
     return;
 
@@ -105,3 +112,20 @@
   }
   hints_->AddSegment(start_segment_offset, segment_size.ValueOrDie());
 }
+
+void CPDF_ReadValidator::ScheduleDownloadWholeFile() {
+  const FX_SAFE_UINT32 safe_size = GetSize();
+  if (safe_size.IsValid())
+    ScheduleDataDownload(0, safe_size.ValueOrDie());
+}
+
+bool CPDF_ReadValidator::IsDataRangeAvailable(FX_FILESIZE offset,
+                                              uint32_t size) const {
+  return !file_avail_ || file_avail_->IsDataAvail(offset, size);
+}
+
+bool CPDF_ReadValidator::IsWholeFileAvailable() {
+  const FX_SAFE_UINT32 safe_size = GetSize();
+  return safe_size.IsValid() ? IsDataRangeAvailable(0, safe_size.ValueOrDie())
+                             : false;
+}
diff --git a/core/fpdfapi/parser/cpdf_read_validator.h b/core/fpdfapi/parser/cpdf_read_validator.h
index 9cc22c1..106f6e4 100644
--- a/core/fpdfapi/parser/cpdf_read_validator.h
+++ b/core/fpdfapi/parser/cpdf_read_validator.h
@@ -36,6 +36,12 @@
 
   void ResetErrors();
 
+  bool IsDataRangeAvailable(FX_FILESIZE offset, uint32_t size) const;
+  bool IsWholeFileAvailable();
+
+  void ScheduleDataDownload(FX_FILESIZE offset, uint32_t size);
+  void ScheduleDownloadWholeFile();
+
   // IFX_SeekableReadStream overrides:
   bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override;
   FX_FILESIZE GetSize() override;