messagingfw/wappushfw/ROAPTContentHandler/src/ROAPTContentHandler.cpp
changeset 62 db3f5fa34ec7
parent 0 8e480a14352b
equal deleted inserted replaced
60:9f5ae1728557 62:db3f5fa34ec7
       
     1 // Copyright (c) 2006-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 // Local includes
       
    17 //
       
    18 #include "ROAPTContentHandler.h"
       
    19 #include "MessageComposer.h"
       
    20 
       
    21 //Rights object text MIME types
       
    22 _LIT8(KROAPTTextContentType, "application/vnd.oma.drm.roap-trigger+xml");
       
    23 _LIT(KROAPTBinContentType, "application/vnd.oma.drm.roap-trigger+wbxml");
       
    24 
       
    25 #if defined(_DEBUG)
       
    26 _LIT(KErrPushMsgNull,	"NULL CPushMessage");
       
    27 #endif
       
    28 
       
    29 //temp output file
       
    30 _LIT(KWapPushDir, "C:\\system\\data\\wappush\\");
       
    31 _LIT(KTempROAPTFile, "roaptrigger.Dat");
       
    32 
       
    33 // Constants
       
    34 _LIT(KReserved, "Reserved");
       
    35 
       
    36 const TInt KXMLHeader=12;
       
    37 
       
    38 void CROAPTContentHandler::CPushHandlerBase_Reserved1()
       
    39 	{
       
    40 	User::Panic(KReserved, KErrNotSupported);
       
    41 	}
       
    42 
       
    43 void CROAPTContentHandler::CPushHandlerBase_Reserved2()
       
    44 	{
       
    45 	User::Panic(KReserved, KErrNotSupported);
       
    46 	}
       
    47 
       
    48 /**
       
    49  * The ROAP Trigger Content handler private constructor.
       
    50  */ 
       
    51 CROAPTContentHandler::CROAPTContentHandler(): CContentHandlerBase(), iPushMsgAction(KErrNotFound)
       
    52 	{
       
    53 	//Do nothing
       
    54 	}
       
    55 
       
    56 /**
       
    57  *  This will complete initialization of the object
       
    58  */
       
    59 void CROAPTContentHandler::ConstructL()
       
    60 	{
       
    61 	CActiveScheduler::Add(this);
       
    62 	User::LeaveIfError(iFs.Connect());		
       
    63 	}
       
    64 
       
    65 /**
       
    66  * Static Factory Construction
       
    67  *
       
    68  * Version of NewL which leaves nothing
       
    69  * on the cleanup stack
       
    70  */
       
    71 CROAPTContentHandler* CROAPTContentHandler::NewL()
       
    72 	{
       
    73 	CROAPTContentHandler* self = new(ELeave) CROAPTContentHandler;
       
    74 	CleanupStack::PushL(self);
       
    75 	self->ConstructL();
       
    76 	CleanupStack::Pop(self);
       
    77 	return self;
       
    78 	}
       
    79 
       
    80 /**
       
    81  * Default d'tor
       
    82  */
       
    83 CROAPTContentHandler::~CROAPTContentHandler()
       
    84 	{
       
    85 	__LOG_PTR_DEBUG("CROAPTContentHandler:: Destructor Called"); 
       
    86 	delete iCAFInterface;
       
    87 	delete iData;
       
    88 	}
       
    89 
       
    90 /**
       
    91  * HandleMessage Async. Version
       
    92  *	Takes ownership of Push Message and sets self active to continue
       
    93  *	processing message.
       
    94  *	@param aPushMsg
       
    95  *		CPushMessage to process
       
    96  *	@param aStatus
       
    97  *		request status variable for use in asynchronous operations
       
    98  */
       
    99 void CROAPTContentHandler::HandleMessageL(CPushMessage* aPushMsg, TRequestStatus& aStatus)
       
   100 	{
       
   101 	__LOG_PTR_DEBUG("CROAPTContentHandler:: HandleMessage Async Func. Called"); 
       
   102 	__ASSERT_DEBUG( aPushMsg != NULL, User::Panic(KErrPushMsgNull, KErrNone));
       
   103 
       
   104 	iMessage = aPushMsg;
       
   105 	SetConfirmationStatus(aStatus);
       
   106 
       
   107 	iState= EConverting;
       
   108 	IdleComplete();
       
   109 	}
       
   110 
       
   111 /**
       
   112  * HandleMessage Sync. Version
       
   113  *	Takes ownership of Push Message and sets self active to continue
       
   114  *	processing message.
       
   115  *
       
   116  *	Initial State: Set data members then go to the next state 
       
   117  *	@param aPushMsg
       
   118  *		CPushMessage to process
       
   119  */
       
   120 void CROAPTContentHandler::HandleMessageL(CPushMessage* aPushMsg)
       
   121 	{
       
   122 	__LOG_PTR_DEBUG("CROAPTContentHandler:: HandleMessage Sync Func. Called");
       
   123 	__ASSERT_DEBUG( aPushMsg != NULL, User::Panic(KErrPushMsgNull, KErrNone));
       
   124 	
       
   125 	iMessage = aPushMsg;
       
   126 
       
   127 	iState =EConverting;
       
   128 	IdleComplete();
       
   129 	}
       
   130 
       
   131 /** 
       
   132  *	Convert the ROAP Trigger message from WBXML to XML if necessary
       
   133  *	@leave KErrNotFound
       
   134  *		indicates general processing error and to abandon processing
       
   135  *	@leave KErrCorrupt
       
   136  *		there is no body in the message
       
   137  *	@leave TDesC::AllocL
       
   138  
       
   139  */
       
   140 void CROAPTContentHandler::ConvertPushMsgL()
       
   141 	{
       
   142 	__LOG_PTR_DEBUG("CROAPTContentHandler:: ConvertPushMsgL Called"); 
       
   143 
       
   144 	TPtrC contentType;
       
   145 	iMessage->GetContentType(contentType);
       
   146 	if(contentType.CompareF(KROAPTBinContentType) == 0)
       
   147 		{
       
   148 		//This is a WBXML Push Msg, we need to convert it into XML format before passing to DRM agent
       
   149 		CMessageComposer* msgComposer = CMessageComposer::NewL ( *iMessage );
       
   150 		CleanupStack::PushL ( msgComposer );
       
   151 		const TDesC8& convertedMsg = msgComposer->ParseMessageL ();
       
   152 		User::LeaveIfError ( msgComposer->LastError() );
       
   153 		HBufC8* msgBody = convertedMsg.AllocLC();
       
   154 		TPtrC8 hdr;
       
   155 		iMessage->GetHeader(hdr);
       
   156 		HBufC8* hdrBuf = hdr.AllocLC();
       
   157 		delete iMessage;
       
   158 		iMessage = NULL;
       
   159 		
       
   160 		//CPushMessage now takes ownership of hdrBuf and convertedBody
       
   161 		//replace received Binary Push message with newly converted XML Massage
       
   162 		iMessage = CPushMessage::NewL(hdrBuf, msgBody);
       
   163 
       
   164 		CleanupStack::Pop(2,msgBody); //msgBody, hdrBuf
       
   165 		CleanupStack::PopAndDestroy( msgComposer );
       
   166 		}
       
   167 	iState = EStoring;
       
   168 	IdleComplete();
       
   169 	}
       
   170 
       
   171 
       
   172 /** 
       
   173  *	Save push message in message store.
       
   174  * 	Creates link to message service provided by wap push utils
       
   175  *	and uses this and the associated push entry class to save
       
   176  *	the push message.
       
   177  */
       
   178 void CROAPTContentHandler::SaveROAPTToXMLFileL() 
       
   179 	{
       
   180 	__LOG_PTR_DEBUG("CROAPTContentHandler:: SaveROAPTToXMLFile called");
       
   181 
       
   182 	TPtrC8 body;
       
   183 	iMessage->GetMessageBody(body);
       
   184 	// If there is no body in the message leave with an error
       
   185 	if (body.Length() ==0)
       
   186 		{
       
   187 		User::Leave(KErrCorrupt);
       
   188 		}
       
   189 		
       
   190 	HBufC8* bodyBuf = body.AllocLC();
       
   191 	//Create file to store the message in Wap Push temp area 
       
   192 	TFileName roaptfilename;
       
   193 	RFile file;
       
   194 	roaptfilename.Append(KWapPushDir);
       
   195 	roaptfilename.Append(KTempROAPTFile);
       
   196 	iFs.MkDirAll(KWapPushDir);
       
   197 	User::LeaveIfError(file.Replace(iFs, roaptfilename, EFileWrite));
       
   198 	CleanupClosePushL(file);
       
   199 
       
   200 	//Create a heap buffer to store message with header.
       
   201 	//Agent expects the 12 bytes of header to store the message.
       
   202 	//First four bytes to store the offset of the rights message size in the buffer.
       
   203 	//Next four bytes to store the offset of the content message size in the buffer.
       
   204 	//Last four bytes to store the size of rights message.
       
   205 	TInt size=bodyBuf->Length();
       
   206 	iData=HBufC8::NewL(size+KXMLHeader);
       
   207 	TPtr8 bufPtr(iData->Des());
       
   208 	TPckg<TInt> rightsOffset(8);	//There is no content. the rights message size is at offset of 8 bytes.
       
   209 	bufPtr.Append(rightsOffset);
       
   210 	TPckg<TInt> contentOffset(0);
       
   211 	bufPtr.Append(contentOffset);	//No content. Hence set it to zero.
       
   212 	TPckg<TInt> rightsSize(size);
       
   213 	bufPtr.Append(rightsSize);		//Append the size of the rights message
       
   214 	//Append the xml message
       
   215 	bufPtr.Append(*bodyBuf);
       
   216 	
       
   217 	//Now write this buffer to file
       
   218 	User::LeaveIfError(file.Write(*iData));
       
   219 	CleanupStack::PopAndDestroy(&file);
       
   220 	CleanupStack::PopAndDestroy(bodyBuf);
       
   221 	
       
   222 	iState = ECAFInterface;
       
   223 	IdleComplete();
       
   224 	}
       
   225 
       
   226 void CROAPTContentHandler::ProcessToCAFL() 
       
   227 	{
       
   228 	__LOG_PTR_DEBUG("CROAPTContentHandler:: ProcessToCAF called");
       
   229 
       
   230 	iCAFInterface = new(ELeave) CROCHCAFInterface(iFs);
       
   231 
       
   232 	//Register with the CAF session - this initialises the generic variables
       
   233 
       
   234 	iCAFInterface->RegisterL(KROAPTTextContentType);
       
   235 	
       
   236 	if(!iCAFInterface->Registered())
       
   237 		{
       
   238 		//No CAF agent registered which can handle this mime type.
       
   239 		User::Leave(KErrNotFound);
       
   240 		}
       
   241 	
       
   242 	//Initialise the CAF session - this initialises the interface variables and import file
       
   243 	iCAFInterface->PrepareProcessingL();
       
   244 	//Then start the CAF processing
       
   245 	if(iCAFInterface->Processing())
       
   246 		{
       
   247 		//Write the data to CAF
       
   248 		iCAFInterface->WriteDataL(*iData);
       
   249 		iCAFInterface->EndProcessingL();
       
   250 		}
       
   251 	
       
   252 	iState = EDeleteFile;
       
   253 	IdleComplete();	
       
   254 	}
       
   255 
       
   256 void CROAPTContentHandler::DeleteTempROAPTFile()
       
   257 	{
       
   258 	TFileName roaptFileName;
       
   259 	roaptFileName.Append(KWapPushDir);
       
   260 	roaptFileName.Append(KTempROAPTFile);
       
   261 	iFs.Delete(roaptFileName);
       
   262 	iState = EDone;
       
   263 	IdleComplete();
       
   264 	}
       
   265 
       
   266 /** 
       
   267  *	Same functionality as DoCancel()
       
   268  */
       
   269 void CROAPTContentHandler::CancelHandleMessage()
       
   270 	{
       
   271 	__LOG_PTR_DEBUG("CROAPTContentHandler:: CancelHandleMessage called");
       
   272 	Complete(KErrCancel);
       
   273 	}
       
   274 
       
   275 /** 
       
   276  *	Cancels the handling of the message and revokes the active status
       
   277  *	of the handler 
       
   278  */
       
   279 void CROAPTContentHandler::DoCancel()
       
   280 	{
       
   281 	__LOG_PTR_DEBUG("CROAPTContentHandler:: DoCancel Called");
       
   282 	Complete(KErrCancel);	
       
   283 	}
       
   284 
       
   285 /** 
       
   286  *	Step through the various representative states for handling a message 
       
   287  *	1. Conversion of WBXML if necessary
       
   288  *	2. Store the xml file
       
   289  *	3. Pass file to CAF
       
   290  *	4. Delete File
       
   291  *	5. Complete
       
   292  */
       
   293 void CROAPTContentHandler::RunL()
       
   294 	{
       
   295 	__LOG_PTR_DEBUG("CROAPTContentHandler:: RunL Called");
       
   296 	
       
   297 
       
   298 	// use active state machine routine to manage activites:
       
   299 	switch (iState)
       
   300 		{
       
   301 	case EConverting : 	
       
   302 		ConvertPushMsgL();
       
   303 		break;
       
   304 	case EStoring:
       
   305 		SaveROAPTToXMLFileL();
       
   306 		break;
       
   307 	case ECAFInterface:
       
   308 		ProcessToCAFL();
       
   309 		break;
       
   310 	case EDeleteFile:		
       
   311 		DeleteTempROAPTFile();
       
   312 		break;	
       
   313 	case EDone:
       
   314 		Complete(KErrNone);
       
   315 		break;
       
   316 	default:
       
   317 		break;
       
   318 		}
       
   319 	}
       
   320 
       
   321 /** 
       
   322  * CPluginActiveBase methods
       
   323  */
       
   324 TInt CROAPTContentHandler::RunError(TInt aError)
       
   325 	{
       
   326 	__LOG_PTR_DEBUG("CROAPTContentHandler:: RunError Called"); 
       
   327 	iState=EDone;
       
   328 	Complete(aError);
       
   329 	return KErrNone;
       
   330 	}