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 "../../include/fxcodec/fx_codec.h"
8 #include "../../include/fpdfapi/fpdf_module.h"
9 static CPDF_ModuleMgr*	g_FPDFAPI_pDefaultMgr = NULL;
Get()10 CPDF_ModuleMgr* CPDF_ModuleMgr::Get()
11 {
12     return g_FPDFAPI_pDefaultMgr;
13 }
Create()14 void CPDF_ModuleMgr::Create()
15 {
16     g_FPDFAPI_pDefaultMgr = new CPDF_ModuleMgr;
17     g_FPDFAPI_pDefaultMgr->Initialize();
18 }
Destroy()19 void CPDF_ModuleMgr::Destroy()
20 {
21     if (g_FPDFAPI_pDefaultMgr) {
22         delete g_FPDFAPI_pDefaultMgr;
23     }
24     g_FPDFAPI_pDefaultMgr = NULL;
25 }
CPDF_ModuleMgr()26 CPDF_ModuleMgr::CPDF_ModuleMgr()
27 {
28     m_pCodecModule = NULL;
29     m_pPageModule = NULL;
30     m_pRenderModule = NULL;
31     m_FileBufSize = 512;
32 }
Initialize()33 void CPDF_ModuleMgr::Initialize()
34 {
35     InitModules();
36     m_FileBufSize = 512;
37 }
InitModules()38 void CPDF_ModuleMgr::InitModules()
39 {
40     m_pCodecModule = NULL;
41     m_pPageModule = new CPDF_PageModuleDef;
42     m_pRenderModule = new CPDF_RenderModuleDef;
43 }
~CPDF_ModuleMgr()44 CPDF_ModuleMgr::~CPDF_ModuleMgr()
45 {
46     if (m_pPageModule) {
47         delete m_pPageModule;
48     }
49     if (m_pRenderModule) {
50         delete m_pRenderModule;
51     }
52 }
SetDownloadCallback(FX_BOOL (* callback)(FX_LPCSTR module_name))53 void CPDF_ModuleMgr::SetDownloadCallback(FX_BOOL (*callback)(FX_LPCSTR module_name))
54 {
55     m_pDownloadCallback = callback;
56 }
DownloadModule(FX_LPCSTR module_name)57 FX_BOOL CPDF_ModuleMgr::DownloadModule(FX_LPCSTR module_name)
58 {
59     if (m_pDownloadCallback == NULL) {
60         return FALSE;
61     }
62     return m_pDownloadCallback(module_name);
63 }
NotifyModuleAvailable(FX_LPCSTR module_name)64 void CPDF_ModuleMgr::NotifyModuleAvailable(FX_LPCSTR module_name)
65 {
66     if (FXSYS_strcmp(module_name, ADDIN_NAME_CJK) == 0) {
67         m_pPageModule->NotifyCJKAvailable();
68     } else if (FXSYS_strcmp(module_name, ADDIN_NAME_DECODER) == 0) {
69         m_pRenderModule->NotifyDecoderAvailable();
70     }
71 }
RegisterSecurityHandler(FX_LPCSTR filter,CPDF_SecurityHandler * (* CreateHandler)(void * param),void * param)72 void CPDF_ModuleMgr::RegisterSecurityHandler(FX_LPCSTR filter, CPDF_SecurityHandler * (*CreateHandler)(void* param), void* param)
73 {
74     if (CreateHandler == NULL) {
75         m_SecurityHandlerMap.RemoveKey(filter);
76     } else {
77         m_SecurityHandlerMap.SetAt(filter, (void*)CreateHandler);
78     }
79     if (param) {
80         m_SecurityHandlerMap.SetAt(FX_BSTRC("_param_") + filter, param);
81     }
82 }
SetPrivateData(FX_LPVOID module_id,FX_LPVOID pData,PD_CALLBACK_FREEDATA callback)83 void CPDF_ModuleMgr::SetPrivateData(FX_LPVOID module_id, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback)
84 {
85     m_privateData.SetPrivateData(module_id, pData, callback);
86 }
GetPrivateData(FX_LPVOID module_id)87 FX_LPVOID CPDF_ModuleMgr::GetPrivateData(FX_LPVOID module_id)
88 {
89     return m_privateData.GetPrivateData(module_id);
90 }
CreateSecurityHandler(FX_LPCSTR filter)91 CPDF_SecurityHandler* CPDF_ModuleMgr::CreateSecurityHandler(FX_LPCSTR filter)
92 {
93     CPDF_SecurityHandler* (*CreateHandler)(void*) = NULL;
94     if (!m_SecurityHandlerMap.Lookup(filter, (void*&)CreateHandler)) {
95         return NULL;
96     }
97     if (CreateHandler == NULL) {
98         return NULL;
99     }
100     void* param = NULL;
101     m_SecurityHandlerMap.Lookup(FX_BSTRC("_param_") + filter, param);
102     return CreateHandler(param);
103 }
GetFaxModule()104 ICodec_FaxModule* CPDF_ModuleMgr::GetFaxModule()
105 {
106     return m_pCodecModule ? m_pCodecModule->GetFaxModule() : NULL;
107 }
GetJpegModule()108 ICodec_JpegModule* CPDF_ModuleMgr::GetJpegModule()
109 {
110     return m_pCodecModule ? m_pCodecModule->GetJpegModule() : NULL;
111 }
GetJpxModule()112 ICodec_JpxModule* CPDF_ModuleMgr::GetJpxModule()
113 {
114     return m_pCodecModule ? m_pCodecModule->GetJpxModule() : NULL;
115 }
GetJbig2Module()116 ICodec_Jbig2Module* CPDF_ModuleMgr::GetJbig2Module()
117 {
118     return m_pCodecModule ? m_pCodecModule->GetJbig2Module() : NULL;
119 }
GetIccModule()120 ICodec_IccModule* CPDF_ModuleMgr::GetIccModule()
121 {
122     return m_pCodecModule ? m_pCodecModule->GetIccModule() : NULL;
123 }
GetFlateModule()124 ICodec_FlateModule* CPDF_ModuleMgr::GetFlateModule()
125 {
126     return m_pCodecModule ? m_pCodecModule->GetFlateModule() : NULL;
127 }
128