contextframework/cfw/src/cfoperationpluginservices/cfoperationpluginmanager.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2008-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CFContextSourceManager class implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <ecom/ecom.h>
       
    21 #include <centralrepository.h>
       
    22 #include <bautils.h>
       
    23 
       
    24 #include "cfoperationpluginmanager.h"
       
    25 #include "ContextFrameworkPrivateCRKeys.h"
       
    26 #include "cfoperationplugin.h"
       
    27 #include "cfoperationservices.h"
       
    28 #include "cftrace.h"
       
    29 #include "CFScriptEngineInterface.h"
       
    30 
       
    31 // ============================= LOCAL FUNCTIONS ==============================
       
    32 
       
    33 /**
       
    34 * Clenup operation for RImplInfoPtrArray
       
    35 */
       
    36 LOCAL_C void CleanUpImplInfoArray( TAny* aParams )
       
    37     {
       
    38     RImplInfoPtrArray* array = static_cast<RImplInfoPtrArray*>( aParams );
       
    39     array->ResetAndDestroy();
       
    40     }
       
    41 
       
    42 /**
       
    43 * Push operations
       
    44 */
       
    45 LOCAL_C void CleanupResetAndDestroyPushL( RImplInfoPtrArray& aArray )
       
    46     {
       
    47     TCleanupItem item( CleanUpImplInfoArray, &aArray );
       
    48     CleanupStack::PushL( item );
       
    49     }
       
    50 
       
    51 /**
       
    52 * Operation plug-in information.
       
    53 */
       
    54 NONSHARABLE_CLASS( TCFOperationPluginInfo )
       
    55     {
       
    56     public:
       
    57 
       
    58         TCFOperationPluginInfo( CImplementationInformation& aImplInfo,
       
    59             CCFOperationPlugIn* aPlugIn ):
       
    60             iImplUid( aImplInfo.ImplementationUid() ),
       
    61             iImplVersion( aImplInfo.Version() ),
       
    62             iMissing( EFalse ),
       
    63             iPlugIn( aPlugIn )
       
    64             {
       
    65             }
       
    66 
       
    67     public:
       
    68 
       
    69         TUid iImplUid;
       
    70         TInt iImplVersion;
       
    71         TBool iMissing;
       
    72         CCFOperationPlugIn* iPlugIn;
       
    73     };
       
    74 
       
    75 // ============================= MEMBER FUNCTIONS =============================
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // CCFOperationPluginManager::NewL
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C CCFOperationPluginManager* CCFOperationPluginManager::NewL(
       
    82     MCFScriptEngineInterface& aScriptEngine )
       
    83     {
       
    84     FUNC_LOG;
       
    85 
       
    86     CCFOperationPluginManager* self =
       
    87         CCFOperationPluginManager::NewLC( aScriptEngine );
       
    88     CleanupStack::Pop( self );
       
    89 
       
    90     return self;
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CCFOperationPluginManager::NewLC
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CCFOperationPluginManager* CCFOperationPluginManager::NewLC(
       
    98     MCFScriptEngineInterface& aScriptEngine )
       
    99     {
       
   100     FUNC_LOG;
       
   101 
       
   102     CCFOperationPluginManager* self =
       
   103         new( ELeave ) CCFOperationPluginManager( aScriptEngine );
       
   104     CleanupStack::PushL( self );
       
   105     self->ConstructL();
       
   106 
       
   107     return self;
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CCFOperationPluginManager::~CCFOperationPluginManager
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C CCFOperationPluginManager::~CCFOperationPluginManager()
       
   115     {
       
   116     FUNC_LOG;
       
   117 
       
   118     // Delete plug-ins
       
   119     TInt count = iPlugIns.Count();
       
   120     for( TInt i = 0; i < count; i++ )
       
   121         {
       
   122         delete iPlugIns[i].iPlugIn;
       
   123         }
       
   124     iPlugIns.Close();
       
   125     }
       
   126 
       
   127 // ---------------------------------------------------------------------------
       
   128 // CCFOperationPluginManager::CCFOperationPluginManager
       
   129 // ---------------------------------------------------------------------------
       
   130 //
       
   131 CCFOperationPluginManager::CCFOperationPluginManager(
       
   132     MCFScriptEngineInterface& aScriptEngine ):
       
   133     iScriptEngine( aScriptEngine )
       
   134     {
       
   135     FUNC_LOG;
       
   136 
       
   137     // Nothing to do
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CCFOperationPluginManager::ConstructL
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CCFOperationPluginManager::ConstructL()
       
   145     {
       
   146     FUNC_LOG;
       
   147 
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CCFOperationPluginManager::InitializePhaseL
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CCFOperationPluginManager::InitializePhaseL(
       
   155     CCFPhaseBase::TCFPhaseId aPhase )
       
   156     {
       
   157     FUNC_LOG;
       
   158 
       
   159     switch( aPhase )
       
   160         {
       
   161         case CCFPhaseBase::ECFDeviceStarting:
       
   162             {
       
   163             InitDeviceStartingPhaseL();
       
   164             break;
       
   165             }
       
   166         case CCFPhaseBase::ECFLoadingPlugins:
       
   167             {
       
   168             InitLoadingPluginsPhaseL();
       
   169             break;
       
   170             }
       
   171         default:
       
   172             {
       
   173             break;
       
   174             }
       
   175         }
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // CCFOperationPluginManager::SetEventHandler
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CCFOperationPluginManager::SetEventHandler( MCFStarterEventHandler& /*aEventHandler*/ )
       
   183     {
       
   184     FUNC_LOG;	  
       
   185     }
       
   186 
       
   187 // ---------------------------------------------------------------------------
       
   188 // CCFOperationPluginManager::CreateAndInitializePlugInL
       
   189 // ---------------------------------------------------------------------------
       
   190 //
       
   191 CCFOperationPlugIn* CCFOperationPluginManager::CreateAndInitializePlugInL(
       
   192     const TUid& aImplementationUid, TOperationPluginInitParams& aParams )
       
   193     {
       
   194     FUNC_LOG;
       
   195 
       
   196     // Create plug-in
       
   197     CCFOperationPlugIn* plugIn = CCFOperationPlugIn::NewL(
       
   198         aImplementationUid,
       
   199         &aParams );
       
   200     CleanupStack::PushL( plugIn );
       
   201 
       
   202     // Cleanup
       
   203     CleanupStack::Pop( plugIn );
       
   204 
       
   205     return plugIn;
       
   206     }
       
   207 
       
   208 // ---------------------------------------------------------------------------
       
   209 // CCFOperationPluginManager::InitDeviceStartingPhaseL
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 void CCFOperationPluginManager::InitDeviceStartingPhaseL()
       
   213     {
       
   214     FUNC_LOG;
       
   215 
       
   216     INFO( "Checking plug-ins in [Init Device Starting] phase." );
       
   217     UpdatePlugInsL();
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CCFOperationPluginManager::InitLoadingPluginsPhaseL
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void CCFOperationPluginManager::InitLoadingPluginsPhaseL()
       
   225     {
       
   226     FUNC_LOG;
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CCFOperationPluginManager::LoadPluginL
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 void CCFOperationPluginManager::LoadPluginL(
       
   234 	CImplementationInformation& aImplInfo )
       
   235     {
       
   236     FUNC_LOG;
       
   237 
       
   238     INFO_2( "Loading operation plug-in with UID [%x], version [%d]",
       
   239     		aImplInfo.ImplementationUid().iUid,	aImplInfo.Version() );
       
   240 
       
   241     HEAP_2( "Before loading operation plug-in with UID [%x], version [%d]",
       
   242             aImplInfo.ImplementationUid().iUid, aImplInfo.Version() );
       
   243 
       
   244     TOperationPluginInitParams initParams;
       
   245     CCFOperationPlugIn* plugIn = NULL;
       
   246     TRAPD( err, plugIn = CreateAndInitializePlugInL(
       
   247     		aImplInfo.ImplementationUid(),
       
   248     		initParams ) );
       
   249     if( err == KErrNone )
       
   250         {
       
   251         // Plug-in loaded succesfully, store it
       
   252         TCFOperationPluginInfo info( aImplInfo, plugIn );
       
   253         TInt err = iPlugIns.Append( info );
       
   254         if( err != KErrNone )
       
   255             {
       
   256             ERROR_1( err, "Operation plug-in: [%x] could not be appended in array",
       
   257             		aImplInfo.ImplementationUid().iUid );
       
   258             delete plugIn;
       
   259             plugIn = NULL;
       
   260             }
       
   261         else
       
   262             {
       
   263             INFO_1( "Operation plug-in: [%x] succesfully loaded",
       
   264             		aImplInfo.ImplementationUid().iUid );
       
   265             }
       
   266         }
       
   267     else
       
   268         {
       
   269         ERROR_2( err, "Operation plug-in [%x] load error: [%d]",
       
   270         		aImplInfo.ImplementationUid().iUid, err );
       
   271 
       
   272         err = AddToBlackList( aImplInfo.ImplementationUid() );
       
   273         ERROR( err, "Failed to add UID to blacklist" );
       
   274         }
       
   275 
       
   276     HEAP_2( "After loading operation plug-in with UID [%x], version [%d]",
       
   277             aImplInfo.ImplementationUid().iUid, aImplInfo.Version() );
       
   278     }
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CCFOperationPluginManager::UpdatePlugInsL
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void CCFOperationPluginManager::UpdatePlugInsL()
       
   286 	{
       
   287     FUNC_LOG;
       
   288 
       
   289     // List all plugins
       
   290     RImplInfoPtrArray implInfoArray;
       
   291     CleanupResetAndDestroyPushL( implInfoArray );
       
   292     REComSession::ListImplementationsL( KOperationPlugInInterface,
       
   293         implInfoArray );
       
   294     RemoveBlackListed( implInfoArray );
       
   295 
       
   296     // Check if the update is install or uninstall based
       
   297     TInt allCount = implInfoArray.Count();
       
   298     TInt loadedCount = iPlugIns.Count();
       
   299     INFO_1( "Found %d operation plug-in implementations from ecom", allCount );
       
   300     INFO_1( "%d operation plug-ins currently loaded", loadedCount );
       
   301 
       
   302     // Check if there were plugins installed
       
   303     if( allCount >= loadedCount )
       
   304         {
       
   305         for( TInt i = iPlugIns.Count() - 1; i >= 0; i-- )
       
   306             {
       
   307             TCFOperationPluginInfo& pluginInfo = iPlugIns[i];
       
   308             for( TInt ii = 0; ii < implInfoArray.Count(); ii++ )
       
   309                 {
       
   310                 CImplementationInformation* implInfo = implInfoArray[ii];
       
   311                 if( implInfo->ImplementationUid() == pluginInfo.iImplUid )
       
   312                     {
       
   313                     TInt oldVer = pluginInfo.iImplVersion;
       
   314                     TInt newVer = implInfo->Version();
       
   315 
       
   316                     // Check if the version has increased
       
   317                     if( newVer > oldVer )
       
   318                         {
       
   319                         // Remove scripts which have a dependency to the provider
       
   320                         // which is being deleted
       
   321                         TInt err = iScriptEngine.RemoveScriptByProviderUid(
       
   322                             pluginInfo.iImplUid, ETrue );
       
   323                         INFO_2( "Remove script by provider [%x] completed with code [%d]",
       
   324                             pluginInfo.iImplUid.iUid, err );
       
   325 
       
   326                         // Delete old plugin
       
   327                         ReleasePlugIn( pluginInfo.iPlugIn );
       
   328                         iPlugIns.Remove( i );
       
   329 
       
   330                         // Clean up old implementation
       
   331                         REComSession::FinalClose();
       
   332 
       
   333                         // Load the new version
       
   334                         TRAP( err, LoadPluginL( *implInfo ) );
       
   335                         ERROR_3( err, "Upgrade of new version of operation plugin [%x], oldVer: [%d], newVer: [%d] failed",
       
   336                             implInfo->ImplementationUid().iUid, oldVer, newVer );
       
   337                         if( err == KErrNone )
       
   338                             {
       
   339                             INFO_3( "Upgraded new version of operation plugin [%x], oldVer: [%d], newVer: [%d]",
       
   340                                 implInfo->ImplementationUid().iUid, oldVer, newVer );
       
   341 
       
   342                             // Rollback removed scripts
       
   343                             iScriptEngine.RollbackScripts();
       
   344                             }
       
   345                         }
       
   346 
       
   347                     delete implInfo;
       
   348                     implInfoArray.Remove( ii );
       
   349                     break;
       
   350                     }
       
   351                 }
       
   352             }
       
   353 
       
   354         // Check if the installed plugin was not an updgrade but a new plugin
       
   355         for( TInt i = 0; i < implInfoArray.Count(); i++ )
       
   356             {
       
   357             CImplementationInformation* implInfo = implInfoArray[i];
       
   358             TRAPD( err, LoadPluginL( *implInfo ) );
       
   359             if( err == KErrNone )
       
   360                 {
       
   361                 INFO_2( "Loaded operation plugin [%x], ver: [%d]",
       
   362                     implInfo->ImplementationUid().iUid, implInfo->Version() );
       
   363                 }
       
   364             else
       
   365                 {
       
   366                 ERROR_2( err, "Failed to load operation plugin [%x], ver: [%d]",
       
   367                     implInfo->ImplementationUid().iUid, implInfo->Version() );
       
   368                 }
       
   369             }
       
   370         }
       
   371 
       
   372     // Check if there were plugins unistalled
       
   373     else
       
   374         {
       
   375         for( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   376             {
       
   377             TCFOperationPluginInfo& pluginInfo = iPlugIns[i];
       
   378             pluginInfo.iMissing = ETrue;
       
   379             }
       
   380 
       
   381         // Check that which plugins are missing
       
   382         for( TInt i = 0; i < implInfoArray.Count(); i++ )
       
   383             {
       
   384             CImplementationInformation* implInfo = implInfoArray[i];
       
   385             for( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   386                 {
       
   387                 TCFOperationPluginInfo& pluginInfo = iPlugIns[i];
       
   388                 if( pluginInfo.iImplUid == implInfo->ImplementationUid() )
       
   389                     {
       
   390                     pluginInfo.iMissing = EFalse;
       
   391                     break;
       
   392                     }
       
   393                 }
       
   394             }
       
   395 
       
   396         // Delete missing plugins
       
   397         for( TInt i = iPlugIns.Count() - 1; i >= 0; i-- )
       
   398             {
       
   399             TCFOperationPluginInfo& pluginInfo = iPlugIns[i];
       
   400             if( pluginInfo.iMissing )
       
   401                 {
       
   402                 // Remove scripts which have a dependency to the provider
       
   403                 // which is being deleted
       
   404                 TInt err = iScriptEngine.RemoveScriptByProviderUid(
       
   405                     pluginInfo.iImplUid, ETrue );
       
   406                 INFO_2( "Remove script by provider [%x] completed with code [%d]",
       
   407                     pluginInfo.iImplUid.iUid, err );
       
   408 
       
   409                 // Release plug-in
       
   410                 ReleasePlugIn( pluginInfo.iPlugIn );
       
   411                 INFO_2( "Removed uninstalled operation plugin [%x], ver: [%d]",
       
   412                     pluginInfo.iImplUid.iUid, pluginInfo.iImplVersion );
       
   413                 iPlugIns.Remove( i );
       
   414 
       
   415                 // Clean up old implementation
       
   416                 REComSession::FinalClose();
       
   417 
       
   418                 // Rollback removed scripts since we might have uninstalled
       
   419                 // plug-in which original version is in rom
       
   420                 iScriptEngine.RollbackScripts();
       
   421                 }
       
   422             }
       
   423         }
       
   424 
       
   425     // Cleanup
       
   426     CleanupStack::PopAndDestroy( &implInfoArray );
       
   427 	}
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // CCFOperationPluginManager::ParseScriptRootL
       
   431 // ---------------------------------------------------------------------------
       
   432 //
       
   433 CCFScriptRoot* CCFOperationPluginManager::ParseScriptRootL(
       
   434     CCFOperationNode* aParent,
       
   435     CMDXMLNode& aNode,
       
   436     MCFOperationServices& aOperationServices,
       
   437     TUid& aUid )
       
   438     {
       
   439     CCFScriptRoot* root = DoParseScriptRootL(
       
   440         aParent, aNode, aOperationServices, aUid );
       
   441     if ( root )
       
   442         {
       
   443         return root;
       
   444         }
       
   445 
       
   446     // Operation not found, check new plugins.
       
   447     UpdatePlugInsL();
       
   448 
       
   449     root = DoParseScriptRootL(
       
   450         aParent, aNode, aOperationServices, aUid );
       
   451     if ( root )
       
   452         {
       
   453         return root;
       
   454         }
       
   455 
       
   456     // Generate error info to log.
       
   457 
       
   458     return NULL;
       
   459     }
       
   460 
       
   461 // ---------------------------------------------------------------------------
       
   462 // CCFOperationPluginManager::ParseL
       
   463 // ---------------------------------------------------------------------------
       
   464 //
       
   465 CCFOperationNode* CCFOperationPluginManager::ParseL( CCFOperationNode* aParent,
       
   466     CMDXMLNode& aDOMNode,
       
   467     MCFOperationServices& aOperationServices,
       
   468     TUid& aUid )
       
   469     {
       
   470     CCFOperationNode* node = DoParseL(
       
   471         aParent, aDOMNode, aOperationServices, aUid );
       
   472 	if ( node )
       
   473 		{
       
   474 		return node;
       
   475 		}
       
   476 
       
   477     // Operation not found, check new plugins.
       
   478     UpdatePlugInsL();
       
   479 
       
   480     node = DoParseL(
       
   481         aParent, aDOMNode, aOperationServices, aUid );
       
   482     if ( node )
       
   483         {
       
   484         return node;
       
   485         }
       
   486 
       
   487     // Generate error info to log.
       
   488 
       
   489     return NULL;
       
   490     }
       
   491 
       
   492 //------------------------------------------------------------------------------
       
   493 // CCFOperationPluginManager::ReleasePlugIn
       
   494 //------------------------------------------------------------------------------
       
   495 //
       
   496 void CCFOperationPluginManager::ReleasePlugIn( CCFOperationPlugIn*& aPlugIn )
       
   497     {
       
   498     FUNC_LOG;
       
   499 
       
   500     delete aPlugIn;
       
   501     aPlugIn = NULL;
       
   502     }
       
   503 
       
   504 //------------------------------------------------------------------------------
       
   505 // CCFOperationPluginManager::DoParseL
       
   506 //------------------------------------------------------------------------------
       
   507 //
       
   508 CCFOperationNode* CCFOperationPluginManager::DoParseL( CCFOperationNode* aParent,
       
   509     CMDXMLNode& aDOMNode,
       
   510     MCFOperationServices& aOperationServices,
       
   511     TUid& aUid )
       
   512     {
       
   513     FUNC_LOG;
       
   514 
       
   515     CCFOperationNode* node = NULL;
       
   516     for ( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   517         {
       
   518         CCFOperationPlugIn* plugin = iPlugIns[ i ].iPlugIn;
       
   519         node = plugin->ParseL(
       
   520             aParent, aDOMNode, aOperationServices );
       
   521         if( node )
       
   522             {
       
   523             // Node found - break
       
   524             aUid = iPlugIns[i].iImplUid;
       
   525             break;
       
   526             }
       
   527         }
       
   528 
       
   529     return node;
       
   530     }
       
   531 
       
   532 //------------------------------------------------------------------------------
       
   533 // CCFOperationPluginManager::DoParseScriptRootL
       
   534 //------------------------------------------------------------------------------
       
   535 //
       
   536 CCFScriptRoot* CCFOperationPluginManager::DoParseScriptRootL(
       
   537     CCFOperationNode* aParent,
       
   538     CMDXMLNode& aDOMNode,
       
   539     MCFOperationServices& aOperationServices,
       
   540     TUid& aUid )
       
   541     {
       
   542     FUNC_LOG;
       
   543 
       
   544     CCFScriptRoot* root = NULL;
       
   545     for ( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   546         {
       
   547         CCFOperationPlugIn* plugin = iPlugIns[ i ].iPlugIn;
       
   548         root = plugin->ParseScriptRootL(
       
   549             aParent, aDOMNode, aOperationServices );
       
   550         if( root )
       
   551             {
       
   552             // Root found - break
       
   553             aUid = iPlugIns[i].iImplUid;
       
   554             break;
       
   555             }
       
   556         }
       
   557 
       
   558     return root;
       
   559     }
       
   560 
       
   561 // End of file