mmlibs/mmutilitylib/src/MmPluginUtils.cpp
changeset 0 b8ed18f6c07b
equal deleted inserted replaced
-1:000000000000 0:b8ed18f6c07b
       
     1 
       
     2 // MmPluginUtils.cpp
       
     3 
       
     4 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     5 // All rights reserved.
       
     6 // This component and the accompanying materials are made available
       
     7 // under the terms of "Eclipse Public License v1.0"
       
     8 // which accompanies this distribution, and is available
       
     9 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    10 //
       
    11 // Initial Contributors:
       
    12 // Nokia Corporation - initial contribution.
       
    13 //
       
    14 // Contributors:
       
    15 //
       
    16 // Description:
       
    17 //
       
    18 
       
    19 #include <mm/mmpluginutils.h>
       
    20 #include <mm/mmcleanup.h>
       
    21 
       
    22 //
       
    23 // MmPluginUtils
       
    24 //
       
    25 
       
    26 /**
       
    27 Find a plugin with the requested interfaceUid and whose match string matches
       
    28 the default data field. Where there are more than one plugin, choose that
       
    29 with highest version no.
       
    30 */
       
    31 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
    32 									                TInt32 aKeyOffset, 
       
    33 									                const TDesC8& aDefaultData,
       
    34 									                TUid aResolverUid)
       
    35 	{
       
    36 	RImplInfoPtrArray pluginArray;
       
    37 	CleanupResetAndDestroyPushL(pluginArray);
       
    38 
       
    39 	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData, aResolverUid);
       
    40 	
       
    41 	// Get higher version implementation
       
    42 	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
       
    43 	
       
    44 	CleanupStack::PopAndDestroy(&pluginArray);
       
    45 	return higherversionplugin;	
       
    46 	}
       
    47 
       
    48 /**
       
    49 Find a plugin with the requested interfaceUid and whose match string matches
       
    50 the default data field. Where there are more than one plugin, choose that
       
    51 with highest version no.
       
    52 */
       
    53 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
    54 									                TInt32 aKeyOffset, 
       
    55 									                const TDesC8& aDefaultData)
       
    56 	{
       
    57 	RImplInfoPtrArray pluginArray;
       
    58 	CleanupResetAndDestroyPushL(pluginArray);
       
    59 
       
    60 	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData);
       
    61 	
       
    62 	// Get higher version implementation
       
    63 	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
       
    64 
       
    65 	CleanupStack::PopAndDestroy(&pluginArray);
       
    66 	return higherversionplugin;	
       
    67 	}
       
    68 
       
    69 /**
       
    70 Find a plugin with the requested interfaceUid and whose match string matches
       
    71 the default data field. Where there are more than one plugin, choose that
       
    72 with highest version no.
       
    73 */
       
    74 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
    75 									                TInt32 aKeyOffset)
       
    76 	{
       
    77 	RImplInfoPtrArray pluginArray;
       
    78 	CleanupResetAndDestroyPushL(pluginArray);
       
    79 
       
    80 	FindImplementationsL(aInterfaceUid, pluginArray);
       
    81 	
       
    82 	// Get higher version implementation
       
    83 	TAny* higherversionplugin = GetHigherVersionImplementationKeyOffsetL(pluginArray,aKeyOffset);
       
    84 
       
    85 	CleanupStack::PopAndDestroy(&pluginArray);
       
    86 	return higherversionplugin;	
       
    87 	}
       
    88 	
       
    89 	
       
    90 /**
       
    91 Get the higher version of plugin passing keyoffset
       
    92 */
       
    93 TAny* MmPluginUtils::GetHigherVersionImplementationKeyOffsetL(RImplInfoPtrArray& pluginArray, TInt32 aKeyOffset)
       
    94 	{
       
    95 	TAny* self = NULL;
       
    96 	
       
    97 	if (pluginArray.Count() == 0)
       
    98 		{
       
    99 		User::Leave(KErrNotSupported);
       
   100 		}
       
   101 	else if (pluginArray.Count() == 1)
       
   102     	{
       
   103 		self = REComSession::CreateImplementationL(pluginArray[0]->ImplementationUid(), aKeyOffset);
       
   104     	}
       
   105 	else 
       
   106 		{
       
   107 		// create each in turn, starting with the highest version, which is the start element
       
   108 		for (TInt index = 0;  index < pluginArray.Count(); index++)
       
   109 			{
       
   110 			TInt err=KErrNone;
       
   111 			self = NULL;
       
   112 			TRAP(err, self = REComSession::CreateImplementationL(pluginArray[index]->ImplementationUid(), aKeyOffset))
       
   113 			if (err == KErrNone)
       
   114 		    	{
       
   115 		    	break;
       
   116 		    	}
       
   117 			if (err != KErrNotSupported)
       
   118 				{
       
   119 				User::Leave(err);
       
   120 				}
       
   121 			}
       
   122 		if (!self)
       
   123 			{
       
   124 			User::Leave(KErrNotSupported);
       
   125 			}
       
   126 		}
       
   127 	return self;		
       
   128 	}
       
   129 
       
   130 /*
       
   131 Similar but return destructor key via reference
       
   132 */
       
   133 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
   134 									                TUid& aDestructorKey,
       
   135 									                const TDesC8& aDefaultData,
       
   136 									                TUid aResolverUid)
       
   137 	{
       
   138 	RImplInfoPtrArray pluginArray;
       
   139 	CleanupResetAndDestroyPushL(pluginArray);
       
   140 	
       
   141 	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData, aResolverUid);
       
   142     
       
   143     // Get higher version implementation
       
   144     TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
       
   145 	
       
   146 	CleanupStack::PopAndDestroy(&pluginArray);
       
   147 	return higherversionplugin;	
       
   148 	}
       
   149 
       
   150 /*
       
   151 Similar but return destructor key via reference
       
   152 */
       
   153 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
   154 									                TUid& aDestructorKey,
       
   155 									                const TDesC8& aDefaultData)
       
   156 	{
       
   157 	RImplInfoPtrArray pluginArray;
       
   158 	CleanupResetAndDestroyPushL(pluginArray);
       
   159 	
       
   160 	FindImplementationsL(aInterfaceUid, pluginArray, aDefaultData);
       
   161 	
       
   162 	// Get higher version implementation
       
   163     TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
       
   164 
       
   165 	CleanupStack::PopAndDestroy(&pluginArray);
       
   166 	return higherversionplugin;	
       
   167 	}
       
   168 
       
   169 /*
       
   170 Similar but return destructor key via reference
       
   171 */
       
   172 EXPORT_C TAny* MmPluginUtils::CreateImplementationL(TUid aInterfaceUid, 
       
   173 									                TUid& aDestructorKey)
       
   174 	{
       
   175 	RImplInfoPtrArray pluginArray;
       
   176 	CleanupResetAndDestroyPushL(pluginArray);
       
   177 	
       
   178 	FindImplementationsL(aInterfaceUid, pluginArray);
       
   179 	
       
   180 	// Get higher version implementation
       
   181     TAny* higherversionplugin = GetHigherVersionImplementationaDestructorKeyL(pluginArray, aDestructorKey);
       
   182 
       
   183 	CleanupStack::PopAndDestroy(&pluginArray);
       
   184 	return higherversionplugin;	
       
   185 	}
       
   186 	
       
   187 /**
       
   188 Get the higher version of plugin passing destructorkey
       
   189 */
       
   190 TAny* MmPluginUtils::GetHigherVersionImplementationaDestructorKeyL(RImplInfoPtrArray& pluginArray, TUid& aDestructorKey)
       
   191 	{
       
   192 	TAny* self = NULL;
       
   193 	if (pluginArray.Count() == 0)
       
   194 		{
       
   195 		User::Leave(KErrNotSupported);
       
   196 		}
       
   197 	else if (pluginArray.Count() == 1)
       
   198     	{
       
   199 		self = REComSession::CreateImplementationL(pluginArray[0]->ImplementationUid(), aDestructorKey);
       
   200     	}
       
   201 	else 
       
   202 		{
       
   203 		// create each in turn, starting with the highest version, which is the start element
       
   204 		for (TInt index = 0;  index < pluginArray.Count(); index++)
       
   205 			{
       
   206 			TInt err = KErrNone;
       
   207 			self = NULL;
       
   208 			TRAP(err, self = REComSession::CreateImplementationL(pluginArray[index]->ImplementationUid(), aDestructorKey))
       
   209 		    if (err == KErrNone)
       
   210 		    	{
       
   211 		    	break;
       
   212 		    	}
       
   213 			if (err != KErrNotSupported)
       
   214 				{
       
   215 				User::Leave(err);
       
   216 				}
       
   217 			}
       
   218 		if (!self)
       
   219 			{
       
   220 			User::Leave(KErrNotSupported);
       
   221 			}
       
   222 		}
       
   223 	return self;		
       
   224 	}
       
   225 
       
   226 /*
       
   227 The function is an implementation of the templated TLinearOrder so it can order implementation information
       
   228 in an increasing version number.
       
   229 */
       
   230 TInt VersionLinearOrderFunction(const CImplementationInformation& pluginA, 
       
   231                                 const CImplementationInformation& pluginB)
       
   232 	{
       
   233 	if (pluginA.Version() == pluginB.Version())
       
   234 		{
       
   235 		return 0;
       
   236 		}
       
   237 		
       
   238 	return (pluginA.Version() < pluginB.Version())? 1: -1;	
       
   239 	}
       
   240 	
       
   241 EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
       
   242 						                RImplInfoPtrArray& aPluginArray)
       
   243 	{
       
   244 	REComSession::ListImplementationsL(aInterfaceUid, aPluginArray);
       
   245 	
       
   246 	CheckAndSortL(aPluginArray);
       
   247 	}
       
   248 
       
   249 EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
       
   250 						                RImplInfoPtrArray& aPluginArray, 
       
   251 				                        const TDesC8& aDefaultData,
       
   252 						                TUid aResolverUid)
       
   253 	{
       
   254 	TEComResolverParams resolverParams;
       
   255 	resolverParams.SetDataType(aDefaultData);
       
   256 	
       
   257 	FindImplementationsL(aInterfaceUid, aPluginArray, resolverParams, aResolverUid);
       
   258 	}
       
   259 
       
   260 EXPORT_C void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
       
   261 						                RImplInfoPtrArray& aPluginArray, 
       
   262 				                        const TDesC8& aDefaultData)
       
   263 	{
       
   264 	TEComResolverParams resolverParams;
       
   265 	resolverParams.SetDataType(aDefaultData);
       
   266 	
       
   267 	FindImplementationsL(aInterfaceUid, aPluginArray, resolverParams);
       
   268 	}
       
   269 
       
   270 void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
       
   271 						                RImplInfoPtrArray& aPluginArray, 
       
   272 				                        TEComResolverParams& aResolverParams)
       
   273 	{
       
   274 	REComSession::ListImplementationsL(aInterfaceUid, aResolverParams, aPluginArray);
       
   275 	
       
   276 	CheckAndSortL(aPluginArray);
       
   277 	}
       
   278 	
       
   279 void MmPluginUtils::FindImplementationsL(TUid aInterfaceUid, 
       
   280 						                RImplInfoPtrArray& aPluginArray, 
       
   281 				                        TEComResolverParams& aResolverParams,
       
   282 						                TUid aResolverUid)
       
   283 	{
       
   284 	REComSession::ListImplementationsL(aInterfaceUid, aResolverParams, aResolverUid, aPluginArray);
       
   285 	
       
   286 	CheckAndSortL(aPluginArray);
       
   287 	}
       
   288 	
       
   289 void MmPluginUtils::CheckAndSortL(RImplInfoPtrArray& aPluginArray)
       
   290 	{
       
   291 	// there must be at least one implementation
       
   292 	if (aPluginArray.Count() > 0)
       
   293 		{
       
   294 		if (aPluginArray.Count() > 1)
       
   295 			{
       
   296 			aPluginArray.Sort(VersionLinearOrderFunction);
       
   297 			}
       
   298 	
       
   299 		// if the highest version is zero there is no need to linger -- it should not be supported
       
   300 		// and we will leave
       
   301 		if (aPluginArray[0]->Version() == 0)
       
   302 			{
       
   303 			// plugins were found to be malformed, with 0 version numbers
       
   304 			User::Leave(KErrNotSupported);
       
   305 			}
       
   306 		}
       
   307 	}