ncdengine/provider/server/src/ncddownloadsuboperation.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32mem.h>
       
    20 #include <f32file.h>
       
    21 
       
    22 #include "ncddownloadsuboperation.h"
       
    23 #include "catalogsbasemessage.h"
       
    24 #include "catalogshttpsession.h"
       
    25 #include "catalogshttpconfig.h"
       
    26 #include "catalogshttpoperation.h"
       
    27 #include "ncdhttpheaders.h"
       
    28 #include "catalogsutils.h"
       
    29 #include "catalogshttpheaders.h"
       
    30 #include "catalogscontext.h"
       
    31 #include "ncdproviderdefines.h"
       
    32 #include "ncdgeneralmanager.h"
       
    33 
       
    34 #include "catalogsdebug.h"
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // NewL
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CNcdDownloadSubOperation* CNcdDownloadSubOperation::NewL( 
       
    43     CNcdGeneralManager& aGeneralManager,
       
    44     MCatalogsHttpSession& aHttpSession,
       
    45     const TDesC& aUri,
       
    46     const TDesC& aDestination,
       
    47     MNcdOperationObserver& aObserver,
       
    48     MCatalogsSession& aSession )
       
    49     {
       
    50     CNcdDownloadSubOperation* self = new( ELeave ) CNcdDownloadSubOperation( 
       
    51         aGeneralManager, 
       
    52         aHttpSession, 
       
    53         aSession );
       
    54     CleanupClosePushL( *self );
       
    55     self->ConstructL( aUri, aDestination, aObserver );
       
    56 
       
    57     CleanupStack::Pop();
       
    58     return self;
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // NewL
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CNcdDownloadSubOperation* CNcdDownloadSubOperation::NewL( 
       
    67     CNcdGeneralManager& aGeneralManager,
       
    68     MCatalogsHttpSession& aHttpSession,
       
    69     MNcdOperationObserver& aObserver,
       
    70     RReadStream& aStream,
       
    71     MCatalogsSession& aSession )
       
    72     {
       
    73     CNcdDownloadSubOperation* self = new( ELeave ) CNcdDownloadSubOperation( 
       
    74         aGeneralManager,
       
    75         aHttpSession, 
       
    76         aSession );
       
    77     CleanupClosePushL( *self );
       
    78     self->ConstructL( aObserver );
       
    79     self->InternalizeL( aStream );
       
    80 
       
    81     CleanupStack::Pop();
       
    82     return self;
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Destructor
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CNcdDownloadSubOperation::~CNcdDownloadSubOperation()
       
    91     {
       
    92     DLTRACEIN( ( "" ) );
       
    93     if ( iDownload ) 
       
    94         {
       
    95         iDownload->Release();
       
    96         iDownload = NULL;
       
    97         }
       
    98         
       
    99     DLTRACEOUT((""));
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // Download config getter
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 MCatalogsHttpConfig& CNcdDownloadSubOperation::Config()
       
   108     {
       
   109     DASSERT( iDownload );
       
   110     return iDownload->Config();
       
   111     }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // Download request header getter
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 MCatalogsHttpHeaders& CNcdDownloadSubOperation::RequestHeaders()
       
   119     {
       
   120     DASSERT( iDownload );
       
   121     return iDownload->Config().RequestHeaders();    
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // Download response header getter
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 const MCatalogsHttpHeaders& CNcdDownloadSubOperation::ResponseHeadersL()
       
   129     {
       
   130     DASSERT( iDownload );
       
   131     return iDownload->ResponseHeadersL();
       
   132     }
       
   133 
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // HttpOperation
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 MCatalogsHttpOperation& CNcdDownloadSubOperation::HttpOperation()
       
   140     {
       
   141     DASSERT( iDownload );
       
   142     return *iDownload;
       
   143     }
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // Start
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 TInt CNcdDownloadSubOperation::Start()
       
   151     {
       
   152     DLTRACEIN((""));
       
   153     TInt ret = iDownload->Start();
       
   154         
       
   155     if ( ret >= KErrNone ) 
       
   156         {
       
   157         DLTRACEOUT((""));
       
   158         return KErrNone;
       
   159         }
       
   160     DLTRACEOUT((""));
       
   161     return ret;
       
   162     }
       
   163 
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Pause
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 TInt CNcdDownloadSubOperation::Pause()
       
   170     {
       
   171     DLTRACEIN((""));
       
   172     TInt err = iDownload->Pause();              
       
   173     if ( err == KErrNone ) 
       
   174         {
       
   175         iDownloadState = ENcdDownloadPaused;
       
   176         }
       
   177     
       
   178     DLTRACEOUT((""));
       
   179     return err;
       
   180     }
       
   181     
       
   182     
       
   183 // ---------------------------------------------------------------------------
       
   184 // Resume
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 TInt CNcdDownloadSubOperation::Resume()
       
   188     {
       
   189     DLTRACEIN((""));
       
   190     TInt err = KErrNone; 
       
   191     
       
   192     if ( iDownload->State() == ECatalogsHttpOpPaused ) 
       
   193         {
       
   194         err = iDownload->Start();                
       
   195         if ( err == KErrNone ) 
       
   196             {
       
   197             iDownloadState = ENcdDownloadInProgress;
       
   198             }
       
   199     
       
   200         }    
       
   201     DLTRACEOUT((""));
       
   202     return err;
       
   203     }
       
   204 
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // Cancel
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 void CNcdDownloadSubOperation::Cancel() 
       
   211     {    
       
   212     DLTRACEIN(( "" ));
       
   213     if ( iDownload ) 
       
   214         {
       
   215         // Cancel also releases the dl
       
   216         iDownload->Cancel();
       
   217         iDownload = NULL;
       
   218         }
       
   219     DLTRACEOUT(( "" ));
       
   220     }
       
   221 
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // HandleHttpEventL
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 void CNcdDownloadSubOperation::HandleHttpEventL( 
       
   228     MCatalogsHttpOperation& aOperation, 
       
   229     TCatalogsHttpEvent aEvent )
       
   230     {
       
   231     DLTRACEIN((( "Event op state: %d, progress state: %d" ), aEvent.iOperationState, aEvent.iProgressState ));
       
   232     (void) aOperation; // suppresses compiler warning
       
   233     DASSERT( &aOperation == iDownload );
       
   234     DASSERT( aOperation.OperationType() == ECatalogsHttpDownload );
       
   235 
       
   236     TCatalogsTransportProgress progress( iDownload->Progress() );
       
   237     
       
   238     iProgress = TNcdSendableProgress( iDownloadState,
       
   239         iDownload->OperationId().Id(), progress.iProgress,
       
   240         progress.iMaxProgress );
       
   241 
       
   242     switch( aEvent.iOperationState ) 
       
   243         {
       
   244         // Handle completed operation
       
   245         case ECatalogsHttpOpCompleted:
       
   246             {
       
   247             DLTRACE(("ECatalogsHttpOpCompleted"));
       
   248             iDownloadState = ENcdDownloadComplete;
       
   249             NotifyObserversComplete( KErrNone );            
       
   250             break;
       
   251             }
       
   252         
       
   253         // Handle operation in progress
       
   254         case ECatalogsHttpOpInProgress:
       
   255             {
       
   256             iDownloadState = ENcdDownloadInProgress;
       
   257             switch( aEvent.iProgressState ) 
       
   258                 {
       
   259                 case ECatalogsHttpResponseBodyReceived:
       
   260                     {            
       
   261                     iOperationState = EStateRunning;
       
   262                     DLTRACE(("Inform observer about progress"));
       
   263                     NotifyObserversProgress();
       
   264                     break;
       
   265                     }
       
   266                 case ECatalogsHttpDisconnected: 
       
   267                     {
       
   268                     DLTRACE(("ECatalogsHttpDisconnected"));
       
   269                     
       
   270                     iError = KErrDisconnected;
       
   271                     iDownloadState = ENcdDownloadFailed;
       
   272                     iOperationState = EStateComplete;   
       
   273                     NotifyObserversComplete( iError );
       
   274                     break;
       
   275                     }
       
   276                 
       
   277                 case ECatalogsHttpStarted:
       
   278                     {
       
   279                     DLTRACE(("ECatatalogsHttpStarted"));
       
   280                     iOperationState = EStateRunning;
       
   281                     iProgress.iState = ENcdDownloadStarted;
       
   282                     NotifyObserversProgress();
       
   283                     break;
       
   284                     }
       
   285                     
       
   286                 default:
       
   287                     {
       
   288                     break;
       
   289                     }
       
   290                 }
       
   291             break;
       
   292             }
       
   293                 
       
   294         default:
       
   295             {
       
   296             break;
       
   297             }
       
   298         }
       
   299     DLTRACEOUT((""));
       
   300     }
       
   301     
       
   302     
       
   303 // ---------------------------------------------------------------------------
       
   304 // HandleHttpError
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 TBool CNcdDownloadSubOperation::HandleHttpError(
       
   308     MCatalogsHttpOperation& aOperation,
       
   309     TCatalogsHttpError aError )
       
   310     {    
       
   311     DLTRACEIN( ( "Error type: %i, id: %i", aError.iType, aError.iError ) );
       
   312     DASSERT( &aOperation == iDownload );
       
   313     
       
   314     if ( aError.iError == KErrCancel ) 
       
   315         {
       
   316         aOperation.Cancel();
       
   317         }
       
   318     else
       
   319         {        
       
   320         aOperation.Release();
       
   321         }
       
   322     iDownload = NULL;
       
   323     iError = aError.iError;
       
   324     iDownloadState = ENcdDownloadFailed;
       
   325     iOperationState = EStateComplete;   
       
   326 
       
   327     // notify observer
       
   328     NotifyObserversComplete( iError );
       
   329     return ETrue;
       
   330     }
       
   331 
       
   332 
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // Externalize
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 void CNcdDownloadSubOperation::ExternalizeL( RWriteStream& aStream ) const
       
   339     {
       
   340     DLTRACEIN((""));
       
   341 
       
   342     aStream.WriteInt32L( iDownloadState );
       
   343     if ( iDownload && 
       
   344          // don't save the download if there's no platform download 
       
   345          // because we can't restore it from download manager anyway
       
   346          iDownload->OperationId().SecondaryId() != KErrNotFound )
       
   347         {
       
   348         // Download exists
       
   349         aStream.WriteInt8L( 1 );
       
   350 
       
   351         // Write operation id so that it can be retrieved from Transport
       
   352         iDownload->OperationId().ExternalizeL( aStream );
       
   353         
       
   354         iDownload->ExternalizeL( aStream );
       
   355         }
       
   356     else
       
   357         {
       
   358         aStream.WriteInt8L( 0 );
       
   359         }
       
   360             
       
   361     }
       
   362 
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // Internalize
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 void CNcdDownloadSubOperation::InternalizeL( RReadStream& aStream )
       
   369     {
       
   370     DLTRACEIN((""));
       
   371     
       
   372     iDownloadState = static_cast<TNcdDownloadState>( aStream.ReadInt32L() );
       
   373     if ( iDownloadState == ENcdDownloadInProgress ) 
       
   374         {
       
   375         DLTRACE(("State was set to in progress, setting as paused"));
       
   376         iDownloadState = ENcdDownloadPaused;
       
   377         }
       
   378         
       
   379     TInt8 downloadExists = aStream.ReadInt8L();
       
   380     if ( downloadExists )
       
   381         {        
       
   382         TCatalogsTransportOperationId opId;
       
   383         opId.InternalizeL( aStream );
       
   384         
       
   385         // Search through restored downloads
       
   386         const RCatalogsHttpOperationArray& downloads = 
       
   387             iHttpSession.RestoredDownloads();
       
   388             
       
   389         for ( TInt i = 0; i < downloads.Count(); ++i )
       
   390             {
       
   391             TCatalogsTransportOperationId compId( downloads[i]->OperationId() );
       
   392             if ( compId.SecondaryId() == opId.SecondaryId() && 
       
   393                  compId.SessionId() == opId.SessionId() ) 
       
   394                 {
       
   395                 DLINFO(("Found a matching download"));
       
   396                 iDownload = downloads[i];
       
   397                 iDownload->InternalizeL( aStream );
       
   398                 iDownload->Config().SetObserver( this );                
       
   399                                 
       
   400                 // Move dl from restored to current dls so that the restored
       
   401                 // dl list can be purged from unnecessary dls afterwards
       
   402                 User::LeaveIfError( iHttpSession.MoveRestoredDlToCurrentDls( 
       
   403                     *iDownload ) );
       
   404                 return;
       
   405                 }
       
   406             }                
       
   407         }        
       
   408     DLERROR(("Download was not found!"));
       
   409     // Download was not found though it should have existed
       
   410     User::Leave( KErrNotFound );
       
   411     
       
   412     }
       
   413 
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // RunOperation
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 TInt CNcdDownloadSubOperation::RunOperation()
       
   420     {
       
   421     DLTRACEIN(( "Pending message: %X", iPendingMessage ));
       
   422     return KErrNone;
       
   423     }
       
   424     
       
   425     
       
   426 // ---------------------------------------------------------------------------
       
   427 // Constructor
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 CNcdDownloadSubOperation::CNcdDownloadSubOperation(
       
   431     CNcdGeneralManager& aGeneralManager,
       
   432     MCatalogsHttpSession& aHttpSession,
       
   433     MCatalogsSession& aSession ) :
       
   434         CNcdBaseOperation( aGeneralManager, NULL, EDownloadSubOperation, aSession ),
       
   435         iHttpSession( aHttpSession )
       
   436     {
       
   437     }
       
   438 
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // ConstructL
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 void CNcdDownloadSubOperation::ConstructL( const TDesC& aUri, 
       
   445     const TDesC& aDestination, MNcdOperationObserver& aObserver )
       
   446     {
       
   447     DLTRACEIN(( _L("URI: %S, Destination: %S"), &aUri, &aDestination ));
       
   448     
       
   449     AddObserverL( &aObserver );
       
   450     
       
   451     // Call ConstructL for the base class
       
   452     CNcdBaseOperation::ConstructL();    
       
   453     
       
   454     // Create the download
       
   455     iDownload = iHttpSession.CreateDownloadL( aUri, EFalse, this );
       
   456     
       
   457     TParsePtrC parse( aDestination );
       
   458         
       
   459     iDownload->Config().SetDirectoryL( parse.DriveAndPath() );
       
   460     
       
   461     TPtrC filename( parse.NameAndExt() );
       
   462     if ( filename.Length() ) 
       
   463         {
       
   464         DLTRACE(( _L("Destination filename: %S"), &filename ));
       
   465         iDownload->Config().SetFilenameL( filename );
       
   466         }
       
   467  
       
   468     iProgress.iProgress = -1;
       
   469     iProgress.iMaxProgress = -1;
       
   470     DLTRACEOUT(( "" ));
       
   471     }
       
   472 
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // ConstructL
       
   476 // ---------------------------------------------------------------------------
       
   477 //
       
   478 void CNcdDownloadSubOperation::ConstructL( MNcdOperationObserver& aObserver )
       
   479     {
       
   480     DLTRACEIN(( "" ));
       
   481     
       
   482     AddObserverL( &aObserver );
       
   483     
       
   484     // Call ConstructL for the base class
       
   485     CNcdBaseOperation::ConstructL();    
       
   486      
       
   487     iProgress.iProgress = -1;
       
   488     iProgress.iMaxProgress = -1;
       
   489     DLTRACEOUT(( "" ));
       
   490     }
       
   491     
       
   492     
       
   493 // ---------------------------------------------------------------------------
       
   494 // NotifyObserversComplete
       
   495 // ---------------------------------------------------------------------------
       
   496 //
       
   497 void CNcdDownloadSubOperation::NotifyObserversComplete( TInt aError )
       
   498     {
       
   499     Open(); // increase ref count in CCatalogsCommunicable
       
   500     for( TInt i = 0; i < iObservers.Count(); ++i ) 
       
   501         {
       
   502         iObservers[i]->OperationComplete( this, aError );
       
   503         }
       
   504     Close();
       
   505     }
       
   506 
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // NotifyObserversProgress
       
   510 // ---------------------------------------------------------------------------
       
   511 //
       
   512 void CNcdDownloadSubOperation::NotifyObserversProgress()
       
   513     {
       
   514     Open(); // increase ref count in CCatalogsCommunicable
       
   515     for( TInt i = 0; i < iObservers.Count(); ++i ) 
       
   516         {
       
   517         iObservers[i]->Progress( *this );
       
   518         }
       
   519     Close();
       
   520     }