appfw/apparchitecture/apfile/apfmimecontentpolicy.cpp
changeset 0 2e3d3ce01487
child 12 7645e9ce10dc
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <apfmimecontentpolicy.h>
       
    20 #include <f32file.h> // RFs
       
    21 #include <barsread.h>
       
    22 #include <barsc.h> 
       
    23 #include <apfmimecontentpolicy.rsg>
       
    24 #include <caf/content.h>
       
    25 #include <e32base.h>
       
    26 #include <apgcli.h>    // For RApaLsSession 
       
    27 
       
    28 // Resource file name.
       
    29 _LIT(KCEResourceFile, "z:\\resource\\apps\\apfmimecontentpolicy.rsc"); 
       
    30 
       
    31 // This is needed for resource reading.
       
    32 const TInt KCCMask(0x00000fff);
       
    33 
       
    34 
       
    35 NONSHARABLE_CLASS(CApfMimeContentPolicyImpl) : public CBase
       
    36 	{
       
    37 public:	 // Constructors and destructor
       
    38 	static CApfMimeContentPolicyImpl* NewL();
       
    39 	static CApfMimeContentPolicyImpl* NewL(RFs& aFs);
       
    40 	~CApfMimeContentPolicyImpl();	
       
    41 
       
    42 public:  // New functions
       
    43 	TBool IsClosedType(const TDesC& aMimeType) const;
       
    44 	TBool IsClosedExtension(const TDesC& aFileExtension) const;
       
    45 	TBool IsClosedFileL(const TDesC& aFileName) const;
       
    46 	TBool IsDRMEnvelopeL(const TDesC& aFileName) const;	
       
    47 	TBool IsClosedFileL(RFile& aFileHandle) const;
       
    48 	TBool IsDRMEnvelopeL(RFile& aFileHandle) const;
       
    49 
       
    50 private:	
       
    51 	CApfMimeContentPolicyImpl();
       
    52 	void ConstructL();
       
    53     void ConstructL(RFs& aFs);
       
    54 	TBool IsClosedFileL(RFile& aFileHandle, const TDesC& aFileName) const;
       
    55 	void ReadResourcesL(RFs& aFs);
       
    56 
       
    57 private:
       
    58 	CDesCArrayFlat* iCcl;	// Closed content list.
       
    59 	CDesCArrayFlat* iExtList;	// Closed extensions list.
       
    60 	RApaLsSession   iLs;	// A session to the Application Architecture server.
       
    61     mutable RFs iFs;	//	File session
       
    62     TBool   iFsConnected;	// ETrue if connected to file server, else EFalse
       
    63 	};
       
    64 
       
    65 
       
    66 //
       
    67 // class CApfMimeContentPolicy
       
    68 //
       
    69 
       
    70 /**
       
    71 C++ default constructor.
       
    72 */
       
    73 CApfMimeContentPolicy::CApfMimeContentPolicy()
       
    74 	{
       
    75 	// Nothing to do here.
       
    76 	}
       
    77 
       
    78 /**
       
    79 By default Symbian 2nd phase constructor is private.
       
    80 */
       
    81 void CApfMimeContentPolicy::ConstructL()
       
    82 	{
       
    83 	iImpl = CApfMimeContentPolicyImpl::NewL();
       
    84 	}
       
    85 
       
    86 /**
       
    87 By default Symbian 2nd phase constructor is private.
       
    88 @param aFs  A handle to a shared file server session.
       
    89 */
       
    90 void CApfMimeContentPolicy::ConstructL(RFs& aFs)
       
    91 	{
       
    92 	iImpl = CApfMimeContentPolicyImpl::NewL(aFs);
       
    93 	}
       
    94 
       
    95 /**
       
    96 Creates a new Mime Content Policy object, and puts a pointer to it onto the cleanup stack.
       
    97 @return  The new Mime Content Policy object.
       
    98 */
       
    99 EXPORT_C CApfMimeContentPolicy* CApfMimeContentPolicy::NewLC()
       
   100 	{
       
   101 	CApfMimeContentPolicy* self = new (ELeave) CApfMimeContentPolicy();
       
   102 	CleanupStack::PushL(self);
       
   103 	self->ConstructL();
       
   104 	return self;
       
   105 	}
       
   106     
       
   107 /**
       
   108 Creates a new Mime Content Policy object, and puts a pointer to it onto the cleanup stack.
       
   109 @param aFs  A handle to a shared file server session. 
       
   110 @return  The new Mime Content Policy object.
       
   111 */
       
   112 EXPORT_C CApfMimeContentPolicy* CApfMimeContentPolicy::NewLC(RFs& aFs)
       
   113 	{
       
   114 	CApfMimeContentPolicy* self = new (ELeave) CApfMimeContentPolicy();
       
   115 	CleanupStack::PushL(self);
       
   116 	self->ConstructL(aFs);
       
   117 	return self;
       
   118 	}
       
   119 	
       
   120 /**
       
   121 Creates a new Mime Content Policy object.
       
   122 @return  The new Mime Content Policy object.
       
   123 */
       
   124 EXPORT_C CApfMimeContentPolicy* CApfMimeContentPolicy::NewL()
       
   125 	{
       
   126 	CApfMimeContentPolicy* self = NewLC();
       
   127 	CleanupStack::Pop();
       
   128 	return self;
       
   129 	}
       
   130 
       
   131 /**
       
   132 Creates a new Mime Content Policy object.
       
   133 @param aFs  A handle to a shared file server session.
       
   134 @return  	The new Mime Content Policy object.
       
   135 */
       
   136 EXPORT_C CApfMimeContentPolicy* CApfMimeContentPolicy::NewL(RFs& aFs)
       
   137 	{
       
   138 	CApfMimeContentPolicy* self = NewLC(aFs);
       
   139 	CleanupStack::Pop();
       
   140 	return self;
       
   141 	}
       
   142 
       
   143 
       
   144 /**
       
   145 Destructor.
       
   146 */
       
   147 EXPORT_C CApfMimeContentPolicy::~CApfMimeContentPolicy()
       
   148 	{
       
   149 	delete iImpl;
       
   150 	}
       
   151 
       
   152 /**
       
   153 Checks if given MIME type is included in closed content list.
       
   154 @param aMimeType  The mime type to be checked.   
       
   155 @return  ETrue if given MIME type is in closed content list else returns EFalse.
       
   156 */
       
   157 EXPORT_C TBool CApfMimeContentPolicy::IsClosedType(const TDesC& aMimeType)
       
   158 	{	
       
   159 	return iImpl->IsClosedType(aMimeType);
       
   160 	}
       
   161 
       
   162 /**
       
   163 Checks the extension of given file against list of closed file extensions.
       
   164 @param aFileExtension File extension to be checked.
       
   165 @return ETrue if extension of given file name is in closed extensions list else returns EFalse.
       
   166 */
       
   167 EXPORT_C TBool CApfMimeContentPolicy::IsClosedExtension(const TDesC& aFileExtension)
       
   168 	{
       
   169 	return iImpl->IsClosedExtension(aFileExtension);
       
   170 	}
       
   171 
       
   172 /**
       
   173 Checks if given file is Closed or not.
       
   174 This method checks for forward lock and superdistribution statuses of the file, in addition to IsClosedExtension 
       
   175 and IsClosedType checks.
       
   176 
       
   177 @param aFileName  A file to be checked   
       
   178 @return  ETrue if given file is closed else returns EFalse.
       
   179 @leave KErrNone, if successful; otherwise one of the other system-wide error codes
       
   180 */
       
   181 EXPORT_C TBool CApfMimeContentPolicy::IsClosedFileL(const TDesC& aFileName)
       
   182 	{
       
   183 	return iImpl->IsClosedFileL(aFileName);
       
   184 	}
       
   185 
       
   186 
       
   187 /**
       
   188 Checks if given file is a DRM envelope. Can leave if file handling fails.
       
   189 @param aFileName  A file to be checked. 
       
   190 @return ETrue if file is DRM envelope else returns EFalse.
       
   191 @leave	KErrCANotSupported if the requested attribute does not exist. 
       
   192 KErrPermissionDenied if the access to the protected content is not permitted 
       
   193 by the CAF Agent. Otherwise one of the other CAF error codes defined in 
       
   194 caferr.h or one of the other system-wide error codes for any other errors.  
       
   195 */
       
   196 EXPORT_C TBool CApfMimeContentPolicy::IsDRMEnvelopeL(const TDesC& aFileName)
       
   197 	{
       
   198 	return iImpl->IsDRMEnvelopeL(aFileName);
       
   199 	}
       
   200 
       
   201 /**
       
   202 Checks if given file is Closed or not. This method checks for forward lock and 
       
   203 superdistribution statuses of the file, in addition to IsClosedExtension and IsClosedType checks.
       
   204 Remember to make a file handle sharable. When a file handle is shared, the RFs handle has to be shared too. 
       
   205 @param aFileHandle  Handle to the file to be checked.  
       
   206 @return ETrue if given file is closed else returns EFalse.
       
   207 @leave KErrNone, if successful; KErrBadHandle if an invalid handle has been passed as a parameter.
       
   208 otherwise one of the other system-wide error codes
       
   209 */
       
   210 EXPORT_C TBool CApfMimeContentPolicy::IsClosedFileL(RFile& aFileHandle)
       
   211 	{
       
   212 	return iImpl->IsClosedFileL(aFileHandle);
       
   213 	}
       
   214 
       
   215 
       
   216 /**
       
   217 Checks if given file is a DRM envelope. Can leave if file handling fails.
       
   218 @param aFileHandle  Handle to the file to be checked. 
       
   219 @return ETrue if file is DRM envelope else returns EFalse.
       
   220 @leave	KErrCANotSupported if the requested attribute does not exist. 
       
   221 KErrPermissionDenied if the access to the protected content is not permitted 
       
   222 by the CAF Agent. Otherwise one of the other CAF error codes defined in 
       
   223 caferr.h or one of the other system-wide error codes for any other errors.  
       
   224 */
       
   225 EXPORT_C TBool CApfMimeContentPolicy::IsDRMEnvelopeL(RFile& aFileHandle)
       
   226 	{
       
   227 	return iImpl->IsDRMEnvelopeL(aFileHandle);
       
   228 	}
       
   229 
       
   230 //
       
   231 // class CApfMimeContentPolicyImpl
       
   232 //
       
   233 
       
   234 /**
       
   235 C++ default constructor.
       
   236 */
       
   237 CApfMimeContentPolicyImpl::CApfMimeContentPolicyImpl()
       
   238 	{
       
   239 	// C++ default constructor can NOT contain any code, that
       
   240 	}
       
   241 
       
   242 /**
       
   243 By default Symbian 2nd phase constructor is private.
       
   244 */
       
   245 void CApfMimeContentPolicyImpl::ConstructL()
       
   246 	{
       
   247 	// Resource reading is done without coe & eikon env.
       
   248 	User::LeaveIfError(iFs.Connect());
       
   249 	iFsConnected = ETrue;
       
   250 	
       
   251 	User::LeaveIfError(iFs.ShareProtected());
       
   252 	ReadResourcesL(iFs);
       
   253 	}
       
   254 	
       
   255 /**
       
   256 By default Symbian 2nd phase constructor is private.
       
   257 @param aFs A handle to a shared file server session. 
       
   258 */
       
   259 void CApfMimeContentPolicyImpl::ConstructL(RFs& aFs)
       
   260 	{
       
   261 	iFsConnected = EFalse;
       
   262 	iFs = aFs;	
       
   263 	ReadResourcesL(iFs);
       
   264 	}
       
   265 
       
   266 /**
       
   267 Two-phased constructor.
       
   268 */
       
   269 CApfMimeContentPolicyImpl* CApfMimeContentPolicyImpl::NewL()
       
   270 	{
       
   271 	CApfMimeContentPolicyImpl* self = new (ELeave) CApfMimeContentPolicyImpl();
       
   272 	CleanupStack::PushL(self);
       
   273 	self->ConstructL();
       
   274 	CleanupStack::Pop(self);
       
   275 	
       
   276 	return self;
       
   277 	}
       
   278 
       
   279 /**
       
   280 Two-phased constructor.
       
   281 */
       
   282 CApfMimeContentPolicyImpl* CApfMimeContentPolicyImpl::NewL(RFs& aFs)
       
   283 	{
       
   284 	CApfMimeContentPolicyImpl* self = new (ELeave) CApfMimeContentPolicyImpl();
       
   285 	CleanupStack::PushL(self);
       
   286 	self->ConstructL(aFs);
       
   287 	CleanupStack::Pop(self);
       
   288 
       
   289 	return self;
       
   290 	}
       
   291 	
       
   292 /**
       
   293 Destructor.
       
   294 */
       
   295 CApfMimeContentPolicyImpl::~CApfMimeContentPolicyImpl()
       
   296 	{
       
   297 	iLs.Close();
       
   298 	
       
   299 	if(iFsConnected)
       
   300         iFs.Close();
       
   301 
       
   302 	delete iCcl;
       
   303 	delete iExtList;
       
   304 	}
       
   305 
       
   306 /**
       
   307 Checks if given MIME type is included in closed content list.
       
   308 @param aMimeType  The mime type to be checked.
       
   309 @return ETrue if given MIME type is in closed content list else returns EFalse.
       
   310 */
       
   311 TBool CApfMimeContentPolicyImpl::IsClosedType(const TDesC& aMimeType) const 
       
   312 	{
       
   313 	TInt dummy = 0;
       
   314 	 
       
   315 	// Check if given descriptor is in closed content list.
       
   316 	// Find() returns 0 if found from array, non-zero if not.
       
   317 	const TBool found = (iCcl->FindIsq(aMimeType, dummy) == 0);
       
   318 	return found;
       
   319 	}
       
   320 
       
   321 /**
       
   322 Checks the extension of given file against list of closed file extensions.
       
   323 @param aFileExtension File extension to be checked.
       
   324 @return  ETrue  if extension of given file name is in closed extensions list else returns EFalse.
       
   325 */
       
   326 TBool CApfMimeContentPolicyImpl::IsClosedExtension(const TDesC& aFileExtension) const
       
   327 	{
       
   328 	TInt dummy = 0;
       
   329 
       
   330 	// Check if given descriptor is in closed content list.
       
   331 	// Find() returns 0 if found from array, non-zero if not.
       
   332 	const TBool found = (iExtList->FindIsq(aFileExtension, dummy) == 0);
       
   333 	return found;
       
   334 	}
       
   335 	
       
   336 /**
       
   337 Checks if given file is Closed or not. This method checks for forward lock and superdistribution statuses 
       
   338 of the file, in addition to IsClosedExtension and IsClosedType checks.
       
   339 @param aFileName  A file to be checked.
       
   340 @return ETrue if given file is closed else returns EFalse. 
       
   341 @leave KErrNone, if successful; otherwise one of the other system-wide error codes.
       
   342 */
       
   343 TBool CApfMimeContentPolicyImpl::IsClosedFileL(const TDesC& aFileName) const
       
   344 	{
       
   345 	RFile file;
       
   346     TInt protectionError;
       
   347 
       
   348     // open file to be checked for protection
       
   349     protectionError = file.Open(iFs, aFileName, EFileRead | EFileShareReadersOnly);
       
   350     
       
   351     // If the file is already opened using EFileShareAny (which is against best practices), we have to use it also.
       
   352     if (protectionError == KErrInUse) 
       
   353         protectionError = file.Open(iFs, aFileName, EFileRead | EFileShareAny);
       
   354     
       
   355     User::LeaveIfError(protectionError);
       
   356 
       
   357     CleanupClosePushL(file);
       
   358     const TBool retVal = IsClosedFileL(file, aFileName);
       
   359     CleanupStack::PopAndDestroy(); // file
       
   360     return retVal;
       
   361 	}
       
   362 	
       
   363 /**
       
   364 Checks if given file is Closed or not. This method checks for forward lock and superdistribution statuses 
       
   365 of the file, in addition to IsClosedExtension and IsClosedType checks.
       
   366 @param aFileHandle  Handle to the file to be checked.   
       
   367 @return ETrue if given file is closed else returns EFalse.
       
   368 @leave KErrNone, if successful; KErrBadHandle if an invalid handle has been passed as a parameter.
       
   369 otherwise one of the other system-wide error codes.
       
   370 */
       
   371 TBool CApfMimeContentPolicyImpl::IsClosedFileL(RFile& aFileHandle) const
       
   372 	{
       
   373 	if (aFileHandle.SubSessionHandle())
       
   374 	    {
       
   375 	    TFileName name;
       
   376 	    aFileHandle.Name(name);
       
   377         return IsClosedFileL(aFileHandle, name);
       
   378     	}
       
   379 
       
   380     User::Leave(KErrBadHandle);
       
   381     return EFalse;	// to keep compiler happy
       
   382 	}
       
   383 
       
   384 /**
       
   385 Checks if given file is Closed or not.
       
   386 @param aFileHandle  Handle to the file to be checked.
       
   387 @param aFileName  File to be checked.
       
   388 @return ETrue if given file is closed else returns EFalse.
       
   389 @leave KErrNone, if successful; KErrBadHandle if an invalid handle has been passed as a parameter.
       
   390 otherwise one of the other system-wide error codes.
       
   391 */
       
   392 TBool CApfMimeContentPolicyImpl::IsClosedFileL(RFile& aFileHandle, const TDesC& aFileName) const
       
   393 	{
       
   394     TInt value = 0; 
       
   395     // allocate a content object
       
   396     ContentAccess::CContent* content = ContentAccess::CContent::NewL(aFileHandle);
       
   397     
       
   398     // Check if file is protected
       
   399     // If the file type can not be determined just forget it.
       
   400     if (content->GetAttribute(ContentAccess::EIsProtected, value) == KErrNone)
       
   401         {
       
   402         if (value)
       
   403  		    {
       
   404         	// File is DRM protected
       
   405         	value = 0;
       
   406         	// Check if file is forwardable
       
   407         	if (content->GetAttribute(ContentAccess::EIsForwardable, value) == KErrNone)
       
   408         	    {
       
   409         	    delete content;
       
   410         	    if (!value)
       
   411                     {
       
   412                     // EIsProtected == ETrue &&  EIsForwardable == EFalse => EForwardLocked
       
   413                     // If forwardlocked, sending never allowed, so return without further ado.
       
   414 			        return ETrue;	
       
   415                     }
       
   416                 else
       
   417                     {
       
   418                     // EIsProtected == ETrue &&  EIsForwardable == ETrue => ESuperDistributable
       
   419                     // No need to check extension or mime type for files that are superdistributable,
       
   420 			        // they must not be blocked in any case.
       
   421 			        return EFalse;
       
   422                     }
       
   423         	    }
       
   424  		    }
       
   425         }
       
   426     
       
   427     delete content;
       
   428     content = NULL;
       
   429     
       
   430     // Check file extension.	
       
   431 	TParse parser;
       
   432 	parser.Set(aFileName, NULL, NULL);
       
   433 	if (IsClosedExtension(parser.Ext()))
       
   434 		return ETrue;
       
   435 
       
   436 	// Recognize and check MIME type.	
       
   437 	TDataType recData;
       
   438 	TUid uid;
       
   439 	const TInt err(iLs.AppForDocument(aFileHandle, uid, recData));
       
   440 	User::LeaveIfError(err);
       
   441 	    
       
   442 	return IsClosedType(recData.Des());   // Check MIME type.
       
   443 	}
       
   444 
       
   445 /**
       
   446 Checks if given file is a DRM envelope. Can leave if file handling fails.
       
   447 @param aFileName  A file to be checked.
       
   448 @return ETrue if file is DRM envelope else returns EFalse.
       
   449 @leave	KErrCANotSupported if the requested attribute does not exist.
       
   450 KErrPermissionDenied if the access to the protected content is not permitted 
       
   451 by the CAF Agent. Otherwise one of the other CAF error codes defined in 
       
   452 caferr.h or one of the other system-wide error codes for any other errors.
       
   453 */
       
   454 TBool CApfMimeContentPolicyImpl::IsDRMEnvelopeL(const TDesC& aFileName) const
       
   455 	{
       
   456     // allocate a content object
       
   457     ContentAccess::CContent* content = ContentAccess::CContent::NewLC(aFileName, ContentAccess::EContentShareReadOnly);
       
   458     
       
   459     TInt value = 0;
       
   460     User::LeaveIfError(content->GetAttribute(ContentAccess::EIsProtected, value));
       
   461     CleanupStack::PopAndDestroy(content);
       
   462     
       
   463     return (value != 0);    // File is DRM protected if value != 0
       
   464 	}
       
   465 	
       
   466 /**
       
   467 Checks if given file is a DRM envelope. Can leave if file handling fails.
       
   468 @param aFileHandle  Handle to the file to be checked.
       
   469 @return ETrue if file is DRM envelope else returns EFalse.
       
   470 @leave	KErrCANotSupported if the requested attribute does not exist. 
       
   471 KErrPermissionDenied if the access to the protected content is not permitted 
       
   472 by the CAF Agent. Otherwise one of the other CAF error codes defined in 
       
   473 caferr.h or one of the other system-wide error codes for any other errors. 
       
   474 */
       
   475 TBool CApfMimeContentPolicyImpl::IsDRMEnvelopeL(RFile& aFileHandle) const
       
   476 	{
       
   477     // allocate a content object
       
   478     ContentAccess::CContent* content = ContentAccess::CContent::NewLC(aFileHandle);
       
   479     
       
   480     TInt value = 0;
       
   481     User::LeaveIfError(content->GetAttribute(ContentAccess::EIsProtected, value));
       
   482     CleanupStack::PopAndDestroy(content);
       
   483        
       
   484     return (value != 0);    // File is DRM protected if value != 0
       
   485 	}
       
   486 
       
   487 	
       
   488 /**
       
   489 Reads closed content list and closed extensions list. Connects to RApaLsSession.
       
   490 Called by constructor.
       
   491 @param aFs A handle to a shared file server session. 
       
   492 */
       
   493 void CApfMimeContentPolicyImpl::ReadResourcesL(RFs& aFs)
       
   494 	{
       
   495 	TResourceReader reader;	
       
   496 
       
   497 	// Resource reading is done without coe & eikon env.
       
   498 	RResourceFile rsFile;
       
   499 	rsFile.OpenL(aFs, KCEResourceFile);
       
   500 	CleanupClosePushL(rsFile);
       
   501 
       
   502 	// Read closed content list.
       
   503 	// Remove offset from id
       
   504     HBufC8* rBuffer = rsFile.AllocReadLC(R_COMMONENG_CLOSED_CONTENT_LIST & KCCMask);
       
   505 	reader.SetBuffer(rBuffer);
       
   506 	ASSERT(!iCcl);
       
   507 	iCcl = reader.ReadDesCArrayL();
       
   508 	CleanupStack::PopAndDestroy(rBuffer); // rBuffer
       
   509 
       
   510 	// Read closed extensions list.
       
   511 	// Remove offset from id
       
   512     rBuffer = rsFile.AllocReadLC(R_COMMONENG_CLOSED_EXTENSIONS_LIST & KCCMask); 
       
   513 	reader.SetBuffer(rBuffer);
       
   514 	ASSERT(!iExtList);
       
   515 	iExtList = reader.ReadDesCArrayL();
       
   516 	CleanupStack::PopAndDestroy(2); // rBuffer, rsFile 
       
   517 	    
       
   518     // Sort lists to enable binary find
       
   519     iCcl->Sort();
       
   520     iExtList->Sort();
       
   521 
       
   522 	// Connect RApaLsSession and leave it open for the whole
       
   523 	// lifetime of the object.
       
   524 	User::LeaveIfError(iLs.Connect());
       
   525 	}
       
   526