srsf/nssvascontacthdlr/src/nssvasccontacthandler.cpp
branchRCL_3
changeset 23 e36f3802f733
equal deleted inserted replaced
22:cad71a31b7fc 23:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-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:  Responsible for maintaining synchronization between the contact DB
       
    15 *               and the VAS DB
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "nssvasccontacthandler.h"
       
    22 #include "nssvasccontacthandlerimpl.h"
       
    23 #include "rubydebug.h"
       
    24 
       
    25 // CONSTANTS
       
    26 // Thread name
       
    27 _LIT( KCHThreadName, "NssVasCHThread" );
       
    28 // Thread priority
       
    29 const TThreadPriority KCHThreadPriority = EPriorityAbsoluteLow;
       
    30 // Thread heap min-max
       
    31 const TInt KCHHeapMinSize = KMinHeapSize;
       
    32 const TInt KCHHeapMaxSize = 0x300000;
       
    33 
       
    34 /**
       
    35 *  Utility class to handle cleanup of ContactHandler thread
       
    36 */
       
    37 NONSHARABLE_CLASS( CContactHandlerStopper ) : public CAsyncOneShot
       
    38     {
       
    39     public:
       
    40         static CContactHandlerStopper* NewL( TInt aPriority );
       
    41         virtual ~CContactHandlerStopper();
       
    42         void StopCH();
       
    43     protected:
       
    44         void RunL();
       
    45         void DoCancel();
       
    46     private:
       
    47         CContactHandlerStopper( TInt aPriority );
       
    48     };
       
    49 
       
    50 // ---------------------------------------------------------
       
    51 // CNssContactHandler::CNssContactHandler
       
    52 // C++ constructor.
       
    53 // ---------------------------------------------------------
       
    54 //       
       
    55 CNssContactHandler::CNssContactHandler():
       
    56     iStopper( NULL )
       
    57     {
       
    58     // Nothing
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 // CNssContactHandler::NewL
       
    63 // Two-phased constructor.
       
    64 // ---------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CNssContactHandler* CNssContactHandler::NewL()
       
    67     {
       
    68     RUBY_DEBUG_BLOCK("CNssContactHandler::NewL");
       
    69     CNssContactHandler* self = new (ELeave) CNssContactHandler();
       
    70 
       
    71     CleanupStack::PushL( self );
       
    72     self->ConstructL();
       
    73     CleanupStack::Pop( self );
       
    74 
       
    75     return self;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CNssContactHandler::ConstructL
       
    80 // Second-phase constructor.
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 void CNssContactHandler::ConstructL()
       
    84     {
       
    85     // Create processing thread
       
    86     RThread chThread;
       
    87 
       
    88     iThreadNameBuf.Append( _L( "[" ) );
       
    89     // Add this pointer to the thread name to get it unique
       
    90     iThreadNameBuf.AppendNum( ( TInt ) this );
       
    91     iThreadNameBuf.Append( _L( "]" ) );
       
    92     iThreadNameBuf.Append( KCHThreadName );
       
    93 
       
    94     //RUBY_DEBUG1( "CNssContactHandler::ConstructL starting a thread with name: %S", iThreadNameBuf );
       
    95 
       
    96     User::LeaveIfError( chThread.Create( iThreadNameBuf, CHThreadFunction, KDefaultStackSize, KCHHeapMinSize, KCHHeapMaxSize, this ) );
       
    97 
       
    98     chThread.SetPriority( KCHThreadPriority );
       
    99     chThread.Resume();
       
   100     chThread.Close();    
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // CNssContactHandler::~CNssContactHandler
       
   105 // Destructor.
       
   106 // ---------------------------------------------------------
       
   107 //
       
   108 EXPORT_C CNssContactHandler::~CNssContactHandler()
       
   109     {
       
   110     RUBY_DEBUG0("CNssContactHandlerImplementation::~CNssContactHandler");
       
   111 
       
   112     // Check if Contact Handler thread is still alive
       
   113     RThread chThread;
       
   114 
       
   115     _LIT( KProcessThreadDelimeter, "::" );
       
   116     TBuf<256> fullThreadName;
       
   117 	RProcess currentProcess;
       
   118     fullThreadName.Append( currentProcess.FullName() );
       
   119     fullThreadName.Append( KProcessThreadDelimeter );
       
   120     fullThreadName.Append( iThreadNameBuf );
       
   121 
       
   122     if ( chThread.Open( fullThreadName ) == KErrNone )
       
   123         {
       
   124         // Increase the thread priority to get the cleanup done quickly
       
   125         // Fixes priority inversion problem
       
   126         chThread.SetPriority( RThread().Priority() );
       
   127         RUBY_DEBUG1("CNssContactHandlerImplementation::~CNssContactHandler increased priority to: %d", chThread.Priority() );
       
   128 
       
   129         if ( iStopper )
       
   130             {
       
   131             // Start AO which stops the ActiveScheduler in ContactHandler thread
       
   132             // -> ContactHandler thread will clean itself
       
   133             iStopper->StopCH();
       
   134             iStopper = NULL;
       
   135 
       
   136             // Wait that cleanup of ContactHandler thread has been done properly
       
   137             TRequestStatus threadCompletion;
       
   138             chThread.Rendezvous( threadCompletion );
       
   139             User::WaitForRequest( threadCompletion );
       
   140             }
       
   141 
       
   142         chThread.Kill( KErrNone );
       
   143 
       
   144         chThread.Close();
       
   145         }
       
   146     else
       
   147         {
       
   148         //RUBY_ERROR1( "CNssContactHandlerImplementation::~CNssContactHandler failed to open thread: %S", iThreadNameBuf );
       
   149         }
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------
       
   153 // CNssContactHandler::DisableEventHandling
       
   154 // Disables the event handling.
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CNssContactHandler::DisableEventHandling()
       
   158    {
       
   159    RUBY_DEBUG0( "CNssContactHandler::DisableEventHandling" );
       
   160    }
       
   161 
       
   162 // ---------------------------------------------------------
       
   163 // CNssContactHandler::EnableEventHandling
       
   164 // Enables event handling
       
   165 //---------------------------------------------------------
       
   166 //
       
   167 EXPORT_C void CNssContactHandler::EnableEventHandling()
       
   168    {
       
   169    RUBY_DEBUG0( "CNssContactHandler::EnableEventHandling" );
       
   170    }
       
   171 
       
   172 // ---------------------------------------------------------
       
   173 // CNssContactHandler::SetStopperPointer
       
   174 // Sets the pointer to stopper object.
       
   175 //---------------------------------------------------------
       
   176 //
       
   177 void CNssContactHandler::SetStopperPointer( CContactHandlerStopper* aStopper )
       
   178     {
       
   179     iStopper = aStopper;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------
       
   183 // CNssContactHandler::CHThreadFunction
       
   184 // ContactHandler thread function.
       
   185 // ---------------------------------------------------------
       
   186 //
       
   187 TInt CNssContactHandler::CHThreadFunction( TAny* aParams )
       
   188     {
       
   189     TInt result = KErrNone;
       
   190     // CleanupStack creation
       
   191     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   192 
       
   193     // ActiveScheduler creation
       
   194     CActiveScheduler* scheduler = NULL;
       
   195     TRAPD( error, scheduler = new (ELeave) CActiveScheduler() );
       
   196     if ( error == KErrNone )
       
   197         {
       
   198         // Take created ActiveScheduler into use
       
   199         CActiveScheduler::Install( scheduler );
       
   200 
       
   201         // Create utility object which is used when stopping this thread
       
   202         CNssContactHandler* self = ( CNssContactHandler* )aParams;
       
   203         CContactHandlerStopper* stopper = NULL;
       
   204         TRAP( error, stopper  = CContactHandlerStopper::NewL( EPriorityNormal ) );
       
   205         if ( error == KErrNone )
       
   206             {
       
   207             self->SetStopperPointer( stopper );
       
   208 
       
   209             // Implementation of VAS ContactHandler
       
   210             CNssContactHandlerImplementation* impl = NULL;
       
   211             TRAP( error, impl = CNssContactHandlerImplementation::NewL() );
       
   212 
       
   213             if ( error == KErrNone )
       
   214                 {
       
   215                 CActiveScheduler::Start();
       
   216 
       
   217                 delete impl;
       
   218                 self->SetStopperPointer( NULL );
       
   219                 delete stopper;
       
   220                 delete scheduler;
       
   221                 delete cleanupStack;
       
   222 
       
   223                 // Signal that cleanup of ContactHandler thread is now ok
       
   224                 RThread().Rendezvous( KErrNone );
       
   225 
       
   226                 result = KErrNone;                
       
   227                 }  // If created implementation
       
   228             else
       
   229                 {
       
   230                 RUBY_ERROR1( "CNssContactHandler::CHThreadFunction Failed to create CH implementation. Error [%d]",
       
   231                             error );
       
   232                             
       
   233                 self->SetStopperPointer( NULL );
       
   234                 delete stopper;
       
   235                 delete scheduler;
       
   236                 result = error;
       
   237                 }
       
   238                 
       
   239             // Self pointer not needed anymore
       
   240             self = NULL;
       
   241 
       
   242             }  // if created stopper
       
   243         else 
       
   244             {
       
   245             RUBY_ERROR1( "CNssContactHandler::CHThreadFunction Failed to create CContactHandlerStopper. Error [%d]", 
       
   246                         error );
       
   247             delete scheduler;
       
   248             result = error;
       
   249             }
       
   250         
       
   251         }  // if created scheduler
       
   252    else 
       
   253         {
       
   254         RUBY_ERROR1( "CNssContactHandler::CHThreadFunction Failed to create the active scheduler. Error [%d]", 
       
   255                     error );
       
   256         result = error;
       
   257         }
       
   258     
       
   259     return result;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // CContactHandlerStopper::NewL
       
   264 // Two-phased constructor.
       
   265 // ---------------------------------------------------------
       
   266 //
       
   267 CContactHandlerStopper* CContactHandlerStopper::NewL( TInt aPriority )
       
   268 	{
       
   269 	CContactHandlerStopper* self = new (ELeave) CContactHandlerStopper( aPriority );
       
   270     return self;
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------
       
   274 // CContactHandlerStopper::~CContactHandlerStopper
       
   275 // Desctructor.
       
   276 // ---------------------------------------------------------
       
   277 //
       
   278 CContactHandlerStopper::~CContactHandlerStopper()
       
   279 	{
       
   280     Cancel();
       
   281 	}
       
   282 
       
   283 // ---------------------------------------------------------
       
   284 // CContactHandlerStopper::CContactHandlerStopper
       
   285 // C++ constructor.
       
   286 // ---------------------------------------------------------
       
   287 //
       
   288 CContactHandlerStopper::CContactHandlerStopper( TInt aPriority ) : 
       
   289     CAsyncOneShot( aPriority )
       
   290 	{
       
   291 	// Nothing
       
   292 	}
       
   293 
       
   294 // ---------------------------------------------------------
       
   295 // CContactHandlerStopper::StopCH
       
   296 // Method to request stopping.
       
   297 // ---------------------------------------------------------
       
   298 //
       
   299 void CContactHandlerStopper::StopCH()
       
   300     {
       
   301     Call();
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------
       
   305 // CContactHandlerStopper::RunL
       
   306 // From CActive.
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 void CContactHandlerStopper::RunL()
       
   310     {
       
   311     CActiveScheduler::Stop();
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------
       
   315 // CContactHandlerStopper::DoCancel
       
   316 // From CActive.
       
   317 // ---------------------------------------------------------
       
   318 //
       
   319 void CContactHandlerStopper::DoCancel()
       
   320     {
       
   321     // Nothing
       
   322     }
       
   323 
       
   324 // End of file