webengine/osswebengine/WebCore/platform/network/symbian/HttpDownload.cpp
changeset 0 dd21522fd290
child 17 c8a366e56285
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of 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 *
       
    16 */
       
    17 
       
    18 #include "HttpDownload.h"
       
    19 #include <CDownloadMgrUiUserInteractions.h>
       
    20 #include <CDownloadMgrUiDownloadsList.h>
       
    21 #include "HttpDlConnection.h"
       
    22 #include "HttpDefs.h"
       
    23 #include "HttpSessionManager.h"
       
    24 #include "brctl.h"
       
    25 #include "StaticObjectsContainer.h"
       
    26 
       
    27 // CONSTANTS
       
    28 const unsigned long KMinDataSizeToSend = (1024*5);
       
    29 #define PREPARE_DOWNLOAD_MANAGER  if(!m_dMgrReady) { initDownloadMgrL(); m_dMgrReady = true; }
       
    30 
       
    31 using namespace WebCore;
       
    32 
       
    33 HttpDownload::HttpDownload(HttpSessionManager* sessionManager)
       
    34 {
       
    35     m_sessionManager = sessionManager;
       
    36     m_dMgrReady = false;
       
    37     m_dMgrUiReg = NULL;
       
    38 	m_downloadObserver = StaticObjectsContainer::instance()->brctl()->brCtlDownloadObserver();
       
    39     TRAP_IGNORE(PREPARE_DOWNLOAD_MANAGER)
       
    40 }
       
    41 
       
    42 HttpDownload::~HttpDownload()
       
    43 {
       
    44     delete m_dMgrUiReg;
       
    45     if( m_dMgrReady ){
       
    46         m_downloadMgr.Close();
       
    47         m_dMgrReady = false;
       
    48         }
       
    49 }
       
    50 
       
    51 void HttpDownload::initDownloadMgrL()
       
    52 {
       
    53     if( m_dMgrReady ) return;
       
    54     // get capatabilities
       
    55     unsigned int capability = StaticObjectsContainer::instance()->capabilities();
       
    56     // initialize m_suppressDloadConfirmation
       
    57     if (capability & TBrCtlDefs::ECapabilityUseDlMgr){
       
    58         m_suppressDloadConfirmation = true;
       
    59         if (capability & TBrCtlDefs::ECapabilityConfirmDownloads){
       
    60             m_suppressDloadConfirmation = false;
       
    61         }
       
    62     }
       
    63     // initialize m_launchViewer
       
    64     m_launchViewer = false;
       
    65     if (capability & TBrCtlDefs::ECapabilityLaunchViewer){
       
    66         m_launchViewer = true;
       
    67     }
       
    68     int master = !m_sessionManager->m_embedded;
       
    69     RProcess myProcess;
       
    70     m_downloadMgr.ConnectL( myProcess.Identity(), *this, master );
       
    71     if( !master ){
       
    72         // Browser is embedded. Downloads must be paused when the user closes
       
    73         // the embedded Browser (that is always started from the Launcher), and
       
    74         // CDownloadMgrUiUserInteractions::OkToExitL() must not be called.
       
    75         // This behaviour can be indicated to DMgr before hand:
       
    76         User::LeaveIfError( m_downloadMgr.SetIntAttribute( EDlMgrExitAction, EExitPause ) );
       
    77     }
       
    78     // Cookie usage setting
       
    79     m_downloadMgr.SetBoolAttribute( EDlMgrEnableCookies, m_sessionManager->m_cookiesEnabled );
       
    80     // Auto-connection is disabled
       
    81     m_downloadMgr.SetBoolAttribute( EDlMgrAutoConnect, EFalse );
       
    82 
       
    83     #ifdef __BROWSER_SDK
       
    84     m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, m_launchViewer ? ELaunch : EDoNothing );
       
    85     #else
       
    86     if ( !m_launchViewer ){
       
    87         m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, EDoNothing );
       
    88     }
       
    89     else if ( m_sessionManager->m_autoOpenDownloads) {
       
    90         m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, EMove + EPdLaunch);
       
    91     }
       
    92     else {
       
    93         m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, EMove);         
       
    94     }
       
    95     #endif
       
    96     m_downloadMgr.SetNextUriObserver((MHttpDownloadMgrNextUriObserver*) this );
       
    97     initDownloadMgrUiLibL();
       
    98 
       
    99     // go through the download list to define outstanding downloads.
       
   100     // If it exist send the ResumeDownloadL() to the host appl
       
   101     CDownloadMgrUiUserInteractions* dloadMgrUiUserInteractions = &m_dMgrUiReg->UserInteractions();
       
   102     TRAP_IGNORE (dloadMgrUiUserInteractions->SetBoolAttributeL( CDownloadMgrUiUserInteractions::EAttrSuppressDownloadConfirmation,
       
   103                                                                 m_suppressDloadConfirmation ));
       
   104     RHttpDownload* dl = NULL;
       
   105     const CDownloadArray& dls = m_downloadMgr.CurrentDownloads();
       
   106     int downloadCnt = dls.Count();
       
   107     for( int i = 0; i < downloadCnt; ++i )
       
   108     {
       
   109         dl = dls.At(i); // current download
       
   110         if(dl == NULL){
       
   111             User::Leave(KErrGeneral);
       
   112         }
       
   113         // Download Manager Transaction ID
       
   114         long transID = 0;
       
   115         dl->GetIntAttribute(EDlAttrId, transID);
       
   116 
       
   117         // Download Manager total Length of the download
       
   118         long dlLength = 0;
       
   119         dl->GetIntAttribute(EDlAttrLength, dlLength);
       
   120 
       
   121         // local filename
       
   122         HBufC16* fileName = HBufC16::NewLC( KMaxPath );
       
   123         TPtr fileNameDes( fileName->Des() );
       
   124         dl->GetStringAttribute( EDlAttrDestFilename, fileNameDes );
       
   125         // received content type
       
   126         HBufC* contentType = HBufC::NewLC( KMaxContentTypeLength );
       
   127         TPtr contentTypeBuf( contentType->Des() );
       
   128         dl->GetStringAttribute( EDlAttrContentType, contentTypeBuf );
       
   129 
       
   130         //request url
       
   131         HBufC* url = HBufC::NewLC( KMaxUrlLength );
       
   132         TPtr urlBuf( url->Des() );
       
   133         dl->GetStringAttribute( EDlAttrCurrentUrl, urlBuf );
       
   134 
       
   135         // notify Observer of resuming download
       
   136         m_downloadObserver->ResumeDownloadL( transID, dlLength, fileNameDes,
       
   137                                                 contentTypeBuf, urlBuf);
       
   138         CleanupStack::PopAndDestroy(3);
       
   139     }
       
   140     m_dMgrReady = true;
       
   141 }
       
   142 
       
   143 void HttpDownload::continueDownloadL(RHTTPTransaction* connTransaction, HttpDlConnection* dlConnection)
       
   144 {
       
   145     // initialize download manager if it has not been initialized
       
   146     PREPARE_DOWNLOAD_MANAGER
       
   147     // Query HTTP transaction
       
   148     int transaction( REINTERPRET_CAST( int, connTransaction ) );
       
   149     int isNewDl = 1;
       
   150     long id;
       
   151     RHttpDownload& dl = m_downloadMgr.CreateClientSideDownloadL(transaction, isNewDl );
       
   152     dl.GetIntAttribute( EDlAttrId,id );
       
   153     dlConnection->setDownloadId( id );
       
   154     User::LeaveIfError( dl.Start() );    
       
   155 }
       
   156 
       
   157 void HttpDownload::initDownloadMgrUiLibL()
       
   158 {
       
   159     m_dMgrUiReg = CDownloadMgrUiLibRegistry::NewL( m_downloadMgr );
       
   160     m_dMgrUiReg->RegisterDownloadsListL();
       
   161     m_dMgrUiReg->RegisterUserInteractionsL();
       
   162 }
       
   163 
       
   164 //HandleDMgrEventL methodes
       
   165 void HttpDownload::handleDMgrEventDlCompletedL(RHttpDownload& download)
       
   166 {
       
   167     // Download Manager User Data
       
   168     long userData;
       
   169     // Download Manager Transaction ID
       
   170     long transID( 0 );
       
   171     download.GetIntAttribute( EDlAttrId, transID );
       
   172 
       
   173     // Download Manager total Length of the download
       
   174     long dlLength( 0 );
       
   175     download.GetIntAttribute( EDlAttrLength, dlLength );
       
   176 
       
   177     download.GetIntAttribute(EDlAttrUserData, userData);
       
   178     // check if the NewDownloadL() callback has been sent,
       
   179     // if not send it before EDownloadEventCompleted will be send
       
   180     if (!userData){
       
   181         // local filename
       
   182         HBufC16* fileName = HBufC16::NewLC( KMaxPath );
       
   183         TPtr fileNameDes( fileName->Des() );
       
   184         download.GetStringAttribute( EDlAttrDestFilename, fileNameDes );
       
   185 
       
   186         // received content type
       
   187         HBufC* contentType = HBufC::NewLC( KMaxContentTypeLength );
       
   188         TPtr contentTypeBuf( contentType->Des() );
       
   189         download.GetStringAttribute( EDlAttrContentType, contentTypeBuf );
       
   190 
       
   191         //request url
       
   192         HBufC* url = HBufC::NewLC( KMaxUrlLength );
       
   193         TPtr urlBuf( url->Des() );
       
   194         download.GetStringAttribute( EDlAttrCurrentUrl, urlBuf );
       
   195         // Return ETrue if the file is handled progressively, false otherwise
       
   196         m_downloadObserver->NewDownloadL( transID, fileNameDes, contentTypeBuf, urlBuf );
       
   197         // notify Observer of started download
       
   198         m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventStarted, dlLength);
       
   199         CleanupStack::PopAndDestroy(3);
       
   200 
       
   201         // Download Manager User Data
       
   202         userData = 1;
       
   203         download.SetIntAttribute(EDlAttrUserData, userData);
       
   204         }
       
   205     // notify Observer of completed download
       
   206     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventCompleted, dlLength );
       
   207     handleDMgrCompletedEventL( download, EHttpDlCompleted );
       
   208 }
       
   209 
       
   210 void HttpDownload::handleDMgrEventDlFailedL(RHttpDownload& download)
       
   211 {
       
   212     // Download Manager Transaction ID
       
   213     long transID( 0 );
       
   214     download.GetIntAttribute( EDlAttrId, transID );
       
   215 
       
   216     // notify Observer of failed download
       
   217     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventError, 0 );
       
   218     if ( numOfDownloads() == 0 ){
       
   219         m_downloadMgr.SetIntAttribute( EDlMgrHasActiveDownloads, 0 );
       
   220         }
       
   221 }
       
   222 
       
   223 void HttpDownload::handleDMgrEventDlDeletingL(RHttpDownload& download)
       
   224 {
       
   225     // Download Manager Transaction ID
       
   226     long transID( 0 );
       
   227     download.GetIntAttribute(EDlAttrId, transID);
       
   228 
       
   229     // Download Manager total Length of the download
       
   230     long dlLength( 0 );
       
   231     download.GetIntAttribute(EDlAttrLength, dlLength);
       
   232 
       
   233     // notify Observer of paused download
       
   234     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventCanceled, dlLength);
       
   235     unsigned int noActiveDownloads = numOfDownloads() - 1;
       
   236     if ( noActiveDownloads == 0 ){
       
   237         m_downloadMgr.SetIntAttribute( EDlMgrHasActiveDownloads, 0 );
       
   238     }
       
   239 }
       
   240 
       
   241 void HttpDownload::handleDMgrEventDlPausableL(RHttpDownload& download)
       
   242 {
       
   243     // Download Manager Transaction ID
       
   244     long transID( 0 );
       
   245     download.GetIntAttribute(EDlAttrId, transID);
       
   246     // Download Manager total Length of the download
       
   247     long dlLength( 0 );
       
   248     download.GetIntAttribute(EDlAttrLength, dlLength);
       
   249     // notify Observer of paused download
       
   250     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventPausable, ETrue);
       
   251 }
       
   252 
       
   253 void HttpDownload::handleDMgrEventDlNonPausableL(RHttpDownload& download)
       
   254 {
       
   255     // Download Manager Transaction ID
       
   256     long transID( 0 );
       
   257     download.GetIntAttribute(EDlAttrId, transID);
       
   258     // Download Manager total Length of the download
       
   259     long dlLength( 0 );
       
   260     download.GetIntAttribute(EDlAttrLength, dlLength);
       
   261     // notify Observer of paused download
       
   262     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventPausable, EFalse);
       
   263 }
       
   264 
       
   265 void HttpDownload::handleDMgrEventDlPausedL(RHttpDownload& download)
       
   266 {
       
   267     // Download Manager Transaction ID
       
   268     long transID( 0 );
       
   269     download.GetIntAttribute(EDlAttrId, transID);
       
   270     // Download Manager total Length of the download
       
   271     long dlLength( 0 );
       
   272     download.GetIntAttribute(EDlAttrLength, dlLength);
       
   273     // notify Observer of paused download
       
   274     m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventPaused, dlLength);
       
   275 }
       
   276 
       
   277 void HttpDownload::handleDMgrEventProgConnectionNeededL(RHttpDownload& download)
       
   278 {
       
   279     int connected( 0 );
       
   280     if( !m_downloadMgr.GetBoolAttribute( EDlMgrConnected,connected ) ){
       
   281         if( !connected ){
       
   282             // Connection is needed in DMgr.
       
   283             // Now call ConnectionManager to ask if user wants to create a new one.
       
   284             // After that pass the newly created connection's name to DMgr, that
       
   285             // will automatically attaches to the connection.
       
   286             int connectionPtr( 0 );
       
   287             int sockSvrHandle;
       
   288             int newConn;
       
   289             int err;
       
   290             TApBearerType bearerType;
       
   291             err = m_sessionManager->m_uiCallbacks.CreateConnection( &connectionPtr, &sockSvrHandle, &newConn, &bearerType );
       
   292             if( err == KErrNone ){
       
   293                 TName name;
       
   294                 RConnection* connPtr = REINTERPRET_CAST( RConnection*, connectionPtr );
       
   295                 connPtr->Name( name );
       
   296                 connect( name );
       
   297             }
       
   298             else{
       
   299                 // User doesn't want to make a connection, or connection failed.
       
   300                 download.Pause();
       
   301             }
       
   302         }
       
   303     }
       
   304 }
       
   305 
       
   306 void HttpDownload::handleDMgrEventProgResponseBodyReceivedL(RHttpDownload& download)
       
   307 {
       
   308     // Download Manager Transaction ID
       
   309     long transID( 0 );
       
   310     long userData( 0 );
       
   311     download.GetIntAttribute( EDlAttrId, transID );
       
   312     download.GetIntAttribute( EDlAttrUserData, userData );
       
   313     // Download Manager already downloaded content size
       
   314     long downloadedSize( 0 );
       
   315     download.GetIntAttribute( EDlAttrDownloadedSize, downloadedSize );
       
   316     if( userData ) {// already sent out EDownloadEventStarted event
       
   317         // notify Observer of download in progress
       
   318         m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventProgress, downloadedSize );
       
   319     }
       
   320     else {// did not sent EDownloadEventStarted yet and NewDownloadL.
       
   321         // don't call NewDownloadL() until more than 5KB of data available
       
   322         if (downloadedSize > KMinDataSizeToSend) {
       
   323             // Download Manager total Length of the download
       
   324             long dlLength( 0 );
       
   325             download.GetIntAttribute( EDlAttrLength, dlLength );
       
   326             // local filename
       
   327             HBufC16* fileName = HBufC16::NewLC( KMaxPath );
       
   328             TPtr fileNameDes( fileName->Des() );
       
   329             download.GetStringAttribute( EDlAttrDestFilename, fileNameDes );
       
   330             // received content type
       
   331             HBufC* contentType = HBufC::NewLC( KMaxContentTypeLength );
       
   332             TPtr contentTypeBuf( contentType->Des() );
       
   333             download.GetStringAttribute( EDlAttrContentType, contentTypeBuf );
       
   334             //request url
       
   335             HBufC* url = HBufC::NewLC( KMaxUrlLength );
       
   336             TPtr urlBuf( url->Des() );
       
   337             download.GetStringAttribute( EDlAttrCurrentUrl, urlBuf );
       
   338             // Return ETrue if the file is handled progressively, EFalse otherwise
       
   339             int progDl = m_downloadObserver->NewDownloadL( transID, fileNameDes, contentTypeBuf, urlBuf );
       
   340             int isProgDl = 0;
       
   341             download.GetBoolAttribute( EDlAttrProgressive, isProgDl );
       
   342             if ( !isProgDl ){
       
   343                 download.SetBoolAttribute( EDlAttrProgressive, (long)progDl );
       
   344             }   
       
   345             // notify Observer of started download
       
   346             m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventStarted, dlLength );
       
   347             CleanupStack::PopAndDestroy( 3 );
       
   348             // Download Manager User Data
       
   349             userData = 1;
       
   350             download.SetIntAttribute( EDlAttrUserData, userData );
       
   351         }
       
   352     }
       
   353 }
       
   354 
       
   355 void HttpDownload::handleDMgrEventProgResponseHeaderReceivedL(RHttpDownload& download)
       
   356 {
       
   357     long value(0);
       
   358     download.GetIntAttribute(EDlAttrStatusCode, value);
       
   359 
       
   360     if (value == EPartialContent){
       
   361         // Download Manager Transaction ID
       
   362         long transID( 0 );
       
   363         download.GetIntAttribute(EDlAttrId, transID);
       
   364 
       
   365         // Download Manager total Length of the download
       
   366         long dlLength( 0 );
       
   367         download.GetIntAttribute(EDlAttrLength, dlLength);
       
   368 
       
   369         // notify Observer of resuming download
       
   370         m_downloadObserver->HandleDownloadEventL( transID, EDownloadEventResumed, dlLength);
       
   371     }
       
   372     if ( numOfDownloads() ){
       
   373         m_downloadMgr.SetIntAttribute( EDlMgrHasActiveDownloads, 1 );
       
   374     }
       
   375 }
       
   376 
       
   377 void HttpDownload::HandleDMgrEventL(RHttpDownload& download, THttpDownloadEvent event )
       
   378 {
       
   379     PREPARE_DOWNLOAD_MANAGER
       
   380     // Download Manager User Data
       
   381     long userData;
       
   382     switch (event.iDownloadState)
       
   383         {
       
   384         case EHttpDlCompleted:
       
   385             {
       
   386             handleDMgrEventDlCompletedL(download);
       
   387             break;
       
   388             }
       
   389 
       
   390         case EHttpDlFailed:
       
   391             {
       
   392             handleDMgrEventDlFailedL(download);
       
   393             break;
       
   394             }
       
   395 
       
   396         case EHttpDlDeleting:
       
   397             {
       
   398             handleDMgrEventDlDeletingL(download);
       
   399             break;
       
   400             }
       
   401 
       
   402         case EHttpDlPausable:
       
   403             {
       
   404             handleDMgrEventDlPausableL(download);
       
   405             break;
       
   406             }
       
   407 
       
   408         case EHttpDlNonPausable:
       
   409             {
       
   410             handleDMgrEventDlNonPausableL(download);
       
   411             break;
       
   412             }
       
   413 
       
   414         case EHttpDlPaused:
       
   415             {
       
   416             handleDMgrEventDlPausedL(download);
       
   417             break;
       
   418             }
       
   419 
       
   420         case EHttpDlInprogress:
       
   421             {
       
   422             switch (event.iProgressState )
       
   423                 {
       
   424                 case EHttpProgConnectionNeeded:
       
   425                     {
       
   426                     handleDMgrEventProgConnectionNeededL(download);
       
   427                     break;
       
   428                     }
       
   429                 case EHttpProgResponseBodyReceived:
       
   430                     {
       
   431                     handleDMgrEventProgResponseBodyReceivedL(download);
       
   432                     break;
       
   433                     }
       
   434                // when download is resumed send ResumeDownloadL() to the host
       
   435                 case EHttpProgResponseHeaderReceived:
       
   436                     {
       
   437                     handleDMgrEventProgResponseHeaderReceivedL(download);
       
   438                     break;
       
   439                     }
       
   440                 default:
       
   441                     {
       
   442                     break;
       
   443                     }
       
   444                 } //switch (aEvent.iProgressState)
       
   445             break;
       
   446             }
       
   447         default:
       
   448             {
       
   449             break;
       
   450             }
       
   451         } // switch (aEvent.iDownloadState)
       
   452 }
       
   453 
       
   454 unsigned HttpDownload::numOfDownloads()
       
   455 {
       
   456     // this feature is not supported. should be fixed on
       
   457     // the download manager side.
       
   458     // waste of memory to init download manager just for
       
   459     // the sake of number of downloads.
       
   460     if( !m_dMgrReady ){
       
   461         return 0;
       
   462     }
       
   463     long noMediaDownloads( 0 );
       
   464     m_downloadMgr.GetIntAttribute( EDlMgrNoMediaDls, noMediaDownloads );
       
   465     // Return value is ignored as it is a non leaving function!
       
   466     // If it returns an error, then 'noMediaDownloads' is considered 0.
       
   467     return( m_dMgrUiReg->DownloadCount() - noMediaDownloads );
       
   468 }
       
   469 
       
   470 HttpDlConnection* HttpDownload::findDownloadConnection (long dlId, int& position)
       
   471 {
       
   472     // check if this connection is in the list
       
   473     HttpDlConnection* dlConnection = NULL;
       
   474     int index = 0;
       
   475     int size = m_sessionManager->m_pendingHttpDownloadRequests.size();
       
   476     while (index < size){
       
   477         dlConnection = m_sessionManager->m_pendingHttpDownloadRequests[index];
       
   478         if (dlConnection->downloadId() == dlId){
       
   479             position = index;
       
   480             break;
       
   481         }
       
   482         index++;
       
   483     }
       
   484     return dlConnection;
       
   485 }
       
   486 
       
   487 void HttpDownload::CreateDownloadArraysLC(RHttpDownload& download, RArray<TUint>*& typeArray, CDesCArrayFlat*& desArray )
       
   488 {
       
   489     PREPARE_DOWNLOAD_MANAGER
       
   490     // these arrays are to be freed by the download manager
       
   491     typeArray = new (ELeave) RArray<TUint>(4);
       
   492     CleanupStack::PushL( typeArray );
       
   493     desArray = new (ELeave) CDesCArrayFlat(4);
       
   494     CleanupStack::PushL( desArray );
       
   495     // request url
       
   496     HBufC* url = HBufC::NewLC( KMaxUrlLength );
       
   497     TPtr urlBuf( url->Des() );
       
   498     download.GetStringAttribute( EDlAttrCurrentUrl, urlBuf );
       
   499 
       
   500     typeArray->AppendL( EParamRequestUrl );
       
   501     desArray->AppendL( urlBuf );
       
   502     CleanupStack::PopAndDestroy( url );
       
   503     // total content length
       
   504     long contentLength;
       
   505     download.GetIntAttribute( EDlAttrLength, contentLength );
       
   506     HBufC* lengthBuf = HBufC::NewLC( 10 );  // it's enough for a TInt32
       
   507     lengthBuf->Des().Format( _L("%d"), contentLength );
       
   508 
       
   509     typeArray->AppendL( EParamTotalContentLength );
       
   510     desArray->AppendL( *lengthBuf );
       
   511     CleanupStack::PopAndDestroy( lengthBuf );
       
   512     // received content type
       
   513     HBufC* contentType = HBufC::NewLC( KMaxContentTypeLength );
       
   514     TPtr contentTypeBuf( contentType->Des() );
       
   515     download.GetStringAttribute( EDlAttrContentType, contentTypeBuf );
       
   516     typeArray->AppendL( EParamReceivedContentType );
       
   517     desArray->AppendL( contentTypeBuf );
       
   518     CleanupStack::PopAndDestroy( contentType );
       
   519     // referer header
       
   520     HBufC16* referer = HBufC16::NewLC( KMaxUrlLength );
       
   521     TPtr refererDes( referer->Des() );
       
   522     download.GetStringAttribute( EDlAttrRequestReferer, refererDes );
       
   523     typeArray->AppendL( EParamRefererHeader );
       
   524     desArray->AppendL( refererDes );
       
   525     CleanupStack::PopAndDestroy( referer );
       
   526     // charset
       
   527     HBufC16* charSet = HBufC16::NewLC( KMaxDefAttrLength );
       
   528     TPtr charsetDes( charSet->Des() );
       
   529     download.GetStringAttribute( EDlAttrRequestReferer, charsetDes );
       
   530     typeArray->AppendL( EParamCharset );
       
   531     desArray->AppendL( charsetDes );
       
   532     CleanupStack::PopAndDestroy( charSet );
       
   533     // local filename
       
   534     HBufC16* fileName = HBufC16::NewLC( KMaxPath );
       
   535     TPtr fileNameDes( fileName->Des() );
       
   536     download.GetStringAttribute( EDlAttrDestFilename, fileNameDes );
       
   537     typeArray->AppendL( EParamLocalFileName );
       
   538     desArray->AppendL( fileNameDes );
       
   539     CleanupStack::PopAndDestroy( fileName );
       
   540     // Download Manager Transaction ID
       
   541     long transID( 0 );
       
   542     download.GetIntAttribute(EDlAttrId, transID);
       
   543     HBufC* tranIdBuf = HBufC::NewLC( 10 );  // it's enough for a TInt32
       
   544     tranIdBuf->Des().Format( _L("%d"), transID);
       
   545     typeArray->AppendL( EParamTransactionId );
       
   546     desArray->AppendL( *tranIdBuf );
       
   547     CleanupStack::PopAndDestroy( tranIdBuf );
       
   548 }
       
   549 
       
   550 void HttpDownload::handleDMgrCompletedEventL(RHttpDownload& download, TInt event)
       
   551 {
       
   552     PREPARE_DOWNLOAD_MANAGER
       
   553 
       
   554     if( !m_launchViewer && event == EHttpDlCompleted ){
       
   555         // create arrays
       
   556         RArray<TUint>* typeArray = NULL;
       
   557         CDesCArrayFlat* desArray = NULL;
       
   558         CreateDownloadArraysLC( download, typeArray, desArray );
       
   559         // use download manager
       
   560         MBrCtlSpecialLoadObserver* loadObserver = StaticObjectsContainer::instance()->brctl()->brCtlSpecialLoadObserver();
       
   561         bool downloadHandled = (loadObserver && loadObserver->HandleDownloadL(typeArray, desArray));
       
   562         if ( !downloadHandled ){
       
   563             m_dMgrUiReg->UserInteractions().HandleDownloadL(download);
       
   564         }
       
   565         // cleanup arrays
       
   566         if( desArray ){
       
   567             desArray->Reset();
       
   568             CleanupStack::PopAndDestroy(); // desArray
       
   569         }
       
   570         if( typeArray ){
       
   571             typeArray->Close();
       
   572             CleanupStack::PopAndDestroy(); // typeArray
       
   573         }
       
   574     }
       
   575 
       
   576     long dlId( KErrNotFound );
       
   577     download.GetIntAttribute( EDlAttrId, dlId );
       
   578     int position = 0;
       
   579     if( dlId != KErrNotFound ){
       
   580         HttpDlConnection* dlConnection = findDownloadConnection(dlId, position);
       
   581 		if (dlConnection) {
       
   582 			// check if we need to keep pipelining off
       
   583 			long noActiveDownloads = 0;
       
   584 			m_downloadMgr.GetIntAttribute( EDlMgrNumInprogressDownloads, noActiveDownloads );
       
   585 			if( noActiveDownloads == 0 ){
       
   586 				// set pipelining
       
   587 				m_sessionManager->m_httpPipelining = true;
       
   588 			}
       
   589 			// remove connection from the download queue
       
   590             m_sessionManager->removeDlRequest(dlConnection);
       
   591 			delete dlConnection;
       
   592 			dlConnection = NULL;
       
   593 		}
       
   594     }
       
   595     if ( numOfDownloads() == 0 ){
       
   596         m_downloadMgr.SetIntAttribute( EDlMgrHasActiveDownloads, 0 );
       
   597     }
       
   598 }
       
   599 
       
   600 void HttpDownload::connect(TName& name )
       
   601 {
       
   602     TRAP_IGNORE(PREPARE_DOWNLOAD_MANAGER)
       
   603     int err( KErrNone );
       
   604     if( !name.Length() ){
       
   605         int conn( 0 );
       
   606         int dummy;
       
   607         int newConn;
       
   608         int err;
       
   609         TApBearerType bearerType;
       
   610         err = m_sessionManager->m_uiCallbacks.CreateConnection( &conn, &dummy, &newConn, &bearerType );
       
   611         if( err == KErrNone ){
       
   612             RConnection* connPtr = REINTERPRET_CAST( RConnection*, conn );
       
   613             connPtr->Name( name );
       
   614         }
       
   615     }
       
   616     if( err == KErrNone ){
       
   617         m_downloadMgr.SetStringAttribute( EDlMgrConnectionName, name );
       
   618     }
       
   619 }
       
   620 
       
   621 void HttpDownload::disconnect()
       
   622 {
       
   623     if( !m_dMgrReady ) return;
       
   624     m_downloadMgr.Disconnect();
       
   625 }
       
   626 
       
   627 void HttpDownload::ShowDownloadsL()
       
   628 {
       
   629     // this feature is not supported. should be fixed on
       
   630     // the download manager side.
       
   631     // waste of memory to init download manager just for
       
   632     // the sake of number of downloads.
       
   633     if( !m_dMgrReady ){
       
   634         return;
       
   635     }
       
   636     CDownloadMgrUiDownloadsList& dloadlist = m_dMgrUiReg->DownloadsList();
       
   637     if( !dloadlist.IsVisible() ){
       
   638         dloadlist.DisplayDownloadsListL();
       
   639     }
       
   640 }
       
   641 
       
   642 bool HttpDownload::okToExit()
       
   643 {
       
   644     // don't need to go futher if download manger is not up
       
   645     if( !m_dMgrReady ) return ETrue;
       
   646     bool dmgrexit( false );
       
   647     CDownloadMgrUiUserInteractions& dloadMgrUiUserInteractions = m_dMgrUiReg->UserInteractions();
       
   648     TRAP_IGNORE( dmgrexit = dloadMgrUiUserInteractions.OkToExitL() );
       
   649     return dmgrexit;
       
   650 }
       
   651 
       
   652 void HttpDownload::prepareToExit(const long appUid, const long viewId, const long customMessageId )
       
   653 {
       
   654     // don't need to go futher if download manger is not up
       
   655     if( !m_dMgrReady ) return;
       
   656     m_dMgrUiReg->UserInteractions().PrepareToExit( appUid, viewId, customMessageId );
       
   657 }
       
   658 
       
   659 void HttpDownload::HandleDownloadCommandL(unsigned int transId, TBrCtlDefs::TBrCtlDownloadCmd command)
       
   660 {
       
   661     PREPARE_DOWNLOAD_MANAGER
       
   662 
       
   663     RHttpDownload* dl = NULL;
       
   664     const CDownloadArray& dls = m_downloadMgr.CurrentDownloads();
       
   665     int downloadCnt = dls.Count();
       
   666     long dlTransID(0);
       
   667     if ( downloadCnt > 0){
       
   668         for( int i = 0; i < downloadCnt; ++i )
       
   669         {
       
   670         dl = dls.At(i); // current download
       
   671         if(dl == NULL){
       
   672             User::Leave(KErrGeneral);
       
   673         }
       
   674         dl->GetIntAttribute(EDlAttrId, dlTransID);
       
   675         if ( dlTransID == transId){
       
   676             break;
       
   677             }
       
   678         }
       
   679         switch (command)
       
   680         {
       
   681             case TBrCtlDefs::EDownloadCmdPause:
       
   682                 {
       
   683                     dl->Pause();
       
   684                     break;
       
   685                 }
       
   686             case TBrCtlDefs::EDownloadCmdResume:
       
   687                 {
       
   688                     dl->Start();
       
   689                     break;
       
   690                 }
       
   691             case TBrCtlDefs::EDownloadCmdCancel:
       
   692                 {
       
   693                     dl->Delete();
       
   694                     break;
       
   695                 }
       
   696             case TBrCtlDefs::EDownloadCmdMarkAsProgressive:
       
   697                 {
       
   698                     dl->SetBoolAttribute(EDlAttrProgressive, ETrue);
       
   699                     break;
       
   700                 }
       
   701             case TBrCtlDefs::EDownloadCmdMarkAsNotProgressive:
       
   702                 {
       
   703                     dl->SetBoolAttribute(EDlAttrProgressive, EFalse);
       
   704                     break;
       
   705                 }
       
   706             default:
       
   707                 break;
       
   708         } //swith command
       
   709     }
       
   710 }
       
   711 
       
   712 void HttpDownload::enableCookies(int enable )
       
   713 {
       
   714     //this macro can leave
       
   715     TRAP_IGNORE(PREPARE_DOWNLOAD_MANAGER)
       
   716     m_downloadMgr.SetBoolAttribute( EDlMgrEnableCookies, enable );
       
   717 }
       
   718 
       
   719 void HttpDownload::NextUriL( RHttpDownload& /*aDownload*/, const TDesC8& uri )
       
   720 {
       
   721     m_sessionManager->m_uiCallbacks.NextUriL(uri, StaticObjectsContainer::instance()->brctl());
       
   722 }
       
   723 
       
   724 void HttpDownload::updateDownloadsOpenEnabled()
       
   725 {
       
   726     TRAP_IGNORE(PREPARE_DOWNLOAD_MANAGER)
       
   727 #ifdef __BROWSER_SDK
       
   728     m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, m_launchViewer ? ELaunch : EDoNothing );
       
   729 #else
       
   730     if (m_sessionManager->m_autoOpenDownloads){
       
   731         m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, EMove + EPdLaunch);     
       
   732         }
       
   733     else {
       
   734         m_downloadMgr.SetDefaultIntAttribute( EDlAttrAction, EMove);         
       
   735         }
       
   736 #endif
       
   737 }
       
   738 // end of file