wlanutilities/wlansniffer/model/src/wsfapplauncher.cpp
branchRCL_3
changeset 54 63be7eb3fc78
parent 52 b852595f5cbe
child 55 f28ada11abbf
equal deleted inserted replaced
52:b852595f5cbe 54:63be7eb3fc78
     1 /*
       
     2 * Copyright (c) 2007-2008 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 CWsfAppLauncher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 //  EXTERNAL INCLUDES
       
    22 #include <apgtask.h>
       
    23 #include <apgcli.h>
       
    24 #include <centralrepository.h>
       
    25 #include <browseruisdkcrkeys.h>
       
    26 #include <cmmanager.h>
       
    27 #include <cmconnectionmethod.h>
       
    28  
       
    29 //  CLASS HEADER
       
    30 #include "wsfapplauncher.h"
       
    31 
       
    32 //  INTERNAL INCLUDES
       
    33 #include "wsfbrowserlaunchobserver.h"
       
    34 #include "wsflogger.h"
       
    35 
       
    36 
       
    37 //  LOCAL DEFINITIONS
       
    38 /**
       
    39 * Delay that we wait for the browser to start or terminate
       
    40 */
       
    41 static const TInt KTimerTickInterval = 2 * 1000 * 1000;    
       
    42 
       
    43 /**
       
    44 * Repository key ID for the browser's homepage URL
       
    45 */
       
    46 static const TUint32 KBrowserNGHomepageURL = 0x00000030;
       
    47 
       
    48 
       
    49 #ifdef _DEBUG
       
    50     _LIT( KBrowserLauncherPanic, "CWsfAppLauncher" );
       
    51     #define _ASSERTD( cond ) __ASSERT_DEBUG( (cond), \
       
    52             User::Panic( KBrowserLauncherPanic, __LINE__) )
       
    53 #else
       
    54     #define _ASSERTD( cond ) {}
       
    55 #endif //_DEBUG
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CWsfAppLauncher::NewL
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C CWsfAppLauncher* CWsfAppLauncher::NewL()
       
    63     {
       
    64     CWsfAppLauncher* self = CWsfAppLauncher::NewLC();
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // CWsfAppLauncher::NewLC
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C CWsfAppLauncher* CWsfAppLauncher::NewLC()
       
    75     {
       
    76     CWsfAppLauncher* self = new( ELeave ) CWsfAppLauncher();
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     return self;
       
    80     }
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CWsfAppLauncher::~CWsfAppLauncher
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CWsfAppLauncher::~CWsfAppLauncher()
       
    88     {
       
    89     Cancel();
       
    90     delete iURL;
       
    91     delete iRepository;
       
    92     iTimer.Close();
       
    93     iWsSession.Close();     
       
    94     }
       
    95 
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CWsfAppLauncher::CWsfAppLauncher
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CWsfAppLauncher::CWsfAppLauncher(): 
       
   102     CActive( CActive::EPriorityStandard ),
       
   103     iIapId( 0 ), 
       
   104     iLaunchState( EIdle ),
       
   105     iLaunchBookMarks( EFalse )
       
   106     {
       
   107     }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CWsfAppLauncher::ConstructL
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CWsfAppLauncher::ConstructL()
       
   115     {
       
   116     LOG_ENTERFN( "CWsfAppLauncher::ConstructL" );
       
   117     iBrowserUid = KCRUidBrowser;
       
   118     iHomepageKey = KBrowserNGHomepageURL; 
       
   119     iBrowserStartingPageKey = KBrowserNGHomepageType;
       
   120 
       
   121     // Common settings for both browsers
       
   122     iRepository = CRepository::NewL( KCRUidBrowser );
       
   123     
       
   124     User::LeaveIfError( iWsSession.Connect() );    
       
   125     User::LeaveIfError( iTimer.CreateLocal() );
       
   126 
       
   127     iURL = KNullDesC().AllocL();    
       
   128     CActiveScheduler::Add( this );
       
   129     }
       
   130 
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CWsfAppLauncher::LaunchBrowserL
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CWsfAppLauncher::LaunchBrowserL( MWsfBrowserLaunchObserver& aObserver, 
       
   137                                       TUint aIapId,
       
   138                                       const TDesC& aURL )
       
   139     {
       
   140     LOG_ENTERFN( "CWsfAppLauncher::LaunchBrowserL_3" );
       
   141     Cancel();
       
   142     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_3 -->> afer cancel" );
       
   143     iObserver = &aObserver;  
       
   144     iIapId = aIapId;
       
   145     HBufC* url = aURL.AllocL();
       
   146     delete iURL;
       
   147     iURL = url;
       
   148 
       
   149     iLaunchState = EIdle;    
       
   150     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_3 -->> before set active" );
       
   151     SetActive();
       
   152     TRequestStatus* status = &iStatus;
       
   153     User::RequestComplete( status, KErrNone );
       
   154     }
       
   155 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CWsfAppLauncher::LaunchBrowserL
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CWsfAppLauncher::LaunchBrowserL( MWsfBrowserLaunchObserver& aObserver, 
       
   162                                       TUint aIapId )
       
   163     {
       
   164     LOG_ENTERFN( "CWsfAppLauncher::LaunchBrowserL_2" );
       
   165     LOG_WRITEF( "CWsfAppLauncher::LaunchBrowserL_2 -->> iIapid %d", aIapId );
       
   166     HBufC* url = HBufC::NewLC( 
       
   167                         NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
   168     TPtr urlPtr( url->Des() );
       
   169     
       
   170     iLaunchBookMarks = EFalse;
       
   171     
       
   172     RCmManager cmManager;
       
   173     
       
   174     cmManager.OpenL();
       
   175     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> cmmanager opened" );
       
   176     CleanupClosePushL( cmManager );
       
   177 
       
   178     RCmConnectionMethod plugin;
       
   179     CleanupClosePushL( plugin );
       
   180     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> cm before open" );
       
   181     LOG_WRITEF( "CWsfAppLauncher::LaunchBrowserL_2 -->> iIapid %d", aIapId );
       
   182     plugin = cmManager.ConnectionMethodL( aIapId );
       
   183     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> cm after open" );
       
   184     
       
   185     // do we have start page for Access Point?
       
   186     HBufC* apHomepage = NULL;
       
   187     apHomepage = plugin.GetStringAttributeL( CMManager::ECmStartPage );
       
   188     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> String Attrib got" );
       
   189     CleanupStack::PushL( apHomepage );
       
   190  
       
   191     if( apHomepage->Length() == 0 )
       
   192         {
       
   193         LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> hplength = 0" );
       
   194         // if we can't have Access Point URL then
       
   195         // we try to get browser homepage URL
       
   196         TInt err = BrowserHomepage( urlPtr );   
       
   197         
       
   198         // if browser homepage is not defined either,
       
   199         // launch bookmarks view
       
   200         if ( err || url->Length() == 0 )
       
   201             {
       
   202             LOG_WRITE( 
       
   203                    "CWsfAppLauncher::LaunchBrowserL_2 -->> err in url length" );
       
   204             iLaunchBookMarks = ETrue;
       
   205             }
       
   206         }
       
   207     else
       
   208         {
       
   209         LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> hplength>0" );
       
   210         url->Des().Copy( *apHomepage );
       
   211         }
       
   212     
       
   213     LaunchBrowserL( aObserver, aIapId, *url );
       
   214     
       
   215     LOG_WRITE( "CWsfAppLauncher::LaunchBrowserL_2 -->> after launch _3" );
       
   216     CleanupStack::PopAndDestroy( apHomepage );
       
   217     CleanupStack::PopAndDestroy( &plugin );
       
   218     CleanupStack::PopAndDestroy( &cmManager );
       
   219     CleanupStack::PopAndDestroy( url );
       
   220     }
       
   221 
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 // CWsfAppLauncher::DoLaunchBrowserL
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 void CWsfAppLauncher::DoLaunchBrowserL()
       
   228     {
       
   229     LOG_ENTERFN( "CWsfAppLauncher::DoLaunchBrowserL" );
       
   230     _LIT( KFormatCommand, "%d %S" );
       
   231     const TInt KBrowserFirstParamUrlFollows = 4;
       
   232     
       
   233     iLaunchState = EStartingUp;    
       
   234     HBufC* param = NULL; 
       
   235     if ( iLaunchBookMarks )
       
   236         {
       
   237         param = KNullDesC().AllocLC();
       
   238         }
       
   239     else
       
   240         {
       
   241         param = HBufC::NewLC( KFormatCommand().Length() + iURL->Length() );
       
   242         param->Des().Format( KFormatCommand, 
       
   243                              KBrowserFirstParamUrlFollows, iURL );
       
   244         }
       
   245         
       
   246     RApaLsSession appArcSession;
       
   247     User::LeaveIfError( appArcSession.Connect() ); // connect to AppArc server
       
   248     CleanupClosePushL( appArcSession );
       
   249                      
       
   250     User::LeaveIfError( appArcSession.StartDocument( *param, iBrowserUid, 
       
   251                                                                   iThreadId ) );
       
   252 
       
   253     CleanupStack::PopAndDestroy( &appArcSession );
       
   254     CleanupStack::PopAndDestroy( param );
       
   255 
       
   256 
       
   257     iTimer.Cancel();
       
   258     iTimer.After( iStatus, KTimerTickInterval );
       
   259     SetActive();
       
   260     }
       
   261 
       
   262 
       
   263 // ---------------------------------------------------------------------------
       
   264 // CWsfAppLauncher::BrowserExists
       
   265 // ---------------------------------------------------------------------------
       
   266 //
       
   267 TBool CWsfAppLauncher::BrowserExists()
       
   268     {
       
   269     LOG_ENTERFN( "CWsfAppLauncher::BrowserExists" );
       
   270     TApaTaskList taskList( iWsSession );            
       
   271     TApaTask startedtask = taskList.FindApp( iBrowserUid );
       
   272     return startedtask.Exists();   
       
   273     }
       
   274 
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // CWsfAppLauncher::BrowserHomepage
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 TInt CWsfAppLauncher::BrowserHomepage( TDes& aHomePageURL )
       
   281     {
       
   282     LOG_ENTERFN( "CWsfAppLauncher::BrowserHomepage" );
       
   283     CRepository* repository( iRepository );
       
   284 
       
   285     // get the default starting page setting
       
   286     TInt startingPageMode( 0 );
       
   287     TInt err = repository->Get( iBrowserStartingPageKey, startingPageMode );
       
   288     if ( err == KErrNone )
       
   289         {
       
   290         switch ( startingPageMode )
       
   291             {
       
   292             case 1:
       
   293             case 2:
       
   294                 {
       
   295                 // user defined or current page
       
   296                  err = repository->Get( iHomepageKey, aHomePageURL );
       
   297                 break;
       
   298                 }
       
   299             default:
       
   300                 {
       
   301                 aHomePageURL.Zero();                     
       
   302                 }
       
   303             }
       
   304         }
       
   305 
       
   306     return err;    
       
   307     }
       
   308 
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CWsfAppLauncher::KillBrowserIfAlreadyExists
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 TBool CWsfAppLauncher::KillBrowserIfAlreadyExists()
       
   315     {
       
   316     LOG_ENTERFN( "CWsfAppLauncher::KillBrowserIfAlreadyExists" );
       
   317     _ASSERTD( iLaunchState == EIdle);    
       
   318     _ASSERTD( !IsActive() );       
       
   319          
       
   320     TBool killing = EFalse;
       
   321     
       
   322     TApaTaskList taskList( iWsSession );
       
   323     TApaTask task = taskList.FindApp( iBrowserUid );
       
   324 
       
   325     if ( task.Exists() )
       
   326         {    
       
   327         // kill the browser...
       
   328         task.EndTask();
       
   329         killing = ETrue;
       
   330 
       
   331         iTimer.Cancel();
       
   332         iTimer.After( iStatus, KTimerTickInterval );
       
   333         }    
       
   334     else
       
   335         {
       
   336         TRequestStatus* status = &iStatus;
       
   337         User::RequestComplete( status, KErrNone );  
       
   338         }
       
   339         
       
   340     SetActive();
       
   341     iLaunchState = EShuttingDown;
       
   342     return killing;
       
   343     }
       
   344 
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // CWsfAppLauncher::ContinueBrowsingL
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void CWsfAppLauncher::ContinueBrowsingL()
       
   351     {
       
   352     LOG_ENTERFN( "CWsfAppLauncher::ContinueBrowsingL_0" );
       
   353     //Check if the application is already running
       
   354     TBool exists = BrowserExists();
       
   355     if ( exists )
       
   356         {
       
   357         TApaTaskList taskList( iWsSession );
       
   358         TApaTask task = taskList.FindApp( iBrowserUid );
       
   359         task.BringToForeground();
       
   360         }
       
   361     else
       
   362         {
       
   363         User::Leave( KErrNotFound );    
       
   364         }       
       
   365     }
       
   366 
       
   367     
       
   368 // ---------------------------------------------------------------------------
       
   369 // CWsfAppLauncher::ContinueBrowsingL
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 void CWsfAppLauncher::ContinueBrowsingL( MWsfBrowserLaunchObserver& aObserver, 
       
   373                                          TUint aIapId )
       
   374     {
       
   375     LOG_ENTERFN( "CWsfAppLauncher::ContinueBrowsingL_2" );
       
   376     //Check if the application is already running
       
   377     TBool exists = BrowserExists();
       
   378     if ( exists )
       
   379         {
       
   380         TApaTaskList taskList( iWsSession );
       
   381         TApaTask task = taskList.FindApp( iBrowserUid );
       
   382         task.BringToForeground();
       
   383         }
       
   384     else    // browser not running - launch browser
       
   385         {
       
   386         LaunchBrowserL( aObserver, aIapId );
       
   387         }       
       
   388     }    
       
   389 
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // CWsfAppLauncher::Launching
       
   393 // ---------------------------------------------------------------------------
       
   394 //
       
   395 TWsfLaunchState CWsfAppLauncher::Launching()
       
   396     {
       
   397     LOG_ENTERFN( "CWsfAppLauncher::Launching" );
       
   398     return iLaunchState;    
       
   399     }
       
   400 
       
   401 
       
   402 // ---------------------------------------------------------------------------
       
   403 // CWsfAppLauncher::BrowserIap
       
   404 // ---------------------------------------------------------------------------
       
   405 //
       
   406 TUint32 CWsfAppLauncher::BrowserIap() const
       
   407     {
       
   408     return iIapId;
       
   409     }
       
   410 
       
   411 
       
   412 // ---------------------------------------------------------------------------
       
   413 // CWsfAppLauncher::DoCancel
       
   414 // ---------------------------------------------------------------------------
       
   415 //
       
   416 void CWsfAppLauncher::DoCancel()
       
   417     {
       
   418     LOG_ENTERFN( "CWsfAppLauncher::DoCancel" ); 
       
   419     iThread.LogonCancel( iStatus );
       
   420     
       
   421     iThread.Close();
       
   422     iTimer.Cancel();
       
   423     iLaunchState = EIdle;
       
   424     }
       
   425 
       
   426 
       
   427 // ---------------------------------------------------------------------------
       
   428 // CWsfAppLauncher::RunL
       
   429 // ---------------------------------------------------------------------------
       
   430 //
       
   431 void CWsfAppLauncher::RunL()
       
   432     {
       
   433     LOG_ENTERFN( "CWsfAppLauncher::RunL" ); 
       
   434     _ASSERTD( iObserver ); 
       
   435     User::LeaveIfError( iStatus.Int() );
       
   436     
       
   437     switch( iLaunchState )
       
   438         {
       
   439         case EIdle: // Starting to launch
       
   440             {
       
   441             LOG_WRITE( "CWsfAppLauncher::RunL -->> EIdle" );
       
   442             KillBrowserIfAlreadyExists();  
       
   443             break;    
       
   444             }
       
   445         case EShuttingDown: // Existing browser was succesfully closed 
       
   446             {
       
   447             LOG_WRITE( "CWsfAppLauncher::RunL -->> EShuttingDown" );
       
   448             DoLaunchBrowserL();
       
   449             break;    
       
   450             }
       
   451         case EStartingUp:  // Start-up completed, check browser exists 
       
   452             {
       
   453             LOG_WRITE( "CWsfAppLauncher::RunL -->> EStartingUp" );
       
   454             TBool exists = BrowserExists();
       
   455             TInt err = exists ? KErrNone : KErrNotFound;
       
   456             iLaunchState = ECompleted;
       
   457             TRequestStatus* status = &iStatus;
       
   458             User::RequestComplete( status, err );
       
   459             SetActive();    
       
   460             break;    
       
   461             }
       
   462         case ECompleted:  //Browser exists, notify observer about completion
       
   463             {
       
   464             LOG_WRITE( "CWsfAppLauncher::RunL -->> ECompleted" );  
       
   465             iLaunchState = EFinished;
       
   466             LOG_WRITE( "CWsfAppLauncher::RunL -->> Before thread open" );
       
   467             User::LeaveIfError( iThread.Open( iThreadId, EOwnerProcess ) );
       
   468             LOG_WRITE( "CWsfAppLauncher::RunL -->> Thread opened" );
       
   469             iObserver->BrowserLaunchCompleteL(); 
       
   470             TRequestStatus* status = &iStatus;
       
   471             iThread.Logon( *status );
       
   472             SetActive();    
       
   473             break;    
       
   474             }
       
   475         case EFinished:  //Browser exists, notify observer about completion
       
   476             {
       
   477             LOG_WRITE( "CWsfAppLauncher::RunL -->> EFinished" );
       
   478             iObserver->BrowserExitL();  
       
   479             iLaunchState = EIdle;
       
   480             break;    
       
   481             }
       
   482         default:
       
   483             {
       
   484             _ASSERTD( EFalse ); 
       
   485             }
       
   486         }    
       
   487     }
       
   488 
       
   489 // ---------------------------------------------------------------------------
       
   490 // CWsfAppLauncher::RunError
       
   491 // ---------------------------------------------------------------------------
       
   492 //
       
   493 TInt CWsfAppLauncher::RunError( TInt aError )
       
   494     {
       
   495     LOG_ENTERFN( "CWsfAppLauncher::RunError" ); 
       
   496     _ASSERTD( iObserver ); 
       
   497 
       
   498     switch( iLaunchState )
       
   499         {
       
   500         case EIdle:         // 
       
   501         case EShuttingDown: // Shuttind down existing browser failed
       
   502         case EStartingUp:   // Starting up new browser failed
       
   503         case ECompleted:   // Starting up new browser failed
       
   504             {   
       
   505             iObserver->BrowserLaunchFailed( aError );
       
   506             break;    
       
   507             }
       
   508         default:
       
   509             {
       
   510             _ASSERTD( EFalse ); 
       
   511             }
       
   512         }    
       
   513         
       
   514     iLaunchState = EIdle;
       
   515     return aError;
       
   516     }
       
   517 
       
   518