os/mm/mmlibs/mmutilitylib/src/MmPluginUtils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/mm/mmlibs/mmutilitylib/src/MmPluginUtils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,307 @@
     1.4 +
     1.5 +// MmPluginUtils.cpp
     1.6 +
     1.7 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.8 +// All rights reserved.
     1.9 +// This component and the accompanying materials are made available
    1.10 +// under the terms of "Eclipse Public License v1.0"
    1.11 +// which accompanies this distribution, and is available
    1.12 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.13 +//
    1.14 +// Initial Contributors:
    1.15 +// Nokia Corporation - initial contribution.
    1.16 +//
    1.17 +// Contributors:
    1.18 +//
    1.19 +// Description:
    1.20 +//
    1.21 +
    1.22 +#include <mm/mmpluginutils.h>
    1.23 +#include <mm/mmcleanup.h>
    1.24 +
    1.25 +//
    1.26 +// MmPluginUtils
    1.27 +//
    1.28 +
    1.29 +/**
    1.30 +Find a plugin with the requested interfaceUid and whose match string matches
    1.31 +the default data field. Where there are more than one plugin, choose that
    1.32 +with highest version no.
    1.33 +*/
    1.34 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
    1.35 +									                TInt32 aKeyOffset, 
    1.36 +									                const TDesC8& aDefaultData,
    1.37 +									                TUid aResolverUid)
    1.38 +	{
    1.39 +	RImplInfoPtrArray pluginArray;
    1.40 +	CleanupResetAndDestroyPushL(pluginArray);
    1.41 +
    1.42 +	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData, aResolverUid);
    1.43 +	
    1.44 +	// Get higher version implementation
    1.45 +	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
    1.46 +	
    1.47 +	CleanupStack::PopAndDestroy(&pluginArray);
    1.48 +	return higherversionplugin;	
    1.49 +	}
    1.50 +
    1.51 +/**
    1.52 +Find a plugin with the requested interfaceUid and whose match string matches
    1.53 +the default data field. Where there are more than one plugin, choose that
    1.54 +with highest version no.
    1.55 +*/
    1.56 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
    1.57 +									                TInt32 aKeyOffset, 
    1.58 +									                const TDesC8& aDefaultData)
    1.59 +	{
    1.60 +	RImplInfoPtrArray pluginArray;
    1.61 +	CleanupResetAndDestroyPushL(pluginArray);
    1.62 +
    1.63 +	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData);
    1.64 +	
    1.65 +	// Get higher version implementation
    1.66 +	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
    1.67 +
    1.68 +	CleanupStack::PopAndDestroy(&pluginArray);
    1.69 +	return higherversionplugin;	
    1.70 +	}
    1.71 +
    1.72 +/**
    1.73 +Find a plugin with the requested interfaceUid and whose match string matches
    1.74 +the default data field. Where there are more than one plugin, choose that
    1.75 +with highest version no.
    1.76 +*/
    1.77 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
    1.78 +									                TInt32 aKeyOffset)
    1.79 +	{
    1.80 +	RImplInfoPtrArray pluginArray;
    1.81 +	CleanupResetAndDestroyPushL(pluginArray);
    1.82 +
    1.83 +	FindImplementationsL(aInterfaceUid, pluginArray);
    1.84 +	
    1.85 +	// Get higher version implementation
    1.86 +	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
    1.87 +
    1.88 +	CleanupStack::PopAndDestroy(&pluginArray);
    1.89 +	return higherversionplugin;	
    1.90 +	}
    1.91 +	
    1.92 +	
    1.93 +/**
    1.94 +Get the higher version of plugin passing keyoffset
    1.95 +*/
    1.96 +TAny* MmPluginUtils::GetHigherVersionImplementationKeyOffsetL(RImplInfoPtrArray& pluginArray, TInt32 aKeyOffset)
    1.97 +	{
    1.98 +	TAny* self = NULL;
    1.99 +	
   1.100 +	if (pluginArray.Count() == 0)
   1.101 +		{
   1.102 +		User::Leave(KErrNotSupported);
   1.103 +		}
   1.104 +	else if (pluginArray.Count() == 1)
   1.105 +    	{
   1.106 +		self = REComSession::CreateImplementationL(pluginArray[0]->ImplementationUid(), aKeyOffset);
   1.107 +    	}
   1.108 +	else 
   1.109 +		{
   1.110 +		// create each in turn, starting with the highest version, which is the start element
   1.111 +		for (TInt index = 0;  index < pluginArray.Count(); index++)
   1.112 +			{
   1.113 +			TInt err=KErrNone;
   1.114 +			self = NULL;
   1.115 +			TRAP(err, self = REComSession::CreateImplementationL(pluginArray[index]->ImplementationUid(), aKeyOffset))
   1.116 +			if (err == KErrNone)
   1.117 +		    	{
   1.118 +		    	break;
   1.119 +		    	}
   1.120 +			if (err != KErrNotSupported)
   1.121 +				{
   1.122 +				User::Leave(err);
   1.123 +				}
   1.124 +			}
   1.125 +		if (!self)
   1.126 +			{
   1.127 +			User::Leave(KErrNotSupported);
   1.128 +			}
   1.129 +		}
   1.130 +	return self;		
   1.131 +	}
   1.132 +
   1.133 +/*
   1.134 +Similar but return destructor key via reference
   1.135 +*/
   1.136 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
   1.137 +									                TUid& aDestructorKey,
   1.138 +									                const TDesC8& aDefaultData,
   1.139 +									                TUid aResolverUid)
   1.140 +	{
   1.141 +	RImplInfoPtrArray pluginArray;
   1.142 +	CleanupResetAndDestroyPushL(pluginArray);
   1.143 +	
   1.144 +	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData, aResolverUid);
   1.145 +    
   1.146 +    // Get higher version implementation
   1.147 +    TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
   1.148 +	
   1.149 +	CleanupStack::PopAndDestroy(&pluginArray);
   1.150 +	return higherversionplugin;	
   1.151 +	}
   1.152 +
   1.153 +/*
   1.154 +Similar but return destructor key via reference
   1.155 +*/
   1.156 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
   1.157 +									                TUid& aDestructorKey,
   1.158 +									                const TDesC8& aDefaultData)
   1.159 +	{
   1.160 +	RImplInfoPtrArray pluginArray;
   1.161 +	CleanupResetAndDestroyPushL(pluginArray);
   1.162 +	
   1.163 +	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData);
   1.164 +	
   1.165 +	// Get higher version implementation
   1.166 +    TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
   1.167 +
   1.168 +	CleanupStack::PopAndDestroy(&pluginArray);
   1.169 +	return higherversionplugin;	
   1.170 +	}
   1.171 +
   1.172 +/*
   1.173 +Similar but return destructor key via reference
   1.174 +*/
   1.175 +EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
   1.176 +									                TUid& aDestructorKey)
   1.177 +	{
   1.178 +	RImplInfoPtrArray pluginArray;
   1.179 +	CleanupResetAndDestroyPushL(pluginArray);
   1.180 +	
   1.181 +	FindImplementationsL(aInterfaceUid, pluginArray);
   1.182 +	
   1.183 +	// Get higher version implementation
   1.184 +    TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
   1.185 +
   1.186 +	CleanupStack::PopAndDestroy(&pluginArray);
   1.187 +	return higherversionplugin;	
   1.188 +	}
   1.189 +	
   1.190 +/**
   1.191 +Get the higher version of plugin passing destructorkey
   1.192 +*/
   1.193 +TAny* MmPluginUtils::GetHigherVersionImplementationaDestructorKeyL(RImplInfoPtrArray& pluginArray, TUid& aDestructorKey)
   1.194 +	{
   1.195 +	TAny* self = NULL;
   1.196 +	if (pluginArray.Count() == 0)
   1.197 +		{
   1.198 +		User::Leave(KErrNotSupported);
   1.199 +		}
   1.200 +	else if (pluginArray.Count() == 1)
   1.201 +    	{
   1.202 +		self = REComSession::CreateImplementationL(pluginArray[0]->ImplementationUid(), aDestructorKey);
   1.203 +    	}
   1.204 +	else 
   1.205 +		{
   1.206 +		// create each in turn, starting with the highest version, which is the start element
   1.207 +		for (TInt index = 0;  index < pluginArray.Count(); index++)
   1.208 +			{
   1.209 +			TInt err = KErrNone;
   1.210 +			self = NULL;
   1.211 +			TRAP(err, self = REComSession::CreateImplementationL(pluginArray[index]->ImplementationUid(), aDestructorKey))
   1.212 +		    if (err == KErrNone)
   1.213 +		    	{
   1.214 +		    	break;
   1.215 +		    	}
   1.216 +			if (err != KErrNotSupported)
   1.217 +				{
   1.218 +				User::Leave(err);
   1.219 +				}
   1.220 +			}
   1.221 +		if (!self)
   1.222 +			{
   1.223 +			User::Leave(KErrNotSupported);
   1.224 +			}
   1.225 +		}
   1.226 +	return self;		
   1.227 +	}
   1.228 +
   1.229 +/*
   1.230 +The function is an implementation of the templated TLinearOrder so it can order implementation information
   1.231 +in an increasing version number.
   1.232 +*/
   1.233 +TInt VersionLinearOrderFunction(const CImplementationInformation& pluginA, 
   1.234 +                                const CImplementationInformation& pluginB)
   1.235 +	{
   1.236 +	if (pluginA.Version() == pluginB.Version())
   1.237 +		{
   1.238 +		return 0;
   1.239 +		}
   1.240 +		
   1.241 +	return (pluginA.Version() < pluginB.Version())? 1: -1;	
   1.242 +	}
   1.243 +	
   1.244 +EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
   1.245 +						                RImplInfoPtrArray& aPluginArray)
   1.246 +	{
   1.247 +	REComSession::ListImplementationsL(aInterfaceUid, aPluginArray);
   1.248 +	
   1.249 +	CheckAndSortL(aPluginArray);
   1.250 +	}
   1.251 +
   1.252 +EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
   1.253 +						                RImplInfoPtrArray& aPluginArray, 
   1.254 +				                        const TDesC8& aDefaultData,
   1.255 +						                TUid aResolverUid)
   1.256 +	{
   1.257 +	TEComResolverParams resolverParams;
   1.258 +	resolverParams.SetDataType(aDefaultData);
   1.259 +	
   1.260 +	FindImplementationsL(aInterfaceUid, aPluginArray, resolverParams, aResolverUid);
   1.261 +	}
   1.262 +
   1.263 +EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
   1.264 +						                RImplInfoPtrArray& aPluginArray, 
   1.265 +				                        const TDesC8& aDefaultData)
   1.266 +	{
   1.267 +	TEComResolverParams resolverParams;
   1.268 +	resolverParams.SetDataType(aDefaultData);
   1.269 +	
   1.270 +	FindImplementationsL(aInterfaceUid, aPluginArray, resolverParams);
   1.271 +	}
   1.272 +
   1.273 +void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
   1.274 +						                RImplInfoPtrArray& aPluginArray, 
   1.275 +				                        TEComResolverParams& aResolverParams)
   1.276 +	{
   1.277 +	REComSession::ListImplementationsL(aInterfaceUid, aResolverParams, aPluginArray);
   1.278 +	
   1.279 +	CheckAndSortL(aPluginArray);
   1.280 +	}
   1.281 +	
   1.282 +void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
   1.283 +						                RImplInfoPtrArray& aPluginArray, 
   1.284 +				                        TEComResolverParams& aResolverParams,
   1.285 +						                TUid aResolverUid)
   1.286 +	{
   1.287 +	REComSession::ListImplementationsL(aInterfaceUid, aResolverParams, aResolverUid, aPluginArray);
   1.288 +	
   1.289 +	CheckAndSortL(aPluginArray);
   1.290 +	}
   1.291 +	
   1.292 +void MmPluginUtils::CheckAndSortL(RImplInfoPtrArray& aPluginArray)
   1.293 +	{
   1.294 +	// there must be at least one implementation
   1.295 +	if (aPluginArray.Count() > 0)
   1.296 +		{
   1.297 +		if (aPluginArray.Count() > 1)
   1.298 +			{
   1.299 +			aPluginArray.Sort(VersionLinearOrderFunction);
   1.300 +			}
   1.301 +	
   1.302 +		// if the highest version is zero there is no need to linger -- it should not be supported
   1.303 +		// and we will leave
   1.304 +		if (aPluginArray[0]->Version() == 0)
   1.305 +			{
   1.306 +			// plugins were found to be malformed, with 0 version numbers
   1.307 +			User::Leave(KErrNotSupported);
   1.308 +			}
   1.309 +		}
   1.310 +	}