• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
8 #define CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
9 
10 #include <map>
11 #include <memory>
12 #include <type_traits>
13 #include <utility>
14 #include <vector>
15 
16 #include "core/fpdfapi/parser/cpdf_object.h"
17 #include "core/fxcrt/fx_system.h"
18 #include "core/fxcrt/string_pool_template.h"
19 #include "core/fxcrt/weak_ptr.h"
20 #include "third_party/base/ptr_util.h"
21 
22 class CPDF_IndirectObjectHolder {
23  public:
24   using const_iterator =
25       std::map<uint32_t, std::unique_ptr<CPDF_Object>>::const_iterator;
26 
27   CPDF_IndirectObjectHolder();
28   virtual ~CPDF_IndirectObjectHolder();
29 
30   CPDF_Object* GetIndirectObject(uint32_t objnum) const;
31   virtual CPDF_Object* GetOrParseIndirectObject(uint32_t objnum);
32   void DeleteIndirectObject(uint32_t objnum);
33 
34   // Creates and adds a new object owned by the indirect object holder,
35   // and returns an unowned pointer to it.  We have a special case to
36   // handle objects that can intern strings from our ByteStringPool.
37   template <typename T, typename... Args>
NewIndirect(Args &&...args)38   typename std::enable_if<!CanInternStrings<T>::value, T*>::type NewIndirect(
39       Args&&... args) {
40     return static_cast<T*>(
41         AddIndirectObject(pdfium::MakeUnique<T>(std::forward<Args>(args)...)));
42   }
43   template <typename T, typename... Args>
NewIndirect(Args &&...args)44   typename std::enable_if<CanInternStrings<T>::value, T*>::type NewIndirect(
45       Args&&... args) {
46     return static_cast<T*>(AddIndirectObject(
47         pdfium::MakeUnique<T>(m_pByteStringPool, std::forward<Args>(args)...)));
48   }
49 
50   // Takes ownership of |pObj|, returns unowned pointer to it.
51   CPDF_Object* AddIndirectObject(std::unique_ptr<CPDF_Object> pObj);
52 
53   // Always takes ownership of |pObj|, return true if higher generation number.
54   bool ReplaceIndirectObjectIfHigherGeneration(
55       uint32_t objnum,
56       std::unique_ptr<CPDF_Object> pObj);
57 
GetLastObjNum()58   uint32_t GetLastObjNum() const { return m_LastObjNum; }
SetLastObjNum(uint32_t objnum)59   void SetLastObjNum(uint32_t objnum) { m_LastObjNum = objnum; }
60 
GetByteStringPool()61   WeakPtr<ByteStringPool> GetByteStringPool() const {
62     return m_pByteStringPool;
63   }
64 
begin()65   const_iterator begin() const { return m_IndirectObjs.begin(); }
end()66   const_iterator end() const { return m_IndirectObjs.end(); }
67 
68  protected:
69   virtual std::unique_ptr<CPDF_Object> ParseIndirectObject(uint32_t objnum);
70 
71  private:
72   uint32_t m_LastObjNum;
73   std::map<uint32_t, std::unique_ptr<CPDF_Object>> m_IndirectObjs;
74   std::vector<std::unique_ptr<CPDF_Object>> m_OrphanObjs;
75   WeakPtr<ByteStringPool> m_pByteStringPool;
76 };
77 
78 #endif  // CORE_FPDFAPI_PARSER_CPDF_INDIRECT_OBJECT_HOLDER_H_
79