1 // =================================================================================================
2 // ADOBE SYSTEMS INCORPORATED
3 // Copyright 2011 Adobe Systems Incorporated
4 // All Rights Reserved
5 //
6 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
7 // of the Adobe license agreement accompanying it.
8 // =================================================================================================
9 
10 #ifndef PLUGINMANAGER_H
11 #define PLUGINMANAGER_H
12 #include "PluginHandler.h"
13 #include "ModuleUtils.h"
14 
15 #include "XMPCommon/XMPCommonDefines.h"
16 
17 
18 #if SUPPORT_SHARED_POINTERS_IN_STD
19 	#include <memory>
20 	#include <functional>
21 #elif SUPPORT_SHARED_POINTERS_IN_TR1
22 	#if XMP_WinBuild
23 		#include <memory>
24 		#include <functional>
25 	#else
26 		#include <tr1/memory>
27 		#include <tr1/functional>
28 	#endif
29 #else
30 	#error "location of shared pointer stuff is unknown"
31 #endif
32 
33 
34 namespace XMP_PLUGIN
35 {
36 
37 #if SUPPORT_SHARED_POINTERS_IN_STD
38 	using std::shared_ptr;
39 #elif SUPPORT_SHARED_POINTERS_IN_TR1
40 	using std::tr1::shared_ptr;
41 #endif
42 
43 typedef XMP_Uns32 XMPAtom;
44 typedef XMPAtom FileHandlerType;
45 
46 typedef shared_ptr<class Module>							ModuleSharedPtr;
47 typedef shared_ptr<class FileHandler>						FileHandlerSharedPtr;
48 
49 class FileHandlerInstance;
50 typedef FileHandlerInstance*								FileHandlerInstancePtr;
51 
52 typedef std::vector<std::string>							StringVec;
53 typedef std::vector<ModuleSharedPtr>						ModuleVec;
54 typedef std::vector<XMP_FileFormat>							XMP_FileFormatVec;
55 
56 struct FileHandlerPair;
57 
CheckError(WXMP_Error & error)58 inline void CheckError( WXMP_Error & error )
59 {
60 	if( error.mErrorID != kXMPErr_NoError )
61 	{
62 		if( error.mErrorID >= kXMPErr_PluginInternal &&
63 			error.mErrorID <= kXMPErr_PluginLastError )
64 		{
65 			throw XMP_Error( kXMPErr_InternalFailure, error.mErrorMsg );
66 		}
67 		else
68 		{
69 			throw XMP_Error( error.mErrorID, error.mErrorMsg );
70 		}
71 	}
72 }
73 
74 /** @class PluginManager
75  *  @brief Register all file handlers from all the plugins available in Plugin directory.
76  *
77  *  At the initialization time of XMPFiles, PluginManager loads all the available plugins
78  */
79 class PluginManager
80 {
81 public:
82 	enum HandlerPriority
83 	{
84 		kStandardHandler,
85 		kReplacementHandler,
86 		kUnknown
87 	};
88 
89 	/**
90 	 *  Initialize the plugin manager. It's a singleton class which manages the plugins.
91 	 *  @param pluginDir The directory where to search for the plugins.
92 	 *  @param plugins Comma separated list of the plugins which should be loaded from the plugin directory.
93 	 *  By default, all plug-ins available in the pluginDir will be loaded.
94 	 */
95 	static void initialize( const std::string& pluginDir, const std::string& plugins = std::string() );
96 
97 	/**
98 	 *  Terminate the plugin manager.
99 	 */
100 	static void terminate();
101 
102 	/**
103 	 *  Add file handler corresponding to the given format
104 	 *  @param format FileFormat supported by the file handler
105 	 *  @param handler shared pointer to the file handler which is to be added.
106 	 *  @return Void.
107 	 */
108 	static void addFileHandler( XMP_FileFormat format, FileHandlerSharedPtr handler );
109 
110 	/**
111 	 *  Returns file handler corresponding to the given format and priority
112 	 *
113 	 *  @param format	FileFormat supported by the file handler
114 	 *  @param priority	The priority of the handler (there could be one standard and
115 	 *					one replacing handler, use the enums kStandardHandler or kReplacementHandler)
116 	 *  @return shared pointer to the file handler. It does not need to be freed.
117 	 */
118 	static FileHandlerSharedPtr getFileHandler( XMP_FileFormat format, HandlerPriority priority = kStandardHandler );
119 
120 	/**
121 	 * Store mapping between session reference (comes from plugin) and
122 	 * FileHandlerInstance.
123 	 * @param session	Session reference from plugin
124 	 * @param handler	FileHandlerInstance related to the session
125 	 */
126 	static void addHandlerInstance( SessionRef session, FileHandlerInstancePtr handler );
127 
128 	/**
129 	 * Remove mapping between session reference (comes from plugin) and
130 	 * FilehandlerInstance
131 	 * @param session	Session reference from plugin
132 	 */
133 	static void removeHandlerInstance( SessionRef session );
134 
135 	/**
136 	 * Return FileHandlerInstance that is associated to the session reference
137 	 * @param session	Session reference from plugin
138 	 * @return FileHandlerInstance
139 	 */
140 	static FileHandlerInstancePtr getHandlerInstance( SessionRef session );
141 
142 	/**
143 	 * Return the priority of the handler
144 	 * @param handler	Instance of file handler
145 	 * @return			Return kStandardHandler or kReplacementHandler
146 	 */
147 	static HandlerPriority getHandlerPriority( FileHandlerInstancePtr handler );
148 
149 	/**
150 	 * Return Host API
151 	 */
152 	static HostAPIRef getHostAPI( XMP_Uns32 version );
153 
154 private:
155 	PluginManager( const std::string& pluginDir, const std::string& plugins );
156 	~PluginManager();
157 
158 	/**
159 	 * Terminate host API
160 	 */
161 	void initializeHostAPI();
162 	void terminateHostAPI();
163 
164 	/**
165 	 *  Load resource file of the given module.
166 	 *  @param module
167 	 *  @return Void.
168 	 */
169 	void loadResourceFile( ModuleSharedPtr module );
170 
171 	/**
172 	 *  Scan mPluginDir for the plugins. It also scans nested folder upto level inMaxNumOfNestedFolder.
173 	 *  @param inMaxNumOfNestedFolder Nested level where to scan.
174 	 *  @return Void.
175 	 */
176 	void doScan( const XMP_Int32 inMaxNumOfNestedFolder = 1 );
177 
178 	/**
179 	 *  Scan recursively the directory /a inPath and insert the found plug-in in ioFoundLibs.
180 	 *  @param inPath Full path of the directory name to be scanned.
181 	 *  @param ioFoundLibs Vector of string. Found plug-in will be inserted in this vector.
182 	 *  @param inLevel The current level
183 	 *  @param inMaxNumOfNestedFolder Nested level where to scan upto.
184 	 *  @return Void.
185 	 */
186 	void scanRecursive(
187 			const std::string& inPath,
188 			std::vector<std::string>& ioFoundLibs,
189 			XMP_Int32 inLevel,
190 			XMP_Int32 inMaxNestingLevel );
191 
192 	/**
193 	 * Setup passed in HostAPI structure for the host API v1
194 	 */
195 	static void SetupHostAPI_V1( HostAPIRef hostAPI );
196 
197 	/**
198 	 * Setup passed in HostAPI structure for the host API v2
199 	 */
200 	static void SetupHostAPI_V2( HostAPIRef hostAPI );
201 
202 	/**
203 	 * Setup passed in HostAPI structure for the host API v3
204 	 */
205 	static void SetupHostAPI_V3( HostAPIRef hostAPI );
206 
207 		/**
208 	 * Setup passed in HostAPI structure for the host API v4
209 	 */
210 	static void SetupHostAPI_V4( HostAPIRef hostAPI );
211 
212 
213 	typedef std::map<XMP_FileFormat, FileHandlerPair>		PluginHandlerMap;
214 	typedef std::map<XMP_Uns32, HostAPIRef>					HostAPIMap;
215 	typedef std::map<SessionRef, FileHandlerInstancePtr>	SessionMap;
216 
217 	std::string						mPluginDir;
218 	StringVec						mExtensions;
219 	StringVec						mPluginsNeeded;
220 	PluginHandlerMap				mHandlers;
221 	SessionMap						mSessions;
222 	HostAPIMap						mHostAPIs;
223 
224 	static PluginManager*			msPluginManager;
225 };
226 
227 } //namespace XMP_PLUGIN
228 #endif //PLUGINMANAGER_H
229