Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: core/fpdfapi/parser/cpdf_dictionary.cpp

Issue 2510223002: Make CPDF_Dictionary use unique pointers. (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_dictionary.h ('k') | core/fpdfapi/parser/cpdf_document.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fpdfapi/parser/cpdf_dictionary.h" 7 #include "core/fpdfapi/parser/cpdf_dictionary.h"
8 8
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "core/fpdfapi/parser/cpdf_array.h" 12 #include "core/fpdfapi/parser/cpdf_array.h"
13 #include "core/fpdfapi/parser/cpdf_boolean.h" 13 #include "core/fpdfapi/parser/cpdf_boolean.h"
14 #include "core/fpdfapi/parser/cpdf_name.h" 14 #include "core/fpdfapi/parser/cpdf_name.h"
15 #include "core/fpdfapi/parser/cpdf_number.h" 15 #include "core/fpdfapi/parser/cpdf_number.h"
16 #include "core/fpdfapi/parser/cpdf_reference.h" 16 #include "core/fpdfapi/parser/cpdf_reference.h"
17 #include "core/fpdfapi/parser/cpdf_stream.h" 17 #include "core/fpdfapi/parser/cpdf_stream.h"
18 #include "core/fpdfapi/parser/cpdf_string.h" 18 #include "core/fpdfapi/parser/cpdf_string.h"
19 #include "third_party/base/logging.h" 19 #include "third_party/base/logging.h"
20 #include "third_party/base/stl_util.h" 20 #include "third_party/base/stl_util.h"
21 21
22 CPDF_Dictionary::CPDF_Dictionary() 22 CPDF_Dictionary::CPDF_Dictionary()
23 : CPDF_Dictionary(CFX_WeakPtr<CFX_ByteStringPool>()) {} 23 : CPDF_Dictionary(CFX_WeakPtr<CFX_ByteStringPool>()) {}
24 24
25 CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool) 25 CPDF_Dictionary::CPDF_Dictionary(const CFX_WeakPtr<CFX_ByteStringPool>& pPool)
26 : m_pPool(pPool) {} 26 : m_pPool(pPool) {}
27 27
28 CPDF_Dictionary::~CPDF_Dictionary() { 28 CPDF_Dictionary::~CPDF_Dictionary() {
29 // Mark the object as deleted so that it will not be deleted again 29 // Mark the object as deleted so that it will not be deleted again,
30 // in case of cyclic references. 30 // and break cyclic references.
31 m_ObjNum = kInvalidObjNum; 31 m_ObjNum = kInvalidObjNum;
32 for (const auto& it : m_Map) { 32 for (auto& it : m_Map) {
33 if (it.second && it.second->GetObjNum() != kInvalidObjNum) 33 if (it.second && it.second->GetObjNum() == kInvalidObjNum)
34 delete it.second; 34 it.second.release();
35 } 35 }
36 } 36 }
37 37
38 CPDF_Object::Type CPDF_Dictionary::GetType() const { 38 CPDF_Object::Type CPDF_Dictionary::GetType() const {
39 return DICTIONARY; 39 return DICTIONARY;
40 } 40 }
41 41
42 CPDF_Dictionary* CPDF_Dictionary::GetDict() const { 42 CPDF_Dictionary* CPDF_Dictionary::GetDict() const {
43 // The method should be made non-const if we want to not be const. 43 // The method should be made non-const if we want to not be const.
44 // See bug #234. 44 // See bug #234.
(...skipping 15 matching lines...) Expand all
60 std::unique_ptr<CPDF_Object> CPDF_Dictionary::Clone() const { 60 std::unique_ptr<CPDF_Object> CPDF_Dictionary::Clone() const {
61 return CloneObjectNonCyclic(false); 61 return CloneObjectNonCyclic(false);
62 } 62 }
63 63
64 std::unique_ptr<CPDF_Object> CPDF_Dictionary::CloneNonCyclic( 64 std::unique_ptr<CPDF_Object> CPDF_Dictionary::CloneNonCyclic(
65 bool bDirect, 65 bool bDirect,
66 std::set<const CPDF_Object*>* pVisited) const { 66 std::set<const CPDF_Object*>* pVisited) const {
67 pVisited->insert(this); 67 pVisited->insert(this);
68 auto pCopy = pdfium::MakeUnique<CPDF_Dictionary>(m_pPool); 68 auto pCopy = pdfium::MakeUnique<CPDF_Dictionary>(m_pPool);
69 for (const auto& it : *this) { 69 for (const auto& it : *this) {
70 CPDF_Object* value = it.second; 70 if (!pdfium::ContainsKey(*pVisited, it.second.get())) {
71 if (!pdfium::ContainsKey(*pVisited, value)) {
72 pCopy->m_Map.insert(std::make_pair( 71 pCopy->m_Map.insert(std::make_pair(
73 it.first, value->CloneNonCyclic(bDirect, pVisited).release())); 72 it.first, it.second->CloneNonCyclic(bDirect, pVisited)));
74 } 73 }
75 } 74 }
76 return std::move(pCopy); 75 return std::move(pCopy);
77 } 76 }
78 77
79 CPDF_Object* CPDF_Dictionary::GetObjectFor(const CFX_ByteString& key) const { 78 CPDF_Object* CPDF_Dictionary::GetObjectFor(const CFX_ByteString& key) const {
80 auto it = m_Map.find(key); 79 auto it = m_Map.find(key);
81 return it != m_Map.end() ? it->second : nullptr; 80 return it != m_Map.end() ? it->second.get() : nullptr;
82 } 81 }
83 82
84 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor( 83 CPDF_Object* CPDF_Dictionary::GetDirectObjectFor(
85 const CFX_ByteString& key) const { 84 const CFX_ByteString& key) const {
86 CPDF_Object* p = GetObjectFor(key); 85 CPDF_Object* p = GetObjectFor(key);
87 return p ? p->GetDirect() : nullptr; 86 return p ? p->GetDirect() : nullptr;
88 } 87 }
89 88
90 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key) const { 89 CFX_ByteString CPDF_Dictionary::GetStringFor(const CFX_ByteString& key) const {
91 CPDF_Object* p = GetObjectFor(key); 90 CPDF_Object* p = GetObjectFor(key);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 return pdfium::ContainsKey(m_Map, key); 165 return pdfium::ContainsKey(m_Map, key);
167 } 166 }
168 167
169 bool CPDF_Dictionary::IsSignatureDict() const { 168 bool CPDF_Dictionary::IsSignatureDict() const {
170 CPDF_Object* pType = GetDirectObjectFor("Type"); 169 CPDF_Object* pType = GetDirectObjectFor("Type");
171 if (!pType) 170 if (!pType)
172 pType = GetDirectObjectFor("FT"); 171 pType = GetDirectObjectFor("FT");
173 return pType && pType->GetString() == "Sig"; 172 return pType && pType->GetString() == "Sig";
174 } 173 }
175 174
176 void CPDF_Dictionary::SetFor(const CFX_ByteString& key, CPDF_Object* pObj) { 175 CPDF_Object* CPDF_Dictionary::SetFor(const CFX_ByteString& key,
177 CHECK(!pObj || pObj->IsInline()); 176 std::unique_ptr<CPDF_Object> pObj) {
178 auto it = m_Map.find(key); 177 if (!pObj) {
179 if (it == m_Map.end()) { 178 m_Map.erase(key);
180 if (pObj) 179 return nullptr;
181 m_Map.insert(std::make_pair(MaybeIntern(key), pObj));
182 return;
183 } 180 }
184 181 ASSERT(pObj->IsInline());
185 if (it->second == pObj) 182 CPDF_Object* pRet = pObj.get();
186 return; 183 m_Map[MaybeIntern(key)] = std::move(pObj);
187 delete it->second; 184 return pRet;
188
189 if (pObj)
190 it->second = pObj;
191 else
192 m_Map.erase(it);
193 } 185 }
194 186
195 void CPDF_Dictionary::ConvertToIndirectObjectFor( 187 void CPDF_Dictionary::ConvertToIndirectObjectFor(
196 const CFX_ByteString& key, 188 const CFX_ByteString& key,
197 CPDF_IndirectObjectHolder* pHolder) { 189 CPDF_IndirectObjectHolder* pHolder) {
198 auto it = m_Map.find(key); 190 auto it = m_Map.find(key);
199 if (it == m_Map.end() || it->second->IsReference()) 191 if (it == m_Map.end() || it->second->IsReference())
200 return; 192 return;
201 193
202 CPDF_Object* pObj = 194 CPDF_Object* pObj = pHolder->AddIndirectObject(std::move(it->second));
203 pHolder->AddIndirectObject(pdfium::WrapUnique(it->second)); 195 it->second = pdfium::MakeUnique<CPDF_Reference>(pHolder, pObj->GetObjNum());
204 it->second = new CPDF_Reference(pHolder, pObj->GetObjNum());
205 } 196 }
206 197
207 void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) { 198 void CPDF_Dictionary::RemoveFor(const CFX_ByteString& key) {
208 auto it = m_Map.find(key); 199 m_Map.erase(key);
209 if (it == m_Map.end())
210 return;
211
212 delete it->second;
213 m_Map.erase(it);
214 } 200 }
215 201
216 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey, 202 void CPDF_Dictionary::ReplaceKey(const CFX_ByteString& oldkey,
217 const CFX_ByteString& newkey) { 203 const CFX_ByteString& newkey) {
218 auto old_it = m_Map.find(oldkey); 204 auto old_it = m_Map.find(oldkey);
219 if (old_it == m_Map.end()) 205 if (old_it == m_Map.end())
220 return; 206 return;
221 207
222 auto new_it = m_Map.find(newkey); 208 auto new_it = m_Map.find(newkey);
223 if (new_it == old_it) 209 if (new_it == old_it)
224 return; 210 return;
225 211
226 if (new_it != m_Map.end()) { 212 m_Map[MaybeIntern(newkey)] = std::move(old_it->second);
227 delete new_it->second;
228 new_it->second = old_it->second;
229 } else {
230 m_Map.insert(std::make_pair(MaybeIntern(newkey), old_it->second));
231 }
232 m_Map.erase(old_it); 213 m_Map.erase(old_it);
233 } 214 }
234 215
235 void CPDF_Dictionary::SetIntegerFor(const CFX_ByteString& key, int i) {
236 SetFor(key, new CPDF_Number(i));
237 }
238
239 void CPDF_Dictionary::SetNameFor(const CFX_ByteString& key,
240 const CFX_ByteString& name) {
241 SetFor(key, new CPDF_Name(GetByteStringPool(), name));
242 }
243
244 void CPDF_Dictionary::SetStringFor(const CFX_ByteString& key,
245 const CFX_ByteString& str) {
246 SetFor(key, new CPDF_String(GetByteStringPool(), str, false));
247 }
248
249 void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key,
250 CPDF_IndirectObjectHolder* pDoc,
251 uint32_t objnum) {
252 SetFor(key, new CPDF_Reference(pDoc, objnum));
253 }
254
255 void CPDF_Dictionary::SetReferenceFor(const CFX_ByteString& key,
256 CPDF_IndirectObjectHolder* pDoc,
257 CPDF_Object* pObj) {
258 ASSERT(!pObj->IsInline());
259 SetFor(key, new CPDF_Reference(pDoc, pObj->GetObjNum()));
260 }
261
262 void CPDF_Dictionary::SetNumberFor(const CFX_ByteString& key, FX_FLOAT f) {
263 SetFor(key, new CPDF_Number(f));
264 }
265
266 void CPDF_Dictionary::SetBooleanFor(const CFX_ByteString& key, bool bValue) {
267 SetFor(key, new CPDF_Boolean(bValue));
268 }
269
270 void CPDF_Dictionary::SetRectFor(const CFX_ByteString& key, 216 void CPDF_Dictionary::SetRectFor(const CFX_ByteString& key,
271 const CFX_FloatRect& rect) { 217 const CFX_FloatRect& rect) {
272 CPDF_Array* pArray = new CPDF_Array; 218 CPDF_Array* pArray = SetNewFor<CPDF_Array>(key);
273 pArray->AddNew<CPDF_Number>(rect.left); 219 pArray->AddNew<CPDF_Number>(rect.left);
274 pArray->AddNew<CPDF_Number>(rect.bottom); 220 pArray->AddNew<CPDF_Number>(rect.bottom);
275 pArray->AddNew<CPDF_Number>(rect.right); 221 pArray->AddNew<CPDF_Number>(rect.right);
276 pArray->AddNew<CPDF_Number>(rect.top); 222 pArray->AddNew<CPDF_Number>(rect.top);
277 SetFor(key, pArray);
278 } 223 }
279 224
280 void CPDF_Dictionary::SetMatrixFor(const CFX_ByteString& key, 225 void CPDF_Dictionary::SetMatrixFor(const CFX_ByteString& key,
281 const CFX_Matrix& matrix) { 226 const CFX_Matrix& matrix) {
282 CPDF_Array* pArray = new CPDF_Array; 227 CPDF_Array* pArray = SetNewFor<CPDF_Array>(key);
283 pArray->AddNew<CPDF_Number>(matrix.a); 228 pArray->AddNew<CPDF_Number>(matrix.a);
284 pArray->AddNew<CPDF_Number>(matrix.b); 229 pArray->AddNew<CPDF_Number>(matrix.b);
285 pArray->AddNew<CPDF_Number>(matrix.c); 230 pArray->AddNew<CPDF_Number>(matrix.c);
286 pArray->AddNew<CPDF_Number>(matrix.d); 231 pArray->AddNew<CPDF_Number>(matrix.d);
287 pArray->AddNew<CPDF_Number>(matrix.e); 232 pArray->AddNew<CPDF_Number>(matrix.e);
288 pArray->AddNew<CPDF_Number>(matrix.f); 233 pArray->AddNew<CPDF_Number>(matrix.f);
289 SetFor(key, pArray);
290 } 234 }
291 235
292 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) { 236 CFX_ByteString CPDF_Dictionary::MaybeIntern(const CFX_ByteString& str) {
293 return m_pPool ? m_pPool->Intern(str) : str; 237 return m_pPool ? m_pPool->Intern(str) : str;
294 } 238 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_dictionary.h ('k') | core/fpdfapi/parser/cpdf_document.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698