uiacceltk/hitchcock/Client/src/alfclientBase.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2007 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:   Server client for alfredserver.exe
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <apgcli.h>
       
    21 #include <centralrepository.h>
       
    22 #include <alf/ftokenclient.h>
       
    23 
       
    24 #include <eikenv.h>
       
    25 #include "alf/alfenv.h"
       
    26 #include "alfuids.h"
       
    27 #include "alf/alfclientbase.h"
       
    28 #include "alflogger.h"
       
    29 #include "alf/alfconstants.h"
       
    30 #include <goommonitor.h>
       
    31 #include <pslninternalcrkeys.h>
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 
       
    35 #ifndef __WINS__
       
    36 #define START_GOOM_FROM_ALF_CLIENT	 
       
    37 #endif
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Constructor
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 EXPORT_C RAlfClientBase::RAlfClientBase(TInt aServiceUid)
       
    44     : RAknAppServiceBase(), iServiceUid(aServiceUid), iEnv(0), iCmdLine(0), iApa(0)
       
    45     {
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Opens the connection
       
    50 // ---------------------------------------------------------------------------
       
    51 //    
       
    52 EXPORT_C void RAlfClientBase::OpenL()
       
    53     {
       
    54     __ALFLOGSTRING( "RAlfClient::OpenL start" )
       
    55     TUint differentiator = 
       
    56         StartServerL( TUid::Uid(KAlfAppServerInterfaceUid3) );
       
    57     TName serverName;
       
    58     ConstructServerName( 
       
    59         serverName, 
       
    60         TUid::Uid(KAlfAppServerInterfaceUid3), 
       
    61         differentiator );
       
    62     ConnectExistingByNameL( serverName );
       
    63     __ALFLOGSTRING( "RAlfClient::OpenL end" )
       
    64     }
       
    65   
       
    66 // ---------------------------------------------------------------------------
       
    67 // Creates server name
       
    68 // ---------------------------------------------------------------------------
       
    69 //    
       
    70 EXPORT_C void RAlfClientBase::ConstructServerName( TName& aServerName, 
       
    71                                         TUid aAppServerUid, 
       
    72                                         TUint aServerDifferentiator )
       
    73     {
       
    74 
       
    75     _LIT(KServerNameFormat, "%08x_%08x_AppServer");
       
    76     aServerName.Format( 
       
    77         KServerNameFormat, 
       
    78         aServerDifferentiator, 
       
    79         aAppServerUid.iUid );
       
    80     }    
       
    81     
       
    82 // ---------------------------------------------------------------------------
       
    83 // Creates name, launches server and waits it to start.
       
    84 // ---------------------------------------------------------------------------
       
    85 // 
       
    86 EXPORT_C TUint RAlfClientBase::StartServerL( TUid aAppUid )
       
    87     {
       
    88     // Start the server application
       
    89     TName serverName;
       
    90     TUint differentiator( 0 );
       
    91     
       
    92     differentiator = KAlfAppServerInterfaceUid3;
       
    93     ConstructServerName( 
       
    94         serverName, 
       
    95         TUid::Uid(KAlfAppServerInterfaceUid3) , 
       
    96         differentiator );
       
    97         
       
    98     TFindServer find( serverName );
       
    99     TFullName fullName;
       
   100     if ( find.Next( fullName ) == KErrNone )
       
   101         {
       
   102         return differentiator;
       
   103         }
       
   104     
       
   105     TThreadId serverThreadId;
       
   106     LaunchAppL( aAppUid, differentiator, serverThreadId );
       
   107 
       
   108     return differentiator;
       
   109     }
       
   110     
       
   111 // ---------------------------------------------------------------------------
       
   112 // Launches the app server.
       
   113 // ---------------------------------------------------------------------------
       
   114 // 
       
   115 EXPORT_C void RAlfClientBase::LaunchAppL( 
       
   116     TUid aAppUid, 
       
   117     TUint aServerDifferentiator, 
       
   118     TThreadId& aThreadId )
       
   119 	{
       
   120 	RApaLsSession apa;
       
   121 	User::LeaveIfError( apa.Connect() );
       
   122 	CleanupClosePushL( apa );
       
   123 	
       
   124 	TApaAppInfo info;
       
   125 	User::LeaveIfError( apa.GetAppInfo( info, aAppUid ) );
       
   126 
       
   127 	CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   128 	cmdLine->SetExecutableNameL( info.iFullName );
       
   129 	cmdLine->SetServerRequiredL( aServerDifferentiator );
       
   130     // Set the command to start the server in background
       
   131     cmdLine->SetCommandL( EApaCommandBackground );        
       
   132 
       
   133 	TRequestStatus status;	
       
   134 	TInt err = apa.StartApp( *cmdLine, aThreadId, &status );
       
   135     
       
   136     User::LeaveIfError( err );
       
   137     
       
   138     User::WaitForRequest(status);
       
   139     
       
   140     User::LeaveIfError( status.Int() );
       
   141     
       
   142 	CleanupStack::PopAndDestroy( cmdLine );
       
   143 	CleanupStack::PopAndDestroy( &apa );
       
   144 	}
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // From class RAknAppServiceBase.
       
   148 // Returns the service UID supported.
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 EXPORT_C TUid RAlfClientBase::ServiceUid() const
       
   152     {
       
   153     return TUid::Uid(iServiceUid);
       
   154     }
       
   155 
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Grant access
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 EXPORT_C TInt RAlfClientBase::GrantAccessToPrivateFile( 
       
   162                                             const TDesC& aFileName, 
       
   163                                             TInt aObjectIdentfier, 
       
   164                                             TInt aCommandId )
       
   165     {
       
   166     if (!iEnv || !iEnv->TokenClient())
       
   167         {
       
   168         return KErrNotReady;        
       
   169         }
       
   170     
       
   171     TInt64 token = 0;
       
   172     TPckg<TInt64> tokenBuf(token);
       
   173     TInt err = iEnv->TokenClient()->GenerateToken(aFileName, TUid::Uid(KAlfAppServerInterfaceUid3), tokenBuf);
       
   174     if (!err)
       
   175         {
       
   176         err = SendReceive(EAlfDoSubSessionCmd, TIpcArgs(aCommandId,&tokenBuf,&tokenBuf,aObjectIdentfier));
       
   177         }
       
   178  
       
   179     return err;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Grant access
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C TInt RAlfClientBase::GrantAccessToPrivateFile( 
       
   187                                             const RFile& aFile, 
       
   188                                             TInt aObjectIdentfier, 
       
   189                                             TInt aCommandId )
       
   190    {
       
   191     if (!iEnv || !iEnv->TokenClient())
       
   192         {
       
   193         return KErrNotReady;        
       
   194         }
       
   195     
       
   196     TInt64 token = 0;
       
   197     TPckg<TInt64> tokenBuf(token);
       
   198     TInt err = iEnv->TokenClient()->GenerateToken(aFile, TUid::Uid(KAlfAppServerInterfaceUid3), tokenBuf);
       
   199     if (!err)
       
   200         {
       
   201         err = SendReceive(EAlfDoSubSessionCmd, TIpcArgs(aCommandId,&tokenBuf,&tokenBuf,aObjectIdentfier));
       
   202         }
       
   203  
       
   204     return err;
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // Sets env
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void RAlfClientBase::SetAlfEnv(CAlfEnv& aAlf)
       
   212     {
       
   213     iEnv = &aAlf;
       
   214     }
       
   215 
       
   216 void RAlfClientBase::StartAsyncL(TRequestStatus* aStatus)
       
   217     {
       
   218     ASSERT(iApa==0 && iCmdLine == 0);
       
   219     // Start the server application
       
   220     TName serverName;
       
   221     TUint differentiator( 0 );
       
   222     
       
   223     differentiator = KAlfAppServerInterfaceUid3;
       
   224     ConstructServerName( 
       
   225         serverName, 
       
   226         TUid::Uid(KAlfAppServerInterfaceUid3) , 
       
   227         differentiator );
       
   228     
       
   229     TFindServer serverFinder(serverName);
       
   230     TFullName fullName;
       
   231     if (serverFinder.Next(fullName) == KErrNone)
       
   232         {
       
   233         User::Leave(KErrAlreadyExists);
       
   234         }
       
   235         
       
   236     TThreadId threadId;
       
   237     // we don't have proper destructor and thus we don't take
       
   238     // "normal" ownership on our members...
       
   239     
       
   240     // assign to member after poping from cleanup stack - codescanner now happy
       
   241   RApaLsSession*  apa = new (ELeave) RApaLsSession;
       
   242 	CleanupStack::PushL(apa);
       
   243 	User::LeaveIfError( apa->Connect() );
       
   244 	CleanupClosePushL( *apa );
       
   245 	
       
   246 	TApaAppInfo info;
       
   247 	User::LeaveIfError( apa->GetAppInfo( info, TUid::Uid(KAlfAppServerInterfaceUid3) ) );
       
   248 
       
   249 	CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   250 	cmdLine->SetExecutableNameL( info.iFullName );
       
   251 	cmdLine->SetServerRequiredL( differentiator );
       
   252     // Set the command to start the server in background
       
   253     cmdLine->SetCommandL( EApaCommandBackground );        
       
   254 
       
   255 	User::LeaveIfError(apa->StartApp( *cmdLine, threadId, aStatus ));
       
   256 
       
   257     CleanupStack::Pop(3);
       
   258     iCmdLine=cmdLine;
       
   259     iApa = apa;
       
   260     }
       
   261 
       
   262 #if !defined(__WINS__)    
       
   263 _LIT(KBgAnimExeName, "z:\\sys\\bin\\backgroundanimhost.exe");
       
   264 #endif
       
   265 
       
   266 void RAlfClientBase::CompleteAsynchConstructionL()
       
   267     {
       
   268     if (iApa)
       
   269         {    
       
   270         delete iCmdLine;
       
   271         iCmdLine = 0; 
       
   272         iApa->Close();
       
   273         delete iApa;
       
   274         iApa = 0;
       
   275 
       
   276         TName serverName;
       
   277             ConstructServerName( 
       
   278             serverName, 
       
   279             TUid::Uid(KAlfAppServerInterfaceUid3), 
       
   280             KAlfAppServerInterfaceUid3 );
       
   281         ConnectExistingByNameL( serverName );
       
   282         }
       
   283     else
       
   284         {
       
   285     	TInt value = 0;
       
   286         CRepository* repo = CRepository::NewLC(KCRUidThemes);
       
   287         User::LeaveIfError(repo->Get(KThemesAnimBackgroundSupport, value));
       
   288         CleanupStack::PopAndDestroy();
       
   289 #if !defined(__WINS__)
       
   290         if (!value)
       
   291             {         
       
   292             RProcess image;
       
   293             User::LeaveIfError(image.Create(KBgAnimExeName,KNullDesC));
       
   294             image.Resume();
       
   295             image.Close();
       
   296             }
       
   297 #endif
       
   298         }
       
   299 	       
       
   300     }
       
   301 
       
   302 CAlfAsynchStartup::CAlfAsynchStartup():CActive(CActive::EPriorityHigh), iClient(RAlfClientBase(KAlfAppServerInterfaceUid3))
       
   303     {
       
   304     CActiveScheduler::Add(this);
       
   305     }
       
   306 
       
   307 EXPORT_C void CAlfAsynchStartup::StartL()
       
   308     {
       
   309 	  TInt err(KErrNone);
       
   310 	
       
   311 #ifdef START_GOOM_FROM_ALF_CLIENT	 
       
   312 	  // do the actual goom startup : starting this first in case something goes wrong and startup needs to stop
       
   313     TRAP( err, CreateGOOMWatcherThreadL());
       
   314 	if(err != KErrNone)
       
   315 		{
       
   316 	    RDebug::Print(_L("Creating out of graphics mem thread failed with err %d"), err);
       
   317 		}
       
   318 #endif
       
   319 	
       
   320 	//everything went ok for goom: moving over for alf now
       
   321     CAlfAsynchStartup* me = new (ELeave) CAlfAsynchStartup();
       
   322     TRAP(err, me->iClient.StartAsyncL(&(me->iStatus)));
       
   323     if ( err )
       
   324         {
       
   325         if (err == KErrAlreadyExists) // NGA Always
       
   326             {
       
   327             me->SetActive();    
       
   328             TRequestStatus* sptr = &(me->iStatus);
       
   329             User::RequestComplete(sptr, err);    
       
   330             }    
       
   331         else
       
   332             {
       
   333             // just die gracefully
       
   334             delete me;
       
   335             }
       
   336         return;
       
   337         }
       
   338 
       
   339     // everything fine, wait for rendezvous
       
   340     me->SetActive();    
       
   341     }
       
   342     
       
   343 void CAlfAsynchStartup::RunL()
       
   344     {
       
   345 
       
   346     TRAP_IGNORE(iClient.CompleteAsynchConstructionL());
       
   347     iClient.Close();
       
   348     
       
   349     delete this;
       
   350     }
       
   351 
       
   352 void CAlfAsynchStartup::DoCancel()
       
   353     {
       
   354     // not possible at the moment
       
   355     }
       
   356 
       
   357 // end of file
       
   358