ncdengine/provider/server/src/ncddescriptordownloadsuboperation.cpp
changeset 4 32704c33136d
equal deleted inserted replaced
-1:000000000000 4:32704c33136d
       
     1 /*
       
     2 * Copyright (c) 2006-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 "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 "ncddescriptordownloadsuboperation.h"
       
    23 #include "catalogsbasemessage.h"
       
    24 #include "catalogshttpsession.h"
       
    25 #include "catalogshttpconfig.h"
       
    26 #include "catalogshttpoperation.h"
       
    27 #include "catalogsdebug.h"
       
    28 #include "ncdhttpheaders.h"
       
    29 #include "catalogsutils.h"
       
    30 #include "catalogshttpheaders.h"
       
    31 #include "catalogscontext.h"
       
    32 #include "ncdproviderdefines.h"
       
    33 #include "ncdgeneralmanager.h"
       
    34 
       
    35 
       
    36 const TInt KBodyExpandSize = 4096;
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // NewL
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CNcdDescriptorDownloadSubOperation* CNcdDescriptorDownloadSubOperation::NewL( 
       
    45     CNcdGeneralManager& aGeneralManager,
       
    46     MCatalogsHttpSession& aHttpSession,
       
    47     const TDesC& aUri,
       
    48     MNcdOperationObserver& aObserver,
       
    49     MCatalogsSession& aSession )
       
    50     {
       
    51     CNcdDescriptorDownloadSubOperation* self = new( ELeave ) 
       
    52         CNcdDescriptorDownloadSubOperation( 
       
    53             aGeneralManager, aHttpSession, aSession );
       
    54     CleanupClosePushL( *self );
       
    55     self->ConstructL( aUri, aObserver );
       
    56 
       
    57     CleanupStack::Pop();
       
    58     return self;
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Destructor
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 CNcdDescriptorDownloadSubOperation::~CNcdDescriptorDownloadSubOperation()
       
    67     {
       
    68     DLTRACEIN( ( "" ) );
       
    69     if ( iDownload ) 
       
    70         {
       
    71         iDownload->Release();
       
    72         iDownload = NULL;
       
    73         }
       
    74     
       
    75     delete iBody;
       
    76     iBody = NULL;
       
    77     DLTRACEOUT((""));
       
    78     }
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Download config getter
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 MCatalogsHttpConfig& CNcdDescriptorDownloadSubOperation::Config()
       
    86     {
       
    87     DASSERT( iDownload );
       
    88     return iDownload->Config();
       
    89     }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // Download request header getter
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 MCatalogsHttpHeaders& CNcdDescriptorDownloadSubOperation::RequestHeaders()
       
    97     {
       
    98     DASSERT( iDownload );
       
    99     return iDownload->Config().RequestHeaders();    
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Download response header getter
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 const MCatalogsHttpHeaders& CNcdDescriptorDownloadSubOperation::ResponseHeadersL()
       
   107     {
       
   108     DASSERT( iDownload );
       
   109     return iDownload->ResponseHeadersL();
       
   110     }
       
   111 
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // HttpOperation
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 MCatalogsHttpOperation& 
       
   118     CNcdDescriptorDownloadSubOperation::HttpOperation()
       
   119     {
       
   120     DASSERT( iDownload );
       
   121     return *iDownload;
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // Body
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 const TDesC8& CNcdDescriptorDownloadSubOperation::Body() const
       
   129     {
       
   130     if ( iBody && iBody->Size() ) 
       
   131         {
       
   132         iBodyPtr.Set( iBody->Ptr( 0 ) );
       
   133         return iBodyPtr;
       
   134         }
       
   135     return KNullDesC8();
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Start
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 TInt CNcdDescriptorDownloadSubOperation::Start()
       
   143     {
       
   144     DLTRACEIN((""));
       
   145     TInt ret = iDownload->Start();
       
   146         
       
   147     if ( ret >= KErrNone ) 
       
   148         {
       
   149         DLTRACEOUT((""));
       
   150         return KErrNone;
       
   151         }
       
   152     DLTRACEOUT((""));
       
   153     return ret;
       
   154     }
       
   155 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Cancel
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CNcdDescriptorDownloadSubOperation::Cancel() 
       
   162     {    
       
   163     DLTRACEIN(( "" ));
       
   164     if ( iDownload ) 
       
   165         {
       
   166         // Cancel also releases the dl
       
   167         iDownload->Cancel();
       
   168         iDownload = NULL;
       
   169         }
       
   170     DLTRACEOUT(( "" ));
       
   171     }
       
   172 
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // HandleHttpEvent
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 void CNcdDescriptorDownloadSubOperation::HandleHttpEventL( 
       
   179     MCatalogsHttpOperation& aOperation, 
       
   180     TCatalogsHttpEvent aEvent )
       
   181     {
       
   182     DLTRACEIN( ( "" ) );
       
   183     (void) aOperation; // suppresses compiler warning
       
   184     DASSERT( &aOperation == iDownload );
       
   185     DASSERT( aOperation.OperationType() == ECatalogsHttpTransaction );
       
   186 
       
   187     TCatalogsTransportProgress progress( iDownload->Progress() );
       
   188     
       
   189     iProgress = TNcdSendableProgress( iDownloadState,
       
   190         iDownload->OperationId().Id(), progress.iProgress,
       
   191         progress.iMaxProgress );
       
   192 
       
   193     switch( aEvent.iOperationState ) 
       
   194         {
       
   195         // Handle completed operation
       
   196         case ECatalogsHttpOpCompleted:
       
   197             {
       
   198             DLTRACE(("ECatalogsHttpOpCompleted"));
       
   199             iDownloadState = ENcdDownloadComplete;
       
   200             NotifyObserversComplete( KErrNone );            
       
   201             break;
       
   202             }
       
   203         
       
   204         // Handle operation in progress
       
   205         case ECatalogsHttpOpInProgress:
       
   206             {
       
   207             iDownloadState = ENcdDownloadInProgress;
       
   208             switch ( aEvent.iProgressState ) 
       
   209                 {
       
   210                 
       
   211                 case ECatalogsHttpResponseBodyReceived:
       
   212                     {                
       
   213                     DLTRACE(("Response body received"));
       
   214                     TRAP( iError, 
       
   215                         {                        
       
   216                         // Append the body
       
   217                         iBody->InsertL( iBody->Size(), iDownload->Body() );
       
   218                         });
       
   219                     
       
   220                     if ( iError != KErrNone ) 
       
   221                         {
       
   222                         DLERROR(("Error when appending body: %d", iError ));
       
   223                         iDownload->Release();
       
   224                         iDownload = NULL;
       
   225                         iOperationState = EStateComplete;
       
   226                         iDownloadState = ENcdDownloadFailed;
       
   227                         NotifyObserversComplete( iError );
       
   228                         }
       
   229                     else 
       
   230                         {                    
       
   231                         iOperationState = EStateRunning;
       
   232                         DLTRACE(("Inform observer about progress"));
       
   233                         NotifyObserversProgress();
       
   234                         }
       
   235                     break;
       
   236                     }
       
   237                 
       
   238                 case ECatalogsHttpStarted:
       
   239                     {
       
   240                     DLTRACE(("Operation started"));
       
   241                     iOperationState = EStateRunning;
       
   242                     iProgress.iState = ENcdDownloadStarted;
       
   243                     DLTRACE(("Inform observer about progress"));
       
   244                     NotifyObserversProgress();
       
   245                     
       
   246                     break;
       
   247                     }
       
   248                 
       
   249                 default:
       
   250                     break;
       
   251                 }
       
   252             break;
       
   253             }
       
   254         
       
   255         default:
       
   256             {
       
   257             break;
       
   258             }
       
   259         }
       
   260     DLTRACEOUT((""));
       
   261     }
       
   262     
       
   263     
       
   264 // ---------------------------------------------------------------------------
       
   265 // HandleHttpError
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 TBool CNcdDescriptorDownloadSubOperation::HandleHttpError(
       
   269     MCatalogsHttpOperation& aOperation,
       
   270     TCatalogsHttpError aError )
       
   271     {    
       
   272     DLTRACEIN( ( "Error type: %i, id: %i", aError.iType, aError.iError ) );
       
   273     DASSERT( &aOperation == iDownload );
       
   274     
       
   275     aOperation.Release();
       
   276     iDownload = NULL;
       
   277     iError = aError.iError;
       
   278     iDownloadState = ENcdDownloadFailed;
       
   279     iOperationState = EStateComplete;   
       
   280 
       
   281     // notify observer
       
   282     NotifyObserversComplete( iError );
       
   283     return ETrue;
       
   284     }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // RunOperation
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 TInt CNcdDescriptorDownloadSubOperation::RunOperation()
       
   292     {
       
   293     DLTRACEIN(( "Pending message: %X", iPendingMessage ));
       
   294     return KErrNone;
       
   295     }
       
   296     
       
   297     
       
   298 // ---------------------------------------------------------------------------
       
   299 // Constructor
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 CNcdDescriptorDownloadSubOperation::CNcdDescriptorDownloadSubOperation(
       
   303     CNcdGeneralManager& aGeneralManager,
       
   304     MCatalogsHttpSession& aHttpSession,
       
   305     MCatalogsSession& aSession ) :
       
   306         CNcdBaseOperation( aGeneralManager, NULL, EDescriptorDownloadSubOperation, aSession ),
       
   307         iHttpSession( aHttpSession ),
       
   308         iBodyPtr( NULL, 0 )
       
   309     {
       
   310     }
       
   311 
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // ConstructL
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 void CNcdDescriptorDownloadSubOperation::ConstructL( const TDesC& aUri, 
       
   318     MNcdOperationObserver& aObserver )
       
   319     {
       
   320     DLTRACEIN(( _L("URI: %S"), &aUri ));
       
   321     // Call ConstructL for the base class
       
   322     CNcdBaseOperation::ConstructL();    
       
   323 
       
   324     
       
   325     AddObserverL( &aObserver );
       
   326 
       
   327     iBody = CBufFlat::NewL( KBodyExpandSize );
       
   328     
       
   329     // Create the transaction
       
   330     iDownload = iHttpSession.CreateTransactionL( aUri, this );
       
   331     
       
   332  
       
   333     DLTRACEOUT(( "" ));
       
   334     }
       
   335     
       
   336     
       
   337 // ---------------------------------------------------------------------------
       
   338 // NotifyObserversComplete
       
   339 // ---------------------------------------------------------------------------
       
   340 //
       
   341 void CNcdDescriptorDownloadSubOperation::NotifyObserversComplete( TInt aError )
       
   342     {
       
   343     Open(); // increase ref count in CCatalogsCommunicable
       
   344     for( TInt i = 0; i < iObservers.Count(); ++i ) 
       
   345         {
       
   346         iObservers[i]->OperationComplete( this, aError );
       
   347         }
       
   348     Close();
       
   349     }
       
   350 
       
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // NotifyObserversProgress
       
   354 // ---------------------------------------------------------------------------
       
   355 //
       
   356 void CNcdDescriptorDownloadSubOperation::NotifyObserversProgress()
       
   357     {
       
   358     Open(); // increase ref count in CCatalogsCommunicable
       
   359     for( TInt i = 0; i < iObservers.Count(); ++i ) 
       
   360         {
       
   361         iObservers[i]->Progress( *this );
       
   362         }
       
   363     Close();
       
   364     }