omadrm/drmengine/roapstorage/src/DRMContextDB.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Datatype for Rights Constraint
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32file.h>
       
    22 #include <s32std.h>
       
    23 #include <caf/caf.h>
       
    24 
       
    25 #include "DRMContextDB.h"
       
    26 #include "DRMRIContext.h"
       
    27 #include "DRMDomainContext.h"
       
    28 
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 
       
    34 // CONSTANTS
       
    35 
       
    36 // MACROS
       
    37 
       
    38 // LOCAL CONSTANTS AND MACROS
       
    39 
       
    40 // MODULE DATA STRUCTURES
       
    41 
       
    42 // LOCAL FUNCTION PROTOTYPES
       
    43 
       
    44 // FORWARD DECLARATIONS
       
    45 
       
    46 // ============================= LOCAL FUNCTIONS ===============================
       
    47 // ============================ MEMBER FUNCTIONS ===============================
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CDRMContextDB::NewLC
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CDRMContextDB* CDRMContextDB::NewLC( const TFileName& aRightsIssuerContextFile, 
       
    54                                      const TFileName& aDomainContextFile,
       
    55                                      RFs& aFileServer )
       
    56     {
       
    57     CDRMContextDB* self = new( ELeave ) CDRMContextDB( aFileServer );
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL( aRightsIssuerContextFile, 
       
    60                       aDomainContextFile );
       
    61 
       
    62     return self;    
       
    63     };
       
    64     
       
    65 // -----------------------------------------------------------------------------
       
    66 // CDRMContextDB::NewL
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CDRMContextDB* CDRMContextDB::NewL( const TFileName& aRightsIssuerContextFile, 
       
    70                                     const TFileName& aDomainContextFile,
       
    71                                     RFs& aFileServer )
       
    72     {
       
    73     CDRMContextDB* self = NewLC( aRightsIssuerContextFile, 
       
    74                                  aDomainContextFile,  
       
    75                                  aFileServer );
       
    76     CleanupStack::Pop();	
       
    77     
       
    78     return self;    
       
    79     };
       
    80                                  
       
    81 // -----------------------------------------------------------------------------
       
    82 // CDRMContextDB::OpenLC
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CDRMContextDB* CDRMContextDB::OpenLC( const TFileName& aRightsIssuerContextFile, 
       
    86                                       const TFileName& aDomainContextFile,
       
    87                                       RFs& aFileServer )
       
    88     {
       
    89     CDRMContextDB* self = new( ELeave ) CDRMContextDB( aFileServer );
       
    90     CleanupStack::PushL( self );    
       
    91     self->ConstructL( aRightsIssuerContextFile, 
       
    92                       aDomainContextFile );
       
    93     
       
    94     return self;
       
    95     };    
       
    96     
       
    97 // -----------------------------------------------------------------------------
       
    98 // CDRMContextDB::OpenL
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CDRMContextDB* CDRMContextDB::OpenL( const TFileName& aRightsIssuerContextFile, 
       
   102                                      const TFileName& aDomainContextFile,
       
   103                                      RFs& aFileServer )
       
   104     {
       
   105     CDRMContextDB* self = OpenLC( aRightsIssuerContextFile, 
       
   106                                   aDomainContextFile,  
       
   107                                   aFileServer );
       
   108     CleanupStack::Pop();
       
   109     
       
   110     return self;    
       
   111     };    
       
   112     
       
   113 // -----------------------------------------------------------------------------
       
   114 // Destructor
       
   115 // -----------------------------------------------------------------------------
       
   116 // 
       
   117 CDRMContextDB::~CDRMContextDB()
       
   118     {
       
   119     // Permanent File Store
       
   120     if( iDomainStore )
       
   121         {
       
   122         delete iDomainStore;
       
   123         iDomainStore = NULL;
       
   124         }
       
   125         
       
   126     // Permanent File Store
       
   127     if( iRightsIssuerStore )
       
   128         {
       
   129         delete iRightsIssuerStore;
       
   130         iRightsIssuerStore = NULL;
       
   131         }
       
   132         
       
   133     // index array    
       
   134     iDomainArray.Close();
       
   135     
       
   136     // index array
       
   137     iRightsIssuerArray.Close();
       
   138     }; 
       
   139       
       
   140     
       
   141 // -----------------------------------------------------------------------------
       
   142 // CDRMContextDB::StoreDomainContextL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CDRMContextDB::StoreDomainContextL( const CDRMDomainContext& aContext )
       
   146     {
       
   147     TStreamId rootId;
       
   148     RStoreWriteStream stream;
       
   149     RStoreWriteStream rootStream;
       
   150     CDRMContextDB::TContextIndex searchData;
       
   151     TInt index;
       
   152     
       
   153     // Copy the domain id to the 
       
   154     searchData.iContextID.Copy( aContext.DomainID() );
       
   155     
       
   156     // get the id from the array if it doesn't exist, error
       
   157     index = iDomainArray.Find( searchData, TContextIndex::Match );    
       
   158     
       
   159     if( index == KErrNotFound )
       
   160         {
       
   161         TStreamId sid;
       
   162         // Create a new stream to the store:
       
   163         sid = stream.CreateLC( *iDomainStore );    
       
   164         
       
   165         searchData.iStreamID = sid.Value();
       
   166         
       
   167         // Externalize the domain data
       
   168         aContext.ExternalizeL( stream );
       
   169         stream.CommitL();
       
   170         
       
   171         // add the id to the array
       
   172         iDomainArray.Append( searchData );
       
   173         
       
   174         // Get the stream id of the root
       
   175         rootId = iDomainStore->Root();  
       
   176         
       
   177         // Open the root stream
       
   178         rootStream.ReplaceLC( *iDomainStore, rootId );
       
   179         
       
   180         // Store the changed index
       
   181         StoreDomainIndexL( rootStream );    
       
   182         rootStream.CommitL(); 
       
   183         
       
   184         // Commit the store
       
   185         iDomainStore->CommitL();                      
       
   186         
       
   187         CleanupStack::PopAndDestroy();  // root stream
       
   188         CleanupStack::PopAndDestroy();  // stream       
       
   189         }
       
   190     else
       
   191         {
       
   192         // open an existing one
       
   193         TStreamId sid( iDomainArray[ index ].iStreamID );
       
   194         
       
   195         // Replace the existing stream
       
   196         stream.ReplaceLC( *iDomainStore, sid );
       
   197     
       
   198         // Externalize the permission data
       
   199         aContext.ExternalizeL( stream );
       
   200     
       
   201         // Required by the ReplaceLC
       
   202         stream.CommitL();
       
   203 
       
   204         // Commit the store    
       
   205         iDomainStore->CommitL();
       
   206     
       
   207         CleanupStack::PopAndDestroy(); // stream    
       
   208         }
       
   209         
       
   210     iDomainStore->CompactL();
       
   211     iDomainStore->CommitL();
       
   212     };    
       
   213     
       
   214 // -----------------------------------------------------------------------------
       
   215 // CDRMContextDB::FetchDomainContextL
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CDRMContextDB::FetchDomainContextL( CDRMDomainContext& aContext ) const
       
   219     {   
       
   220     RStoreReadStream stream;
       
   221     TUint32 index = 0;
       
   222 
       
   223     TContextIndex searchData;
       
   224     
       
   225     // Copy the domain id to the search data
       
   226     searchData.iContextID.Copy( aContext.DomainID() );
       
   227     
       
   228     // get the id from the array if it doesn't exist, error
       
   229     User::LeaveIfError( index = iDomainArray.Find( searchData, 
       
   230                                                    TContextIndex::Match ) );
       
   231 
       
   232     TStreamId sid( iDomainArray[ index ].iStreamID );
       
   233     // Open the root stream
       
   234     
       
   235     stream.OpenLC( *iDomainStore, sid );
       
   236 
       
   237     // Read the common id
       
   238     aContext.InternalizeL( stream );
       
   239     
       
   240     // Close the stream
       
   241     CleanupStack::PopAndDestroy();        
       
   242     };
       
   243     
       
   244 // -----------------------------------------------------------------------------
       
   245 // CDRMContextDB::FetchDomainContextL 
       
   246 // For getting with the current id
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 void CDRMContextDB::FetchDomainContextL( CDRMDomainContext& aContext, 
       
   250                                          const TInt32& aStream ) const
       
   251     {
       
   252     RStoreReadStream stream;
       
   253     TStreamId sid( aStream );
       
   254        
       
   255     stream.OpenLC( *iDomainStore, sid );
       
   256 
       
   257     // Read the common id
       
   258     aContext.InternalizeL( stream );
       
   259     
       
   260     // Close the stream
       
   261     CleanupStack::PopAndDestroy();        
       
   262     };    
       
   263     
       
   264 // -----------------------------------------------------------------------------
       
   265 // CDRMContextDB::DeleteDomainContextL
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CDRMContextDB::DeleteDomainContextL( const TDesC8& aDomainID )
       
   269     {
       
   270     TStreamId rootId;
       
   271     TInt index = 0;
       
   272     RStoreWriteStream rootStream;
       
   273 
       
   274     TContextIndex searchData;
       
   275     
       
   276     // Copy the domain id to the search data
       
   277     // The last three digits presents the Domain generation
       
   278     searchData.iContextID.Copy( aDomainID.Mid(0, aDomainID.Length() - 3 ) );
       
   279     
       
   280     // get the id from the array if it doesn't exist, error
       
   281     index = iDomainArray.Find( searchData, TContextIndex::Match ); 
       
   282     
       
   283     User::LeaveIfError( index = iDomainArray.Find( searchData, 
       
   284                                                    TContextIndex::Match ) );
       
   285 
       
   286     TStreamId sid( iDomainArray[ index ].iStreamID );
       
   287 
       
   288     // Delete the stream from the store
       
   289     iDomainStore->DeleteL( sid );
       
   290             
       
   291     iDomainArray.Remove( index );       
       
   292     
       
   293     
       
   294     // Get the stream id of the root
       
   295     rootId = iDomainStore->Root();
       
   296     
       
   297     // Open the root stream
       
   298     rootStream.ReplaceLC( *iDomainStore, rootId );
       
   299         
       
   300     // Store the changed index
       
   301     StoreDomainIndexL( rootStream );    
       
   302     rootStream.CommitL();
       
   303 
       
   304     // Commit the store
       
   305     iDomainStore->CommitL();
       
   306     
       
   307     CleanupStack::PopAndDestroy();  // root stream
       
   308     
       
   309     // Compact and commit the changes
       
   310     iDomainStore->CompactL();
       
   311     iDomainStore->CommitL();        
       
   312     };
       
   313  
       
   314 // -----------------------------------------------------------------------------
       
   315 // CDRMContextDB::StoreRightsIssuerContextL
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CDRMContextDB::StoreRightsIssuerContextL( const CDRMRIContext& aContext )
       
   319     {
       
   320     TStreamId rootId;
       
   321     RStoreWriteStream stream;
       
   322     RStoreWriteStream rootStream;
       
   323     TContextIndex searchData;
       
   324     TInt index;
       
   325     
       
   326     // Copy the domain id to the 
       
   327     searchData.iContextID.Copy( aContext.RIID() );
       
   328     
       
   329     // get the id from the array if it doesn't exist, error
       
   330     index = iRightsIssuerArray.Find( searchData, TContextIndex::Match );    
       
   331     
       
   332     if( index == KErrNotFound )
       
   333         {
       
   334         TStreamId sid;
       
   335         // Create a new stream to the store:
       
   336         sid = stream.CreateLC( *iRightsIssuerStore );    
       
   337         
       
   338         searchData.iStreamID = sid.Value();
       
   339         
       
   340         // Externalize the domain data
       
   341         aContext.ExternalizeL( stream );
       
   342         stream.CommitL();
       
   343         
       
   344         // add the id to the array
       
   345         iRightsIssuerArray.Append( searchData );
       
   346         
       
   347         // Get the stream id of the root
       
   348         rootId = iRightsIssuerStore->Root();  
       
   349         
       
   350         // Open the root stream
       
   351         rootStream.ReplaceLC( *iRightsIssuerStore, rootId );
       
   352         
       
   353         // Store the changed index
       
   354         StoreRightsIssuerIndexL( rootStream );    
       
   355         rootStream.CommitL(); 
       
   356         
       
   357         // Commit the store
       
   358         iRightsIssuerStore->CommitL();                      
       
   359         
       
   360         CleanupStack::PopAndDestroy();  // root stream
       
   361         CleanupStack::PopAndDestroy();  // stream       
       
   362         }
       
   363     else
       
   364         {
       
   365         // open an existing one
       
   366         TStreamId sid( iRightsIssuerArray[ index ].iStreamID );
       
   367         
       
   368         // Replace the existing stream
       
   369         stream.ReplaceLC( *iRightsIssuerStore, sid );
       
   370     
       
   371         // Externalize the permission data
       
   372         aContext.ExternalizeL( stream );
       
   373     
       
   374         // Required by the ReplaceLC
       
   375         stream.CommitL();
       
   376 
       
   377         // Commit the store    
       
   378         iRightsIssuerStore->CommitL();
       
   379     
       
   380         CleanupStack::PopAndDestroy(); // stream    
       
   381         }
       
   382         
       
   383     iRightsIssuerStore->CompactL();
       
   384     iRightsIssuerStore->CommitL();
       
   385     };    
       
   386     
       
   387 // -----------------------------------------------------------------------------
       
   388 // CDRMContextDB::FetchRightsIssuerContextL
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CDRMContextDB::FetchRightsIssuerContextL( CDRMRIContext& aContext ) const
       
   392     {   
       
   393     RStoreReadStream stream;
       
   394     TUint32 index = 0;
       
   395 
       
   396     TContextIndex searchData;
       
   397     
       
   398     // Copy the domain id to the search data
       
   399     searchData.iContextID.Copy( aContext.RIID() );
       
   400     
       
   401     // get the id from the array if it doesn't exist, error
       
   402     User::LeaveIfError( index = iRightsIssuerArray.Find( searchData, 
       
   403                                                    TContextIndex::Match ) );
       
   404 
       
   405     TStreamId sid( iRightsIssuerArray[ index ].iStreamID );
       
   406     // Open the root stream
       
   407     
       
   408     stream.OpenLC( *iRightsIssuerStore, sid );
       
   409 
       
   410     // Read the common id
       
   411     aContext.InternalizeL( stream );
       
   412     
       
   413     // Close the stream
       
   414     CleanupStack::PopAndDestroy();        
       
   415     };
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // CDRMContextDB::FetchRightsIssuerContextL 
       
   419 // For getting with the current id
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 void CDRMContextDB::FetchRightsIssuerContextL( CDRMRIContext& aContext, 
       
   423                                                const TInt32& aStream ) const
       
   424     {
       
   425     RStoreReadStream stream;
       
   426     TStreamId sid( aStream );
       
   427        
       
   428     stream.OpenLC( *iRightsIssuerStore, sid );
       
   429 
       
   430     // Read the common id
       
   431     aContext.InternalizeL( stream );
       
   432     
       
   433     // Close the stream
       
   434     CleanupStack::PopAndDestroy();        
       
   435     };
       
   436 
       
   437 
       
   438 // -----------------------------------------------------------------------------
       
   439 // CDRMContextDB::FetchAllRightsIssuerContextsL
       
   440 // For getting with the current id
       
   441 // -----------------------------------------------------------------------------
       
   442 //
       
   443 void CDRMContextDB::FetchAllRightsIssuerContextsL( RPointerArray<CDRMRIContext>& aContexts) const
       
   444     {
       
   445     CDRMRIContext* context = NULL;
       
   446 
       
   447     // No RI contexts to retrieve
       
   448     if ( iRightsIssuerArray.Count() == 0) 
       
   449         {
       
   450         return;
       
   451         }
       
   452     
       
   453     for( TInt count = 0; count < iRightsIssuerArray.Count(); count++ )
       
   454         {
       
   455         context = CDRMRIContext::NewLC();
       
   456         FetchRightsIssuerContextL( *context, iRightsIssuerArray[ count ].iStreamID );
       
   457         aContexts.AppendL( context );
       
   458         CleanupStack::Pop( context );    
       
   459         }    
       
   460     }
       
   461 
       
   462 // -----------------------------------------------------------------------------
       
   463 // CDRMContextDB::UpdateRightsIssuerContextL
       
   464 // -----------------------------------------------------------------------------
       
   465 //
       
   466 void CDRMContextDB::UpdateRightsIssuerContextL( const CDRMRIContext& aContext )
       
   467     {
       
   468     TStreamId rootId;
       
   469     TInt index = 0;
       
   470     RStoreWriteStream stream;    
       
   471     CDRMRIContext* context = NULL;
       
   472     
       
   473     TContextIndex searchData;
       
   474     
       
   475     // Copy the domain id to the search data
       
   476     searchData.iContextID.Copy( aContext.RIID() );
       
   477     
       
   478     // get the id from the array if it doesn't exist, error
       
   479     User::LeaveIfError( index = iRightsIssuerArray.Find( searchData, 
       
   480                                                    TContextIndex::Match ) );
       
   481 
       
   482     TStreamId sid( iRightsIssuerArray[ index ].iStreamID );
       
   483     
       
   484     // Read the old context:    
       
   485     context = CDRMRIContext::NewLC();
       
   486     FetchRightsIssuerContextL( *context, iRightsIssuerArray[ index ].iStreamID );
       
   487     
       
   488     // update only the metering status:    
       
   489     context->SetMeteringStatus( aContext.IsMeteringAllowed() );
       
   490     
       
   491     // Replace the existing stream
       
   492     stream.ReplaceLC( *iRightsIssuerStore, sid );
       
   493     
       
   494     // Externalize the permission data
       
   495     context->ExternalizeL( stream );
       
   496      
       
   497     // Required by the ReplaceLC
       
   498     stream.CommitL();   
       
   499     
       
   500     // Commit the store    
       
   501     iRightsIssuerStore->CommitL();
       
   502     
       
   503     CleanupStack::PopAndDestroy(); // stream
       
   504     CleanupStack::PopAndDestroy(context);        
       
   505     
       
   506     
       
   507     // Compact and commit the changes
       
   508     iRightsIssuerStore->CompactL();
       
   509     iRightsIssuerStore->CommitL();        
       
   510     };
       
   511 
       
   512 
       
   513     
       
   514 // -----------------------------------------------------------------------------
       
   515 // CDRMContextDB::DeleteRightsIssuerContextL
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 void CDRMContextDB::DeleteRightsIssuerContextL( const TDesC8& aStream )
       
   519     {
       
   520     TStreamId rootId;
       
   521     TInt index = 0;
       
   522     RStoreWriteStream rootStream;
       
   523 
       
   524     TContextIndex searchData;
       
   525     
       
   526     // Copy the domain id to the search data
       
   527     searchData.iContextID.Copy( aStream );
       
   528     
       
   529     // get the id from the array if it doesn't exist, error
       
   530     User::LeaveIfError( index = iRightsIssuerArray.Find( searchData, 
       
   531                                                    TContextIndex::Match ) );
       
   532 
       
   533     TStreamId sid( iRightsIssuerArray[ index ].iStreamID );
       
   534 
       
   535     // Delete the stream from the store
       
   536     iRightsIssuerStore->DeleteL( sid );
       
   537             
       
   538     iRightsIssuerArray.Remove( index );       
       
   539     
       
   540     
       
   541     // Get the stream id of the root
       
   542     rootId = iRightsIssuerStore->Root();
       
   543     
       
   544     // Open the root stream
       
   545     rootStream.ReplaceLC( *iRightsIssuerStore, rootId );
       
   546         
       
   547     // Store the changed index
       
   548     StoreDomainIndexL( rootStream );    
       
   549     rootStream.CommitL();
       
   550 
       
   551     // Commit the store
       
   552     iRightsIssuerStore->CommitL();
       
   553     
       
   554     CleanupStack::PopAndDestroy();  // root stream
       
   555     
       
   556     // Compact and commit the changes
       
   557     iRightsIssuerStore->CompactL();
       
   558     iRightsIssuerStore->CommitL();        
       
   559     }; 
       
   560                            
       
   561 // -----------------------------------------------------------------------------
       
   562 // CDRMContextDB::WhiteListURLExistsL
       
   563 // -----------------------------------------------------------------------------
       
   564 //
       
   565 TBool CDRMContextDB::WhiteListURLExistsL( const TDesC8& aRightsIssuerURL )
       
   566     {
       
   567     CDRMRIContext* context = NULL;
       
   568     TBool retVal = EFalse;
       
   569 
       
   570     if ( iRightsIssuerArray.Count() == 0) 
       
   571         {
       
   572         return retVal;
       
   573         }
       
   574     
       
   575     context = CDRMRIContext::NewLC();
       
   576     
       
   577     for( TInt count = 0; count < iRightsIssuerArray.Count(); count++ )
       
   578         {
       
   579         FetchRightsIssuerContextL( *context, iRightsIssuerArray[ count ].iStreamID );
       
   580         if( context->WhiteListURLExistsL( aRightsIssuerURL ) )
       
   581             {
       
   582             retVal = ETrue;
       
   583             count = iRightsIssuerArray.Count();           
       
   584             }
       
   585         }
       
   586     CleanupStack::PopAndDestroy(); // context       
       
   587     return retVal;    
       
   588     };    
       
   589 
       
   590 // -----------------------------------------------------------------------------
       
   591 // CDRMContextDB::DeleteExpiredDomainContextsL
       
   592 // -----------------------------------------------------------------------------
       
   593 //
       
   594 void CDRMContextDB::DeleteExpiredDomainContextsL( const TTime& aTime )
       
   595     {  
       
   596     CDRMDomainContext* context = NULL;    
       
   597     TStreamId rootId;
       
   598     RStoreWriteStream rootStream;
       
   599 
       
   600     // Check if there is anything to go through
       
   601     if ( iDomainArray.Count() == 0) 
       
   602         {
       
   603         return;
       
   604         }
       
   605       
       
   606     context = CDRMDomainContext::NewLC();
       
   607         
       
   608     // Delete all permissions from the store which have expired    
       
   609     for( TInt i = 0; i < iDomainArray.Count();)
       
   610         {
       
   611         FetchDomainContextL( *context, iDomainArray[i].iStreamID );
       
   612         
       
   613         if( context->ExpiryTime() < aTime && context->ExpiryTime() != Time::NullTTime() )
       
   614             {
       
   615             iDomainStore->DeleteL( TStreamId( iDomainArray[i].iStreamID ) );
       
   616             iDomainArray.Remove(i);
       
   617             }
       
   618         else 
       
   619             {
       
   620             i++; // increment the counter for the next run       
       
   621             }   
       
   622         }
       
   623         
       
   624     CleanupStack::PopAndDestroy();  // context
       
   625     
       
   626     // Get the stream id of the root
       
   627     rootId = iDomainStore->Root();
       
   628     
       
   629     // Open the root stream
       
   630     rootStream.ReplaceLC( *iDomainStore, rootId );
       
   631         
       
   632     // Store the changed index
       
   633     StoreDomainIndexL( rootStream );    
       
   634     rootStream.CommitL();
       
   635 
       
   636     // Commit the store
       
   637     iDomainStore->CommitL();
       
   638     
       
   639     CleanupStack::PopAndDestroy();  // root stream
       
   640     
       
   641     // Compact and Commit the store
       
   642     iDomainStore->CompactL();
       
   643     iDomainStore->CommitL();          
       
   644     };
       
   645 
       
   646 // -----------------------------------------------------------------------------
       
   647 // CDRMContextDB::DeleteExpiredRightsIssuerContextsL
       
   648 // -----------------------------------------------------------------------------
       
   649 //
       
   650 void CDRMContextDB::DeleteExpiredRightsIssuerContextsL( const TTime& aTime )
       
   651     {
       
   652     CDRMRIContext* context = NULL;    
       
   653     TStreamId rootId;
       
   654     RStoreWriteStream rootStream;
       
   655 
       
   656     // Check if there is anything to go through
       
   657     if ( iRightsIssuerArray.Count() == 0) 
       
   658         {
       
   659         return;
       
   660         }
       
   661         
       
   662     context = CDRMRIContext::NewLC(); 
       
   663            
       
   664     // Delete all permissions from the store which have expired    
       
   665     for( TInt i = 0; i < iRightsIssuerArray.Count();)
       
   666         {
       
   667         FetchRightsIssuerContextL( *context, iRightsIssuerArray[i].iStreamID );
       
   668         
       
   669         if( context->ExpiryTime() < aTime && context->ExpiryTime() != Time::NullTTime() )
       
   670             {
       
   671             iRightsIssuerStore->DeleteL( TStreamId( iRightsIssuerArray[i].iStreamID ) );
       
   672             iRightsIssuerArray.Remove(i);
       
   673             }
       
   674         else
       
   675             {
       
   676             i++; // increment the counter for the next run                 
       
   677             }       
       
   678         }
       
   679         
       
   680     CleanupStack::PopAndDestroy();  // context 
       
   681     
       
   682     // Get the stream id of the root
       
   683     rootId = iRightsIssuerStore->Root();
       
   684     
       
   685     // Open the root stream
       
   686     rootStream.ReplaceLC( *iRightsIssuerStore, rootId );
       
   687         
       
   688     // Store the changed index
       
   689     StoreRightsIssuerIndexL( rootStream );    
       
   690     rootStream.CommitL();
       
   691 
       
   692     // Commit the store
       
   693     iRightsIssuerStore->CommitL();
       
   694     
       
   695     CleanupStack::PopAndDestroy();  // root stream
       
   696     
       
   697     // Compact and Commit the store
       
   698     iRightsIssuerStore->CompactL();
       
   699     iRightsIssuerStore->CommitL();           
       
   700     };
       
   701 
       
   702 // -----------------------------------------------------------------------------
       
   703 // CDRMContextDB::CreateAndInitializeRIStoreL
       
   704 // -----------------------------------------------------------------------------
       
   705 //
       
   706 void CDRMContextDB::CreateAndInitializeRIStoreL( const TFileName& aRightsIssuerStore )
       
   707     {
       
   708     TStreamId rootId;  
       
   709     RStoreWriteStream rootStream;
       
   710         
       
   711     // Create and initialize the permanent file store
       
   712     iRightsIssuerStore = CPermanentFileStore::ReplaceL( iFileServer, 
       
   713                                                         aRightsIssuerStore,
       
   714                                                         EFileRead|EFileWrite );                                          
       
   715     iRightsIssuerStore->SetTypeL( KPermanentFileStoreLayoutUid );
       
   716     iRightsIssuerStore->CommitL(); 
       
   717     
       
   718     // Create the root stream:                                                   
       
   719     rootId = rootStream.CreateLC( *iRightsIssuerStore );
       
   720          
       
   721     // Create the index
       
   722     // Commit the previous changes 
       
   723     StoreRightsIssuerIndexL( rootStream );
       
   724     rootStream.CommitL();
       
   725     
       
   726     iRightsIssuerStore->SetRootL( rootId );
       
   727     iRightsIssuerStore->CommitL();  
       
   728     
       
   729     // Commit the changes to the store as well
       
   730     CleanupStack::PopAndDestroy(); // rootStream
       
   731     
       
   732     iRightsIssuerStore->CompactL();
       
   733     iRightsIssuerStore->CommitL();
       
   734     };
       
   735 
       
   736 
       
   737 // -----------------------------------------------------------------------------
       
   738 // CDRMContextDB::CreateAndInitializeDomainStoreL
       
   739 // -----------------------------------------------------------------------------
       
   740 //
       
   741 void CDRMContextDB::CreateAndInitializeDomainStoreL( const TFileName& aDomainStore )
       
   742     {
       
   743     TStreamId rootId;  
       
   744     RStoreWriteStream rootStream;
       
   745         
       
   746     // Create and initialize the permanent file store
       
   747     iDomainStore = CPermanentFileStore::ReplaceL( iFileServer, 
       
   748                                                         aDomainStore,
       
   749                                                         EFileRead|EFileWrite );                                          
       
   750     iDomainStore->SetTypeL( KPermanentFileStoreLayoutUid );
       
   751     iDomainStore->CommitL(); 
       
   752     
       
   753     // Create the root stream:                                                   
       
   754     rootId = rootStream.CreateLC( *iDomainStore );
       
   755                                                
       
   756     // Create the index
       
   757     // Commit the previous changes 
       
   758     StoreDomainIndexL( rootStream );
       
   759     rootStream.CommitL();
       
   760     
       
   761     iDomainStore->SetRootL( rootId );   
       
   762     iDomainStore->CommitL();    
       
   763         
       
   764     // Commit the changes to the store as well
       
   765     CleanupStack::PopAndDestroy(); // rootStream 
       
   766     
       
   767     iDomainStore->CompactL();
       
   768     iDomainStore->CommitL();       
       
   769     };
       
   770 
       
   771 // -----------------------------------------------------------------------------
       
   772 // CDRMContextDB::StoreRightsIssuerIndexL
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CDRMContextDB::StoreRightsIssuerIndexL( RWriteStream& aStream ) const
       
   776     {
       
   777     // Write the array size and possibly the array to the stream
       
   778     aStream.WriteInt32L( iRightsIssuerArray.Count() );
       
   779              
       
   780     for( TInt count = 0; count < iRightsIssuerArray.Count(); count++ )
       
   781         {
       
   782         aStream << iRightsIssuerArray[ count ].iContextID;
       
   783         aStream.WriteUint32L( iRightsIssuerArray[ count ].iStreamID );
       
   784         }
       
   785     };
       
   786 
       
   787 // -----------------------------------------------------------------------------
       
   788 // CDRMContextDB::StoreDomainIndexL
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 void CDRMContextDB::StoreDomainIndexL( RWriteStream& aStream ) const
       
   792     {
       
   793     // Write the array size and possibly the array to the stream
       
   794     aStream.WriteInt32L( iDomainArray.Count() );
       
   795     
       
   796     for( TInt count = 0; count < iDomainArray.Count(); count++ )
       
   797         {
       
   798         aStream << iDomainArray[ count ].iContextID;
       
   799         aStream.WriteUint32L( iDomainArray[ count ].iStreamID );
       
   800         }
       
   801     };
       
   802     
       
   803 // -----------------------------------------------------------------------------
       
   804 // CDRMContextDB::FetchRightsIssuerIndexL
       
   805 // -----------------------------------------------------------------------------
       
   806 //
       
   807 void CDRMContextDB::FetchRightsIssuerIndexL()
       
   808     {
       
   809     TStreamId rootId;
       
   810     RStoreReadStream stream;
       
   811     TInt count = 0;
       
   812     TContextIndex index;
       
   813  
       
   814     // Get the stream id of the root
       
   815     rootId = iRightsIssuerStore->Root();
       
   816     
       
   817     // Open the root stream
       
   818     stream.OpenLC( *iRightsIssuerStore, rootId );
       
   819     
       
   820     // read the count of the array:
       
   821     iRightsIssuerArray.Reset();
       
   822     
       
   823     count = stream.ReadInt32L();
       
   824     
       
   825     for( ; count > 0; count-- )
       
   826         {
       
   827         stream >> index.iContextID;
       
   828         index.iStreamID = stream.ReadInt32L();
       
   829         iRightsIssuerArray.Append( index );
       
   830         }
       
   831      
       
   832     // Close the stream
       
   833     CleanupStack::PopAndDestroy();    
       
   834     };
       
   835     
       
   836 // -----------------------------------------------------------------------------
       
   837 // CDRMContextDB::FetchDomainIndexL
       
   838 // -----------------------------------------------------------------------------
       
   839 //
       
   840 void CDRMContextDB::FetchDomainIndexL()
       
   841     {
       
   842     TStreamId rootId;
       
   843     RStoreReadStream stream;
       
   844     TInt count = 0;
       
   845     TContextIndex index;
       
   846  
       
   847     // Get the stream id of the root
       
   848     rootId = iDomainStore->Root();
       
   849     
       
   850     // Open the root stream
       
   851     stream.OpenLC( *iDomainStore, rootId );
       
   852     
       
   853     // read the count of the array:
       
   854     iDomainArray.Reset();
       
   855     
       
   856     count = stream.ReadInt32L();
       
   857     
       
   858     for( ; count > 0; count-- )
       
   859         {
       
   860         stream >> index.iContextID;
       
   861         index.iStreamID = stream.ReadInt32L();
       
   862         iDomainArray.Append( index );
       
   863         }
       
   864      
       
   865     // Close the stream
       
   866     CleanupStack::PopAndDestroy();    
       
   867     };
       
   868 
       
   869 // -----------------------------------------------------------------------------
       
   870 // CDRMContextDB::DeleteAllL
       
   871 // -----------------------------------------------------------------------------
       
   872 //
       
   873 void CDRMContextDB::DeleteAllL( const TFileName& aRightsIssuerContextFile, 
       
   874                                 const TFileName& aDomainContextFile )
       
   875     {
       
   876     delete iRightsIssuerStore;
       
   877     iRightsIssuerStore = NULL;
       
   878     iRightsIssuerArray.Reset();
       
   879     CreateAndInitializeRIStoreL( aRightsIssuerContextFile );
       
   880  
       
   881     delete iDomainStore;
       
   882     iDomainStore = NULL;
       
   883     iDomainArray.Reset();
       
   884     CreateAndInitializeDomainStoreL( aDomainContextFile );
       
   885     }
       
   886 
       
   887 // -----------------------------------------------------------------------------
       
   888 // Default Constructor - First phase.
       
   889 // Can be used by itself to generate an empty object
       
   890 // -----------------------------------------------------------------------------
       
   891 //
       
   892 CDRMContextDB::CDRMContextDB( RFs& aFileServer ) :
       
   893     iFileServer( aFileServer )
       
   894     {
       
   895     };
       
   896 
       
   897 // -----------------------------------------------------------------------------
       
   898 // 2nd phase Constructor
       
   899 // -----------------------------------------------------------------------------
       
   900 //
       
   901 void CDRMContextDB::ConstructL( const TFileName& aRightsIssuerContextFile, 
       
   902                                 const TFileName& aDomainContextFile )
       
   903     {
       
   904     TInt error = KErrNone;
       
   905     
       
   906     // Open the file store
       
   907     TRAP( error, iRightsIssuerStore = CPermanentFileStore::OpenL( iFileServer, 
       
   908                                                       aRightsIssuerContextFile, 
       
   909                                                       EFileRead|EFileWrite ) );
       
   910     if( error == KErrNotFound )
       
   911         {
       
   912         CreateAndInitializeRIStoreL( aRightsIssuerContextFile );
       
   913         }
       
   914     else 
       
   915         {   
       
   916         User::LeaveIfError( error );
       
   917         
       
   918         // Get the index
       
   919         FetchRightsIssuerIndexL();       
       
   920         }    
       
   921     
       
   922     
       
   923     // Open the file store
       
   924     TRAP( error, iDomainStore = CPermanentFileStore::OpenL( iFileServer, 
       
   925                                                       aDomainContextFile, 
       
   926                                                       EFileRead|EFileWrite ) );
       
   927     if( error == KErrNotFound ) 
       
   928         {
       
   929         CreateAndInitializeDomainStoreL( aDomainContextFile );
       
   930         }
       
   931     else 
       
   932         {   
       
   933         User::LeaveIfError( error );
       
   934         
       
   935         // Get the index
       
   936         FetchDomainIndexL();       
       
   937         
       
   938         }
       
   939     };
       
   940             
       
   941 // End of File