browserutilities/downloadmgr/DownloadMgrClntSrv/src/DownloadMgrHandler.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *     This file contains the declaration of the  receive handler of Download Mgr Server.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "DownloadMgrLogger.h"
       
    22 #include "DownloadMgrHandler.h"
       
    23 #include "DownloadMgrClient.h"
       
    24 #include "DownloadMgrDefAttrib.h"
       
    25 
       
    26 // CONSTANTS
       
    27 
       
    28 
       
    29 // GLOBAL FUNCTIONS
       
    30 
       
    31 // ================= MEMBER FUNCTIONS =======================
       
    32 
       
    33 // ---------------------------------------------------------
       
    34 // CDownloadMgrHandler::CDownloadMgrHandler
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 CDownloadMgrHandler::CDownloadMgrHandler( MHttpDownloadMgrObserver* aHandler,
       
    38                                           RHttpDownload* aSubSession )
       
    39 :CActive( EPriorityStandard + 2 ) /* EPriorityStandard + 2 is used until EHttpProgResponseBodyReceived
       
    40                                     event is received, then it must be set to EPriorityLow 
       
    41                                     until an other event arrives. Then it is set back to 
       
    42                                     EPriorityStandard + 2. */
       
    43 ,iPtr(0,0)                             
       
    44     {
       
    45     CLOG_CREATE;
       
    46     
       
    47     CLOG_ENTERFN( "CDownloadMgrHandler::CDownloadMgrHandler" )
       
    48     iHandler = aHandler;
       
    49     iDownloadSubSession = aSubSession;
       
    50     iDownloadSession = NULL;
       
    51     CActiveScheduler::Add( this );
       
    52     iClosed = NULL;
       
    53     iHandlerType = EHttpDownload;
       
    54     iPrevDlEvent.iDownloadState = THttpDownloadState(0); // None
       
    55     iPrevDlEvent.iProgressState = EHttpProgNone;
       
    56     //iPrevDlEvent.iMoIndex = 0;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------
       
    60 // CDownloadMgrHandler::CDownloadMgrHandler
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 CDownloadMgrHandler::CDownloadMgrHandler( RHttpDownloadMgr* aSession )
       
    64 :CActive( EPriorityStandard ) /* Session-level event handler: priority remains 
       
    65                                  EPriorityStandard! */
       
    66 ,iPtr(0,0)                                 
       
    67     {
       
    68     CLOG_CREATE;
       
    69     
       
    70     CLOG_ENTERFN( "CDownloadMgrHandler::CDownloadMgrHandler" )
       
    71     iHandler = NULL;
       
    72     iDownloadSubSession = NULL;
       
    73     iDownloadSession = aSession;
       
    74     CActiveScheduler::Add( this );
       
    75     iClosed = NULL;
       
    76     iHandlerType = EHttpDownloadMgr;
       
    77     iPrevDlEvent.iDownloadState = THttpDownloadState(0); // None
       
    78     iPrevDlEvent.iProgressState = EHttpProgNone;
       
    79     //iPrevDlEvent.iMoIndex = 0;
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------
       
    83 // CDownloadMgrHandler::CDownloadMgrHandler
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 CDownloadMgrHandler::~CDownloadMgrHandler()
       
    87     {
       
    88     CLOG_WRITE( "CDownloadMgrHandler::~CDownloadMgrHandler" )
       
    89     Cancel();
       
    90     CLOG_WRITE_FORMAT( "this: 0x%x", this );
       
    91     
       
    92 
       
    93     CLOG_WRITE_FORMAT( "~iClosed: 0x%x", iClosed );
       
    94     if( iClosed )
       
    95         {
       
    96         CLOG_WRITE( "*iClosed = ETrue;" );
       
    97         *iClosed = ETrue;
       
    98         }
       
    99 
       
   100         
       
   101     if( iPackedEventAttribs )
       
   102         {
       
   103         delete iPackedEventAttribs;
       
   104         }
       
   105         
       
   106     CLOG_CLOSE;
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CDownloadMgrHandler::AdjustPriority
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 void CDownloadMgrHandler::AdjustPriority( THttpDownloadEvent& aCurrentDlEvent )
       
   114     {
       
   115     if( !iDownloadSubSession->EventPriorityFlag() )
       
   116     //The flag is zero if there is no cod AND client side download
       
   117         {
       
   118         if ( aCurrentDlEvent.iProgressState == EHttpProgCodDescriptorDownloaded )
       
   119             {
       
   120             // Cod download to be started in client side
       
   121             // Set priority to the original (EPriorityStandard + 2) value.
       
   122             CLOG_WRITE( "Setting EPriorityStandard + 2" );            
       
   123             }        
       
   124         else if( aCurrentDlEvent.iProgressState == EHttpProgResponseBodyReceived && 
       
   125             iPrevDlEvent.iProgressState != EHttpProgResponseBodyReceived )
       
   126             { // Progress changes to EHttpProgResponseBodyReceived
       
   127             // Set priority to a low value.
       
   128             CLOG_WRITE( "Setting EPriorityStandard + 1" );
       
   129             SetPriority( EPriorityStandard + 1 );
       
   130             }
       
   131         else if ( aCurrentDlEvent.iProgressState != EHttpProgResponseBodyReceived && 
       
   132                   iPrevDlEvent.iProgressState == EHttpProgResponseBodyReceived )
       
   133             { // Progress changes from EHttpProgResponseBodyReceived
       
   134             // Set priority to the original (EPriorityStandard + 2) value.
       
   135             CLOG_WRITE( "Setting EPriorityStandard + 2" );
       
   136             SetPriority( EPriorityStandard + 2 );
       
   137             }
       
   138 
       
   139         // Otherwise no changes in priority!
       
   140         
       
   141         iPrevDlEvent = aCurrentDlEvent; // Remember for the event
       
   142         }
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // CDownloadMgrHandler::RunL
       
   147 // ---------------------------------------------------------
       
   148 //
       
   149 void CDownloadMgrHandler::RunL()
       
   150     {
       
   151     CLOG_WRITE_FORMAT( " CDownloadMgrHandler::RunL DH-iStat: %d", iStatus.Int() );
       
   152     if ( iStatus.Int() == KErrNone )
       
   153         {
       
   154         THttpDownloadEvent event;
       
   155         event.iDownloadState = (THttpDownloadState)iDownloadState();
       
   156         event.iProgressState = (THttpProgressState)iProgressState();
       
   157         CLOG_WRITE_FORMAT( "DS: %d", event.iDownloadState );
       
   158         CLOG_WRITE_FORMAT( "PS: %d", event.iProgressState );
       
   159         CLOG_WRITE_FORMAT( "Handler Type: %d", iHandlerType );
       
   160         CLOG_WRITE_FORMAT( "this: 0x%x", this );
       
   161         switch( iHandlerType )
       
   162             {
       
   163             case EHttpDownload:
       
   164                 {
       
   165                 TBool closed( EFalse );
       
   166                 iClosed = &closed;
       
   167                 CLOG_WRITE_FORMAT( "iClosed before: %d", *iClosed );
       
   168                 
       
   169                 // *** Do active object priority adjustments here ***
       
   170                 AdjustPriority( event );
       
   171                 TPtr8 ptr = iPackedEventAttribs->Des();
       
   172                 iDownloadSubSession->UnpackEventAttribL( ptr );
       
   173                 
       
   174                 // Media object index
       
   175                 //event.iMoIndex = iDownloadSubSession->ActiveMoIndex();
       
   176                 
       
   177                 if( EHttpDlDeleting == event.iDownloadState )
       
   178                     {
       
   179                     iDownloadSubSession->Delete();
       
   180 					iClosed = NULL;
       
   181                     }
       
   182                 else
       
   183                     {
       
   184                     iHandler->HandleDMgrEventL( *iDownloadSubSession, event );
       
   185                     }
       
   186                 // Check if the download subsession has been closed by the client
       
   187                 CLOG_WRITE_FORMAT( "closed: %d", closed );
       
   188                 iClosed = NULL;
       
   189                 if( !closed )
       
   190                     {
       
   191                     StartL();
       
   192                     }
       
   193                 break;
       
   194                 }
       
   195             case EHttpDownloadMgr:
       
   196                 {
       
   197                 // Unmask iHandle into 'handle' and 'media index'
       
   198                 TInt handle = (iHandle() & 0x0000FFFF);
       
   199                 //event.iMoIndex = (iHandle() >> 16);
       
   200                 TBool closed( EFalse );
       
   201                 iClosed = &closed;
       
   202                 iDownloadSession->HandleSessionEventL( handle, event );
       
   203                 CLOG_WRITE_FORMAT( "closed: %d", closed );
       
   204                 iClosed = NULL;
       
   205                 if( !closed )
       
   206                     {
       
   207                     StartL();
       
   208                     }
       
   209                 break;
       
   210                 }
       
   211             default:
       
   212                 {
       
   213                 break;
       
   214                 }
       
   215             }
       
   216         }
       
   217     else
       
   218         {
       
   219         //Should be got here!
       
   220         }
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------
       
   224 // CDownloadMgrHandler::DoCancel
       
   225 // ---------------------------------------------------------
       
   226 //
       
   227 void CDownloadMgrHandler::DoCancel()
       
   228     {
       
   229     CLOG_ENTERFN( "CDownloadMgrHandler::DoCancel" )
       
   230     switch( iHandlerType )
       
   231         {
       
   232         case EHttpDownload:
       
   233             {
       
   234             iDownloadSubSession->CancelEventNotification();
       
   235             break;
       
   236             }
       
   237         case EHttpDownloadMgr:
       
   238             {
       
   239             iDownloadSession->CancelSessionEventNotification();
       
   240             break;
       
   241             }
       
   242         default:
       
   243             {
       
   244             break;
       
   245             }
       
   246         }
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------
       
   250 // CDownloadMgrHandler::StartL
       
   251 // ---------------------------------------------------------
       
   252 //
       
   253 void CDownloadMgrHandler::StartL()
       
   254     {
       
   255     CLOG_ENTERFN( "CDownloadMgrHandler::StartL" )
       
   256     if( !IsActive() )
       
   257         {
       
   258         iStatus = KRequestPending;
       
   259         switch( iHandlerType )
       
   260             {
       
   261             case EHttpDownload:
       
   262                 {
       
   263                 //Use packer to alloc buffer
       
   264                 HBufC8* temp_packer = TDMgrUtils::PackEventAttribL();
       
   265                 if( iPackedEventAttribs )
       
   266                     {
       
   267                     delete iPackedEventAttribs;
       
   268                     }                
       
   269                 iPackedEventAttribs = temp_packer;
       
   270                 CLOG_WRITE( "EHttpDownload" );
       
   271                 TInt length = iPackedEventAttribs->Des().Length();
       
   272                 iPtr.Set( iPackedEventAttribs->Des() );
       
   273                 iDownloadSubSession->StartEventNotification( iDownloadState,
       
   274                                                              iProgressState,
       
   275                                                              iPtr,
       
   276                                                              iStatus );
       
   277                 break;
       
   278                 }
       
   279             case EHttpDownloadMgr:
       
   280                 {
       
   281                 CLOG_WRITE( "EHttpDownloadMgr" );
       
   282                 iDownloadSession->StartSessionEventNotification( iDownloadState,
       
   283                                                                  iProgressState,
       
   284                                                                  iHandle,
       
   285                                                                  iStatus );
       
   286                 break;
       
   287                 }
       
   288             default:
       
   289                 {
       
   290                 break;
       
   291                 }
       
   292             }
       
   293         SetActive();
       
   294         }
       
   295     }