breakdeps/DRMEngine/DRMRightsServer.cpp
changeset 88 ca165d35976d
child 90 62156f66dbad
equal deleted inserted replaced
87:e9fb2728ea8b 88:ca165d35976d
       
     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 // CUsageUrl:
       
   298 
       
   299 //--------------------------------------------------------------------------
       
   300 // CUsageUrl::CUsageUrl
       
   301 // Storage class default constructor
       
   302 //--------------------------------------------------------------------------
       
   303 //
       
   304 CUsageUrl::CUsageUrl()
       
   305     {
       
   306     }
       
   307 
       
   308 //--------------------------------------------------------------------------
       
   309 // CUsageUrl::~CUsageUrl
       
   310 // Storage class destructor
       
   311 //--------------------------------------------------------------------------
       
   312 //
       
   313 CUsageUrl::~CUsageUrl()
       
   314     {
       
   315     delete iUrl;    
       
   316     }
       
   317 
       
   318 // CDRMRightsServer:
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CDRMRightsServer::NewLC
       
   322 // Two-phased constructor.
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 CDRMRightsServer* CDRMRightsServer::NewL()
       
   326     {
       
   327     CDRMRightsServer* self = new( ELeave ) CDRMRightsServer();
       
   328 
       
   329     CleanupStack::PushL( self );
       
   330 
       
   331     self->ConstructL();
       
   332 
       
   333     CleanupStack::Pop( self );
       
   334 
       
   335     return self;
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // Destructor
       
   340 // -----------------------------------------------------------------------------
       
   341 CDRMRightsServer::~CDRMRightsServer()
       
   342     {
       
   343     DRMLOG( _L( "CDRMRightsServer::~" ) );
       
   344 
       
   345     delete iIMEI; iIMEI = NULL;
       
   346 
       
   347     delete iIMSI; iIMSI = NULL;
       
   348 
       
   349     delete iDb; iDb = NULL;
       
   350 
       
   351     iClock.Close();
       
   352     iCache.Close();
       
   353 
       
   354     iMeteringDb.Close();
       
   355 
       
   356     iFs.Close();
       
   357     iActiveCountConstraints.ResetAndDestroy();
       
   358     iActiveCountConstraints.Close();
       
   359 
       
   360     delete iBackupObserver;
       
   361     delete iBackupHandler;
       
   362     delete iActiveBackupClient;
       
   363     delete iDbWatcher;
       
   364 
       
   365 #if 0
       
   366     // Close and delete the shared data client
       
   367     if( iSharedDataClient )
       
   368         {
       
   369         iSharedDataClient->Close();
       
   370         delete iSharedDataClient;
       
   371         iSharedDataClient = NULL;
       
   372         }
       
   373 #endif
       
   374 
       
   375     if( iNotifier )
       
   376         {
       
   377         delete iNotifier; iNotifier = NULL;
       
   378         }
       
   379 
       
   380     iActiveUrls.ResetAndDestroy();
       
   381 
       
   382     //An empty semaphore
       
   383     RSemaphore semaphore;
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // CDRMRightsServer::GetSecureTime
       
   388 // Fetch the time from (secure) source.
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 TBool CDRMRightsServer::GetSecureTime( TTime& aTime ) const
       
   392     {
       
   393     DRMClock::ESecurityLevel secLevel = DRMClock::KInsecure;
       
   394 
       
   395     TInt timezone( 0 );
       
   396 
       
   397     iClock.GetSecureTime( aTime, timezone, secLevel );
       
   398 
       
   399     if( secLevel == DRMClock::KSecure )
       
   400         {
       
   401         DRMLOG( _L( "CDRMRightsServer::GetSecureTime: Time is secure\r\n" ) );
       
   402         return ETrue;
       
   403         }
       
   404 
       
   405     DRMLOG( _L( "CDRMRightsServer::GetSecureTime: Time is not secure\r\n" ) );
       
   406 
       
   407     return EFalse;
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // CDRMRightsServer::Notifier
       
   412 // Return a handle to DRM Notifier.
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 CDRMNotifier& CDRMRightsServer::Notifier()
       
   416     {
       
   417     return *iNotifier;
       
   418     }
       
   419 
       
   420 // -----------------------------------------------------------------------------
       
   421 // CDRMRightsServer::Notifier
       
   422 // Return a handle to DRM Notifier.
       
   423 // -----------------------------------------------------------------------------
       
   424 //
       
   425 CDRMRightsDB& CDRMRightsServer::Database()
       
   426     {
       
   427     return *iDb;
       
   428     }
       
   429 
       
   430 RFs& CDRMRightsServer::FileServerSession()
       
   431     {
       
   432     return iFs;
       
   433     }
       
   434 
       
   435 
       
   436 RDRMReplayCache& CDRMRightsServer::ReplayCache()
       
   437     {
       
   438     return iCache;
       
   439     }
       
   440 
       
   441 
       
   442 RDrmMeteringDb& CDRMRightsServer::MeteringDatabase()
       
   443     {
       
   444     return iMeteringDb;
       
   445     }
       
   446 
       
   447 // -----------------------------------------------------------------------------
       
   448 // CDRMRightsServer::HandleNotifyL
       
   449 // Forward the event to the database.
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 void CDRMRightsServer::HandleNotifyL(const TUid /*aUid*/,
       
   453                                      const TDesC& /*aKey*/,
       
   454                                      const TDesC& /*aValue*/)
       
   455     {
       
   456     /* XXX Backup via Publish/Subscribe
       
   457     __ASSERT_DEBUG( iDb, User::Invariant() );
       
   458     TInt value = -1;
       
   459     TLex16 parser( aValue );
       
   460 
       
   461     if ( aUid == KSDUidSystem )
       
   462         {
       
   463         // Check if it's a backup / restore status event
       
   464         if( !aKey.Compare( KBackupRestoreStatus ) )
       
   465             {
       
   466             User::LeaveIfError( parser.Val( value ) );
       
   467             if( value == 3 ) // Complete
       
   468                 {
       
   469                 iDb->MergeDBL();
       
   470                 }
       
   471             }
       
   472         // Check if it's a drm backup restore status event
       
   473         else if ( aUid == KSDUidSystem )
       
   474             {
       
   475             if( !aKey.Compare( KDRMBackupRestoreStatus ) )
       
   476                 {
       
   477                 User::LeaveIfError( parser.Val( value ) );
       
   478 
       
   479                 if( value == 1 ) // PrepareForBackup
       
   480                     {
       
   481                     TRAPD( error, iDb->BackupDBL( KNullDesC,
       
   482                                                   KNullDesC8 ) );
       
   483                     // Notify that it's done
       
   484                     User::LeaveIfError( iSharedDataClient->AssignToTemporaryFile(
       
   485                                         KSDUidSystem ) );
       
   486                     User::LeaveIfError( iSharedDataClient->SetInt(
       
   487                                         KDRMBackupRestoreStatus, 0 ) );
       
   488                     iSharedDataClient->Flush();
       
   489                     }
       
   490                 }
       
   491             }
       
   492         }
       
   493     */
       
   494     }
       
   495 
       
   496 // -----------------------------------------------------------------------------
       
   497 // CDRMRightsServer::RunErrorL
       
   498 // From CActive. Complete the request and restart the scheduler.
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 TInt CDRMRightsServer::RunError( TInt aError )
       
   502     {
       
   503     DRMLOG2( _L( "CDRMRightsServer::RunError: %d" ), aError );
       
   504 
       
   505     // Inform the client.
       
   506     if ( !Message().IsNull() )
       
   507         {
       
   508         Message().Complete( aError );
       
   509         }
       
   510 
       
   511     // Restart the scheduler.
       
   512     ReStart();
       
   513 
       
   514     // Error handled.
       
   515     return KErrNone;
       
   516     }
       
   517 
       
   518 // -----------------------------------------------------------------------------
       
   519 // CDRMRightsServer::NewSessionL
       
   520 // Called when a client requires a new instance.
       
   521 // -----------------------------------------------------------------------------
       
   522 CSession2* CDRMRightsServer::NewSessionL( const TVersion& aVersion,
       
   523                                           const RMessage2& /*aMessage*/ ) const
       
   524     {
       
   525     DRMLOG( _L( "CDRMRightsServer::NewSessionL" ) );
       
   526 
       
   527     if ( ! User::QueryVersionSupported( TVersion( DRMEngine::KServerMajorVersion,
       
   528         DRMEngine::KServerMinorVersion,
       
   529         DRMEngine::KServerBuildVersion ),
       
   530         aVersion ) )
       
   531         {
       
   532         // Sorry, no can do.
       
   533         User::Leave( KErrNotSupported );
       
   534         }
       
   535 
       
   536     DRMLOG( _L( "CDRMRightsServer::NewSessionL: Creating a new session" ) );
       
   537 
       
   538     return CDRMDbSession::NewL();
       
   539     }
       
   540 // -----------------------------------------------------------------------------
       
   541 // CDRMRightsServer::CDRMRightsServer
       
   542 // C++ default constructor can NOT contain any code, that
       
   543 // might leave.
       
   544 // -----------------------------------------------------------------------------
       
   545 //
       
   546 CDRMRightsServer::CDRMRightsServer() :
       
   547     CServer2( EPriorityStandard ),
       
   548     iIMEI( NULL ),
       
   549     iArmed( EFalse ),
       
   550     iIMSI( NULL ),
       
   551     iGetImsi( ETrue )
       
   552     {
       
   553     // Nothing
       
   554     }
       
   555 
       
   556 // -----------------------------------------------------------------------------
       
   557 // CDRMRightsServer::ConstructL
       
   558 // Symbian 2nd phase constructor can leave.
       
   559 // -----------------------------------------------------------------------------
       
   560 //
       
   561 void CDRMRightsServer::ConstructL()
       
   562     {
       
   563     DRMLOG( _L( "CDRMRightsServer::ConstructL" ) );
       
   564 
       
   565     TDRMKey key;
       
   566     RSemaphore semaphore;
       
   567     RProcess currentprocess;
       
   568 
       
   569     // Ignore errors
       
   570     User::RenameThread( KRightsServerThread );
       
   571     User::LeaveIfError( iFs.Connect() );
       
   572 
       
   573 #ifndef RD_MULTIPLE_DRIVE
       
   574 
       
   575     // Ignore errors
       
   576     iFs.MkDirAll( KDRMDbTempPath );
       
   577 
       
   578 #else //RD_MULTIPLE_DRIVE
       
   579 
       
   580     TFileName tempPath;
       
   581     TFileName tempPath2;
       
   582     TFileName tempRemovablePath;
       
   583     TInt driveNumber( -1 );
       
   584     TChar driveLetter;
       
   585     TChar driveLetterRemovable;
       
   586     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   587     iFs.DriveToChar( driveNumber, driveLetter );
       
   588 
       
   589     tempPath.Format( KDbTempPath, (TUint)driveLetter );
       
   590 
       
   591     // Ignore errors
       
   592     iFs.MkDirAll( tempPath );
       
   593 
       
   594 #endif
       
   595 
       
   596     DRMLOG( _L( "CDRMRightsServer::ConstructL: SharedDataClient" ) );
       
   597 
       
   598     // Create and instance of the shared data client
       
   599     // iSharedDataClient = new (ELeave) RSharedDataClient(this);
       
   600 
       
   601     // Connecting to the shared data server
       
   602     // User::LeaveIfError(iSharedDataClient->Connect());
       
   603 
       
   604     /* XXX Backup via Publish/Subscribe
       
   605     User::LeaveIfError(iSharedDataClient->NotifyChange(
       
   606         KSDUidSystem, &KBackupRestoreStatus ) );
       
   607     User::LeaveIfError(iSharedDataClient->NotifyChange(
       
   608         KSDUidSystem, &KDRMBackupRestoreStatus) );
       
   609     */
       
   610 
       
   611 
       
   612     GetDbKeyL( key );
       
   613 
       
   614 
       
   615     DRMLOG( _L( "CDRMRightsServer::ConstructL: database" ) );
       
   616 
       
   617     GetIMEIL();
       
   618 
       
   619     // Create the imsi pointer array:
       
   620     iIMSI = CDRMPointerArray<HBufC8>::NewL();
       
   621     iIMSI->SetAutoCleanup(ETrue);
       
   622 
       
   623     GetIMSIL();
       
   624 
       
   625 #ifndef RD_MULTIPLE_DRIVE
       
   626 
       
   627     iDb = CDRMRightsDB::NewL( iFs, KRightsDir, key, *iIMEI, const_cast<CDRMRightsServer*>(this) );
       
   628 
       
   629 #else //RD_MULTIPLE_DRIVE
       
   630 
       
   631     tempPath.Format( KRightsDir, (TUint)driveLetter );
       
   632 
       
   633     iDb = CDRMRightsDB::NewL( iFs, tempPath, key, *iIMEI, const_cast<CDRMRightsServer*>(this) );
       
   634 
       
   635 #endif
       
   636 
       
   637     key.FillZ();
       
   638 
       
   639     DRMLOG( _L( "CDRMRightsServer::ConstructL: DB started." ) );
       
   640 
       
   641     DRMLOG( _L( "CDRMRightsServer::ConstructL: Starting Notifier ." ) );
       
   642 
       
   643     User::LeaveIfError( semaphore.CreateGlobal( KDRMEngCommonSemaphore, 0 ) );
       
   644     CleanupClosePushL( semaphore );
       
   645 
       
   646     StartThreadL( DRMNotifier::KServerName, StartupNotifier, semaphore );
       
   647     DRMLOG( _L( "CDRMRightsServer::ConstructL: Notifier thread created." ) );
       
   648 
       
   649     StartThreadL( Roap::KServerName, StartupRoapStorage, semaphore );
       
   650     DRMLOG( _L( "CDRMRightsServer::ConstructL: ROAP thread created." ) );
       
   651 
       
   652 #ifdef __DRM_CLOCK
       
   653     StartThreadL( DRMClock::KServerName, StartupClock, semaphore );
       
   654     DRMLOG( _L( "CDRMRightsServer::ConstructL: clock thread created." ) );
       
   655 #endif
       
   656 
       
   657     CleanupStack::PopAndDestroy(); // semaphore
       
   658 
       
   659     iNotifier = CDRMNotifier::NewL();
       
   660 
       
   661     iCache.Set( iFs );
       
   662 
       
   663 #ifndef RD_MULTIPLE_DRIVE
       
   664 
       
   665     iCache.InitL( KTimedReplayCacheFile, KPlainReplayCacheFile );
       
   666 
       
   667 #ifdef RD_DRM_METERING
       
   668     iMeteringDb.Set( iFs );
       
   669     iMeteringDb.InitL( KMeteringDataBaseFile );
       
   670 #endif
       
   671 
       
   672 #else //RD_MULTIPLE_DRIVE
       
   673 
       
   674     tempPath.Format( KTimedReplayCacheFile, (TUint)driveLetter );
       
   675     tempPath2.Format( KPlainReplayCacheFile, (TUint)driveLetter );
       
   676 
       
   677     iCache.InitL( tempPath, tempPath2 );
       
   678 
       
   679 #ifdef RD_DRM_METERING
       
   680 
       
   681     tempPath.Format( KMeteringDataBaseFile, (TUint)driveLetter );
       
   682 
       
   683     iMeteringDb.Set( iFs );
       
   684     iMeteringDb.InitL( tempPath );
       
   685 
       
   686 #endif
       
   687 
       
   688 #endif
       
   689 
       
   690     User::LeaveIfError( iClock.Connect() );
       
   691 
       
   692     // xoma header list creation
       
   693     iXOmaHeaders = new (ELeave) RPointerArray< CDRMXOma >();
       
   694 
       
   695     // p/s
       
   696     iBackupObserver = CDRMBackupObserver::NewL( *(const_cast<CDRMRightsServer*>(this)));
       
   697     iBackupObserver->Start();
       
   698 
       
   699 #ifdef USE_RO_IMPORT
       
   700     // Import any OMA DRM 1.0 RO in the import directory, ignore all errors (except
       
   701     // when checking the default removable mass storage)
       
   702     TInt r = KErrNone;
       
   703 
       
   704 #ifndef RD_MULTIPLE_DRIVE
       
   705 
       
   706     TRAP( r, ImportRightsObjectsL( KInternalImportDir ) );
       
   707     TRAP( r, ImportRightsObjectsL( KUserDiskImportDir ) );
       
   708 
       
   709 #else //RD_MULTIPLE_DRIVE
       
   710 
       
   711     tempPath.Format( KInternalImportDir, (TUint)driveLetter );
       
   712 
       
   713     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultMassStorage, driveNumber );
       
   714     iFs.DriveToChar( driveNumber, driveLetter );
       
   715 
       
   716     // Default mass storage is usually eMMC
       
   717     tempPath2.Format( KUserDiskImportDir, (TUint)driveLetter );
       
   718 
       
   719     // Find out if a removable mass storage also exists
       
   720     r = DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRemovableMassStorage, driveNumber );
       
   721     iFs.DriveToChar( driveNumber, driveLetterRemovable );
       
   722 
       
   723     // Import is not needed from the default removable mass storage drive if the drive
       
   724     // letter of the default mass storage and the default removable mass storage are
       
   725     // the same or the removable mass storage is not supported
       
   726     if ( ( driveLetter != driveLetterRemovable ) && ( r == KErrNone ) )
       
   727         {
       
   728         tempRemovablePath.Format( KUserRemovableDiskImportDir, (TUint)driveLetterRemovable );
       
   729         TRAP( r, ImportRightsObjectsL( tempRemovablePath ) );
       
   730         }
       
   731 
       
   732     TRAP( r, ImportRightsObjectsL( tempPath ) );
       
   733     TRAP( r, ImportRightsObjectsL( tempPath2 ) );
       
   734 
       
   735 #endif
       
   736 
       
   737 #endif
       
   738 
       
   739     // Add the server to the scheduler.
       
   740     StartL( DRMEngine::KServerName );
       
   741 
       
   742     // Start watching our RDB
       
   743     iDbWatcher = CDbWatcher::NewL( *this );
       
   744     iDbWatcher->StartWatching();
       
   745 
       
   746     // Start watching the helper server
       
   747     iProcWatcher = CProcWatcher::NewL( *this, _L( "*DcfRepSrv*" ), _L( "DcfRepSrv" ) );
       
   748     iProcWatcher->StartWatching();
       
   749 
       
   750     // Ready to watch
       
   751     iArmed = ETrue;
       
   752 
       
   753     __UHEAP_MARK;
       
   754     TRAP( r, FeatureManager::InitializeLibL() );
       
   755     if( !r && FeatureManager::FeatureSupported( KFeatureIdWindowsMediaDrm ) )
       
   756         {
       
   757         static const TInt KGateOrdinal = 1;
       
   758         RLibrary library;
       
   759         r = library.Load( KWmDrmClientWrapperName );
       
   760         if( !r )
       
   761             {
       
   762             CWmDrmClientWrapper* wrapper = NULL;
       
   763             TLibraryFunction function = library.Lookup( KGateOrdinal );
       
   764             if( function != NULL )
       
   765                 {
       
   766                 __UHEAP_MARK;
       
   767                 TRAP( r, wrapper = reinterpret_cast<CWmDrmClientWrapper*>( function() ) );
       
   768                 if( !r )
       
   769                     {
       
   770                     r = wrapper->Connect();
       
   771                     }
       
   772                 delete wrapper;
       
   773                 __UHEAP_MARKEND;
       
   774                 }
       
   775             }
       
   776         library.Close();
       
   777         }
       
   778     FeatureManager::UnInitializeLib();
       
   779     __UHEAP_MARKEND;
       
   780     }
       
   781 
       
   782 // -----------------------------------------------------------------------------
       
   783 // CDRMRightsServer::StartThreadL
       
   784 // Start a new thread.
       
   785 // -----------------------------------------------------------------------------
       
   786 void CDRMRightsServer::StartThreadL( const TDesC& aThreadName,
       
   787                                      TThreadFunction aFunc,
       
   788                                      RSemaphore& aSemaphore )
       
   789     {
       
   790     RThread thread;
       
   791 
       
   792     User::LeaveIfError(
       
   793           thread.Create( aThreadName,
       
   794                          aFunc,
       
   795                          KDefaultStackSize,
       
   796                          KMinHeapSize,
       
   797                          KMaxHeapsize,
       
   798                          NULL ) );
       
   799 
       
   800     thread.Resume();
       
   801 
       
   802     aSemaphore.Wait();
       
   803 
       
   804     thread.Close();
       
   805     }
       
   806 
       
   807 // -----------------------------------------------------------------------------
       
   808 // CDRMRightsServer::GetDbKeyL
       
   809 // Fetches the rights database key from Wallet or uses a constant
       
   810 // key if Wallet is not supported.
       
   811 // -----------------------------------------------------------------------------
       
   812 //
       
   813 void CDRMRightsServer::GetDbKeyL( TDRMKey& aKey  )
       
   814     {
       
   815     TInt r = KErrNone;
       
   816 
       
   817     DRMLOG( _L( "CDRMRightsServer::GetDbKey" ) );
       
   818     MDrmKeyStorage* storage = DrmKeyStorageNewL();
       
   819     TRAP( r, storage->GetDeviceSpecificKeyL( aKey ) );
       
   820     delete storage;
       
   821     User::LeaveIfError( r );
       
   822     }
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // CDRMRightsServer::GenerateKeyL
       
   826 // Generates the actual key based on the given key seed.
       
   827 // -----------------------------------------------------------------------------
       
   828 //
       
   829 void CDRMRightsServer::GenerateKeyL( HBufC*& aKeySeed,
       
   830                                     TDRMKey& aKey ) const
       
   831     {
       
   832     __ASSERT_ALWAYS( aKeySeed->Size() >= KDRMKeyLength,
       
   833         User::Leave( KErrUnderflow ) );
       
   834 
       
   835     TPtrC8 key( reinterpret_cast< TUint8* >( const_cast< TUint16* >( aKeySeed->Ptr() ) ),
       
   836                 KDRMKeyLength );
       
   837 
       
   838     aKey = key;
       
   839     }
       
   840 
       
   841 
       
   842 // -----------------------------------------------------------------------------
       
   843 // CDRMRightsServer::XOmaHeaders()
       
   844 // return the pointer of the X-Oma headers list
       
   845 // -----------------------------------------------------------------------------
       
   846 //
       
   847 RPointerArray< CDRMXOma >& CDRMRightsServer::XOmaHeaders( void )
       
   848     {
       
   849     return *iXOmaHeaders;
       
   850     }
       
   851 
       
   852 
       
   853 
       
   854 // -----------------------------------------------------------------------------
       
   855 // CDRMRightsServer::GetIMEIL
       
   856 // -----------------------------------------------------------------------------
       
   857 //
       
   858 const TDesC& CDRMRightsServer::GetIMEIL()
       
   859     {
       
   860     if ( iIMEI )
       
   861         {
       
   862         return *iIMEI;
       
   863         }
       
   864 
       
   865 #ifdef DRM_USE_SERIALNUMBER_URI
       
   866     TInt error( KErrNone );
       
   867     TInt count( 0 );
       
   868     TInt count2( 0 );
       
   869     TUint32 caps( 0 );
       
   870     TBool found (EFalse);
       
   871 
       
   872     RTelServer etelServer;
       
   873     RMobilePhone phone;
       
   874 
       
   875     TUint KMaxImeiTries = 5;
       
   876 
       
   877     for ( TUint8 i = 0; i < KMaxImeiTries; ++i )
       
   878         {
       
   879         error = etelServer.Connect();
       
   880         if ( error )
       
   881             {
       
   882             User::After( TTimeIntervalMicroSeconds32( KWaitingTime ) );
       
   883             }
       
   884         else
       
   885             {
       
   886             break;
       
   887             }
       
   888         }
       
   889 
       
   890     User::LeaveIfError( error );
       
   891     CleanupClosePushL( etelServer );
       
   892 
       
   893     User::LeaveIfError( etelServer.LoadPhoneModule( KMmTsyModuleName ) );
       
   894 
       
   895     TUnloadModule unload;
       
   896     unload.iServer = &etelServer;
       
   897     unload.iName = &KMmTsyModuleName;
       
   898 
       
   899     TCleanupItem item( DoUnloadPhoneModule, &unload );
       
   900     CleanupStack::PushL( item );
       
   901     User::LeaveIfError( etelServer.EnumeratePhones( count ) );
       
   902 
       
   903     for ( count2 = 0; count2 < count && !found; ++count2 )
       
   904         {
       
   905         RTelServer::TPhoneInfo phoneInfo;
       
   906         User::LeaveIfError( etelServer.GetTsyName( count2, phoneInfo.iName ) );
       
   907 
       
   908         if ( phoneInfo.iName.CompareF(KMmTsyModuleName()) == 0 )
       
   909            {
       
   910             User::LeaveIfError( etelServer.GetPhoneInfo( count2, phoneInfo ) );
       
   911             User::LeaveIfError( phone.Open( etelServer, phoneInfo.iName ) );
       
   912             CleanupClosePushL( phone );
       
   913             found = ETrue;
       
   914             }
       
   915         }
       
   916 
       
   917     if ( !found )
       
   918         {
       
   919         // Not found.
       
   920         User::Leave( KErrNotFound );
       
   921         }
       
   922 
       
   923     User::LeaveIfError( phone.GetIdentityCaps( caps ) );
       
   924     if ( caps & RMobilePhone::KCapsGetSerialNumber )
       
   925         {
       
   926         RMobilePhone::TMobilePhoneIdentityV1 id;
       
   927         TRequestStatus status;
       
   928 
       
   929         phone.GetPhoneId( status, id );
       
   930 
       
   931         User::WaitForRequest( status );
       
   932 
       
   933         User::LeaveIfError( status.Int() );
       
   934 
       
   935         iIMEI = id.iSerialNumber.AllocL();
       
   936 
       
   937         CleanupStack::PopAndDestroy( 3 ); // phone, item, etelServer
       
   938 
       
   939         HBufC8* buf = HBufC8::NewL( iIMEI->Size() );
       
   940         TPtr8 ptr( buf->Des() );
       
   941         ptr.Copy( *iIMEI );
       
   942 
       
   943         DRMLOG(_L("IMEI:"));
       
   944         DRMLOGHEX(ptr);
       
   945         delete buf;
       
   946 
       
   947         return *iIMEI;
       
   948         }
       
   949 
       
   950     User::Leave( KErrNotFound );
       
   951 
       
   952     // Never happens...
       
   953     return *iIMEI;
       
   954 
       
   955 #else
       
   956     _LIT( KDefaultSerialNumber, "123456789123456789" );
       
   957     iIMEI = KDefaultSerialNumber().AllocL();
       
   958 
       
   959     return *iIMEI;
       
   960 #endif
       
   961     }
       
   962 
       
   963 // -----------------------------------------------------------------------------
       
   964 // CDRMRightsServer::GetIMSIL
       
   965 // -----------------------------------------------------------------------------
       
   966 //
       
   967 const CDRMPointerArray<HBufC8>& CDRMRightsServer::GetIMSIL()
       
   968     {
       
   969 
       
   970     if ( !iGetImsi )
       
   971         {
       
   972         return *iIMSI;
       
   973         }
       
   974 
       
   975 #ifndef __WINS__
       
   976     TInt error( KErrNone );
       
   977     TInt count( 0 );
       
   978     TInt count2( 0 );
       
   979     TUint32 caps( 0 );
       
   980     TBool found (EFalse);
       
   981     HBufC8* imsi = NULL;
       
   982     HBufC8* imsiNumber = NULL;
       
   983 
       
   984     RTelServer etelServer;
       
   985     RMobilePhone phone;
       
   986 
       
   987     TUint KMaxImeiTries = 5;
       
   988     for ( TUint8 i = 0; i < KMaxImeiTries; ++i )
       
   989         {
       
   990         error = etelServer.Connect();
       
   991         if ( error )
       
   992             {
       
   993             User::After( TTimeIntervalMicroSeconds32( KWaitingTime ) );
       
   994             }
       
   995         else
       
   996             {
       
   997             break;
       
   998             }
       
   999         }
       
  1000 
       
  1001     User::LeaveIfError( error );
       
  1002     CleanupClosePushL( etelServer );
       
  1003     User::LeaveIfError( etelServer.LoadPhoneModule( KMmTsyModuleName ) );
       
  1004 
       
  1005     TUnloadModule unload;
       
  1006     unload.iServer = &etelServer;
       
  1007     unload.iName = &KMmTsyModuleName;
       
  1008 
       
  1009     TCleanupItem item( DoUnloadPhoneModule, &unload );
       
  1010     CleanupStack::PushL( item );
       
  1011 
       
  1012     User::LeaveIfError( etelServer.EnumeratePhones( count ) );
       
  1013 
       
  1014     for ( count2 = 0; count2 < count && !found; ++count2 )
       
  1015         {
       
  1016         RTelServer::TPhoneInfo phoneInfo;
       
  1017         User::LeaveIfError( etelServer.GetTsyName( count2, phoneInfo.iName ) );
       
  1018 
       
  1019         if ( phoneInfo.iName.CompareF(KMmTsyModuleName()) == 0 )
       
  1020            {
       
  1021             User::LeaveIfError( etelServer.GetPhoneInfo( count2, phoneInfo ) );
       
  1022             User::LeaveIfError( phone.Open( etelServer, phoneInfo.iName ) );
       
  1023             CleanupClosePushL( phone );
       
  1024             found = ETrue;
       
  1025             }
       
  1026         }
       
  1027 
       
  1028     if ( !found )
       
  1029         {
       
  1030         // Not found.
       
  1031         User::Leave( KErrNotFound );
       
  1032         }
       
  1033 
       
  1034 
       
  1035     User::LeaveIfError( phone.GetIdentityCaps( caps ) );
       
  1036 
       
  1037     if( caps & RMobilePhone::KCapsGetSubscriberId )
       
  1038         {
       
  1039         RMobilePhone::TMobilePhoneSubscriberId imsiId;
       
  1040         TRequestStatus status;
       
  1041 
       
  1042         phone.GetSubscriberId( status, imsiId );
       
  1043 
       
  1044         User::WaitForRequest( status );
       
  1045 
       
  1046         if( ! status.Int() )
       
  1047             {
       
  1048             imsi = HBufC8::NewMaxLC( imsiId.Length() + KImsiId().Size() );
       
  1049             TPtr8 imsiPtr(const_cast<TUint8*>(imsi->Ptr()), 0, imsi->Size());
       
  1050 
       
  1051             imsiNumber = CnvUtfConverter::ConvertFromUnicodeToUtf8L( imsiId );
       
  1052             CleanupStack::PushL( imsiNumber );
       
  1053 
       
  1054             imsiPtr.Copy( KImsiId() );
       
  1055             imsiPtr.Append( *imsiNumber );
       
  1056             CleanupStack::PopAndDestroy(); // imsiNumber
       
  1057             }
       
  1058         else
       
  1059             {
       
  1060             imsi = NULL;
       
  1061             }
       
  1062         }
       
  1063     else
       
  1064         {
       
  1065         imsi = NULL;
       
  1066         }
       
  1067 
       
  1068 
       
  1069     // Clean up whatever is in there
       
  1070     iIMSI->ResetAndDestroy();
       
  1071 
       
  1072     if( imsi )
       
  1073         {
       
  1074         // if we got it we wont try again
       
  1075         iIMSI->AppendL( imsi );
       
  1076         CleanupStack::Pop(); // imsi
       
  1077         iGetImsi = EFalse;
       
  1078         }
       
  1079 
       
  1080     // Check for possible extra IMSI individual constraints
       
  1081     AppendExtendedIndividualConstraintsL(&phone);
       
  1082 
       
  1083     CleanupStack::PopAndDestroy(); // phone
       
  1084     CleanupStack::PopAndDestroy(); // cleanup item
       
  1085     CleanupStack::PopAndDestroy(); // etel server
       
  1086 
       
  1087     return *iIMSI;
       
  1088 
       
  1089 #else
       
  1090     HBufC8* imsi = NULL;
       
  1091 
       
  1092     if( iGetImsi )
       
  1093         {
       
  1094         iGetImsi = EFalse;
       
  1095         _LIT8( KDefaultSerialNumber, "IMSI:123456789123456789" );
       
  1096         imsi = KDefaultSerialNumber().AllocLC();
       
  1097         iIMSI->AppendL( imsi );
       
  1098         CleanupStack::Pop();
       
  1099         AppendExtendedIndividualConstraintsL();
       
  1100         }
       
  1101 
       
  1102 
       
  1103     return *iIMSI;
       
  1104 #endif // __WINS__
       
  1105     }
       
  1106 
       
  1107 // -----------------------------------------------------------------------------
       
  1108 // CDRMRightsServer::AppendExtendedIndividualConstraintsL
       
  1109 // If the extension DLL exists it is loaded and used to obtain additional
       
  1110 // valid individual constraints
       
  1111 // -----------------------------------------------------------------------------
       
  1112 void CDRMRightsServer::AppendExtendedIndividualConstraintsL(RMobilePhone* aMobilePhone)
       
  1113     {
       
  1114     // Load the externsion DLL
       
  1115     RLibrary lib;
       
  1116 
       
  1117 #ifndef RD_MULTIPLE_DRIVE
       
  1118 
       
  1119     if (lib.LoadRomLibrary(KDRMIndividualConstraintExtensionDll,KNullDesC)==KErrNone)
       
  1120 
       
  1121 #else //RD_MULTIPLE_DRIVE
       
  1122 
       
  1123     TInt driveNumber( -1 );
       
  1124     TChar driveLetter;
       
  1125     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
  1126     iFs.DriveToChar( driveNumber, driveLetter );
       
  1127 
       
  1128     TFileName individualConstraindExtensionDll;
       
  1129     individualConstraindExtensionDll.Format(
       
  1130                         KIndividualConstraintExtensionDll, (TUint)driveLetter );
       
  1131 
       
  1132     if ( lib.LoadRomLibrary( individualConstraindExtensionDll, KNullDesC ) == KErrNone )
       
  1133 
       
  1134 #endif
       
  1135 
       
  1136         {
       
  1137         CleanupClosePushL(lib);
       
  1138 
       
  1139         // Get first exported ordinal - factory method returning
       
  1140         // MDRMIndividualConstraintExtension*
       
  1141         TLibraryFunction factory = lib.Lookup(1);
       
  1142 
       
  1143         if (factory)
       
  1144             {
       
  1145             // Instantiate object
       
  1146             MDRMIndividualConstraintExtension* extendedConstraints =
       
  1147                 reinterpret_cast<MDRMIndividualConstraintExtension*>(factory());
       
  1148 
       
  1149             if (extendedConstraints)
       
  1150                 {
       
  1151                 CleanupStack::PushL(TCleanupItem(Release,extendedConstraints));
       
  1152                 extendedConstraints->AppendConstraintsL(*iIMSI,aMobilePhone);
       
  1153                 CleanupStack::PopAndDestroy(extendedConstraints); //calls Release
       
  1154                 }
       
  1155             }
       
  1156 
       
  1157         // unload library
       
  1158         CleanupStack::PopAndDestroy(&lib); //close
       
  1159         }
       
  1160     }
       
  1161 
       
  1162 // -----------------------------------------------------------------------------
       
  1163 // CDRMRightsServer::Release
       
  1164 // -----------------------------------------------------------------------------
       
  1165 void CDRMRightsServer::Release(TAny* aIndividualConstraintExtension)
       
  1166     {
       
  1167     MDRMIndividualConstraintExtension* extendedConstraints =
       
  1168         reinterpret_cast<MDRMIndividualConstraintExtension*>(aIndividualConstraintExtension);
       
  1169     extendedConstraints->Release(); //free resources
       
  1170     }
       
  1171 
       
  1172 // -----------------------------------------------------------------------------
       
  1173 // CDRMRightsServer::HandleBackupEventL
       
  1174 // Handle Backup Events
       
  1175 // -----------------------------------------------------------------------------
       
  1176 //
       
  1177 
       
  1178 void CDRMRightsServer::HandleBackupEventL( TInt aBackupEvent )
       
  1179     {
       
  1180     //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::BackupCalled\n\r"));
       
  1181 
       
  1182     //conn::TBURPartType eventType;
       
  1183     //conn::TBackupIncType incType;
       
  1184     TDriveList aDriveList;
       
  1185 
       
  1186     //RFileLogger::WriteFormat(KLogDir, KLogName, EFileLoggingModeAppend, _L8("backupevent: %d"), aBackupEvent);
       
  1187 
       
  1188     // If there is no operation going or state is normal
       
  1189     // Delete the client and handler
       
  1190 
       
  1191     if( aBackupEvent == conn::EBURUnset ||
       
  1192         aBackupEvent & conn::EBURNormal )
       
  1193         {
       
  1194         /*
       
  1195         if( aBackupEvent == conn::EBURUnset )
       
  1196             {
       
  1197                         RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Unset\n\r"));
       
  1198             }
       
  1199         else
       
  1200             {
       
  1201             RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Normal\n\r"));
       
  1202             }
       
  1203         */
       
  1204         if( iActiveBackupClient )
       
  1205             {
       
  1206             delete iActiveBackupClient;
       
  1207             iActiveBackupClient = NULL;
       
  1208             }
       
  1209 
       
  1210         if( iBackupHandler )
       
  1211             {
       
  1212             delete iBackupHandler;
       
  1213             iBackupHandler = NULL;
       
  1214             }
       
  1215         }
       
  1216     else if( aBackupEvent & conn::EBURBackupFull ||
       
  1217              aBackupEvent & conn::EBURRestoreFull )
       
  1218         {
       
  1219         //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Full\n\r"));
       
  1220         // ab handler
       
  1221         iBackupHandler = CDRMBackup::NewL( iDb, iFs );
       
  1222 
       
  1223         // ab client
       
  1224         iActiveBackupClient = conn::CActiveBackupClient::NewL( iBackupHandler );
       
  1225 
       
  1226         // Confirm that we have done everything if there even was anything to do
       
  1227         //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm F \n\r"));
       
  1228         iActiveBackupClient->ConfirmReadyForBURL( KErrNone );
       
  1229         }
       
  1230     else if( aBackupEvent & conn::EBURBackupPartial ||
       
  1231              aBackupEvent & conn::EBURRestorePartial )
       
  1232         {
       
  1233         //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Partial\n\r"));
       
  1234         // ab handler
       
  1235         iBackupHandler = CDRMBackup::NewL( iDb, iFs );
       
  1236 
       
  1237         // ab client
       
  1238         iActiveBackupClient = conn::CActiveBackupClient::NewL( iBackupHandler );
       
  1239 
       
  1240         if( !iActiveBackupClient->DoesPartialBURAffectMeL() )
       
  1241             {
       
  1242             //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::NotMe\n\r"));
       
  1243             delete iActiveBackupClient;
       
  1244             iActiveBackupClient = NULL;
       
  1245 
       
  1246             delete iBackupHandler;
       
  1247             iBackupHandler = NULL;
       
  1248             }
       
  1249         else
       
  1250             {
       
  1251             //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm P \n\r"));
       
  1252             // Confirm that we have done everything if there even was anything to do
       
  1253             iActiveBackupClient->ConfirmReadyForBURL( KErrNone );
       
  1254             //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Confirm P Done \n\r"));
       
  1255             }
       
  1256         }
       
  1257     else
       
  1258         {
       
  1259         //RFileLogger::Write(KLogDir, KLogName, EFileLoggingModeAppend, _L8("Handle::Argument\n\r"));
       
  1260         // Unknown operation
       
  1261         User::Leave(KErrArgument);
       
  1262         }
       
  1263     };
       
  1264 
       
  1265 // -----------------------------------------------------------------------------
       
  1266 // CDRMRightsServer::WatchedObjectChangedL
       
  1267 // Handle Backup Events
       
  1268 // -----------------------------------------------------------------------------
       
  1269 //
       
  1270 void CDRMRightsServer::WatchedObjectChangedL( const TDesC& aObject )
       
  1271     {
       
  1272     DRMLOG( _L( "CDRMRightsServer::WatchedObjectChangedL ->" ) );
       
  1273     DRMLOG( aObject );
       
  1274 
       
  1275     if ( aObject.Left( KDirIdentifier().Length() ) == KDirIdentifier &&
       
  1276          !iDb->Updating() && iArmed )
       
  1277         {
       
  1278 #ifdef _DEBUG
       
  1279         DRMLOG( _L( "RDB modified by outside party (DEBUG mode, not deleting the DB)" ) );
       
  1280 #else
       
  1281         DRMLOG( _L( "RDB modified by outside party, deleting the DB" ) );
       
  1282         iDb->MarkAsCorrupted();
       
  1283         RStarterSession starter;
       
  1284         User::LeaveIfError( starter.Connect() );
       
  1285         starter.Reset( RStarterSession::EDRMReset );
       
  1286         starter.Close();
       
  1287 #endif
       
  1288         }
       
  1289     else if ( aObject.Left( KProcIdentifier().Length() ) == KProcIdentifier && iArmed )
       
  1290         {
       
  1291 #ifdef _DEBUG
       
  1292         DRMLOG( _L( "Peer process killed (DEBUG mode, not rebooting)" ) );
       
  1293 #else
       
  1294         DRMLOG( _L( "Peer process killed, rebooting" ) );
       
  1295         RStarterSession starter;
       
  1296         User::LeaveIfError( starter.Connect() );
       
  1297         starter.Reset( RStarterSession::EDRMReset );
       
  1298         starter.Close();
       
  1299 #endif
       
  1300         }
       
  1301 
       
  1302     DRMLOG( _L( "CDRMRightsServer::WatchedObjectChangedL <-" ) );
       
  1303     }
       
  1304 
       
  1305 // -----------------------------------------------------------------------------
       
  1306 // CDRMRightsServer::HasActiveCountConstraint
       
  1307 // Check ID for active count constraint
       
  1308 // -----------------------------------------------------------------------------
       
  1309 //
       
  1310 TBool CDRMRightsServer::HasActiveCountConstraint( const TDesC8& aContentId )
       
  1311     {
       
  1312     TInt i;
       
  1313     TBool r = EFalse;
       
  1314 
       
  1315     for ( i = 0; r == EFalse && i < iActiveCountConstraints.Count(); i++ )
       
  1316         {
       
  1317         if ( iActiveCountConstraints[i]->CompareF( aContentId ) == 0 )
       
  1318             {
       
  1319             r = ETrue;
       
  1320             }
       
  1321         }
       
  1322     return r;
       
  1323     }
       
  1324 
       
  1325 // -----------------------------------------------------------------------------
       
  1326 // CDRMRightsServer::RemoveActiveCountConstraint
       
  1327 // Remove ID from count constraint list
       
  1328 // -----------------------------------------------------------------------------
       
  1329 //
       
  1330 void CDRMRightsServer::RemoveActiveCountConstraint( const TDesC8& aContentId )
       
  1331     {
       
  1332     TInt i;
       
  1333     TInt r = KErrNotFound;
       
  1334     HBufC8* id = NULL;
       
  1335 
       
  1336     for ( i = 0; r == KErrNotFound && i < iActiveCountConstraints.Count(); i++ )
       
  1337         {
       
  1338         if ( iActiveCountConstraints[i]->CompareF( aContentId ) == 0 )
       
  1339             {
       
  1340             r = i;
       
  1341             }
       
  1342         }
       
  1343     if ( r != KErrNotFound )
       
  1344         {
       
  1345         id = iActiveCountConstraints[r];
       
  1346         iActiveCountConstraints.Remove( r );
       
  1347         delete id;
       
  1348         id = NULL;
       
  1349         }
       
  1350     }
       
  1351 
       
  1352 // -----------------------------------------------------------------------------
       
  1353 // CDRMRightsServer::AddActiveCountConstraint
       
  1354 // Add ID to count constraint list
       
  1355 // -----------------------------------------------------------------------------
       
  1356 //
       
  1357 void CDRMRightsServer::AddActiveCountConstraintL( const TDesC8& aContentId )
       
  1358     {
       
  1359     if ( !HasActiveCountConstraint( aContentId ) )
       
  1360         {
       
  1361         iActiveCountConstraints.AppendL( aContentId.AllocL() );
       
  1362         }
       
  1363     }
       
  1364 
       
  1365 
       
  1366 
       
  1367 // -----------------------------------------------------------------------------
       
  1368 // CDRMRightsServer::IsAccessingUrl
       
  1369 // Add ID to count constraint list
       
  1370 // -----------------------------------------------------------------------------
       
  1371 //
       
  1372 TInt CDRMRightsServer::IsAccessingUrl( const TDesC8& aContentId )
       
  1373     {
       
  1374     for( TInt i = 0; i < iActiveUrls.Count(); i++ )
       
  1375         {
       
  1376         if( !iActiveUrls[i]->iUrl->Compare( aContentId ) ) 
       
  1377             {
       
  1378             return i;
       
  1379             }    
       
  1380         }
       
  1381     return KErrNotFound;        
       
  1382     }
       
  1383 
       
  1384 // -----------------------------------------------------------------------------
       
  1385 // CDRMRightsServer::RemoveAccessingUrl
       
  1386 // Add ID to count constraint list
       
  1387 // -----------------------------------------------------------------------------
       
  1388 //
       
  1389 void CDRMRightsServer::RemoveAccessingUrl( const TDesC8& aContentId )
       
  1390     {
       
  1391     CUsageUrl* usage = NULL;    
       
  1392     TInt index = KErrNotFound;
       
  1393     
       
  1394     index = IsAccessingUrl( aContentId );
       
  1395     
       
  1396     if( index != KErrNotFound )
       
  1397         {
       
  1398         // If there are negative or 0 values in the list for some reason
       
  1399         // remove them    
       
  1400         if( iActiveUrls[index]->iRefCounter <= 1 )
       
  1401             {
       
  1402             usage = iActiveUrls[index];    
       
  1403             iActiveUrls.Remove( index );    
       
  1404             delete usage;
       
  1405             }
       
  1406         else
       
  1407             {
       
  1408             iActiveUrls[index]->iRefCounter--;
       
  1409             }           
       
  1410         }
       
  1411     }
       
  1412 
       
  1413 // -----------------------------------------------------------------------------
       
  1414 // CDRMRightsServer::AddAccessingUrlL
       
  1415 // Add ID to count constraint list
       
  1416 // -----------------------------------------------------------------------------
       
  1417 //
       
  1418 void CDRMRightsServer::AddAccessingUrlL( const TDesC8& aContentId )
       
  1419     {
       
  1420     CUsageUrl* usage = NULL;
       
  1421     TInt index = KErrNotFound;
       
  1422     
       
  1423     index = IsAccessingUrl( aContentId );
       
  1424     
       
  1425     if( index == KErrNotFound )
       
  1426         {
       
  1427         usage = new ( ELeave ) CUsageUrl();
       
  1428         CleanupStack::PushL( usage );
       
  1429         usage->iUrl = aContentId.AllocL();
       
  1430         usage->iRefCounter = 1;
       
  1431         iActiveUrls.AppendL( usage );
       
  1432         CleanupStack::Pop( usage );
       
  1433         }
       
  1434     else
       
  1435         {
       
  1436         usage = iActiveUrls[index];
       
  1437         usage->iRefCounter++;    
       
  1438         }           
       
  1439     }
       
  1440 
       
  1441 
       
  1442 
       
  1443 
       
  1444 // -----------------------------------------------------------------------------
       
  1445 // CDRMRightsServer::StopWatchingL
       
  1446 // Delete the watchers
       
  1447 // -----------------------------------------------------------------------------
       
  1448 //
       
  1449 void CDRMRightsServer::StopWatchingL()
       
  1450     {
       
  1451     iArmed = EFalse;
       
  1452     }
       
  1453 
       
  1454 #ifdef USE_RO_IMPORT
       
  1455 // -----------------------------------------------------------------------------
       
  1456 // CDRMRightsServer::ImportRightsObjectsL
       
  1457 // Open the import directory and add all ROs that can be found there. ROs file
       
  1458 // names must end with .dr. Only OMA DRM 1.0 ROs in XML format are supported for
       
  1459 // security reasons
       
  1460 // -----------------------------------------------------------------------------
       
  1461 //
       
  1462 void CDRMRightsServer::ImportRightsObjectsL( const TDesC& aImportDir )
       
  1463     {
       
  1464     CDrmRightsParser* p;
       
  1465     HBufC8* d = NULL;
       
  1466     HBufC8* k = NULL;
       
  1467     RFs fs;
       
  1468     RFile file;
       
  1469     TInt size;
       
  1470     RPointerArray<CDRMRights> rights;
       
  1471     CDir* dir;
       
  1472     TFileName name;
       
  1473     TPtr8 ptr( NULL, 0 );
       
  1474     TInt i;
       
  1475     TInt r = KErrNone;
       
  1476     TCleanupItem listCleanup(PointerArrayResetDestroyAndClose<CDRMRights>,
       
  1477         &rights);
       
  1478     TDRMUniqueID id;
       
  1479     TTime time;
       
  1480 
       
  1481     DRMLOG( _L( "CDRMRightsServer::ImportRightsObjectsL" ) );
       
  1482     DRMLOG( aImportDir );
       
  1483     __UHEAP_MARK;
       
  1484     GetSecureTime( time );
       
  1485     p = CDrmRightsParser::NewL();
       
  1486     CleanupStack::PushL( p );
       
  1487     User::LeaveIfError( iFs.GetDir( aImportDir, KEntryAttNormal,
       
  1488         ESortNone, dir ) );
       
  1489     CleanupStack::PushL( dir );
       
  1490     for (i = 0; i < dir->Count(); i++)
       
  1491         {
       
  1492         name.Copy( aImportDir );
       
  1493         name.Append( (*dir)[i].iName );
       
  1494         if ( ( name.Length() > 3 && name.Right(3).CompareF( KDrSuffix ) == 0 ) )
       
  1495             {
       
  1496             User::LeaveIfError( file.Open( iFs, name, EFileRead ) );
       
  1497             CleanupClosePushL( file );
       
  1498             User::LeaveIfError( file.Size( size ) );
       
  1499             d = HBufC8::NewLC( size );
       
  1500             ptr.Set( d->Des() );
       
  1501             User::LeaveIfError( file.Read( ptr ) );
       
  1502             p->ParseL( ptr, rights );
       
  1503             if ( rights.Count() > 0 )
       
  1504                 {
       
  1505                 k = NULL;
       
  1506                 CleanupStack::PushL( listCleanup );
       
  1507                 CDRMPermission& permission = rights[0]->GetPermission();
       
  1508                 CDRMAsset& asset = rights[0]->GetAsset();
       
  1509 
       
  1510                 // Add RO only if no rights are available at all for this content
       
  1511                 TRAP( r, k = iDb->GetDecryptionKeyL( *asset.iUid ) );
       
  1512                 if (k == NULL )
       
  1513                     {
       
  1514                     iDb->AddDBEntryL( *asset.iUid, permission, asset.iKey, id );
       
  1515                     }
       
  1516                 else
       
  1517                     {
       
  1518                     delete k;
       
  1519                     }
       
  1520                 CleanupStack::PopAndDestroy(); // listCleanup
       
  1521                 }
       
  1522             CleanupStack::PopAndDestroy( 2 ); // d, file
       
  1523             iFs.Delete( name );
       
  1524             }
       
  1525         }
       
  1526     CleanupStack::PopAndDestroy( 2 ); // dir, p
       
  1527     __UHEAP_MARKEND;
       
  1528     DRMLOG( _L( "CDRMRightsServer::ImportRightsObjectsL done" ) );
       
  1529     }
       
  1530 #endif
       
  1531 
       
  1532 
       
  1533 
       
  1534 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1535 
       
  1536 
       
  1537 TInt E32Main()
       
  1538     {
       
  1539     return Startup();
       
  1540     }
       
  1541 
       
  1542 
       
  1543 //  End of File