pushmtm/Plugins/PushContentHandler/PushMtmCacheSupply.cpp
changeset 51 48e827313edd
parent 37 481242ead638
child 53 f427d27b98d8
equal deleted inserted replaced
37:481242ead638 51:48e827313edd
     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 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:  Implementation of CPushMtmCacheSupply
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "PushMtmCacheSupply.h"
       
    21 #include "httpcachemanager.h"
       
    22 #include "PushMtmCacheDataSupplier.h"
       
    23 
       
    24 #include <http/RHTTPTransaction.h>
       
    25 #include <http/mhttpdatasupplier.h>
       
    26 #include <http/rhttpsession.h>
       
    27 #include <httperr.h>
       
    28 
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES
       
    33 
       
    34 // CONSTANTS
       
    35 const TInt KResponseTimeout = 0;
       
    36 
       
    37 // MACROS
       
    38 
       
    39 // LOCAL CONSTANTS AND MACROS
       
    40 
       
    41 // MODULE DATA STRUCTURES
       
    42 
       
    43 // LOCAL FUNCTION PROTOTYPES
       
    44 
       
    45 // FORWARD DECLARATIONS
       
    46 
       
    47 // ============================= LOCAL FUNCTIONS ===============================
       
    48 
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CPushMtmCacheSupply::CPushMtmCacheSupply
       
    54 // C++ default constructor can NOT contain any code, that
       
    55 // might leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CPushMtmCacheSupply::CPushMtmCacheSupply(MCacheSupplyCallbacks* aCacheSupplyCallbacks ):
       
    59       // closed by default
       
    60       iReponseState( THTTPEvent::EClosed )
       
    61     {
       
    62     iCacheEntry.iCacheHandler = NULL;
       
    63     iCacheEntry.iCacheEntry = NULL;
       
    64     iCacheSupplyCallbacks = aCacheSupplyCallbacks;
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CPushMtmCacheSupply::ConstructL
       
    69 // Symbian 2nd phase constructor can leave.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void CPushMtmCacheSupply::ConstructL(CHttpCacheManager* aCacheMgr)
       
    73     {
       
    74     iCacheManager = aCacheMgr;
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CPushMtmCacheSupply::NewL
       
    79 // Two-phased constructor.
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CPushMtmCacheSupply* CPushMtmCacheSupply::NewL(CHttpCacheManager* aCacheMgr, MCacheSupplyCallbacks* aCacheSupplyCallbacks  )
       
    83     {
       
    84     CPushMtmCacheSupply* self = new( ELeave ) CPushMtmCacheSupply( aCacheSupplyCallbacks);
       
    85 
       
    86     CleanupStack::PushL( self );
       
    87     self->ConstructL(aCacheMgr);
       
    88     CleanupStack::Pop();
       
    89 
       
    90     return self;
       
    91     }
       
    92 
       
    93 // Destructor
       
    94 CPushMtmCacheSupply::~CPushMtmCacheSupply()
       
    95     {
       
    96     delete iDataSupplier;
       
    97     CloseRequest();
       
    98     if (iResponseTimer)
       
    99         {
       
   100         iResponseTimer->Cancel();
       
   101         }
       
   102     delete iResponseTimer;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CPushMtmCacheSupply::StartRequestL
       
   107 //
       
   108 //
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 TInt CPushMtmCacheSupply::StartRequestL( )
       
   112     {
       
   113     TInt status;
       
   114 
       
   115     status = iCacheManager->RequestL( *(iCacheSupplyCallbacks->Transaction()), 
       
   116     TBrCtlDefs::ECacheModeNormal , iCacheEntry );
       
   117     // start a timer that feeds the content to the transaction
       
   118     if( status == KErrNone )
       
   119         {
       
   120         iClosed = EFalse;
       
   121         // set response state. start with the http headers.
       
   122         iReponseState = THTTPEvent::EGotResponseHeaders;
       
   123         //
       
   124         iResponseTimer = CPeriodic::NewL( CActive::EPriorityHigh );
       
   125         iResponseTimer->Start( KResponseTimeout, KResponseTimeout, TCallBack( &ResponseCallbackL, this ) );
       
   126         }
       
   127     return status;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CPushMtmCacheSupply::CloseRequest
       
   132 //
       
   133 //
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 void CPushMtmCacheSupply::CloseRequest()
       
   137     {
       
   138     if( !iClosed )
       
   139 	    {
       
   140 	    iCacheManager->RequestClosed( (iCacheSupplyCallbacks->Transaction()), iCacheEntry );
       
   141 	    iClosed = ETrue;
       
   142 	    }
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CPushMtmCacheSupply::HeadersReceived
       
   147 //
       
   148 //
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CPushMtmCacheSupply::HeadersReceivedL()
       
   152     {
       
   153     // do not cache content we just sent off
       
   154     if( iReponseState == THTTPEvent::EClosed )
       
   155         {
       
   156         RHTTPTransaction* trans = iCacheSupplyCallbacks->Transaction();
       
   157         iCacheManager->ReceivedResponseHeadersL( *(iCacheSupplyCallbacks->Transaction()), iCacheEntry );
       
   158         //
       
   159         iNotModified = trans->Response().StatusCode() == HTTPStatus::ENotModified;
       
   160         if( iNotModified )
       
   161             {
       
   162             // change from 304 to 200 -otherwise trans exits with EFailed
       
   163             trans->Response().SetStatusCode( HTTPStatus::EOk );
       
   164             }
       
   165         }
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CPushMtmCacheSupply::BodyReceivedL
       
   170 //
       
   171 //
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CPushMtmCacheSupply::BodyReceivedL()
       
   175     {
       
   176     // do not cache content we just sent off
       
   177     if( iReponseState == THTTPEvent::EClosed )
       
   178         {
       
   179         RHTTPTransaction* trans = iCacheSupplyCallbacks->Transaction();
       
   180         MHTTPDataSupplier* supplier = trans->Response().Body();
       
   181         //
       
   182         iCacheManager->ReceivedResponseBodyDataL( *trans, *supplier, iCacheEntry );
       
   183         }
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPushMtmCacheSupply::ResponseComplete
       
   188 //
       
   189 //
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 void CPushMtmCacheSupply::ResponseCompleteL()
       
   193     {
       
   194     if( iReponseState == THTTPEvent::EClosed )
       
   195         {
       
   196         RHTTPTransaction* trans = iCacheSupplyCallbacks->Transaction();
       
   197         // not modified needs body before response complete
       
   198         if( iNotModified )
       
   199             {
       
   200             // use cache
       
   201             // close response first
       
   202             iCacheManager->ResponseComplete( *trans, iCacheEntry );
       
   203             // request the item from cache
       
   204             if( iCacheManager->RequestL( *trans, TBrCtlDefs::ECacheModeOnlyCache, iCacheEntry ) == KErrNone )
       
   205                 {
       
   206                 // ser
       
   207                 iReponseState = THTTPEvent::EGotResponseBodyData;
       
   208                 //
       
   209                 SendBodyL();
       
   210                 CloseRequest();
       
   211                 }
       
   212             }
       
   213         else
       
   214             {
       
   215             // normal response complete
       
   216             iCacheManager->ResponseComplete( *trans, iCacheEntry );
       
   217             }
       
   218         }
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CPushMtmCacheSupply::ResponseCallbackL
       
   223 //
       
   224 //
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 TInt CPushMtmCacheSupply::ResponseCallbackL(
       
   228     TAny* aAny )
       
   229     {
       
   230     //
       
   231     CPushMtmCacheSupply* thisObj = (CPushMtmCacheSupply*)aAny;
       
   232     thisObj->SupplyResponseL();
       
   233     return KErrNone;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CPushMtmCacheSupply::SupplyResponseL
       
   238 //
       
   239 //
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 void CPushMtmCacheSupply::SupplyResponseL()
       
   243     {
       
   244     RHTTPTransaction* trans = iCacheSupplyCallbacks->Transaction();
       
   245     //
       
   246     switch( iReponseState )
       
   247         {
       
   248         case THTTPEvent::EGotResponseHeaders:
       
   249             {
       
   250             //
       
   251             iFailed = EFalse;
       
   252             if( iCacheManager->RequestHeadersL( *trans, iCacheEntry ) == KErrNone )
       
   253                 {
       
   254                 //
       
   255                 trans->Response().SetStatusCode( HTTPStatus::EOk );
       
   256                 //
       
   257                 iCacheSupplyCallbacks->HandleEventL( THTTPEvent::EGotResponseHeaders );
       
   258                 // move to the next state
       
   259                 iReponseState = THTTPEvent::EGotResponseBodyData;
       
   260                 }
       
   261             else
       
   262                 {
       
   263                 // move to the next state
       
   264                 iFailed = ETrue;
       
   265                 // move to the next state
       
   266                 iReponseState = THTTPEvent::EResponseComplete;
       
   267                 }
       
   268             break;
       
   269             }
       
   270         case THTTPEvent::EGotResponseBodyData:
       
   271             {
       
   272             //
       
   273             SendBodyL();
       
   274             // move to the next state
       
   275             iReponseState = THTTPEvent::EResponseComplete;
       
   276             break;
       
   277             }
       
   278         case THTTPEvent::EResponseComplete:
       
   279             {
       
   280             iCacheSupplyCallbacks->HandleEventL( THTTPEvent::EResponseComplete );
       
   281             // move to the next state
       
   282             iReponseState = !iFailed ? THTTPEvent::ESucceeded : THTTPEvent::EFailed;
       
   283             break;
       
   284             }
       
   285         case THTTPEvent::ESucceeded:
       
   286             {
       
   287             // move to the next state
       
   288             iReponseState = THTTPEvent::EClosed;
       
   289             // cancel timer
       
   290             iResponseTimer->Cancel();
       
   291             //
       
   292             iCacheSupplyCallbacks->HandleEventL( THTTPEvent::ESucceeded );
       
   293             // this obj is destroyed at this point
       
   294             break;
       
   295             }
       
   296         case THTTPEvent::EFailed:
       
   297             {
       
   298             // move to the next state
       
   299             iReponseState = THTTPEvent::EClosed;
       
   300             // cancel timer
       
   301             iResponseTimer->Cancel();
       
   302             //
       
   303             iCacheSupplyCallbacks->HandleEventL( THTTPEvent::EFailed );
       
   304             // this obj is destroyed at this point
       
   305             break;
       
   306             }
       
   307         default:
       
   308             {
       
   309             //
       
   310             }
       
   311         }
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CPushMtmCacheSupply::SendBodyL
       
   316 //
       
   317 //
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CPushMtmCacheSupply::SendBodyL()
       
   321     {
       
   322     RHTTPTransaction* trans = iCacheSupplyCallbacks->Transaction();
       
   323     //
       
   324     TBool lastChunk;
       
   325     // currently it is always the last chunk
       
   326     HBufC8* body = iCacheManager->RequestNextChunkL( *trans, lastChunk, iCacheEntry );
       
   327     if( body )
       
   328         {
       
   329         CleanupStack::PushL( body );
       
   330         // create datasupplier and attach it to the transaction
       
   331         if( !iDataSupplier )
       
   332             iDataSupplier = CPushMtmCacheDataSupplier::NewL( body );
       
   333         trans->Response().SetBody( *iDataSupplier );
       
   334         CleanupStack::Pop(); // body
       
   335         //
       
   336         iCacheSupplyCallbacks->HandleEventL( THTTPEvent::EGotResponseBodyData );
       
   337         }
       
   338     else
       
   339         {
       
   340         // move to the next state
       
   341         iFailed = ETrue;
       
   342         }
       
   343     }
       
   344     
       
   345 // -----------------------------------------------------------------------------
       
   346 // CPushMtmCacheSupply::PauseSupply
       
   347 // -----------------------------------------------------------------------------
       
   348 void CPushMtmCacheSupply::PauseSupply()
       
   349 	{
       
   350 	if( iResponseTimer && iResponseTimer->IsActive() )
       
   351 		iResponseTimer->Cancel();
       
   352 	}
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CPushMtmCacheSupply::ResumeSupply
       
   356 // -----------------------------------------------------------------------------
       
   357 void CPushMtmCacheSupply::ResumeSupply()
       
   358 	{
       
   359 	if( iResponseTimer && !iResponseTimer->IsActive() )
       
   360 		iResponseTimer->Start( KResponseTimeout, KResponseTimeout, TCallBack( &ResponseCallbackL, this ) );
       
   361 	}
       
   362 
       
   363 //  End of File