contentpublishingsrv/contentpublishingserver/cpserver/src/cpactionhandlerthread.cpp
changeset 73 4bc7b118b3df
parent 66 32469d7d46ff
child 80 397d00875918
child 81 5ef31a21fdd5
equal deleted inserted replaced
66:32469d7d46ff 73:4bc7b118b3df
     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:  
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cpactionhandlerthread.h"
       
    19 #include "cpserverdef.h"
       
    20 #include "cpserveractionmanager.h"
       
    21 #include "cpglobals.h"
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // 
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 TInt CPExecutionThreadFunction( TAny* aParam )
       
    28     {
       
    29     ASSERT( aParam );
       
    30     CCPActionHandlerThread* actionHandlerThread =
       
    31             reinterpret_cast<CCPActionHandlerThread*>( aParam );
       
    32     return actionHandlerThread->ThreadFunction();
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // 
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CCPActionHandlerThread* CCPActionHandlerThread::NewL()
       
    40     {
       
    41     CCPActionHandlerThread* self = NewLC();
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // 
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CCPActionHandlerThread* CCPActionHandlerThread::NewLC()
       
    51     {
       
    52     CCPActionHandlerThread* self = new ( ELeave ) CCPActionHandlerThread;
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // 
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CCPActionHandlerThread::~CCPActionHandlerThread()
       
    63     {
       
    64     if ( ExecutionThreadRunning() && iActionExecutorAO )
       
    65         {
       
    66         // stop action execution thread
       
    67         
       
    68         // request notification when action execution thread terminates
       
    69         TRequestStatus exitStatus;
       
    70         iActionExecThread.Logon( exitStatus );
       
    71         
       
    72         // passing NULL as action parameters will cause the action executor to
       
    73         // stop active scheduler in command execution thread
       
    74         iActionExecutorAO->SetActionParams( NULL );
       
    75         // ask action executor to execute the action
       
    76         TRequestStatus* actionExecutorAOStatus =
       
    77             iActionExecutorAO->RequestStatus();
       
    78         iActionExecThread.RequestComplete( actionExecutorAOStatus, KErrNone );
       
    79         
       
    80         // wait for the action execution thread to terminate
       
    81         User::WaitForRequest( exitStatus );
       
    82         }
       
    83     iActionExecThread.Close();
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // 
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CCPActionHandlerThread::ExecuteL(
       
    91         const CLiwGenericParamList& aActionParams )
       
    92     {
       
    93     StartExecutionThreadIfNeededL();
       
    94     
       
    95     ASSERT( iActionExecutorAO );
       
    96     iActionExecutorAO->SetActionParams( &aActionParams );
       
    97     
       
    98     // create rendezvous so that we are notified when action
       
    99     // execution finishes
       
   100     TRequestStatus actionExecutionStatus = KRequestPending;
       
   101     iActionExecThread.Rendezvous( actionExecutionStatus );
       
   102     
       
   103     // request action execution by completing request for the AO running
       
   104     // in the action execution thread 
       
   105     TRequestStatus* actionExecutorAOStatus =
       
   106             iActionExecutorAO->RequestStatus();
       
   107     iActionExecThread.RequestComplete( actionExecutorAOStatus, KErrNone );
       
   108     
       
   109     // wait until action execution finishes
       
   110     User::WaitForRequest( actionExecutionStatus );
       
   111 
       
   112     TInt err = actionExecutionStatus.Int();
       
   113     // please note that checking actionExecutionStatus is not a reliable way of
       
   114     // determining whether everything is OK because if plugin panics with 
       
   115     // reason (panic number) KErrNone, actionExecutionStatus will contain KErrNone
       
   116     if ( !ExecutionThreadRunning() )
       
   117         {
       
   118         // action execution thread died,
       
   119         // iActionExecutorAO pointer is no longer valid
       
   120         iActionExecutorAO = NULL;
       
   121         if ( err >= 0 )
       
   122             {
       
   123             err = KErrDied;
       
   124             }
       
   125         }
       
   126     else
       
   127         {
       
   128         iActionExecutorAO->SetActionParams( NULL );
       
   129         }
       
   130     
       
   131     User::LeaveIfError( err );
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // 
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138  void CCPActionHandlerThread::StartExecutionThreadIfNeededL()
       
   139     {
       
   140     if ( !ExecutionThreadRunning() )
       
   141         {
       
   142         TBuf<KThreadNameLimit> threadName;
       
   143         if ( iActionExecThreadStarted )
       
   144             {
       
   145             iActionExecThread.Close();
       
   146             }
       
   147         
       
   148         // create action execution thread
       
   149         threadName.Format( KCPServerPluginThreadName, ++iThreadNum );
       
   150         TInt err = iActionExecThread.Create( threadName,
       
   151                 CPExecutionThreadFunction,
       
   152                 KDefaultStackSize,
       
   153                 KCPServerPluginThreadMinHeapSize,
       
   154                 KCPServerPluginThreadMaxHeapSize,
       
   155                 reinterpret_cast<TAny*>( this ) );
       
   156         User::LeaveIfError( err );
       
   157         
       
   158         // create rendezvous so that we are notified when the thread
       
   159         // has started
       
   160         TRequestStatus threadStatus;
       
   161         iActionExecThread.Rendezvous( threadStatus );
       
   162         
       
   163         // start the action execution thread
       
   164         iActionExecThread.Resume();
       
   165         
       
   166         // wait for the thread to start
       
   167         User::WaitForRequest( threadStatus );
       
   168         
       
   169         User::LeaveIfError( threadStatus.Int() );
       
   170         
       
   171         iActionExecThreadStarted = ETrue;
       
   172         ASSERT( ExecutionThreadRunning() );
       
   173         }
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // RThread::ExitType() returns EExitPending if the thread is alive or has not
       
   178 // been started yet. There is a member variable, iActionExecThreadStarted
       
   179 // which tells if the thread was started or not. Combining the information
       
   180 // from the member variable and RThread::ExitType() allows us to determine if
       
   181 // the thread is running (alive).
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184  TBool CCPActionHandlerThread::ExecutionThreadRunning()
       
   185     {
       
   186     TBool actionExecThreadDidNotDie =
       
   187             iActionExecThread.ExitType() == EExitPending;
       
   188 
       
   189     TBool threadRunning =
       
   190             iActionExecThreadStarted && actionExecThreadDidNotDie;
       
   191     
       
   192     // if the thread is alive, it must have created an active object for
       
   193     // executing actions in plugins; verify that it is the case
       
   194     ASSERT( !threadRunning || ( threadRunning && iActionExecutorAO ));
       
   195     
       
   196     return threadRunning;
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------------------------
       
   200 // 
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203  CCPActionHandlerThread::CCPActionHandlerThread()
       
   204     {
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // 
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CCPActionHandlerThread::ConstructL()
       
   212     {
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // 
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 TInt CCPActionHandlerThread::ThreadFunction()
       
   220     {
       
   221     __UHEAP_MARK;
       
   222 
       
   223     CServerEikonEnv* env = new CServerEikonEnv;
       
   224     if ( !env )
       
   225         {
       
   226         return KErrNoMemory;
       
   227         }
       
   228     CServerAppUi* ui = new CServerAppUi;
       
   229     if ( !ui )
       
   230         {
       
   231         delete env;
       
   232         return KErrNoMemory;
       
   233         }
       
   234     
       
   235     TRAPD( err,
       
   236         env->ConstructL();
       
   237         env->DisableExitChecks( ETrue );
       
   238         ui->ConstructL();
       
   239 
       
   240         iActionExecutorAO = CActionExecutorAO::NewL();
       
   241         
       
   242         // notify the main thread that action execution thread
       
   243         // has successfully started
       
   244         RThread::Rendezvous( KErrNone );
       
   245 
       
   246         CActiveScheduler::Start();
       
   247     );
       
   248 
       
   249     delete iActionExecutorAO;
       
   250     iActionExecutorAO = NULL;
       
   251 
       
   252     ui->PrepareToExit();
       
   253     env->DestroyEnvironment();
       
   254     
       
   255     __UHEAP_MARKEND;
       
   256     
       
   257     return err;
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CContentHarvesterEikonEnv::DestroyEnvironment
       
   262 // 
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CCPActionHandlerThread::CServerEikonEnv::DestroyEnvironment()
       
   266     {
       
   267     CEikonEnv::DestroyEnvironment( );
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CContentHarvesterEikonEnv::ConstructL
       
   272 // 
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CCPActionHandlerThread::CServerEikonEnv::ConstructL()
       
   276     {
       
   277     CEikonEnv::ConstructL( EFalse );
       
   278     SetAutoForwarding( ETrue );
       
   279     User::SetPriorityControl( EFalse );
       
   280     }
       
   281 // -----------------------------------------------------------------------------
       
   282 // CContentHarvesterAppUi::~CContentHarvesterAppUi
       
   283 // 
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 CCPActionHandlerThread::CServerAppUi::~CServerAppUi()
       
   287     {
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // CContentHarvesterAppUi::ConstructL
       
   292 // 
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CCPActionHandlerThread::CServerAppUi::ConstructL()
       
   296     {
       
   297     CEikAppUi::BaseConstructL( ENoAppResourceFile | ENoScreenFurniture );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------------------------
       
   301 // 
       
   302 // ---------------------------------------------------------------------------
       
   303 //
       
   304 CCPActionHandlerThread::CActionExecutorAO*
       
   305 CCPActionHandlerThread::CActionExecutorAO::NewL()
       
   306     {
       
   307     CActionExecutorAO* self = NewLC();
       
   308     CleanupStack::Pop( self );
       
   309     return self;
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // 
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 CCPActionHandlerThread::CActionExecutorAO*
       
   317 CCPActionHandlerThread::CActionExecutorAO::NewLC()
       
   318     {
       
   319     CActionExecutorAO* self = new ( ELeave ) CActionExecutorAO();
       
   320     CleanupStack::PushL( self );
       
   321     self->ConstructL();
       
   322     return self;
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // 
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 CCPActionHandlerThread::CActionExecutorAO::~CActionExecutorAO()
       
   330     {
       
   331     delete iActionManager;
       
   332     }
       
   333         
       
   334 // ---------------------------------------------------------------------------
       
   335 // Any code executed in this method must not leave!
       
   336 // This way we avoid implementing RunError while making sure that 
       
   337 // Rendezvous will always be called
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CCPActionHandlerThread::CActionExecutorAO::RunL()
       
   341     {
       
   342     if( iActionParams )
       
   343         {
       
   344         // execute action
       
   345         TRAPD( err, iActionManager->ExecuteL( *iActionParams ) );
       
   346         
       
   347         // notify the main thread that action execution has finished
       
   348         RThread::Rendezvous( err );
       
   349     
       
   350         // wait until another action execution is requested
       
   351         iStatus = KRequestPending;
       
   352         SetActive();
       
   353         }
       
   354     else
       
   355         {
       
   356         CActiveScheduler::Stop();
       
   357         }
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // 
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CCPActionHandlerThread::CActionExecutorAO::DoCancel()
       
   365     {
       
   366     TRequestStatus* myRequestStatus = &iStatus;
       
   367     User::RequestComplete( myRequestStatus, KErrCancel );
       
   368     }
       
   369 
       
   370 // ---------------------------------------------------------------------------
       
   371 // 
       
   372 // ---------------------------------------------------------------------------
       
   373 //
       
   374 TRequestStatus* CCPActionHandlerThread::CActionExecutorAO::RequestStatus()
       
   375     {
       
   376     return &iStatus;
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // 
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 void CCPActionHandlerThread::CActionExecutorAO::SetActionParams(
       
   384         const CLiwGenericParamList* aActionParams )
       
   385     {
       
   386     iActionParams = aActionParams;
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // 
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 CCPActionHandlerThread::CActionExecutorAO::CActionExecutorAO()
       
   394     : CActive( CActive::EPriorityLow )
       
   395     {
       
   396     CActiveScheduler::Add( this );
       
   397 
       
   398     // wait until action execution is requested
       
   399     iStatus = KRequestPending;
       
   400     SetActive();
       
   401     }
       
   402 
       
   403 // ---------------------------------------------------------------------------
       
   404 // 
       
   405 // ---------------------------------------------------------------------------
       
   406 //
       
   407 void CCPActionHandlerThread::CActionExecutorAO::ConstructL()
       
   408     {
       
   409     iActionManager = CCPActionManager::NewL();
       
   410     }