locationsystemui/locationsysui/posindicator/posreversegeocodeplugin/src/posrevgeocodehttpclientengine.cpp
changeset 33 834e27cad510
equal deleted inserted replaced
32:b12ea03c50a3 33:834e27cad510
       
     1 /*
       
     2 * Copyright (c) 2010 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: Implementation of HTTP client engine class.
       
    15 *
       
    16 */
       
    17 
       
    18 //Symbian headers
       
    19 #include "posrevgeocodehttpclientengine.h"
       
    20 #include "posrevgeocodelogger.h"
       
    21 
       
    22 #include <http.h>
       
    23 #include <CommDbConnPref.h>
       
    24 #include <connpref.h>
       
    25 #include <extendedconnpref.h>
       
    26 #include <cmgenconnsettings.h>
       
    27 
       
    28 // Used user agent for requests
       
    29 _LIT8(KUserAgent, "ReverseGeoCode 1.0");
       
    30 
       
    31 // This client accepts all content types.
       
    32 _LIT8(KAccept, "*/*");
       
    33 
       
    34 // ----------------------------------------------------------------------------
       
    35 // CPosRevGeoCodeHTTPClientEngine::NewL()
       
    36 // ----------------------------------------------------------------------------
       
    37 CPosRevGeoCodeHTTPClientEngine* CPosRevGeoCodeHTTPClientEngine::NewL( 
       
    38                                     MPosRevGeoCodeHTTPClientObserver& aObserver )
       
    39     {
       
    40     FUNC("CPosRevGeoCodeHTTPClientEngine::NewL");
       
    41     CPosRevGeoCodeHTTPClientEngine* self = new ( ELeave ) 
       
    42                                     CPosRevGeoCodeHTTPClientEngine( aObserver );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48 
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CPosRevGeoCodeHTTPClientEngine::CPosRevGeoCodeHTTPClientEngine()
       
    52 // ----------------------------------------------------------------------------
       
    53 CPosRevGeoCodeHTTPClientEngine::CPosRevGeoCodeHTTPClientEngine(
       
    54                                               MPosRevGeoCodeHTTPClientObserver& aObserver ): 
       
    55                                               CActive( CActive::EPriorityStandard ),
       
    56                                               iObserver( aObserver ),
       
    57                                               iConnectionSetupDone( EFalse ),
       
    58                                               iPrevProfileId( -1 ),
       
    59                                               iMobility(NULL),
       
    60                                               iTransactionOpen( EFalse ),
       
    61                                               iUri(NULL)
       
    62     {
       
    63     FUNC("CPosRevGeoCodeHTTPClientEngine::CPosRevGeoCodeHTTPClientEngine");
       
    64     CActiveScheduler::Add(this);
       
    65     }
       
    66 
       
    67 // ----------------------------------------------------------------------------
       
    68 // CPosRevGeoCodeHTTPClientEngine::~CPosRevGeoCodeHTTPClientEngine()
       
    69 // ----------------------------------------------------------------------------
       
    70 CPosRevGeoCodeHTTPClientEngine::~CPosRevGeoCodeHTTPClientEngine()
       
    71     {
       
    72     FUNC("CPosRevGeoCodeHTTPClientEngine::~CPosRevGeoCodeHTTPClientEngine");
       
    73     Cancel();
       
    74     
       
    75     if( iMobility )
       
    76         {
       
    77 		iMobility->Cancel();
       
    78         delete iMobility;
       
    79         }
       
    80 
       
    81     if ( iTransactionOpen )
       
    82         {
       
    83         iTransaction.Close();
       
    84         }
       
    85 
       
    86     
       
    87     iSession.Close();
       
    88     iConnection.Close();
       
    89     iSocketServ.Close();
       
    90 
       
    91     delete iUri;
       
    92 
       
    93     iCmManager.Close();
       
    94     }
       
    95 
       
    96 // ----------------------------------------------------------------------------
       
    97 // CPosRevGeoCodeHTTPClientEngine::ConstructL()
       
    98 // ----------------------------------------------------------------------------
       
    99 void CPosRevGeoCodeHTTPClientEngine::ConstructL()
       
   100     {
       
   101     FUNC("CPosRevGeoCodeHTTPClientEngine::ConstructL");
       
   102     iCmManager.OpenL();
       
   103     }
       
   104 
       
   105 // ----------------------------------------------------------------------------
       
   106 // CPosRevGeoCodeHTTPClientEngine::CloseConnection()
       
   107 // ----------------------------------------------------------------------------
       
   108 void CPosRevGeoCodeHTTPClientEngine::CloseConnection()
       
   109     {
       
   110     FUNC("CPosRevGeoCodeHTTPClientEngine::CloseConnection");
       
   111     if ( iTransactionOpen )
       
   112         {
       
   113         iTransaction.Close();
       
   114         iTransactionOpen = EFalse;
       
   115         }
       
   116     
       
   117     if ( iMobility )
       
   118         {
       
   119         iMobility->Cancel();
       
   120         delete iMobility;
       
   121         iMobility = NULL;
       
   122         }
       
   123 
       
   124     iSession.Close();
       
   125     iConnection.Close();
       
   126     iSocketServ.Close();  
       
   127     iConnectionSetupDone = EFalse;
       
   128     }
       
   129 
       
   130 
       
   131 // ----------------------------------------------------------------------------
       
   132 // CPosRevGeoCodeHTTPClientEngine::SetupConnectionL()
       
   133 // ----------------------------------------------------------------------------
       
   134 void CPosRevGeoCodeHTTPClientEngine::SetupConnectionL()
       
   135     {
       
   136     FUNC("CPosRevGeoCodeHTTPClientEngine::SetupConnectionL");
       
   137     if ( iConnectionSetupDone )
       
   138         {
       
   139         // Connection setup is done
       
   140         User::Leave(KErrAlreadyExists);
       
   141         }
       
   142  
       
   143     
       
   144     // Open HTTP Session
       
   145     iSession.OpenL();
       
   146     
       
   147     User::LeaveIfError(iSocketServ.Connect());
       
   148     User::LeaveIfError(iConnection.Open(iSocketServ));
       
   149     
       
   150     TConnPrefList prefList;
       
   151     TExtendedConnPref prefs;
       
   152     prefs.SetSnapPurpose( CMManager::ESnapPurposeInternet );
       
   153     prefs.SetForcedRoaming( EFalse );
       
   154     prefs.SetBearerSet( TExtendedConnPref::EExtendedConnBearerCellular | 
       
   155                         TExtendedConnPref::EExtendedConnBearerWLAN );
       
   156     prefList.AppendL( &prefs );
       
   157     
       
   158     iConnection.Start( prefList,iStatus );
       
   159     SetActive();
       
   160     }
       
   161 
       
   162 // ----------------------------------------------------------------------------
       
   163 // CPosRevGeoCodeHTTPClientEngine::SetHeaderL()
       
   164 // ----------------------------------------------------------------------------
       
   165 void CPosRevGeoCodeHTTPClientEngine::SetHeaderL( RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue )
       
   166     {
       
   167     FUNC("CPosRevGeoCodeHTTPClientEngine::SetHeaderL");
       
   168     RStringF valStr = iSession.StringPool().OpenFStringL( aHdrValue );
       
   169     CleanupClosePushL( valStr );
       
   170     THTTPHdrVal val(valStr);
       
   171     aHeaders.SetFieldL( iSession.StringPool().StringF( aHdrField, RHTTPSession::GetTable()), val);
       
   172     CleanupStack::PopAndDestroy();  // valStr
       
   173     }
       
   174 
       
   175 // ----------------------------------------------------------------------------
       
   176 // CPosRevGeoCodeHTTPClientEngine::IssueHTTPGetL()
       
   177 // ----------------------------------------------------------------------------
       
   178 void CPosRevGeoCodeHTTPClientEngine::IssueHTTPGetL( const TDesC8& aUri )
       
   179     {
       
   180     FUNC("CPosRevGeoCodeHTTPClientEngine::IssueHTTPGetL");
       
   181     if ( IsActive() )
       
   182         {
       
   183         return;
       
   184         }
       
   185     
       
   186     iEngineState = EGet;
       
   187     
       
   188     delete iUri;
       
   189     iUri = NULL;
       
   190     iUri = aUri.AllocL();
       
   191     // Create HTTP connection
       
   192     TRAPD( err, SetupConnectionL() );
       
   193     //If the Err is KErrNone, It will lead to RunL and
       
   194     //hence jump to the DoHTTPGetL() from there.
       
   195     
       
   196     if( err == KErrAlreadyExists )
       
   197         {
       
   198         DoHTTPGetL();
       
   199         }
       
   200     else if( err != KErrNone )
       
   201         {
       
   202         //If Setup Connection fails
       
   203         iObserver.ClientEvent( EHttpConnectionFailure );
       
   204         User::Leave(err);
       
   205         }
       
   206     }
       
   207 
       
   208 // ----------------------------------------------------------------------------
       
   209 // CPosRevGeoCodeHTTPClientEngine::DoHTTPGetL()
       
   210 // ----------------------------------------------------------------------------
       
   211 void CPosRevGeoCodeHTTPClientEngine::DoHTTPGetL()
       
   212     {
       
   213     FUNC("CPosRevGeoCodeHTTPClientEngine::DoHTTPGetL");
       
   214     // Parse string to URI (as defined in RFC2396)
       
   215     TUriParser8 uri;
       
   216     uri.Parse( *iUri );
       
   217     
       
   218     // Get request method string for HTTP GET
       
   219     RStringF method = iSession.StringPool().StringF( HTTP::EGET,RHTTPSession::GetTable() );
       
   220     CleanupClosePushL( method );
       
   221     // Open transaction with previous method and parsed uri. This class will
       
   222     // receive transaction events in MHFRunL and MHFRunError.
       
   223     iTransaction = iSession.OpenTransactionL( uri, *this, method );
       
   224     iTransactionOpen = ETrue;
       
   225     
       
   226     // Set headers for request; user agent and accepted content type
       
   227     RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
       
   228     SetHeaderL( hdr, HTTP::EUserAgent, KUserAgent );
       
   229     SetHeaderL( hdr, HTTP::EAccept, KAccept );
       
   230     
       
   231     // Submit the transaction. After this the framework will give transaction
       
   232     // events via MHFRunL and MHFRunError.
       
   233     iTransaction.SubmitL();
       
   234     CleanupStack::PopAndDestroy( 1 ); // method
       
   235     iObserver.ClientEvent( EHttpConnecting );
       
   236     }
       
   237 
       
   238 // ----------------------------------------------------------------------------
       
   239 // CPosRevGeoCodeHTTPClientEngine::CancelTransaction()
       
   240 // ----------------------------------------------------------------------------
       
   241 void CPosRevGeoCodeHTTPClientEngine::CancelTransaction()
       
   242     {
       
   243     FUNC("CPosRevGeoCodeHTTPClientEngine::CancelTransaction");
       
   244     iEngineState = EIdle;
       
   245     delete iUri; 
       
   246     iUri = NULL;
       
   247     
       
   248     // Close() also cancels transaction (Cancel() can also be used but
       
   249     // resources allocated by transaction must be still freed with Close())
       
   250     if( iTransactionOpen )
       
   251         {
       
   252         iTransaction.Close();
       
   253         iTransactionOpen = EFalse;
       
   254 
       
   255         iObserver.ClientEvent( EHttpTxCancelled );
       
   256         }
       
   257     }
       
   258 
       
   259 // ----------------------------------------------------------------------------
       
   260 // CPosRevGeoCodeHTTPClientEngine::MHFRunL()
       
   261 // ----------------------------------------------------------------------------
       
   262 void CPosRevGeoCodeHTTPClientEngine::MHFRunL( RHTTPTransaction aTransaction, const THTTPEvent& aEvent )
       
   263     {
       
   264     FUNC("CPosRevGeoCodeHTTPClientEngine::MHFRunL");
       
   265     switch ( aEvent.iStatus )
       
   266         {
       
   267         case THTTPEvent::EGotResponseHeaders:
       
   268             {
       
   269             // HTTP response headers have been received. Use
       
   270             // aTransaction.Response() to get the response. However, it's not
       
   271             // necessary to do anything with the response when this event occurs.
       
   272             iObserver.ClientEvent( EHttpHdrReceived );
       
   273             break;
       
   274             }
       
   275         case THTTPEvent::EGotResponseBodyData:
       
   276             {
       
   277             // Part (or all) of response's body data received. Use
       
   278             // aTransaction.Response().Body()->GetNextDataPart() to get the actual
       
   279             // body data.
       
   280         
       
   281             // Get the body data supplier
       
   282             MHTTPDataSupplier* body = aTransaction.Response().Body();
       
   283             TPtrC8 dataChunk;
       
   284         
       
   285             // GetNextDataPart() returns ETrue, if the received part is the last
       
   286             // one.
       
   287             TBool isLast = body->GetNextDataPart(dataChunk);
       
   288             iObserver.ClientBodyReceived(dataChunk);
       
   289         
       
   290             iObserver.ClientEvent( EHttpBytesReceieved );
       
   291 
       
   292             // NOTE: isLast may not be ETrue even if last data part received.
       
   293             // (e.g. multipart response without content length field)
       
   294             // Use EResponseComplete to reliably determine when body is completely
       
   295             // received.
       
   296             if( isLast )
       
   297                 {
       
   298                 iObserver.ClientEvent( EHttpBodyReceieved );
       
   299                 }
       
   300             // Always remember to release the body data.
       
   301             body->ReleaseData();
       
   302             break;
       
   303             }
       
   304         case THTTPEvent::EResponseComplete:
       
   305             {
       
   306             // Indicates that header & body of response is completely received.
       
   307             // No further action here needed.
       
   308 
       
   309             iObserver.ClientEvent( EHttpTxCompleted );
       
   310             break;
       
   311             }
       
   312         case THTTPEvent::ESucceeded:
       
   313             {
       
   314             // Indicates that transaction succeeded.
       
   315             iObserver.ClientEvent( EHttpTxSuccess );
       
   316             // Transaction can be closed now. It's not needed anymore.
       
   317             aTransaction.Close();
       
   318             iTransactionOpen = EFalse;
       
   319             break;
       
   320             }
       
   321         case THTTPEvent::EFailed:
       
   322             {
       
   323             // Transaction completed with failure.
       
   324             iObserver.ClientEvent( EHttpTxFailed );
       
   325             aTransaction.Close();
       
   326             iTransactionOpen = EFalse;
       
   327             break;
       
   328             }
       
   329         default:
       
   330             // There are more events in THTTPEvent, but they are not usually
       
   331             // needed. However, event status smaller than zero should be handled
       
   332             // correctly since it's error.
       
   333             {
       
   334             if ( aEvent.iStatus < 0 )
       
   335                 {
       
   336                 iObserver.ClientEvent( EHttpConnectionFailure );
       
   337                 // Close the transaction on errors
       
   338                 aTransaction.Close();
       
   339                 iTransactionOpen = EFalse;
       
   340                 }
       
   341                 break;
       
   342             }
       
   343         }
       
   344     }
       
   345 
       
   346 // ----------------------------------------------------------------------------
       
   347 // CPosRevGeoCodeHTTPClientEngine::MHFRunError()
       
   348 // ----------------------------------------------------------------------------
       
   349 TInt CPosRevGeoCodeHTTPClientEngine::MHFRunError( TInt /*aError*/, 
       
   350                                                   RHTTPTransaction /*aTransaction*/,
       
   351                                                   const THTTPEvent& /*aEvent*/ )
       
   352     {
       
   353     FUNC("CPosRevGeoCodeHTTPClientEngine::MHFRunError");
       
   354     // Just notify about the error and return KErrNone.
       
   355     CloseConnection();
       
   356     iObserver.ClientEvent(EHttpMhfRunError);
       
   357     return KErrNone;
       
   358     }
       
   359 
       
   360 // ----------------------------------------------------------------------------
       
   361 // CPosRevGeoCodeHTTPClientEngine::PreferredCarrierAvailable()
       
   362 // ----------------------------------------------------------------------------
       
   363 void CPosRevGeoCodeHTTPClientEngine::PreferredCarrierAvailable( 
       
   364                                                TAccessPointInfo /*aOldAPInfo*/,
       
   365                                                TAccessPointInfo /*aNewAPInfo*/,
       
   366                                                TBool /*aIsUpgrade*/,
       
   367                                                TBool aIsSeamless )
       
   368     {
       
   369     FUNC("CPosRevGeoCodeHTTPClientEngine::PreferredCarrierAvailable");
       
   370     if( !aIsSeamless && iMobility)
       
   371         {
       
   372         iMobility->MigrateToPreferredCarrier();
       
   373         }
       
   374     }
       
   375 
       
   376 // ----------------------------------------------------------------------------
       
   377 // CPosRevGeoCodeHTTPClientEngine::NewCarrierActive()
       
   378 // ----------------------------------------------------------------------------
       
   379 void CPosRevGeoCodeHTTPClientEngine::NewCarrierActive( TAccessPointInfo /*aNewAPInfo*/,
       
   380                                                       TBool aIsSeamless )
       
   381     {
       
   382     FUNC("CPosRevGeoCodeHTTPClientEngine::NewCarrierActive");
       
   383     if( !aIsSeamless && iMobility)
       
   384         {
       
   385         iMobility->NewCarrierAccepted();
       
   386         }
       
   387     }
       
   388 
       
   389 // ----------------------------------------------------------------------------
       
   390 // CPosRevGeoCodeHTTPClientEngine::Error()
       
   391 // ----------------------------------------------------------------------------
       
   392 void CPosRevGeoCodeHTTPClientEngine::Error(TInt /*aError*/)
       
   393     {
       
   394     FUNC("CPosRevGeoCodeHTTPClientEngine::Error");
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------------------------------
       
   398 // CPosRevGeoCodeHTTPClientEngine::DoCancel()
       
   399 // ----------------------------------------------------------------------------
       
   400 void CPosRevGeoCodeHTTPClientEngine::DoCancel()
       
   401     {
       
   402     FUNC("CPosRevGeoCodeHTTPClientEngine::DoCancel");
       
   403     iConnection.Stop();
       
   404     }
       
   405 
       
   406 // ----------------------------------------------------------------------------
       
   407 // CPosRevGeoCodeHTTPClientEngine::DoCancel()
       
   408 // ----------------------------------------------------------------------------
       
   409 TInt CPosRevGeoCodeHTTPClientEngine::RunError(TInt /*aError*/)
       
   410     {
       
   411     FUNC("CPosRevGeoCodeHTTPClientEngine::RunError");
       
   412     // Just notify about the error and return KErrNone.
       
   413     CloseConnection();
       
   414     iObserver.ClientEvent( EHttpTxFailed );
       
   415     return KErrNone;
       
   416     }
       
   417 
       
   418 // ----------------------------------------------------------------------------
       
   419 // CPosRevGeoCodeHTTPClientEngine::RunL()
       
   420 // ----------------------------------------------------------------------------
       
   421 void CPosRevGeoCodeHTTPClientEngine::RunL()
       
   422     {
       
   423     FUNC("CPosRevGeoCodeHTTPClientEngine::RunL");
       
   424     TInt statusCode = iStatus.Int();
       
   425     
       
   426     if ( statusCode == KErrNone )
       
   427         {
       
   428         // Connection done ok
       
   429         iConnectionSetupDone = ETrue;
       
   430         
       
   431         RStringPool strPool = iSession.StringPool();
       
   432         CleanupClosePushL( strPool );
       
   433         // Remove first session properties just in case.
       
   434         RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
       
   435         
       
   436         // Clear RConnection and Socket Server instances
       
   437         connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketServ,RHTTPSession::GetTable()));
       
   438         connInfo.RemoveProperty(strPool.StringF(HTTP::EHttpSocketConnection,RHTTPSession::GetTable()));
       
   439         
       
   440         // Clear the proxy settings
       
   441         connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyUsage,RHTTPSession::GetTable()));
       
   442         connInfo.RemoveProperty(strPool.StringF(HTTP::EProxyAddress,RHTTPSession::GetTable()));
       
   443         
       
   444         // RConnection and Socket Server
       
   445         connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketServ, 
       
   446                                         RHTTPSession::GetTable()), 
       
   447                                 THTTPHdrVal (iSocketServ.Handle()) );
       
   448         
       
   449         TInt connPtr1 = REINTERPRET_CAST(TInt, &iConnection);
       
   450         connInfo.SetPropertyL ( strPool.StringF(HTTP::EHttpSocketConnection, 
       
   451                                 RHTTPSession::GetTable() ), THTTPHdrVal (connPtr1) );    
       
   452 
       
   453         // Register for mobility API
       
   454         delete iMobility;
       
   455         iMobility = NULL;
       
   456         iMobility = CActiveCommsMobilityApiExt::NewL( iConnection, *this );
       
   457         CleanupStack::Pop( &strPool ); // strPool
       
   458         
       
   459         // Start selected HTTP action
       
   460         switch( iEngineState )
       
   461             {
       
   462             case EIdle:
       
   463                 {
       
   464                 //
       
   465                 CancelTransaction();
       
   466                 break;
       
   467                 }
       
   468             case EGet:
       
   469                {
       
   470                DoHTTPGetL();
       
   471                break;
       
   472                }
       
   473             };
       
   474         }
       
   475     else
       
   476         {
       
   477         //handle error
       
   478         if ( statusCode == KErrPermissionDenied )
       
   479             {
       
   480             iObserver.ClientEvent( EHttpAuthFailed );
       
   481             }
       
   482         else
       
   483             {
       
   484             //Throw some general Transaction falure error!
       
   485             iObserver.ClientEvent( EHttpTxFailed );
       
   486             }
       
   487         CloseConnection();
       
   488         }
       
   489     }
       
   490 
       
   491 // End of file