omadrm/drmengine/server/src/DRMRightsServer.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2003 - 2007 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:  DRM3 Engine manages all DRM related database operations.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32std.h>
       
    21 #include <e32test.h>
       
    22 #include <etelmm.h>
       
    23 #include <DrmTypes.h>
       
    24 #include <starterclient.h>
       
    25 #include <featmgr.h>
       
    26 
       
    27 #ifdef RD_MULTIPLE_DRIVE
       
    28 #include <DriveInfo.h>
       
    29 #endif
       
    30 
       
    31 #include "DRMRightsServer.h"
       
    32 #include "DRMRightsDb.h"
       
    33 #include "DRMDbSession.h"
       
    34 #include "DRMEngineClientServer.h"
       
    35 #include "DRMLog.h"
       
    36 #include "DRMNotifier.h"
       
    37 #include "DrmKeyStorage.h"
       
    38 #include "drmnotifierserver.h"
       
    39 #include "roapstorageserver.h"
       
    40 #include "drmnotifierclientserver.h"
       
    41 #include "drmroapclientserver.h"
       
    42 #include "DRMXOma.h"
       
    43 #include "DRMBackupObserver.h"
       
    44 #include "DRMBackup.h"
       
    45 #include "flogger.h"
       
    46 #include "drmrightsparser.h"
       
    47 #include "drmrights.h"
       
    48 #include "drmhelpercommon.h"
       
    49 
       
    50 #include  "wmdrmclientwrapper.h"
       
    51 
       
    52 
       
    53 
       
    54 #ifdef __DRM_CLOCK
       
    55 #include "DRMClockServer.h"
       
    56 #include "drmclockclientserver.h"
       
    57 #endif
       
    58 
       
    59 #include <utf.h>
       
    60 #include <DRMIndividualConstraintExtension.h>
       
    61 
       
    62 /*
       
    63 _LIT( KLogDir, "drm");
       
    64 _LIT( KLogName, "backup.log");
       
    65 */
       
    66 
       
    67 // EXTERNAL DATA STRUCTURES
       
    68 // EXTERNAL FUNCTION PROTOTYPES 
       
    69 // CONSTANTS
       
    70 LOCAL_C const TUint KMaxHeapsize = 0x7A120;
       
    71 
       
    72 _LIT8( KImsiId,"IMSI:");
       
    73 
       
    74 // MACROS
       
    75 #ifdef _DRM_TESTING
       
    76 _LIT( KDateTimeFormat, "%F%Y%M%D%H%T%S%C" );
       
    77 #endif
       
    78 
       
    79 // LOCAL CONSTANTS AND MACROS
       
    80 const TUint8 KMaxStartTries = 30;
       
    81 const TInt KWaitingTime = 2000000; // 2 sec
       
    82 _LIT( KRightsServerThread, "RightsServer" );
       
    83 
       
    84 #ifdef RD_MULTIPLE_DRIVE
       
    85 
       
    86 _LIT( KDbTempPath, "%c:\\system\\temp\\" );
       
    87 _LIT( KIndividualConstraintExtensionDll, "%c:\\sys\\bin\\DRMIndividualConstraintExtension.dll" );
       
    88 _LIT( KRightsDir, "%c:\\private\\101F51F2\\rdb\\" );
       
    89 _LIT( KTimedReplayCacheFile, "%c:\\private\\101F51F2\\timererc.dat" );
       
    90 _LIT( KPlainReplayCacheFile, "%c:\\private\\101F51F2\\plainrc.dat" );
       
    91 #ifdef RD_DRM_METERING
       
    92 _LIT( KMeteringDataBaseFile, "%c:\\private\\101F51F2\\meterdb.dat" );
       
    93 #endif
       
    94 
       
    95 #define USE_RO_IMPORT
       
    96 
       
    97 #ifdef USE_RO_IMPORT
       
    98 _LIT( KInternalImportDir, "%c:\\private\\101F51F2\\import\\" );
       
    99 _LIT( KUserDiskImportDir, "%c:\\import\\" ); // usually embedded MMC
       
   100 _LIT( KUserRemovableDiskImportDir, "%c:\\import\\" ); // usually external MMC
       
   101 _LIT( KDrSuffix, ".dr" );
       
   102 #endif
       
   103 
       
   104 #else
       
   105 
       
   106 _LIT( KRightsDir, "c:\\private\\101F51F2\\rdb\\" );
       
   107 _LIT( KTimedReplayCacheFile, "c:\\private\\101F51F2\\timererc.dat" );
       
   108 _LIT( KPlainReplayCacheFile, "c:\\private\\101F51F2\\plainrc.dat" );
       
   109 #ifdef RD_DRM_METERING
       
   110 _LIT( KMeteringDataBaseFile, "c:\\private\\101F51F2\\meterdb.dat" );
       
   111 #endif
       
   112 
       
   113 #define USE_RO_IMPORT
       
   114 
       
   115 #ifdef USE_RO_IMPORT
       
   116 _LIT( KInternalImportDir, "c:\\private\\101F51F2\\import\\" );
       
   117 _LIT( KUserDiskImportDir, "e:\\import\\" );
       
   118 _LIT( KDrSuffix, ".dr" );
       
   119 #endif
       
   120 
       
   121 #endif
       
   122 
       
   123 _LIT(KWmDrmClientWrapperName, "wmdrmclientwrapper.dll");
       
   124 
       
   125 // MODULE DATA STRUCTURES
       
   126 
       
   127 NONSHARABLE_STRUCT( TUnloadModule )
       
   128     {
       
   129     RTelServer* iServer;
       
   130     const TDesC* iName;
       
   131     };
       
   132 
       
   133 // LOCAL FUNCTION PROTOTYPES
       
   134 
       
   135 LOCAL_C TInt Startup( void );
       
   136 LOCAL_C void SignalClient();
       
   137 LOCAL_C TInt StartDBServer( void );
       
   138 
       
   139 #if defined( __WINS__ )
       
   140 #else
       
   141 #define DRM_USE_SERIALNUMBER_URI
       
   142 #include <mmtsy_names.h>
       
   143 #endif
       
   144 
       
   145 
       
   146 #ifdef DRM_USE_SERIALNUMBER_URI
       
   147 LOCAL_C void DoUnloadPhoneModule( TAny* aAny );
       
   148 #endif
       
   149 
       
   150 // #define USE_RO_IMPORT
       
   151 
       
   152 // FORWARD DECLARATIONS
       
   153 
       
   154 // ============================= LOCAL FUNCTIONS ===============================
       
   155 // -----------------------------------------------------------------------------
       
   156 // Function Startup().
       
   157 // This function starts the actual DRM Rights server after initializing
       
   158 // the cleanup stack and active scheduler.
       
   159 // Returns: TInt: Symbian OS error code.
       
   160 // -----------------------------------------------------------------------------
       
   161 //
       
   162 LOCAL_C TInt Startup( void ) 
       
   163     {
       
   164     TInt error = KErrNone;
       
   165     CTrapCleanup* trap = CTrapCleanup::New();
       
   166     CActiveScheduler* scheduler = new CActiveScheduler();
       
   167     
       
   168     if ( trap && scheduler )
       
   169         {
       
   170         CActiveScheduler::Install( scheduler );
       
   171     
       
   172         error = StartDBServer();
       
   173         }
       
   174     else
       
   175         {
       
   176         error = KErrNoMemory;
       
   177         }
       
   178 
       
   179     delete scheduler; 
       
   180     scheduler = NULL;
       
   181     
       
   182     delete trap;
       
   183     trap = NULL;
       
   184     
       
   185     if ( error )
       
   186         {
       
   187         // Something went wrong. Release the client (if any).
       
   188         SignalClient();
       
   189         
       
   190         if ( error == KErrAlreadyExists )
       
   191             {
       
   192             error = KErrNone;
       
   193             }
       
   194         }
       
   195        
       
   196     return error;
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // Function SignalClient().
       
   201 // Signal the waiting client (one of them if any exists).
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void SignalClient( void )
       
   205     {
       
   206     RSemaphore semaphore;
       
   207     if ( !semaphore.OpenGlobal( DRMEngine::KDRMSemaphore ) )
       
   208         {
       
   209         semaphore.Signal();
       
   210         semaphore.Close();
       
   211         }
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // Function StartDBServer().
       
   216 // This function starts the actual server under TRAP harness and starts
       
   217 // waiting for connections. This function returns only if there has been
       
   218 // errors during server startup or the server is stopped for some reason.
       
   219 // 
       
   220 // Returns: TInt: Symbian OS error code.
       
   221 // -----------------------------------------------------------------------------
       
   222 TInt StartDBServer( void ) 
       
   223     {
       
   224     TInt error = KErrNone;
       
   225     CDRMRightsServer* server = NULL;
       
   226     TUint8 count = 0;
       
   227     
       
   228     do
       
   229         {
       
   230         DRMLOG2( _L( "RightsServer.exe: StartDBServer: %d" ), error );
       
   231         
       
   232         ++count;
       
   233         
       
   234         TRAP( error, ( server = CDRMRightsServer::NewL() ) );
       
   235         
       
   236         if ( error ) 
       
   237             {
       
   238             User::After( TTimeIntervalMicroSeconds32(KWaitingTime) );
       
   239             }
       
   240         
       
   241         } while( error && ( count <= KMaxStartTries ) );
       
   242         
       
   243     if( error ) 
       
   244         {
       
   245         DRMLOG2( _L( "RightsServer.exe: CDRMRightsServer::NewL failed: %d " ), error );
       
   246         // Failed
       
   247         return error;
       
   248         }
       
   249     
       
   250     // Release the semaphore if necessary.
       
   251     SignalClient();
       
   252    
       
   253     // Start waiting for connections
       
   254     CActiveScheduler::Start();
       
   255     
       
   256     // Dying...
       
   257     // Delete CDRMRigntsServer
       
   258         
       
   259     DRMLOG( _L( "RightsServer.exe: DB server dying..." ) );
       
   260 
       
   261     delete server;
       
   262     
       
   263     return KErrNone;
       
   264     }
       
   265 
       
   266 #ifdef DRM_USE_SERIALNUMBER_URI
       
   267 // -----------------------------------------------------------------------------
       
   268 // Function DoUnloadPhoneModule
       
   269 // Unload phone module
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void DoUnloadPhoneModule( TAny* aAny )
       
   273     {
       
   274     __ASSERT_DEBUG( aAny, User::Invariant() );
       
   275     TUnloadModule* module = ( TUnloadModule* ) aAny;
       
   276     module->iServer->UnloadPhoneModule( *( module->iName ) );
       
   277     }
       
   278 #endif
       
   279 
       
   280 #ifdef USE_RO_IMPORT
       
   281 // -----------------------------------------------------------------------------
       
   282 // PointerArrayResetDestroyAndClose
       
   283 // Template method used to push RPointerArrays to the cleanup stack. Takes
       
   284 // care of deleting all pointers in the array.
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 template<class S>
       
   288 void PointerArrayResetDestroyAndClose(TAny* aPtr)
       
   289     {
       
   290     (reinterpret_cast<RPointerArray<S>*>(aPtr))->ResetAndDestroy();
       
   291     (reinterpret_cast<RPointerArray<S>*>(aPtr))->Close();
       
   292     }
       
   293 #endif    
       
   294 
       
   295 // ============================ MEMBER FUNCTIONS ===============================
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CDRMRightsServer::NewLC
       
   299 // Two-phased constructor.
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 CDRMRightsServer* CDRMRightsServer::NewL()
       
   303     {
       
   304     CDRMRightsServer* self = new( ELeave ) CDRMRightsServer();
       
   305     
       
   306     CleanupStack::PushL( self );
       
   307     
       
   308     self->ConstructL();
       
   309     
       
   310     CleanupStack::Pop( self );
       
   311     
       
   312     return self;
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // Destructor
       
   317 // -----------------------------------------------------------------------------
       
   318 CDRMRightsServer::~CDRMRightsServer() 
       
   319     {
       
   320     DRMLOG( _L( "CDRMRightsServer::~" ) );
       
   321     
       
   322     delete iIMEI; iIMEI = NULL;
       
   323     
       
   324     delete iIMSI; iIMSI = NULL;
       
   325     
       
   326     delete iDb; iDb = NULL;
       
   327     
       
   328     iClock.Close();
       
   329     iCache.Close();
       
   330     
       
   331     iMeteringDb.Close();
       
   332     
       
   333     iFs.Close();
       
   334     iActiveCountConstraints.ResetAndDestroy();
       
   335     iActiveCountConstraints.Close();
       
   336     
       
   337     delete iBackupObserver;
       
   338     delete iBackupHandler;    
       
   339     delete iActiveBackupClient;
       
   340     delete iDbWatcher;
       
   341 
       
   342 #if 0    
       
   343     // Close and delete the shared data client
       
   344     if( iSharedDataClient ) 
       
   345         {
       
   346         iSharedDataClient->Close();
       
   347         delete iSharedDataClient;
       
   348         iSharedDataClient = NULL;
       
   349         }
       
   350 #endif
       
   351 
       
   352     if( iNotifier )
       
   353         {
       
   354         delete iNotifier; iNotifier = NULL;
       
   355         }
       
   356         
       
   357     //An empty semaphore
       
   358     RSemaphore semaphore;
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CDRMRightsServer::GetSecureTime
       
   363 // Fetch the time from (secure) source.
       
   364 // -----------------------------------------------------------------------------
       
   365 //
       
   366 TBool CDRMRightsServer::GetSecureTime( TTime& aTime ) const
       
   367     {
       
   368     DRMClock::ESecurityLevel secLevel = DRMClock::KInsecure;
       
   369 	
       
   370 	TInt timezone( 0 );
       
   371 	
       
   372     iClock.GetSecureTime( aTime, timezone, secLevel );
       
   373 	
       
   374     if( secLevel == DRMClock::KSecure )
       
   375         {
       
   376         DRMLOG( _L( "CDRMRightsServer::GetSecureTime: Time is secure\r\n" ) );
       
   377         return ETrue;		
       
   378         }	
       
   379         
       
   380     DRMLOG( _L( "CDRMRightsServer::GetSecureTime: Time is not secure\r\n" ) );    	
       
   381 
       
   382     return EFalse;
       
   383     }
       
   384 
       
   385 // -----------------------------------------------------------------------------
       
   386 // CDRMRightsServer::Notifier
       
   387 // Return a handle to DRM Notifier.
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 CDRMNotifier& CDRMRightsServer::Notifier()
       
   391     {
       
   392     return *iNotifier;
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CDRMRightsServer::Notifier
       
   397 // Return a handle to DRM Notifier.
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 CDRMRightsDB& CDRMRightsServer::Database()
       
   401     {
       
   402     return *iDb;
       
   403     }
       
   404 
       
   405 RFs& CDRMRightsServer::FileServerSession()
       
   406     {
       
   407     return iFs;
       
   408     }
       
   409     
       
   410     
       
   411 RDRMReplayCache& CDRMRightsServer::ReplayCache()
       
   412     {
       
   413     return iCache;
       
   414     }
       
   415 
       
   416 
       
   417 RDrmMeteringDb& CDRMRightsServer::MeteringDatabase()
       
   418     {
       
   419     return iMeteringDb;
       
   420     }
       
   421 
       
   422 // -----------------------------------------------------------------------------
       
   423 // CDRMRightsServer::HandleNotifyL
       
   424 // Forward the event to the database.
       
   425 // -----------------------------------------------------------------------------
       
   426 //
       
   427 void CDRMRightsServer::HandleNotifyL(const TUid /*aUid*/,
       
   428                                      const TDesC& /*aKey*/,
       
   429                                      const TDesC& /*aValue*/)  
       
   430     {   
       
   431     /* XXX Backup via Publish/Subscribe
       
   432     __ASSERT_DEBUG( iDb, User::Invariant() );
       
   433     TInt value = -1;
       
   434     TLex16 parser( aValue );
       
   435 
       
   436     if ( aUid == KSDUidSystem )
       
   437         {
       
   438         // Check if it's a backup / restore status event
       
   439         if( !aKey.Compare( KBackupRestoreStatus ) )
       
   440             {
       
   441             User::LeaveIfError( parser.Val( value ) );
       
   442             if( value == 3 ) // Complete
       
   443                 {
       
   444                 iDb->MergeDBL();
       
   445                 }
       
   446             }
       
   447         // Check if it's a drm backup restore status event
       
   448         else if ( aUid == KSDUidSystem )
       
   449             {
       
   450             if( !aKey.Compare( KDRMBackupRestoreStatus ) )
       
   451                 {
       
   452                 User::LeaveIfError( parser.Val( value ) );
       
   453 
       
   454                 if( value == 1 ) // PrepareForBackup
       
   455                     {
       
   456                     TRAPD( error, iDb->BackupDBL( KNullDesC, 
       
   457                                                   KNullDesC8 ) );
       
   458                     // Notify that it's done
       
   459                     User::LeaveIfError( iSharedDataClient->AssignToTemporaryFile(
       
   460                                         KSDUidSystem ) );
       
   461                     User::LeaveIfError( iSharedDataClient->SetInt( 
       
   462                                         KDRMBackupRestoreStatus, 0 ) );
       
   463                     iSharedDataClient->Flush();
       
   464                     }
       
   465                 }
       
   466             }
       
   467         }
       
   468     */
       
   469     }
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // CDRMRightsServer::RunErrorL
       
   473 // From CActive. Complete the request and restart the scheduler.
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 TInt CDRMRightsServer::RunError( TInt aError ) 
       
   477     {
       
   478     DRMLOG2( _L( "CDRMRightsServer::RunError: %d" ), aError );
       
   479 
       
   480     // Inform the client.
       
   481     if ( !Message().IsNull() )
       
   482         {
       
   483         Message().Complete( aError );
       
   484         }
       
   485     
       
   486     // Restart the scheduler.
       
   487     ReStart();
       
   488     
       
   489     // Error handled.
       
   490     return KErrNone;
       
   491     }
       
   492 
       
   493 // -----------------------------------------------------------------------------
       
   494 // CDRMRightsServer::NewSessionL
       
   495 // Called when a client requires a new instance.
       
   496 // -----------------------------------------------------------------------------
       
   497 CSession2* CDRMRightsServer::NewSessionL( const TVersion& aVersion,
       
   498                                           const RMessage2& /*aMessage*/ ) const
       
   499     {
       
   500     DRMLOG( _L( "CDRMRightsServer::NewSessionL" ) );
       
   501 
       
   502     if ( ! User::QueryVersionSupported( TVersion( DRMEngine::KServerMajorVersion,
       
   503         DRMEngine::KServerMinorVersion,
       
   504         DRMEngine::KServerBuildVersion ), 
       
   505         aVersion ) ) 
       
   506         {
       
   507         // Sorry, no can do.
       
   508         User::Leave( KErrNotSupported );
       
   509         }
       
   510 
       
   511     DRMLOG( _L( "CDRMRightsServer::NewSessionL: Creating a new session" ) );
       
   512 
       
   513     return CDRMDbSession::NewL();
       
   514     }
       
   515 // -----------------------------------------------------------------------------
       
   516 // CDRMRightsServer::CDRMRightsServer
       
   517 // C++ default constructor can NOT contain any code, that
       
   518 // might leave.
       
   519 // -----------------------------------------------------------------------------
       
   520 //
       
   521 CDRMRightsServer::CDRMRightsServer() : 
       
   522     CServer2( EPriorityStandard ),
       
   523     iIMEI( NULL ),
       
   524     iArmed( EFalse ),    
       
   525     iIMSI( NULL ),
       
   526     iGetImsi( ETrue )
       
   527     {
       
   528     // Nothing
       
   529     }
       
   530     
       
   531 // -----------------------------------------------------------------------------
       
   532 // CDRMRightsServer::ConstructL
       
   533 // Symbian 2nd phase constructor can leave.
       
   534 // -----------------------------------------------------------------------------
       
   535 //
       
   536 void CDRMRightsServer::ConstructL() 
       
   537     {
       
   538     DRMLOG( _L( "CDRMRightsServer::ConstructL" ) );
       
   539 
       
   540     TDRMKey key;
       
   541     RSemaphore semaphore;
       
   542     RProcess currentprocess;
       
   543     
       
   544     // Ignore errors
       
   545     User::RenameThread( KRightsServerThread );
       
   546     User::LeaveIfError( iFs.Connect() );
       
   547     
       
   548 #ifndef RD_MULTIPLE_DRIVE
       
   549     
       
   550     // Ignore errors
       
   551     iFs.MkDirAll( KDRMDbTempPath );
       
   552     
       
   553 #else //RD_MULTIPLE_DRIVE
       
   554     
       
   555     TFileName tempPath;
       
   556     TFileName tempPath2;
       
   557     TFileName tempRemovablePath;
       
   558     TInt driveNumber( -1 );
       
   559     TChar driveLetter;
       
   560     TChar driveLetterRemovable;
       
   561     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   562 	iFs.DriveToChar( driveNumber, driveLetter );
       
   563 	
       
   564 	tempPath.Format( KDbTempPath, (TUint)driveLetter );
       
   565     
       
   566     // Ignore errors
       
   567     iFs.MkDirAll( tempPath );
       
   568     
       
   569 #endif
       
   570         
       
   571     DRMLOG( _L( "CDRMRightsServer::ConstructL: SharedDataClient" ) );
       
   572 
       
   573     // Create and instance of the shared data client
       
   574     // iSharedDataClient = new (ELeave) RSharedDataClient(this);
       
   575 
       
   576     // Connecting to the shared data server
       
   577     // User::LeaveIfError(iSharedDataClient->Connect());
       
   578 
       
   579     /* XXX Backup via Publish/Subscribe
       
   580     User::LeaveIfError(iSharedDataClient->NotifyChange(
       
   581         KSDUidSystem, &KBackupRestoreStatus ) );
       
   582     User::LeaveIfError(iSharedDataClient->NotifyChange(
       
   583         KSDUidSystem, &KDRMBackupRestoreStatus) );
       
   584     */  
       
   585       
       
   586       
       
   587     GetDbKeyL( key );
       
   588     
       
   589     
       
   590     DRMLOG( _L( "CDRMRightsServer::ConstructL: database" ) );
       
   591 
       
   592     GetIMEIL();
       
   593     
       
   594     // Create the imsi pointer array:
       
   595     iIMSI = CDRMPointerArray<HBufC8>::NewL();
       
   596     iIMSI->SetAutoCleanup(ETrue);
       
   597     
       
   598     GetIMSIL();
       
   599     
       
   600 #ifndef RD_MULTIPLE_DRIVE
       
   601     
       
   602     iDb = CDRMRightsDB::NewL( iFs, KRightsDir, key, *iIMEI );
       
   603     
       
   604 #else //RD_MULTIPLE_DRIVE
       
   605     
       
   606     tempPath.Format( KRightsDir, (TUint)driveLetter );
       
   607     
       
   608     iDb = CDRMRightsDB::NewL( iFs, tempPath, key, *iIMEI );
       
   609     
       
   610 #endif
       
   611         
       
   612     key.FillZ();
       
   613         
       
   614     DRMLOG( _L( "CDRMRightsServer::ConstructL: DB started." ) );
       
   615 
       
   616     DRMLOG( _L( "CDRMRightsServer::ConstructL: Starting Notifier ." ) );
       
   617         
       
   618     User::LeaveIfError( semaphore.CreateGlobal( KDRMEngCommonSemaphore, 0 ) );
       
   619     CleanupClosePushL( semaphore );
       
   620     
       
   621     StartThreadL( DRMNotifier::KServerName, StartupNotifier, semaphore );
       
   622     DRMLOG( _L( "CDRMRightsServer::ConstructL: Notifier thread created." ) );
       
   623     
       
   624     StartThreadL( Roap::KServerName, StartupRoapStorage, semaphore );
       
   625     DRMLOG( _L( "CDRMRightsServer::ConstructL: ROAP thread created." ) );
       
   626     
       
   627 #ifdef __DRM_CLOCK
       
   628     StartThreadL( DRMClock::KServerName, StartupClock, semaphore );
       
   629     DRMLOG( _L( "CDRMRightsServer::ConstructL: clock thread created." ) );
       
   630 #endif
       
   631     
       
   632     CleanupStack::PopAndDestroy(); // semaphore
       
   633     
       
   634     iNotifier = CDRMNotifier::NewL();
       
   635     
       
   636     iCache.Set( iFs );
       
   637 
       
   638 #ifndef RD_MULTIPLE_DRIVE
       
   639     
       
   640     iCache.InitL( KTimedReplayCacheFile, KPlainReplayCacheFile );
       
   641 
       
   642 #ifdef RD_DRM_METERING               
       
   643     iMeteringDb.Set( iFs );
       
   644     iMeteringDb.InitL( KMeteringDataBaseFile );
       
   645 #endif
       
   646     
       
   647 #else //RD_MULTIPLE_DRIVE
       
   648     
       
   649     tempPath.Format( KTimedReplayCacheFile, (TUint)driveLetter );
       
   650     tempPath2.Format( KPlainReplayCacheFile, (TUint)driveLetter );
       
   651     
       
   652     iCache.InitL( tempPath, tempPath2 );
       
   653     
       
   654 #ifdef RD_DRM_METERING               
       
   655     
       
   656     tempPath.Format( KMeteringDataBaseFile, (TUint)driveLetter );
       
   657     
       
   658     iMeteringDb.Set( iFs );
       
   659     iMeteringDb.InitL( tempPath );
       
   660 
       
   661 #endif
       
   662     
       
   663 #endif
       
   664     
       
   665     User::LeaveIfError( iClock.Connect() );
       
   666 
       
   667     // xoma header list creation
       
   668     iXOmaHeaders = new (ELeave) RPointerArray< CDRMXOma >();
       
   669     
       
   670     // p/s
       
   671     iBackupObserver = CDRMBackupObserver::NewL( *(const_cast<CDRMRightsServer*>(this)));
       
   672     iBackupObserver->Start();
       
   673     
       
   674 #ifdef USE_RO_IMPORT
       
   675     // Import any OMA DRM 1.0 RO in the import directory, ignore all errors (except
       
   676     // when checking the default removable mass storage)
       
   677     TInt r = KErrNone;
       
   678     
       
   679 #ifndef RD_MULTIPLE_DRIVE
       
   680     
       
   681     TRAP( r, ImportRightsObjectsL( KInternalImportDir ) );
       
   682     TRAP( r, ImportRightsObjectsL( KUserDiskImportDir ) );
       
   683     
       
   684 #else //RD_MULTIPLE_DRIVE
       
   685     
       
   686     tempPath.Format( KInternalImportDir, (TUint)driveLetter );
       
   687     
       
   688     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultMassStorage, driveNumber );
       
   689 	iFs.DriveToChar( driveNumber, driveLetter );
       
   690     
       
   691 	// Default mass storage is usually eMMC
       
   692     tempPath2.Format( KUserDiskImportDir, (TUint)driveLetter );
       
   693     
       
   694     // Find out if a removable mass storage also exists
       
   695     r = DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRemovableMassStorage, driveNumber );
       
   696     iFs.DriveToChar( driveNumber, driveLetterRemovable );
       
   697     
       
   698     // Import is not needed from the default removable mass storage drive if the drive 
       
   699     // letter of the default mass storage and the default removable mass storage are 
       
   700     // the same or the removable mass storage is not supported
       
   701     if ( ( driveLetter != driveLetterRemovable ) && ( r == KErrNone ) ) 
       
   702         {
       
   703         tempRemovablePath.Format( KUserRemovableDiskImportDir, (TUint)driveLetterRemovable );
       
   704         TRAP( r, ImportRightsObjectsL( tempRemovablePath ) );
       
   705         }
       
   706     
       
   707     TRAP( r, ImportRightsObjectsL( tempPath ) );
       
   708     TRAP( r, ImportRightsObjectsL( tempPath2 ) );
       
   709     
       
   710 #endif
       
   711     
       
   712 #endif
       
   713 
       
   714     // Add the server to the scheduler.
       
   715     StartL( DRMEngine::KServerName );
       
   716 
       
   717     // Start watching our RDB
       
   718     iDbWatcher = CDbWatcher::NewL( *this );
       
   719     iDbWatcher->StartWatching();
       
   720     
       
   721     // Start watching the helper server
       
   722     iProcWatcher = CProcWatcher::NewL( *this, _L( "*DcfRepSrv*" ), _L( "DcfRepSrv" ) );
       
   723     iProcWatcher->StartWatching();
       
   724     
       
   725     // Ready to watch
       
   726     iArmed = ETrue;
       
   727 
       
   728     __UHEAP_MARK;
       
   729     TRAP( r, FeatureManager::InitializeLibL() );
       
   730     if( !r && FeatureManager::FeatureSupported( KFeatureIdWindowsMediaDrm ) ) 
       
   731         {
       
   732         static const TInt KGateOrdinal = 1;
       
   733         RLibrary library;
       
   734         r = library.Load( KWmDrmClientWrapperName );
       
   735         if( !r )
       
   736             {
       
   737             CWmDrmClientWrapper* wrapper = NULL;
       
   738             TLibraryFunction function = library.Lookup( KGateOrdinal );
       
   739             if( function != NULL )
       
   740                 {
       
   741                 __UHEAP_MARK;
       
   742                 TRAP( r, wrapper = reinterpret_cast<CWmDrmClientWrapper*>( function() ) );
       
   743                 if( !r )
       
   744                     {
       
   745                     r = wrapper->Connect();
       
   746                     }
       
   747                 delete wrapper;
       
   748                 __UHEAP_MARKEND;
       
   749                 }
       
   750             }
       
   751         library.Close();    
       
   752         }
       
   753     FeatureManager::UnInitializeLib();
       
   754     __UHEAP_MARKEND;  
       
   755     }
       
   756  
       
   757 // -----------------------------------------------------------------------------   
       
   758 // CDRMRightsServer::StartThreadL
       
   759 // Start a new thread.
       
   760 // -----------------------------------------------------------------------------    
       
   761 void CDRMRightsServer::StartThreadL( const TDesC& aThreadName,
       
   762                                      TThreadFunction aFunc,
       
   763                                      RSemaphore& aSemaphore )
       
   764     {
       
   765     RThread thread;
       
   766     
       
   767     User::LeaveIfError( 
       
   768           thread.Create( aThreadName,
       
   769                          aFunc, 
       
   770                          KDefaultStackSize,
       
   771                          KMinHeapSize, 
       
   772                          KMaxHeapsize,
       
   773                          NULL ) );
       
   774     
       
   775     thread.Resume();
       
   776     
       
   777     aSemaphore.Wait();
       
   778     
       
   779     thread.Close();
       
   780     }
       
   781     
       
   782 // -----------------------------------------------------------------------------
       
   783 // CDRMRightsServer::GetDbKeyL
       
   784 // Fetches the rights database key from Wallet or uses a constant 
       
   785 // key if Wallet is not supported.
       
   786 // -----------------------------------------------------------------------------
       
   787 //
       
   788 void CDRMRightsServer::GetDbKeyL( TDRMKey& aKey  ) 
       
   789     {
       
   790     TInt r = KErrNone;
       
   791     
       
   792     DRMLOG( _L( "CDRMRightsServer::GetDbKey" ) );
       
   793     MDrmKeyStorage* storage = DrmKeyStorageNewL();
       
   794     TRAP( r, storage->GetDeviceSpecificKeyL( aKey ) );
       
   795     delete storage;
       
   796     User::LeaveIfError( r );
       
   797     }
       
   798 
       
   799 // -----------------------------------------------------------------------------
       
   800 // CDRMRightsServer::GenerateKeyL
       
   801 // Generates the actual key based on the given key seed.
       
   802 // -----------------------------------------------------------------------------
       
   803 //
       
   804 void CDRMRightsServer::GenerateKeyL( HBufC*& aKeySeed, 
       
   805                                     TDRMKey& aKey ) const
       
   806     {
       
   807     __ASSERT_ALWAYS( aKeySeed->Size() >= KDRMKeyLength, 
       
   808         User::Leave( KErrUnderflow ) );
       
   809     
       
   810     TPtrC8 key( reinterpret_cast< TUint8* >( const_cast< TUint16* >( aKeySeed->Ptr() ) ),
       
   811                 KDRMKeyLength );
       
   812     
       
   813     aKey = key;
       
   814     }
       
   815 
       
   816 
       
   817 // -----------------------------------------------------------------------------
       
   818 // CDRMRightsServer::XOmaHeaders()
       
   819 // return the pointer of the X-Oma headers list 
       
   820 // -----------------------------------------------------------------------------
       
   821 //
       
   822 RPointerArray< CDRMXOma >& CDRMRightsServer::XOmaHeaders( void )
       
   823 	{
       
   824 	return *iXOmaHeaders;
       
   825 	}
       
   826 
       
   827 
       
   828 
       
   829 // -----------------------------------------------------------------------------
       
   830 // CDRMRightsServer::GetIMEIL
       
   831 // -----------------------------------------------------------------------------
       
   832 //
       
   833 const TDesC& CDRMRightsServer::GetIMEIL()
       
   834     {
       
   835     if ( iIMEI )
       
   836         {
       
   837         return *iIMEI;
       
   838         }
       
   839     
       
   840 #ifdef DRM_USE_SERIALNUMBER_URI
       
   841     TInt error( KErrNone );
       
   842     TInt count( 0 );
       
   843     TInt count2( 0 );
       
   844     TUint32 caps( 0 );
       
   845     TBool found (EFalse);
       
   846     
       
   847     RTelServer etelServer;
       
   848     RMobilePhone phone;
       
   849     
       
   850     TUint KMaxImeiTries = 5;
       
   851     
       
   852     for ( TUint8 i = 0; i < KMaxImeiTries; ++i )
       
   853         {
       
   854         error = etelServer.Connect();
       
   855         if ( error )
       
   856             {
       
   857             User::After( TTimeIntervalMicroSeconds32( KWaitingTime ) );
       
   858             }
       
   859         else 
       
   860             {
       
   861             break;
       
   862             }
       
   863         }
       
   864     
       
   865     User::LeaveIfError( error );
       
   866     CleanupClosePushL( etelServer );
       
   867     
       
   868     User::LeaveIfError( etelServer.LoadPhoneModule( KMmTsyModuleName ) );  
       
   869     
       
   870     TUnloadModule unload;
       
   871     unload.iServer = &etelServer;
       
   872     unload.iName = &KMmTsyModuleName;
       
   873     
       
   874     TCleanupItem item( DoUnloadPhoneModule, &unload );
       
   875     CleanupStack::PushL( item );
       
   876     User::LeaveIfError( etelServer.EnumeratePhones( count ) );
       
   877         
       
   878     for ( count2 = 0; count2 < count && !found; ++count2 )
       
   879         {
       
   880         RTelServer::TPhoneInfo phoneInfo;
       
   881         User::LeaveIfError( etelServer.GetTsyName( count2, phoneInfo.iName ) );
       
   882         
       
   883         if ( phoneInfo.iName.CompareF(KMmTsyModuleName()) == 0 )   
       
   884            {
       
   885             User::LeaveIfError( etelServer.GetPhoneInfo( count2, phoneInfo ) );
       
   886             User::LeaveIfError( phone.Open( etelServer, phoneInfo.iName ) );
       
   887             CleanupClosePushL( phone );
       
   888             found = ETrue;
       
   889             }
       
   890         }
       
   891 
       
   892     if ( !found )
       
   893         {
       
   894         // Not found.
       
   895         User::Leave( KErrNotFound );
       
   896         }
       
   897    
       
   898     User::LeaveIfError( phone.GetIdentityCaps( caps ) );
       
   899     if ( caps & RMobilePhone::KCapsGetSerialNumber )
       
   900         {
       
   901         RMobilePhone::TMobilePhoneIdentityV1 id;
       
   902         TRequestStatus status;
       
   903     
       
   904         phone.GetPhoneId( status, id );
       
   905         
       
   906         User::WaitForRequest( status );
       
   907         
       
   908         User::LeaveIfError( status.Int() );
       
   909         
       
   910         iIMEI = id.iSerialNumber.AllocL();
       
   911         
       
   912         CleanupStack::PopAndDestroy( 3 ); // phone, item, etelServer
       
   913         
       
   914         HBufC8* buf = HBufC8::NewL( iIMEI->Size() );
       
   915         TPtr8 ptr( buf->Des() );
       
   916         ptr.Copy( *iIMEI );
       
   917         
       
   918         DRMLOG(_L("IMEI:"));
       
   919         DRMLOGHEX(ptr);
       
   920         delete buf;
       
   921     
       
   922         return *iIMEI;
       
   923         }
       
   924     
       
   925     User::Leave( KErrNotFound );
       
   926     
       
   927     // Never happens...
       
   928     return *iIMEI; 
       
   929     
       
   930 #else
       
   931     _LIT( KDefaultSerialNumber, "123456789123456789" );
       
   932     iIMEI = KDefaultSerialNumber().AllocL();
       
   933         
       
   934     return *iIMEI;
       
   935 #endif
       
   936     }
       
   937 
       
   938 // -----------------------------------------------------------------------------
       
   939 // CDRMRightsServer::GetIMSIL
       
   940 // -----------------------------------------------------------------------------
       
   941 //
       
   942 const CDRMPointerArray<HBufC8>& CDRMRightsServer::GetIMSIL()
       
   943     {
       
   944     
       
   945     if ( !iGetImsi )
       
   946         {
       
   947         return *iIMSI;
       
   948         }
       
   949 
       
   950 #ifndef __WINS__   
       
   951     TInt error( KErrNone );
       
   952     TInt count( 0 );
       
   953     TInt count2( 0 );
       
   954     TUint32 caps( 0 );
       
   955     TBool found (EFalse);
       
   956     HBufC8* imsi = NULL;
       
   957     HBufC8* imsiNumber = NULL;
       
   958     
       
   959     RTelServer etelServer;
       
   960     RMobilePhone phone;
       
   961     
       
   962     TUint KMaxImeiTries = 5;    
       
   963     for ( TUint8 i = 0; i < KMaxImeiTries; ++i )
       
   964         {
       
   965         error = etelServer.Connect();
       
   966         if ( error )
       
   967             {
       
   968             User::After( TTimeIntervalMicroSeconds32( KWaitingTime ) );
       
   969             }
       
   970         else 
       
   971             {
       
   972             break;
       
   973             }
       
   974         }
       
   975     
       
   976     User::LeaveIfError( error );
       
   977     CleanupClosePushL( etelServer );     
       
   978     User::LeaveIfError( etelServer.LoadPhoneModule( KMmTsyModuleName ) );  
       
   979     
       
   980     TUnloadModule unload;
       
   981     unload.iServer = &etelServer;
       
   982     unload.iName = &KMmTsyModuleName;
       
   983     
       
   984     TCleanupItem item( DoUnloadPhoneModule, &unload );
       
   985     CleanupStack::PushL( item );
       
   986     
       
   987     User::LeaveIfError( etelServer.EnumeratePhones( count ) );
       
   988         
       
   989     for ( count2 = 0; count2 < count && !found; ++count2 )
       
   990         {
       
   991         RTelServer::TPhoneInfo phoneInfo;
       
   992         User::LeaveIfError( etelServer.GetTsyName( count2, phoneInfo.iName ) );
       
   993         
       
   994         if ( phoneInfo.iName.CompareF(KMmTsyModuleName()) == 0 )   
       
   995            {
       
   996             User::LeaveIfError( etelServer.GetPhoneInfo( count2, phoneInfo ) );
       
   997             User::LeaveIfError( phone.Open( etelServer, phoneInfo.iName ) );
       
   998             CleanupClosePushL( phone );
       
   999             found = ETrue;
       
  1000             }
       
  1001         }
       
  1002 
       
  1003     if ( !found )
       
  1004         {
       
  1005         // Not found.
       
  1006         User::Leave( KErrNotFound );
       
  1007         }
       
  1008    
       
  1009  
       
  1010     User::LeaveIfError( phone.GetIdentityCaps( caps ) );
       
  1011 
       
  1012     if( caps & RMobilePhone::KCapsGetSubscriberId )
       
  1013         {
       
  1014         RMobilePhone::TMobilePhoneSubscriberId imsiId;        
       
  1015         TRequestStatus status;
       
  1016     
       
  1017         phone.GetSubscriberId( status, imsiId );
       
  1018         
       
  1019         User::WaitForRequest( status );
       
  1020         
       
  1021         if( ! status.Int() )
       
  1022             {             
       
  1023             imsi = HBufC8::NewMaxLC( imsiId.Length() + KImsiId().Size() );
       
  1024             TPtr8 imsiPtr(const_cast<TUint8*>(imsi->Ptr()), 0, imsi->Size());
       
  1025       
       
  1026             imsiNumber = CnvUtfConverter::ConvertFromUnicodeToUtf8L( imsiId );
       
  1027             CleanupStack::PushL( imsiNumber );
       
  1028             
       
  1029             imsiPtr.Copy( KImsiId() );
       
  1030             imsiPtr.Append( *imsiNumber );
       
  1031             CleanupStack::PopAndDestroy(); // imsiNumber
       
  1032             }
       
  1033         else
       
  1034             {            
       
  1035             imsi = NULL;
       
  1036             }
       
  1037         }
       
  1038     else
       
  1039         {         
       
  1040         imsi = NULL;
       
  1041         } 
       
  1042     
       
  1043     
       
  1044     // Clean up whatever is in there
       
  1045     iIMSI->ResetAndDestroy();
       
  1046     
       
  1047     if( imsi ) 
       
  1048         { 
       
  1049         // if we got it we wont try again             
       
  1050         iIMSI->AppendL( imsi );
       
  1051         CleanupStack::Pop(); // imsi
       
  1052         iGetImsi = EFalse;            
       
  1053         }
       
  1054 
       
  1055     // Check for possible extra IMSI individual constraints
       
  1056     AppendExtendedIndividualConstraintsL(&phone);
       
  1057     
       
  1058     CleanupStack::PopAndDestroy(); // phone
       
  1059     CleanupStack::PopAndDestroy(); // cleanup item
       
  1060     CleanupStack::PopAndDestroy(); // etel server
       
  1061 
       
  1062     return *iIMSI; 
       
  1063     
       
  1064 #else
       
  1065     HBufC8* imsi = NULL;
       
  1066     
       
  1067     if( iGetImsi ) 
       
  1068         {
       
  1069         iGetImsi = EFalse;
       
  1070         _LIT8( KDefaultSerialNumber, "IMSI:123456789123456789" );
       
  1071         imsi = KDefaultSerialNumber().AllocLC();
       
  1072         iIMSI->AppendL( imsi );
       
  1073         CleanupStack::Pop();        
       
  1074         AppendExtendedIndividualConstraintsL();
       
  1075         }
       
  1076 
       
  1077         
       
  1078     return *iIMSI;
       
  1079 #endif // __WINS__
       
  1080     }
       
  1081 
       
  1082 // -----------------------------------------------------------------------------   
       
  1083 // CDRMRightsServer::AppendExtendedIndividualConstraintsL
       
  1084 // If the extension DLL exists it is loaded and used to obtain additional
       
  1085 // valid individual constraints
       
  1086 // -----------------------------------------------------------------------------    
       
  1087 void CDRMRightsServer::AppendExtendedIndividualConstraintsL(RMobilePhone* aMobilePhone)
       
  1088     {    	    	
       
  1089     // Load the externsion DLL
       
  1090     RLibrary lib;
       
  1091     
       
  1092 #ifndef RD_MULTIPLE_DRIVE
       
  1093     
       
  1094     if (lib.LoadRomLibrary(KDRMIndividualConstraintExtensionDll,KNullDesC)==KErrNone)
       
  1095     
       
  1096 #else //RD_MULTIPLE_DRIVE
       
  1097     
       
  1098     TInt driveNumber( -1 );
       
  1099     TChar driveLetter;
       
  1100     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
  1101 	iFs.DriveToChar( driveNumber, driveLetter );
       
  1102 	
       
  1103 	TFileName individualConstraindExtensionDll;
       
  1104 	individualConstraindExtensionDll.Format( 
       
  1105 	                    KIndividualConstraintExtensionDll, (TUint)driveLetter );
       
  1106     
       
  1107     if ( lib.LoadRomLibrary( individualConstraindExtensionDll, KNullDesC ) == KErrNone )
       
  1108     
       
  1109 #endif
       
  1110     
       
  1111     	{
       
  1112         CleanupClosePushL(lib);
       
  1113 
       
  1114         // Get first exported ordinal - factory method returning 
       
  1115         // MDRMIndividualConstraintExtension*
       
  1116         TLibraryFunction factory = lib.Lookup(1); 
       
  1117 
       
  1118         if (factory)
       
  1119             {
       
  1120             // Instantiate object
       
  1121             MDRMIndividualConstraintExtension* extendedConstraints = 
       
  1122                 reinterpret_cast<MDRMIndividualConstraintExtension*>(factory());
       
  1123 
       
  1124             if (extendedConstraints)
       
  1125                 {
       
  1126 			    CleanupStack::PushL(TCleanupItem(Release,extendedConstraints));
       
  1127                 extendedConstraints->AppendConstraintsL(*iIMSI,aMobilePhone);
       
  1128 			    CleanupStack::PopAndDestroy(extendedConstraints); //calls Release
       
  1129                 }
       
  1130             }
       
  1131 
       
  1132         // unload library
       
  1133         CleanupStack::PopAndDestroy(&lib); //close
       
  1134         }
       
  1135     }
       
  1136 
       
  1137 // -----------------------------------------------------------------------------
       
  1138 // CDRMRightsServer::Release
       
  1139 // -----------------------------------------------------------------------------
       
  1140 void CDRMRightsServer::Release(TAny* aIndividualConstraintExtension)
       
  1141     {
       
  1142     MDRMIndividualConstraintExtension* extendedConstraints = 
       
  1143     	reinterpret_cast<MDRMIndividualConstraintExtension*>(aIndividualConstraintExtension);
       
  1144     extendedConstraints->Release(); //free resources
       
  1145     }
       
  1146 
       
  1147 // -----------------------------------------------------------------------------
       
  1148 // CDRMRightsServer::HandleBackupEventL
       
  1149 // Handle Backup Events
       
  1150 // -----------------------------------------------------------------------------
       
  1151 //
       
  1152 
       
  1153 void CDRMRightsServer::HandleBackupEventL( TInt aBackupEvent )
       
  1154     {
       
  1155 	//RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::BackupCalled\n\r"));    
       
  1156 
       
  1157     //conn::TBURPartType eventType;
       
  1158     //conn::TBackupIncType incType;
       
  1159     TDriveList aDriveList;
       
  1160     
       
  1161     //RFileLogger::WriteFormat(KLogDir, KLogName, EFileLoggingModeAppend, _L8("backupevent: %d"), aBackupEvent);              
       
  1162     
       
  1163     // If there is no operation going or state is normal
       
  1164     // Delete the client and handler
       
  1165 
       
  1166     if( aBackupEvent == conn::EBURUnset ||
       
  1167         aBackupEvent & conn::EBURNormal )
       
  1168         {
       
  1169         /*
       
  1170         if( aBackupEvent == conn::EBURUnset )
       
  1171             {
       
  1172             	        RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Unset\n\r"));                
       
  1173             }
       
  1174         else 
       
  1175             {
       
  1176 	        RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Normal\n\r"));                
       
  1177             } 
       
  1178         */                 
       
  1179         if( iActiveBackupClient )
       
  1180             {
       
  1181             delete iActiveBackupClient;
       
  1182             iActiveBackupClient = NULL;    
       
  1183             }
       
  1184         
       
  1185         if( iBackupHandler )
       
  1186             {
       
  1187             delete iBackupHandler;
       
  1188             iBackupHandler = NULL;    
       
  1189             }
       
  1190         }  
       
  1191     else if( aBackupEvent & conn::EBURBackupFull ||
       
  1192              aBackupEvent & conn::EBURRestoreFull )
       
  1193         {
       
  1194 	    //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Full\n\r"));         
       
  1195         // ab handler
       
  1196         iBackupHandler = CDRMBackup::NewL( iDb, iFs );
       
  1197     
       
  1198         // ab client
       
  1199         iActiveBackupClient = conn::CActiveBackupClient::NewL( iBackupHandler );
       
  1200         
       
  1201         // Confirm that we have done everything if there even was anything to do
       
  1202 	    //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm F \n\r"));         
       
  1203         iActiveBackupClient->ConfirmReadyForBURL( KErrNone );        
       
  1204         }
       
  1205     else if( aBackupEvent & conn::EBURBackupPartial ||
       
  1206              aBackupEvent & conn::EBURRestorePartial )
       
  1207         {
       
  1208 	    //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Partial\n\r"));         
       
  1209         // ab handler
       
  1210         iBackupHandler = CDRMBackup::NewL( iDb, iFs );
       
  1211     
       
  1212         // ab client
       
  1213         iActiveBackupClient = conn::CActiveBackupClient::NewL( iBackupHandler );                
       
  1214 
       
  1215         if( !iActiveBackupClient->DoesPartialBURAffectMeL() ) 
       
  1216             {
       
  1217 	        //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::NotMe\n\r"));             
       
  1218             delete iActiveBackupClient;
       
  1219             iActiveBackupClient = NULL;    
       
  1220 
       
  1221             delete iBackupHandler;
       
  1222             iBackupHandler = NULL;    
       
  1223             }
       
  1224         else
       
  1225             {
       
  1226 	        //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm P \n\r"));            
       
  1227             // Confirm that we have done everything if there even was anything to do
       
  1228             iActiveBackupClient->ConfirmReadyForBURL( KErrNone );
       
  1229 	        //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm P Done \n\r"));                             
       
  1230             }    
       
  1231         }
       
  1232     else 
       
  1233         {
       
  1234 	    //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Argument\n\r"));         
       
  1235         // Unknown operation
       
  1236         User::Leave(KErrArgument);    
       
  1237         }
       
  1238     };  
       
  1239 
       
  1240 // -----------------------------------------------------------------------------
       
  1241 // CDRMRightsServer::WatchedObjectChangedL
       
  1242 // Handle Backup Events
       
  1243 // -----------------------------------------------------------------------------
       
  1244 //
       
  1245 void CDRMRightsServer::WatchedObjectChangedL( const TDesC& aObject )
       
  1246     {
       
  1247     DRMLOG( _L( "CDRMRightsServer::WatchedObjectChangedL ->" ) );
       
  1248     DRMLOG( aObject );
       
  1249     
       
  1250     if ( aObject.Left( KDirIdentifier().Length() ) == KDirIdentifier &&
       
  1251          !iDb->Updating() && iArmed )
       
  1252         {
       
  1253 #ifdef _DEBUG
       
  1254         DRMLOG( _L( "RDB modified by outside party (DEBUG mode, not deleting the DB)" ) );
       
  1255 #else
       
  1256         DRMLOG( _L( "RDB modified by outside party, deleting the DB" ) );
       
  1257         iDb->MarkAsCorrupted();
       
  1258         RStarterSession starter;
       
  1259         User::LeaveIfError( starter.Connect() );
       
  1260         starter.Reset( RStarterSession::EDRMReset );
       
  1261         starter.Close();
       
  1262 #endif        
       
  1263         }
       
  1264     else if ( aObject.Left( KProcIdentifier().Length() ) == KProcIdentifier && iArmed )
       
  1265         {
       
  1266 #ifdef _DEBUG
       
  1267         DRMLOG( _L( "Peer process killed (DEBUG mode, not rebooting)" ) );
       
  1268 #else
       
  1269         DRMLOG( _L( "Peer process killed, rebooting" ) );
       
  1270         RStarterSession starter;
       
  1271         User::LeaveIfError( starter.Connect() );
       
  1272         starter.Reset( RStarterSession::EDRMReset );
       
  1273         starter.Close();
       
  1274 #endif
       
  1275         }
       
  1276     
       
  1277     DRMLOG( _L( "CDRMRightsServer::WatchedObjectChangedL <-" ) );
       
  1278     }
       
  1279     
       
  1280 // -----------------------------------------------------------------------------
       
  1281 // CDRMRightsServer::HasActiveCountConstraint
       
  1282 // Check ID for active count constraint
       
  1283 // -----------------------------------------------------------------------------
       
  1284 //
       
  1285 TBool CDRMRightsServer::HasActiveCountConstraint( const TDesC8& aContentId )
       
  1286     {
       
  1287     TInt i;
       
  1288     TBool r = EFalse;
       
  1289     
       
  1290     for ( i = 0; r == EFalse && i < iActiveCountConstraints.Count(); i++ )
       
  1291         {
       
  1292         if ( iActiveCountConstraints[i]->CompareF( aContentId ) == 0 )
       
  1293             {
       
  1294             r = ETrue;
       
  1295             }
       
  1296         }
       
  1297     return r;
       
  1298     }
       
  1299 
       
  1300 // -----------------------------------------------------------------------------
       
  1301 // CDRMRightsServer::RemoveActiveCountConstraint
       
  1302 // Remove ID from count constraint list
       
  1303 // -----------------------------------------------------------------------------
       
  1304 //
       
  1305 void CDRMRightsServer::RemoveActiveCountConstraint( const TDesC8& aContentId )
       
  1306     {
       
  1307     TInt i;
       
  1308     TInt r = KErrNotFound;
       
  1309     HBufC8* id = NULL;
       
  1310     
       
  1311     for ( i = 0; r == KErrNotFound && i < iActiveCountConstraints.Count(); i++ )
       
  1312         {
       
  1313         if ( iActiveCountConstraints[i]->CompareF( aContentId ) == 0 )
       
  1314             {
       
  1315             r = i;
       
  1316             }
       
  1317         }
       
  1318     if ( r != KErrNotFound )
       
  1319         {
       
  1320         id = iActiveCountConstraints[r];	
       
  1321         iActiveCountConstraints.Remove( r );
       
  1322         delete id;
       
  1323         id = NULL;
       
  1324         }
       
  1325     }
       
  1326 
       
  1327 // -----------------------------------------------------------------------------
       
  1328 // CDRMRightsServer::AddActiveCountConstraint
       
  1329 // Add ID to count constraint list
       
  1330 // -----------------------------------------------------------------------------
       
  1331 //
       
  1332 void CDRMRightsServer::AddActiveCountConstraintL( const TDesC8& aContentId )
       
  1333     {
       
  1334     if ( !HasActiveCountConstraint( aContentId ) )
       
  1335         {
       
  1336         iActiveCountConstraints.AppendL( aContentId.AllocL() );
       
  1337         }
       
  1338     }
       
  1339 
       
  1340 // -----------------------------------------------------------------------------
       
  1341 // CDRMRightsServer::StopWatchingL
       
  1342 // Delete the watchers
       
  1343 // -----------------------------------------------------------------------------
       
  1344 //
       
  1345 void CDRMRightsServer::StopWatchingL()
       
  1346     {
       
  1347     iArmed = EFalse;
       
  1348     }
       
  1349 
       
  1350 #ifdef USE_RO_IMPORT
       
  1351 // -----------------------------------------------------------------------------
       
  1352 // CDRMRightsServer::ImportRightsObjectsL
       
  1353 // Open the import directory and add all ROs that can be found there. ROs file
       
  1354 // names must end with .dr. Only OMA DRM 1.0 ROs in XML format are supported for
       
  1355 // security reasons
       
  1356 // -----------------------------------------------------------------------------
       
  1357 //
       
  1358 void CDRMRightsServer::ImportRightsObjectsL( const TDesC& aImportDir )
       
  1359     {
       
  1360     CDrmRightsParser* p;
       
  1361     HBufC8* d = NULL;
       
  1362     HBufC8* k = NULL;
       
  1363     RFs fs;
       
  1364     RFile file;
       
  1365     TInt size;
       
  1366     RPointerArray<CDRMRights> rights;
       
  1367     CDir* dir;
       
  1368     TFileName name;
       
  1369     TPtr8 ptr( NULL, 0 );
       
  1370     TInt i;
       
  1371     TInt r = KErrNone;
       
  1372     TCleanupItem listCleanup(PointerArrayResetDestroyAndClose<CDRMRights>,
       
  1373         &rights);
       
  1374     TDRMUniqueID id;
       
  1375     TTime time;
       
  1376     
       
  1377     DRMLOG( _L( "CDRMRightsServer::ImportRightsObjectsL" ) );
       
  1378     DRMLOG( aImportDir );
       
  1379     __UHEAP_MARK;
       
  1380     GetSecureTime( time );
       
  1381     p = CDrmRightsParser::NewL();
       
  1382     CleanupStack::PushL( p );
       
  1383     User::LeaveIfError( iFs.GetDir( aImportDir, KEntryAttNormal,
       
  1384         ESortNone, dir ) );
       
  1385     CleanupStack::PushL( dir );
       
  1386     for (i = 0; i < dir->Count(); i++)
       
  1387         {
       
  1388         name.Copy( aImportDir );
       
  1389         name.Append( (*dir)[i].iName );
       
  1390         if ( ( name.Length() > 3 && name.Right(3).CompareF( KDrSuffix ) == 0 ) )
       
  1391             {
       
  1392             User::LeaveIfError( file.Open( iFs, name, EFileRead ) );
       
  1393             CleanupClosePushL( file );
       
  1394             User::LeaveIfError( file.Size( size ) );
       
  1395             d = HBufC8::NewLC( size );
       
  1396             ptr.Set( d->Des() );
       
  1397             User::LeaveIfError( file.Read( ptr ) );
       
  1398             p->ParseL( ptr, rights );
       
  1399             if ( rights.Count() > 0 )
       
  1400                 {
       
  1401                 k = NULL;
       
  1402                 CleanupStack::PushL( listCleanup );
       
  1403                 CDRMPermission& permission = rights[0]->GetPermission();
       
  1404                 CDRMAsset& asset = rights[0]->GetAsset();
       
  1405                 
       
  1406                 // Add RO only if no rights are available at all for this content
       
  1407                 TRAP( r, k = iDb->GetDecryptionKeyL( *asset.iUid ) );
       
  1408                 if (k == NULL )
       
  1409                     {
       
  1410                     iDb->AddDBEntryL( *asset.iUid, permission, asset.iKey, id );
       
  1411                     }
       
  1412                 else
       
  1413                     {
       
  1414                     delete k;
       
  1415                     }
       
  1416                 CleanupStack::PopAndDestroy(); // listCleanup
       
  1417                 }
       
  1418             CleanupStack::PopAndDestroy( 2 ); // d, file
       
  1419             iFs.Delete( name );
       
  1420             }
       
  1421         }
       
  1422     CleanupStack::PopAndDestroy( 2 ); // dir, p
       
  1423     __UHEAP_MARKEND;
       
  1424     DRMLOG( _L( "CDRMRightsServer::ImportRightsObjectsL done" ) );
       
  1425     }
       
  1426 #endif    
       
  1427     
       
  1428 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1429 
       
  1430 
       
  1431 TInt E32Main() 
       
  1432     {
       
  1433     return Startup(); 
       
  1434     }
       
  1435 
       
  1436 
       
  1437 //  End of File