• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = FX_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 = FX_NEW CPDF_PageModuleDef;
42     m_pRenderModule = FX_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 }
_GetPath(const CFX_ByteString & folder,FX_LPCSTR name)64 static CFX_ByteString _GetPath(const CFX_ByteString& folder, FX_LPCSTR name)
65 {
66     FX_STRSIZE folder_len = folder.GetLength();
67 #if _FX_OS_ == _FX_SYMBIAN_ || _FXM_PLATFORM_  == _FXM_PLATFORM_WINDOWS_
68     if (folder[folder_len - 1] == '\\') {
69         return folder + name;
70     } else {
71         return (folder + "\\") + name;
72     }
73 #else
74     if (folder[folder_len - 1] == '/') {
75         return folder + name;
76     } else {
77         return (folder + "/") + name;
78     }
79 #endif
80 }
SetModulePath(FX_LPCSTR module_name,FX_LPCSTR path)81 void CPDF_ModuleMgr::SetModulePath(FX_LPCSTR module_name, FX_LPCSTR path)
82 {
83     if (module_name == NULL || module_name[0] == 0) {
84         m_DefaultModulePath = path;
85     } else {
86         m_ModulePathList.SetAt(module_name, FX_NEW CFX_ByteString(path, -1));
87     }
88 }
GetModuleFilePath(FX_LPCSTR module_name,FX_LPCSTR name)89 CFX_ByteString CPDF_ModuleMgr::GetModuleFilePath(FX_LPCSTR module_name, FX_LPCSTR name)
90 {
91     CFX_ByteString* pPath = NULL;
92     if (m_ModulePathList.Lookup(module_name, (FX_LPVOID&)pPath)) {
93         return _GetPath(*pPath, name);
94     }
95     if (!m_DefaultModulePath.IsEmpty()) {
96         return _GetPath(m_DefaultModulePath, name);
97     }
98 #if _FXM_PLATFORM_  == _FXM_PLATFORM_WINDOWS_
99     FX_WCHAR app_path[260];
100     ::GetModuleFileNameW(NULL, (LPWSTR)app_path, 260);
101     FX_INTPTR len = FXSYS_wcslen(app_path);
102     for (FX_INTPTR i = len; i >= 0; i --)
103         if (app_path[i] == '\\') {
104             app_path[i] = 0;
105             break;
106         }
107     CFX_ByteString path = CFX_ByteString::FromUnicode(app_path);
108     path += '\\';
109     path += name;
110     return path;
111 #else
112     return name;
113 #endif
114 }
NotifyModuleAvailable(FX_LPCSTR module_name)115 void CPDF_ModuleMgr::NotifyModuleAvailable(FX_LPCSTR module_name)
116 {
117     if (FXSYS_strcmp(module_name, ADDIN_NAME_CJK) == 0) {
118         m_pPageModule->NotifyCJKAvailable();
119     } else if (FXSYS_strcmp(module_name, ADDIN_NAME_DECODER) == 0) {
120         m_pRenderModule->NotifyDecoderAvailable();
121     }
122 }
RegisterSecurityHandler(FX_LPCSTR filter,CPDF_SecurityHandler * (* CreateHandler)(void * param),void * param)123 void CPDF_ModuleMgr::RegisterSecurityHandler(FX_LPCSTR filter, CPDF_SecurityHandler * (*CreateHandler)(void* param), void* param)
124 {
125     if (CreateHandler == NULL) {
126         m_SecurityHandlerMap.RemoveKey(filter);
127     } else {
128         m_SecurityHandlerMap.SetAt(filter, (void*)CreateHandler);
129     }
130     if (param) {
131         m_SecurityHandlerMap.SetAt(FX_BSTRC("_param_") + filter, param);
132     }
133 }
SetPrivateData(FX_LPVOID module_id,FX_LPVOID pData,PD_CALLBACK_FREEDATA callback)134 void CPDF_ModuleMgr::SetPrivateData(FX_LPVOID module_id, FX_LPVOID pData, PD_CALLBACK_FREEDATA callback)
135 {
136     m_privateData.SetPrivateData(module_id, pData, callback);
137 }
GetPrivateData(FX_LPVOID module_id)138 FX_LPVOID CPDF_ModuleMgr::GetPrivateData(FX_LPVOID module_id)
139 {
140     return m_privateData.GetPrivateData(module_id);
141 }
CreateSecurityHandler(FX_LPCSTR filter)142 CPDF_SecurityHandler* CPDF_ModuleMgr::CreateSecurityHandler(FX_LPCSTR filter)
143 {
144     CPDF_SecurityHandler* (*CreateHandler)(void*) = NULL;
145     if (!m_SecurityHandlerMap.Lookup(filter, (void*&)CreateHandler)) {
146         return NULL;
147     }
148     if (CreateHandler == NULL) {
149         return NULL;
150     }
151     void* param = NULL;
152     m_SecurityHandlerMap.Lookup(FX_BSTRC("_param_") + filter, param);
153     return CreateHandler(param);
154 }
GetFaxModule()155 ICodec_FaxModule* CPDF_ModuleMgr::GetFaxModule()
156 {
157     return m_pCodecModule ? m_pCodecModule->GetFaxModule() : NULL;
158 }
GetJpegModule()159 ICodec_JpegModule* CPDF_ModuleMgr::GetJpegModule()
160 {
161     return m_pCodecModule ? m_pCodecModule->GetJpegModule() : NULL;
162 }
GetJpxModule()163 ICodec_JpxModule* CPDF_ModuleMgr::GetJpxModule()
164 {
165     return m_pCodecModule ? m_pCodecModule->GetJpxModule() : NULL;
166 }
GetJbig2Module()167 ICodec_Jbig2Module* CPDF_ModuleMgr::GetJbig2Module()
168 {
169     return m_pCodecModule ? m_pCodecModule->GetJbig2Module() : NULL;
170 }
GetIccModule()171 ICodec_IccModule* CPDF_ModuleMgr::GetIccModule()
172 {
173     return m_pCodecModule ? m_pCodecModule->GetIccModule() : NULL;
174 }
GetFlateModule()175 ICodec_FlateModule* CPDF_ModuleMgr::GetFlateModule()
176 {
177     return m_pCodecModule ? m_pCodecModule->GetFlateModule() : NULL;
178 }
179