Merge 25Q1 (ab/BP1A.250305.020) to AOSP main

Bug: 385190204
Merged-In: I97af0caebf0bbc5bd9642deb10411080f23f82e6
Change-Id: I0c103da535304fc1faf1b61a83b7b7aff9f613c4
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index be87afa..586162c 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -609,7 +609,7 @@
     }
     m_pDocument->AddIndirectObject(gsDict);
     name = RealizeResource(std::move(gsDict), "ExtGState");
-    pPageObj->SetGraphicsResourceNames({name});
+    pPageObj->m_GeneralState.SetGraphicsResourceNames({name});
     m_pObjHolder->GraphicsMapInsert(graphD, name);
   }
   *buf << "/" << PDF_NameEncode(name) << " gs ";
diff --git a/core/fpdfapi/page/cpdf_allstates.cpp b/core/fpdfapi/page/cpdf_allstates.cpp
index 996b3f79..e26c931 100644
--- a/core/fpdfapi/page/cpdf_allstates.cpp
+++ b/core/fpdfapi/page/cpdf_allstates.cpp
@@ -25,7 +25,6 @@
 
 void CPDF_AllStates::Copy(const CPDF_AllStates& src) {
   CopyStates(src);
-  m_GraphicsResourceNames = src.m_GraphicsResourceNames;
   m_TextMatrix = src.m_TextMatrix;
   m_ParentMatrix = src.m_ParentMatrix;
   m_CTM = src.m_CTM;
diff --git a/core/fpdfapi/page/cpdf_allstates.h b/core/fpdfapi/page/cpdf_allstates.h
index 3d4d9a3..d7bbf29 100644
--- a/core/fpdfapi/page/cpdf_allstates.h
+++ b/core/fpdfapi/page/cpdf_allstates.h
@@ -7,10 +7,7 @@
 #ifndef CORE_FPDFAPI_PAGE_CPDF_ALLSTATES_H_
 #define CORE_FPDFAPI_PAGE_CPDF_ALLSTATES_H_
 
-#include <vector>
-
 #include "core/fpdfapi/page/cpdf_graphicstates.h"
-#include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/fx_coordinates.h"
 
 class CPDF_Array;
@@ -27,7 +24,7 @@
                     CPDF_StreamContentParser* pParser);
   void SetLineDash(const CPDF_Array* pArray, float phase, float scale);
 
-  std::vector<ByteString> m_GraphicsResourceNames;
+
   CFX_Matrix m_TextMatrix;
   CFX_Matrix m_CTM;
   CFX_Matrix m_ParentMatrix;
diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp
index 8ebefa3..10e4c43 100644
--- a/core/fpdfapi/page/cpdf_generalstate.cpp
+++ b/core/fpdfapi/page/cpdf_generalstate.cpp
@@ -263,6 +263,24 @@
   return &m_Ref.GetPrivateCopy()->m_Matrix;
 }
 
+void CPDF_GeneralState::SetGraphicsResourceNames(
+  std::vector<ByteString> names) {
+  m_Ref.GetPrivateCopy()->m_GraphicsResourceNames = std::move(names);
+}
+
+void CPDF_GeneralState::AppendGraphicsResourceName(ByteString name) {
+  m_Ref.GetPrivateCopy()->m_GraphicsResourceNames.push_back(std::move(name));
+}
+
+pdfium::span<const ByteString> CPDF_GeneralState::GetGraphicsResourceNames()
+  const {
+  const StateData* data = m_Ref.GetObject();
+  if (!data) {
+    return {};
+  }
+  return data->m_GraphicsResourceNames;
+}
+
 CPDF_GeneralState::StateData::StateData() = default;
 
 CPDF_GeneralState::StateData::StateData(const StateData& that)
diff --git a/core/fpdfapi/page/cpdf_generalstate.h b/core/fpdfapi/page/cpdf_generalstate.h
index 2fb2285..a04a0af 100644
--- a/core/fpdfapi/page/cpdf_generalstate.h
+++ b/core/fpdfapi/page/cpdf_generalstate.h
@@ -7,12 +7,15 @@
 #ifndef CORE_FPDFAPI_PAGE_CPDF_GENERALSTATE_H_
 #define CORE_FPDFAPI_PAGE_CPDF_GENERALSTATE_H_
 
+#include <vector>
+
 #include "constants/transparency.h"
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/retain_ptr.h"
 #include "core/fxcrt/shared_copy_on_write.h"
 #include "core/fxge/dib/fx_dib.h"
