commondrm/drmutility/src/DrmRightsInfoImpl.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  DRM Rights Information handling implementation class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <caf/caftypes.h>
       
    22 #include <oma2agent.h>
       
    23 #include <utf.h>
       
    24 #include <drmutilitytypes.h>
       
    25 #include <drmasyncobserver.h>
       
    26 
       
    27 #include "drmrightsinfoimpl.h"
       
    28 #include "drmrightsinfodata.h"
       
    29 #include "drmutilitycommon.h"
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt KCommandCheckRights( 1 );
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CDrmRightsInfoImpl::CDrmRightsInfoImpl
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 DRM::CDrmRightsInfoImpl::CDrmRightsInfoImpl(): CActive( EPriorityStandard )
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CDrmRightsInfoImpl::ConstructL
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void DRM::CDrmRightsInfoImpl::ConstructL()
       
    52     {
       
    53     iDrmUtilityCommon = DRM::CDrmUtilityCommon::NewL();
       
    54 
       
    55     User::LeaveIfError( iOmaClient.Connect() );
       
    56 
       
    57     User::LeaveIfError( iSemaphore.CreateLocal( 1 ) );
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CDrmRightsInfoImpl::NewL
       
    62 // Two-phased constructor.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C DRM::CDrmRightsInfoImpl* DRM::CDrmRightsInfoImpl::NewL()
       
    66     {
       
    67     DRM::CDrmRightsInfoImpl* self( NewLC() );
       
    68 
       
    69     CleanupStack::Pop( self );
       
    70 
       
    71     return self;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CDrmRightsInfoImpl::NewLC
       
    76 // Two-phased constructor.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 EXPORT_C DRM::CDrmRightsInfoImpl* DRM::CDrmRightsInfoImpl::NewLC()
       
    80     {
       
    81     DRM::CDrmRightsInfoImpl* self( new( ELeave ) CDrmRightsInfoImpl );
       
    82 
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85 
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 // Destructor
       
    91 DRM::CDrmRightsInfoImpl::~CDrmRightsInfoImpl()
       
    92     {
       
    93     DRM::CDrmRightsInfoData* data( PopFront() );
       
    94 
       
    95     // Empty the queue:
       
    96     while ( data )
       
    97         {
       
    98         // Complete the outstanding requestest with cancel
       
    99         data->iObserver->OperationCompleted( data->iOperationId, KErrCancel );
       
   100         delete data;
       
   101         data = PopFront();
       
   102         }
       
   103 
       
   104     delete iDrmUtilityCommon;
       
   105 
       
   106     iOmaClient.Close();
       
   107 
       
   108     // Close the semaphone
       
   109     iSemaphore.Close();
       
   110 
       
   111     // Remove the object from active scheduler etc.
       
   112     if ( IsAdded() )
       
   113         {
       
   114         Deque();
       
   115         }
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CDrmRightsInfo::CheckRightsL
       
   120 // Syncronous method
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 EXPORT_C void DRM::CDrmRightsInfoImpl::CheckRightsL(
       
   124     const TDesC& aUniqueId,
       
   125     ContentAccess::TIntent aIntent,
       
   126     DRM::TDrmRightsInfo& aDetails )
       
   127     {
       
   128     DRM::CDrmRightsInfoData* data( DRM::CDrmRightsInfoData::NewLC() );
       
   129 
       
   130     data->iUniqueId = aUniqueId.AllocL();
       
   131     TRAPD( err, data->iUniqueId8 =
       
   132                 CnvUtfConverter::ConvertFromUnicodeToUtf8L( aUniqueId ) );
       
   133     if ( err )
       
   134         {
       
   135         data->iUniqueId8 = NULL;
       
   136         }
       
   137     data->iIntent = aIntent;
       
   138     data->iDetails = &aDetails;
       
   139 
       
   140     PerformCheckRightsL( *data );
       
   141 
       
   142     CleanupStack::PopAndDestroy( data );
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CDrmRightsInfo::CheckRightsAsyncL
       
   147 // Asyncronous method
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 EXPORT_C TInt DRM::CDrmRightsInfoImpl::CheckRightsAsyncL(
       
   151     const TDesC& aUniqueId,
       
   152     ContentAccess::TIntent aIntent,
       
   153     DRM::TDrmRightsInfo& aDetails,
       
   154     DRM::MDrmAsyncObserver& aObserver )
       
   155     {
       
   156     // Create Queue object:
       
   157     DRM::CDrmRightsInfoData* data( DRM::CDrmRightsInfoData::NewLC() );
       
   158     TRequestStatus* status( &iStatus );
       
   159 
       
   160     data->iObserver = &aObserver;
       
   161     data->iOperationId = reinterpret_cast<TInt>( data );
       
   162     data->iOperation = KCommandCheckRights;
       
   163     data->iUniqueId = aUniqueId.AllocL();
       
   164     TRAPD( err, data->iUniqueId8 =
       
   165                 CnvUtfConverter::ConvertFromUnicodeToUtf8L( aUniqueId ) );
       
   166     if ( err )
       
   167         {
       
   168         data->iUniqueId8 = NULL;
       
   169         }
       
   170     data->iIntent = aIntent;
       
   171     data->iDetails = &aDetails;
       
   172 
       
   173     // Add it to the queue:
       
   174     AppendToQueue( data );
       
   175 
       
   176     // Pop it from the cleanup stack
       
   177     CleanupStack::Pop( data );
       
   178 
       
   179     if( !IsAdded() )
       
   180         {
       
   181         CActiveScheduler::Add( this );
       
   182         }
       
   183 
       
   184     if ( !IsActive() )
       
   185         {
       
   186         SetActive();
       
   187 
       
   188         // complete internal request:
       
   189         User::RequestComplete( status, KErrNone );
       
   190 
       
   191         }
       
   192     return data->iOperationId;
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CDrmRightsInfoImpl::CancelOperation
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 EXPORT_C TInt DRM::CDrmRightsInfoImpl::CancelOperation( TInt aOperationId )
       
   200     {
       
   201     // Critical area start:
       
   202     iSemaphore.Wait();
       
   203 
       
   204     TBool found( EFalse );
       
   205 
       
   206     DRM::CDrmRightsInfoData* previousData( NULL );
       
   207     DRM::CDrmRightsInfoData* currentData( iFirst );
       
   208 
       
   209     while ( currentData )
       
   210         {
       
   211         if ( currentData->iOperationId == aOperationId )
       
   212             {
       
   213             found = ETrue;
       
   214 
       
   215             //Update the previous link
       
   216             if ( previousData )
       
   217                 {
       
   218                 previousData->iNext = currentData->iNext;
       
   219                 }
       
   220             else
       
   221                 {
       
   222                 iFirst = currentData->iNext;
       
   223                 }
       
   224 
       
   225             //If the current is the only one, set iLast NULL
       
   226             if ( !iFirst )
       
   227                 {
       
   228                 iLast = NULL;
       
   229                 }
       
   230             //If only one left, set it also to be the iLast
       
   231             else if ( iFirst && !currentData->iNext )
       
   232                 {
       
   233                 iLast = iFirst;
       
   234                 }
       
   235 
       
   236             //Delete the cancelled operation
       
   237             currentData->iObserver->OperationCompleted( currentData->iOperationId, KErrCancel );
       
   238             delete currentData;
       
   239             currentData = NULL;
       
   240             }
       
   241         else
       
   242             {
       
   243             previousData = currentData;
       
   244             currentData = currentData->iNext;
       
   245             }
       
   246         }
       
   247 
       
   248     if( !found )
       
   249         {
       
   250         // Critical Area End
       
   251         iSemaphore.Signal();
       
   252         return KErrNotFound;
       
   253         }
       
   254     // Critical Area End
       
   255     iSemaphore.Signal();
       
   256     return KErrNone;
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CDrmRightsInfoImpl::DoCancel
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 void DRM::CDrmRightsInfoImpl::DoCancel()
       
   264     {
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CDrmRightsInfoImpl::RunL
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void DRM::CDrmRightsInfoImpl::RunL()
       
   272     {
       
   273     DRM::CDrmRightsInfoData* data( PopFront() );
       
   274     TRequestStatus* status( &iStatus );
       
   275 
       
   276     if( !data )
       
   277         {
       
   278         return;
       
   279         }
       
   280 
       
   281     CleanupStack::PushL( data );
       
   282 
       
   283     switch ( data->iOperation )
       
   284         {
       
   285         case KCommandCheckRights:
       
   286 
       
   287           // Take this into the "current" variable in case an error occurs
       
   288           iObserver = data->iObserver;
       
   289           iOperationId = data->iOperationId;
       
   290 
       
   291           // Perform the operation
       
   292           PerformCheckRightsL( *data );
       
   293 
       
   294           // Compelete the client request
       
   295           iObserver->OperationCompleted( iOperationId, KErrNone );
       
   296 
       
   297           break;
       
   298         default:
       
   299           // Do nothing
       
   300           break;
       
   301         }
       
   302 
       
   303     // destroy the object:
       
   304     CleanupStack::PopAndDestroy( data );
       
   305 
       
   306 
       
   307     // Get ready for another round:
       
   308     SetActive();
       
   309 
       
   310     // complete internal request:
       
   311     User::RequestComplete( status, KErrNone );
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CDrmRightsInfoImpl::RunError
       
   316 // If an error occurs complete the async request with it
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 TInt DRM::CDrmRightsInfoImpl::RunError( TInt aError )
       
   320     {
       
   321     iObserver->OperationCompleted( iOperationId, aError );
       
   322     iObserver = NULL;
       
   323     iOperationId = NULL;
       
   324     return KErrNone;
       
   325     }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CDrmRightsInfoImpl::PerformCheckRightsL
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 void DRM::CDrmRightsInfoImpl::PerformCheckRightsL(
       
   332     DRM::CDrmRightsInfoData& aData )
       
   333     {
       
   334     // Check for Oma Drm Rights, if not found, check for WmDrm Rights
       
   335     TInt omaerror( KErrNone );
       
   336     TInt wmerror( KErrNone );
       
   337     TUint32 reason( 0 );
       
   338 
       
   339     // if the outcome is KErrCANoRights we have the check both:
       
   340     omaerror = iOmaClient.CheckRights( aData.iIntent, *aData.iUniqueId8,  reason );
       
   341     if ( omaerror == KErrCANoRights )
       
   342         {
       
   343         *aData.iDetails = DRM::EURightsInfoMissing;
       
   344         // Check Also WmDrm
       
   345         // wmerror = check rights for WMDRM
       
   346         // Show info for wm drm error
       
   347         if ( !wmerror )
       
   348             {
       
   349 
       
   350             }
       
   351         }
       
   352     else
       
   353         {
       
   354         if ( !omaerror )
       
   355             {
       
   356             *aData.iDetails = DRM::EURightsInfoValid;
       
   357             }
       
   358         else
       
   359             {
       
   360             *aData.iDetails = iDrmUtilityCommon->GetOmaRightsStatusL(
       
   361                                                         aData.iUniqueId8,
       
   362                                                         aData.iIntent,
       
   363                                                         NULL );
       
   364             }
       
   365         }
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CDrmRightsInfoImpl::AppendToQueue
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 void DRM::CDrmRightsInfoImpl::AppendToQueue( DRM::CDrmRightsInfoData* aData )
       
   373     {
       
   374     // Critical area start:
       
   375     iSemaphore.Wait();
       
   376 
       
   377     if ( !iLast )
       
   378         {
       
   379         iFirst = aData;
       
   380         iLast = iFirst;
       
   381         }
       
   382     else
       
   383         {
       
   384         iLast->iNext = aData;
       
   385         iLast = aData;
       
   386         }
       
   387 
       
   388     // Critical area end
       
   389     iSemaphore.Signal();
       
   390     }
       
   391 
       
   392 // -----------------------------------------------------------------------------
       
   393 // CDrmRightsInfoImpl::PopFront
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 DRM::CDrmRightsInfoData* DRM::CDrmRightsInfoImpl::PopFront()
       
   397     {
       
   398     DRM::CDrmRightsInfoData* data = NULL;
       
   399 
       
   400     // If there is nothing in the queue return NULL
       
   401     if ( !iFirst )
       
   402         {
       
   403         return data;
       
   404         }
       
   405 
       
   406     // Critical area start:
       
   407     iSemaphore.Wait();
       
   408 
       
   409     data = iFirst;
       
   410 
       
   411     // Set the first and the next
       
   412     iFirst = iFirst->iNext;
       
   413 
       
   414     // reset the pointer on data
       
   415     data->iNext = NULL;
       
   416 
       
   417     // if there is no next in iFirst, this is the last or if there is no iFirst
       
   418     if ( !iFirst || !iFirst->iNext )
       
   419         {
       
   420         iLast = iFirst;
       
   421         }
       
   422 
       
   423     // Critical Area End
       
   424     iSemaphore.Signal();
       
   425 
       
   426     return data;
       
   427     }
       
   428 
       
   429 //  End of File