omadrm/drmhelper/drmhelperserver/src/DRMHelperServer.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004-2009 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:  Implementation of the Helper Server functionality
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <DRMPermission.h> // CDRMPermission
       
    22 #include <DRMConstraint.h> // CDRMConstraint
       
    23 #include <s32file.h>
       
    24 #include <DRMNotifier.h>
       
    25 #include <DRMEventAddRemove.h>
       
    26 #include <DRMTypes.h>
       
    27 #include <bacntf.h> //CEnvironmentChangeNotifier
       
    28 #include <centralrepository.h> // link against centralrepository.lib
       
    29 
       
    30 #ifdef RD_MULTIPLE_DRIVE
       
    31 #include <DriveInfo.h>
       
    32 #endif
       
    33 
       
    34 #include "DRMEventModify.h"
       
    35 #include "DRMHelperServerInternalCRKeys.h"
       
    36 #include "DRMHelperCommon.h"
       
    37 #include "DRMHelperServer.h"
       
    38 #include "DRMHelperSession.h"
       
    39 #include "EndTimeFactory.h"
       
    40 #include "EndTimeBased.h"
       
    41 #include "IdleObserver.h"
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 
       
    47 // LOCAL CONSTANTS AND MACROS
       
    48 _LIT8(KExpiredMark , "E");
       
    49 
       
    50 #ifdef RD_MULTIPLE_DRIVE
       
    51 _LIT( KHelperServerDataStorage , "%c:DRMHS.dat" );
       
    52 #else
       
    53 _LIT( KHelperServerDataStorage , "c:DRMHS.dat" );
       
    54 #endif
       
    55 
       
    56 // #define _MEM_HEAP_USAGE
       
    57 
       
    58 
       
    59 // ============================ LOCAL FUNCTIONS ===============================
       
    60 #ifdef _DRM_TESTING
       
    61 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs );
       
    62 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName );
       
    63 LOCAL_C void CreateLogL();
       
    64 LOCAL_C void WriteL( const TDesC8& aText );
       
    65 LOCAL_C void WriteTimeL( TTime aTime );
       
    66 LOCAL_C void WriteCurrentTimeL();
       
    67 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr );
       
    68 
       
    69 #ifdef _MEM_HEAP_USAGE
       
    70 LOCAL_C void WriteHeapAllocL();
       
    71 #endif //_MEM_HEAP_USAGE
       
    72 
       
    73 #endif
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // Testing stuff
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 #ifdef _DRM_TESTING
       
    80 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    81     {
       
    82     _LIT( KLogFile , "c:\\HSLog.txt" );
       
    83     WriteFileL( text , aFs , KLogFile );
       
    84     }
       
    85 
       
    86 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    87     {
       
    88     RFile file;
       
    89     TInt size;
       
    90     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    91     CleanupClosePushL( file );
       
    92     User::LeaveIfError( file.Size( size ) );
       
    93     User::LeaveIfError( file.Write( size, text ) );
       
    94     CleanupStack::PopAndDestroy(&file); //file
       
    95     }
       
    96 
       
    97 LOCAL_C void CreateLogL()
       
    98     {
       
    99     RFs fs;
       
   100     User::LeaveIfError(fs.Connect());
       
   101     CleanupClosePushL(fs);
       
   102     RFile file;
       
   103     TInt err = file.Open( fs , _L("c:\\HSLog.txt") , EFileWrite );
       
   104     if (err)
       
   105         {
       
   106         err = file.Replace( fs , _L("c:\\HSLog.txt") , EFileWrite );
       
   107         }
       
   108     file.Close();
       
   109     CleanupStack::PopAndDestroy(&fs); //fs
       
   110     }
       
   111 
       
   112 LOCAL_C void WriteL( const TDesC8& aText )
       
   113     {
       
   114     RFs fs;
       
   115     User::LeaveIfError( fs.Connect() );
       
   116     CleanupClosePushL(fs);
       
   117     HBufC8* text = HBufC8::NewLC(1000);
       
   118     TPtr8 textptr(text->Des() );
       
   119     textptr.Append( aText );
       
   120     textptr.Append( _L8("\r\n") );
       
   121     WriteLogL(textptr , fs);
       
   122     CleanupStack::PopAndDestroy(text);
       
   123     CleanupStack::PopAndDestroy(&fs); //fs
       
   124     WriteCurrentTimeL();
       
   125 #ifdef _MEM_HEAP_USAGE
       
   126     WriteHeapAllocL();
       
   127 #endif //_MEM_HEAP_USAGE
       
   128     }
       
   129 
       
   130 LOCAL_C void WriteTimeL( TTime aTime )
       
   131     {
       
   132     RFs fs;
       
   133     User::LeaveIfError( fs.Connect() );
       
   134     CleanupClosePushL(fs);
       
   135     HBufC8* text = HBufC8::NewLC(100);
       
   136     TPtr8 textptr(text->Des() );
       
   137 // Date and Time display
       
   138     TBuf<256> dateString;
       
   139     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   140     aTime.FormatL(dateString,KDate);
       
   141     textptr.Append(_L( "\r\n\t\t\t\tData:\t" ) );
       
   142     textptr.Append( dateString );
       
   143     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   144     aTime.FormatL(dateString,KTime);
       
   145     textptr.Append(_L( "\r\n\t\t\t\tTime:\t" ) );
       
   146     textptr.Append( dateString );
       
   147     textptr.Append(_L( "\r\n" ) );
       
   148     textptr.Append(_L( "\r\n" ) );
       
   149     WriteLogL(textptr , fs);
       
   150     CleanupStack::PopAndDestroy(text);
       
   151     CleanupStack::PopAndDestroy(&fs); //fs
       
   152     }
       
   153 
       
   154 LOCAL_C void WriteCurrentTimeL()
       
   155     {
       
   156     RFs fs;
       
   157     User::LeaveIfError( fs.Connect() );
       
   158     CleanupClosePushL(fs);
       
   159     HBufC8* text = HBufC8::NewLC(100);
       
   160     TPtr8 textptr(text->Des() );
       
   161 // Date and Time display
       
   162     TTime time;
       
   163     time.HomeTime();
       
   164     TBuf<256> dateString;
       
   165     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   166     time.FormatL(dateString,KDate);
       
   167     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   168     textptr.Append( dateString );
       
   169     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   170     time.FormatL(dateString,KTime);
       
   171     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   172     textptr.Append( dateString );
       
   173     textptr.Append(_L( "\r\n" ) );
       
   174     textptr.Append(_L( "\r\n" ) );
       
   175     WriteLogL(textptr , fs);
       
   176     CleanupStack::PopAndDestroy(text);
       
   177     CleanupStack::PopAndDestroy(&fs); //fs
       
   178     }
       
   179 
       
   180 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr )
       
   181     {
       
   182     _LIT8(KErr,": %d");
       
   183     HBufC8* text = HBufC8::NewLC(1000+20);
       
   184     TBuf8<20> num;
       
   185     TPtr8 textptr(text->Des());
       
   186     textptr.Append(aText);
       
   187     num.Format(KErr(),aErr);
       
   188     textptr.Append(num);
       
   189     WriteL(textptr);
       
   190     CleanupStack::PopAndDestroy(text);
       
   191 #ifdef _MEM_HEAP_USAGE
       
   192     WriteHeapAllocL();
       
   193 #endif //_MEM_HEAP_USAGE
       
   194     }
       
   195 
       
   196 #ifdef _MEM_HEAP_USAGE
       
   197 LOCAL_C void WriteHeapAllocL()
       
   198     {
       
   199     _LIT8(KHeapUsage,"\r\n***** Heap cells allocated: %d *****\r\n");
       
   200     TBuf8<256> string;
       
   201     TInt size = 0;
       
   202     RFs fs;
       
   203     User::LeaveIfError( fs.Connect() );
       
   204     CleanupClosePushL(fs);
       
   205     User::Heap().AllocSize(size);
       
   206     string.Format(KHeapUsage(),size);
       
   207     WriteLogL(string , fs);
       
   208     CleanupStack::PopAndDestroy(&fs); //fs
       
   209     }
       
   210 #endif //_MEM_HEAP_USAGE
       
   211 
       
   212 #endif //_DRM_TESTING
       
   213 
       
   214 
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // DataFileL
       
   218 // return data filename as a TParse
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 LOCAL_C TParse DataFileL(RFs& aFs)
       
   222     {
       
   223     TBuf<256> path;
       
   224     TParse p;
       
   225     User::LeaveIfError(aFs.PrivatePath(path));
       
   226 
       
   227 #ifndef RD_MULTIPLE_DRIVE
       
   228 
       
   229     p.Set(KHelperServerDataStorage,&path,NULL);
       
   230 
       
   231 #else //RD_MULTIPLE_DRIVE
       
   232 
       
   233     TInt driveNumber( -1 );
       
   234     TChar driveLetter;
       
   235     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   236     aFs.DriveToChar( driveNumber, driveLetter );
       
   237 
       
   238     TFileName helperServerDataStorage;
       
   239     helperServerDataStorage.Format(
       
   240                     KHelperServerDataStorage, (TUint)driveLetter );
       
   241 
       
   242     p.Set( helperServerDataStorage, &path, NULL );
       
   243 
       
   244 #endif
       
   245 
       
   246     return p;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CreatePrivatePathL
       
   251 // Create private path
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 LOCAL_C void CreatePrivatePathL(RFs& aFs)
       
   255     {
       
   256 #ifdef _DRM_TESTING
       
   257     TRAPD( r , WriteL(_L8("CreatePrivatePathL")) );
       
   258 #endif
       
   259 
       
   260 #ifndef RD_MULTIPLE_DRIVE
       
   261 
       
   262     TInt err = aFs.CreatePrivatePath(EDriveC);
       
   263 
       
   264 #else //RD_MULTIPLE_DRIVE
       
   265 
       
   266     TInt driveNumber( -1 );
       
   267     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   268 
       
   269     TInt err = aFs.CreatePrivatePath(driveNumber);
       
   270 
       
   271 #endif
       
   272 
       
   273     if (err == KErrAlreadyExists)
       
   274         {
       
   275         err = KErrNone;
       
   276         }
       
   277     User::LeaveIfError(err);
       
   278 
       
   279 #ifdef _DRM_TESTING
       
   280     TRAP( r , WriteL(_L8("CreatePrivatePathL->End")) );
       
   281 #endif
       
   282     return;
       
   283 
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CreateCRKeyL
       
   288 // Create CenRep Key
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 LOCAL_C void CreateCRKeyL()
       
   292     {
       
   293 #ifdef _DRM_TESTING
       
   294     TRAPD( r , WriteL(_L8("CreateCRKeyL")) );
       
   295 #endif
       
   296 
       
   297   CRepository* repository = CRepository::NewL(KCRUidDRMHelperServer);
       
   298     CleanupStack::PushL( repository );
       
   299     TInt err = repository->Create(KDRMHelperServerNotification, KNullDesC8);
       
   300     if (err == KErrAlreadyExists)
       
   301         {
       
   302         err = KErrNone;
       
   303         }
       
   304 #ifdef _DRM_TESTING
       
   305     TRAP( r , WriteL(_L8("CreateCRKeyL->Create1"),err) );
       
   306 #endif
       
   307 
       
   308     User::LeaveIfError(err);
       
   309     err = repository->Create(KDRMHelperServerNotificationPassive, KNullDesC8);
       
   310     if (err == KErrAlreadyExists)
       
   311         {
       
   312         err = KErrNone;
       
   313         }
       
   314 #ifdef _DRM_TESTING
       
   315     TRAP( r , WriteL(_L8("CreateCRKeyL->Create2"),err) );
       
   316 #endif
       
   317     User::LeaveIfError(err);
       
   318     CleanupStack::PopAndDestroy(repository); // repository
       
   319 
       
   320 #ifdef _DRM_TESTING
       
   321     TRAP( r , WriteL(_L8("CreateCRKeyL->End")) );
       
   322 #endif
       
   323     }
       
   324 
       
   325 
       
   326 // ============================ MEMBER FUNCTIONS ===============================
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CDRMHelperServer::CDRMHelperServer
       
   330 // C++ default constructor can NOT contain any code, that
       
   331 // might leave.
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 CDRMHelperServer::CDRMHelperServer() :
       
   335     CServer2( EPriorityStandard ) ,
       
   336                     iEndTimeFactory(NULL) ,
       
   337                     iNotifier(NULL),
       
   338                     iIdleObserver(NULL) ,
       
   339                     iUiTimeNotifier(NULL)
       
   340     {
       
   341     // Nothing
       
   342     }
       
   343 
       
   344 // -----------------------------------------------------------------------------
       
   345 // CDRMHelperServer::NewLC
       
   346 // Two-phased constructor.
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 CDRMHelperServer* CDRMHelperServer::NewLC()
       
   350     {
       
   351 
       
   352 #ifdef _DRM_TESTING
       
   353     CreateLogL();
       
   354     TRAPD( err , WriteL(_L8("NewLC")) );
       
   355 #endif
       
   356     CDRMHelperServer* self = new(ELeave) CDRMHelperServer;
       
   357     CleanupStack::PushL( self );
       
   358     self->ConstructL();
       
   359     return self;
       
   360     };
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CDRMHelperServer::ConstructL
       
   364 // Symbian 2nd phase constructor can leave.
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 void CDRMHelperServer::ConstructL()
       
   368     {
       
   369     TInt err = 0;
       
   370 
       
   371 #ifdef _DRM_TESTING
       
   372     TRAPD( r , WriteL(_L8("ConstructL")) );
       
   373 #endif
       
   374 
       
   375 
       
   376     TUint8 waitCount = 0;
       
   377     User::LeaveIfError(iFs.Connect());
       
   378     User::LeaveIfError(iDrm.StartServer());
       
   379     User::LeaveIfError(iDrm.Connect());
       
   380     StartL(KDRMHelperServerName);
       
   381     //ignore errors
       
   382     err = User::RenameProcess( KDRMHSServerFileName );
       
   383     err = User::RenameThread(KDRMHSServerFileName);
       
   384 
       
   385 
       
   386     CreatePrivatePathL(iFs); // create path for internal data file
       
   387     iEndTimeFactory = CEndTimeFactory::NewL( *this );
       
   388     err = KErrNotFound;
       
   389     while( err!=0 && waitCount < 30 ) // wait maximum of 6s
       
   390         {
       
   391         TRAP( err , iNotifier = CDRMNotifier::NewL() );
       
   392         if ( err ) // wait only if the server isn't running
       
   393             {
       
   394             User::After(200000); // 0.2s
       
   395             waitCount++;
       
   396             }
       
   397         if (iNotifier)
       
   398             {
       
   399             iNotifier->RegisterEventObserverL( *this , KEventTimeChange );
       
   400             }
       
   401         }
       
   402 #ifdef _DRM_TESTING
       
   403     TRAP( r , WriteL(_L8("ConstructL->Notifier")) );
       
   404 #endif
       
   405     User::LeaveIfError( err ); // None can do, throw an exception.
       
   406 
       
   407     iUiTimeNotifier = CEnvironmentChangeNotifier::NewL(
       
   408         EPriorityHigh,
       
   409         TCallBack(UiTimeChanged,this)
       
   410         );
       
   411     iUiTimeNotifier->Start();
       
   412 
       
   413     iIdleObserver = CIdleObserver::NewL( *this );
       
   414 
       
   415     iIdleObserver->StartL();
       
   416 #ifdef _DRM_TESTING
       
   417     TRAP( r , WriteL(_L8("ConstructL->IdleObserver")) );
       
   418 #endif
       
   419 
       
   420     CreateCRKeyL();
       
   421     RestoreL();
       
   422 
       
   423     iEndTimeFactory->HandleExpiredL();
       
   424 
       
   425 #ifdef _DRM_TESTING
       
   426     TRAP( r , WriteL(_L8("ConstructL->End")) );
       
   427 #endif
       
   428     }
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CDRMHelperServer::~CDRMHelperServer
       
   432 // Destructor.
       
   433 // -----------------------------------------------------------------------------
       
   434 //
       
   435 CDRMHelperServer::~CDRMHelperServer()
       
   436     {
       
   437 #ifdef _DRM_TESTING
       
   438     TRAPD( err , WriteL(_L8("destructor")) );
       
   439 #endif
       
   440 
       
   441 
       
   442     if ( iEndTimeFactory )
       
   443         {
       
   444         TRAPD( error , StoreL() );
       
   445         TRAP( error , RemoveAllL() );
       
   446         }
       
   447     delete iEndTimeFactory;
       
   448     if (iNotifier)
       
   449         {
       
   450         TRAP_IGNORE( iNotifier->UnRegisterEventObserverL( *this , KEventTimeChange ) );
       
   451         }
       
   452     delete iNotifier;
       
   453     if(iUiTimeNotifier)
       
   454         {
       
   455         iUiTimeNotifier->Cancel();
       
   456         delete iUiTimeNotifier;
       
   457         }
       
   458     delete iIdleObserver;
       
   459     iFs.Close();
       
   460     iDrm.Close();
       
   461     }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // CDRMHelperServer::NewSessionL
       
   465 // Called when a client requires a new instance.
       
   466 // -----------------------------------------------------------------------------
       
   467 CSession2* CDRMHelperServer::NewSessionL(const TVersion &aVersion,
       
   468                                          const RMessage2& /*aMessage*/ ) const
       
   469     {
       
   470     // check we're the right version
       
   471     if (!User::QueryVersionSupported(TVersion(KDRMHSMajorVersionNumber,
       
   472                                               KDRMHSMinorVersionNumber,
       
   473                                               KDRMHSBuildVersionNumber),
       
   474                                      aVersion))
       
   475         {
       
   476         User::Leave(KErrNotSupported);
       
   477         }
       
   478 
       
   479     // make new session
       
   480     return CDRMHelperSession::NewL();
       
   481     }
       
   482 
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CDRMHelperServer::StartupL().
       
   486 // This function starts the actual DRM Helper server after initializing
       
   487 // the cleanup stack and active scheduler.
       
   488 // Returns: TInt: Symbian OS error code.
       
   489 // -----------------------------------------------------------------------------
       
   490 //
       
   491 
       
   492 void CDRMHelperServer::StartupL()
       
   493     {
       
   494     // Construct active scheduler
       
   495     CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
       
   496     CleanupStack::PushL(activeScheduler) ;
       
   497 
       
   498     // Install active scheduler
       
   499     // We don't need to check whether an active scheduler is already installed
       
   500     // as this is a new thread, so there won't be one
       
   501     CActiveScheduler::Install(activeScheduler);
       
   502 
       
   503     // Construct our server
       
   504     CDRMHelperServer::NewLC();
       
   505 
       
   506     RSemaphore semaphore;
       
   507     User::LeaveIfError(semaphore.OpenGlobal(KDRMHelperServerSemaphoreName));
       
   508 
       
   509     // Semaphore opened ok
       
   510     semaphore.Signal();
       
   511     semaphore.Close();
       
   512 
       
   513     // Start handling requests
       
   514     CActiveScheduler::Start();
       
   515 
       
   516     CleanupStack::PopAndDestroy(2, activeScheduler);    //  anonymous CDRMHelperServer
       
   517     }
       
   518 
       
   519 
       
   520 void CDRMHelperServer::PanicServer(TDRMHelperServPanic aPanic)
       
   521     {
       
   522     User::Panic(KDRMHSServer, aPanic);
       
   523     }
       
   524 
       
   525 
       
   526 // -----------------------------------------------------------------------------
       
   527 // CDRMHelperServer::Startup().
       
   528 // This function starts the actual DRM Rights server after initializing
       
   529 // the cleanup stack and active scheduler.
       
   530 // Returns: TInt: Symbian OS error code.
       
   531 // -----------------------------------------------------------------------------
       
   532 //
       
   533 
       
   534 TInt CDRMHelperServer::Startup()
       
   535     {
       
   536     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   537     if (!cleanupStack)
       
   538         {
       
   539         PanicServer(ECreateTrapCleanup);
       
   540         }
       
   541     TRAPD(err, StartupL());
       
   542     if (err != KErrNone)
       
   543         {
       
   544         PanicServer(ESrvCreateServer);
       
   545         }
       
   546     delete cleanupStack;
       
   547     cleanupStack = NULL;
       
   548 
       
   549     return KErrNone;
       
   550     }
       
   551 
       
   552 // -----------------------------------------------------------------------------
       
   553 // CDRMHelperServer::RunError().
       
   554 // This function handle errors from CActive
       
   555 // -----------------------------------------------------------------------------
       
   556 //
       
   557 TInt CDRMHelperServer::RunError(TInt aError)
       
   558     {
       
   559 #ifdef _DRM_TESTING
       
   560     TRAPD( err , WriteL(_L8("RunError")) );
       
   561 #endif
       
   562 
       
   563 
       
   564     Message().Complete(aError);
       
   565     //
       
   566     // The leave will result in an early return from CServer::RunL(), skipping
       
   567     // the call to request another message. So do that now in order to keep the
       
   568     // server running.
       
   569     ReStart();
       
   570     return KErrNone;    // handled the error fully
       
   571     }
       
   572 
       
   573 // -----------------------------------------------------------------------------
       
   574 // CDRMHelperServer::HandleExpiredL().
       
   575 // This function send expiration info out through CenRep
       
   576 // -----------------------------------------------------------------------------
       
   577 //
       
   578 void CDRMHelperServer::HandleExpiredL( CEndTimeBased*& aItem )
       
   579     {
       
   580 #ifdef _DRM_TESTING
       
   581     //test
       
   582     _LIT8(KShowTimes , "Informed Times: %d");
       
   583     TBuf8<40> timesBuf;
       
   584     timesBuf.Format(KShowTimes , aItem->Count());
       
   585     WriteL(_L8("HandleExpiredL"));
       
   586     WriteL(aItem->Uri()->Des());
       
   587     WriteL(timesBuf);
       
   588 #endif
       
   589 
       
   590     HBufC8* buf = NULL;
       
   591     TPtr8 ptr(NULL,0);
       
   592     FormatKeyValueLC( buf , aItem , KExpiredMark ); // buf get pushed into cleanupstack
       
   593     SetKeyValueL( *buf , aItem->RegType() );
       
   594     CleanupStack::PopAndDestroy(buf);
       
   595     }
       
   596 
       
   597 
       
   598 // -----------------------------------------------------------------------------
       
   599 // CDRMHelperServer::FormatKeyValueLC().
       
   600 // This function format key for CenRep
       
   601 // -----------------------------------------------------------------------------
       
   602 //
       
   603 void CDRMHelperServer::FormatKeyValueLC(
       
   604         HBufC8*& aBuf ,
       
   605         CEndTimeBased*& aItem ,
       
   606         const TDesC8& aMark )
       
   607     {
       
   608     TPtr8 ptr(NULL,0,0);
       
   609 
       
   610     aBuf = HBufC8::NewLC( aItem->Uri()->Des().Length() + 4 );
       
   611     ptr.Set(aBuf->Des());
       
   612     ptr.Append(aItem->Count());
       
   613     ptr.Append( aMark );
       
   614     ptr.Append(aItem->PermType());
       
   615     ptr.Append(aItem->AutoType());
       
   616     ptr.Append( aItem->Uri()->Des() );
       
   617 
       
   618     // a walk around for the symbian bugs in cenrep
       
   619     TInt length = ptr.Length();
       
   620     if (length&1)
       
   621         {
       
   622         ptr.SetLength(length+1);
       
   623         ptr[length] = 0;
       
   624         }
       
   625     }
       
   626 
       
   627 // -----------------------------------------------------------------------------
       
   628 // CDRMHelperServer::HandleIdleL().
       
   629 // This function is called when phone enter idle status
       
   630 // -----------------------------------------------------------------------------
       
   631 //
       
   632 void CDRMHelperServer::HandleIdleL()
       
   633     {
       
   634 #ifdef _DRM_TESTING
       
   635     //test
       
   636     WriteL(_L8("HandleIdleL"));
       
   637 #endif
       
   638     iEndTimeFactory->HandleIdleL();
       
   639     }
       
   640 
       
   641 // -----------------------------------------------------------------------------
       
   642 // CDRMHelperServer::RegisterL().
       
   643 // This function register a item
       
   644 // -----------------------------------------------------------------------------
       
   645 //
       
   646 void CDRMHelperServer::RegisterL(
       
   647                         const TDesC8& aUri ,
       
   648                         const TUint8& aPermType ,
       
   649                         const TUint8& aRegType ,
       
   650                         const TUint8& aAutoType )
       
   651     {
       
   652 #ifdef _DRM_TESTING
       
   653     WriteL(_L8("RegisterL"));
       
   654     WriteL(aUri);
       
   655 #endif
       
   656     TTime endTime = Time::MinTTime();
       
   657     CheckExpirationL( aUri , aPermType , aRegType , endTime );
       
   658     iEndTimeFactory->AddL( aUri , endTime , aPermType , aRegType , aAutoType );
       
   659     GetNotificationL( aUri );
       
   660     }
       
   661 
       
   662 // -----------------------------------------------------------------------------
       
   663 // CDRMHelperServer::IsRegistered().
       
   664 // This function check if item is registered
       
   665 // -----------------------------------------------------------------------------
       
   666 //
       
   667 TBool CDRMHelperServer::IsRegistered(
       
   668         const TDesC8& aUri ,
       
   669         const TUint8& aPermType ,
       
   670         const TUint8& aRegType ,
       
   671         const TUint8& aAutoType )
       
   672     {
       
   673 #ifdef _DRM_TESTING
       
   674     TRAPD(err,WriteL(_L8("IsRegisteredL(uri,perm,reg,auto)")));
       
   675     TRAP(err,WriteL(aUri));
       
   676 #endif
       
   677 
       
   678     return iEndTimeFactory->IsRegistered( aUri , aPermType , aRegType , aAutoType );
       
   679     }
       
   680 
       
   681 // -----------------------------------------------------------------------------
       
   682 // CDRMHelperServer::IsRegistered().
       
   683 // This function check if item is registered
       
   684 // -----------------------------------------------------------------------------
       
   685 //
       
   686 TBool CDRMHelperServer::IsRegistered(
       
   687         const TDesC8& aUri ,
       
   688         const TUint8& aPermType ,
       
   689         const TUint8& aRegType )
       
   690     {
       
   691 #ifdef _DRM_TESTING
       
   692     TRAPD(err,WriteL(_L8("IsRegisteredL(uri,perm,reg)")));
       
   693     TRAP(err,WriteL(aUri));
       
   694 #endif
       
   695 
       
   696     return iEndTimeFactory->IsRegistered( aUri , aPermType , aRegType );
       
   697     }
       
   698 
       
   699 
       
   700 // -----------------------------------------------------------------------------
       
   701 // CDRMHelperServer::IsRegistered().
       
   702 // This function check if item is registered
       
   703 // -----------------------------------------------------------------------------
       
   704 //
       
   705 TBool CDRMHelperServer::IsRegistered2(
       
   706         const TDesC8& aUri ,
       
   707         const TUint8& aRegType,
       
   708         const TUint8& aAutoType )
       
   709     {
       
   710 #ifdef _DRM_TESTING
       
   711     TRAPD(err,WriteL(_L8("IsRegisteredL(uri,reg,auto)")));
       
   712     TRAP(err,WriteL(aUri));
       
   713 #endif
       
   714 
       
   715     return iEndTimeFactory->IsRegistered2( aUri, aRegType, aAutoType );
       
   716     }
       
   717 
       
   718 // -----------------------------------------------------------------------------
       
   719 // CDRMHelperServer::UpdateL().
       
   720 // This function update all items
       
   721 // -----------------------------------------------------------------------------
       
   722 //
       
   723 void CDRMHelperServer::UpdateL()
       
   724     {
       
   725 #ifdef _DRM_TESTING
       
   726     WriteL(_L8("UpdateL(all)"));
       
   727 #endif
       
   728 
       
   729     iEndTimeFactory->UpdateL();
       
   730     }
       
   731 
       
   732 
       
   733 // -----------------------------------------------------------------------------
       
   734 // CDRMHelperServer::UpdateL().
       
   735 // This function update items with a URI
       
   736 // -----------------------------------------------------------------------------
       
   737 //
       
   738 void CDRMHelperServer::UpdateL( const TDesC8& aUri )
       
   739     {
       
   740 #ifdef _DRM_TESTING
       
   741     WriteL(_L8("UpdateL(aUri)"));
       
   742     WriteL(aUri);
       
   743 #endif
       
   744 
       
   745     UpdateL( aUri , ContentAccess::EView);
       
   746     UpdateL( aUri , ContentAccess::EPlay);
       
   747     UpdateL( aUri , ContentAccess::EExecute);
       
   748     UpdateL( aUri , ContentAccess::EPrint);
       
   749 
       
   750     }
       
   751 
       
   752 // -----------------------------------------------------------------------------
       
   753 // CDRMHelperServer::UpdateL().
       
   754 // This function update items with a URI and a permission type
       
   755 // -----------------------------------------------------------------------------
       
   756 //
       
   757 void CDRMHelperServer::UpdateL(
       
   758             const TDesC8& aUri ,
       
   759             const TUint8& aPermType )
       
   760     {
       
   761 #ifdef _DRM_TESTING
       
   762     WriteL(_L8("UpdateL(aUri,aPerm)"));
       
   763     WriteL(aUri);
       
   764 #endif
       
   765 
       
   766     UpdateL( aUri , aPermType , CDRMHelperServer::EActive );
       
   767     UpdateL( aUri , aPermType , CDRMHelperServer::EPassive );
       
   768 
       
   769     }
       
   770 
       
   771 // -----------------------------------------------------------------------------
       
   772 // CDRMHelperServer::UpdateL().
       
   773 // This function update items with a URI, a permission type and a register type
       
   774 // -----------------------------------------------------------------------------
       
   775 //
       
   776 void CDRMHelperServer::UpdateL(
       
   777             const TDesC8& aUri ,
       
   778             const TUint8& aPermType ,
       
   779             const TUint8& aRegType )
       
   780     {
       
   781 #ifdef _DRM_TESTING
       
   782     WriteL(_L8("UpdateL(aUri,aPerm,aReg)"));
       
   783     WriteL(aUri);
       
   784 #endif
       
   785 
       
   786     TTime endTime;
       
   787     TBool reg = IsRegistered(aUri , aPermType , aRegType);
       
   788     if (reg)
       
   789         {
       
   790         CheckExpirationL( aUri , aPermType , aRegType , endTime );
       
   791         iEndTimeFactory->UpdateEndTimeL( aUri , endTime , aPermType , aRegType );
       
   792         }
       
   793     }
       
   794 
       
   795 // -----------------------------------------------------------------------------
       
   796 // CDRMHelperServer::UpdateL().
       
   797 // This function update items with a URI, a permission type and a register type
       
   798 // -----------------------------------------------------------------------------
       
   799 //
       
   800 void CDRMHelperServer::UpdateL(
       
   801             const TDesC8& aUri ,
       
   802             const TUint8& aPermType ,
       
   803             const TUint8& aRegType ,
       
   804             const TUint8& aAutoType )
       
   805     {
       
   806 #ifdef _DRM_TESTING
       
   807     WriteL(_L8("UpdateL(aUri,aPerm,aReg,aAutoType)"));
       
   808     WriteL(aUri);
       
   809 #endif
       
   810 
       
   811     TTime endTime;
       
   812     CheckExpirationL( aUri , aPermType , aRegType , endTime );
       
   813     iEndTimeFactory->UpdateEndTimeL( aUri , endTime , aPermType , aRegType , aAutoType );
       
   814     }
       
   815 
       
   816 // -----------------------------------------------------------------------------
       
   817 // CDRMHelperServer::RemoveL().
       
   818 // This function update an item
       
   819 // -----------------------------------------------------------------------------
       
   820 //
       
   821 void CDRMHelperServer::RemoveL(
       
   822                         const TDesC8& aUri ,
       
   823                         const TUint8& aPermType ,
       
   824                         const TUint8& aRegType ,
       
   825                         const TUint8& aAutoType )
       
   826     {
       
   827 #ifdef _DRM_TESTING
       
   828     WriteL(_L8("RemoveL"));
       
   829     WriteL(aUri);
       
   830 #endif
       
   831 
       
   832     TBool found = iEndTimeFactory->IsRegistered( aUri , aPermType , aRegType , aAutoType );
       
   833     if (found)
       
   834         {
       
   835         iEndTimeFactory->Remove( aUri , aPermType , aRegType , aAutoType );
       
   836         iEndTimeFactory->ResetAlarm();
       
   837         if ( !iEndTimeFactory->IsRegistered( aUri ) )
       
   838             {
       
   839             RemoveNotificationL( aUri );
       
   840             }
       
   841         }
       
   842     }
       
   843 
       
   844 // -----------------------------------------------------------------------------
       
   845 // CDRMHelperServer::RemoveAllL().
       
   846 // This function removes all items
       
   847 // -----------------------------------------------------------------------------
       
   848 //
       
   849 void CDRMHelperServer::RemoveAllL()
       
   850     {
       
   851 #ifdef _DRM_TESTING
       
   852     WriteL(_L8("RemoveAllL"));
       
   853 #endif
       
   854 
       
   855     iEndTimeFactory->RemoveAllL();
       
   856     }
       
   857 
       
   858 
       
   859 ContentAccess::TIntent CDRMHelperServer::MapPermissionType(TUint8 aPermType)
       
   860     {
       
   861     switch(aPermType)
       
   862         {
       
   863         case ContentAccess::EPlay:
       
   864             return ContentAccess::EPlay;
       
   865         case ContentAccess::EView:
       
   866             return ContentAccess::EView;
       
   867         case ContentAccess::EExecute:
       
   868             return ContentAccess::EExecute;
       
   869         case ContentAccess::EPrint:
       
   870             return ContentAccess::EPrint;
       
   871         default:
       
   872             break;
       
   873         }
       
   874     return ContentAccess::EUnknown;
       
   875     }
       
   876 
       
   877 // -----------------------------------------------------------------------------
       
   878 // CDRMHelperServer::CheckPermission().
       
   879 // This function check timebased contraints for a permission
       
   880 // -----------------------------------------------------------------------------
       
   881 //
       
   882 void CDRMHelperServer::CheckPermission(
       
   883                 CDRMPermission* aPermission ,
       
   884                 const TUint8& aPermType ,
       
   885                 TTimeBased& aTime ,
       
   886                 TTimeIntervalSeconds& aInterval,
       
   887                 const RPointerArray<HBufC8>& aIndividual )
       
   888     {
       
   889 #ifdef _DRM_TESTING
       
   890     TRAPD(err,WriteL(_L8("CheckPermission")));
       
   891 #endif
       
   892 
       
   893     CDRMConstraint* c = NULL;
       
   894     ContentAccess::TIntent type = MapPermissionType(aPermType);
       
   895     TTime temp;
       
   896     temp.HomeTime();
       
   897     TUint32 reason( 0 );
       
   898     TBool validity = EFalse;
       
   899 
       
   900     aTime.SetStart(Time::MinTTime());
       
   901     aTime.SetEnd(Time::MinTTime());
       
   902     if (aPermission->iAvailableRights & type)
       
   903         {
       
   904         c = aPermission->ConstraintForIntent(type);
       
   905         if (!c)
       
   906             {
       
   907             return;
       
   908             }
       
   909         if (aPermission->iAvailableRights & ERightsTopLevel)
       
   910             {
       
   911             CDRMConstraint* temp = aPermission->TopLevelConstraint();
       
   912             c->Merge(*temp);
       
   913             }
       
   914 
       
   915         validity = c->Valid( temp, aIndividual, reason );
       
   916         if ( !validity && reason & EConstraintIndividual )
       
   917             {
       
   918             return;
       
   919             }
       
   920         if (c->iActiveConstraints==EConstraintNone ||
       
   921             c->iActiveConstraints == EConstraintIndividual )
       
   922             {
       
   923             aTime.SetStart(Time::MinTTime());
       
   924             aTime.SetEnd(Time::MaxTTime());
       
   925             return;
       
   926             }
       
   927         if (c->iActiveConstraints & EConstraintInterval)
       
   928             {
       
   929             if (c->iIntervalStart == Time::NullTTime())
       
   930                 {
       
   931                 aInterval = c->iInterval;
       
   932                 }
       
   933             else
       
   934                 {
       
   935                 aTime.SetStart(c->iIntervalStart);
       
   936                 aTime.SetEnd(c->iIntervalStart + c->iInterval);
       
   937                 }
       
   938             }
       
   939         if (c->iActiveConstraints & EConstraintStartTime)
       
   940             {
       
   941             if ( !(c->iActiveConstraints & EConstraintInterval ) )
       
   942                 {
       
   943                 aTime.SetStart( c->iStartTime );
       
   944                 if (c->iActiveConstraints&EConstraintEndTime)
       
   945                     {
       
   946                     aTime.SetEnd( c->iEndTime );
       
   947                     }
       
   948                 else
       
   949                     {
       
   950                     aTime.SetEnd( Time::MaxTTime() );
       
   951                     }
       
   952                 }
       
   953             else
       
   954                 {
       
   955                 aTime.SetStart( aTime.StartTime() > c->iStartTime ? aTime.StartTime() : c->iStartTime );
       
   956                 if (c->iActiveConstraints&EConstraintEndTime)
       
   957                     {
       
   958                     aTime.SetEnd( aTime.EndTime() < c->iEndTime ? aTime.EndTime() : c->iEndTime );
       
   959                     }
       
   960                 }
       
   961             }
       
   962         else
       
   963             {
       
   964             if (c->iActiveConstraints==EConstraintEndTime)
       
   965                 {
       
   966                 if ( !(c->iActiveConstraints & EConstraintInterval ) )
       
   967                     {
       
   968                     aTime.SetStart(Time::MinTTime());
       
   969                     aTime.SetEnd( c->iEndTime );
       
   970                     }
       
   971                 else
       
   972                     {
       
   973                     aTime.SetEnd( aTime.EndTime() < c->iEndTime ? aTime.EndTime() : c->iEndTime );
       
   974                     }
       
   975                 }
       
   976             }
       
   977         }
       
   978 #ifdef _DRM_TESTING
       
   979     TRAP(err,WriteL(_L8("Start Time:")));
       
   980     TRAP(err,WriteTimeL(aTime.StartTime()));
       
   981     TRAP(err,WriteL(_L8("End Time:")));
       
   982     TRAP(err,WriteTimeL(aTime.EndTime()));
       
   983 #endif
       
   984     }
       
   985 
       
   986 
       
   987 // -----------------------------------------------------------------------------
       
   988 // CDRMHelperServer::CheckExpirationL().
       
   989 // This function check the time for rights to expire
       
   990 // -----------------------------------------------------------------------------
       
   991 //
       
   992 void CDRMHelperServer::CheckExpirationL(
       
   993         const TDesC8& aUri ,
       
   994         const TUint8& aPermType ,
       
   995         const TUint8& aRegType ,
       
   996         TTime& aEndTime )
       
   997     {
       
   998 #ifdef _DRM_TESTING
       
   999     WriteL(_L8("CheckExpiration"));
       
  1000 #endif
       
  1001 
       
  1002 
       
  1003     RPointerArray<CDRMPermission> rights;
       
  1004     RArray<TTimeBased> timeList;
       
  1005     TBool stop = EFalse;
       
  1006     TInt i = 0;
       
  1007     TInt err = 0;
       
  1008     TTimeIntervalYears years(KTimeIntervalYears);
       
  1009     TTime time = Time::MinTTime();
       
  1010     TTimeBased item;
       
  1011     TTimeIntervalSeconds interval = 0;
       
  1012     RPointerArray<HBufC8> imsi;
       
  1013 
       
  1014     aEndTime = Time::MinTTime();
       
  1015 
       
  1016 
       
  1017     TRAP( err , iDrm.GetDBEntriesL(aUri, rights) );
       
  1018     CleanupClosePushL(rights);
       
  1019     if ( err == KErrCANoRights || err == KErrCANoPermission )
       
  1020         {
       
  1021         aEndTime = Time::MinTTime();
       
  1022         err = KErrNone;
       
  1023         stop = ETrue;
       
  1024         }
       
  1025     User::LeaveIfError(err);
       
  1026 
       
  1027     TRAP( err, iDrm.GetSupportedIndividualsL( imsi ) );
       
  1028 
       
  1029     if ( err )
       
  1030         {
       
  1031         imsi.ResetAndDestroy();
       
  1032         }
       
  1033 
       
  1034     time.HomeTime();
       
  1035     // Goes through all the rights associated to this specific URI
       
  1036     CleanupClosePushL(timeList);
       
  1037 
       
  1038     for (i = 0; i < rights.Count() && !stop; i++)
       
  1039         {
       
  1040         CheckPermission( rights[i], aPermType, item, interval ,imsi );
       
  1041         if (interval.Int()>0 ||
       
  1042             (item.StartTime()==Time::MinTTime()&&item.EndTime()==Time::MaxTTime()))
       
  1043             {
       
  1044             stop = ETrue;
       
  1045             aEndTime = Time::MaxTTime();
       
  1046             }
       
  1047         else
       
  1048             {
       
  1049             if (aRegType == CDRMHelperServer::EActive && item.StartTime()>time)
       
  1050                 {
       
  1051                 // dont count future time for active usage
       
  1052                 }
       
  1053             else
       
  1054                 {
       
  1055                 err = timeList.Append( item );
       
  1056                 if ( err )
       
  1057                     {
       
  1058                     rights.ResetAndDestroy();
       
  1059                     imsi.ResetAndDestroy();
       
  1060                     }
       
  1061                 User::LeaveIfError(err);
       
  1062                 }
       
  1063             }
       
  1064         }
       
  1065     rights.ResetAndDestroy();
       
  1066     imsi.ResetAndDestroy();
       
  1067 
       
  1068     // if there were no inactivated interval- or full-rights then calculate the expiration date
       
  1069     // based on what we stored to timeList
       
  1070     if( !stop )
       
  1071         {
       
  1072         time.HomeTime(); // preset time to current time. This is what we compare against.
       
  1073         TTime temp;
       
  1074         TTime pastTime;
       
  1075         temp = time;
       
  1076         pastTime = Time::MinTTime();
       
  1077 
       
  1078         TBool action = ETrue;
       
  1079 
       
  1080         // Loop while there are still items in the list and we have done something
       
  1081         while( action && timeList.Count() > 0 )
       
  1082             {
       
  1083             action = EFalse;
       
  1084             for ( i = 0 ; i < timeList.Count() ; i++ ) // go through the whole timeList
       
  1085                 {
       
  1086 #ifdef _DRM_TESTING
       
  1087                 _LIT8(KCount , "time list count: %d");
       
  1088                 TBuf8<40> buf;
       
  1089                 buf.Format( KCount , timeList.Count());
       
  1090                 WriteL( buf );
       
  1091 #endif
       
  1092                 if ( timeList[i].StartTime() <= time && timeList[i].EndTime() > time )
       
  1093                     {
       
  1094                     // Case1: valid rights
       
  1095                     time = timeList[i].EndTime();
       
  1096                     timeList.Remove(i);
       
  1097                     action = ETrue;
       
  1098 #ifdef _DRM_TESTING
       
  1099                     WriteL(_L8("case 1:"));
       
  1100                     WriteTimeL( time );
       
  1101 #endif
       
  1102                     }
       
  1103                 else if ( timeList[i].StartTime() <= time && timeList[i].EndTime() <= time )
       
  1104                     {
       
  1105                     // Case2: expired rights
       
  1106                     if (timeList[i].EndTime()>pastTime) // just in case there is no valid rights
       
  1107                         {
       
  1108                         pastTime = timeList[i].EndTime(); // save the latest end time from the expired rights
       
  1109                         }
       
  1110                     timeList.Remove(i);
       
  1111                     action = ETrue;
       
  1112 #ifdef _DRM_TESTING
       
  1113                     WriteL(_L8("case 2:"));
       
  1114 #endif
       
  1115                     }
       
  1116                 else if ( timeList[i].StartTime() > time && timeList[i].EndTime() <= time )
       
  1117                     {
       
  1118                     // Case3: Illegal case. Start time after end-time.
       
  1119                     timeList.Remove(i);
       
  1120                     action = ETrue;
       
  1121 #ifdef _DRM_TESTING
       
  1122                     WriteL(_L8("case 3:"));
       
  1123 #endif
       
  1124                     }
       
  1125                 else
       
  1126                     {
       
  1127                     // Case4: Only thing left is the not yet valid -rights
       
  1128 #ifdef _DRM_TESTING
       
  1129                     WriteL(_L8("case 4:"));
       
  1130 #endif
       
  1131                     }
       
  1132                 }
       
  1133             }
       
  1134 
       
  1135         if (temp<time)
       
  1136             {
       
  1137             aEndTime = time;   // time has been changed, so we use it, otherwise it means there is no valid rights
       
  1138             }
       
  1139         else
       
  1140             {
       
  1141             aEndTime = pastTime;
       
  1142             }
       
  1143 
       
  1144 
       
  1145 #ifdef _DRM_TESTING
       
  1146         WriteL(_L8("expiration date:"));
       
  1147         WriteTimeL( aEndTime );
       
  1148 #endif
       
  1149         }
       
  1150 
       
  1151 #ifdef _DRM_TESTING
       
  1152     WriteL(_L8("Endtime calculation is done:"));
       
  1153     WriteTimeL( aEndTime );
       
  1154 #endif
       
  1155 
       
  1156     timeList.Reset();
       
  1157     CleanupStack::PopAndDestroy(&timeList); // timeList
       
  1158     CleanupStack::PopAndDestroy(&rights); // rights
       
  1159     }
       
  1160 
       
  1161 
       
  1162 // -----------------------------------------------------------------------------
       
  1163 // CDRMHelperServer::SetKeyValueL().
       
  1164 // This function set CenRep key value
       
  1165 // -----------------------------------------------------------------------------
       
  1166 //
       
  1167 void CDRMHelperServer::SetKeyValueL( const TDesC8& aKeyValue , const TUint8& aRegType )
       
  1168     {
       
  1169 #ifdef _DRM_TESTING
       
  1170     WriteL(_L8("SetKeyValueL"));
       
  1171     WriteL( aKeyValue );
       
  1172 #endif
       
  1173 
       
  1174     // Connecting and initialization:
       
  1175     CRepository* repository = CRepository::NewL(KCRUidDRMHelperServer);
       
  1176     CleanupStack::PushL( repository );
       
  1177     if ( aRegType == CDRMHelperServer::EActive )
       
  1178         {
       
  1179         User::LeaveIfError(repository->Set(KDRMHelperServerNotification, aKeyValue));
       
  1180         }
       
  1181     else if ( aRegType == CDRMHelperServer::EPassive )
       
  1182         {
       
  1183         User::LeaveIfError(repository->Set(KDRMHelperServerNotificationPassive, aKeyValue));
       
  1184         }
       
  1185     CleanupStack::PopAndDestroy(repository); // repository
       
  1186     }
       
  1187 
       
  1188 // -----------------------------------------------------------------------------
       
  1189 // CDRMHelperServer::StoreL().
       
  1190 // This function store internal list into data file
       
  1191 // -----------------------------------------------------------------------------
       
  1192 //
       
  1193 void CDRMHelperServer::StoreL()
       
  1194     {
       
  1195 
       
  1196 #ifdef _DRM_TESTING
       
  1197     WriteL(_L8("StoreL"));
       
  1198 #endif
       
  1199 
       
  1200     RFileWriteStream file;
       
  1201     User::LeaveIfError(file.Replace( iFs , DataFileL(iFs).FullName() , EFileWrite ));
       
  1202     file.PushL();
       
  1203     ExternalizeL(file);
       
  1204     file.CommitL();
       
  1205     CleanupStack::PopAndDestroy(&file);//file
       
  1206     }
       
  1207 
       
  1208 // -----------------------------------------------------------------------------
       
  1209 // CDRMHelperServer::RestoreL().
       
  1210 // This function restore internal list from data file
       
  1211 // -----------------------------------------------------------------------------
       
  1212 //
       
  1213 void CDRMHelperServer::RestoreL()
       
  1214     {
       
  1215 #ifdef _DRM_TESTING
       
  1216     WriteL(_L8("RestoreL"));
       
  1217 #endif
       
  1218 
       
  1219     TInt err = 0;
       
  1220     RFileReadStream file;
       
  1221 
       
  1222     RFile test;
       
  1223     err = test.Open( iFs , DataFileL(iFs).FullName() , EFileRead );
       
  1224     if ( !err )
       
  1225         {
       
  1226         CleanupClosePushL(test);
       
  1227         TInt size = 0;
       
  1228         err = test.Size(size);
       
  1229         User::LeaveIfError(err);
       
  1230         CleanupStack::PopAndDestroy(&test); //test
       
  1231         if (size == 0)
       
  1232             {
       
  1233             StoreL();
       
  1234             }
       
  1235         }
       
  1236     else if ( err == KErrNotFound )
       
  1237         {
       
  1238         StoreL();
       
  1239         }
       
  1240     else
       
  1241         {
       
  1242         User::Leave(err);
       
  1243         }
       
  1244 
       
  1245     User::LeaveIfError(file.Open( iFs , DataFileL(iFs).FullName() , EFileRead ));
       
  1246     file.PushL();
       
  1247     InternalizeL(file);
       
  1248     CleanupStack::PopAndDestroy(&file); //file
       
  1249 
       
  1250 #ifdef _DRM_TESTING
       
  1251     WriteL(_L8("RestoreL->End"));
       
  1252 #endif
       
  1253     }
       
  1254 
       
  1255 // -----------------------------------------------------------------------------
       
  1256 // CDRMHelperServer::ExternalizeL().
       
  1257 // This function externalize internal list
       
  1258 // -----------------------------------------------------------------------------
       
  1259 //
       
  1260 void CDRMHelperServer::ExternalizeL(RWriteStream& aStream) const
       
  1261     {
       
  1262 #ifdef _DRM_TESTING
       
  1263     WriteL(_L8("ExternalizeL"));
       
  1264 #endif
       
  1265     aStream << *iEndTimeFactory;
       
  1266     }
       
  1267 
       
  1268 // -----------------------------------------------------------------------------
       
  1269 // CDRMHelperServer::InternalizeL().
       
  1270 // This function internalizeL internal list
       
  1271 // -----------------------------------------------------------------------------
       
  1272 //
       
  1273 void CDRMHelperServer::InternalizeL(RReadStream& aStream)
       
  1274     {
       
  1275 #ifdef _DRM_TESTING
       
  1276     WriteL(_L8("InternalizeL"));
       
  1277 #endif
       
  1278     iEndTimeFactory->InternalizeL(aStream);
       
  1279     }
       
  1280 
       
  1281 // -----------------------------------------------------------------------------
       
  1282 // CDRMHelperServer::GetNotificationL().
       
  1283 // This function register event listening to DRM notifier
       
  1284 // -----------------------------------------------------------------------------
       
  1285 //
       
  1286 void CDRMHelperServer::GetNotificationL(const TDesC8& aUri)
       
  1287     {
       
  1288 #ifdef _DRM_TESTING
       
  1289     WriteL(_L8("GetNotificationL"));
       
  1290 #endif
       
  1291     iNotifier->RegisterEventObserverL( *this , KEventAddRemove , aUri );
       
  1292     iNotifier->RegisterEventObserverL( *this , KEventModify , aUri );
       
  1293     }
       
  1294 
       
  1295 // -----------------------------------------------------------------------------
       
  1296 // CDRMHelperServer::RemoveNotificationL().
       
  1297 // This function remove event listening to DRM notifier
       
  1298 // -----------------------------------------------------------------------------
       
  1299 //
       
  1300 void CDRMHelperServer::RemoveNotificationL(const TDesC8& aUri)
       
  1301     {
       
  1302 #ifdef _DRM_TESTING
       
  1303     WriteL(_L8("RemoveNotificationL"));
       
  1304 #endif
       
  1305     iNotifier->UnRegisterEventObserverL( *this , KEventAddRemove , aUri );
       
  1306     iNotifier->UnRegisterEventObserverL( *this , KEventModify , aUri );
       
  1307     }
       
  1308 
       
  1309 // -----------------------------------------------------------------------------
       
  1310 // CDRMHelperServer::HandleEventL().
       
  1311 // This function handle event from DRM notifier
       
  1312 // -----------------------------------------------------------------------------
       
  1313 //
       
  1314 void CDRMHelperServer::HandleEventL( MDRMEvent* aEvent )
       
  1315     {
       
  1316 #ifdef _DRM_TESTING
       
  1317     WriteL(_L8("HandleEventL"));
       
  1318 #endif
       
  1319 
       
  1320     HBufC8* contentID = 0;
       
  1321     TDRMEventType event;
       
  1322 
       
  1323     aEvent->GetEventType(event);
       
  1324 
       
  1325 #ifdef _DRM_TESTING
       
  1326             WriteL(_L8("Notifier:"),event);
       
  1327 #endif
       
  1328     switch(event)
       
  1329         {
       
  1330         case KEventAddRemove:
       
  1331             {
       
  1332 #ifdef _DRM_TESTING
       
  1333             WriteL(_L8("Notifier: EventAddRemove"));
       
  1334 #endif
       
  1335             contentID = REINTERPRET_CAST( CDRMEventAddRemove* , aEvent )->GetContentIDL();
       
  1336             CleanupStack::PushL( contentID );
       
  1337 #ifdef _DRM_TESTING
       
  1338             WriteL(contentID->Des());
       
  1339 #endif
       
  1340             switch( REINTERPRET_CAST( CDRMEventAddRemove* , aEvent )->Status() )
       
  1341                 {
       
  1342                 case ERightsObjectRecieved:
       
  1343                     {
       
  1344 #ifdef _DRM_TESTING
       
  1345                     WriteL(_L8("Notifier: RightsObjectRecieved"));
       
  1346 #endif
       
  1347                     UpdateL( contentID->Des() );
       
  1348                     }
       
  1349                     break;
       
  1350                 case ERightsObjectDeleted:
       
  1351                     {
       
  1352 #ifdef _DRM_TESTING
       
  1353                     WriteL(_L8("Notifier: RightsObjectDeleted"));
       
  1354 #endif
       
  1355                     UpdateL( contentID->Des() );
       
  1356                     }
       
  1357                     break;
       
  1358                 case ERightsObjectDeletedAll:
       
  1359                     {
       
  1360 #ifdef _DRM_TESTING
       
  1361                     WriteL(_L8("Notifier: RightsObjectDeletedAll"));
       
  1362 #endif
       
  1363                     UpdateL( contentID->Des() );
       
  1364                     }
       
  1365                     break;
       
  1366                 default:
       
  1367                     {
       
  1368 #ifdef _DRM_TESTING
       
  1369                     WriteL(_L8("Notifier: Unknown event"));
       
  1370 #endif
       
  1371                     }
       
  1372                     break;
       
  1373                 }
       
  1374             CleanupStack::PopAndDestroy( contentID );
       
  1375             }
       
  1376             break;
       
  1377         case KEventModify:
       
  1378             {
       
  1379 #ifdef _DRM_TESTING
       
  1380             WriteL(_L8("Notifier: EventModify"));
       
  1381 #endif
       
  1382             contentID = REINTERPRET_CAST( CDRMEventModify* , aEvent )->GetContentIDL();
       
  1383             CleanupStack::PushL( contentID );
       
  1384 #ifdef _DRM_TESTING
       
  1385             WriteL(contentID->Des());
       
  1386 #endif
       
  1387             UpdateL( contentID->Des() );
       
  1388             CleanupStack::PopAndDestroy( contentID );
       
  1389             }
       
  1390             break;
       
  1391         case KEventTimeChange:
       
  1392             {
       
  1393 #ifdef _DRM_TESTING
       
  1394             WriteL(_L8("Notifier: EventTimeChange"));
       
  1395 #endif
       
  1396             UpdateL();
       
  1397             }
       
  1398             break;
       
  1399         default:
       
  1400             {
       
  1401 #ifdef _DRM_TESTING
       
  1402             WriteL(_L8("Notifier: Unknown event"));
       
  1403 #endif
       
  1404             }
       
  1405             break;
       
  1406 
       
  1407         }
       
  1408     return;
       
  1409     }
       
  1410 
       
  1411 // -----------------------------------------------------------------------------
       
  1412 // CDRMHelperServer::UiTimeChanged
       
  1413 // CPeriodic callback function to update the clock
       
  1414 // -----------------------------------------------------------------------------
       
  1415 //
       
  1416 TInt CDRMHelperServer::UiTimeChanged(void* aPointer)
       
  1417     {
       
  1418 #ifdef _DRM_TESTING
       
  1419     TRAPD(r,WriteL(_L8("UiTimeChanged")));
       
  1420 #endif
       
  1421     CDRMHelperServer* current = static_cast<CDRMHelperServer*>(aPointer);
       
  1422     TInt err = 0;
       
  1423     if(current)
       
  1424         {
       
  1425         if(current->iUiTimeNotifier->Change() & EChangesSystemTime)
       
  1426             {
       
  1427             TRAP_IGNORE( current->UpdateL() );
       
  1428             }
       
  1429         if(current->iUiTimeNotifier->Change() & EChangesLocale)
       
  1430             {
       
  1431             // time zone is changed
       
  1432             }
       
  1433         }
       
  1434 #ifdef _DRM_TESTING
       
  1435     TRAP(r,WriteL(_L8("UiTimeChanged:->End"),current->iUiTimeNotifier->Change()));
       
  1436 #endif
       
  1437     return err;
       
  1438     };
       
  1439 
       
  1440 
       
  1441 TInt E32Main()
       
  1442     {
       
  1443     return CDRMHelperServer::Startup();
       
  1444     }
       
  1445 //  End of File