contentpublishingsrv/contentpublishingserver/cpserver/src/cpserversession.cpp
changeset 0 79c6a41cd166
child 11 bd874ee5e5e2
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     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:  Content Publisher Server Session
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32mem.h>
       
    21 // for CleanupResetAndDestroyPushL
       
    22 #include <mmf/common/mmfcontrollerpluginresolver.h>
       
    23 #include <liwgenericparam.h>
       
    24 
       
    25 #include "cpserversession.h"
       
    26 #include "cpliwmap.h"
       
    27 #include "cpublisherregistrymap.h"
       
    28 #include "cpsecuritypolicy.h"
       
    29 #include "cpserverdef.h"
       
    30 #include "cpdebug.h"
       
    31 #include "cpserver.h"
       
    32 #include "cpserverdatamanager.h"
       
    33 #include "cpactionhandlerthread.h"
       
    34 #include "cpnotificationhandler.h"
       
    35 
       
    36 
       
    37 using namespace LIW;
       
    38 
       
    39 // ================= MEMBER FUNCTIONS =======================
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CCPServerSession::NewL
       
    43 // Two-phased constructor.
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 CCPServerSession* CCPServerSession::NewL( TPointersForSession& aPasser )
       
    47     {
       
    48     CCPServerSession* self = CCPServerSession::NewLC( aPasser );
       
    49     CleanupStack::Pop( self ) ;
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CCPServerSession::NewLC
       
    55 // Two-phased constructor.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CCPServerSession* CCPServerSession::NewLC( TPointersForSession& aPasser )
       
    59     {
       
    60     CCPServerSession* self = new ( ELeave ) CCPServerSession();
       
    61     CleanupStack::PushL( self );
       
    62     self->ConstructL( aPasser ) ;
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CCPServerSession::~CCPServerSession
       
    68 // Destructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CCPServerSession::~CCPServerSession()
       
    72     {
       
    73     //remove notification handler from an array of sessions in data manager
       
    74     if ( iNotificationHandler && iDataManager )
       
    75         {
       
    76         iDataManager->RemoveObserver( iNotificationHandler );
       
    77         }
       
    78     if ( isRegister && iNotificationHandler )
       
    79         {
       
    80         iNotificationHandler->ErrorComplete( KErrCancel );
       
    81         }
       
    82     if ( iServer )
       
    83         {
       
    84         iServer->RemoveSession( );
       
    85         }
       
    86     delete iNotificationHandler;
       
    87     delete iParamList;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CCPServerSession::CCPServerSession
       
    92 // C++ default constructor can NOT contain any code, that
       
    93 // might leave.
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CCPServerSession::CCPServerSession() :
       
    97     CSession2()
       
    98     {
       
    99 
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CCPServerSession::ConstructL
       
   104 // Symbian 2nd phase constructor can leave.
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 void CCPServerSession::ConstructL( TPointersForSession& aPasser )
       
   108     {
       
   109     CP_DEBUG( _L8("CCPServerSession::ConstructL()") );
       
   110     iDataManager = aPasser.iDataManager;
       
   111     iServer = aPasser.iServer;
       
   112     iActionHandlerThread = aPasser.iActionHandlerThread;
       
   113     iServer->AddSession( );
       
   114     isRegister = EFalse;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CCPServerSession::ServiceL
       
   119 // Handle client requests.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CCPServerSession::ServiceL( const RMessage2& aMessage )
       
   123     {
       
   124     CP_DEBUG( _L8("CCPServerSession::ServiceL()" ) );
       
   125     TBool serverLocked = GetServerLock( aMessage );
       
   126     if ( serverLocked )
       
   127         {
       
   128         aMessage.Complete( KErrLocked );
       
   129         }
       
   130     else
       
   131         {
       
   132         TInt err(KErrNone);
       
   133         TBool panicedClient(EFalse);
       
   134         TRAP( err , DispatchMessageL( aMessage, panicedClient ) );
       
   135         if ( (!(aMessage.Function( ) == ECpServerRegisterObserver ) 
       
   136             || err == KErrInUse) && !panicedClient )
       
   137             {
       
   138             aMessage.Complete( err );
       
   139             }
       
   140         }
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CCPServerSession::ServiceL
       
   145 // Handle client requests.
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void CCPServerSession::DispatchMessageL( const RMessage2& aMessage, TBool& aPanicedClient )
       
   149     {
       
   150     CP_DEBUG( _L8("CCPServerSession::DispatchMessageL()" ) );
       
   151     switch ( aMessage.Function( ) )
       
   152         {
       
   153         case ECpServerAddData:
       
   154             AddDataL( aMessage );
       
   155             break;
       
   156         case ECpServerAddDataNonPersistent:
       
   157             AddDataNonPersistentL( aMessage );
       
   158             break;
       
   159         case ECpServerGetListSize:
       
   160             GetListSizeL( aMessage );
       
   161             break;
       
   162         case ECpServerGetListData:
       
   163             GetListDataL( aMessage );
       
   164             break;
       
   165         case ECpServerRemoveData:
       
   166             RemoveDataL( aMessage );
       
   167             break;
       
   168         case ECpServerRegisterObserver:
       
   169             RegisterObserverL( aMessage );
       
   170             break;
       
   171         case ECpServerAddObserver:
       
   172             AddObserverL( aMessage );
       
   173             break;
       
   174         case ECpServerRemoveObserver:
       
   175             RemoveObserverL( aMessage );
       
   176             break;
       
   177         case ECpServerUnRegisterObserver:
       
   178             UnregisterObserverL( );
       
   179             break;
       
   180         case ECpServerGetChangeInfoData:
       
   181             GetChangeInfoDataL( aMessage );
       
   182             break;
       
   183         case ECpServerExecuteAction:
       
   184             ExecuteActionL( aMessage );
       
   185             break;
       
   186         default:
       
   187             iServer->PanicClient( aMessage, ECPServerBadRequest );
       
   188             aPanicedClient = ETrue;
       
   189             break;
       
   190         }
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CCPServerSession::AddDataL
       
   195 // --------------- --------------------------------------------------------------
       
   196 //
       
   197 void CCPServerSession::AddDataL( const RMessage2& aMessage )
       
   198     {
       
   199     CP_DEBUG( _L8("CCPServerSession::AddDataL()" ) );
       
   200     CCPLiwMap* map = UnpackFromClientLC( aMessage );  
       
   201     map->SetSecurityL( aMessage );
       
   202     TBool activateSupport = map->ActivateActionSupport( );
       
   203     TUint id( 0 );
       
   204     id = iDataManager->AddDataL( *map );
       
   205     TPckg<TUint> idData(id);
       
   206     aMessage.WriteL( KReturnPosition, idData );
       
   207 	if(activateSupport)
       
   208 	    {
       
   209 	    CLiwDefaultList* list = CLiwDefaultList::NewLC( );
       
   210 	    // execute activation or deactivation action for publisher
       
   211 	    TRAP_IGNORE( GetAndExecuteActionL( map, list ) );
       
   212 	    TRAP_IGNORE( SendNotificationL( map, list ) );
       
   213 	    CleanupStack::PopAndDestroy( list );
       
   214 	    }
       
   215     CleanupStack::PopAndDestroy( map );
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CCPServerSession::AddDataNonPersistentL
       
   220 // --------------- --------------------------------------------------------------
       
   221 //
       
   222 void CCPServerSession::AddDataNonPersistentL( const RMessage2& aMessage )
       
   223     {
       
   224     CP_DEBUG( _L8("CCPServerSession::AddDataL()" ) );
       
   225     CCPLiwMap* map = UnpackFromClientLC( aMessage );
       
   226     map->SetSecurityL( aMessage );
       
   227     TUint id( 0 );
       
   228     TPckg<TUint> idData(id);
       
   229     iDataManager->AddNonPersistentDataL( map );
       
   230     aMessage.WriteL( KReturnPosition, idData );
       
   231     CleanupStack::PopAndDestroy( map );
       
   232     }
       
   233 // -----------------------------------------------------------------------------
       
   234 // CCPServerSession::GetListL
       
   235 // --------------- --------------------------------------------------------------
       
   236 //
       
   237 void CCPServerSession::GetListSizeL( const RMessage2& aMessage )
       
   238     {
       
   239     CP_DEBUG( _L8("CCPServerSession::GetListSizeL()" ) );
       
   240     CCPLiwMap* map = UnpackFromClientLC( aMessage );
       
   241     map->SetSecurityL( aMessage );
       
   242     delete iParamList;
       
   243     iParamList = NULL;
       
   244     iParamList = CLiwGenericParamList::NewL( );
       
   245     iDataManager->GetListL( *map, *iParamList );
       
   246     TPckg<TInt> sizeDes(iParamList->Size( ) );
       
   247     aMessage.WriteL( KReturnPosition, sizeDes );
       
   248     CleanupStack::PopAndDestroy( map );
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CCPServerSession::GetListDataL
       
   253 // --------------- --------------------------------------------------------------
       
   254 //
       
   255 void CCPServerSession::GetListDataL( const RMessage2& aMessage )
       
   256     {
       
   257     CP_DEBUG( _L8("CCPServerSession::GetListDataL()" ) );
       
   258     if( iParamList )
       
   259         {
       
   260         ExternalizeAndWriteToClientL( aMessage, iParamList );
       
   261         delete iParamList;
       
   262         }
       
   263     iParamList = NULL;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCPServerSession::RemoveDataL
       
   268 // --------------- --------------------------------------------------------------
       
   269 //
       
   270 void CCPServerSession::RemoveDataL( const RMessage2& aMessage )
       
   271     {
       
   272     CP_DEBUG( _L8("CCPServerSession::RemoveDataL()" ) );
       
   273     CCPLiwMap* map = UnpackFromClientLC( aMessage );
       
   274     map->SetSecurityL( aMessage );
       
   275     iDataManager->RemoveDataL( *map );
       
   276     CleanupStack::PopAndDestroy( map );
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CCPServerSession::ExecuteActionL
       
   281 // --------------- --------------------------------------------------------------
       
   282 //
       
   283 void CCPServerSession::ExecuteActionL( const RMessage2& aMessage )
       
   284     {
       
   285     TInt error(KErrNone);
       
   286     CP_DEBUG( _L8("CCPServerSession::ExecuteActionSizeL()" ) );
       
   287     CCPLiwMap* map = UnpackFromClientLC( aMessage );
       
   288     CLiwGenericParamList* paramList = CLiwGenericParamList::NewLC( );
       
   289     CLiwDefaultList* list = CLiwDefaultList::NewLC();
       
   290     error = iDataManager->GetActionL( *map, *paramList, list );
       
   291     //we notify apart from action execution result. So in fact
       
   292     //notification means there was an attempt to execute action 
       
   293     iDataManager->HandleChangeL( list );
       
   294     User::LeaveIfError( error );
       
   295     ExecuteL( *paramList );    
       
   296     CleanupStack::PopAndDestroy( list );
       
   297     CleanupStack::PopAndDestroy( paramList );
       
   298     CleanupStack::PopAndDestroy( map );
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CCPServerSession::ExecuteL
       
   303 // --------------- --------------------------------------------------------------
       
   304 //
       
   305 void CCPServerSession::ExecuteL(
       
   306         const CLiwGenericParamList& aActionParams )
       
   307     {
       
   308     for ( TInt i = 0; i < aActionParams.Count(); i++ )
       
   309     	{
       
   310 		TLiwGenericParam param;
       
   311  	    param.PushL();
       
   312     	aActionParams.AtL( i, param );
       
   313     	CLiwGenericParamList* singleAction = CLiwGenericParamList::NewLC();
       
   314     	singleAction->AppendL( param );
       
   315     	TRAP_IGNORE( iActionHandlerThread->ExecuteL( *singleAction ) );
       
   316     	CleanupStack::PopAndDestroy( singleAction );
       
   317 		CleanupStack::PopAndDestroy( &param );
       
   318     	}
       
   319     }
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CCPServerSession::RegisterObserverL
       
   323 // --------------- --------------------------------------------------------------
       
   324 //
       
   325 void CCPServerSession::RegisterObserverL( const RMessage2& aMessage )
       
   326     {
       
   327     CP_DEBUG( _L8("CCPServerSession::RegisterObserverL()" ) );
       
   328     if ( !isRegister )
       
   329         {
       
   330         if (aMessage.Int3() != KErrAlreadyExists &&
       
   331             aMessage.GetDesLength( KDescriptorPosition) < KErrNone )
       
   332             {
       
   333             iServer->PanicClient( aMessage, ECPServerBadRequest );
       
   334             User::Leave( KErrGeneral );
       
   335             }
       
   336         if ( !iNotificationHandler )
       
   337             {
       
   338             iNotificationHandler = CCPNotificationHandler::NewL(
       
   339                                                iServer->GetNotifications());
       
   340             iDataManager->AddObserverL( iNotificationHandler );
       
   341             }
       
   342         iNotificationHandler->SaveMessageL( aMessage );
       
   343         isRegister = ETrue;
       
   344         }
       
   345     else
       
   346         {
       
   347         User::Leave( KErrInUse );
       
   348         }
       
   349     }
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CCPServerSession::AddObserverL
       
   353 // --------------- --------------------------------------------------------------
       
   354 //
       
   355 void CCPServerSession::AddObserverL( const RMessage2& aMessage )
       
   356     {
       
   357     CP_DEBUG( _L8("CCPServerSession::AddObserverL()" ) );
       
   358     if( iNotificationHandler )
       
   359         {
       
   360         iNotificationHandler->AddObserverL( aMessage );
       
   361         }
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CCPServerSession::RemoveObserverL
       
   366 // --------------- --------------------------------------------------------------
       
   367 //
       
   368 void CCPServerSession::RemoveObserverL( const RMessage2& aMessage )
       
   369     {
       
   370     CP_DEBUG( _L8("CCPServerSession::RemoveObserverL()" ) );
       
   371     if( iNotificationHandler )
       
   372         {
       
   373         iNotificationHandler->RemoveObserverL( aMessage );
       
   374         }
       
   375     }
       
   376 
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CCPServerSession::UnregisterObserverL
       
   380 // --------------- --------------------------------------------------------------
       
   381 //
       
   382 void CCPServerSession::UnregisterObserverL()
       
   383     {
       
   384     CP_DEBUG( _L8("CCPServerSession::UnregisterObserverL()" ) );
       
   385     if ( isRegister )
       
   386         {
       
   387         //remove notification handler from an array of sessions in data manager
       
   388         iDataManager->RemoveObserver( iNotificationHandler );
       
   389         //reset filter for this session
       
   390         iNotificationHandler->ErrorComplete( KErrCancel );
       
   391         delete iNotificationHandler;
       
   392         iNotificationHandler = NULL;
       
   393         isRegister = EFalse;
       
   394         }
       
   395     else
       
   396         {
       
   397         User::Leave( KErrNotFound );
       
   398         }
       
   399     }
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CCPServerSession::GetChangeInfoDataL
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 void CCPServerSession::GetChangeInfoDataL( const RMessage2& aMessage )
       
   406     {
       
   407     CP_DEBUG( _L8("CCPServerSession::GetChangeInfoData()" ) );
       
   408     if( iNotificationHandler )
       
   409         {
       
   410         isRegister = EFalse;
       
   411         ExternalizeAndWriteToClientL( aMessage,
       
   412             iNotificationHandler->GetPointerToChangeInfoList( ) );
       
   413         iNotificationHandler->Reset( );
       
   414         }
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // CCPServerSession::ExternalizeAndWriteToClient
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 void CCPServerSession::ExternalizeAndWriteToClientL(
       
   422     const RMessage2& aMessage, const CLiwGenericParamList* outParamList )
       
   423     {
       
   424     CP_DEBUG( _L8("CCPServerSession::ExternalizeAndWriteToClientL()" ) );
       
   425     HBufC8* buf = HBufC8::NewLC( outParamList->Size( ) );
       
   426     TPtr8 des = buf->Des( );
       
   427     RDesWriteStream writeStream(des);
       
   428     CleanupClosePushL( writeStream );
       
   429     outParamList->ExternalizeL( writeStream );
       
   430     writeStream.CommitL( );
       
   431     aMessage.WriteL( KDescriptorPosition, des );
       
   432     CleanupStack::PopAndDestroy( &writeStream );
       
   433     CleanupStack::PopAndDestroy( buf );
       
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // 
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 CCPLiwMap* CCPServerSession::UnpackFromClientLC( const RMessage2& aMessage )
       
   441     {
       
   442     CP_DEBUG( _L8("CCPServerSession::UnpackFromClientLC()") );
       
   443     TInt deslen = aMessage.GetDesLengthL( KDescriptorPosition );
       
   444     HBufC8* buffer = HBufC8::NewLC( deslen );
       
   445     TPtr8 tempDes = buffer->Des( );
       
   446     aMessage.Read( KDescriptorPosition, tempDes );
       
   447     RDesReadStream datastrm( *buffer);
       
   448     CleanupClosePushL( datastrm );
       
   449     CCPLiwMap* inParamList = CCPLiwMap::NewL( datastrm );
       
   450     CleanupStack::PopAndDestroy( &datastrm );
       
   451     CleanupStack::PopAndDestroy( buffer );
       
   452     inParamList->PushL( );
       
   453     return inParamList;
       
   454     }
       
   455     
       
   456 // -----------------------------------------------------------------------------
       
   457 // CCPServerSession::SendNotificationL
       
   458 // -----------------------------------------------------------------------------
       
   459 //
       
   460 void CCPServerSession::SendNotificationL( CCPLiwMap* aMap, 
       
   461 		CLiwDefaultList* aNotificationList ) 
       
   462     {
       
   463     RBuf8 trigger;
       
   464     trigger.CleanupClosePushL();
       
   465     aMap->GetPropertyL( KActionTrigger, trigger );
       
   466     if( ( trigger == KActivateTrigger ) || ( trigger == KDeactivateTrigger ) )
       
   467         {
       
   468         iDataManager->HandleChangeL( aNotificationList );
       
   469         }
       
   470     CleanupStack::PopAndDestroy( &trigger );
       
   471     }
       
   472 
       
   473 // -----------------------------------------------------------------------------
       
   474 // CCPServerSession::GetAndExecuteActionL
       
   475 // --------------- --------------------------------------------------------------
       
   476 //
       
   477 void CCPServerSession::GetAndExecuteActionL( CCPLiwMap* aMap,
       
   478 		CLiwDefaultList* aNotificationList, TBool aInsertTrigger )
       
   479     {
       
   480     if (aInsertTrigger)
       
   481         {
       
   482         aMap->InsertL( KActionTrigger, TLiwVariant( KActivateTrigger ) ); 
       
   483         }
       
   484     CLiwGenericParamList* paramList = CLiwGenericParamList::NewLC();
       
   485     iDataManager->GetActionL( *aMap, *paramList, aNotificationList );
       
   486     iActionHandlerThread->ExecuteL( *paramList );    
       
   487     CleanupStack::PopAndDestroy( paramList );
       
   488     }
       
   489  
       
   490 // -----------------------------------------------------------------------------
       
   491 // CCPServerSession::GetServerLock
       
   492 // --------------- --------------------------------------------------------------
       
   493 //
       
   494 TBool CCPServerSession::GetServerLock( const RMessage2& aMessage )
       
   495 	{
       
   496 	// Allways allow to unregister
       
   497 	return ( (aMessage.Function() != ECpServerUnRegisterObserver) 
       
   498 			&& iServer->GetLock() ); 
       
   499 			
       
   500 	}
       
   501 // End of File