omadrm/drmengine/dcfrepository/server/src/DcfRepSrvSes.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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:  server session implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "DcfRepSrv.h"
       
    22 #include    "DcfRepSrvSes.h"
       
    23 #include    "DcfRepCommon.h"
       
    24 #include    "Pair.h"
       
    25 #include    <e32std.h>
       
    26 #include    <e32base.h>
       
    27 
       
    28 // EXTERNAL DATA STRUCTURES
       
    29 // EXTERNAL FUNCTION PROTOTYPES  
       
    30 // CONSTANTS
       
    31 const TUint32 KTrustedShutdownClient = 0x10205CB5;
       
    32 
       
    33 // MACROS
       
    34 #define SERVER const_cast< CDcfRepSrv* >( \
       
    35     reinterpret_cast< const CDcfRepSrv* >( Server() ) )
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 const TInt KSanityDataLengthLow = 0;
       
    39 const TInt KSanityDataLengthHigh = 32768;
       
    40 
       
    41 // MODULE DATA STRUCTURES
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 // FORWARD DECLARATIONS
       
    44 
       
    45 // #define _DRM_TESTING
       
    46 // ============================= LOCAL FUNCTIONS ===============================
       
    47 #ifdef _DRM_TESTING
       
    48 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs );
       
    49 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName );
       
    50 LOCAL_C void CreateLogL();
       
    51 LOCAL_C void WriteL( const TDesC8& aText );
       
    52 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr );
       
    53 LOCAL_C void WriteCurrentTimeL();
       
    54 #endif
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // SanitizeL
       
    58 // Performs a sanity check on length parameters
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 LOCAL_C void SanitizeL( TInt aParam )
       
    62     {
       
    63     if( aParam <= KSanityDataLengthLow || aParam > KSanityDataLengthHigh )
       
    64         {
       
    65         User::Leave(KErrArgument);
       
    66         }
       
    67     }
       
    68     
       
    69 // -----------------------------------------------------------------------------
       
    70 // Testing stuff
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 #ifdef _DRM_TESTING
       
    74 
       
    75 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    76     {
       
    77     _LIT( KLogFile , "c:\\CDcfRepSrvSes.txt" );
       
    78     WriteFileL( text , aFs , KLogFile );
       
    79     }
       
    80 
       
    81 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    82     {
       
    83     RFile file;
       
    84     TInt size;
       
    85     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    86     CleanupClosePushL( file );
       
    87     User::LeaveIfError( file.Size( size ) );
       
    88     User::LeaveIfError( file.Write( size, text ) );
       
    89     CleanupStack::PopAndDestroy(&file); //file
       
    90     }
       
    91 
       
    92 LOCAL_C void CreateLogL()
       
    93     {
       
    94     RFs fs;
       
    95     User::LeaveIfError(fs.Connect());
       
    96     CleanupClosePushL(fs);
       
    97     RFile file;
       
    98     User::LeaveIfError( file.Replace( fs , _L("c:\\CDcfRepSrvSes.txt") , EFileWrite ) );
       
    99     file.Close();
       
   100     CleanupStack::PopAndDestroy(&fs); //fs
       
   101     }
       
   102 
       
   103 LOCAL_C void WriteL( const TDesC8& aText )
       
   104     {
       
   105     RFs fs;
       
   106     User::LeaveIfError( fs.Connect() );
       
   107     CleanupClosePushL(fs);
       
   108     HBufC8* text = HBufC8::NewLC(1000);
       
   109     TPtr8 textptr(text->Des() );
       
   110     textptr.Append( aText );
       
   111     textptr.Append( _L8("\r\n") );
       
   112     WriteLogL(textptr , fs);
       
   113     CleanupStack::PopAndDestroy(text);
       
   114     CleanupStack::PopAndDestroy(&fs); //fs
       
   115     WriteCurrentTimeL();
       
   116     }
       
   117 
       
   118 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr )
       
   119     {
       
   120     _LIT8(KErr,": %d");
       
   121     HBufC8* text = HBufC8::NewLC(1000+20);
       
   122     TBuf8<20> num;
       
   123     TPtr8 textptr(text->Des());
       
   124     textptr.Append(aText);
       
   125     num.Format(KErr(),aErr);
       
   126     textptr.Append(num);
       
   127     WriteL(textptr);
       
   128     CleanupStack::PopAndDestroy(text);
       
   129     }
       
   130 
       
   131 LOCAL_C void WriteCurrentTimeL()
       
   132     {
       
   133     RFs fs;
       
   134     User::LeaveIfError( fs.Connect() );
       
   135     CleanupClosePushL(fs);
       
   136     HBufC8* text = HBufC8::NewLC(100);
       
   137     TPtr8 textptr(text->Des() );
       
   138 // Date and Time display
       
   139     TTime time;
       
   140     time.HomeTime();
       
   141     TBuf<256> dateString;
       
   142     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   143     time.FormatL(dateString,KDate);
       
   144     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   145     textptr.Append( dateString );
       
   146     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   147     time.FormatL(dateString,KTime);
       
   148     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   149     textptr.Append( dateString );
       
   150     textptr.Append(_L( "\r\n" ) );
       
   151     textptr.Append(_L( "\r\n" ) );
       
   152     WriteLogL(textptr , fs);
       
   153     CleanupStack::PopAndDestroy(text);
       
   154     CleanupStack::PopAndDestroy(&fs); //fs
       
   155     }
       
   156 #endif
       
   157 // ============================ MEMBER FUNCTIONS ===============================
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CDcfRepSrvSes::CDcfRepSrvSes
       
   161 // C++ default constructor can NOT contain any code, that
       
   162 // might leave.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 CDcfRepSrvSes::CDcfRepSrvSes()
       
   166     {
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CDcfRepSrvSes::ConstructL
       
   171 // Symbian 2nd phase constructor can leave.
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CDcfRepSrvSes::ConstructL()
       
   175     {
       
   176 #ifdef _DRM_TESTING
       
   177     CreateLogL();
       
   178     WriteL(_L8("ConstructL"));
       
   179 #endif
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // CDcfRepSrvSes::NewL
       
   184 // Two-phased constructor.
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 CDcfRepSrvSes* CDcfRepSrvSes::NewL()
       
   188     {
       
   189     CDcfRepSrvSes* self = new( ELeave ) CDcfRepSrvSes;
       
   190     
       
   191     CleanupStack::PushL( self );
       
   192     self->ConstructL();
       
   193     CleanupStack::Pop(self);
       
   194 
       
   195     return self;
       
   196     }
       
   197 
       
   198     
       
   199 // Destructor
       
   200 CDcfRepSrvSes::~CDcfRepSrvSes()
       
   201     {
       
   202 #ifdef _DRM_TESTING
       
   203     TRAPD(err,WriteL(_L8("~CDcfRepSrvSes")));
       
   204 #endif
       
   205 
       
   206     SERVER->RemoveMessageL( iMessage );
       
   207 
       
   208 
       
   209     if (iCidList)
       
   210         {
       
   211         iCidList->ResetAndDestroy();
       
   212         iCidList->Close();
       
   213         delete iCidList;    
       
   214         }
       
   215         
       
   216     if (iPairList)
       
   217         {
       
   218         iPairList->ResetAndDestroy();
       
   219         iPairList->Close();
       
   220         delete iPairList;    
       
   221         }    
       
   222     }
       
   223 
       
   224 
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CDcfRepSrvSes::ServiceL
       
   228 // This method runs DispatchL() under TRAP harness, since every error case
       
   229 // can be handled ==> no need to let this function leave.
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CDcfRepSrvSes::ServiceL( const RMessage2& aMessage )
       
   233     {
       
   234 #ifdef _DRM_TESTING
       
   235     WriteL(_L8("ServiceL"));
       
   236 #endif
       
   237     // Trap possible errors...
       
   238     TRAPD( error, DispatchL( aMessage ) );
       
   239     
       
   240     if ( error )
       
   241         {
       
   242 #ifdef _DRM_TESTING
       
   243     WriteL(_L8("ServiceL->error"),error);
       
   244 #endif
       
   245         aMessage.Complete( error );
       
   246         return;
       
   247         }
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CDcfRepSrvSes::DispatchL
       
   252 // Checks which command the user requested, and forwards the request to 
       
   253 // appropriate private method. This helps to keep the code more readable.
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 void CDcfRepSrvSes::DispatchL( const RMessage2& aMessage ) 
       
   257     {
       
   258 #ifdef _DRM_TESTING
       
   259     WriteL(_L8("DispatchL"));
       
   260 #endif
       
   261 
       
   262     TBool complete = ETrue;
       
   263     switch ( aMessage.Function() )
       
   264         {
       
   265         case EAddFile:
       
   266             AddFileL( aMessage );
       
   267             break;
       
   268         case EFullScan:
       
   269             FullScanL( aMessage );
       
   270             complete = EFalse;
       
   271             break;
       
   272         case EScanAdd:
       
   273             ScanAddL( aMessage );
       
   274             break;
       
   275         case EScanEnd:
       
   276             ScanEndL( aMessage );
       
   277             complete = EFalse;
       
   278             break;
       
   279         case EScanStart:
       
   280             ScanStartL( aMessage );
       
   281             break;
       
   282         case ESetTtidStart:
       
   283             SetTtidStartL( aMessage );
       
   284             break;
       
   285         case ESetTtidAdd:
       
   286             SetTtidAddL( aMessage );
       
   287             break;                    
       
   288         case ESetTtidEnd:
       
   289             SetTtidEndL( aMessage );
       
   290             complete = EFalse;
       
   291             break;
       
   292         case ENotifyIdle:
       
   293             NotifyIdleL( aMessage );
       
   294             complete = EFalse;
       
   295             break;
       
   296         case EStopWatching:
       
   297             StopWatchingL( aMessage );
       
   298             break;
       
   299         default:
       
   300             User::Leave( KErrNotSupported );
       
   301         }
       
   302     if (complete)
       
   303         {
       
   304         aMessage.Complete(KErrNone);
       
   305         }
       
   306     }
       
   307     
       
   308     
       
   309 // -----------------------------------------------------------------------------
       
   310 // CDcfRepSrvSes::AddFileL
       
   311 // -----------------------------------------------------------------------------
       
   312 //    
       
   313 void CDcfRepSrvSes::AddFileL( const RMessage2& aMessage )
       
   314     {
       
   315 #ifdef _DRM_TESTING
       
   316     WriteL(_L8("AddFileL"));
       
   317 #endif
       
   318 
       
   319     TFileName file;
       
   320     aMessage.ReadL( 0, file );
       
   321     SERVER->AddFileL(file);    
       
   322     }
       
   323     
       
   324 // -----------------------------------------------------------------------------
       
   325 // CDcfRepSrvSes::FullScanL
       
   326 // -----------------------------------------------------------------------------
       
   327 //    
       
   328 void CDcfRepSrvSes::FullScanL( const RMessage2& aMessage )
       
   329     {
       
   330 #ifdef _DRM_TESTING
       
   331     WriteL(_L8("FullScanL"));
       
   332 #endif
       
   333 
       
   334     TInt err = SERVER->State();
       
   335     if (err!=EStateIdle)
       
   336         {
       
   337         User::Leave(KErrServerBusy);    
       
   338         }
       
   339     else
       
   340         {
       
   341         iMessage = SERVER->SetMessageL(aMessage);
       
   342         SERVER->SetState(EStateFullScan);   
       
   343         User::LeaveIfError(SERVER->Scan());
       
   344         }
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CDcfRepSrvSes::ScanSingleL
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 void CDcfRepSrvSes::ScanAddL( const RMessage2& aMessage )
       
   352     {
       
   353 #ifdef _DRM_TESTING
       
   354     WriteL(_L8("ScanAddL"));
       
   355 #endif   
       
   356  
       
   357     TInt size = 0;
       
   358     HBufC8* cid = NULL;
       
   359     TPtr8 data(NULL,0,0);
       
   360     if (iPreviousAction == EScanStart || iPreviousAction == EScanAdd)
       
   361         {
       
   362         size = aMessage.GetDesLength( 0 );
       
   363         SanitizeL(size);
       
   364         cid = HBufC8::NewMaxLC( size );
       
   365         data.Set( cid->Des() );
       
   366         data.SetLength(size);
       
   367         aMessage.ReadL( 0, data );
       
   368         User::LeaveIfError(iCidList->Append(cid));
       
   369         CleanupStack::Pop(cid);    
       
   370         iPreviousAction = EScanAdd;
       
   371         }
       
   372     }
       
   373     
       
   374 // -----------------------------------------------------------------------------
       
   375 // CDcfRepSrvSes::ScanEndL
       
   376 // -----------------------------------------------------------------------------
       
   377 //    
       
   378 void CDcfRepSrvSes::ScanEndL( const RMessage2& aMessage )
       
   379     {
       
   380 #ifdef _DRM_TESTING
       
   381     WriteL(_L8("ScanEndL"));
       
   382 #endif  
       
   383 
       
   384     TInt err = 0;
       
   385     if (iPreviousAction == EScanAdd)
       
   386         {
       
   387         err = SERVER->State();
       
   388         if (err!=EStateIdle)
       
   389             {
       
   390             User::Leave(KErrServerBusy);    
       
   391             }
       
   392         else
       
   393             {
       
   394             iMessage = SERVER->SetMessageL(aMessage);
       
   395             SERVER->SetCidList(iCidList);
       
   396             iCidList = NULL;
       
   397             SERVER->SetState(EStateScan);
       
   398             User::LeaveIfError(SERVER->Scan());
       
   399             } 
       
   400         }
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // CDcfRepSrvSes::ScanStartL
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 void CDcfRepSrvSes::ScanStartL( const RMessage2& /*aMessage*/ )
       
   408     {
       
   409     TInt err = SERVER->State();
       
   410     
       
   411     if (err!=EStateIdle)
       
   412         {
       
   413         User::Leave(KErrServerBusy);    
       
   414         }
       
   415     else
       
   416         {
       
   417         if (iCidList)
       
   418             {
       
   419             // clean list
       
   420             iCidList->ResetAndDestroy();    
       
   421             }
       
   422         else
       
   423             {
       
   424             // initilize list
       
   425             iCidList = new (ELeave) RPointerArray<HBufC8>();        
       
   426             }
       
   427         
       
   428         } 
       
   429     iPreviousAction = EScanStart;  
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CDcfRepSrvSes::SetTtidStartL
       
   434 // -----------------------------------------------------------------------------
       
   435 //    
       
   436 void CDcfRepSrvSes::SetTtidStartL( const RMessage2& /*aMessage*/ )
       
   437     {
       
   438 #ifdef _DRM_TESTING
       
   439     WriteL(_L8("SetTtidStartL"));
       
   440 #endif    
       
   441     
       
   442     TInt err = SERVER->State();
       
   443     if (err!=EStateIdle)
       
   444         {
       
   445         User::Leave(KErrServerBusy);    
       
   446         }    
       
   447     else
       
   448         {
       
   449         if (iPairList)
       
   450             {
       
   451             // clean list
       
   452             iPairList->ResetAndDestroy();    
       
   453             }
       
   454         else
       
   455             {
       
   456             // initilize list
       
   457             iPairList = new (ELeave) RPointerArray<CPair>();        
       
   458             }        
       
   459         }
       
   460     iPreviousAction = ESetTtidStart;  
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // CDcfRepSrvSes::SetTtidAddL
       
   465 // -----------------------------------------------------------------------------
       
   466 //
       
   467 void CDcfRepSrvSes::SetTtidAddL( const RMessage2& aMessage )
       
   468     {
       
   469 #ifdef _DRM_TESTING
       
   470     WriteL(_L8("SetTtidAddL"));
       
   471 #endif 
       
   472 
       
   473     TInt size = 0;
       
   474     TPtr8 data(NULL,0,0);
       
   475     if (iPreviousAction == ESetTtidStart || iPreviousAction == ESetTtidAdd)
       
   476         {
       
   477         CPair* pair = CPair::NewL();
       
   478         CleanupStack::PushL(pair);
       
   479         
       
   480         size = aMessage.GetDesLength( 0 );
       
   481         SanitizeL( size );
       
   482         pair->iCid = HBufC8::NewMaxL( size );
       
   483         data.Set( pair->iCid->Des() );
       
   484         data.SetLength(size);
       
   485         aMessage.ReadL( 0, data );
       
   486         
       
   487         
       
   488         size = aMessage.GetDesLength( 1 );
       
   489         SanitizeL( size );
       
   490         pair->iTtid = HBufC8::NewMaxL( size );
       
   491         data.Set( pair->iTtid->Des() );
       
   492         data.SetLength(size);
       
   493         aMessage.ReadL( 1, data );
       
   494         
       
   495         User::LeaveIfError(iPairList->Append(pair));
       
   496         CleanupStack::Pop(pair);
       
   497             
       
   498         iPreviousAction = ESetTtidAdd;
       
   499         }    
       
   500     }
       
   501 
       
   502 
       
   503     
       
   504 // -----------------------------------------------------------------------------
       
   505 // CDcfRepSrvSes::SetTtidEndL
       
   506 // -----------------------------------------------------------------------------
       
   507 //    
       
   508 void CDcfRepSrvSes::SetTtidEndL( const RMessage2& aMessage )
       
   509     {
       
   510 #ifdef _DRM_TESTING
       
   511     WriteL(_L8("SetTtidEndL"));
       
   512 #endif 
       
   513 
       
   514     TInt err = 0;
       
   515     if (iPreviousAction == ESetTtidAdd)
       
   516         {
       
   517         err = SERVER->State();
       
   518         if (err!=EStateIdle)
       
   519             {
       
   520             User::Leave(KErrServerBusy);    
       
   521             }
       
   522         else
       
   523             {
       
   524             iMessage = SERVER->SetMessageL(aMessage);
       
   525             SERVER->SetPairList(iPairList);
       
   526             iPairList = NULL;
       
   527             SERVER->SetState(EStateSetTtid);
       
   528             User::LeaveIfError(SERVER->Scan());
       
   529             } 
       
   530         }   
       
   531     }
       
   532 
       
   533 // -----------------------------------------------------------------------------
       
   534 // CDcfRepSrvSes::NotifyIdleL
       
   535 // -----------------------------------------------------------------------------
       
   536 //    
       
   537 void CDcfRepSrvSes::NotifyIdleL( const RMessage2& aMessage )
       
   538     {
       
   539 #ifdef _DRM_TESTING
       
   540     WriteL(_L8("NotifyIdleL"));
       
   541 #endif
       
   542 
       
   543     TInt err = SERVER->State();
       
   544     if (err==EStateIdle)
       
   545         {
       
   546 #ifdef _DRM_TESTING
       
   547     WriteL(_L8("NotifyIdleL->Idle"));
       
   548 #endif
       
   549         aMessage.Complete(KErrNone);
       
   550         }
       
   551     else
       
   552         {
       
   553         iMessage = SERVER->SetMessageL(aMessage);
       
   554         }
       
   555     }
       
   556 
       
   557 // -----------------------------------------------------------------------------
       
   558 // CDcfRepSrvSes::StopWatchingL
       
   559 // -----------------------------------------------------------------------------
       
   560 //    
       
   561 void CDcfRepSrvSes::StopWatchingL( const RMessage2& aMessage )
       
   562     {
       
   563 #ifdef _DRM_TESTING
       
   564     WriteL(_L8("StopWatchingL"));
       
   565 #endif
       
   566     _LIT_SECURITY_POLICY_S0( sidCheck, KTrustedShutdownClient );
       
   567     if ( sidCheck.CheckPolicy( aMessage ) )
       
   568         {
       
   569         SERVER->StopWatchingL();
       
   570         }
       
   571     }
       
   572 //  End of File