omadrm/drmengine/server/src/DRMObsoleteFinder.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of the DRM Rights Database
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <e32std.h>		// RPointerArray
       
    22 #include <e32def.h>		// Type definitions
       
    23 
       
    24 #include <caf/caf.h>
       
    25 #include <dcfrep.h>
       
    26 #include "DRMCommon.h"	// DRM Error messages
       
    27 #include "DRMObsoleteFinder.h"
       
    28 #include "DRMRightsDB.h"
       
    29 #include "drmlog.h"
       
    30 #include <dcfrep.h>
       
    31 
       
    32 #include <SysUtil.h>    // Disk space checking
       
    33 
       
    34 #ifdef RD_MULTIPLE_DRIVE
       
    35 #include <DriveInfo.h>
       
    36 #endif
       
    37 
       
    38 // EXTERNAL DATA STRUCTURES
       
    39 
       
    40 // EXTERNAL FUNCTION PROTOTYPES  
       
    41 
       
    42 // CONSTANTS
       
    43 
       
    44 // MACROS
       
    45 
       
    46 // LOCAL CONSTANTS AND MACROS
       
    47 
       
    48 const TInt KScanFileSystem = 1;
       
    49 const TInt KScanContents = 2;
       
    50 const TInt KRemoveUsedParents = 3;
       
    51 const TInt KWriteTempFile = 4;
       
    52 
       
    53 // MODULE DATA STRUCTURES
       
    54 
       
    55 // LOCAL FUNCTION PROTOTYPES
       
    56 LOCAL_C TInt CompareHBufC8( const HBufC8& aFirst, const HBufC8& aSecond );
       
    57 
       
    58 // FORWARD DECLARATIONS
       
    59 
       
    60 // ============================= LOCAL FUNCTIONS ===============================
       
    61 
       
    62 LOCAL_C TInt CompareHBufC8( const HBufC8& aFirst, const HBufC8& aSecond )
       
    63     {
       
    64 	return aFirst.Compare( aSecond );
       
    65     };
       
    66 
       
    67 // ============================ MEMBER FUNCTIONS ===============================
       
    68 
       
    69         
       
    70 // -----------------------------------------------------------------------------
       
    71 // CDRMObsoleteFinder::NewL
       
    72 // Two-phased constructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CDRMObsoleteFinder* CDRMObsoleteFinder::NewL( RFs& aFs,
       
    76                                             CDRMRightsDB* aDatabase,
       
    77                                             TRequestStatus& aStatus,
       
    78                                             RWriteStream& aStream,
       
    79                                             TBool aPerformScan )
       
    80     {
       
    81     CDRMObsoleteFinder* self = new( ELeave ) CDRMObsoleteFinder( aFs, 
       
    82                                                         aDatabase, aStatus, 
       
    83                                                         aStream );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL( aPerformScan );
       
    86     CleanupStack::Pop();
       
    87     
       
    88     return self;
       
    89     }
       
    90   
       
    91         
       
    92 // -----------------------------------------------------------------------------
       
    93 // Destructor        
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 CDRMObsoleteFinder::~CDRMObsoleteFinder()
       
    97     {
       
    98     // just in case
       
    99     Cancel();
       
   100     
       
   101     if( iDcfClient )
       
   102         {
       
   103         delete iDcfClient;
       
   104         iDcfClient = NULL;    
       
   105         }
       
   106     
       
   107     if( iContents )
       
   108         {
       
   109         delete iContents;
       
   110         iContents = NULL;   
       
   111         }
       
   112 
       
   113     if( iParents )
       
   114         {
       
   115         delete iParents;
       
   116         iParents = NULL;            
       
   117         }
       
   118      
       
   119     if( iNoContents )
       
   120         {
       
   121         delete iNoContents;
       
   122         iNoContents = NULL;            
       
   123         }            
       
   124     };
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CDRMObsoleteFinder::ExecuteCleanupLD        
       
   128 // -----------------------------------------------------------------------------
       
   129 //    
       
   130 void CDRMObsoleteFinder::ExecuteFinderLD()
       
   131     {
       
   132     TRequestStatus* status = 0;
       
   133     
       
   134     if( !IsAdded() ) 
       
   135         {
       
   136         CActiveScheduler::Add(this);        
       
   137         }
       
   138     if ( !IsActive() ) 
       
   139         {
       
   140         SetActive();        
       
   141         }
       
   142     
       
   143     iOperationStatus = KRequestPending;
       
   144     status = &iStatus;
       
   145     User::RequestComplete(status,KErrNone);        
       
   146     };
       
   147 
       
   148 // -----------------------------------------------------------------------------                        
       
   149 // Default Constructor - First phase.
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 CDRMObsoleteFinder::CDRMObsoleteFinder( RFs& aFs,
       
   153                                         CDRMRightsDB* aDatabase,
       
   154                                         TRequestStatus& aStatus,
       
   155                                         RWriteStream& aStream ) :
       
   156     CActive( EPriorityLow ),
       
   157     iContents( NULL ),
       
   158     iParents( NULL ),
       
   159     iNoContents( NULL ),
       
   160     iIndex( -1 ),
       
   161     iFileServer( aFs ),
       
   162     iStream( aStream ),
       
   163     iRightsDb( aDatabase ),
       
   164     iOperationStatus( aStatus ),
       
   165     iState( KScanFileSystem )                         
       
   166     {
       
   167         
       
   168     }; 
       
   169         
       
   170 
       
   171 // -----------------------------------------------------------------------------                        
       
   172 // CDRMObsoleteFinder::ConstructL
       
   173 // -----------------------------------------------------------------------------
       
   174 // 
       
   175 void CDRMObsoleteFinder::ConstructL( const TBool aPerformScan )
       
   176     {
       
   177     // if the scan needs to be done, the initial state is different:
       
   178     if( aPerformScan )
       
   179         {
       
   180         iState = KScanFileSystem;    
       
   181         }
       
   182     else
       
   183         {
       
   184         iState = KScanContents;    
       
   185         } 
       
   186        
       
   187     // connect to dcf repository
       
   188     iDcfClient = CDcfRep::NewL();
       
   189     
       
   190     // Create a new list
       
   191     iContents = CDRMPointerArray<HBufC8>::NewL();    
       
   192     
       
   193     // Create a new list
       
   194     iParents = CDRMPointerArray<HBufC8>::NewL();  
       
   195 
       
   196     // Create a new list
       
   197     iNoContents = CDRMPointerArray<HBufC8>::NewL();  
       
   198         
       
   199     };  
       
   200 
       
   201 // -----------------------------------------------------------------------------                        
       
   202 // CDRMObsoleteFinder::RunError
       
   203 // -----------------------------------------------------------------------------
       
   204 //  
       
   205 TInt CDRMObsoleteFinder::RunError(TInt aError)
       
   206     {
       
   207     TRequestStatus* status = 0;
       
   208     
       
   209     if( aError != KErrNone )
       
   210         {
       
   211         status = &iOperationStatus;
       
   212         User::RequestComplete( status, aError );                
       
   213         delete this;            
       
   214         }
       
   215     return KErrNone;
       
   216     };
       
   217 
       
   218 // -----------------------------------------------------------------------------                        
       
   219 // CDRMObsoleteFinder::RunL
       
   220 // -----------------------------------------------------------------------------
       
   221 //      
       
   222 void CDRMObsoleteFinder::RunL()
       
   223     {
       
   224     TRequestStatus* status = 0;    
       
   225     TInt error = KErrNone;
       
   226     HBufC8* buffer = NULL;
       
   227 
       
   228     // If the status of the cleaning is other than KErrNone
       
   229     User::LeaveIfError( iStatus.Int() );
       
   230 
       
   231     switch( iState )
       
   232         {
       
   233         case KScanFileSystem:
       
   234             iState = KScanContents;
       
   235             iDcfClient->RefreshDcf( iStatus );            
       
   236             SetActive();    
       
   237             break;
       
   238         case KScanContents:
       
   239             if( iIndex == -1 )
       
   240                 {
       
   241                 // Get the contents
       
   242                 iRightsDb->GetContentIDListL( *iContents );
       
   243                 // Reset the index
       
   244                 iIndex = 0;  
       
   245                 }
       
   246             else 
       
   247                 {
       
   248                 iIndex++;                     
       
   249                 }  
       
   250                 
       
   251             if( iIndex >= iContents->Count() )
       
   252                 {
       
   253                 iState = KRemoveUsedParents;
       
   254                 iIndex = -1;   
       
   255                 }
       
   256             else 
       
   257                 {
       
   258                 // Check if there is content
       
   259                 TRAP( error, iDcfClient->OrderListL( *(*iContents)[iIndex] ));
       
   260         
       
   261                 // If an error occurs, leave if it's ok, continue
       
   262                 if( error != KErrNotFound )
       
   263                     {
       
   264                     User::LeaveIfError( error ); 
       
   265                     
       
   266                     // Get all the parents
       
   267                     if( !error ) 
       
   268                         {
       
   269                         GetParentsL( *(*iContents)[iIndex], *iParents );   
       
   270                         }                      
       
   271                     }
       
   272                 // If the error is not found, add to the no content list    
       
   273                 else
       
   274                     {
       
   275                     buffer = (*iContents)[iIndex]->AllocLC();
       
   276                     iNoContents->AppendL( buffer );
       
   277                     CleanupStack::Pop();    
       
   278                     }                       
       
   279                 }
       
   280             SetActive();
       
   281             status = &iStatus;
       
   282             User::RequestComplete(status,KErrNone);              
       
   283             break;
       
   284         case KRemoveUsedParents:
       
   285             if( iIndex == -1 )
       
   286                 {
       
   287                 iIndex = 0;  
       
   288                 }
       
   289             else 
       
   290                 {
       
   291                 iIndex++;                     
       
   292                 }
       
   293                   
       
   294             if( iIndex >= iParents->Count() )
       
   295                 {
       
   296                 iState = KWriteTempFile;
       
   297                 iIndex = -1; 
       
   298                 }
       
   299             else
       
   300                 {
       
   301                 // Find the parent
       
   302                 error = iNoContents->FindInOrder( (*iParents)[iIndex], 
       
   303                                        TLinearOrder<HBufC8>(CompareHBufC8));
       
   304                                       
       
   305                 if( error != KErrNotFound )
       
   306                     {
       
   307                     buffer = (*iNoContents)[error];
       
   308                     iNoContents->Remove( error );
       
   309                     delete buffer;
       
   310                     buffer = 0;    
       
   311                     }                                                           
       
   312                 }
       
   313             SetActive();
       
   314             status = &iStatus;
       
   315             User::RequestComplete(status,KErrNone);                 
       
   316             break;
       
   317         case KWriteTempFile:
       
   318             ObsoleteToStreamL();
       
   319             // we are complete:
       
   320             status = &iOperationStatus;
       
   321             User::RequestComplete( status, KErrNone );                
       
   322             delete this; 
       
   323             return;                          
       
   324         default:
       
   325             // illegal status, return error and delete object
       
   326             status = &iOperationStatus;
       
   327             User::RequestComplete( status, KErrGeneral );
       
   328             delete this; 
       
   329             return;                       
       
   330         }
       
   331     };    
       
   332 
       
   333 
       
   334 // ----------------------------------------------------------------------------
       
   335 // CDRMObsoleteFinder::GetParentsL
       
   336 // ----------------------------------------------------------------------------
       
   337 //
       
   338 void CDRMObsoleteFinder::GetParentsL( const TDesC8& aContentId,
       
   339                                       RPointerArray<HBufC8>& aParents )
       
   340     {
       
   341     HBufC8* parentId = NULL;
       
   342     TInt error = KErrNone;
       
   343     CDRMPointerArray<CDRMPermission>* permissions = 
       
   344       CDRMPointerArray<CDRMPermission>::NewLC();
       
   345     permissions->SetAutoCleanup( ETrue );
       
   346     CDRMPointerArray<CDRMPermission>& perm = *permissions;  
       
   347 
       
   348     TRAP( error, iRightsDb->GetDBEntryByContentIDL( aContentId, *permissions ) );
       
   349     
       
   350     // If there are no keys it means that there is encryption key and such, but
       
   351     // no available permissions
       
   352     if( error == KErrCANoRights )
       
   353         {
       
   354         CleanupStack::PopAndDestroy(); // permissions
       
   355         return;    
       
   356         }
       
   357     else 
       
   358         {
       
   359         User::LeaveIfError(error);    
       
   360         }
       
   361         
       
   362     for( TInt i = 0; i < permissions->Count(); i++, error = KErrNone )
       
   363         {
       
   364         // Check if the permission has a parent
       
   365         if( perm[i]->iParentUID ) 
       
   366             {
       
   367             // if it does, insert it to the aParents array
       
   368             error = aParents.FindInOrder( perm[i]->iParentUID, 
       
   369                                           TLinearOrder<HBufC8>(CompareHBufC8));
       
   370             if( error == KErrNotFound )
       
   371                 {
       
   372                 parentId = perm[i]->iParentUID->AllocLC();
       
   373                 User::LeaveIfError( aParents.InsertInOrder(parentId, 
       
   374                                     TLinearOrder<HBufC8>(CompareHBufC8)) );
       
   375                 CleanupStack::Pop();       
       
   376                 }
       
   377             }
       
   378         }
       
   379 
       
   380     CleanupStack::PopAndDestroy(); // permissions     
       
   381     }; 
       
   382 
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // CDRMObsoleteFinder::ObsoleteToStreamL
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 void CDRMObsoleteFinder::ObsoleteToStreamL()
       
   389     {
       
   390     TInt count( 0 );
       
   391 
       
   392 
       
   393     TInt size( 4 ); // size of the count
       
   394     for( count = 0; count < iNoContents->Count(); count++ )
       
   395         {
       
   396         size += (*iNoContents)[ count ]->Size();
       
   397         size += sizeof(TUint16);
       
   398         }
       
   399 
       
   400     // Reset count variable:
       
   401     count = 0;
       
   402 
       
   403 #ifndef RD_MULTIPLE_DRIVE
       
   404 
       
   405     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFileServer, 
       
   406                                                 size, 
       
   407                                                 EDriveC ) )
       
   408     
       
   409 #else //RD_MULTIPLE_DRIVE
       
   410     
       
   411     TInt driveNumber( -1 );
       
   412     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
   413 
       
   414     if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFileServer, 
       
   415                                                 size, 
       
   416                                                 driveNumber ) )
       
   417     
       
   418 #endif
       
   419 
       
   420         {
       
   421         DRMLOG( _L( "CDRMDbSession::UriListToFileL: KErrDiskFull" ) );
       
   422         User::Leave( KErrDiskFull );
       
   423         }     
       
   424     // Write the whole stuff into the file.
       
   425     while( count < iNoContents->Count() )
       
   426         {
       
   427         iStream.WriteUint16L( (*iNoContents)[count]->Length() );
       
   428         iStream.WriteL( *(*iNoContents)[count] );
       
   429         ++count;
       
   430         }
       
   431     // Finish with a 0    
       
   432     iStream.WriteUint16L( 0 );    
       
   433     
       
   434     iStream.CommitL();
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------                        
       
   438 // CDRMObsoleteFinder::DoCancel
       
   439 // -----------------------------------------------------------------------------
       
   440 //      
       
   441 void CDRMObsoleteFinder::DoCancel()
       
   442     {  
       
   443     }; 
       
   444 
       
   445 // -----------------------------------------------------------------------------                        
       
   446 // CDRMObsoleteFinder::DoCleanup
       
   447 // -----------------------------------------------------------------------------
       
   448 //      
       
   449 void CDRMObsoleteFinder::DoCleanup()
       
   450     {
       
   451     TRequestStatus* status = 0;      
       
   452     if( iCancel <= 0 )
       
   453         {
       
   454         iCancel = 1;
       
   455         status = &iStatus;
       
   456         User::RequestComplete(status, KErrCancel);
       
   457         }       
       
   458     }; 
       
   459 
       
   460 
       
   461 
       
   462 
       
   463 
       
   464 
       
   465 
       
   466