+#include "third_party/base/containers/span.h"
 
 class CPDF_Dictionary;
 class CPDF_Object;
@@ -79,6 +82,10 @@
   void SetMatrix(const CFX_Matrix& matrix);
   CFX_Matrix* GetMutableMatrix();
 
+  void SetGraphicsResourceNames(std::vector<ByteString> names);
+  void AppendGraphicsResourceName(ByteString name);
+  pdfium::span<const ByteString> GetGraphicsResourceNames() const;
+
  private:
   class StateData final : public Retainable {
    public:
@@ -107,6 +114,8 @@
     RetainPtr<const CPDF_Object> m_pHT;
     float m_Flatness = 1.0f;
     float m_Smoothness = 0.0f;
+    // The resource names of the graphics states that apply to this object.
+    std::vector<ByteString> m_GraphicsResourceNames;
 
    private:
     StateData();
diff --git a/core/fpdfapi/page/cpdf_pageobject.cpp b/core/fpdfapi/page/cpdf_pageobject.cpp
index a7b2156..03c20d1 100644
--- a/core/fpdfapi/page/cpdf_pageobject.cpp
+++ b/core/fpdfapi/page/cpdf_pageobject.cpp
@@ -75,9 +75,9 @@
   return nullptr;
 }
 
-void CPDF_PageObject::SetGraphicsResourceNames(
-    std::vector<ByteString> resource_names) {
-  m_GraphicsResourceNames = std::move(resource_names);
+pdfium::span<const ByteString> CPDF_PageObject::GetGraphicsResourceNames()
+  const {
+  return m_GeneralState.GetGraphicsResourceNames();
 }
 
 void CPDF_PageObject::CopyData(const CPDF_PageObject* pSrc) {
diff --git a/core/fpdfapi/page/cpdf_pageobject.h b/core/fpdfapi/page/cpdf_pageobject.h
index ccdbbaf..7eaaf41 100644
--- a/core/fpdfapi/page/cpdf_pageobject.h
+++ b/core/fpdfapi/page/cpdf_pageobject.h
@@ -9,12 +9,12 @@
 
 #include <stdint.h>
 
-#include <vector>
 
 #include "core/fpdfapi/page/cpdf_contentmarks.h"
 #include "core/fpdfapi/page/cpdf_graphicstates.h"
 #include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/fx_coordinates.h"
+#include "third_party/base/containers/span.h"
 
 class CPDF_FormObject;
 class CPDF_ImageObject;
@@ -96,10 +96,7 @@
     m_ResourceName = resource_name;
   }
 
-  const std::vector<ByteString>& GetGraphicsResourceNames() const {
-    return m_GraphicsResourceNames;
-  }
-  void SetGraphicsResourceNames(std::vector<ByteString> resource_names);
+  pdfium::span<const ByteString> GetGraphicsResourceNames() const;
 
  protected:
   void CopyData(const CPDF_PageObject* pSrcObject);
@@ -112,9 +109,6 @@
   int32_t m_ContentStream;
   // The resource name for this object.
   ByteString m_ResourceName;
-  // Like `m_ResourceName` but for graphics. Though unlike the resource name,
-  // multiple graphics states can apply at once.
-  std::vector<ByteString> m_GraphicsResourceNames;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECT_H_
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index cae9d72..6c559af 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -429,7 +429,6 @@
   if (bText) {
     pObj->m_TextState = m_pCurStates->m_TextState;
   }
-  pObj->SetGraphicsResourceNames(m_pCurStates->m_GraphicsResourceNames);
 }
 
 // static
@@ -790,7 +789,6 @@
   auto pFormObj = std::make_unique<CPDF_FormObject>(GetCurrentStreamIndex(),
                                                     std::move(form), matrix);
   pFormObj->SetResourceName(name);
-  pFormObj->SetGraphicsResourceNames(m_pCurStates->m_GraphicsResourceNames);
   if (!m_pObjectHolder->BackgroundAlphaNeeded() &&
       pFormObj->form()->BackgroundAlphaNeeded()) {
     m_pObjectHolder->SetBackgroundAlphaNeeded(true);
@@ -916,7 +914,7 @@
     return;
 
   CHECK(!name.IsEmpty());
-  m_pCurStates->m_GraphicsResourceNames.push_back(std::move(name));
+  m_pCurStates->m_GeneralState.AppendGraphicsResourceName(std::move(name));
   m_pCurStates->ProcessExtGS(pGS.Get(), this);
 }