OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 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 "xfa/fde/css/fde_csscache.h" | 7 #include "xfa/fde/css/fde_csscache.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "core/fxcrt/include/fx_ext.h" | 11 #include "core/fxcrt/include/fx_ext.h" |
12 | 12 |
13 FDE_CSSCacheItem::FDE_CSSCacheItem(IFDE_CSSStyleSheet* p) | 13 FDE_CSSCacheItem::FDE_CSSCacheItem(IFDE_CSSStyleSheet* p) |
14 : pStylesheet(p), dwActivity(0) { | 14 : pStylesheet(p), dwActivity(0) { |
15 FXSYS_assert(pStylesheet); | 15 FXSYS_assert(pStylesheet); |
16 pStylesheet->AddRef(); | 16 pStylesheet->AddRef(); |
17 } | 17 } |
18 FDE_CSSCacheItem::~FDE_CSSCacheItem() { | 18 FDE_CSSCacheItem::~FDE_CSSCacheItem() { |
19 pStylesheet->Release(); | 19 pStylesheet->Release(); |
20 } | 20 } |
21 IFDE_CSSStyleSheetCache* IFDE_CSSStyleSheetCache::Create() { | |
22 return new CFDE_CSSStyleSheetCache; | |
23 } | |
24 | 21 |
25 CFDE_CSSStyleSheetCache::CFDE_CSSStyleSheetCache() | |
26 : m_pFixedStore(NULL), m_iMaxItems(5) {} | |
27 | |
28 CFDE_CSSStyleSheetCache::~CFDE_CSSStyleSheetCache() { | |
29 for (const auto& pair : m_Stylesheets) { | |
30 FXTARGET_DeleteWith(FDE_CSSCacheItem, m_pFixedStore, pair.second); | |
31 } | |
32 m_Stylesheets.clear(); | |
33 if (m_pFixedStore) { | |
34 m_pFixedStore->Release(); | |
35 } | |
36 } | |
37 void CFDE_CSSStyleSheetCache::AddStyleSheet(const CFX_ByteStringC& szKey, | |
38 IFDE_CSSStyleSheet* pStyleSheet) { | |
39 FXSYS_assert(pStyleSheet != NULL); | |
40 if (m_pFixedStore == NULL) { | |
41 m_pFixedStore = | |
42 FX_CreateAllocator(FX_ALLOCTYPE_Fixed, std::max(10, m_iMaxItems), | |
43 sizeof(FDE_CSSCacheItem)); | |
44 FXSYS_assert(m_pFixedStore != NULL); | |
45 } | |
46 auto it = m_Stylesheets.find(szKey); | |
47 if (it != m_Stylesheets.end()) { | |
48 FDE_CSSCacheItem* pItem = it->second; | |
49 if (pItem->pStylesheet != pStyleSheet) { | |
50 pItem->pStylesheet->Release(); | |
51 pItem->pStylesheet = pStyleSheet; | |
52 pItem->pStylesheet->AddRef(); | |
53 pItem->dwActivity = 0; | |
54 } | |
55 } else { | |
56 while (static_cast<int32_t>(m_Stylesheets.size()) >= m_iMaxItems) { | |
57 RemoveLowestActivityItem(); | |
58 } | |
59 m_Stylesheets[szKey] = | |
60 FXTARGET_NewWith(m_pFixedStore) FDE_CSSCacheItem(pStyleSheet); | |
61 } | |
62 } | |
63 IFDE_CSSStyleSheet* CFDE_CSSStyleSheetCache::GetStyleSheet( | |
64 const CFX_ByteStringC& szKey) const { | |
65 auto it = m_Stylesheets.find(szKey); | |
66 if (it == m_Stylesheets.end()) { | |
67 return nullptr; | |
68 } | |
69 FDE_CSSCacheItem* pItem = it->second; | |
70 pItem->dwActivity++; | |
71 pItem->pStylesheet->AddRef(); | |
72 return pItem->pStylesheet; | |
73 } | |
74 void CFDE_CSSStyleSheetCache::RemoveStyleSheet(const CFX_ByteStringC& szKey) { | |
75 auto it = m_Stylesheets.find(szKey); | |
76 if (it == m_Stylesheets.end()) { | |
77 return; | |
78 } | |
79 FXTARGET_DeleteWith(FDE_CSSCacheItem, m_pFixedStore, it->second); | |
80 m_Stylesheets.erase(it); | |
81 } | |
82 void CFDE_CSSStyleSheetCache::RemoveLowestActivityItem() { | |
83 auto found = m_Stylesheets.end(); | |
84 for (auto it = m_Stylesheets.begin(); it != m_Stylesheets.end(); ++it) { | |
85 switch (it->first.GetID()) { | |
86 case FXBSTR_ID('#', 'U', 'S', 'E'): | |
87 case FXBSTR_ID('#', 'A', 'G', 'E'): | |
88 continue; | |
89 } | |
90 if (found == m_Stylesheets.end() || | |
91 it->second->dwActivity > found->second->dwActivity) { | |
92 found = it; | |
93 } | |
94 } | |
95 if (found != m_Stylesheets.end()) { | |
96 FXTARGET_DeleteWith(FDE_CSSCacheItem, m_pFixedStore, found->second); | |
97 m_Stylesheets.erase(found); | |
98 } | |
99 } | |
100 FDE_CSSTagCache::FDE_CSSTagCache(FDE_CSSTagCache* parent, | 22 FDE_CSSTagCache::FDE_CSSTagCache(FDE_CSSTagCache* parent, |
101 IFDE_CSSTagProvider* tag) | 23 IFDE_CSSTagProvider* tag) |
102 : pTag(tag), | 24 : pTag(tag), |
103 pParent(parent), | 25 pParent(parent), |
104 dwIDHash(0), | 26 dwIDHash(0), |
105 dwTagHash(0), | 27 dwTagHash(0), |
106 iClassIndex(0), | 28 iClassIndex(0), |
107 dwClassHashs(1) { | 29 dwClassHashs(1) { |
108 FXSYS_assert(pTag != NULL); | 30 FXSYS_assert(pTag != NULL); |
109 CFX_WideStringC wsValue, wsName = pTag->GetTagName(); | 31 CFX_WideStringC wsValue, wsName = pTag->GetTagName(); |
(...skipping 29 matching lines...) Expand all Loading... |
139 void CFDE_CSSAccelerator::OnEnterTag(IFDE_CSSTagProvider* pTag) { | 61 void CFDE_CSSAccelerator::OnEnterTag(IFDE_CSSTagProvider* pTag) { |
140 FDE_CSSTagCache* pTop = GetTopElement(); | 62 FDE_CSSTagCache* pTop = GetTopElement(); |
141 FDE_CSSTagCache item(pTop, pTag); | 63 FDE_CSSTagCache item(pTop, pTag); |
142 m_Stack.Push(item); | 64 m_Stack.Push(item); |
143 } | 65 } |
144 void CFDE_CSSAccelerator::OnLeaveTag(IFDE_CSSTagProvider* pTag) { | 66 void CFDE_CSSAccelerator::OnLeaveTag(IFDE_CSSTagProvider* pTag) { |
145 FXSYS_assert(m_Stack.GetTopElement()); | 67 FXSYS_assert(m_Stack.GetTopElement()); |
146 FXSYS_assert(m_Stack.GetTopElement()->GetTag() == pTag); | 68 FXSYS_assert(m_Stack.GetTopElement()->GetTag() == pTag); |
147 m_Stack.Pop(); | 69 m_Stack.Pop(); |
148 } | 70 } |
OLD | NEW |