contextframework/cfw/src/cfcontextsourcemanager/CFContextSourceManager.cpp
changeset 0 2e3d3ce01487
child 24 a72ff4214918
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2006-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 #include <cfcontextsourceupgrade.h>
       
    24 #include <cfcontextsourceinterface.h>
       
    25 #include <cfcontextsourcecommand.h>
       
    26 #include <cfcontextsubscriptionlistener.h>
       
    27 
       
    28 #include "CFContextSourceManager.h"
       
    29 #include "cfcontextsourcesettingsmanager.h"
       
    30 #include "ContextFrameworkPrivateCRKeys.h"
       
    31 #include "cfcontextsourceplugin.h"
       
    32 #include "cfextendedcontextinterface.h"
       
    33 #include "cfcontextsourcesettingarray.h"
       
    34 #include "cftrace.h"
       
    35 
       
    36 // CONSTANTS
       
    37 
       
    38 const TInt KUIDNameStartPos = 1;
       
    39 const TInt KUIDNameEndPos = 8;
       
    40 
       
    41 // Drive letter hard coded since context source plug-in settings
       
    42 // can only exist in ROM or RAM
       
    43 _LIT( KSettingsPathRom, "Z:\\Private\\10282BC4\\Settings\\" );
       
    44 _LIT( KSettingsPathRam, "C:\\Private\\10282BC4\\Settings\\" );
       
    45 
       
    46 // Folder extension
       
    47 _LIT( KFolderExt, "\\" );
       
    48 
       
    49 // Setting file extension
       
    50 _LIT( KSettingExtension, "*.xml" );
       
    51 
       
    52 // Separates SID from filename for client installed settings
       
    53 _LIT( KUnderscore, "_" );
       
    54 
       
    55 
       
    56 /**
       
    57  * Information about a client acting as a context source.
       
    58  * This info is used to keep track of clients acting as context sources to
       
    59  * enable sending them context source commands.
       
    60  *
       
    61  * @lib CFContextSourceManager
       
    62  * @since S60 5.0
       
    63  */
       
    64 NONSHARABLE_CLASS( TCFClientContextSourceInfo )
       
    65 {
       
    66 public: // Constructors
       
    67 
       
    68     TCFClientContextSourceInfo() : iSource( NULL ), iClientSid( KNullUid )
       
    69         {
       
    70         FUNC_LOG;
       
    71         }
       
    72 
       
    73     TCFClientContextSourceInfo( MCFContextSource* aSource, const TUid& aSid )
       
    74         :   iSource( aSource ), iClientSid( aSid )
       
    75         {
       
    76         FUNC_LOG;
       
    77         }
       
    78 
       
    79 public: // Data
       
    80 
       
    81     /**
       
    82     * Context source interface pointer; not owned.
       
    83     */
       
    84     MCFContextSource* iSource;
       
    85 
       
    86     /**
       
    87     * Client's secure id (from client thread).
       
    88     */
       
    89     TUid iClientSid;
       
    90 };
       
    91 
       
    92 // ============================= LOCAL FUNCTIONS ==============================
       
    93 
       
    94 /**
       
    95 * Clenup operation for RImplInfoPtrArray
       
    96 */
       
    97 LOCAL_C void CleanUpImplInfoArray( TAny* aParams )
       
    98     {
       
    99     RImplInfoPtrArray* array = static_cast<RImplInfoPtrArray*>( aParams );
       
   100     array->ResetAndDestroy();
       
   101     }
       
   102 
       
   103 /**
       
   104 * Push operations
       
   105 */
       
   106 LOCAL_C void CleanupResetAndDestroyPushL( RImplInfoPtrArray& aArray )
       
   107     {
       
   108     TCleanupItem item( CleanUpImplInfoArray, &aArray );
       
   109     CleanupStack::PushL( item );
       
   110     }
       
   111 
       
   112 /**
       
   113 * Orders client countext source infos based on their uids.
       
   114 * @param aFirst First context source info.
       
   115 * @param aSecond Second context source info.
       
   116 * @return Zero if the uid of the first and second are equal. Negative value
       
   117 *   if the uid of the first is less than the uid of the second. Positive
       
   118 *   value if the uid of the first is greater than the uid of the second.
       
   119 */
       
   120 LOCAL_C TInt ClientContextSourceInfoUidOrder(
       
   121     const TCFClientContextSourceInfo& aFirst,
       
   122     const TCFClientContextSourceInfo& aSecond )
       
   123     {
       
   124     TInt orderValue = 1;
       
   125 
       
   126     if ( aFirst.iClientSid == aSecond.iClientSid )
       
   127         {
       
   128         orderValue = 0;
       
   129         }
       
   130     else if ( aFirst.iClientSid.iUid < aSecond.iClientSid.iUid )
       
   131         {
       
   132         orderValue = -1;
       
   133         }
       
   134 
       
   135     return orderValue;
       
   136     }
       
   137 
       
   138 /**
       
   139 * Context loader information.
       
   140 */
       
   141 NONSHARABLE_CLASS( TCFLoaderInfo )
       
   142     {
       
   143     public:
       
   144     
       
   145     TCFLoaderInfo( CImplementationInformation& aImplementationInfo,
       
   146                    CCFContextSourceManager& aManager ):
       
   147                    iImplUid( aImplementationInfo.ImplementationUid() ),
       
   148                    iImplVersion( aImplementationInfo.Version() ),
       
   149                    iManager( aManager )
       
   150         {
       
   151         }
       
   152             
       
   153     public:
       
   154         TUid iImplUid;
       
   155         TInt iImplVersion;
       
   156         CCFContextSourceManager& iManager;
       
   157     };
       
   158 
       
   159 /**
       
   160 * Context source information.
       
   161 */
       
   162 NONSHARABLE_CLASS( TCFContextSourceInfo )
       
   163     {
       
   164     public:
       
   165 
       
   166         TCFContextSourceInfo( CImplementationInformation& aImplInfo,
       
   167             CCFContextSourcePlugIn* aPlugIn ):
       
   168             iImplUid( aImplInfo.ImplementationUid() ), iImplVersion( aImplInfo.Version() ),
       
   169             iPlugIn( aPlugIn ), iMissing( ETrue )
       
   170             {
       
   171             }
       
   172     
       
   173     TCFContextSourceInfo( TUid aImplUid,
       
   174                           TInt aImplVersion,
       
   175                           CCFContextSourcePlugIn* aPlugIn ):
       
   176                           iImplUid( aImplUid ),
       
   177                           iImplVersion( aImplVersion ),
       
   178                           iPlugIn( aPlugIn ),
       
   179                           iMissing( ETrue )
       
   180         {
       
   181         }
       
   182 
       
   183     public:
       
   184 
       
   185         TUid iImplUid;
       
   186         TInt iImplVersion;
       
   187         CCFContextSourcePlugIn* iPlugIn;
       
   188         TBool iMissing;
       
   189     };
       
   190 
       
   191 // ============================= MEMBER FUNCTIONS =============================
       
   192 
       
   193 EXPORT_C CCFContextSourceManager* CCFContextSourceManager::NewL(
       
   194     MCFExtendedContextInterface& aCF,
       
   195     RFs& aFs )
       
   196     {
       
   197     FUNC_LOG;
       
   198 
       
   199     CCFContextSourceManager* self =
       
   200         CCFContextSourceManager::NewLC( aCF, aFs );
       
   201     CleanupStack::Pop( self );
       
   202 
       
   203     return self;
       
   204     }
       
   205 
       
   206 EXPORT_C CCFContextSourceManager* CCFContextSourceManager::NewLC(
       
   207     MCFExtendedContextInterface& aCF,
       
   208     RFs& aFs )
       
   209     {
       
   210     FUNC_LOG;
       
   211 
       
   212     CCFContextSourceManager* self =
       
   213         new( ELeave ) CCFContextSourceManager( aCF, aFs );
       
   214     CleanupStack::PushL( self );
       
   215     self->ConstructL();
       
   216 
       
   217     return self;
       
   218     }
       
   219 
       
   220 EXPORT_C CCFContextSourceManager::~CCFContextSourceManager()
       
   221     {
       
   222     FUNC_LOG;
       
   223 
       
   224     iClientContextSources.Close();
       
   225 
       
   226     // Delete plug-ins
       
   227     TInt count = iPlugIns.Count();
       
   228     for( TInt i = 0; i < count; i++ )
       
   229         {
       
   230         CCFContextSourcePlugIn* plugIn = iPlugIns[i].iPlugIn;
       
   231         ReleasePlugIn( plugIn );
       
   232         }
       
   233     iPlugIns.Close();
       
   234     delete iSettingsManager;
       
   235     
       
   236     iLoaders.ResetAndDestroy();
       
   237     }
       
   238 
       
   239 CCFContextSourceManager::CCFContextSourceManager(
       
   240     MCFExtendedContextInterface& aCF,
       
   241     RFs& aFs ):
       
   242     iCF( aCF ),
       
   243     iFs( aFs )
       
   244     {
       
   245     FUNC_LOG;
       
   246 
       
   247     // Nothing to do
       
   248     }
       
   249 
       
   250 void CCFContextSourceManager::ConstructL()
       
   251     {
       
   252     FUNC_LOG;
       
   253 
       
   254     iSettingsManager = CCFContextSourceSettingsManager::NewL( iFs );
       
   255     }
       
   256 
       
   257 void CCFContextSourceManager::SetEventHandler( MCFStarterEventHandler& aEventHandler )
       
   258     {
       
   259     FUNC_LOG;
       
   260 	  iEventHandler = &aEventHandler;
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CCFContextSourceManager::InstallSettingL
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 EXPORT_C void CCFContextSourceManager::InstallSettingL( RFile& aSettingFile,
       
   268     const TUid& aContextSourceUid,
       
   269     RThread& aClientThread )
       
   270     {
       
   271     FUNC_LOG;
       
   272 
       
   273     // Get plugin.
       
   274     MCFContextSourceUpgrade* sourceUpgrade = PlugInL( aContextSourceUid );
       
   275 
       
   276     // Parse setting file.
       
   277     CCFContextSourceSettingArray* settings
       
   278             = CCFContextSourceSettingArray::NewLC();    // CLEANUP<< settings
       
   279     RFile parseFile;
       
   280     User::LeaveIfError( parseFile.Duplicate( aSettingFile ) );
       
   281     // parseFile handle closed by the parser.
       
   282     iSettingsManager->ParseSettingsL( parseFile, *settings );
       
   283 
       
   284     // Check setting validity from plugin.
       
   285     TInt err = sourceUpgrade->CheckValidity( aClientThread, *settings );
       
   286     CleanupStack::PopAndDestroy( settings );            // CLEANUP>> settings
       
   287     ERROR( err, "CCFContextSourceManager::InstallSettingL - Context source CheckSecurity for new settings failed" );
       
   288     User::LeaveIfError( err );
       
   289 
       
   290     // Copy the setting file.
       
   291     CopySettingFileL( aSettingFile, aContextSourceUid, aClientThread );
       
   292 
       
   293     // Read all settings and give them to the plugin.
       
   294     CCFContextSourceSettingArray* settingArray
       
   295             = PlugInSettingsL( aContextSourceUid );
       
   296     // Ownership of settingArray is transferred to plugin.
       
   297     sourceUpgrade->UpdateSettingsL( settingArray );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // CCFContextSourceManager::UninstallSettingL
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 EXPORT_C void CCFContextSourceManager::UninstallSettingL(
       
   305     const TDesC& aSettingFilename,
       
   306     const TUid& aContextSourceUid,
       
   307     RThread& aClientThread )
       
   308     {
       
   309     FUNC_LOG;
       
   310 
       
   311     // Get plugin.
       
   312     MCFContextSourceUpgrade* sourceUpgrade = PlugInL( aContextSourceUid );
       
   313 
       
   314     // Delete the setting file.
       
   315     DeleteSettingFileL( aSettingFilename, aContextSourceUid, aClientThread );
       
   316 
       
   317     // Read all settings and give them to the plugin.
       
   318     CCFContextSourceSettingArray* settingArray
       
   319             = PlugInSettingsL( aContextSourceUid );
       
   320     // Ownership of settingArray is transferred to plugin.
       
   321     sourceUpgrade->UpdateSettingsL( settingArray );
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // CCFContextSourceManager::UninstallSettingsL
       
   326 // ---------------------------------------------------------------------------
       
   327 //
       
   328 EXPORT_C void CCFContextSourceManager::UninstallSettingsL(
       
   329     const TUid& aContextSourceUid,
       
   330     RThread& aClientThread )
       
   331     {
       
   332     FUNC_LOG;
       
   333 
       
   334     // Get plugin.
       
   335     MCFContextSourceUpgrade* sourceUpgrade = PlugInL( aContextSourceUid );
       
   336 
       
   337     // Delete all setting files installed by the client.
       
   338     DeleteSettingFileL( KSettingExtension, aContextSourceUid, aClientThread );
       
   339 
       
   340     // Read all settings and give them to the plugin.
       
   341     CCFContextSourceSettingArray* settingArray
       
   342             = PlugInSettingsL( aContextSourceUid );
       
   343     // Ownership of settingArray is transferred to plugin.
       
   344     sourceUpgrade->UpdateSettingsL( settingArray );
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // CCFContextSourceManager::HandleContextSourceCommandL
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 EXPORT_C void CCFContextSourceManager::HandleContextSourceCommandL(
       
   352     const CCFContextSourceCommand& aCommand )
       
   353     {
       
   354     FUNC_LOG;
       
   355 
       
   356     const TUid sourceUid = aCommand.SourceUid();
       
   357     // Find plugin.
       
   358     CCFContextSourcePlugIn* plugIn = NULL;
       
   359     for( TInt i = 0; i < iPlugIns.Count(); ++i )
       
   360         {
       
   361         if( iPlugIns[ i ].iImplUid == sourceUid )
       
   362             {
       
   363             plugIn = iPlugIns[ i ].iPlugIn;
       
   364             break;
       
   365             }
       
   366         }
       
   367 
       
   368     MCFContextSource* contextSource = NULL;
       
   369     if ( plugIn )
       
   370         {
       
   371         // Check plugin's support for MCFContextSource extension.
       
   372         contextSource = reinterpret_cast< MCFContextSource* >(
       
   373                 plugIn->Extension( KCFContextSourceInterfaceUid ) );
       
   374         if ( !contextSource )
       
   375             {
       
   376             User::Leave( KErrExtensionNotSupported );
       
   377             }
       
   378         }
       
   379     else
       
   380         {
       
   381         TLinearOrder< TCFClientContextSourceInfo > uidOrder(
       
   382                 ClientContextSourceInfoUidOrder );
       
   383         TCFClientContextSourceInfo clientInfo;
       
   384         clientInfo.iClientSid = sourceUid;
       
   385         TInt pos = iClientContextSources.FindInOrder( clientInfo, uidOrder );
       
   386         if ( pos == KErrNotFound )
       
   387             {
       
   388             User::Leave( KErrBadHandle );
       
   389             }
       
   390         contextSource = iClientContextSources[ pos ].iSource;
       
   391         }
       
   392 
       
   393     // Process source command
       
   394     if ( contextSource )
       
   395         {
       
   396         contextSource->HandleCommand( aCommand );
       
   397         }
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CCFContextSourceManager::RegisterClientContextSource
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 EXPORT_C TInt CCFContextSourceManager::RegisterClientContextSource(
       
   405     MCFContextSource* aPublisher,
       
   406     const TUid& aPublisherUid )
       
   407     {
       
   408     FUNC_LOG;
       
   409 
       
   410     TLinearOrder< TCFClientContextSourceInfo > uidOrder(
       
   411             ClientContextSourceInfoUidOrder );
       
   412     TCFClientContextSourceInfo clientInfo( aPublisher, aPublisherUid );
       
   413 
       
   414     TInt err = iClientContextSources.InsertInOrder( clientInfo, uidOrder );
       
   415     if ( err == KErrAlreadyExists )
       
   416         {
       
   417         err = KErrNone;
       
   418         }
       
   419 
       
   420     ERROR( err, "CCFContextSourceManager::RegisterContextSourceClient - Failed" );
       
   421 
       
   422     return err;
       
   423     }
       
   424 
       
   425 // ---------------------------------------------------------------------------
       
   426 // CCFContextSourceManager::DeregisterClientContextSource
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 EXPORT_C void CCFContextSourceManager::DeregisterClientContextSource(
       
   430     const MCFContextSource& aPublisher )
       
   431     {
       
   432     FUNC_LOG;
       
   433 
       
   434     for ( TInt i = 0; i < iClientContextSources.Count(); ++i )
       
   435         {
       
   436         if ( &aPublisher == iClientContextSources[ i ].iSource )
       
   437             {
       
   438             iClientContextSources.Remove( i );
       
   439             break; // Stop, no duplicate entries are allowed.
       
   440             }
       
   441         }
       
   442     }
       
   443 
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // CCFContextSourceManager::InitializePhaseL
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 void CCFContextSourceManager::InitializePhaseL(
       
   450     CCFPhaseBase::TCFPhaseId aPhase )
       
   451     {
       
   452     FUNC_LOG;
       
   453 
       
   454     switch( aPhase )
       
   455         {
       
   456         case CCFPhaseBase::ECFDeviceStarting:
       
   457             {
       
   458             InitDeviceStartingPhaseL();
       
   459             break;
       
   460             }
       
   461         case CCFPhaseBase::ECFLoadingPlugins:
       
   462             {
       
   463             InitLoadingPluginsPhaseL();
       
   464             break;
       
   465             }
       
   466         default:
       
   467             {
       
   468             break;
       
   469             }
       
   470         }
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------------------------
       
   474 // CCFContextSourceManager::UpdatePlugInsL
       
   475 // ---------------------------------------------------------------------------
       
   476 //
       
   477 void CCFContextSourceManager::UpdatePlugInsL()
       
   478 	{
       
   479     FUNC_LOG;
       
   480 
       
   481     // List all plugins
       
   482     RImplInfoPtrArray implInfoArray;
       
   483     CleanupResetAndDestroyPushL( implInfoArray );
       
   484     REComSession::ListImplementationsL( KContextSourcePlugInInterface,
       
   485         implInfoArray );
       
   486     RemoveBlackListed( implInfoArray );
       
   487 
       
   488     // Check if the update is install or uninstall based
       
   489     TInt allCount = implInfoArray.Count();
       
   490     TInt loadedCount = iPlugIns.Count();
       
   491     INFO_1( "Found %d context source plug-in implementations from ecom", allCount );
       
   492     INFO_1( "%d context source plug-ins currently loaded", loadedCount );
       
   493 
       
   494     // Check if there were plugins installed
       
   495     if( allCount >= loadedCount )
       
   496         {
       
   497         for( TInt i = iPlugIns.Count() - 1; i >= 0; i-- )
       
   498             {
       
   499             TCFContextSourceInfo& pluginInfo = iPlugIns[i];
       
   500             for( TInt ii = 0; ii < implInfoArray.Count(); ii++ )
       
   501                 {
       
   502                 CImplementationInformation* implInfo = implInfoArray[ii];
       
   503                 if( implInfo->ImplementationUid() == pluginInfo.iImplUid )
       
   504                     {
       
   505                     TInt oldVer = pluginInfo.iImplVersion;
       
   506                     TInt newVer = implInfo->Version();
       
   507 
       
   508                     // Check if the version has increased
       
   509                     if( newVer > oldVer )
       
   510                         {
       
   511                         // Delete old plugin
       
   512                         ReleasePlugIn( pluginInfo.iPlugIn );
       
   513                         iPlugIns.Remove( i );
       
   514 
       
   515                         // Clean up garbage
       
   516                         REComSession::FinalClose();
       
   517 
       
   518                         // Load the new version
       
   519                         TRAPD( err, LoadPluginL( implInfo->ImplementationUid(), implInfo->Version() ) );
       
   520                         ERROR_3( err, "Upgrade of new version of context source plugin [%x], oldVer: [%d], newVer: [%d] failed",
       
   521                             implInfo->ImplementationUid().iUid, oldVer, newVer );
       
   522                         if( err == KErrNone )
       
   523                             {
       
   524                             INFO_3( "Upgraded new version of context source plugin [%x], oldVer: [%d], newVer: [%d]",
       
   525                                 implInfo->ImplementationUid().iUid, oldVer, newVer );
       
   526                             }
       
   527                         }
       
   528 
       
   529                     delete implInfo;
       
   530                     implInfoArray.Remove( ii );
       
   531                     break;
       
   532                     }
       
   533                 }
       
   534             }
       
   535 
       
   536         // Check if the installed plugin was not an updgrade but a new plugin
       
   537         for( TInt i = 0; i < implInfoArray.Count(); i++ )
       
   538             {
       
   539             CImplementationInformation* implInfo = implInfoArray[i];
       
   540             
       
   541             // Prepare loaders
       
   542             PrepareLoaderL( implInfo );
       
   543             }
       
   544             // Execute loaders
       
   545            ExecuteLoaders();
       
   546         }
       
   547 
       
   548     // Check if there were plugins unistalled
       
   549     else
       
   550         {
       
   551         for( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   552             {
       
   553             TCFContextSourceInfo& pluginInfo = iPlugIns[i];
       
   554             pluginInfo.iMissing = ETrue;
       
   555             }
       
   556 
       
   557         // Check that which plugins are missing
       
   558         for( TInt i = 0; i < implInfoArray.Count(); i++ )
       
   559             {
       
   560             CImplementationInformation* implInfo = implInfoArray[i];
       
   561             for( TInt i = 0; i < iPlugIns.Count(); i++ )
       
   562                 {
       
   563                 TCFContextSourceInfo& pluginInfo = iPlugIns[i];
       
   564                 if( pluginInfo.iImplUid == implInfo->ImplementationUid() )
       
   565                     {
       
   566                     pluginInfo.iMissing = EFalse;
       
   567                     break;
       
   568                     }
       
   569                 }
       
   570             }
       
   571 
       
   572         // Delete missing plugins
       
   573         for( TInt i = iPlugIns.Count() - 1; i >= 0; i-- )
       
   574             {
       
   575             TCFContextSourceInfo& pluginInfo = iPlugIns[i];
       
   576             if( pluginInfo.iMissing )
       
   577                 {
       
   578                 INFO_2( "Removed uninstalled context source plugin [%x], ver: [%d]",
       
   579                     pluginInfo.iImplUid.iUid, pluginInfo.iImplVersion );
       
   580 
       
   581                 ReleasePlugIn( pluginInfo.iPlugIn );
       
   582                 iPlugIns.Remove( i );
       
   583 
       
   584                 // Clean up garbage
       
   585                 REComSession::FinalClose();
       
   586                 }
       
   587             }
       
   588         }
       
   589 
       
   590     // Cleanup
       
   591     CleanupStack::PopAndDestroy( &implInfoArray );
       
   592 	}
       
   593 
       
   594 // ---------------------------------------------------------------------------
       
   595 // CCFContextSourceManager::PrepareLoaderL
       
   596 // ---------------------------------------------------------------------------
       
   597 //
       
   598 void CCFContextSourceManager::PrepareLoaderL( CImplementationInformation* aImplementationInfo )
       
   599     {
       
   600     FUNC_LOG;
       
   601 
       
   602     TCFLoaderInfo* loaderInfo = new(ELeave)TCFLoaderInfo( *aImplementationInfo, *this );
       
   603     CleanupStack::PushL( loaderInfo );
       
   604     TCallBack cb( LoaderCallBack, loaderInfo );
       
   605     CAsyncCallBack* loader = new( ELeave ) CAsyncCallBack( cb, CActive::EPriorityStandard );
       
   606     CleanupStack::PushL( loader );
       
   607     iLoaders.AppendL( loader );
       
   608     CleanupStack::Pop( loader );
       
   609     CleanupStack::Pop( loaderInfo );    
       
   610     }
       
   611 
       
   612 // ---------------------------------------------------------------------------
       
   613 // CCFContextSourceManager::ExecuteLoaders
       
   614 // ---------------------------------------------------------------------------
       
   615 //
       
   616 void CCFContextSourceManager::ExecuteLoaders()
       
   617     {
       
   618     FUNC_LOG;
       
   619     
       
   620     for( TInt i = 0; i < iLoaders.Count(); i++ )
       
   621         {
       
   622         CAsyncCallBack* loader = iLoaders[i];
       
   623         loader->CallBack();
       
   624         }
       
   625     }
       
   626 
       
   627 // ---------------------------------------------------------------------------
       
   628 // CCFContextSourceManager::LoaderCallBack
       
   629 // ---------------------------------------------------------------------------
       
   630 //
       
   631 TInt CCFContextSourceManager::LoaderCallBack( TAny* aLoaderInfo )
       
   632     {
       
   633     FUNC_LOG;
       
   634     
       
   635     TCFLoaderInfo* loaderInfo = static_cast<TCFLoaderInfo*>( aLoaderInfo );
       
   636     CCFContextSourceManager& manager = loaderInfo->iManager;    
       
   637     
       
   638     TRAPD( err, manager.LoadPluginL( loaderInfo->iImplUid,
       
   639                                      loaderInfo->iImplVersion ));                
       
   640 
       
   641     if( err == KErrNone )
       
   642         {
       
   643         INFO_2( "Loaded context source plugin [%x], ver: [%d]",
       
   644                  loaderInfo->iImplUid.iUid, loaderInfo->iImplVersion );
       
   645         }
       
   646     else
       
   647         {
       
   648         ERROR_2( err, "Failed to load context source plugin [%x], ver: [%d]",
       
   649                  loaderInfo->iImplUid.iUid, loaderInfo->iImplVersion );
       
   650         }
       
   651     
       
   652     manager.iLoadedCount++;
       
   653     
       
   654     if( manager.iLoadedCount == manager.iLoaders.Count() )
       
   655         {
       
   656         manager.iLoaders.ResetAndDestroy();
       
   657         manager.iLoadedCount = 0;
       
   658 
       
   659         if( manager.iEventHandler )
       
   660             {
       
   661         	manager.iEventHandler->HandleEvent( MCFStarterEventHandler::EContextSourcePluginsLoaded );
       
   662             }
       
   663         }
       
   664     delete loaderInfo;    
       
   665     return KErrNone;
       
   666     }
       
   667 
       
   668 // ---------------------------------------------------------------------------
       
   669 // CCFContextSourceManager::CreateAndInitializePlugInL
       
   670 // ---------------------------------------------------------------------------
       
   671 //
       
   672 CCFContextSourcePlugIn* CCFContextSourceManager::CreateAndInitializePlugInL(
       
   673     const TUid& aImplementationUid,
       
   674     TContextSourceInitParams& aParams )
       
   675     {
       
   676     FUNC_LOG;
       
   677 
       
   678     // Create plug-in
       
   679     CCFContextSourcePlugIn* plugIn = CCFContextSourcePlugIn::NewL(
       
   680         aImplementationUid,
       
   681         &aParams );
       
   682     CleanupStack::PushL( plugIn );
       
   683 
       
   684     // Handle settings
       
   685     CCFContextSourceSettingArray* settings
       
   686             = PlugInSettingsL( aImplementationUid );
       
   687 
       
   688     if ( settings->Count() )
       
   689         {
       
   690         // Forward settings to plug-in
       
   691         // Plug-in takes ownership of the array.
       
   692         plugIn->HandleSettingL( settings );
       
   693         }
       
   694     else
       
   695         {
       
   696         // Manager owns the array, cleanup now.
       
   697         delete settings;
       
   698         settings = NULL;
       
   699         }
       
   700 
       
   701     // Define context
       
   702     plugIn->DefineContextsL();
       
   703 
       
   704     // Initialize
       
   705     plugIn->InitializeL();
       
   706 
       
   707     // Cleanup
       
   708     CleanupStack::Pop( plugIn );
       
   709 
       
   710     return plugIn;
       
   711     }
       
   712 
       
   713 // ---------------------------------------------------------------------------
       
   714 // CCFContextSourceManager::InitDeviceStartingPhaseL
       
   715 // ---------------------------------------------------------------------------
       
   716 //
       
   717 void CCFContextSourceManager::InitDeviceStartingPhaseL()
       
   718     {
       
   719     FUNC_LOG;
       
   720 
       
   721     // List all plugins
       
   722     RImplInfoPtrArray implInfoArray;
       
   723     CleanupResetAndDestroyPushL( implInfoArray );
       
   724     REComSession::ListImplementationsL( KContextSourcePlugInInterface,
       
   725         implInfoArray );
       
   726 
       
   727     // Load context source manager configuration cenrep
       
   728     CRepository* cenRep = CRepository::NewLC( KCRUidCFContextSourceConf );
       
   729     TInt count = 0;
       
   730     TInt err = cenRep->Get( KContextSourceNumberOfMandatoryPlugIns, count );
       
   731     if( err == KErrNone && count )
       
   732         {
       
   733         INFO_1( "Found %d context source plug-in implementations from cenrep", count );
       
   734         TUint32 key = KContextSourceNumberOfMandatoryPlugIns + 1;
       
   735         for( TInt i = 0; i < count; i++ )
       
   736             {
       
   737             TInt uid = 0;
       
   738 
       
   739             // Ignore first key
       
   740             err = cenRep->Get( key + i, uid );
       
   741             if( err == KErrNone && uid )
       
   742                 {
       
   743                 CImplementationInformation* info = NULL;
       
   744                 for( TInt ii = 0; ii < implInfoArray.Count(); ii++ )
       
   745                     {
       
   746                     info = implInfoArray[ii];
       
   747                     if( info->ImplementationUid() == TUid::Uid( uid ) )
       
   748                         {
       
   749                         break;
       
   750                         }
       
   751                     info = NULL;
       
   752                     }
       
   753                 if( info )
       
   754                     {
       
   755                     TRAPD( err, LoadPluginL( info->ImplementationUid(), info->Version() ) );
       
   756                     if( err == KErrNone )
       
   757                         {
       
   758                         INFO_2( "Loaded context source plugin [%x], ver: [%d]",
       
   759                             info->ImplementationUid().iUid, info->Version() );
       
   760                         }
       
   761                     else
       
   762                         {
       
   763                         ERROR_2( err, "Failed to load context source plugin [%x], ver: [%d]",
       
   764                             info->ImplementationUid().iUid, info->Version() );
       
   765                         }
       
   766                     }
       
   767                 }
       
   768             else
       
   769                 {
       
   770                 INFO_2( "UID 0x%08x skipped, error code %d", uid, err );
       
   771                 }
       
   772             }
       
   773         }
       
   774 
       
   775     // Clean up
       
   776     CleanupStack::PopAndDestroy( cenRep );
       
   777     CleanupStack::PopAndDestroy( &implInfoArray );
       
   778     }
       
   779 
       
   780 // ---------------------------------------------------------------------------
       
   781 // CCFContextSourceManager::InitLoadingPluginsPhaseL
       
   782 // ---------------------------------------------------------------------------
       
   783 //
       
   784 void CCFContextSourceManager::InitLoadingPluginsPhaseL()
       
   785     {
       
   786     FUNC_LOG;
       
   787 
       
   788     UpdatePlugInsL();
       
   789     }
       
   790 
       
   791 // ---------------------------------------------------------------------------
       
   792 // CCFContextSourceManager::LoadPluginL
       
   793 // ---------------------------------------------------------------------------
       
   794 //
       
   795 void CCFContextSourceManager::LoadPluginL( TUid aUid,
       
   796                                            TInt aImplVersion )
       
   797     {
       
   798     FUNC_LOG;
       
   799 
       
   800     INFO_2( "Loading context source plug-in with UID [%x], version [%d]",
       
   801     		aUid.iUid,
       
   802     		aImplVersion );
       
   803     HEAP_2( "Before loading context source plug-in with UID [%x], version [%d]",
       
   804             aUid.iUid,
       
   805             aImplVersion );
       
   806     TContextSourceInitParams initParams( static_cast<MCFContextInterface&>( iCF ) );
       
   807     CCFContextSourcePlugIn* plugIn = NULL;
       
   808     TRAPD( err, plugIn = CreateAndInitializePlugInL( aUid,
       
   809     		                                         initParams ) );
       
   810     if( err == KErrNone )
       
   811         {
       
   812         // Plug-in loaded succesfully, store it
       
   813         TCFContextSourceInfo info( aUid, aImplVersion, plugIn );
       
   814         TInt err = iPlugIns.Append( info );
       
   815         if( err != KErrNone )
       
   816             {
       
   817             ERROR_1( err, "Context source plug-in: %x could not be appended in array",
       
   818             		aUid.iUid );
       
   819             ReleasePlugIn( plugIn );
       
   820             }
       
   821         else
       
   822             {
       
   823             INFO_1( "Context source plug-in: %x succesfully loaded",
       
   824             		aUid.iUid );
       
   825             }
       
   826         }
       
   827     else
       
   828         {
       
   829         ERROR_2( err, "Context source plug-in %x load error: %d",
       
   830         		aUid.iUid,
       
   831     	    	err );
       
   832 
       
   833         err = AddToBlackList( aUid );
       
   834         ERROR( err, "Failed to add UID to blacklist" );
       
   835         }
       
   836 
       
   837     HEAP_2( "After loading context source plug-in with UID [%x], version [%d]",
       
   838             aUid.iUid,
       
   839             aImplVersion );
       
   840     }
       
   841 
       
   842 // ---------------------------------------------------------------------------
       
   843 // CCFContextSourceManager::ConfigureRomSettingFolder
       
   844 // ---------------------------------------------------------------------------
       
   845 //
       
   846 TBool CCFContextSourceManager::ConfigureRomSettingFolder( TDes& aFile,
       
   847     const TUid& aUid )
       
   848     {
       
   849     FUNC_LOG;
       
   850 
       
   851     // Check rom
       
   852     // '[12345678]' -> 'z:\private\10282BC4\Settings\12345678\'
       
   853     aFile.Copy( KSettingsPathRom );
       
   854     aFile.Append( aUid.Name().Mid(
       
   855         KUIDNameStartPos,
       
   856         KUIDNameEndPos ) );
       
   857     aFile.Append( KFolderExt );
       
   858 
       
   859     return BaflUtils::PathExists( iFs, aFile );
       
   860     }
       
   861 
       
   862 // ---------------------------------------------------------------------------
       
   863 // CCFContextSourceManager::ConfigureRamSettingFolder
       
   864 // ---------------------------------------------------------------------------
       
   865 //
       
   866 TBool CCFContextSourceManager::ConfigureRamSettingFolder( TDes& aFile,
       
   867     const TUid& aUid )
       
   868     {
       
   869     FUNC_LOG;
       
   870 
       
   871     // Check rom
       
   872     // '[12345678]' -> 'c:\private\10282BC4\Settings\12345678\'
       
   873     aFile.Copy( KSettingsPathRam );
       
   874     aFile.Append( aUid.Name().Mid(
       
   875         KUIDNameStartPos,
       
   876         KUIDNameEndPos ) );
       
   877     aFile.Append( KFolderExt );
       
   878 
       
   879     return BaflUtils::PathExists( iFs, aFile );
       
   880     }
       
   881 
       
   882 // ---------------------------------------------------------------------------
       
   883 // CCFContextSourceManager::ParsePlugInSettingsL
       
   884 // ---------------------------------------------------------------------------
       
   885 //
       
   886 void CCFContextSourceManager::ParsePlugInSettingsL( const TDesC& aFolder,
       
   887     CCFContextSourceSettingArray& aSettings )
       
   888     {
       
   889     CDir* dirList = NULL;
       
   890     TInt err = iFs.GetDir( aFolder,
       
   891         KEntryAttMaskSupported,
       
   892         ESortByName,
       
   893         dirList );
       
   894     if( err == KErrNone )
       
   895         {
       
   896         CleanupStack::PushL( dirList );
       
   897         TInt count = dirList->Count();
       
   898         INFO_1( "Found %d setting files", count );
       
   899 
       
   900         HBufC* file = HBufC::NewLC( KMaxFileName );
       
   901         TPtr filePtr = file->Des();
       
   902         for( TInt i = 0; i < count; i++ )
       
   903             {
       
   904             // Configure full file path
       
   905             filePtr.Copy( aFolder.Left(
       
   906                 aFolder.Length() - KSettingExtension().Length() ) );
       
   907             filePtr.Append( (*dirList)[i].iName );
       
   908             INFO_1( "Parsing setting file '%S'", &(*dirList)[i].iName );
       
   909 
       
   910             // Parse settings
       
   911             iSettingsManager->ParseSettingsL( filePtr, aSettings );
       
   912             }
       
   913 
       
   914         CleanupStack::PopAndDestroy( file );
       
   915         CleanupStack::PopAndDestroy( dirList );
       
   916         }
       
   917     }
       
   918 
       
   919 // ---------------------------------------------------------------------------
       
   920 // CCFContextSourceManager::PlugInSettingsL
       
   921 // ---------------------------------------------------------------------------
       
   922 //
       
   923 CCFContextSourceSettingArray* CCFContextSourceManager::PlugInSettingsL(
       
   924     const TUid& aImplementationUid )
       
   925     {
       
   926     // Get settings for the plugin.
       
   927     CCFContextSourceSettingArray* settings
       
   928             = CCFContextSourceSettingArray::NewLC();    // CLEANUP<< settings
       
   929     HBufC* settingFile = HBufC::NewLC( KMaxFileName );  // CLEANUP<< settingFile
       
   930     TPtr settingFilePtr = settingFile->Des();
       
   931     if( ConfigureRomSettingFolder( settingFilePtr, aImplementationUid ) )
       
   932         {
       
   933         INFO_1( "Found context source ROM setting folder '%S'", &settingFilePtr );
       
   934         // Setting folder found, parse settings.
       
   935         settingFilePtr.Append( KSettingExtension );
       
   936         ParsePlugInSettingsL( settingFilePtr, *settings );
       
   937         }
       
   938 
       
   939     if( ConfigureRamSettingFolder( settingFilePtr, aImplementationUid ) )
       
   940         {
       
   941         INFO_1( "Found context source RAM setting folder '%S'", &settingFilePtr );
       
   942         // Setting folder found, parse settings.
       
   943         settingFilePtr.Append( KSettingExtension );
       
   944         ParsePlugInSettingsL( settingFilePtr, *settings );
       
   945         }
       
   946 
       
   947     CleanupStack::PopAndDestroy( settingFile );         // CLEANUP>> settingFile
       
   948     CleanupStack::Pop( settings );                      // CLEANUP>> settings
       
   949     return settings;
       
   950     }
       
   951 
       
   952 // ---------------------------------------------------------------------------
       
   953 // CCFContextSourceManager::PlugInL
       
   954 // ---------------------------------------------------------------------------
       
   955 //
       
   956 MCFContextSourceUpgrade* CCFContextSourceManager::PlugInL(
       
   957     const TUid& aContextSourceUid ) const
       
   958     {
       
   959     FUNC_LOG;
       
   960 
       
   961     // Find plugin.
       
   962     CCFContextSourcePlugIn* plugIn = NULL;
       
   963     for( TInt i = 0; i < iPlugIns.Count(); ++i )
       
   964         {
       
   965         if( iPlugIns[ i ].iImplUid == aContextSourceUid )
       
   966             {
       
   967             plugIn = iPlugIns[ i ].iPlugIn;
       
   968             break;
       
   969             }
       
   970         }
       
   971     if ( !plugIn )
       
   972         {
       
   973         User::Leave( KErrBadHandle );
       
   974         }
       
   975 
       
   976     // Check plugin's support for MCFContextSourceUpgrade extension.
       
   977     MCFContextSourceUpgrade* contextSourceUpgrade =
       
   978             ( MCFContextSourceUpgrade* )plugIn->Extension(
       
   979                     KCFContextSourceUpgradeUid );
       
   980     if ( !contextSourceUpgrade )
       
   981         {
       
   982         User::Leave( KErrExtensionNotSupported );
       
   983         }
       
   984 
       
   985     return contextSourceUpgrade;
       
   986     }
       
   987 
       
   988 // ---------------------------------------------------------------------------
       
   989 // CCFContextSourceManager::CopySettingFileL
       
   990 // ---------------------------------------------------------------------------
       
   991 //
       
   992 void CCFContextSourceManager::CopySettingFileL( const RFile& aSettingFile,
       
   993     const TUid& aContextSourceUid,
       
   994     RThread& aClientThread )
       
   995     {
       
   996     FUNC_LOG;
       
   997 
       
   998     HBufC* settingFile = HBufC::NewLC( KMaxFileName );  // CLEANUP<< settingFile
       
   999     TPtr settingFilePtr = settingFile->Des();
       
  1000 
       
  1001     ConfigureRamSettingFolder( settingFilePtr, aContextSourceUid );
       
  1002 
       
  1003     HBufC* filename = HBufC::NewLC( KMaxFileName );     // CLEANUP<< filename
       
  1004     TPtr filenamePtr = filename->Des();
       
  1005 
       
  1006     User::LeaveIfError( aSettingFile.Name( filenamePtr ) );
       
  1007 
       
  1008     TUid clientSid( aClientThread.SecureId() );
       
  1009     settingFilePtr.Append( clientSid.Name().Mid( KUIDNameStartPos,
       
  1010             KUIDNameEndPos ) );
       
  1011     settingFilePtr.Append( KUnderscore );
       
  1012     settingFilePtr.Append( filenamePtr );
       
  1013     BaflUtils::EnsurePathExistsL( iFs, settingFilePtr );
       
  1014 
       
  1015     CleanupStack::PopAndDestroy( filename );            // CLEANUP>> filename
       
  1016 
       
  1017     CFileMan* fileMan = CFileMan::NewL( iFs );
       
  1018     CleanupStack::PushL( fileMan );                     // CLEANUP<< fileMan
       
  1019 
       
  1020     TInt err = fileMan->Copy( aSettingFile, settingFilePtr );
       
  1021     ERROR( err, "CCFContextSourceManager::CopySettingFileL - Copying setting file failed" );
       
  1022     User::LeaveIfError( err );
       
  1023     TInt clearMask = KEntryAttReadOnly | KEntryAttHidden | KEntryAttSystem;
       
  1024     err = fileMan->Attribs( settingFilePtr, 0, clearMask, 0 );
       
  1025     User::LeaveIfError( err );
       
  1026 
       
  1027     CleanupStack::PopAndDestroy( fileMan );             // CLEANUP>> fileMan
       
  1028     CleanupStack::PopAndDestroy( settingFile );         // CLEANUP>> settingFile
       
  1029     }
       
  1030 
       
  1031 // ---------------------------------------------------------------------------
       
  1032 // CCFContextSourceManager::DeleteSettingFileL
       
  1033 // ---------------------------------------------------------------------------
       
  1034 //
       
  1035 void CCFContextSourceManager::DeleteSettingFileL( const TDesC& aSettingFile,
       
  1036     const TUid& aContextSourceUid,
       
  1037     RThread& aClientThread )
       
  1038     {
       
  1039     FUNC_LOG;
       
  1040 
       
  1041     HBufC* settingFile = HBufC::NewLC( KMaxFileName );  // CLEANUP<< settingFile
       
  1042     TPtr settingFilePtr = settingFile->Des();
       
  1043 
       
  1044     ConfigureRamSettingFolder( settingFilePtr, aContextSourceUid );
       
  1045 
       
  1046     TUid clientSid( aClientThread.SecureId() );
       
  1047     settingFilePtr.Append( clientSid.Name().Mid( KUIDNameStartPos,
       
  1048             KUIDNameEndPos ) );
       
  1049     settingFilePtr.Append( KUnderscore );
       
  1050     settingFilePtr.Append( aSettingFile );
       
  1051 
       
  1052     TInt err = BaflUtils::DeleteFile( iFs, settingFilePtr );
       
  1053     ERROR( err, "CCFContextSourceManager::DeleteSettingFileL - Deleting setting file(s) failed" );
       
  1054     User::LeaveIfError( err );
       
  1055 
       
  1056     CleanupStack::PopAndDestroy( settingFile );         // CLEANUP>> settingFile
       
  1057     }
       
  1058 
       
  1059 //------------------------------------------------------------------------------
       
  1060 // CCFContextSourceManager::ReleasePlugIn
       
  1061 //------------------------------------------------------------------------------
       
  1062 //
       
  1063 void CCFContextSourceManager::ReleasePlugIn( CCFContextSourcePlugIn*& aPlugIn )
       
  1064     {
       
  1065     FUNC_LOG;
       
  1066 
       
  1067     // Deregister publisher
       
  1068     TAny* extension = aPlugIn->Extension( KCFContextSourceInterfaceUid );
       
  1069     if( extension )
       
  1070         {
       
  1071         MCFContextSource* contextSource =
       
  1072             reinterpret_cast<MCFContextSource*>( extension );
       
  1073         iCF.DeregisterPublisher( *contextSource );
       
  1074         }
       
  1075     // Unsubscribe all contexts if the plug-in is uninstalled
       
  1076     extension = aPlugIn->Extension( KCFSubscriptionListenerInterfaceUid );
       
  1077     if( extension )
       
  1078         {
       
  1079         MCFContextSubscriptionListener* interface =
       
  1080             static_cast<MCFContextSubscriptionListener*>( extension );
       
  1081         iCF.UnsubscribeContexts( *interface );
       
  1082         }
       
  1083     delete aPlugIn;
       
  1084     aPlugIn = NULL;
       
  1085     }