idlefw/hslaunch/src/hslaunch.cpp
changeset 4 1a2a00e78665
child 14 15e4dd19031c
equal deleted inserted replaced
3:ff572005ac23 4:1a2a00e78665
       
     1 /*
       
     2  * Copyright (c) 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: application class.                 
       
    15  *
       
    16  */
       
    17 
       
    18 #include <e32property.h>
       
    19 #include "hslaunch.h"
       
    20 
       
    21 // ========================= DECLARATIONS ==================================
       
    22 _LIT( KHsExeName, "homescreen.exe" );
       
    23 _LIT( KHsProcessName, "Home screen" );
       
    24 const TInt KSleepOnRetry = 250000; // 250ms
       
    25 const TUid KPSCategoryUid = TUid::Uid( 0x200286E3 );
       
    26 const TInt KPSCrashCountKey = 1;
       
    27 _LIT_SECURITY_POLICY_C1( KPSReadPolicy, ECapabilityReadDeviceData );
       
    28 _LIT_SECURITY_POLICY_C1( KPSWritePolicy, ECapabilityWriteDeviceData );
       
    29 
       
    30 // ========================= LOCAL FUNCTIONS ==================================
       
    31 
       
    32 void RunAppL()
       
    33     {
       
    34     // Construct active scheduler.
       
    35     CActiveScheduler* activeScheduler = new ( ELeave )CActiveScheduler();
       
    36     CleanupStack::PushL( activeScheduler );
       
    37     CActiveScheduler::Install( activeScheduler );        
       
    38     
       
    39     // Construct app.
       
    40     CHsLaunch* app = CHsLaunch::NewL();
       
    41     CleanupStack::PushL( app );        
       
    42     
       
    43     app->Activate();
       
    44     
       
    45     // Signal startup.
       
    46     RProcess::Rendezvous(KErrNone);
       
    47     
       
    48     // Start active scheduler.
       
    49     CActiveScheduler::Start();
       
    50     
       
    51     User::LeaveIfError( app->ApplicationReturnValue() );
       
    52     
       
    53     // Cleanup.
       
    54     CleanupStack::PopAndDestroy( app );
       
    55     CleanupStack::PopAndDestroy( activeScheduler );
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // E32Main
       
    60 // entry-point
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 TInt E32Main()
       
    64     {            
       
    65     TInt error = KErrNone;
       
    66     
       
    67     __UHEAP_MARK;    
       
    68     
       
    69     // Construct cleanup stack.
       
    70     CTrapCleanup* cleanupstack = CTrapCleanup::New();
       
    71     if( !cleanupstack )
       
    72         {
       
    73         error  = KErrNoMemory;
       
    74         }
       
    75     
       
    76     // Call next phase of startup.
       
    77     if( !error )
       
    78         {
       
    79         TRAP( error, RunAppL() );
       
    80         }
       
    81     
       
    82     // Cleanup.
       
    83     delete cleanupstack;
       
    84     cleanupstack = NULL;
       
    85     
       
    86     __UHEAP_MARKEND;
       
    87     
       
    88     return error;
       
    89     }
       
    90 
       
    91 // ========================= MEMBER FUNCTIONS ==================================
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CHsLaunch::NewL()
       
    95 // Two-phased constructor.
       
    96 // -----------------------------------------------------------------------------
       
    97 CHsLaunch* CHsLaunch::NewL()
       
    98     {
       
    99     CHsLaunch* self = new ( ELeave ) CHsLaunch();
       
   100     CleanupStack::PushL( self );
       
   101     self->ConstructL();
       
   102     CleanupStack::Pop( self );
       
   103     return self;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CHsLaunch::ConstructL()
       
   108 // Symbian 2nd phase constructor can leave.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CHsLaunch::ConstructL()
       
   112     {
       
   113     iShutdownMonitor = CHsShutdownMonitor::NewL( *this );
       
   114     iShutdownMonitor->StartMonitor();
       
   115     
       
   116     // Create property to pub/sub.    
       
   117     TInt error = RProperty::Define( KPSCrashCountKey,
       
   118                                     RProperty::EInt,
       
   119                                     KPSReadPolicy,
       
   120                                     KPSWritePolicy );    
       
   121     if( error == KErrNone )
       
   122         {
       
   123         // Init it to zero.
       
   124         error = RProperty::Set( KPSCategoryUid, KPSCrashCountKey, 0 );
       
   125         }                
       
   126     else if( error == KErrAlreadyExists )
       
   127         {
       
   128         error = KErrNone;        
       
   129         }        
       
   130     
       
   131     User::LeaveIfError( error );
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CHsLaunch::CHsLaunch()
       
   136 // C++ default constructor can NOT contain any code, that might leave.
       
   137 // -----------------------------------------------------------------------------
       
   138 CHsLaunch::CHsLaunch() :
       
   139         CActive( EPriorityStandard ),
       
   140         iApplicationReturnValue( KErrNone )
       
   141     {
       
   142     CActiveScheduler::Add( this );
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CHsLaunch::~CHsLaunch()
       
   147 // Destructor.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 CHsLaunch::~CHsLaunch()
       
   151     {
       
   152     Cancel();
       
   153     
       
   154     delete iProcessMonitor;
       
   155     iProcessMonitor = NULL;
       
   156     
       
   157     delete iShutdownMonitor;
       
   158     iShutdownMonitor = NULL;
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CHsLaunch::Activate()
       
   163 // Set active object to be ran
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CHsLaunch::Activate()
       
   167     {   
       
   168     SetActive();
       
   169     TRequestStatus* tmp = &iStatus;
       
   170     User::RequestComplete( tmp, KErrNone );
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CHsLaunch::ApplicationReturnValue()
       
   175 // Get application return value
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 TInt CHsLaunch::ApplicationReturnValue()
       
   179     {
       
   180     return iApplicationReturnValue;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CHsLaunch::RunL
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CHsLaunch::RunL()
       
   188     {    
       
   189     // Create app or connect to existing.
       
   190     TInt processExisted = EFalse;
       
   191     
       
   192     RProcess process;
       
   193     TInt processError = process.Create( KHsExeName, KNullDesC );
       
   194     if( processError == KErrAlreadyExists )
       
   195         {        
       
   196         processError = process.Open( KHsProcessName, EOwnerProcess );
       
   197         processExisted = ETrue;
       
   198         }
       
   199     
       
   200     TInt monitorError = KErrNone;
       
   201     if( processError == KErrNone )
       
   202         {
       
   203         TRAP( monitorError, InitProcessMonitorL( process.Id() ) );
       
   204         }
       
   205     
       
   206     if( processError == KErrNone &&
       
   207         !processExisted )
       
   208         {
       
   209         // Make sure process is started even if monitor startup
       
   210         // fails. This will assure that process is not left in
       
   211         // suspended state. Resume can not be called for
       
   212         // already running process (will cause KERN-EXEC 46).
       
   213         process.Resume();
       
   214         }
       
   215                      
       
   216     process.Close();
       
   217     
       
   218     if( processError != KErrNone ||
       
   219         monitorError != KErrNone )
       
   220         {
       
   221         /* Error in this method is critical and it might cause
       
   222          * endless active scheduler loop if no active objects are
       
   223          * not triggered. Therefore shutdown. This application
       
   224          * will be restarted by startup sw. */
       
   225         ShutdownApp( KErrGeneral );
       
   226         }
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CHsLaunch::DoCancel()
       
   231 // From CActive.
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CHsLaunch::DoCancel()
       
   235     {
       
   236     if( iProcessMonitor )
       
   237         {
       
   238         iProcessMonitor->Cancel();
       
   239         }
       
   240     
       
   241     if( iShutdownMonitor )
       
   242         {
       
   243         iShutdownMonitor->Cancel();
       
   244         }
       
   245     }      
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CHsLaunch::RunError()
       
   249 // From CActive.
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 TInt CHsLaunch::RunError( TInt aError )
       
   253     {
       
   254     return aError;
       
   255     } 
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CHsLaunch::ProcessEndedL()
       
   259 // From MHsProcessMonitorObserver
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 void CHsLaunch::ProcessEnded( const TExitType& aExitType,
       
   263                               const TInt /*aExitReason*/,
       
   264                               const TExitCategoryName& /*aExitCategory*/ )
       
   265     {    
       
   266     // Only respond to panic. EExitTerminate and EExitKill are ignored.
       
   267     if( aExitType != EExitPanic )
       
   268         {
       
   269         return;
       
   270         }
       
   271     
       
   272     TInt crashCount = 0;
       
   273     TInt error = RProperty::Get( KPSCategoryUid,
       
   274                                  KPSCrashCountKey,
       
   275                                  crashCount );
       
   276     
       
   277     if( error == KErrNone )
       
   278         {
       
   279         crashCount++;
       
   280         error = RProperty::Set( KPSCategoryUid,
       
   281                                 KPSCrashCountKey,
       
   282                                 crashCount );
       
   283         }
       
   284     
       
   285     if( error == KErrNone )
       
   286         {
       
   287         User::After( KSleepOnRetry ); 
       
   288         Activate();
       
   289         }
       
   290     else
       
   291         {    
       
   292         ShutdownApp( error );
       
   293         }
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CHsLaunch::ProcessMonitoringErrorL()
       
   298 // From MHsProcessMonitorObserver
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 void CHsLaunch::ProcessMonitoringError( TInt aError )
       
   302     {
       
   303     /* Error in this method is critical and it might cause
       
   304      * endless active scheduler loop if no active objects are
       
   305      * not triggered. Therefore shutdown. This application
       
   306      * will be restarted by startup sw. */
       
   307     ShutdownApp( aError );
       
   308     }
       
   309 
       
   310 // -----------------------------------------------------------------------------
       
   311 // CHsLaunch::SystemShutdownEvent()
       
   312 // From MHsShutdownMonitorObserver
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CHsLaunch::SystemShutdownEvent()
       
   316     {
       
   317     // Do not shut down app. Startup app would try to restart this app.
       
   318     // Just deactivate all active objects and wait for power off.
       
   319     if( IsActive() )
       
   320         {
       
   321         Cancel();
       
   322         }
       
   323     else
       
   324         {
       
   325         DoCancel();
       
   326         }    
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CHsLaunch::InitProcessMonitorL()
       
   331 // Initialize process monitor
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 void CHsLaunch::InitProcessMonitorL( const TInt aProcessId )
       
   335     {
       
   336     if( iProcessMonitor )
       
   337         {
       
   338         delete iProcessMonitor;
       
   339         iProcessMonitor = NULL;
       
   340         }
       
   341     iProcessMonitor = CHsProcessMonitor::NewL( aProcessId, *this );    
       
   342     User::LeaveIfError( iProcessMonitor->StartMonitor() );
       
   343     }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CHsLaunch::ShutdownApp()
       
   347 // Shut down application
       
   348 // -----------------------------------------------------------------------------
       
   349 //
       
   350 void CHsLaunch::ShutdownApp( const TInt aApplicationReturnValue )
       
   351     {
       
   352     if( IsActive() )
       
   353         {
       
   354         Cancel();
       
   355         }
       
   356     else
       
   357         {
       
   358         DoCancel();
       
   359         }
       
   360     
       
   361     iApplicationReturnValue = aApplicationReturnValue;    
       
   362     CActiveScheduler::Stop();
       
   363     }
       
   364 
       
   365 // End of File