1 // Copyright 2014 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 #include <algorithm>
8 
9 #include "xfa/src/foxitlib.h"
10 #include "xfa/src/fxfa/src/common/xfa_common.h"
11 #include "xfa_ffdochandler.h"
12 #include "xfa_fwladapter.h"
13 #include "xfa_ffdoc.h"
14 #include "xfa_ffapp.h"
15 #include "xfa_fwltheme.h"
16 #include "xfa_fontmgr.h"
17 #include "xfa_ffwidgethandler.h"
18 
CXFA_FileRead(const CFX_ArrayTemplate<CPDF_Stream * > & streams)19 CXFA_FileRead::CXFA_FileRead(const CFX_ArrayTemplate<CPDF_Stream*>& streams) {
20   int32_t iCount = streams.GetSize();
21   for (int32_t i = 0; i < iCount; i++) {
22     CPDF_StreamAcc& acc = m_Data.Add();
23     acc.LoadAllData(streams[i]);
24   }
25 }
GetSize()26 FX_FILESIZE CXFA_FileRead::GetSize() {
27   FX_DWORD dwSize = 0;
28   int32_t iCount = m_Data.GetSize();
29   for (int32_t i = 0; i < iCount; i++) {
30     CPDF_StreamAcc& acc = m_Data[i];
31     dwSize += acc.GetSize();
32   }
33   return dwSize;
34 }
ReadBlock(void * buffer,FX_FILESIZE offset,size_t size)35 FX_BOOL CXFA_FileRead::ReadBlock(void* buffer,
36                                  FX_FILESIZE offset,
37                                  size_t size) {
38   int32_t iCount = m_Data.GetSize();
39   int32_t index = 0;
40   while (index < iCount) {
41     CPDF_StreamAcc& acc = m_Data[index];
42     FX_FILESIZE dwSize = acc.GetSize();
43     if (offset < dwSize) {
44       break;
45     }
46     offset -= dwSize;
47     index++;
48   }
49   while (index < iCount) {
50     CPDF_StreamAcc& acc = m_Data[index];
51     FX_DWORD dwSize = acc.GetSize();
52     size_t dwRead = std::min(size, static_cast<size_t>(dwSize - offset));
53     FXSYS_memcpy(buffer, acc.GetData() + offset, dwRead);
54     size -= dwRead;
55     if (size == 0) {
56       return TRUE;
57     }
58     buffer = (uint8_t*)buffer + dwRead;
59     offset = 0;
60     index++;
61   }
62   return FALSE;
63 }
64 // static
Create(IXFA_AppProvider * pProvider)65 IXFA_App* IXFA_App::Create(IXFA_AppProvider* pProvider) {
66   return new CXFA_FFApp(pProvider);
67 }
68 // virtual
~IXFA_App()69 IXFA_App::~IXFA_App() {}
CXFA_FFApp(IXFA_AppProvider * pProvider)70 CXFA_FFApp::CXFA_FFApp(IXFA_AppProvider* pProvider)
71     : m_pDocHandler(nullptr),
72       m_pFWLTheme(nullptr),
73       m_pProvider(pProvider),
74       m_pFontMgr(nullptr),
75 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
76       m_pFontSource(nullptr),
77 #endif
78       m_pAdapterWidgetMgr(nullptr),
79       m_pWidgetMgrDelegate(nullptr),
80       m_pFDEFontMgr(nullptr),
81       m_pMenuHandler(nullptr),
82       m_pAdapterThreadMgr(nullptr) {
83   m_pFWLApp = IFWL_App::Create(this);
84   FWL_SetApp(m_pFWLApp);
85   m_pFWLApp->Initialize();
86   IXFA_TimeZoneProvider::Create();
87 }
~CXFA_FFApp()88 CXFA_FFApp::~CXFA_FFApp() {
89   if (m_pDocHandler) {
90     delete m_pDocHandler;
91   }
92   if (m_pFWLApp) {
93     m_pFWLApp->Finalize();
94     m_pFWLApp->Release();
95     delete m_pFWLApp;
96   }
97   if (m_pFWLTheme) {
98     m_pFWLTheme->Release();
99   }
100   if (m_pAdapterWidgetMgr) {
101     delete m_pAdapterWidgetMgr;
102   }
103   if (m_pAdapterThreadMgr) {
104     delete m_pAdapterThreadMgr;
105     m_pAdapterThreadMgr = NULL;
106   }
107   if (m_pMenuHandler) {
108     delete m_pMenuHandler;
109     m_pMenuHandler = NULL;
110   }
111   IXFA_TimeZoneProvider::Destroy();
112   if (m_pFontMgr != NULL) {
113     delete m_pFontMgr;
114     m_pFontMgr = NULL;
115   }
116 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
117   if (m_pFontSource != NULL) {
118     m_pFontSource->Release();
119   }
120 #endif
121   if (m_pFDEFontMgr) {
122     m_pFDEFontMgr->Release();
123   }
124 }
GetMenuHandler()125 IXFA_MenuHandler* CXFA_FFApp::GetMenuHandler() {
126   if (!m_pMenuHandler) {
127     m_pMenuHandler = new CXFA_FFMenuHandler;
128   }
129   return m_pMenuHandler;
130 }
GetDocHandler()131 IXFA_DocHandler* CXFA_FFApp::GetDocHandler() {
132   if (!m_pDocHandler) {
133     m_pDocHandler = new CXFA_FFDocHandler;
134   }
135   return m_pDocHandler;
136 }
CreateDoc(IXFA_DocProvider * pProvider,IFX_FileRead * pStream,FX_BOOL bTakeOverFile)137 IXFA_Doc* CXFA_FFApp::CreateDoc(IXFA_DocProvider* pProvider,
138                                 IFX_FileRead* pStream,
139                                 FX_BOOL bTakeOverFile) {
140   CXFA_FFDoc* pDoc = new CXFA_FFDoc(this, pProvider);
141   FX_BOOL bSuccess = pDoc->OpenDoc(pStream, bTakeOverFile);
142   if (!bSuccess) {
143     delete pDoc;
144     pDoc = NULL;
145   }
146   return pDoc;
147 }
CreateDoc(IXFA_DocProvider * pProvider,CPDF_Document * pPDFDoc)148 IXFA_Doc* CXFA_FFApp::CreateDoc(IXFA_DocProvider* pProvider,
149                                 CPDF_Document* pPDFDoc) {
150   if (pPDFDoc == NULL) {
151     return NULL;
152   }
153   CXFA_FFDoc* pDoc = new CXFA_FFDoc(this, pProvider);
154   FX_BOOL bSuccess = pDoc->OpenDoc(pPDFDoc);
155   if (!bSuccess) {
156     delete pDoc;
157     pDoc = NULL;
158   }
159   return pDoc;
160 }
161 
SetDefaultFontMgr(IXFA_FontMgr * pFontMgr)162 void CXFA_FFApp::SetDefaultFontMgr(IXFA_FontMgr* pFontMgr) {
163   if (!m_pFontMgr) {
164     m_pFontMgr = new CXFA_FontMgr();
165   }
166   m_pFontMgr->SetDefFontMgr(pFontMgr);
167 }
GetXFAFontMgr()168 CXFA_FontMgr* CXFA_FFApp::GetXFAFontMgr() {
169   return m_pFontMgr;
170 }
GetFDEFontMgr()171 IFX_FontMgr* CXFA_FFApp::GetFDEFontMgr() {
172   if (!m_pFDEFontMgr) {
173 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
174     m_pFDEFontMgr = IFX_FontMgr::Create(FX_GetDefFontEnumerator());
175 #else
176     m_pFontSource = FX_CreateDefaultFontSourceEnum();
177     m_pFDEFontMgr = IFX_FontMgr::Create(m_pFontSource);
178 #endif
179   }
180   return m_pFDEFontMgr;
181 }
GetFWLTheme()182 CXFA_FWLTheme* CXFA_FFApp::GetFWLTheme() {
183   if (!m_pFWLTheme) {
184     m_pFWLTheme = new CXFA_FWLTheme(this);
185   }
186   return m_pFWLTheme;
187 }
GetWidgetMgr(IFWL_WidgetMgrDelegate * pDelegate)188 IFWL_AdapterWidgetMgr* CXFA_FFApp::GetWidgetMgr(
189     IFWL_WidgetMgrDelegate* pDelegate) {
190   if (!m_pAdapterWidgetMgr) {
191     m_pAdapterWidgetMgr = new CXFA_FWLAdapterWidgetMgr;
192     pDelegate->OnSetCapability(FWL_WGTMGR_DisableThread |
193                                FWL_WGTMGR_DisableForm);
194     m_pWidgetMgrDelegate = pDelegate;
195   }
196   return m_pAdapterWidgetMgr;
197 }
GetThreadMgr()198 IFWL_AdapterThreadMgr* CXFA_FFApp::GetThreadMgr() {
199   if (!m_pAdapterThreadMgr) {
200     m_pAdapterThreadMgr = new CFWL_SDAdapterThreadMgr;
201   }
202   return m_pAdapterThreadMgr;
203 }
GetTimerMgr()204 IFWL_AdapterTimerMgr* CXFA_FFApp::GetTimerMgr() {
205   return m_pProvider->GetTimerMgr();
206 }
GetCursorMgr()207 IFWL_AdapterCursorMgr* CXFA_FFApp::GetCursorMgr() {
208   return NULL;
209 }
GetMonitorMgr()210 IFWL_AdapterMonitorMgr* CXFA_FFApp::GetMonitorMgr() {
211   return NULL;
212 }
GetClipboardMgr()213 IFWL_AdapterClipboardMgr* CXFA_FFApp::GetClipboardMgr() {
214   return NULL;
215 }
216