phonebookui/Phonebook/View/src/CPbkDrmManager.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *           Phonebook DRM policy managing class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <CPbkDrmManager.h>
       
    22 #include <PbkConfig.hrh>
       
    23 
       
    24 #include <featmgr.h>
       
    25 #include <DRMCommon.h>
       
    26 #include <bldvariant.hrh>
       
    27 #include <PbkView.rsg>
       
    28 #include <eikenv.h>
       
    29 #include <StringLoader.h>
       
    30 #include <aknnotewrappers.h>
       
    31 #include <apgcli.h>                     // RApaLsSession
       
    32 #include <MusicPlayerInternalCRKeys.h>  // KCRUidMusicPlayerFeatures
       
    33 #include <centralrepository.h>          // CRepository
       
    34 #include <DRMHelper.h>                  // CDRMHelper
       
    35 
       
    36 namespace {
       
    37     
       
    38 // CONSTANTS
       
    39 
       
    40 // Blocked mimetypes when DRM is enforced. Keep the last space in list.
       
    41 // List comes from 107-14385: VFKK: Updates to non-DRM audio blocking requirements
       
    42 _LIT( KBlockedMimeTypes, "audio/amr-wb audio/mp4 audio/3gpp audio/3gpp2 audio/aac audio/mpeg audio/x-wav audio/wav audio/x-ms-wma ");
       
    43 
       
    44 } // namespace
       
    45 
       
    46 // ================= MEMBER FUNCTIONS =======================
       
    47 
       
    48 inline CPbkDrmManager::CPbkDrmManager() :
       
    49     iDrmEnabled(EFalse)
       
    50     {
       
    51     }
       
    52 
       
    53 inline void CPbkDrmManager::ConstructL()
       
    54     {
       
    55     // Sets up TLS, must be done before FeatureManager is used.
       
    56     FeatureManager::InitializeLibL();
       
    57 
       
    58     iDrmHelper = CDRMHelper::NewL();
       
    59     iDrmClient = DRMCommon::NewL();
       
    60     if (iDrmClient->Connect() == DRMCommon::EOk)
       
    61         {
       
    62         // if unable to connect, DRM protection is always on
       
    63         iDrmEnabled = ETrue;
       
    64         }
       
    65     }
       
    66 
       
    67 EXPORT_C CPbkDrmManager* CPbkDrmManager::NewL()
       
    68     {
       
    69     CPbkDrmManager* self = new(ELeave) CPbkDrmManager();
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL();
       
    72     CleanupStack::Pop(self);
       
    73     return self;
       
    74     }
       
    75 
       
    76 EXPORT_C CPbkDrmManager::~CPbkDrmManager()
       
    77     {
       
    78     if (iDrmClient)
       
    79         {
       
    80         iDrmClient->Disconnect();
       
    81         }
       
    82     delete iDrmClient;
       
    83 
       
    84     delete iDrmHelper;
       
    85     iDrmHelper = NULL;
       
    86 
       
    87     FeatureManager::UnInitializeLib();
       
    88     }
       
    89 
       
    90 EXPORT_C TBool CPbkDrmManager::IsProtectedFile
       
    91         (const TDesC& aFileName)
       
    92     {
       
    93     TBool result( ETrue );
       
    94     const TBool drmSupported =
       
    95         (FeatureManager::FeatureSupported(KFeatureIdDrm)
       
    96         || FeatureManager::FeatureSupported(KFeatureIdDrmFull));
       
    97 
       
    98     if (!drmSupported)
       
    99         {
       
   100         // No DRM support, file retrieval always ok
       
   101         result = EFalse;
       
   102         }
       
   103     else if (iDrmEnabled && drmSupported)
       
   104         {
       
   105         TBool protection( ETrue );
       
   106         // Check is the file protected
       
   107         TInt drmErrorCode = iDrmClient->IsProtectedFile(aFileName, protection);
       
   108 
       
   109         switch (drmErrorCode)
       
   110             {
       
   111             case KErrInUse:
       
   112                 {
       
   113                 // File is in use. Assume that file is protected.
       
   114                 // Otherwise there is a threat that protected file 
       
   115                 // is handled as unprotected.
       
   116                 result = ETrue;                                
       
   117                 break;
       
   118                 }
       
   119 
       
   120             case DRMCommon::EOk:    // FALLTHROUGH
       
   121             default:
       
   122                 {
       
   123                 result = protection;
       
   124                 break;
       
   125                 }
       
   126             }
       
   127         }
       
   128     return result;
       
   129     }
       
   130 
       
   131 EXPORT_C TBool CPbkDrmManager::IsRingingToneProtectedL( const TDesC& aFileName )
       
   132     {
       
   133     TInt result( KErrGeneral );
       
   134     const TBool drmSupported =
       
   135         (FeatureManager::FeatureSupported(KFeatureIdDrm)
       
   136         || FeatureManager::FeatureSupported(KFeatureIdDrmFull));
       
   137 
       
   138     if (!drmSupported)
       
   139         {
       
   140         // No DRM support, file retrieval always ok
       
   141         result = KErrNone;
       
   142         }
       
   143     else
       
   144         {
       
   145         // DRM is enabled. File must be checked
       
   146         if (IsProtectedFile(aFileName))
       
   147             {
       
   148             result = CheckProtectedFileL(aFileName);
       
   149             }
       
   150         else
       
   151             {
       
   152             result = CheckUnprotectedFileL(aFileName);
       
   153             }
       
   154         }
       
   155     if (result == KErrNone)
       
   156         {
       
   157         return EFalse;
       
   158         }
       
   159     else
       
   160         {
       
   161         return ETrue;
       
   162         }
       
   163     }
       
   164 
       
   165 void CPbkDrmManager::ShowErrorNoteL( TInt aResource )
       
   166     {
       
   167     CEikonEnv* env = CEikonEnv::Static();
       
   168     HBufC* noteText = StringLoader::LoadLC( aResource, env );
       
   169 
       
   170     CAknErrorNote* note =
       
   171         new(ELeave)CAknErrorNote( R_AKN_INFORMATION_NOTE_WAIT );
       
   172     note->ExecuteLD( *noteText );
       
   173 
       
   174     CleanupStack::PopAndDestroy( noteText ); //noteText
       
   175     }
       
   176 
       
   177 
       
   178 EXPORT_C TBool CPbkDrmManager::IsThumbnailProtectedL( const TDesC& aFileName )
       
   179     {
       
   180     // Thumbnails with any DRM protection are not allowed
       
   181     if (IsProtectedFile( aFileName ) )
       
   182         {
       
   183         ShowErrorNoteL( R_QTN_DRM_NOT_ALLOWED );
       
   184         return ETrue;
       
   185         }
       
   186     else
       
   187         {
       
   188         return EFalse;
       
   189         }
       
   190     }
       
   191 
       
   192 
       
   193 EXPORT_C TBool CPbkDrmManager::HasFileRestrictedRights( const TDesC& aFileName )
       
   194     {
       
   195     TInt result( KErrGeneral );
       
   196     DRMCommon::TContentProtection protection;
       
   197     HBufC8* mimeType = NULL; // ignored
       
   198     HBufC8* contentUri = NULL;
       
   199     TUint dataLength; // ignored
       
   200 
       
   201     const TBool drmSupported =
       
   202         (FeatureManager::FeatureSupported(KFeatureIdDrm)
       
   203         || FeatureManager::FeatureSupported(KFeatureIdDrmFull));
       
   204 
       
   205     if (!drmSupported)
       
   206         {
       
   207         // No DRM support, file retrieval always ok
       
   208         result = KErrNone;
       
   209         }
       
   210     else
       
   211         {
       
   212         // DRM is enabled. File must be checked
       
   213         TInt err = iDrmClient->GetFileInfo( aFileName, protection, mimeType,
       
   214                                             contentUri, dataLength );
       
   215         delete mimeType;
       
   216         if( err )
       
   217             {
       
   218             // Some error occured. Treat the file as protected
       
   219             result = err;
       
   220             }
       
   221         else if ( protection == DRMCommon::ENoDCFFile )
       
   222             {
       
   223             result = KErrNone;
       
   224             }
       
   225         else
       
   226             {
       
   227             CDRMRights* rights = NULL;
       
   228             err = iDrmClient->GetActiveRights( *contentUri, DRMCommon::EPlay, rights );
       
   229 
       
   230             if ( err == CDRMRights::EFullRights )
       
   231                 {
       
   232                 result = KErrNone;
       
   233                 }
       
   234 
       
   235             if ( rights )
       
   236                 {
       
   237                 delete rights;
       
   238                 }
       
   239             }
       
   240         }
       
   241 
       
   242     if ( contentUri )
       
   243         {
       
   244         delete contentUri;
       
   245         }
       
   246 
       
   247     if ( result == KErrNone )
       
   248         {
       
   249         return EFalse;
       
   250         }
       
   251     else
       
   252         {
       
   253         return ETrue;
       
   254         }
       
   255     }
       
   256 
       
   257 
       
   258 TBool CPbkDrmManager::IsDrmRequiredForPlaybackL()
       
   259     {
       
   260     // It is possible to disable support for non-DRM protected rich audio
       
   261     // formats.
       
   262     CRepository* musicPlayerFeatures =
       
   263         CRepository::NewLC(KCRUidMusicPlayerFeatures);
       
   264     TInt value=0;
       
   265     User::LeaveIfError(
       
   266         musicPlayerFeatures->Get(KRequireDRMInPlayback, value));
       
   267     CleanupStack::PopAndDestroy(musicPlayerFeatures);
       
   268     return static_cast<TBool>(value);
       
   269     }
       
   270 
       
   271 
       
   272  TBool CPbkDrmManager::IsBlockedMimeTypeL( const TDesC& aMimeType)
       
   273     {
       
   274     // Add space to aMimeType for searching (FindF() would find "audio/3gpp" in
       
   275     // "audio/3gpp2" without the added space).
       
   276     TBool isBlocked(EFalse);
       
   277     if( aMimeType.Length() > 0 )
       
   278         {
       
   279         HBufC* mimeBuf = HBufC::NewLC(aMimeType.Length() + 1);
       
   280         mimeBuf->Des().Copy(aMimeType);
       
   281         mimeBuf->Des().Append(' ');
       
   282         isBlocked = (KBlockedMimeTypes().FindF(*mimeBuf) != KErrNotFound);
       
   283         CleanupStack::PopAndDestroy(mimeBuf);
       
   284         }
       
   285     return isBlocked;
       
   286     }
       
   287 
       
   288 
       
   289 TInt CPbkDrmManager::CheckProtectedFileL( const TDesC& aFileName )
       
   290     {
       
   291     TBool automatedOk(EFalse);
       
   292     
       
   293     // Check that file can be set as automated content.
       
   294     TInt err = iDrmHelper->CanSetAutomated(aFileName,
       
   295             automatedOk);        
       
   296     if (err==KErrNone && !automatedOk)
       
   297         {
       
   298         ShowErrorNoteL(R_QTN_PHOB_DRM_PREV_RIGHTS_SET);     
       
   299         return KErrGeneral;        
       
   300         }                        
       
   301     if (err!=KErrNone)
       
   302         {
       
   303         // Locked file case is handled separately.
       
   304         if (err==KErrInUse)
       
   305             {
       
   306             CCoeEnv::Static()->HandleError(KErrInUse);
       
   307             }            
       
   308         else
       
   309             {
       
   310             iDrmHelper->HandleErrorL(err,aFileName);
       
   311             }            
       
   312         return KErrGeneral;
       
   313         }
       
   314     
       
   315     DRMCommon::TContentProtection protection;
       
   316     HBufC8* mimeType = NULL; // ignored
       
   317     HBufC8* contentUri = NULL;
       
   318     TUint dataLength; // ignored
       
   319     err = iDrmClient->GetFileInfo( aFileName, protection, mimeType,
       
   320                                         contentUri, dataLength );
       
   321     delete mimeType;
       
   322     if( err )
       
   323         {
       
   324         delete contentUri;
       
   325         User::Leave( err );
       
   326         }
       
   327 
       
   328     TInt result( KErrGeneral );
       
   329     CDRMRights* rights = NULL;
       
   330     err = iDrmClient->GetActiveRights( *contentUri, DRMCommon::EPlay, rights );
       
   331 
       
   332     switch ( err )
       
   333         {
       
   334         case CDRMRights::EFullRights: // Fall through
       
   335         case CDRMRights::ERestrictedRights:
       
   336             {
       
   337             CDRMRights::TRestriction restriction; // ignore
       
   338             CDRMRights::TExpiration expiration;
       
   339             TUint32 constType;
       
   340 
       
   341             rights->GetRightsInfo( DRMCommon::EPlay, restriction, // ignore return
       
   342                                expiration, constType );       // value
       
   343 
       
   344 
       
   345             // This is CFM protected file, ringingtone is set to "no"
       
   346             if ( ( rights->GetPermission().iInfoBits & ENoRingingTone )
       
   347                 // This is normal DRM protected tone
       
   348                 || ( constType & ( CDRMRights::ECountBased |
       
   349                                    CDRMRights::ETimeIsAccumulatedTime ) ) )
       
   350                 {
       
   351                 ShowErrorNoteL( R_QTN_PHOB_DRM_PREV_RIGHTS_SET );
       
   352                 }
       
   353             else
       
   354                 {
       
   355                 switch ( expiration )
       
   356                     {
       
   357                     case CDRMRights::EValidRights:
       
   358                         {
       
   359                         result=KErrNone;
       
   360                         break;
       
   361                         }
       
   362 
       
   363                     case CDRMRights::EFutureRights:
       
   364                         {
       
   365                         iDrmHelper->HandleErrorL( DRMCommon::ENoRights, aFileName );
       
   366                         break;
       
   367                         }
       
   368 
       
   369                     case CDRMRights::EExpiredRights:
       
   370                         {
       
   371                         iDrmHelper->HandleErrorL( DRMCommon::ERightsExpired, aFileName );
       
   372                         break;
       
   373                         }
       
   374 
       
   375                     default:
       
   376                         {
       
   377                         break;
       
   378                         }
       
   379                     }
       
   380                 }
       
   381             break;
       
   382             }
       
   383 
       
   384         case CDRMRights::EPreviewRights:
       
   385             {
       
   386             ShowErrorNoteL( R_QTN_PHOB_DRM_PREV_RIGHTS_SET );
       
   387             break;
       
   388             }
       
   389 
       
   390         case DRMCommon::ENoRights:
       
   391             {
       
   392             iDrmHelper->HandleErrorL( DRMCommon::ENoRights, aFileName );
       
   393             break;
       
   394             }
       
   395 
       
   396         default:
       
   397             {
       
   398             break;
       
   399             }
       
   400 
       
   401         }
       
   402 
       
   403     delete rights;
       
   404     delete contentUri;
       
   405     return result;
       
   406     }
       
   407 
       
   408 
       
   409 TInt CPbkDrmManager::CheckUnprotectedFileL(const TDesC& aFileName)
       
   410     {
       
   411     TInt result = KErrNone;
       
   412     RApaLsSession apaLs;
       
   413     User::LeaveIfError( apaLs.Connect() );
       
   414     TUid appUidNotUsed = KNullUid;
       
   415     TDataType dataType;
       
   416     TInt err = apaLs.AppForDocument( aFileName, appUidNotUsed, dataType );
       
   417     apaLs.Close();
       
   418     User::LeaveIfError( err );
       
   419     // Check if this unprotected MIME type should be blocked
       
   420     if (IsDrmRequiredForPlaybackL() &&
       
   421         IsBlockedMimeTypeL(dataType.Des()))
       
   422         {
       
   423         result = KErrPermissionDenied;
       
   424         ShowErrorNoteL( R_QTN_PHOB_UNPROTECTED_TONE );
       
   425         }
       
   426     return result;
       
   427     }
       
   428 
       
   429 //  End of File