1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
// =================================================================================================
// Copyright Adobe
// Copyright 2011 Adobe
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================
#include "Module.h"
#include "HostAPI.h"
#if EnablePluginManager
namespace XMP_PLUGIN
{
static bool CheckAPICompatibility_V1 ( const PluginAPIRef pluginAPIs )
{
// these plugin APIs are mandatory to run an XMP file handler
if ( pluginAPIs->mTerminatePluginProc
&& pluginAPIs->mSetHostAPIProc
&& pluginAPIs->mInitializeSessionProc
&& pluginAPIs->mTerminateSessionProc
&& pluginAPIs->mCheckFileFormatProc
&& pluginAPIs->mCheckFolderFormatProc
&& pluginAPIs->mGetFileModDateProc
&& pluginAPIs->mCacheFileDataProc
&& pluginAPIs->mUpdateFileProc
&& pluginAPIs->mWriteTempFileProc )
{
return true;
}
return false;
}
static bool CheckAPICompatibility_V2 ( const PluginAPIRef pluginAPIs )
{
if ( CheckAPICompatibility_V1 ( pluginAPIs ) )
{
if ( pluginAPIs->mFillMetadataFilesProc
&& pluginAPIs->mFillAssociatedResourcesProc )
{
return true;
}
}
return false;
}
static bool CheckAPICompatibility_V3 ( const PluginAPIRef pluginAPIs )
{
if ( CheckAPICompatibility_V2 ( pluginAPIs ) )
{
if ( pluginAPIs->mIsMetadataWritableProc )
{
return true;
}
}
return false;
}
static bool CheckAPICompatibility ( const PluginAPIRef pluginAPIs )
{
// Note: This is the place where we can reject old plugins.
// For example if we consider all functionality of
// plugin API version 2 mandatory we can reject
// plugin version 1 by returning false in case 1.
switch ( pluginAPIs->mVersion )
{
case 1:
return CheckAPICompatibility_V1 ( pluginAPIs );
break;
case 2:
return CheckAPICompatibility_V2 ( pluginAPIs );
break;
case 3:
return CheckAPICompatibility_V3 ( pluginAPIs );
break;
default:
// The loaded plugin is newer than the host.
// Only basic functionality to run the plugin is required.
return CheckAPICompatibility_V1 ( pluginAPIs );
break;
}
}
PluginAPIRef Module::getPluginAPIs()
{
//
// return ref. to Plugin API, load module if not yet loaded
//
if ( !mPluginAPIs || mLoaded != kModuleLoaded )
{
if ( !load() )
{
XMP_Throw ( "Plugin API not available.", kXMPErr_Unavailable );
}
}
return mPluginAPIs;
}
bool Module::load()
{
XMP_AutoLock lock ( &mLoadingLock, kXMP_WriteLock );
return loadInternal();
}
void Module::unload()
{
XMP_AutoLock lock (&mLoadingLock, kXMP_WriteLock);
unloadInternal();
}
void Module::unloadInternal()
{
WXMP_Error error;
//
// terminate plugin
//
if( mPluginAPIs != NULL )
{
if( mPluginAPIs->mTerminatePluginProc )
{
mPluginAPIs->mTerminatePluginProc( &error );
}
delete mPluginAPIs;
mPluginAPIs = NULL;
}
//
// unload plugin module
//
if( mLoaded != kModuleNotLoaded )
{
UnloadModule(mHandle, false);
mHandle = NULL;
if( mLoaded == kModuleLoaded )
{
//
// Reset mLoaded to kModuleNotLoaded, if the module was loaded successfully.
// Otherwise let it remain kModuleErrorOnLoad so that we won't try to load
// it again if some other handler ask to do so.
//
mLoaded = kModuleNotLoaded;
}
}
CheckError( error );
}
bool Module::loadInternal()
{
if( mLoaded == kModuleNotLoaded )
{
const char * errorMsg = NULL;
//
// load module
//
mLoaded = kModuleErrorOnLoad;
mHandle = LoadModule(mPath, false);
if( mHandle != NULL )
{
//
// get entry point function pointer
//
InitializePluginProc InitializePlugin = reinterpret_cast<InitializePluginProc>(
GetFunctionPointerFromModuleImpl(mHandle, "InitializePlugin") ); // legacy entry point
InitializePlugin2Proc InitializePlugin2 = reinterpret_cast<InitializePlugin2Proc>(
GetFunctionPointerFromModuleImpl(mHandle, "InitializePlugin2") );
if( InitializePlugin2 != NULL || InitializePlugin != NULL )
{
std::string moduleID;
GetResourceDataFromModule(mHandle, "MODULE_IDENTIFIER", "txt", moduleID);
mPluginAPIs = new PluginAPI();
memset( mPluginAPIs, 0, sizeof(PluginAPI) );
mPluginAPIs->mSize = sizeof(PluginAPI);
mPluginAPIs->mVersion = XMP_PLUGIN_VERSION; // informational: the latest version that the host knows about
WXMP_Error error;
//
// initialize plugin by calling entry point function
//
if( InitializePlugin2 != NULL )
{
HostAPIRef hostAPI = PluginManager::getHostAPI( XMP_HOST_API_VERSION );
InitializePlugin2( moduleID.c_str(), hostAPI, mPluginAPIs, &error );
if ( error.mErrorID == kXMPErr_NoError )
{
// check all function pointers are correct based on version numbers
if( CheckAPICompatibility( mPluginAPIs ) )
{
mLoaded = kModuleLoaded;
}
else
{
errorMsg = "Incompatible plugin API version.";
}
}
else
{
errorMsg = "Plugin initialization failed.";
}
}
else if( InitializePlugin != NULL )
{
// initialize through legacy plugin entry point
InitializePlugin( moduleID.c_str(), mPluginAPIs, &error );
if ( error.mErrorID == kXMPErr_NoError ) {
// check all function pointers are correct based on version numbers
bool compatibleAPIs = CheckAPICompatibility(mPluginAPIs);
if ( compatibleAPIs )
{
//
// set host API at plugin
//
HostAPIRef hostAPI = PluginManager::getHostAPI( mPluginAPIs->mVersion );
mPluginAPIs->mSetHostAPIProc( hostAPI, &error );
if( error.mErrorID == kXMPErr_NoError )
{
mLoaded = kModuleLoaded;
}
else
{
errorMsg = "Plugin API incomplete.";
}
}
else
{
errorMsg = "Incompatible plugin API version.";
}
}
else
{
errorMsg = "Plugin initialization failed.";
}
}
}
if( mLoaded != kModuleLoaded )
{
//
// plugin wasn't loaded and initialized successfully,
// so unload the module
//
this->unloadInternal();
}
}
else
{
errorMsg = "Can't load module";
}
if ( mLoaded != kModuleLoaded && errorMsg )
{
//
// error occurred
//
throw XMP_Error( kXMPErr_InternalFailure, errorMsg);
}
}
return ( mLoaded == kModuleLoaded );
}
} //namespace XMP_PLUGIN
#endif
|