browserutilities/downloadmgr/DownloadMgrClntSrv/src/DownloadMgrSrvSession.cpp
changeset 0 dd21522fd290
child 37 cb62a4f66ebe
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2004 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 the License "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 *     This file contains the declaration of the Download Mgr Server.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "DownloadMgrLogger.h"
       
    22 #include "DownloadMgrServer.h"
       
    23 #include "DownloadMgrDef.h"
       
    24 #include "DownloadMgrEventQueue.h"
       
    25 
       
    26 #include <HttpClientAppInstance.h>
       
    27 #include <HttpDownload.h>
       
    28 #include <e32svr.h>
       
    29 #include <basched.h>
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 // GLOBAL FUNCTIONS
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // CDownloadMgrSession::NewL
       
    37 // ---------------------------------------------------------
       
    38 //
       
    39 CDownloadMgrSession* CDownloadMgrSession::NewL( CDownloadMgrServer* aServer )
       
    40 	{
       
    41     CLOG_ENTERFN_NULL( "CDownloadMgrSession::NewL" )
       
    42 
       
    43     CDownloadMgrSession* self = new (ELeave) CDownloadMgrSession();
       
    44 	CleanupStack::PushL( self );
       
    45 	self->ConstructL( aServer );
       
    46 	CleanupStack::Pop( self ); // self
       
    47 
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 // ---------------------------------------------------------
       
    52 // CDownloadMgrSession::CDownloadMgrSession
       
    53 // ---------------------------------------------------------
       
    54 //
       
    55 CDownloadMgrSession::CDownloadMgrSession()
       
    56 : iComplete( ETrue )
       
    57 ,iIsMaster( EFalse )
       
    58 	{
       
    59 	__DECLARE_NAME( _S( "CDownloadMgrSession" ) );
       
    60 	
       
    61 	CLOG_CREATE;
       
    62 	}
       
    63 
       
    64 
       
    65 // ---------------------------------------------------------
       
    66 // CDownloadMgrSession::CDownloadMgrSession
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 void CDownloadMgrSession::ConstructL( CDownloadMgrServer* aServer )
       
    70 	{
       
    71     CLOG_ENTERFN( "CDownloadMgrSession::ConstructL" )
       
    72 
       
    73     iSessionId = aServer->NextSessionId();
       
    74     
       
    75 	// second-phase construct base class
       
    76     //CSession2::CreateL();
       
    77 	iDownloadMgrServer = aServer;
       
    78 	// create new object index
       
    79 	iObjectIx = CObjectIx::NewL();
       
    80 	// initialize the object container using the object container index in the server.
       
    81 	iObjectContainer = iDownloadMgrServer->NewContainerL();
       
    82     // Increment the serves's session count and cancel shutdown timer
       
    83     iDownloadMgrServer->IcrementSessions();
       
    84     iEventQueue = new (ELeave) CDownloadMgrEventQueue();
       
    85     CLOG_ATTACH( iEventQueue, this );
       
    86 	}
       
    87 
       
    88 // ---------------------------------------------------------
       
    89 // CDownloadMgrSession::~CDownloadMgrSession
       
    90 // ---------------------------------------------------------
       
    91 //
       
    92 CDownloadMgrSession::~CDownloadMgrSession() 
       
    93 	{
       
    94     CLOG_WRITE_FORMAT( "CDownloadMgrSession::~CDownloadMgrSession iClientAppInstance %d", iClientAppInstance );
       
    95     if( iDownloadMgrServer )
       
    96         {
       
    97 	    CLOG_WRITE_FORMAT( "Number of sessions: %d", iDownloadMgrServer->SessionsCount() );
       
    98         }
       
    99     TRAP_IGNORE( PauseAllL() );
       
   100     if( iDownloadMgrServer )
       
   101         {
       
   102         iDownloadMgrServer->CloseClientInstance( iClientAppInstance );
       
   103         // Decrement the servers's session count and start shutdown timer if nedded
       
   104         iDownloadMgrServer->DecrementSessions();
       
   105         }
       
   106 	//delete the object index (this stores the objects for this session)
       
   107     //it calls Close() on all reference counting objects in the index.
       
   108 	delete iObjectIx;
       
   109     iObjectIx = NULL;
       
   110     if( iDownloadMgrServer )
       
   111         {
       
   112         // Remove and delete the object container
       
   113         iDownloadMgrServer->RemoveContainer( iObjectContainer );
       
   114         }
       
   115     delete iEventQueue;
       
   116     iEventQueue = NULL;
       
   117     
       
   118     CLOG_CLOSE;
       
   119 	}
       
   120 
       
   121 
       
   122 // ---------------------------------------------------------
       
   123 // CDownloadMgrSession::CounterFromHandle
       
   124 // ---------------------------------------------------------
       
   125 //
       
   126 CDownloadSubSession* CDownloadMgrSession::DownloadFromHandle(TUint aHandle)
       
   127     {
       
   128     CLOG_ENTERFN( "CDownloadMgrSession::DownloadFromHandle" )
       
   129 	CDownloadSubSession* download = ( CDownloadSubSession* )iObjectIx->At( aHandle );
       
   130 	if( download == NULL )
       
   131         {
       
   132 		PanicClient( EBadSubsessionHandle ); 
       
   133         }
       
   134 	return download;
       
   135     }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------
       
   139 // CDownloadMgrSession::CounterFromHandle
       
   140 // ---------------------------------------------------------
       
   141 //
       
   142 void CDownloadMgrSession::ServiceL( const RMessage2& aMessage )
       
   143 	{
       
   144     CLOG_ENTERFN( "CDownloadMgrSession::ServiceL" )
       
   145 	TRAPD( err, DispatchMessageL( aMessage ) );
       
   146     if( iComplete || ( KErrNone != err ) )
       
   147         {
       
   148         // Only the syncron requests has to be completed here!
       
   149 	    aMessage.Complete( err );
       
   150         }
       
   151 	}
       
   152 
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CDownloadMgrSession::DispatchMessageL
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 void CDownloadMgrSession::DispatchMessageL( const RMessage2& aMessage )
       
   159 	{
       
   160     CLOG_ENTERFN( "CDownloadMgrSession::DispatchMessageL" )
       
   161     CLOG_WRITE_FORMAT( "Message function: %d", aMessage.Function() )
       
   162     // Set this false in case of the asyncron requests!!!
       
   163     iComplete = ETrue;
       
   164     SetCurrentMessage( aMessage );
       
   165     
       
   166     // Fixed for Bug id - JJUN-78WCJ3 (DownloadMgrServer crashes under IPC attack)
       
   167     //This check will make sure that before start of any download, InitializeL() should
       
   168     // be called so that all the member variables will be initialized.
       
   169     if(aMessage.Function() != EHttpDownloadMgrInitialize && iClientAppInstance == NULL)
       
   170         {
       
   171         User::Leave(KErrGeneral);
       
   172         }
       
   173         
       
   174     // check for session-relative requests
       
   175 	switch( aMessage.Function() )
       
   176 		{
       
   177         case EHttpDownloadMgrInitialize:
       
   178             {
       
   179             InitializeL();
       
   180             return;
       
   181             }
       
   182 	    case EHttpDownloadMgrCreateDownload:
       
   183             {
       
   184 		    NewDownloadL();
       
   185             return;
       
   186             }
       
   187         case EHttpDownloadMgrAttach:
       
   188             {
       
   189             DownloadMgrAttachL();
       
   190             return;
       
   191             }
       
   192         case EHttpDownloadAttach:
       
   193             {
       
   194             DownloadAttachL();
       
   195             return;
       
   196             }
       
   197 		case EHttpDownloadAttachToDownload:
       
   198 		    {
       
   199 		    AttachToDownloadL();
       
   200 		    return;
       
   201 		    }
       
   202         case EHttpDownloadMgrPauseAll:
       
   203             {
       
   204             PauseAllL();
       
   205             return;
       
   206             }
       
   207         case EHttpDownloadMgrStartAllL:
       
   208             {
       
   209             StartAllL();
       
   210             return;
       
   211             }
       
   212         case EHttpDownloadMgrResetAll:
       
   213             {
       
   214             ResetAll();
       
   215             return;
       
   216             }
       
   217         case EHttpDownloadMgrDeleteAll:
       
   218             {
       
   219             DeleteAll();
       
   220             return;
       
   221             }
       
   222         case EHttpDownloadMgrDisconnect:
       
   223             {
       
   224             Disconnect();
       
   225             return;
       
   226             }
       
   227         case EHttpDownloadMgrGetIntAttribute:
       
   228             {
       
   229             GetIntAttributeL();
       
   230             return;
       
   231             }
       
   232         case EHttpDownloadMgrGetBoolAttribute:
       
   233             {
       
   234             GetBoolAttributeL();
       
   235             return;
       
   236             }
       
   237         case EHttpDownloadMgrGetStringAttribute:
       
   238             {
       
   239             GetStringAttributeL();
       
   240             return;
       
   241             }
       
   242         case EHttpDownloadMgrGetString8Attribute:
       
   243             {
       
   244             GetString8AttributeL();
       
   245             return;
       
   246             }
       
   247         case EHttpDownloadMgrSetIntAttribute:
       
   248             {
       
   249             SetIntAttributeL();
       
   250             return;
       
   251             }
       
   252         case EHttpDownloadMgrSetBoolAttribute:
       
   253             {
       
   254             SetBoolAttributeL();
       
   255             return;
       
   256             }
       
   257         case EHttpDownloadMgrSetStringAttribute:
       
   258             {
       
   259             SetStringAttributeL();
       
   260             return;
       
   261             }
       
   262         case EHttpDownloadMgrSetString8Attribute:
       
   263             {
       
   264             SetString8AttributeL();
       
   265             return;
       
   266             }
       
   267         case EHttpDownloadMgrSessionEventSubscription:
       
   268             {
       
   269             InitSessionEvent( aMessage );
       
   270             // Send event from the queue
       
   271             TUint dummy( 0 );
       
   272             Event( NULL, (THttpDownloadState)dummy, (THttpProgressState)dummy, dummy );
       
   273             // This is an asyncron request will be completed later!
       
   274             SetComplete( EFalse );
       
   275             return;
       
   276             }
       
   277         case EHttpDownloadMgrSessionEventCancel:
       
   278             {
       
   279             CancelMovedEvent();
       
   280             return;
       
   281             }
       
   282 		}
       
   283 	// ok, it must be subsession relative
       
   284 	CDownloadSubSession* download = DownloadFromHandle( aMessage.Int3() );
       
   285     __ASSERT_DEBUG( download, PanicClient( EBadSubsessionHandle ) );
       
   286 
       
   287     TRAPD( err, download->DispatchMessageL( aMessage ) );
       
   288 
       
   289     if( err )
       
   290         {
       
   291         download->OnError( err );
       
   292         User::Leave( err );
       
   293         }
       
   294 	}
       
   295 
       
   296 // ---------------------------------------------------------
       
   297 // CDownloadMgrSession::SetCurrentMessage
       
   298 // ---------------------------------------------------------
       
   299 //
       
   300 void CDownloadMgrSession::SetCurrentMessage( const RMessage2& aMessage )
       
   301     {
       
   302     iCurrentMessage = aMessage;
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // CDownloadMgrSession::CurrentMessage
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 const RMessage2& CDownloadMgrSession::CurrentMessage() const
       
   310     {
       
   311     return iCurrentMessage;
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------
       
   315 // CDownloadMgrSession::InitializeL
       
   316 // ---------------------------------------------------------
       
   317 //
       
   318 void CDownloadMgrSession::InitializeL()
       
   319     {
       
   320     CLOG_ENTERFN( "CDownloadMgrSession::DownloadMgrAttachCountL" )
       
   321     iIsMaster = (TBool)CurrentMessage().Int2();
       
   322 
       
   323     TUid uid;
       
   324     TPckg<TUid> uidPckg( uid );
       
   325 
       
   326     Read( 0, uidPckg );
       
   327 
       
   328     CreateClientAppInstanceL( uidPckg().iUid );
       
   329 
       
   330     // Check how many download we have and return it to the client
       
   331     CLOG_WRITE_FORMAT( "CDownloadMgrSession::InitializeL iClientAppInstance %d", iClientAppInstance );
       
   332     CArrayPtrFlat<CHttpDownload>* currentDownloads = iClientAppInstance->DownloadsL();
       
   333 	TPckg<TInt> countPckg( currentDownloads->Count() );
       
   334 	TPckg<TInt> sessionId( iSessionId );
       
   335 
       
   336     Write( 1, CurrentMessage(), countPckg );
       
   337     Write( 3, CurrentMessage(), sessionId );
       
   338 
       
   339     CLOG_NAME_2( _L("Session_%x_%x"), uidPckg().iUid, iSessionId );
       
   340 
       
   341     currentDownloads->Reset();
       
   342     delete currentDownloads;
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------
       
   346 // CDownloadMgrSession::DownloadMgrAttachL
       
   347 // ---------------------------------------------------------
       
   348 //
       
   349 void CDownloadMgrSession::DownloadMgrAttachL()
       
   350     {
       
   351     CLOG_ENTERFN( "CDownloadMgrSession::DownloadMgrAttachL" )
       
   352     // The client would like to attach the dowmloads were created previously.
       
   353     // Here, we create a buffer with the unique handles of download subsessions
       
   354     // and copy it to the client's address space.
       
   355     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL iClientAppInstance %d", iClientAppInstance );
       
   356     CArrayPtrFlat<CHttpDownload>* currentDownloads = iClientAppInstance->DownloadsL();
       
   357     CleanupStack::PushL( currentDownloads );
       
   358     TPckgBuf<TInt> arrayPckg;
       
   359     HBufC8* buf = HBufC8::NewLC( currentDownloads->Count() * arrayPckg.Size() );
       
   360     // Cerate subsessions one by one and write the unique handles to the client.
       
   361     // Later the handles is used by the client subsession to attach to the server.
       
   362     for( TInt i = 0; i < currentDownloads->Count(); i++ )
       
   363         {
       
   364         CHttpDownload* httpDownload = (*currentDownloads)[i];
       
   365 	    // make a new download object
       
   366 	    CDownloadSubSession* download = CDownloadSubSession::NewL( this, httpDownload );
       
   367         CleanupStack::PushL( download );
       
   368 	    // add object to object container to gererate unique id
       
   369 	    iObjectContainer->AddL( download );
       
   370 	    // add object to object index; this returns a unique handle so we can get it again
       
   371         CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, download %d", download );
       
   372 	    arrayPckg() = iObjectIx->AddL( download );
       
   373         CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, iObjectIx->ActiveCount() %d", iObjectIx->ActiveCount() );
       
   374         // Store handle to the subsession. It is used when the URL is checked.
       
   375         download->SetHandle( arrayPckg() );
       
   376         CleanupStack::Pop( download ); //download
       
   377         buf->Des().Append( arrayPckg );
       
   378         }
       
   379     TPtr8 ptr = buf->Des();
       
   380 
       
   381     Write( 0, CurrentMessage(), ptr );
       
   382 
       
   383     CleanupStack::PopAndDestroy( buf ); // buf
       
   384     currentDownloads->Reset();
       
   385     CleanupStack::PopAndDestroy( currentDownloads ); // currentDownloads
       
   386     }
       
   387 
       
   388 // ---------------------------------------------------------
       
   389 // CDownloadMgrSession::DownloadAttachL
       
   390 // ---------------------------------------------------------
       
   391 //
       
   392 void CDownloadMgrSession::DownloadAttachL()
       
   393 	{
       
   394     CLOG_ENTERFN( "CDownloadMgrSession::DownloadAttachL" )
       
   395     // All we have to do to write the handle to the 3rd parameter.
       
   396     // The client-server framework will take care of the 3rd parameter
       
   397     // whenever the client calls a subsession related service.
       
   398     TInt handle = CurrentMessage().Int1();
       
   399     TPckg<TInt> handlePckg( handle );
       
   400     WriteHandle( handlePckg() );
       
   401 	}
       
   402 
       
   403 // ---------------------------------------------------------
       
   404 // CDownloadMgrSession::AttachToDownloadL
       
   405 // ---------------------------------------------------------
       
   406 //
       
   407 void CDownloadMgrSession::AttachToDownloadL()
       
   408     {
       
   409     CLOG_ENTERFN( "CDownloadMgrSession::AttachToDownload" )
       
   410     __ASSERT_DEBUG( iClientAppInstance, PanicClient( EMultipleInitDownloadEvent ) );
       
   411 
       
   412     const TInt32 id = CurrentMessage().Int0();
       
   413 
       
   414     CHttpDownload* dl = iClientAppInstance->AttachDownloadL( id );
       
   415 		if (dl)
       
   416 		{
       
   417 			CreateDownloadL( dl );
       
   418 		}
       
   419 		else
       
   420 		{
       
   421 			User::LeaveIfError(KErrNotFound);
       
   422 		}
       
   423     }
       
   424 
       
   425 // ---------------------------------------------------------
       
   426 // CDownloadMgrSession::CreateDownloadL
       
   427 // ---------------------------------------------------------
       
   428 //
       
   429 void CDownloadMgrSession::CreateDownloadL( CHttpDownload* aDownload, TBool aDefAttrib )
       
   430 	{
       
   431 	// make a new download object
       
   432 	CDownloadSubSession* download = CDownloadSubSession::NewL( this, aDownload );
       
   433     CleanupStack::PushL( download );
       
   434     if( aDefAttrib )
       
   435         {
       
   436         download->SetDefAttributesL();
       
   437         }
       
   438 	// add object to object container to gererate unique id
       
   439 	iObjectContainer->AddL( download );
       
   440 	// add object to object index; this returns a unique handle so we can get it again
       
   441     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, download %d", download );
       
   442 	TInt handle = iObjectIx->AddL( download );
       
   443     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, iObjectIx->ActiveCount() %d", iObjectIx->ActiveCount() );
       
   444     // Store handle to the subsession. It is used when the URL is checked.
       
   445     download->SetHandle( handle );
       
   446     CleanupStack::Pop( download ); //download
       
   447 	// write the handle to client
       
   448     TPckg<TInt> handlePckg( handle );
       
   449 
       
   450     Write( 1, CurrentMessage(), handlePckg );
       
   451 
       
   452     // write the handle to the framework
       
   453 	WriteHandle( handle );	
       
   454 	}
       
   455 
       
   456 // ---------------------------------------------------------
       
   457 // CDownloadMgrSession::CreateClientAppInstanceL
       
   458 // ---------------------------------------------------------
       
   459 //
       
   460 void CDownloadMgrSession::CreateClientAppInstanceL( TUint32 aAppUid )
       
   461     {
       
   462     CLOG_ENTERFN( "CDownloadMgrSession::CreateClientAppInstanceL" )
       
   463     if( !iClientAppInstance )
       
   464         {
       
   465         CLOG_ENTERFN( "CDownloadMgrSession::CreateClientAppInstanceL REALLY!!!" )
       
   466 
       
   467         iClientAppInstance = iDownloadMgrServer->CreateNewClientAppInstanceL(
       
   468             aAppUid, *this, iIsMaster );
       
   469         CLOG_WRITE_FORMAT( "CDownloadMgrSession::CreateClientAppInstanceL iClientAppInstance %d", iClientAppInstance );
       
   470         }
       
   471     }
       
   472 
       
   473 // ---------------------------------------------------------
       
   474 // CDownloadMgrSession::WriteHandle
       
   475 // ---------------------------------------------------------
       
   476 //
       
   477 void CDownloadMgrSession::WriteHandle( TInt aHandle )
       
   478     {
       
   479     CLOG_ENTERFN( "CDownloadMgrSession::WriteHandle" )
       
   480 	TPckg<TInt> handlePckg( aHandle );
       
   481 
       
   482     TInt res = CurrentMessage().Write( 3, handlePckg );
       
   483 
       
   484 	if( res != KErrNone )
       
   485 		{
       
   486 		iObjectIx->Remove( aHandle );
       
   487 		PanicClient( EBadDescriptor );
       
   488 		return;
       
   489 		}
       
   490     }
       
   491 
       
   492 // ---------------------------------------------------------
       
   493 // CDownloadMgrSession::NewDownloadL
       
   494 // ---------------------------------------------------------
       
   495 //
       
   496 void CDownloadMgrSession::NewDownloadL()
       
   497 	{
       
   498     CLOG_ENTERFN( "CDownloadMgrSession::NewDownloadL" )
       
   499 
       
   500     TInt desLen = CurrentMessage().GetDesLength( 0 );
       
   501 
       
   502 	HBufC8* urlBuf = HBufC8::NewLC( desLen );
       
   503 	TPtr8 urlptr = urlBuf->Des();
       
   504 
       
   505     Read( 0, urlptr );
       
   506 
       
   507     CLOG_WRITE_FORMAT( "CDownloadMgrSession::NewDownloadL iClientAppInstance %d", iClientAppInstance );
       
   508     CHttpDownload* httpDownload = 
       
   509         iClientAppInstance->CreateNewDownloadL( urlptr );
       
   510     iClientAppInstance->SetObserver( this );
       
   511     CleanupStack::PopAndDestroy( urlBuf ); // urlBuf
       
   512     
       
   513     CreateDownloadL( httpDownload, ETrue );
       
   514 	}
       
   515 
       
   516 // ---------------------------------------------------------
       
   517 // CDownloadMgrSession::CreateSubSessionAndSendEventL
       
   518 // ---------------------------------------------------------
       
   519 //
       
   520 TBool CDownloadMgrSession::CreateSubSessionAndSendEventL( CHttpDownload* aDownload,
       
   521                                                           TInt32 aDownloadState,
       
   522                                                           TInt32 aProgressState,
       
   523                                                           TInt32 aMoIndex )
       
   524     {
       
   525     CLOG_ENTERFN( "CDownloadMgrSession::CreateSubSessionAndSendEventL" )
       
   526 	// make a new download object
       
   527 	CDownloadSubSession* download = CDownloadSubSession::NewL( this, aDownload );
       
   528     CleanupStack::PushL( download );
       
   529 	// add object to object container to gererate unique id
       
   530 	iObjectContainer->AddL( download );
       
   531 	// add object to object index; this returns a unique handle so we can get it again
       
   532     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, download %d", download );
       
   533 	TInt handle = iObjectIx->AddL( download );
       
   534     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DownloadMgrAttachL, iObjectIx->ActiveCount() %d", iObjectIx->ActiveCount() );
       
   535     // Store handle to the subsession. It is used when the URL is checked.
       
   536     download->SetHandle( handle );
       
   537     CleanupStack::Pop( download ); //download
       
   538 
       
   539     TBool retVal( iEvent );
       
   540     if( iEvent )
       
   541         {
       
   542         TPckg<TInt32> downloadStatePckg( aDownloadState );
       
   543         TPckg<TInt32> progressStatePckg( aProgressState );
       
   544         
       
   545 	    // Mask media object index with handle.
       
   546 	    // For example if aMoIndex = 0x00000004 and handle = 0x00000005,
       
   547 	    // then maskData = 0x00040005.
       
   548 	    TInt32 maskedData = ((aMoIndex << (sizeof(TInt32)/2)) | (handle & 0x0000FFFF));
       
   549         TPckg<TInt> maskPckg( maskedData );
       
   550 
       
   551         Write( 0, iEventMessage, downloadStatePckg );
       
   552         Write( 1, iEventMessage, progressStatePckg );
       
   553         Write( 2, iEventMessage, maskPckg );
       
   554 
       
   555         EventComplete( KErrNone );
       
   556         }
       
   557     CLOG_WRITE_FORMAT( "Session event sent: %d", retVal )
       
   558     return retVal;
       
   559     }
       
   560 
       
   561 // ---------------------------------------------------------
       
   562 // CDownloadMgrSession::EventComplete
       
   563 // ---------------------------------------------------------
       
   564 //
       
   565 void CDownloadMgrSession::EventComplete( TInt aErrorStatus )
       
   566     {
       
   567     if( iEvent )
       
   568         {
       
   569         iEventMessage.Complete( aErrorStatus );
       
   570         iEvent = EFalse;
       
   571         }
       
   572     }
       
   573 
       
   574 // ---------------------------------------------------------
       
   575 // CDownloadMgrSession::DeleteDownload
       
   576 // ---------------------------------------------------------
       
   577 //
       
   578 void CDownloadMgrSession::DeleteDownload( TUint aHandle )
       
   579 	{
       
   580     CLOG_ENTERFN( "CDownloadMgrSession::DeleteDownload" )
       
   581 	// panic if bad handle
       
   582 	CDownloadSubSession* download = ( CDownloadSubSession* )iObjectIx->At( aHandle );
       
   583     CLOG_WRITE_FORMAT( "CDownloadMgrSession::DeleteDownload, download %d", download );
       
   584 	if( download == NULL )
       
   585         {
       
   586 		PanicClient( EBadCounterRemove );
       
   587         }
       
   588     //Removes the reference counting object identified by handle number 
       
   589     //from this object index and closes it.
       
   590     iObjectIx->Remove( aHandle );
       
   591 	}
       
   592 
       
   593 // ---------------------------------------------------------
       
   594 // CDownloadMgrSession::PanicClient
       
   595 // ---------------------------------------------------------
       
   596 //
       
   597 void CDownloadMgrSession::PanicClient( TInt aPanic ) const
       
   598 	{
       
   599     CLOG_ENTERFN( "CDownloadMgrSession::PanicClient" )
       
   600     CLOG_WRITE_FORMAT( " aPanic=%d", aPanic )
       
   601 	_LIT( KTxtDownloadMgrSession,"CDlMgrSession");
       
   602 
       
   603     CurrentMessage().Panic( KTxtDownloadMgrSession, aPanic );
       
   604 	}
       
   605 
       
   606 /// ---------------------------------------------------------
       
   607 // CDownloadMgrSession::PauseAllL
       
   608 // ---------------------------------------------------------
       
   609 //
       
   610 void CDownloadMgrSession::PauseAllL()
       
   611     {
       
   612     CLOG_ENTERFN( "CDownloadMgrSession::PauseAllL" )
       
   613     for( TInt i = 0; i < iObjectIx->ActiveCount(); i++ )
       
   614         {
       
   615         CDownloadSubSession* downloadSess 
       
   616             = ( CDownloadSubSession* )(*iObjectIx)[i];
       
   617         if( downloadSess == NULL )
       
   618             {
       
   619             // Already removed.
       
   620             }
       
   621         else
       
   622             {
       
   623             downloadSess->PauseL();
       
   624             }
       
   625         }
       
   626     }
       
   627 
       
   628 /// ---------------------------------------------------------
       
   629 // CDownloadMgrSession::StartAllL
       
   630 // ---------------------------------------------------------
       
   631 //
       
   632 void CDownloadMgrSession::StartAllL()
       
   633     {
       
   634     CLOG_ENTERFN( "CDownloadMgrSession::StartAllL" )
       
   635     for( TInt i = 0; i < iObjectIx->Count(); i++ )
       
   636         {
       
   637         CDownloadSubSession* downloadSess 
       
   638             = ( CDownloadSubSession* )(*iObjectIx)[i];
       
   639         if( downloadSess == NULL )
       
   640             {
       
   641             // Already removed.
       
   642             }
       
   643         else
       
   644             {
       
   645             downloadSess->StartL();
       
   646             }
       
   647         }
       
   648     }
       
   649 
       
   650 /// ---------------------------------------------------------
       
   651 // CDownloadMgrSession::ResetAll
       
   652 // ---------------------------------------------------------
       
   653 //
       
   654 void CDownloadMgrSession::ResetAll()
       
   655     {
       
   656     CLOG_ENTERFN( "CDownloadMgrSession::CancelAll" )
       
   657     for( TInt i = 0; i < iObjectIx->ActiveCount(); i++ )
       
   658         {
       
   659         CDownloadSubSession* downloadSess 
       
   660             = ( CDownloadSubSession* )(*iObjectIx)[i];
       
   661         if( downloadSess == NULL )
       
   662             {
       
   663             // Already removed.
       
   664             }
       
   665         else
       
   666             {
       
   667             downloadSess->Reset();
       
   668             }
       
   669         }
       
   670     }
       
   671 
       
   672 /// ---------------------------------------------------------
       
   673 // CDownloadMgrSession::DeleteAll
       
   674 // ---------------------------------------------------------
       
   675 //
       
   676 void CDownloadMgrSession::DeleteAll()
       
   677     {
       
   678     CLOG_ENTERFN( "CDownloadMgrSession::DeleteAll" )
       
   679     for( TInt i = 0; i < iObjectIx->ActiveCount(); i++ )
       
   680         {
       
   681         CDownloadSubSession* downloadSess 
       
   682             = ( CDownloadSubSession* )(*iObjectIx)[i];
       
   683         if( downloadSess == NULL )
       
   684             {
       
   685             // Already removed.
       
   686             }
       
   687         else
       
   688             {
       
   689             downloadSess->Delete();
       
   690             }
       
   691         }
       
   692     }
       
   693 
       
   694 /// ---------------------------------------------------------
       
   695 // CDownloadMgrSession::Disconnect
       
   696 // ---------------------------------------------------------
       
   697 //
       
   698 void CDownloadMgrSession::Disconnect()
       
   699     {
       
   700     CLOG_WRITE_FORMAT( "CDownloadMgrSession::Disconnect iClientAppInstance %d", iClientAppInstance );
       
   701     iClientAppInstance->Disconnect();
       
   702     }
       
   703 
       
   704 // ---------------------------------------------------------
       
   705 // CDownloadMgrSession::Write
       
   706 // ---------------------------------------------------------
       
   707 //
       
   708 void CDownloadMgrSession::Write( TInt aParam, RMessage2 aMessage, TDesC8& aDes )
       
   709 	{
       
   710     CLOG_ENTERFN( "CDownloadMgrSession::Write" )
       
   711 
       
   712     TInt ret = aMessage.Write( aParam, aDes );
       
   713 
       
   714 	if( ret != KErrNone )
       
   715         {
       
   716 		PanicClient( EBadDescriptor );
       
   717         }
       
   718 	}
       
   719 
       
   720 // ---------------------------------------------------------
       
   721 // CDownloadMgrSession::Write
       
   722 // ---------------------------------------------------------
       
   723 //
       
   724 void CDownloadMgrSession::Write( TInt aParam, RMessage2 aMessage, TDesC16& aDes )
       
   725 	{
       
   726     CLOG_ENTERFN( "CDownloadMgrSession::Write" )
       
   727 
       
   728     TInt ret = aMessage.Write( aParam, aDes );
       
   729 
       
   730 	if( ret != KErrNone )
       
   731         {
       
   732 		PanicClient( EBadDescriptor );
       
   733         }
       
   734 	}
       
   735 
       
   736 // ---------------------------------------------------------
       
   737 // CDownloadMgrSession::Read
       
   738 // ---------------------------------------------------------
       
   739 //
       
   740 void CDownloadMgrSession::Read( TInt aParam, TPtr8& aDes )
       
   741 	{
       
   742     CLOG_ENTERFN( "CDownloadMgrSession::Read" )
       
   743 
       
   744     TInt ret = CurrentMessage().Read( aParam, aDes );
       
   745 
       
   746 	if( ret != KErrNone )
       
   747         {
       
   748 		PanicClient( EBadDescriptor );
       
   749         }
       
   750 	}
       
   751 
       
   752 // ---------------------------------------------------------
       
   753 // CDownloadMgrSession::Read
       
   754 // ---------------------------------------------------------
       
   755 //
       
   756 void CDownloadMgrSession::Read( TInt aParam, TPtr16& aDes )
       
   757 	{
       
   758     CLOG_ENTERFN( "CDownloadMgrSession::Read" )
       
   759 
       
   760     TInt ret = CurrentMessage().Read( aParam, aDes );
       
   761 
       
   762 	if( ret != KErrNone )
       
   763         {
       
   764 		PanicClient( EBadDescriptor );
       
   765         }
       
   766 	}
       
   767 
       
   768 // ---------------------------------------------------------
       
   769 // CDownloadMgrSession::Event
       
   770 // ---------------------------------------------------------
       
   771 //
       
   772 void CDownloadMgrSession::Event( CHttpDownload* aDownload, 
       
   773                                  THttpDownloadState aDlState,
       
   774                                  THttpProgressState aProgState,
       
   775                                  TInt32 aMoIndex )
       
   776     {
       
   777     CLOG_ENTERFN( "CDownloadMgrSession::Event" )
       
   778     TBool not_sent( ETrue );
       
   779     // First we add the download to the queue then send the next event.
       
   780     // aDownload could be NULL, this case we send the next event.
       
   781     // If aDownload == NULL the state parameters are ignored. (They don't hold information)
       
   782     if( aDownload )
       
   783         {
       
   784         if( !iEventQueue->In( aDownload, aDlState, aProgState, aMoIndex ) )
       
   785             {
       
   786             // OOM
       
   787             return;
       
   788             }
       
   789         }
       
   790     // Then we send an event from the queue
       
   791     if( !iEventQueue->IsEmpty() )
       
   792         {
       
   793         TInt32 downloadState( 0 );
       
   794         TInt32 progressState( 0 );
       
   795         TInt32 mediaIndex( 0 );
       
   796 
       
   797         CHttpDownload* download = iEventQueue->Next( downloadState, progressState, mediaIndex );
       
   798         for( TInt i = 0; i < iObjectIx->Count(); i++ )
       
   799             {
       
   800             CLOG_WRITE_FORMAT( "CDownloadMgrSession::Event, i %d", i );
       
   801             CDownloadSubSession* downloadSess 
       
   802                 = ( CDownloadSubSession* )(*iObjectIx)[i];
       
   803             CLOG_WRITE_FORMAT( "CDownloadMgrSession::Event, downloadSess %d", downloadSess );
       
   804             if( downloadSess == NULL )
       
   805                 {
       
   806                 // Already removed.
       
   807                 }
       
   808             else if ( downloadSess->IsDownload( download ) )
       
   809                 {
       
   810                 CLOG_WRITE_FORMAT( "Download: 0x%x", download )
       
   811                 CLOG_WRITE_FORMAT( "State: %d", downloadState )
       
   812                 CLOG_WRITE_FORMAT( "Progress: %d",progressState )
       
   813                 CLOG_WRITE_FORMAT( "Media Index: %d",mediaIndex )
       
   814                 
       
   815                 TBool isSend( EFalse );
       
   816                 TRAP_IGNORE( isSend = downloadSess->EventL( downloadState, progressState, mediaIndex ) );
       
   817                 if( isSend )
       
   818                     {
       
   819                     // the event was sent, it can be removed from the queue
       
   820                     iEventQueue->Out();
       
   821                     not_sent = EFalse;
       
   822                     }
       
   823                 }
       
   824             }
       
   825 
       
   826         // Then we must deal with the moved event 
       
   827         // if it has not been sent and the event handler is initialized
       
   828         if( (downloadState == EHttpDlMoved) && not_sent )
       
   829             {
       
   830             TBool retVal( EFalse );
       
   831             TRAPD( err, retVal = CreateSubSessionAndSendEventL( aDownload, downloadState, progressState, mediaIndex ) );
       
   832             
       
   833             if( err )
       
   834             	{
       
   835             	// Do't care, never leave
       
   836             	}
       
   837 
       
   838             if ( !retVal )
       
   839                 {
       
   840                 //OOM
       
   841                 return;
       
   842                 }
       
   843             else
       
   844                 {
       
   845                 // the event was sent, it can be removed from the queue
       
   846                 iEventQueue->Out();
       
   847                 not_sent = EFalse;
       
   848                 }
       
   849             }
       
   850         }
       
   851     }
       
   852 
       
   853 // ---------------------------------------------------------
       
   854 // CDownloadMgrSession::SetComplete
       
   855 // ---------------------------------------------------------
       
   856 //
       
   857 void CDownloadMgrSession::SetComplete( TBool aComplete )
       
   858     {
       
   859     iComplete = aComplete;
       
   860     }
       
   861 
       
   862 // ---------------------------------------------------------
       
   863 // CDownloadMgrSession::GetIntAttributeL
       
   864 // ---------------------------------------------------------
       
   865 //
       
   866 void CDownloadMgrSession::GetIntAttributeL()
       
   867     {
       
   868     CLOG_ENTERFN( "CDownloadMgrSession::GetIntAttributeL" )
       
   869 
       
   870     const TInt attr = CurrentMessage().Int0();
       
   871     TInt32 value;
       
   872 
       
   873     CLOG_WRITE_FORMAT( "CDownloadMgrSession::GetIntAttributeL iClientAppInstance %d", iClientAppInstance );
       
   874     iClientAppInstance->GetIntAttributeL( (THttpDownloadMgrAttrib)attr, value );
       
   875 
       
   876     TPckg<TInt32> pckg( value );
       
   877 
       
   878     Write( 1, CurrentMessage(), pckg );
       
   879     }
       
   880 
       
   881 // ---------------------------------------------------------
       
   882 // CDownloadMgrSession::GetBoolAttributeL
       
   883 // ---------------------------------------------------------
       
   884 //
       
   885 void CDownloadMgrSession::GetBoolAttributeL()
       
   886     {
       
   887     CLOG_ENTERFN( "CDownloadMgrSession::GetBoolAttributeL" )
       
   888 
       
   889     const TInt attr = CurrentMessage().Int0();
       
   890     TBool value;
       
   891 
       
   892     CLOG_WRITE_FORMAT( "CDownloadMgrSession::GetBoolAttributeL iClientAppInstance %d", iClientAppInstance );
       
   893     iClientAppInstance->GetBoolAttributeL( (THttpDownloadMgrAttrib)attr, value );
       
   894 
       
   895     TPckg<TBool> pckg( value );
       
   896 
       
   897     Write( 1, CurrentMessage(), pckg );
       
   898     }
       
   899 
       
   900 // ---------------------------------------------------------
       
   901 // CDownloadMgrSession::GetStringAttributeL
       
   902 // ---------------------------------------------------------
       
   903 //
       
   904 void CDownloadMgrSession::GetStringAttributeL()
       
   905     {
       
   906     CLOG_ENTERFN( "CDownloadMgrSession::GetStringAttributeL" )
       
   907     const TInt attr = CurrentMessage().Int0();
       
   908 
       
   909     CLOG_WRITE_FORMAT( "CDownloadMgrSession::GetStringAttributeL iClientAppInstance %d", iClientAppInstance );
       
   910 
       
   911     TBool deleteStr;
       
   912     HBufC* attrStr = iClientAppInstance->GetStringAttributeL( 
       
   913                                             (THttpDownloadMgrAttrib)attr,
       
   914                                             deleteStr );
       
   915     TPtr value( attrStr->Des() );
       
   916 
       
   917     Write( 1, CurrentMessage(), value );
       
   918 
       
   919     if( deleteStr )
       
   920         {
       
   921         delete attrStr;
       
   922         }
       
   923     }
       
   924 
       
   925 // ---------------------------------------------------------
       
   926 // CDownloadMgrSession::GetStringAttributeL
       
   927 // ---------------------------------------------------------
       
   928 //
       
   929 void CDownloadMgrSession::GetString8AttributeL()
       
   930     {
       
   931     CLOG_ENTERFN( "CDownloadMgrSession::GetString8AttributeL" )
       
   932     const TInt attr = CurrentMessage().Int0();
       
   933 
       
   934     CLOG_WRITE_FORMAT( "CDownloadMgrSession::GetStringAttributeL iClientAppInstance %d", iClientAppInstance );
       
   935 
       
   936     TBool deleteStr;
       
   937     HBufC8* attrStr = iClientAppInstance->GetString8AttributeL( 
       
   938                                             (THttpDownloadMgrAttrib)attr,
       
   939                                             deleteStr );
       
   940     TPtr8 value( attrStr->Des() );
       
   941 
       
   942     Write( 1, CurrentMessage(), value );
       
   943 
       
   944     if( deleteStr )
       
   945         {
       
   946         delete attrStr;
       
   947         }
       
   948     }
       
   949 
       
   950 // ---------------------------------------------------------
       
   951 // CDownloadMgrSession::SetIntAttributeL
       
   952 // ---------------------------------------------------------
       
   953 //
       
   954 void CDownloadMgrSession::SetIntAttributeL()
       
   955     {
       
   956     CLOG_ENTERFN( "CDownloadMgrSession::SetIntAttributeL" )
       
   957     const TInt attr = CurrentMessage().Int0();
       
   958     const TInt32 value = CurrentMessage().Int1();
       
   959     CLOG_WRITE_FORMAT( "CDownloadMgrSession::SetIntAttributeL iClientAppInstance %d", iClientAppInstance );
       
   960     iClientAppInstance->SetIntAttributeL( (THttpDownloadMgrAttrib)attr, value );
       
   961     }
       
   962         
       
   963 // ---------------------------------------------------------
       
   964 // CDownloadMgrSession::SetBoolAttributeL
       
   965 // ---------------------------------------------------------
       
   966 //
       
   967 void CDownloadMgrSession::SetBoolAttributeL()
       
   968     {
       
   969     CLOG_ENTERFN( "CDownloadMgrSession::SetBoolAttributeL" )
       
   970     const TInt attr = CurrentMessage().Int0();
       
   971     const TBool value = (TBool)CurrentMessage().Int1();
       
   972     CLOG_WRITE_FORMAT( "CDownloadMgrSession::SetBoolAttributeL iClientAppInstance %d", iClientAppInstance );
       
   973     iClientAppInstance->SetBoolAttributeL( (THttpDownloadMgrAttrib)attr, value );
       
   974     }
       
   975 		
       
   976 // ---------------------------------------------------------
       
   977 // CDownloadMgrSession::SetStringAttributeL
       
   978 // ---------------------------------------------------------
       
   979 //
       
   980 void CDownloadMgrSession::SetStringAttributeL()
       
   981     {
       
   982     CLOG_ENTERFN( "CDownloadMgrSession::SetStringAttributeL" )
       
   983     const TInt attr = CurrentMessage().Int0();
       
   984 
       
   985     TInt desLen = CurrentMessage().GetDesLength( 1 );
       
   986 
       
   987 	HBufC* writeBuf = HBufC::NewLC( desLen );
       
   988 	TPtr initptr = writeBuf->Des();
       
   989 
       
   990     Read( 1, initptr );
       
   991 
       
   992     CLOG_WRITE_FORMAT( "CDownloadMgrSession::SetStringAttributeL iClientAppInstance %d", iClientAppInstance );
       
   993     iClientAppInstance->SetStringAttributeL( (THttpDownloadMgrAttrib)attr, initptr );
       
   994     CleanupStack::PopAndDestroy( writeBuf ); // writeBuf
       
   995     }
       
   996 
       
   997 // ---------------------------------------------------------
       
   998 // CDownloadMgrSession::SetString8AttributeL
       
   999 // ---------------------------------------------------------
       
  1000 //
       
  1001 void CDownloadMgrSession::SetString8AttributeL()
       
  1002     {
       
  1003     CLOG_ENTERFN( "CDownloadMgrSession::SetString8AttributeL" )
       
  1004     const TInt attr = CurrentMessage().Int0();
       
  1005 
       
  1006     TInt desLen = CurrentMessage().GetDesLength( 1 );
       
  1007 
       
  1008 	HBufC8* writeBuf = HBufC8::NewLC( desLen ); 
       
  1009 	TPtr8 initptr = writeBuf->Des();
       
  1010 
       
  1011     Read( 1, initptr );
       
  1012 
       
  1013     CLOG_WRITE_FORMAT( "CDownloadMgrSession::SetString8AttributeL iClientAppInstance %d", iClientAppInstance );
       
  1014     iClientAppInstance->SetStringAttributeL( (THttpDownloadMgrAttrib)attr, initptr );
       
  1015     CleanupStack::PopAndDestroy( writeBuf ); // writeBuf;
       
  1016     }
       
  1017 
       
  1018 /// ---------------------------------------------------------
       
  1019 // CDownloadMgrSession::InitSessionEvent
       
  1020 // ---------------------------------------------------------
       
  1021 //
       
  1022 void CDownloadMgrSession::InitSessionEvent( const RMessage2& aMessage )
       
  1023     {
       
  1024     CLOG_ENTERFN( "CDownloadMgrSession::InitMovedEvent" )
       
  1025     __ASSERT_DEBUG( iEvent == EFalse, PanicClient( EMultipleInitDownloadEvent ) );
       
  1026     iEventMessage = aMessage;
       
  1027     iEvent = ETrue;
       
  1028     }
       
  1029 
       
  1030 /// ---------------------------------------------------------
       
  1031 // CDownloadMgrSession::CancelMovedEvent
       
  1032 // ---------------------------------------------------------
       
  1033 //
       
  1034 void CDownloadMgrSession::CancelMovedEvent()
       
  1035     {
       
  1036     CLOG_ENTERFN( "CDownloadMgrSession::CancelMovedEvent" )
       
  1037     if( iEvent )
       
  1038         {
       
  1039         iEventMessage.Complete( KErrCancel );
       
  1040         iEvent = EFalse;
       
  1041         }
       
  1042     }
       
  1043 
       
  1044 /// ---------------------------------------------------------
       
  1045 // CDownloadMgrSession::RemoveFromQueue
       
  1046 // ---------------------------------------------------------
       
  1047 //
       
  1048 void CDownloadMgrSession::RemoveFromQueue( CHttpDownload* aDownload )
       
  1049     {
       
  1050     iEventQueue->Remove( aDownload );
       
  1051     }
       
  1052 
       
  1053 /// ---------------------------------------------------------
       
  1054 // CDownloadMgrSession::SessionId
       
  1055 // ---------------------------------------------------------
       
  1056 //
       
  1057 TInt CDownloadMgrSession::SessionId() const
       
  1058     {
       
  1059     return iSessionId;
       
  1060     }
       
  1061 
       
  1062 /// ---------------------------------------------------------
       
  1063 // CDownloadMgrSession::ClientAppInstance
       
  1064 // ---------------------------------------------------------
       
  1065 //
       
  1066 CHttpClientAppInstance* CDownloadMgrSession::ClientAppInstance()const
       
  1067     {
       
  1068     return iClientAppInstance;
       
  1069     }