emailservices/emailservermonitor/src/emailshutter.cpp
changeset 4 e7aa27f58ae1
parent 1 12c456ceeff2
child 8 e1b6206813b4
equal deleted inserted replaced
3:a4d6f1ea0416 4:e7aa27f58ae1
    22 #include <e32des16.h>                   // Descriptors
    22 #include <e32des16.h>                   // Descriptors
    23 
    23 
    24 #include <e32property.h>                // RProperty
    24 #include <e32property.h>                // RProperty
    25 #include <apgtask.h>                    // TApaTaskList
    25 #include <apgtask.h>                    // TApaTaskList
    26 #include <w32std.h>                     // RWsSession
    26 #include <w32std.h>                     // RWsSession
       
    27 #include <s32mem.h>                     // RDesRead/WriteStream
    27 
    28 
    28 #include <AlwaysOnlineManagerClient.h>  // RAlwaysOnlineClientSession
    29 #include <AlwaysOnlineManagerClient.h>  // RAlwaysOnlineClientSession
    29 #include <CPsRequestHandler.h>          // CPSRequestHandler
    30 #include <CPsRequestHandler.h>          // CPSRequestHandler
    30 #include <centralrepository.h>          // CRepository
    31 #include <centralrepository.h>          // CRepository
       
    32 #include <platform/mw/aisystemuids.hrh> // HomeScreen UIDs
    31 
    33 
    32 #include "FreestyleEmailUiConstants.h"  // FS Email UI UID
    34 #include "FreestyleEmailUiConstants.h"  // FS Email UI UID
    33 #include "fsmtmsconstants.h"            // MCE, Phonebook & Calendar UIDs
    35 #include "fsmtmsconstants.h"            // MCE, Phonebook & Calendar UIDs
    34 #include "EmailStoreUids.hrh"           // KUidMessageStoreExe
    36 #include "EmailStoreUids.hrh"           // KUidMessageStoreExe
    35 
    37 
    47     KFSEmailUiUid,              // Freestyle Email UI
    49     KFSEmailUiUid,              // Freestyle Email UI
    48     { SettingWizardUidAsTInt }, // TP Wizard
    50     { SettingWizardUidAsTInt }, // TP Wizard
    49     { KMceAppUid },             // MCE
    51     { KMceAppUid },             // MCE
    50     { KPhoneBookUid },          // Phonebook 1 & 2
    52     { KPhoneBookUid },          // Phonebook 1 & 2
    51     { KCalendarAppUid1 }        // Calendar
    53     { KCalendarAppUid1 }        // Calendar
       
    54     };
       
    55 
       
    56 // Applications that should not be closed. Should include only system
       
    57 // applications that free the email resources by some other means.
       
    58 const TUid KApplicationsNotToBeClosed[] =
       
    59     {
       
    60     { AI_SID_AIFW_EXE },        // HomeScreen
       
    61     { AI_UID3_AIFW_COMMON },    // HomeScreen
    52     };
    62     };
    53 
    63 
    54 // Non-UI clients that need to be closed
    64 // Non-UI clients that need to be closed
    55 const TUid KOtherClientsToClose[] =
    65 const TUid KOtherClientsToClose[] =
    56     {
    66     {
    76     {
    86     {
    77     { KUidMessageStoreExe },        // MessageStoreExe.exe
    87     { KUidMessageStoreExe },        // MessageStoreExe.exe
    78     { KUidEmailStorePreInstallExe } // MessageStorePreInstallExe
    88     { KUidEmailStorePreInstallExe } // MessageStorePreInstallExe
    79     };
    89     };
    80 
    90 
       
    91 const TInt KEmailUidExtraBuffer = 2 * KEmailPlatformApiUidItemSize;
    81 
    92 
    82 // ======== MEMBER FUNCTION DEFINITIONS ========
    93 // ======== MEMBER FUNCTION DEFINITIONS ========
    83 
    94 
    84 // ---------------------------------------------------------------------------
    95 // ---------------------------------------------------------------------------
    85 // Two-phased class constructor.
    96 // Two-phased class constructor.
   121 // ---------------------------------------------------------------------------
   132 // ---------------------------------------------------------------------------
   122 //
   133 //
   123 CEmailShutter::~CEmailShutter()
   134 CEmailShutter::~CEmailShutter()
   124     {
   135     {
   125     FUNC_LOG;
   136     FUNC_LOG;
       
   137     iInstStatusProperty.Close();
       
   138     iPlatformApiAppsToClose.Close();
   126     }
   139     }
   127 
   140 
   128 // ---------------------------------------------------------------------------
   141 // ---------------------------------------------------------------------------
   129 // Second phase class constructor.
   142 // Second phase class constructor.
   130 // ---------------------------------------------------------------------------
   143 // ---------------------------------------------------------------------------
   138                                              RProperty::EInt,
   151                                              RProperty::EInt,
   139                                              KAllowAllPolicy,
   152                                              KAllowAllPolicy,
   140                                              KPowerMgmtPolicy );
   153                                              KPowerMgmtPolicy );
   141     if( error != KErrNone && error != KErrAlreadyExists )
   154     if( error != KErrNone && error != KErrAlreadyExists )
   142         {
   155         {
   143         ERROR_1( error, "RProperty::Define failed, error code: ", error );
   156         ERROR( error, "RProperty::Define (EEmailPsKeyInstallationStatus) failed!" );
       
   157         ERROR_1( error, "    error code: ", error );
   144         User::Leave( error );
   158         User::Leave( error );
   145         }
   159         }
   146 
   160 
   147     error = iInstStatusProperty.Attach( KEmailShutdownPsCategory,
   161     error = iInstStatusProperty.Attach( KEmailShutdownPsCategory,
   148                                         EEmailPsKeyInstallationStatus );
   162                                         EEmailPsKeyInstallationStatus );
   149     if( error != KErrNone )
   163     if( error != KErrNone )
   150         {
   164         {
   151         ERROR_1( error, "RProperty::Attach failed, error code: ", error );
   165         ERROR( error, "RProperty::Attach (EEmailPsKeyInstallationStatus) failed!" );
       
   166         ERROR_1( error, "    error code: ", error );
   152         User::Leave( error );
   167         User::Leave( error );
       
   168         }
       
   169 
       
   170     // Define P&S key used to register platform API applications
       
   171     error = RProperty::Define( EEmailPsKeyPlatformApiAppsToCloseLength,
       
   172                                RProperty::EInt,
       
   173                                KAllowAllPolicy,
       
   174                                KWriteDeviceDataPolicy );
       
   175     
       
   176     if( error != KErrNone && error != KErrAlreadyExists )
       
   177         {
       
   178         ERROR( error, "RProperty::Define (EEmailPsKeyPlatformApiAppsToCloseLength) failed!" );
       
   179         ERROR_1( error, "    error code: ", error );
       
   180         }
       
   181     
       
   182     // Define P&S key used to register platform API applications
       
   183     error = RProperty::Define( EEmailPsKeyPlatformApiAppsToClose,
       
   184                                RProperty::EByteArray,
       
   185                                KAllowAllPolicy,
       
   186                                KWriteDeviceDataPolicy );
       
   187     
       
   188     if( error != KErrNone && error != KErrAlreadyExists )
       
   189         {
       
   190         ERROR( error, "RProperty::Define (EEmailPsKeyPlatformApiAppsToClose) failed!" );
       
   191         ERROR_1( error, "    error code: ", error );
   153         }
   192         }
   154     
   193     
   155     CActiveScheduler::Add(this);
   194     CActiveScheduler::Add(this);
   156     }
   195     }
   157 
   196 
   271     User::LeaveIfError( session.Connect() );
   310     User::LeaveIfError( session.Connect() );
   272     CleanupClosePushL( session );
   311     CleanupClosePushL( session );
   273 
   312 
   274     TApaTaskList taskList( session );
   313     TApaTaskList taskList( session );
   275 
   314 
       
   315     // First end our own applications that are defined in hard coded list
   276     TInt count = sizeof(KApplicationsToClose) / sizeof(TUid);
   316     TInt count = sizeof(KApplicationsToClose) / sizeof(TUid);
   277     for( TInt i = 0; i<count; i++ )
   317     for( TInt i = 0; i<count; i++ )
   278         {
   318         {
   279         TApaTask task = taskList.FindApp( KApplicationsToClose[i] );
   319         TApaTask task = taskList.FindApp( KApplicationsToClose[i] );
   280         if ( task.Exists() )
   320         if ( task.Exists() )
   282             INFO_1( "Closing UI app with UID: %d", KApplicationsToClose[i].iUid );
   322             INFO_1( "Closing UI app with UID: %d", KApplicationsToClose[i].iUid );
   283 
   323 
   284             task.EndTask();
   324             task.EndTask();
   285             }
   325             }
   286         }
   326         }
   287 
   327     
       
   328     // Then end applications that are registered in P&S as platform API users
       
   329     for( TInt i = 0; i<iPlatformApiAppsToClose.Count(); i++ )
       
   330         {
       
   331         TApaTask task = taskList.FindApp( iPlatformApiAppsToClose[i] );
       
   332         if ( task.Exists() )
       
   333             {
       
   334             INFO_1( "Closing API UI app with UID: %d", iPlatformApiAppsToClose[i].iUid );
       
   335 
       
   336             task.EndTask();
       
   337             }
       
   338         }
       
   339     
   288     CleanupStack::PopAndDestroy( &session );
   340     CleanupStack::PopAndDestroy( &session );
   289     }
   341     }
   290 
   342 
   291 // ---------------------------------------------------------------------------
   343 // ---------------------------------------------------------------------------
   292 // Define and publish the P&S key to inform all related services about the
   344 // Define and publish the P&S key to inform all related services about the
   435             return ETrue;
   487             return ETrue;
   436             }
   488             }
   437         
   489         
   438         count = sizeof(KOtherClientsToClose) / sizeof(TUid);
   490         count = sizeof(KOtherClientsToClose) / sizeof(TUid);
   439         if( FindFromArray( aSid, KOtherClientsToClose, count ) )
   491         if( FindFromArray( aSid, KOtherClientsToClose, count ) )
       
   492             {
       
   493             return ETrue;
       
   494             }
       
   495 
       
   496         // Check also clients registered as platform API users
       
   497         if( iPlatformApiAppsToClose.Find( aSid ) != KErrNotFound )
   440             {
   498             {
   441             return ETrue;
   499             return ETrue;
   442             }
   500             }
   443         }
   501         }
   444 
   502 
   529     // Cancel Email Server monitoring before closing the server
   587     // Cancel Email Server monitoring before closing the server
   530     if( iMonitor )
   588     if( iMonitor )
   531         {
   589         {
   532         iMonitor->Cancel();
   590         iMonitor->Cancel();
   533         }
   591         }
   534     
   592 
       
   593     // First read the platform API UIDs from P&S, those are needed later
       
   594     TRAP_IGNORE( ReadPlatformApiUidsL() );
       
   595 
   535     // End all clients
   596     // End all clients
   536     EndClients();
   597     EndClients();
   537     // Wait some time to give the clients some time to shut down themselves
   598     // Wait some time to give the clients some time to shut down themselves
   538     Wait( KTimeToWaitApplicationsInSeconds );
   599     Wait( KTimeToWaitApplicationsInSeconds );
   539     
   600     
   585         totalWaitTime += KWaitTimeCycleInSeconds;
   646         totalWaitTime += KWaitTimeCycleInSeconds;
   586         // Do this as long as aMaxWaitTime has not elapsed and 
   647         // Do this as long as aMaxWaitTime has not elapsed and 
   587         // there are some process(es) to wait for
   648         // there are some process(es) to wait for
   588         } while ( ( totalWaitTime < aMaxWaitTime ) && KillEmAll( aMode, ETrue ) );
   649         } while ( ( totalWaitTime < aMaxWaitTime ) && KillEmAll( aMode, ETrue ) );
   589     }
   650     }
       
   651 
       
   652 // ---------------------------------------------------------------------------
       
   653 // Reads platform API process UIDs from Publish and Subscribe key
       
   654 // ---------------------------------------------------------------------------
       
   655 //
       
   656 void CEmailShutter::ReadPlatformApiUidsL()
       
   657     {
       
   658     FUNC_LOG;
       
   659 
       
   660     iPlatformApiAppsToClose.Reset();
       
   661 
       
   662     // Read buffer length
       
   663     TInt bufLength( 0 );
       
   664     User::LeaveIfError( RProperty::Get( KEmailShutdownPsCategory,
       
   665                                         EEmailPsKeyPlatformApiAppsToCloseLength,
       
   666                                         bufLength ) );
       
   667 
       
   668     // Allocate buffer for reading and then read the list of UIDs from P&S.
       
   669     // Adding some extra buffer just in case the size key and actual list
       
   670     // are out of sync. This shouldn't happen, but you never know.
       
   671     HBufC8* readBuf = HBufC8::NewLC( bufLength + KEmailUidExtraBuffer );
       
   672     TPtr8 readPtr = readBuf->Des();
       
   673     
       
   674     User::LeaveIfError( RProperty::Get( KEmailShutdownPsCategory,
       
   675                                         EEmailPsKeyPlatformApiAppsToClose,
       
   676                                         readPtr ) );
       
   677     
       
   678     RDesReadStream readStream( readPtr );
       
   679     CleanupClosePushL( readStream );
       
   680     
       
   681     // Get items count from the actual buffer
       
   682     TInt itemsCount = readPtr.Length() / KEmailPlatformApiUidItemSize;
       
   683     
       
   684     for ( TInt ii = 0;ii < itemsCount; ++ii )
       
   685         {
       
   686         // Read next UID from the stream
       
   687         TUid item = TUid::Uid( readStream.ReadInt32L() );
       
   688 
       
   689         // Append only UIDs of such applications that should be closed
       
   690         TInt count = sizeof(KApplicationsNotToBeClosed) / sizeof(TUid);
       
   691         if( !FindFromArray( item, KApplicationsNotToBeClosed, count ) )
       
   692             {
       
   693             iPlatformApiAppsToClose.AppendL( item );
       
   694             }
       
   695         }
       
   696     
       
   697     CleanupStack::PopAndDestroy( 2, readBuf );
       
   698     }