Make CPDF_NameTree::LookupNamedDest() a static method.

All the callers to CPDF_NameTree::LookupNamedDest() have similar logic.
Consolidate that in preparation for making LookupNamedDest() work with
old style named destinations.

Change-Id: Iee851ebf79e780244921ea8a4b006deacd6e58bf
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/69751
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp
index e99dc86..388355e 100644
--- a/core/fpdfdoc/cpdf_dest.cpp
+++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -45,17 +45,10 @@
   if (!pDest)
     return CPDF_Dest();
 
-  if (pDest->IsString() || pDest->IsName()) {
-    auto name_tree = CPDF_NameTree::Create(pDoc, "Dests");
-    if (!name_tree)
-      return CPDF_Dest();
-    return CPDF_Dest(name_tree->LookupNamedDest(pDoc, pDest->GetString()));
-  }
+  if (pDest->IsString() || pDest->IsName())
+    return CPDF_Dest(CPDF_NameTree::LookupNamedDest(pDoc, pDest->GetString()));
 
-  const CPDF_Array* pArray = pDest->AsArray();
-  if (!pArray)
-    return CPDF_Dest();
-  return CPDF_Dest(pArray);
+  return CPDF_Dest(pDest->AsArray());
 }
 
 int CPDF_Dest::GetDestPageIndex(CPDF_Document* pDoc) const {
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index 0ff847b..e5319ee 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -367,6 +367,15 @@
   return pdfium::WrapUnique(new CPDF_NameTree(pRoot));  // Private ctor.
 }
 
+// static
+CPDF_Array* CPDF_NameTree::LookupNamedDest(CPDF_Document* pDoc,
+                                           const ByteString& name) {
+  std::unique_ptr<CPDF_NameTree> name_tree = Create(pDoc, "Dests");
+  if (!name_tree)
+    return nullptr;
+  return name_tree->LookupNamedDestImpl(pDoc, name);
+}
+
 size_t CPDF_NameTree::GetCount() const {
   return CountNamesInternal(m_pRoot.Get(), 0);
 }
@@ -462,8 +471,8 @@
                               nullptr);
 }
 
-CPDF_Array* CPDF_NameTree::LookupNamedDest(CPDF_Document* pDoc,
-                                           const ByteString& name) {
+CPDF_Array* CPDF_NameTree::LookupNamedDestImpl(CPDF_Document* pDoc,
+                                               const ByteString& name) {
   CPDF_Object* pValue = LookupValue(PDF_DecodeText(name.raw_span()));
   if (!pValue) {
     CPDF_Dictionary* pDests = pDoc->GetRoot()->GetDictFor("Dests");
diff --git a/core/fpdfdoc/cpdf_nametree.h b/core/fpdfdoc/cpdf_nametree.h
index 0d77b44..325867b 100644
--- a/core/fpdfdoc/cpdf_nametree.h
+++ b/core/fpdfdoc/cpdf_nametree.h
@@ -36,12 +36,14 @@
   static std::unique_ptr<CPDF_NameTree> CreateForTesting(
       CPDF_Dictionary* pRoot);
 
+  static CPDF_Array* LookupNamedDest(CPDF_Document* doc,
+                                     const ByteString& name);
+
   bool AddValueAndName(RetainPtr<CPDF_Object> pObj, const WideString& name);
   bool DeleteValueAndName(int nIndex);
 
   CPDF_Object* LookupValueAndName(int nIndex, WideString* csName) const;
   CPDF_Object* LookupValue(const WideString& csName) const;
-  CPDF_Array* LookupNamedDest(CPDF_Document* pDoc, const ByteString& name);
 
   size_t GetCount() const;
   CPDF_Dictionary* GetRootForTesting() const { return m_pRoot.Get(); }
@@ -49,6 +51,8 @@
  private:
   explicit CPDF_NameTree(CPDF_Dictionary* pRoot);
 
+  CPDF_Array* LookupNamedDestImpl(CPDF_Document* pDoc, const ByteString& name);
+
   const RetainPtr<CPDF_Dictionary> m_pRoot;
 };
 
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 3582837..61df7a6 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -1027,12 +1027,8 @@
   if (!pDoc)
     return nullptr;
 
-  auto name_tree = CPDF_NameTree::Create(pDoc, "Dests");
-  if (!name_tree)
-    return nullptr;
-
   ByteString dest_name(name);
-  return FPDFDestFromCPDFArray(name_tree->LookupNamedDest(pDoc, dest_name));
+  return FPDFDestFromCPDFArray(CPDF_NameTree::LookupNamedDest(pDoc, dest_name));
 }
 
 #ifdef PDF_ENABLE_V8
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 07c5945..14c7a20 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -1389,16 +1389,12 @@
     return CJS_Result::Failure(JSMessage::kBadObjectError);
 
   CPDF_Document* pDocument = m_pFormFillEnv->GetPDFDocument();
-  auto name_tree = CPDF_NameTree::Create(pDocument, "Dests");
-  if (!name_tree)
+  CPDF_Array* dest_array = CPDF_NameTree::LookupNamedDest(
+      pDocument, pRuntime->ToByteString(params[0]));
+  if (!dest_array)
     return CJS_Result::Failure(JSMessage::kBadObjectError);
 
-  CPDF_Array* destArray =
-      name_tree->LookupNamedDest(pDocument, pRuntime->ToByteString(params[0]));
-  if (!destArray)
-    return CJS_Result::Failure(JSMessage::kBadObjectError);
-
-  CPDF_Dest dest(destArray);
+  CPDF_Dest dest(dest_array);
   const CPDF_Array* arrayObject = dest.GetArray();
   std::vector<float> scrollPositionArray;
   if (arrayObject) {