browserutilities/feedsengine/FeedsServer/UrlHandler/src/SessionHttpConnection.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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:  A class that fetches resources via HTTP 1.1.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "SessionHttpConnection.h"
       
    20 #include "FeedsServerSession.h"
       
    21 #include "Logger.h"
       
    22 
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 // CSessionHttpConnection::NewL
       
    26 //
       
    27 // Two-phased constructor.
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CSessionHttpConnection* CSessionHttpConnection::NewL( CFeedsServerSession& aFeedsSession )
       
    31     {
       
    32     CSessionHttpConnection* self = new (ELeave) CSessionHttpConnection( aFeedsSession );
       
    33     
       
    34     CleanupStack::PushL(self);
       
    35     self->ConstructL();
       
    36     CleanupStack::Pop();
       
    37 
       
    38     return self;
       
    39     }
       
    40 
       
    41         
       
    42 // -----------------------------------------------------------------------------
       
    43 // CSessionHttpConnection::CSessionHttpConnection
       
    44 // C++ default constructor can NOT contain any code, that
       
    45 // might leave.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CSessionHttpConnection::CSessionHttpConnection( CFeedsServerSession& aFeedsSession ):
       
    49         iLeakTracker( CLeakTracker::EHttpConnection ),
       
    50         iFeedsSession( aFeedsSession ) 
       
    51     {
       
    52     }
       
    53         
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CSessionHttpConnection::ConstructL
       
    57 // Symbian 2nd phase constructor can leave.
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 void CSessionHttpConnection::ConstructL()
       
    61     {
       
    62     BaseConstructL();
       
    63 
       
    64     User::LeaveIfError( iSocketServ.Connect() );
       
    65     iConnName = HBufC::NewL( KMaxName );
       
    66     Reset();
       
    67     }        
       
    68 
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CSessionHttpConnection::~CSessionHttpConnection
       
    72 // Deconstructor.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CSessionHttpConnection::~CSessionHttpConnection()
       
    76     {
       
    77     // close http session
       
    78     iSession.Close();
       
    79 
       
    80     // close RConnection
       
    81     Disconnect();
       
    82     delete iConnName;
       
    83 
       
    84     // close RSocketServer
       
    85     iSocketServ.Close();
       
    86     }
       
    87 
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CSessionHttpConnection::CreateConnection
       
    91 // 
       
    92 // It propagates the connection callback to the browser client.
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 TInt CSessionHttpConnection::CreateConnection(TInt* aConnectionPtr, TInt* aSockSvrHandle, 
       
    96         TBool* aNewConn, TApBearerType* aBearerType)
       
    97     {
       
    98     TInt  err = KErrNone;
       
    99 
       
   100     // If need be establish the connection.
       
   101     if( !IsConnected() )
       
   102         {
       
   103         // Prepare connection name & bearer type ready for client response.
       
   104         Reset();
       
   105 
       
   106 		// server session ask client app for connection
       
   107         TRAP( err, iFeedsSession.NetworkConnectionNeededL() );
       
   108 
       
   109         if  ( err == KErrNone )
       
   110             {
       
   111             // Start the scheduler to wait for client response. Scheduler will be stopped 
       
   112             // when client informs the server which bearer to use.
       
   113             //
       
   114             // See:
       
   115             //
       
   116             // [SERVER]
       
   117             //      CFeedsServerSession::SetConnectionL
       
   118             //      CSessionHttpConnection::SetConnection
       
   119             //
       
   120             // [CLIENT]
       
   121             //      CFolderItemRequestHandler::RequestCompletedL ("EFeedsServerConnectionNeeded")
       
   122             //      CFeedRequestHandler::RequestCompletedL ("EFeedsServerConnectionNeeded")
       
   123 	        //
       
   124 	        iWait.Start();
       
   125 
       
   126             // At this point, assuming that the client was able to provide us
       
   127             // with the connection information then we are able to open the specified
       
   128             // connection.
       
   129             //
       
   130             // If not (e.g. client cancelled connection request) then we must clean up
       
   131             // and handle the error.
       
   132             if  ( ConnectionInformationProvidedByClient() )
       
   133                 {
       
   134                 // connection info come back in response from client
       
   135                 TName connName;
       
   136                 connName.Copy( *iConnName );
       
   137                 err = iConnection.Open( iSocketServ, connName );
       
   138 
       
   139                 if  ( err == KErrNone )
       
   140                     {
       
   141                     *aNewConn = ETrue;
       
   142 
       
   143                     if (iObserver != NULL)
       
   144                         {
       
   145                         iObserver->ConnectionAvailable();
       
   146                         }
       
   147                     }
       
   148                 }
       
   149             else
       
   150                 {
       
   151                 // Use current connection error value if the client was
       
   152                 // not able to supply connection parameters.
       
   153                 err = iConnectionError;
       
   154                 }
       
   155             }
       
   156         }       
       
   157     else
       
   158         {
       
   159         // Otherwise the existing connection is valid and no new connection
       
   160         // is required.
       
   161         *aNewConn = EFalse;
       
   162         }
       
   163 
       
   164     // End of process - either set up the reference/pointer arguments for
       
   165     // a successful connection initiation, or then inform the observer of
       
   166     // the error.
       
   167     if  ( err == KErrNone )
       
   168         {
       
   169         *aConnectionPtr = (TInt) &iConnection;
       
   170         *aSockSvrHandle = iSocketServ.Handle();
       
   171         *aBearerType = iBearerType;
       
   172         }
       
   173     else
       
   174         {
       
   175         // Notify the HttpHandler that the establishing the access point failed
       
   176         if (iObserver != NULL)
       
   177             {
       
   178             iObserver->ConnectionFailed(err);
       
   179             }
       
   180         }
       
   181 
       
   182     return err;
       
   183     }
       
   184 
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CSessionHttpConnection::IsConnected
       
   188 //
       
   189 // Returns whether or not the connection is active.
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CSessionHttpConnection::IsConnected()
       
   193     {
       
   194     if ( !iConnection.SubSessionHandle() )
       
   195         {
       
   196         iConnStage = KConnectionUninitialised;
       
   197         }
       
   198     else
       
   199         {
       
   200         TNifProgress progress;
       
   201         iConnection.Progress( progress );
       
   202         iConnStage = progress.iStage;
       
   203         }
       
   204 
       
   205     return iConnStage == KLinkLayerOpen;
       
   206     }
       
   207     
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CSessionHttpConnection::Disconnect
       
   211 //
       
   212 // Closes the connection.
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 void CSessionHttpConnection::Disconnect()
       
   216     {
       
   217     if (IsConnected())
       
   218         {
       
   219         iConnection.Close();
       
   220         }
       
   221     }
       
   222 
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CSessionHttpConnection::CancelAnyConnectionAttempt
       
   226 //
       
   227 // Closes the connection.
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 void CSessionHttpConnection::CancelAnyConnectionAttempt()
       
   231     {
       
   232     SetObserver(NULL);
       
   233     if  ( iWait.IsStarted() )
       
   234         {
       
   235         iWait.AsyncStop();
       
   236         }
       
   237         
       
   238     Reset();
       
   239     
       
   240     iConnectionError = KErrCancel;
       
   241     }
       
   242 
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CSessionHttpConnection::SetConnection
       
   246 //
       
   247 // Sets the connection.
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 TBool CSessionHttpConnection::SetConnection( TDesC& aName, TInt aBearerType  )
       
   251     {
       
   252     TBool ret = EFalse;
       
   253 
       
   254     // unblocking
       
   255     if  ( iWait.IsStarted() )
       
   256         {
       
   257         iConnName->Des().Copy( aName );
       
   258         iBearerType = (TApBearerType) aBearerType;
       
   259         iConnectionError = KErrNone;
       
   260 
       
   261         ret = ETrue;
       
   262 
       
   263         iWait.AsyncStop();
       
   264         }
       
   265 
       
   266     return ret;
       
   267     }
       
   268 
       
   269 
       
   270 //--------------------------------------------------------------------------
       
   271 //CSessionHttpConnection::Reset()
       
   272 //--------------------------------------------------------------------------
       
   273 void CSessionHttpConnection::Reset()
       
   274     {
       
   275     iConnectionError = KErrNotReady;
       
   276     iConnName->Des().Zero();
       
   277     iBearerType = EApBearerTypeAllBearers;
       
   278     }
       
   279 
       
   280 
       
   281 //--------------------------------------------------------------------------
       
   282 //CSessionHttpConnection::ConnectionInformationProvidedByClient()
       
   283 //--------------------------------------------------------------------------
       
   284 TBool CSessionHttpConnection::ConnectionInformationProvidedByClient() const
       
   285    	{
       
   286     return ( iConnName->Length() > 0 );
       
   287    	}
       
   288 
       
   289 
       
   290 
       
   291