omadrm/drmengine/server/src/drmconsume.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2005 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 class CDRMConsume.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "drmconsume.h"
       
    21 #include "drmenginetypedefs.h"
       
    22 #include "drmrightsdb.h"
       
    23 #include "drmrightsserver.h"
       
    24 #include "drmdbsession.h"
       
    25 #include "drmlog.h"
       
    26 #include "drmeventmodify.h"
       
    27 #include "drmnotifier.h"
       
    28 #ifdef RD_DRM_METERING
       
    29 #include "drmmeteringdb.h"
       
    30 #include "drmmeteringdbdata.h"
       
    31 #include "roapstorageclient.h"
       
    32 #include "drmricontext.h"
       
    33 #endif
       
    34 
       
    35 // EXTERNAL DATA STRUCTURES
       
    36 // EXTERNAL FUNCTION PROTOTYPES
       
    37 // CONSTANTS
       
    38 // MACROS
       
    39 
       
    40 #define NOTIFIER static_cast< CDRMRightsServer* >( const_cast< CServer2* >( \
       
    41            iSession.Server() ) )->Notifier()
       
    42 
       
    43 #define DB static_cast< CDRMRightsServer* >( const_cast< CServer2* >( \
       
    44            iSession.Server() ) )->Database()
       
    45            
       
    46 #define SECURETIME( a ) static_cast< CDRMRightsServer* >\
       
    47            ( const_cast< CServer2* >( \
       
    48            iSession.Server() ) )->GetSecureTime( a )
       
    49            
       
    50 #define SERVER static_cast< CDRMRightsServer* >( const_cast< CServer2* >( \
       
    51            iSession.Server() ) )
       
    52            
       
    53 #ifdef RD_DRM_METERING
       
    54 #define METERINGDB static_cast< CDRMRightsServer* >( const_cast< CServer2* >( \
       
    55            iSession.Server() ) )->MeteringDatabase() 
       
    56 #endif
       
    57 
       
    58 #define SETBIT( a, b ) ( a ) |= ( b )
       
    59 #define CLRBIT( a, b ) ( a ) &= ~( 0xff & b )
       
    60 #define ISSET( a, b ) ( ( a ) & ( b ) )
       
    61 
       
    62 // LOCAL CONSTANTS AND MACROS
       
    63 // Keeps track about modified permissions for UpdateDBL().
       
    64 //static const TUint8 KConsumeParentModified =   0x01;
       
    65 static const TUint8 KConsumeChildModified =    0x02;
       
    66 
       
    67 static const TUint8 KConsumeHasSecureTime =    0x08;
       
    68 
       
    69 // These keep track whether to decrease timed counters when consuming time
       
    70 // based stuff (iTimedCounts member). A used timed count bit is set to zero.
       
    71 // In the beginning all of these are set to one.
       
    72 //static const TUint8 KParentToplevelCount =     0x01;    
       
    73 //static const TUint8 KParentPermCount     =     0x02;
       
    74 static const TUint8 KChildToplevelCount  =     0x04;
       
    75 static const TUint8 KChildPermCount      =     0x08;
       
    76 
       
    77 static const TUint16 KConsumeDefaultTimer = 300; // 5mins
       
    78 
       
    79 // LOCAL FUNCTION PROTOTYPES
       
    80 static inline void PickSmaller( TTime& aFirst,
       
    81                                 const TTime& aSecond );
       
    82 
       
    83 // ============================= LOCAL FUNCTIONS ==========================
       
    84 
       
    85 // ------------------------------------------------------------------------
       
    86 // PickSmaller (overloaded)
       
    87 //
       
    88 // Set the smaller one to aFirst
       
    89 // ------------------------------------------------------------------------
       
    90 //
       
    91 void PickSmaller( TTime& aFirst,
       
    92                   const TTime& aSecond )
       
    93     {
       
    94     if ( aSecond < aFirst )
       
    95         {
       
    96         aFirst = aSecond;
       
    97         }
       
    98     }
       
    99     
       
   100 // ------------------------------------------------------------------------
       
   101 // PickSmaller (overloaded)
       
   102 //
       
   103 // Set the smaller one to aFirst
       
   104 // ------------------------------------------------------------------------
       
   105 //
       
   106 void PickSmaller( TTimeIntervalSeconds& aFirst,
       
   107                   const TTimeIntervalSeconds& aSecond )
       
   108     {
       
   109     if ( aSecond < aFirst )
       
   110         {
       
   111         aFirst = aSecond;
       
   112         }
       
   113     }
       
   114     
       
   115 // ============================ MEMBER FUNCTIONS ==========================
       
   116 
       
   117 // ------------------------------------------------------------------------
       
   118 // CDRMConsume::NewLC
       
   119 // ------------------------------------------------------------------------
       
   120 //
       
   121 CDRMConsume* CDRMConsume::NewLC( CDRMDbSession& aSession,
       
   122                                  const TDesC8& aURI,
       
   123                                  const TDesC8* aParentId )
       
   124     {
       
   125     DRMLOG( _L( "CDRMConsume::NewLC" ) );
       
   126     CDRMConsume* self = new( ELeave ) CDRMConsume( aSession );
       
   127     
       
   128     CleanupStack::PushL( self );
       
   129     
       
   130     self->ConstructL( aURI, aParentId );
       
   131     
       
   132     DRMLOG( _L( "CDRMConsume::NewLC ok" ) );
       
   133     return self;    
       
   134     }
       
   135 
       
   136 // ------------------------------------------------------------------------
       
   137 // CDRMConsume::~CDRMConsume
       
   138 // ------------------------------------------------------------------------
       
   139 //
       
   140 CDRMConsume::~CDRMConsume()
       
   141     {
       
   142     DRMLOG( _L( "CDRMConsume::~" ) );
       
   143     TInt error( KErrNone );
       
   144     
       
   145     if( IsActive() ) 
       
   146         {
       
   147         Cancel();        
       
   148         }
       
   149     else
       
   150         {
       
   151         TRAP( error, DoCancelL() );
       
   152         }
       
   153     #ifdef RD_DRM_METERING
       
   154         // Update metering count and metering accumulated time to the database.
       
   155         TRAP( error, UpdateMeteringDbL() );
       
   156     #endif
       
   157     // ignore errors
       
   158     //TRAP( error, UpdateDBL() );
       
   159     
       
   160     delete iURI; iURI = NULL;
       
   161     delete iParentId; iParentId = NULL;
       
   162     delete iChild; iChild = NULL;
       
   163     delete iCombined; iCombined = NULL;
       
   164     DRMLOG( _L( "CDRMConsume::~ ok" ) );
       
   165     }
       
   166 
       
   167 // ------------------------------------------------------------------------
       
   168 // CDRMConsume::CDRMConsume
       
   169 //
       
   170 // The internal state members iTimedCounts and iCounters are by default
       
   171 // set to 00001111b, and the appropriate bits are lowered when something
       
   172 // changes.
       
   173 // ------------------------------------------------------------------------
       
   174 //
       
   175 CDRMConsume::CDRMConsume( CDRMDbSession& aSession ):
       
   176     CTimer( EPriorityHigh ),
       
   177     iSession( aSession ),
       
   178     iIntent( ContentAccess::EUnknown ),
       
   179     iURI( NULL ),
       
   180     iParentId( NULL ),
       
   181     iTimedCounts( 0xf ),
       
   182     iCounters( 0xf ),
       
   183     iCountConstraintActive( EFalse ),
       
   184     iExpired( EFalse ),
       
   185     iCumulativeDelayTop( 0 ),
       
   186     iCumulativeDelayChild( 0 ),
       
   187     iTotalCumulativeTime( 0 ),
       
   188 	iUsingTimedCount( 0 )
       
   189     {
       
   190     }
       
   191 
       
   192 // ------------------------------------------------------------------------
       
   193 // CDRMConsume::ConstructL
       
   194 // ------------------------------------------------------------------------
       
   195 //
       
   196 void CDRMConsume::ConstructL( const TDesC8& aURI,
       
   197                               const TDesC8* aParentId )
       
   198     {
       
   199     DRMLOG( _L( "CDRMConsume::ConstructL" ) );
       
   200     CTimer::ConstructL();
       
   201     iURI = aURI.AllocL();
       
   202     if( aParentId )
       
   203         {
       
   204         iParentId = aParentId->AllocL();        
       
   205         }
       
   206     }
       
   207 
       
   208 // ------------------------------------------------------------------------
       
   209 // CDRMConsume::HandleL
       
   210 // ------------------------------------------------------------------------
       
   211 //
       
   212 void CDRMConsume::HandleL( ContentAccess::TIntent aIntent )
       
   213     {
       
   214     DRMLOG( _L( "CDRMConsume::HandleL" ) );
       
   215     
       
   216     TTime time;
       
   217     TBool secure( SECURETIME( time ) );
       
   218     InitializeL( aIntent, secure, time );
       
   219     
       
   220     DRMLOG( _L( "CDRMConsume::HandleL ok" ) );
       
   221     }
       
   222 
       
   223 // ------------------------------------------------------------------------
       
   224 // CDRMConsume:: Pause
       
   225 // ------------------------------------------------------------------------
       
   226 //
       
   227 void CDRMConsume::Pause()
       
   228     {
       
   229     DRMLOG( _L( "CDRMConsume::Pause" ) );
       
   230     
       
   231     Cancel();
       
   232     
       
   233     DRMLOG( _L( "CDRMConsume::Pause ok" ) );
       
   234     }
       
   235 
       
   236 // ------------------------------------------------------------------------
       
   237 // CDRMConsume:: ContinueL
       
   238 // ------------------------------------------------------------------------
       
   239 //
       
   240 void CDRMConsume::ContinueL()
       
   241     {
       
   242     DRMLOG( _L( "CDRMConsume::ContinueL" ) );
       
   243     
       
   244     Cancel();
       
   245     
       
   246     TInt error = KErrNone;
       
   247     TUint32 reason = 0;
       
   248                     
       
   249     CDRMPermission* child = NULL;
       
   250     HBufC8* parent = NULL;
       
   251     
       
   252     // This got removed by Pause()
       
   253     if( iCountConstraintActive )
       
   254         {
       
   255         SERVER->AddActiveCountConstraintL( *iURI );
       
   256         DoContinueL();
       
   257         return;
       
   258         }
       
   259     
       
   260     /* Fetch the RO again in order to manage a situation when another 
       
   261        instance has used the same RO (content has been consumed) while the
       
   262        other instance has not been used (player is paused) but will be 
       
   263        used again (content consumption is to be continued). */   
       
   264     error = iSession.FindRightsObject( iIntent, *iURI, child, parent, reason );
       
   265     
       
   266     /* Check if the RO found from the database matches with the one that was used
       
   267        in the original consumption of the content. If the ROs match, use the
       
   268        (possibly updated) one from the database. */
       
   269     if ( !error && child && iChild && 
       
   270          ( iChild->iUniqueID == child->iUniqueID ) &&
       
   271          ( iChild->iOriginalInsertTime == child->iOriginalInsertTime ) )
       
   272         {
       
   273         if ( iParentId )
       
   274             {
       
   275             delete iParentId; 
       
   276             iParentId = parent;
       
   277             }
       
   278             
       
   279         delete iChild;
       
   280         iChild = child;
       
   281         }      
       
   282     else 
       
   283         {
       
   284         /* The original RO was not found. Delete temporary objects and also
       
   285            delete the iChild and iCombined because the RO is no longer valid and
       
   286            need to be re-fetched using InitializeL method (in DoContinueL).   
       
   287            (in DoContinueL). */  
       
   288         if ( iChild )
       
   289             {
       
   290             delete iChild;
       
   291             iChild = NULL;
       
   292             }
       
   293         
       
   294         if ( iCombined )
       
   295             {
       
   296             delete iCombined;
       
   297             iCombined = NULL;
       
   298             }
       
   299         
       
   300         if ( child ) 
       
   301             {
       
   302             delete child;
       
   303             child = NULL;
       
   304             }
       
   305         if ( parent )
       
   306             {
       
   307             delete parent;
       
   308             parent = NULL;
       
   309             }
       
   310         } 
       
   311           
       
   312     DoContinueL();
       
   313     
       
   314     //HandleL( iIntent );
       
   315     
       
   316     DRMLOG( _L( "CDRMConsume::ContinueL ok" ) );
       
   317     }
       
   318 
       
   319 // ------------------------------------------------------------------------
       
   320 // CDRMConsume:: Stop
       
   321 // ------------------------------------------------------------------------
       
   322 //
       
   323 void CDRMConsume::Stop()
       
   324     {
       
   325     DRMLOG( _L( "CDRMConsume::Stop" ) );
       
   326     
       
   327     Cancel();
       
   328     
       
   329     DRMLOG( _L( "CDRMConsume::Stop ok" ) );
       
   330     }   
       
   331 
       
   332 // ------------------------------------------------------------------------
       
   333 // CDRMConsume:: ActivateL
       
   334 //
       
   335 // Calculate the smallest end time based on interval, end time, 
       
   336 // accumulated time & timed count.
       
   337 // ------------------------------------------------------------------------
       
   338 //
       
   339 void CDRMConsume::ActivateL( TBool aSecureTime,
       
   340                              const TTime& aTrustedTime  )
       
   341     {
       
   342     DRMLOG( _L( "CDRMConsume::ActivateL" ) );
       
   343     
       
   344     __ASSERT_DEBUG( iChild && iCombined, User::Invariant() );
       
   345     TTime endTime( Time::MaxTTime() );
       
   346     TTimeIntervalSeconds timed( KMaxTInt32 );
       
   347     TBool timeUsed( EFalse );
       
   348     TBool endTimeUsed( EFalse );
       
   349     
       
   350     iCurrentDelay = 0;
       
   351     
       
   352     if ( iCombined->iActiveConstraints & EConstraintTimedCounter )
       
   353         {
       
   354         // Take this, even if timed counts have been updated. 
       
   355         // This might cause unnecessary RunL's to be called, but it 
       
   356         // ensures both child & parent will be consumed when needed.
       
   357         // If e.g. it would be checked that iTimedCounts == 0xf, 
       
   358         // either one (child or parent) might not get updated in case of
       
   359         // "Child expired, but parent didn't -> find new child".        
       
   360         PickSmaller( timed, iCombined->iTimedInterval );
       
   361         timeUsed = ETrue;
       
   362         }
       
   363     
       
   364     if ( iCombined->iActiveConstraints & EConstraintAccumulated )
       
   365         {
       
   366         PickSmaller( timed, iCombined->iAccumulatedTime );
       
   367         timeUsed = ETrue;
       
   368         }
       
   369         
       
   370     if ( iCombined->iActiveConstraints & EConstraintInterval )
       
   371         {
       
   372         if ( iCombined->iIntervalStart != Time::NullTTime() )
       
   373             {
       
   374             endTime = iCombined->iIntervalStart;
       
   375             endTime += iCombined->iInterval;
       
   376             endTimeUsed = ETrue;
       
   377             }
       
   378         else
       
   379             {
       
   380             TInt64 tmp( iCombined->iInterval.Int() );
       
   381             
       
   382             PickSmaller( timed, tmp );
       
   383             timeUsed = ETrue;
       
   384             }
       
   385         }
       
   386      
       
   387     if ( iCombined->iActiveConstraints & EConstraintEndTime )
       
   388         {
       
   389         PickSmaller( endTime, iCombined->iEndTime );
       
   390         endTimeUsed = ETrue;
       
   391         }
       
   392         
       
   393     // Put the "smallest time" information to "endTime".
       
   394     if ( timeUsed )
       
   395         {
       
   396         TTime current( aTrustedTime );
       
   397         
       
   398         current += timed;
       
   399         
       
   400         PickSmaller( endTime, current );
       
   401         endTimeUsed = ETrue;
       
   402         }
       
   403         
       
   404      // Interval gets initialised immediately, and so do count constraints.
       
   405      // Timed/accumulated won't: those are consumed after the 
       
   406      // interval if secure time exists.
       
   407      Consume( ETrue, ETrue, EFalse, 0,
       
   408               aSecureTime,
       
   409               aTrustedTime );
       
   410     
       
   411      // In case something was modified, update the db also.
       
   412      UpdateDBL();
       
   413               
       
   414      if ( endTimeUsed )
       
   415         {     
       
   416         // Something exists.
       
   417         TTimeIntervalSeconds secs( 0 );
       
   418         TTime current( aTrustedTime );
       
   419         TInt err( KErrNone );
       
   420         
       
   421         // SecondsFrom returns an error if the difference is too great.
       
   422         err = endTime.SecondsFrom( current, secs );
       
   423         if ( err )
       
   424             {
       
   425             iCurrentDelay = KConsumeDefaultTimer;
       
   426             }
       
   427         else if ( secs.Int() < 0 )
       
   428             {
       
   429             iCurrentDelay = 0; // Already expired.
       
   430             }
       
   431         else if ( secs.Int() < KConsumeDefaultTimer )
       
   432             {
       
   433             iCurrentDelay = secs.Int();
       
   434             }
       
   435         else
       
   436             {
       
   437             iCurrentDelay = KConsumeDefaultTimer;
       
   438             }
       
   439            
       
   440         if ( !IsAdded() )
       
   441             {
       
   442             CActiveScheduler::Add( this );
       
   443             }
       
   444         
       
   445         DRMLOG2( _L( "CDRMConsume::ActivateL: using interval %d" ), 
       
   446                   ( TInt )iCurrentDelay );
       
   447                   
       
   448         // secs -> microsecs. The method sets the AO active.
       
   449         After( TTimeIntervalMicroSeconds32( iCurrentDelay * 1000000 ) );
       
   450         
       
   451         iTime = current;
       
   452       
       
   453         // If we see timed things here, we also have secure time.  
       
   454         //SETBIT( iMask, KConsumeHasSecureTime );
       
   455         }
       
   456     else    // For metering we always need to have this:
       
   457         {
       
   458         iCurrentDelay = KConsumeDefaultTimer;
       
   459         iTime = aTrustedTime;
       
   460  
       
   461         if ( !IsAdded() )
       
   462             {
       
   463             CActiveScheduler::Add( this );
       
   464             }
       
   465                 
       
   466         DRMLOG2( _L( "CDRMConsume::ActivateL: using interval %d" ), 
       
   467                   ( TInt )iCurrentDelay );
       
   468                   
       
   469         // secs -> microsecs. The method sets the AO active.
       
   470         After( TTimeIntervalMicroSeconds32( iCurrentDelay * 1000000 ) );              
       
   471         }    
       
   472         
       
   473     // If we see timed things here, we also have secure time.  
       
   474     if( aTrustedTime != Time::NullTTime())
       
   475         {
       
   476         SETBIT( iMask, KConsumeHasSecureTime );        
       
   477         }      
       
   478     DRMLOG( _L( "CDRMConsume::ActivateL ok" ) );
       
   479     }
       
   480 
       
   481 // ------------------------------------------------------------------------
       
   482 // CDRMConsume:: Consume
       
   483 //
       
   484 // Consume child & parent. Whether to consume parent's explicit
       
   485 // usage permission: the information is returned earlier by 
       
   486 // FindRightsObjectL.
       
   487 // ------------------------------------------------------------------------
       
   488 //
       
   489 void CDRMConsume::Consume( TBool aUpdateCounter,
       
   490                            TBool aInitInterval,
       
   491                            TBool aUpdateTimedCount,
       
   492                            const TTimeIntervalSeconds& aElapsedTime,
       
   493                            TBool aSecureTime,
       
   494                            const TTime& aTrustedTime )
       
   495     {
       
   496     DRMLOG( _L( "CDRMConsume::Consume" ) );
       
   497     
       
   498     __ASSERT_DEBUG( iChild && iCombined, User::Invariant() );
       
   499     
       
   500     // Decrease timed counters & regular counters only once.
       
   501     // In the beginning the bitmasks are both 0xF.     
       
   502     if ( iChild->TopLevelConstraint() )
       
   503         {
       
   504         if ( ConsumeConstraint( *( iChild->TopLevelConstraint() ),
       
   505                                 ( aUpdateCounter && 
       
   506                                   ISSET( iCounters, KChildToplevelCount ) ),
       
   507                                 aInitInterval,
       
   508                                 ( aUpdateTimedCount && 
       
   509                                   ISSET( iTimedCounts, 
       
   510                                          KChildToplevelCount ) ),
       
   511                                 aElapsedTime,
       
   512                                 aSecureTime, 
       
   513                                 aTrustedTime,
       
   514                                 iCumulativeDelayTop ) )
       
   515             {
       
   516             SETBIT( iMask, KConsumeChildModified );
       
   517             if ( aUpdateTimedCount )
       
   518                 {
       
   519                 CLRBIT( iTimedCounts, KChildToplevelCount );
       
   520                 }
       
   521             if ( aUpdateCounter )
       
   522                 {
       
   523                 CLRBIT( iCounters, KChildToplevelCount );
       
   524                 }
       
   525             }
       
   526         }
       
   527         
       
   528     if ( ConsumeConstraint( *( iChild->ConstraintForIntent( iIntent ) ),
       
   529                             ( aUpdateCounter && 
       
   530                               ISSET( iCounters, KChildPermCount ) ),
       
   531                             aInitInterval,
       
   532                             ( aUpdateTimedCount && 
       
   533                               ISSET( iTimedCounts, KChildPermCount ) ),
       
   534                             aElapsedTime,
       
   535                             aSecureTime,
       
   536                             aTrustedTime,
       
   537                             iCumulativeDelayChild ) )
       
   538         {
       
   539         SETBIT( iMask, KConsumeChildModified );
       
   540         if ( aUpdateTimedCount )
       
   541             {
       
   542             CLRBIT( iTimedCounts, KChildPermCount );
       
   543             }
       
   544         if ( aUpdateCounter )
       
   545             {
       
   546             CLRBIT( iCounters, KChildPermCount );
       
   547             }
       
   548         }
       
   549     
       
   550     DRMLOG( _L( "CDRMConsume::Consume ok" ) );
       
   551     }
       
   552 
       
   553 // ------------------------------------------------------------------------
       
   554 // CDRMConsume::RunL
       
   555 // ------------------------------------------------------------------------
       
   556 //
       
   557 void CDRMConsume::RunL()
       
   558     {
       
   559     DRMLOG2( _L( "CDRMConsume::RunL with %d" ), iStatus.Int() );
       
   560     
       
   561     switch ( iStatus.Int() )
       
   562         {
       
   563         case KErrNone:
       
   564             // Normal completition.
       
   565             
       
   566         case KErrUnderflow:
       
   567             // Time already passed.
       
   568             
       
   569         case KErrAbort:
       
   570             // System time changed ==> consume.
       
   571             DoContinueL();
       
   572             break;
       
   573 
       
   574         default:
       
   575             // Some other (real) error.
       
   576             // Handled in RunError.
       
   577             User::Leave( iStatus.Int() );
       
   578         };
       
   579         
       
   580     DRMLOG( _L( "CDRMConsume::RunL ok" ) );
       
   581     }
       
   582 
       
   583 // ------------------------------------------------------------------------
       
   584 // CDRMConsume::RunError
       
   585 // ------------------------------------------------------------------------
       
   586 //
       
   587 #if defined( _DEBUG ) || defined( _LOGGING )
       
   588 TInt CDRMConsume::RunError( TInt aError )
       
   589 #else
       
   590 TInt CDRMConsume::RunError( TInt /* aError */ )
       
   591 #endif
       
   592     {
       
   593     DRMLOG2( _L( "CDRMConsume::RunError: %d" ), aError );
       
   594      
       
   595     Deque();
       
   596     
       
   597     DRMLOG( _L( "CDRMConsume::RunError ok" ) );
       
   598     return 0;
       
   599     }
       
   600 
       
   601 // ------------------------------------------------------------------------
       
   602 // CDRMConsume::DoCancel
       
   603 // ------------------------------------------------------------------------
       
   604 //
       
   605 void CDRMConsume::DoCancel()
       
   606     {
       
   607     DRMLOG( _L( "CDRMConsume::DoCancel" ) );
       
   608     
       
   609     TInt error( KErrNone );
       
   610     TRAP( error, DoCancelL() );
       
   611     
       
   612     DRMLOG2( _L( "CDRMConsume::DoCancel: %d" ), error );
       
   613     }
       
   614 
       
   615 // ------------------------------------------------------------------------
       
   616 // CDRMConsume::DoCancelL
       
   617 // ------------------------------------------------------------------------
       
   618 //
       
   619 void CDRMConsume::DoCancelL()
       
   620     {
       
   621     DRMLOG( _L( "CDRMConsume::DoCancelL" ) );
       
   622     
       
   623     if ( iCurrentDelay )
       
   624         {
       
   625         TTimeIntervalSeconds secs;
       
   626         TTime trustedTime;
       
   627         TBool secureTime;
       
   628                         
       
   629         CTimer::DoCancel();
       
   630                 
       
   631         secureTime = SECURETIME( trustedTime );
       
   632         trustedTime.SecondsFrom( iTime, secs );
       
   633         
       
   634         #ifdef RD_DRM_METERING
       
   635             // Update total cumulative time for content metering purposes
       
   636             iTotalCumulativeTime = iTotalCumulativeTime.Int() + secs.Int();
       
   637         #endif
       
   638              
       
   639         // If the top level timed counter has not been activated yet
       
   640         // increment the counter
       
   641         if( ISSET( iTimedCounts, KChildToplevelCount ) )
       
   642             {
       
   643             iCumulativeDelayTop = iCumulativeDelayTop.Int() + secs.Int();            
       
   644             }
       
   645 
       
   646         // If the child timed counter has not been activated yet
       
   647         // increment the counter            
       
   648         if( ISSET( iTimedCounts, KChildPermCount ) )
       
   649             {
       
   650             iCumulativeDelayChild = iCumulativeDelayChild.Int() + secs.Int();
       
   651             }
       
   652             
       
   653         // Always >= 0.
       
   654         ConsumeTimedItemsL( secs,
       
   655                             secureTime, 
       
   656                             trustedTime );
       
   657         iCurrentDelay = 0;
       
   658         }
       
   659     
       
   660     UpdateDBL();
       
   661     
       
   662     if ( SERVER->HasActiveCountConstraint( *iURI ) )
       
   663         {
       
   664         SERVER->RemoveActiveCountConstraint( *iURI );
       
   665         }
       
   666     
       
   667     DRMLOG( _L( "CDRMConsume::DoCancel ok" ) );
       
   668     }
       
   669 
       
   670 // ------------------------------------------------------------------------
       
   671 // CDRMConsume::CombinePermissionsL
       
   672 //
       
   673 // Combine iChild's & iParent's top level constraints, and merge the usage
       
   674 // intention constraint (either one of them).
       
   675 // ------------------------------------------------------------------------
       
   676 //
       
   677 void CDRMConsume::CombinePermissionsL()
       
   678     {
       
   679     DRMLOG( _L( "CDRMConsume::CombinePermissions" ) );
       
   680     
       
   681     __ASSERT_DEBUG( iChild, User::Invariant() );
       
   682     
       
   683     // Reset
       
   684     delete iCombined; iCombined = NULL;
       
   685     iCombined = CDRMConstraint::NewL();
       
   686     
       
   687     if ( iChild->TopLevelConstraint() )
       
   688         {
       
   689         iCombined->Merge( *( iChild->TopLevelConstraint() ) );
       
   690         }
       
   691         
       
   692     iCombined->Merge( *( iChild->ConstraintForIntent( iIntent ) ) );
       
   693     
       
   694     DRMLOG( _L( "CDRMConsume::CombinePermissions ok" ) );
       
   695     }
       
   696 
       
   697 // ------------------------------------------------------------------------
       
   698 // CDRMConsume::UpdateDBL
       
   699 //
       
   700 // Update the DB if something was changed.
       
   701 // ------------------------------------------------------------------------
       
   702 //
       
   703 void CDRMConsume::UpdateDBL()
       
   704     {
       
   705     DRMLOG( _L( "CDRMConsume::UpdateDBL" ) );
       
   706     // __ASSERT_DEBUG( iChild, User::Invariant() );
       
   707         
       
   708     CDRMEventModify* event = NULL;
       
   709     TRequestStatus status;
       
   710         
       
   711     if( ISSET( iMask, KConsumeChildModified ) ) 
       
   712         {
       
   713         event = CDRMEventModify::NewL();
       
   714         }
       
   715     CleanupStack::PushL( event );    
       
   716 
       
   717     if ( ISSET( iMask, KConsumeChildModified ) )
       
   718         {
       
   719         DRMLOG( _L( "CDRMConsume: commiting child to DB" ) );
       
   720         
       
   721         if( iParentId )
       
   722             {
       
   723             DB.UpdateDBEntryL( *iParentId, *iChild );
       
   724             // Notify
       
   725             event->SetContentIDL(*iParentId);
       
   726             event->SetUniqueID(iChild->iUniqueID);
       
   727             }
       
   728         else
       
   729             {
       
   730             DB.UpdateDBEntryL( *iURI, *iChild );            
       
   731             // Notify
       
   732             event->SetContentIDL(*iURI);
       
   733             event->SetUniqueID(iChild->iUniqueID);
       
   734             }    
       
   735         
       
   736         // Notify
       
   737         event->SetContentIDL(*iURI);
       
   738         event->SetUniqueID(iChild->iUniqueID);
       
   739         
       
   740         NOTIFIER.SendEventL(*event,status);
       
   741         User::WaitForRequest(status);           
       
   742         
       
   743         CLRBIT( iMask, KConsumeChildModified );
       
   744         }
       
   745     
       
   746     CleanupStack::PopAndDestroy();    
       
   747     DRMLOG( _L( "CDRMConsume::UpdateDBL ok" ) );
       
   748     }
       
   749 // ------------------------------------------------------------------------
       
   750 // CDRMConsume::ConsumeConstraint
       
   751 //
       
   752 // Consume child & parent. Whether to consume parent's explicit
       
   753 // usage permission: the information is returned earlier by 
       
   754 // FindRightsObjectL.
       
   755 // ------------------------------------------------------------------------
       
   756 //
       
   757 TBool CDRMConsume::ConsumeConstraint( CDRMConstraint& aConstraint,
       
   758                                       TBool aUpdateCounter,
       
   759                                       TBool aInitInterval,
       
   760                                       TBool aUpdateTimedCount,
       
   761                                       const TTimeIntervalSeconds& aElapsedTime,
       
   762                                       TBool aSecureTime,
       
   763                                       const TTime& aTrustedTime,
       
   764                                       TTimeIntervalSeconds& aCumulativeTime )
       
   765     {
       
   766     DRMLOG( _L( "CDRMConsume::ConsumeConstraints" ) );
       
   767     
       
   768     TBool res( EFalse );
       
   769     
       
   770     if ( aUpdateCounter && 
       
   771          ( aConstraint.iActiveConstraints & EConstraintCounter ) )
       
   772         {
       
   773         --( aConstraint.iCounter);
       
   774         res = ETrue;
       
   775         iCountConstraintActive = ETrue;
       
   776         TRAP_IGNORE( SERVER->AddActiveCountConstraintL( *iURI ) ); 
       
   777         }
       
   778         
       
   779     if ( aInitInterval &&
       
   780          ( aConstraint.iActiveConstraints & EConstraintInterval ) &&
       
   781          aSecureTime &&
       
   782          aConstraint.iIntervalStart == Time::NullTTime() )
       
   783         {
       
   784         aConstraint.iIntervalStart = aTrustedTime;
       
   785         res = ETrue;
       
   786         }
       
   787         
       
   788     /* change to timed counter, we don't check the latest time, we check the
       
   789     cumulated time */    
       
   790         
       
   791     if ( aUpdateTimedCount &&
       
   792          ( aConstraint.iActiveConstraints & EConstraintTimedCounter &&
       
   793            ( aCumulativeTime >= aConstraint.iTimedInterval ||
       
   794              aElapsedTime >= aConstraint.iTimedInterval  ) ) )
       
   795         {
       
   796         --( aConstraint.iTimedCounter );
       
   797         res = ETrue;
       
   798         iCountConstraintActive = ETrue;
       
   799         TRAP_IGNORE( SERVER->AddActiveCountConstraintL( *iURI ) ); 
       
   800         aCumulativeTime = TTimeIntervalSeconds( 0 );
       
   801         }
       
   802     else if ( aUpdateTimedCount &&
       
   803          ( aConstraint.iActiveConstraints & EConstraintTimedCounter ) )
       
   804         {
       
   805         iUsingTimedCount = ETrue;
       
   806         }
       
   807         
       
   808     
       
   809     if ( aElapsedTime.Int() != 0 &&
       
   810          ( aConstraint.iActiveConstraints & EConstraintAccumulated ) )
       
   811         {
       
   812         __ASSERT_DEBUG( aElapsedTime.Int() > 0, User::Invariant() );
       
   813         
       
   814         if ( aConstraint.iAccumulatedTime < aElapsedTime )
       
   815             {
       
   816             aConstraint.iAccumulatedTime = 0;
       
   817             }
       
   818         else
       
   819             {
       
   820             aConstraint.iAccumulatedTime = aConstraint.iAccumulatedTime.Int() - 
       
   821             	aElapsedTime.Int();
       
   822             }
       
   823         
       
   824         res = ETrue;
       
   825         }
       
   826         
       
   827     if ( !iCountConstraintActive && aConstraint.Expired( aTrustedTime ) )
       
   828         {
       
   829         iExpired = ETrue;
       
   830         }
       
   831     else
       
   832         {
       
   833         iExpired = EFalse;
       
   834         }
       
   835     
       
   836     DRMLOG2( 
       
   837         _L( "CDRMConsume::ConsumeConstraints ok, returning %d" ), 
       
   838         ( TInt )res );
       
   839         
       
   840     return res;
       
   841     }
       
   842 
       
   843 // ------------------------------------------------------------------------
       
   844 // CDRMConsume::ConsumeTimedItemsL
       
   845 // ------------------------------------------------------------------------
       
   846 //
       
   847 void CDRMConsume::ConsumeTimedItemsL( TTimeIntervalSeconds aDelay,
       
   848                                       TBool aSecureTime,
       
   849                                       const TTime& aTrustedTime )
       
   850     {
       
   851     DRMLOG( _L( "CDRMConsume::ConsumeTimedItemsL" ) );
       
   852     
       
   853     // Update accumulated constraints & timed count.
       
   854     Consume( EFalse,
       
   855              ETrue,
       
   856              ETrue,
       
   857              aDelay,
       
   858              aSecureTime,
       
   859              aTrustedTime );
       
   860     
       
   861     DRMLOG( _L( "CDRMConsume::ConsumeTimedItems ok" ) );
       
   862     }
       
   863 
       
   864 // ------------------------------------------------------------------------
       
   865 // CDRMConsume::DoContinueL
       
   866 // ------------------------------------------------------------------------
       
   867 //
       
   868 void CDRMConsume::DoContinueL()
       
   869     {
       
   870     DRMLOG( _L( "CDRMConsume::DoContinueL" ) );
       
   871     
       
   872     TTime time;
       
   873     TBool secureTime( EFalse );
       
   874     secureTime = SECURETIME( time );
       
   875     
       
   876     if ( !iChild || !iCombined )
       
   877         {
       
   878         InitializeL( iIntent, secureTime, time );
       
   879         // User::Leave( KErrCANoRights );
       
   880         return;
       
   881         }
       
   882    
       
   883     ConsumeTimedItemsL( iCurrentDelay, secureTime, time );
       
   884     UpdateDBL();
       
   885     iCurrentDelay = 0;
       
   886     
       
   887     CombinePermissionsL();
       
   888     
       
   889     // If the content has expired, find new permissions, unless the expired
       
   890     // constraint was a timed count. In that case, the constraint
       
   891     // did not really expire.
       
   892     if ( SecurityLevelChanged( secureTime ) ||
       
   893          ( iCombined->Expired( time ) && 
       
   894            !SERVER->HasActiveCountConstraint( *iURI ) ) )
       
   895         {
       
   896         InitializeL( iIntent, secureTime, time );
       
   897         }
       
   898     else
       
   899         {
       
   900         ActivateL( secureTime, time );
       
   901         }
       
   902         
       
   903     DRMLOG( _L( "CDRMConsume::DoContinueL ok" ) );
       
   904     }
       
   905 
       
   906 // ------------------------------------------------------------------------
       
   907 // CDRMConsume::SecurityLevelChanged
       
   908 // ------------------------------------------------------------------------
       
   909 //
       
   910 TBool CDRMConsume::SecurityLevelChanged( TBool aSecureTime ) const
       
   911     {
       
   912     DRMLOG( _L( "CDRMConsume::SecurityLevelChanged" ) );
       
   913     
       
   914     if ( ( ISSET( iMask, KConsumeHasSecureTime ) && aSecureTime ) ||
       
   915          !( ISSET( iMask, KConsumeHasSecureTime ) || aSecureTime ) )
       
   916         {
       
   917         return EFalse;
       
   918         }
       
   919         
       
   920     return ETrue;
       
   921     }
       
   922 
       
   923 // ------------------------------------------------------------------------
       
   924 // CDRMConsume::InitializeL
       
   925 // ------------------------------------------------------------------------
       
   926 //
       
   927 void CDRMConsume::InitializeL( ContentAccess::TIntent aIntent,
       
   928                                TBool aSecureTime,
       
   929                                const TTime& aTrustedTime )
       
   930     {
       
   931     DRMLOG( _L( "CDRMConsume::InitializeL" ) );
       
   932     
       
   933     // aIntent is either EPlay, EView, EExecute or EPrint.
       
   934     // Store the old  consumption information in case this is 
       
   935     // called in case of "ran out of permissions, find new ones"
       
   936     CDRMPermission* child( iChild );
       
   937     TUint8 timedCounts( iTimedCounts );
       
   938     TUint8 counters( iCounters );
       
   939     TUint32 reason = 0;
       
   940     
       
   941     // Reset.
       
   942     iTimedCounts = 0xf;
       
   943     iCounters = 0xf;
       
   944     iMask = 0x0;
       
   945     iCurrentDelay = 0;
       
   946     
       
   947     iExpired = ETrue;
       
   948     
       
   949     delete iCombined; iCombined = NULL;
       
   950     
       
   951     // Previous child & parent need to be stored in case the internal 
       
   952     // counter states need to be restored.
       
   953     if ( child )
       
   954         {
       
   955         CleanupStack::PushL( child );
       
   956         iChild = NULL;
       
   957         }
       
   958     
       
   959     // If the next call won't leave, we have permissions.
       
   960     User::LeaveIfError( iSession.FindRightsObject( aIntent, 
       
   961                                                    *iURI,
       
   962                                                    iChild,
       
   963                                                    iParentId,
       
   964                                                    reason ) );
       
   965                                 
       
   966     User::LeaveIfError( iSession.VerifyCredentials( iURI, iChild, aIntent ) );
       
   967                                 
       
   968     iExpired = EFalse;
       
   969 
       
   970     // Check whether to restore the internal state.
       
   971     if ( iIntent == aIntent ) 
       
   972         {
       
   973         if ( child && 
       
   974              ( child->iUniqueID == iChild->iUniqueID ) )
       
   975             {
       
   976             DRMLOG( _L( "CDRMConsume: using the previous child" ) );
       
   977             
       
   978             CLRBIT( iTimedCounts, 
       
   979                     ( KChildToplevelCount | KChildPermCount ) & ~timedCounts );
       
   980                     
       
   981             CLRBIT( iCounters,
       
   982                     ( KChildToplevelCount | KChildPermCount ) & ~counters );
       
   983             }
       
   984         }
       
   985     
       
   986     if ( child )
       
   987         {
       
   988         CleanupStack::PopAndDestroy();
       
   989         }
       
   990     
       
   991     iIntent = aIntent;
       
   992     
       
   993     CombinePermissionsL();
       
   994     ActivateL( aSecureTime, aTrustedTime );
       
   995 
       
   996     DRMLOG( _L( "CDRMConsume::InitializeL ok" ) );
       
   997     }
       
   998     
       
   999 // ------------------------------------------------------------------------
       
  1000 // CDRMConsume::CountConstraintActive
       
  1001 // ------------------------------------------------------------------------
       
  1002 //
       
  1003 TBool CDRMConsume::CountConstraintActive()
       
  1004     {
       
  1005     return iCountConstraintActive || SERVER->HasActiveCountConstraint( *iURI );
       
  1006     }
       
  1007 
       
  1008 // ------------------------------------------------------------------------
       
  1009 // CDRMConsume::IsExpired
       
  1010 // ------------------------------------------------------------------------
       
  1011 //
       
  1012 TBool CDRMConsume::IsExpired()
       
  1013     {
       
  1014     return iExpired;
       
  1015     }
       
  1016 
       
  1017 // ------------------------------------------------------------------------
       
  1018 // CDRMConsume::GetChild
       
  1019 // ------------------------------------------------------------------------
       
  1020 //
       
  1021 CDRMPermission& CDRMConsume::GetChild()
       
  1022     {
       
  1023     return *iChild;
       
  1024     }
       
  1025 
       
  1026 #ifdef RD_DRM_METERING
       
  1027 
       
  1028 // ------------------------------------------------------------------------
       
  1029 // CDRMConsume::UpdateMeteringDbL
       
  1030 // ------------------------------------------------------------------------
       
  1031 //
       
  1032 void CDRMConsume::UpdateMeteringDbL()
       
  1033     {
       
  1034     DRMLOG( _L( "CDRMConsume::UpdateMeteringDbL" ) ); 
       
  1035     TTimeIntervalSeconds graceTime = 0;
       
  1036     CDRMConstraint* constraint = NULL;
       
  1037              
       
  1038     if ( iChild )
       
  1039         {
       
  1040         constraint = iChild->ConstraintForIntent( iIntent ); 
       
  1041         if ( constraint && constraint->iDrmMeteringInfo )  
       
  1042             { 
       
  1043             
       
  1044             graceTime = constraint->iDrmMeteringInfo->iGraceTime;
       
  1045             
       
  1046             // Grace time exceeded, increase metering count and metering 
       
  1047             // accumulated time    
       
  1048             if ( iTotalCumulativeTime >= graceTime )  
       
  1049                 {
       
  1050                 // Check Rights Issuer rights from Roap storage
       
  1051                 if ( !CheckRiContextRightsL( iChild->iRiId ) )    
       
  1052                     {
       
  1053                     return;
       
  1054                     }
       
  1055                 
       
  1056                 // Update database only if Rights Issuer Id is available
       
  1057                 if ( ( iChild->iRiId.Length() > 0 ) && 
       
  1058                      ( iChild->iRiId.Length() <= KRiIdSize ) ) 
       
  1059                     {
       
  1060                     
       
  1061                     if ( iURI )
       
  1062                         {
       
  1063                         CDrmMeteringDbData* meteringData = 
       
  1064                             CDrmMeteringDbData::NewLC();
       
  1065                         meteringData->iContentId = iURI->AllocL();
       
  1066                         meteringData->iRiId = iChild->iRiId; 
       
  1067                         if( iUsingTimedCount && !iCountConstraintActive ) 
       
  1068                             {
       
  1069                             meteringData->iCount = 0;                            
       
  1070                             }
       
  1071                         else
       
  1072                             {
       
  1073                         meteringData->iCount = 1;
       
  1074                             }
       
  1075                             
       
  1076                         meteringData->iAccumulatedTime = iTotalCumulativeTime;
       
  1077                         
       
  1078                         if ( iParentId ) 
       
  1079                             {
       
  1080                             meteringData->iParentUid = iParentId->AllocL();
       
  1081                             }
       
  1082                  
       
  1083                         METERINGDB.AddL( meteringData );
       
  1084                         CleanupStack::PopAndDestroy(); // meteringData
       
  1085                         }            
       
  1086                     }
       
  1087                 } 
       
  1088             }             
       
  1089         }
       
  1090         DRMLOG( _L( "CDRMConsume::UpdateMeteringDbL ok" ) );
       
  1091     }
       
  1092     
       
  1093 // ------------------------------------------------------------------------
       
  1094 // CDRMConsume::CheckRiContextRightsL
       
  1095 // ------------------------------------------------------------------------
       
  1096 //
       
  1097 
       
  1098 TBool CDRMConsume::CheckRiContextRightsL( const TDesC8& aRiId )
       
  1099     {
       
  1100     
       
  1101     DRMLOG( _L( "CDRMConsume::CheckRiContextRightsL" ) );
       
  1102     
       
  1103     CDRMRIContext* riContext = NULL;
       
  1104     TBool haveRights = EFalse; 
       
  1105   
       
  1106     // Connect to the storage of the registered Rights Issuers 
       
  1107     
       
  1108     if( iSession.ConnectRoapClient() == KErrNone )
       
  1109         {
       
  1110     riContext = iSession.RoapClient().GetRIContextL( aRiId );
       
  1111      
       
  1112     if ( riContext == NULL )
       
  1113         {
       
  1114         DRMLOG( _L ( "RI not registered" ) );
       
  1115         User::Leave( KErrRightsServerRiNotRegistered );
       
  1116         }
       
  1117         
       
  1118     // Check (via the Rights Issuer context) whether Rights Issuer 
       
  1119     // is allowed to use metering or not
       
  1120     if ( riContext->IsMeteringAllowed() )
       
  1121         {
       
  1122         haveRights = ETrue;
       
  1123         }
       
  1124         
       
  1125     if ( riContext ) 
       
  1126         {
       
  1127         delete riContext;
       
  1128         riContext = NULL;    
       
  1129         }
       
  1130         }
       
  1131         
       
  1132     return haveRights;
       
  1133     
       
  1134     }
       
  1135 
       
  1136 #endif
       
  1137 
       
  1138 // EOF