iaupdate/IAD/api/client/src/iaupdateclient.cpp
changeset 0 ba25891c3a9e
child 29 26b6f0522fd8
child 36 f9033e605ee2
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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:   This module contains the implementation of RIAUpdateClient
       
    15 *                class member functions.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 //INCLUDES
       
    22 #include <eikenv.h>
       
    23 #include <apgcli.h>
       
    24 #include <e32math.h>
       
    25 #include "iaupdateclient.h"
       
    26 #include "iaupdateclientdefines.h"
       
    27 #include "iaupdatetools.h"
       
    28 #include "iaupdatedebug.h"
       
    29 
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // RIAUpdateClient::RIAUpdateClient
       
    33 // 
       
    34 // -----------------------------------------------------------------------------
       
    35 // 
       
    36 RIAUpdateClient::RIAUpdateClient() 
       
    37 : iPtr1( NULL, 0 ),
       
    38   iPtr2( NULL, 0 ),
       
    39   iPtr3( NULL, 0 )
       
    40     {
       
    41     }
       
    42 
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // RIAUpdateClient::Open
       
    46 // 
       
    47 // -----------------------------------------------------------------------------
       
    48 // 
       
    49 TInt RIAUpdateClient::Open( TBool aToBackground )
       
    50     {
       
    51     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Open() begin");
       
    52     TInt error( KErrNone );
       
    53     
       
    54     if ( !iConnected )
       
    55         {
       
    56         if ( aToBackground )
       
    57             {
       
    58             TRAP( error, ConnectNewAppToBackgroundL( ServiceUid() ) );
       
    59             }
       
    60         else
       
    61             {
       
    62             TRAP( error, ConnectNewAppL( ServiceUid() ) );
       
    63             }
       
    64          if ( error == KErrNone ) 
       
    65             {
       
    66             iConnected = ETrue;
       
    67             CEikonEnv* eikEnv = CEikonEnv::Static();
       
    68             if ( eikEnv )
       
    69                 {
       
    70             	RWindowGroup owngroup;
       
    71 		        iOwnWgId = eikEnv->RootWin().Identifier(); 
       
    72 		        
       
    73 	            TPckg<TInt> wgId( iOwnWgId );
       
    74 	            delete iData;
       
    75 	            iData = NULL;
       
    76 	             TRAP_IGNORE( iData = wgId.AllocL() );
       
    77 	            
       
    78                 TIpcArgs args;
       
    79                 args.Set( 0, iData );
       
    80     
       
    81 	            SendReceive( IAUpdateClientDefines::EIAUpdateServerSendWgId, args );    
       
    82 		        } 
       
    83             }
       
    84         }
       
    85     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", error );
       
    86     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Open() begin");
       
    87     return error;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // RIAUpdateClient::Close
       
    92 // 
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void RIAUpdateClient::Close()
       
    96     {
       
    97     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Close() begin");
       
    98     // Let the parent handle closing.
       
    99     RAknAppServiceBase::Close();
       
   100     iConnected = EFalse;
       
   101     delete iData;
       
   102     iData = NULL;
       
   103     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::Close() end");
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // RIAUpdateClient::CheckUpdates
       
   108 // 
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 TInt RIAUpdateClient::CheckUpdates( const CIAUpdateParameters& aUpdateParameters,
       
   112                                     TInt& aAvailableUpdates, 
       
   113                                     TRequestStatus& aStatus )
       
   114     {
       
   115     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CheckUpdates()");
       
   116     // Inform the caller about the success of the request initializing.
       
   117     return SendCheckUpdatesRequest( IAUpdateClientDefines::EIAUpdateServerCheckUpdates,
       
   118                                     aUpdateParameters,
       
   119                                     aAvailableUpdates,
       
   120                                     aStatus );
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // RIAUpdateClient::ShowUpdates
       
   125 // 
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 TInt RIAUpdateClient::ShowUpdates( const CIAUpdateParameters& aUpdateParameters,
       
   129                                    TInt& aNumberOfSuccessfullUpdates,
       
   130                                    TInt& aNumberOfFailedUpdates,
       
   131                                    TInt& aNumberOfCancelledUpdates,
       
   132                                    TRequestStatus& aStatus )
       
   133     {
       
   134     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::ShowUpdates()");
       
   135     // Inform the caller about the success of the request initializing.
       
   136     return SendUpdateRequest( IAUpdateClientDefines::EIAUpdateServerShowUpdates,
       
   137                               aUpdateParameters,
       
   138                               aNumberOfSuccessfullUpdates,
       
   139                               aNumberOfFailedUpdates,
       
   140                               aNumberOfCancelledUpdates,
       
   141                               aStatus );
       
   142     }
       
   143     
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // RIAUpdateClient::UpdateQuery
       
   147 // 
       
   148 // -----------------------------------------------------------------------------
       
   149 //    
       
   150 TInt RIAUpdateClient::UpdateQuery( TBool& aUpdateNow, TRequestStatus& aStatus )
       
   151     {
       
   152     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::UpdateQuery() begin");
       
   153 	TPckg<TBool> updateNow( aUpdateNow );
       
   154 	iPtr1.Set( updateNow );
       
   155 	            
       
   156     TIpcArgs args;
       
   157     args.Set( 1, &iPtr1 );
       
   158     
       
   159 	SendReceive( IAUpdateClientDefines::EIAUpdateServerShowUpdateQuery, 
       
   160 	             args, 
       
   161 	             aStatus );    
       
   162 	IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::UpdateQuery() begin");    
       
   163     return KErrNone;
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // RIAUpdateClient::BroughtToForeground
       
   168 // 
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void RIAUpdateClient::BroughtToForeground()
       
   172     {
       
   173     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::BroughtToForeground() begin");
       
   174     if ( iConnected )
       
   175         {
       
   176     	SendReceive( IAUpdateClientDefines::EIAUpdateServerToForeground );
       
   177         }
       
   178     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::BroughtToForeground() end");
       
   179     }
       
   180 
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // RIAUpdateClient::CancelAsyncRequest
       
   184 // 
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void RIAUpdateClient::CancelAsyncRequest()
       
   188     {
       
   189     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CancelAsyncRequest() begin");
       
   190     // We suppose that an active object will
       
   191     // wait for the cancellation to complete.
       
   192     // So, let the server know that operation is cancelled.
       
   193     // The server should set the correct status and complete
       
   194     // the request of the active object. So, the cancellation 
       
   195     // can proceed to the end.
       
   196     if ( iConnected )
       
   197         {
       
   198     	SendReceive( IAUpdateClientDefines::EIAUpdateServerCancel ); 
       
   199         }
       
   200     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::CancelAsyncRequest() end");
       
   201     }
       
   202 
       
   203 
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // RIAUpdateClient::ServiceUid()
       
   207 // 
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 TUid RIAUpdateClient::ServiceUid() const
       
   211     {
       
   212     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::ServiceUid()");
       
   213     return IAUpdateClientDefines::KIAUpdateServiceUid;    
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // RIAUpdateClient::SendCheckUpdatesRequest
       
   218 // 
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 TInt RIAUpdateClient::SendCheckUpdatesRequest( TInt aUpdateFunction,
       
   222                                                const CIAUpdateParameters& aUpdateParameters,
       
   223                                                TInt& aCount,
       
   224                                                TRequestStatus& aStatus )
       
   225     {
       
   226     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendCheckUpdatesRequest() begin");
       
   227     
       
   228     aStatus = KRequestPending;
       
   229 
       
   230     delete iData;
       
   231     iData = NULL;   
       
   232     
       
   233     TInt error( KErrNone );
       
   234     TRAP( error, 
       
   235           IAUpdateTools::ExternalizeParametersL( iData, 
       
   236                                                  aUpdateParameters ) );
       
   237     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", error );
       
   238 
       
   239     // Because this function does not leave,
       
   240     // use error value to check if request can be done.
       
   241     if ( error == KErrNone )
       
   242         {
       
   243         TPckg<TInt> count( aCount );
       
   244 	    iPtr1.Set( count );
       
   245 	            
       
   246         TIpcArgs args;
       
   247         args.Set( 0, iData );
       
   248         args.Set( 1, &iPtr1 );
       
   249               
       
   250         // Start the asynchronous operation in the server side.
       
   251         SendReceive( aUpdateFunction, args, aStatus );        
       
   252         }
       
   253 
       
   254     // Inform the caller about the success of the request initializing.
       
   255     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendCheckUpdatesRequest() begin");
       
   256     return error;
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // RIAUpdateClient::SendUpdateRequest
       
   261 // 
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 TInt RIAUpdateClient::SendUpdateRequest( TInt aUpdateFunction,
       
   265                                          const CIAUpdateParameters& aUpdateParameters,
       
   266                                          TInt& aNumberOfSuccessfullUpdates,
       
   267                                          TInt& aNumberOfFailedUpdates,
       
   268                                          TInt& aNumberOfCancelledUpdates,
       
   269                                          TRequestStatus& aStatus )
       
   270     {
       
   271     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendUpdateRequest() begin");
       
   272     aStatus = KRequestPending;
       
   273 
       
   274     delete iData;
       
   275     iData = NULL;   
       
   276     
       
   277     TInt error( KErrNone );
       
   278     TRAP( error, 
       
   279           IAUpdateTools::ExternalizeParametersL( iData, 
       
   280                                                  aUpdateParameters ) );
       
   281     IAUPDATE_TRACE_1("[IAUPDATE] error code: %d", error );
       
   282     // Because this function does not leave,
       
   283     // use error value to check if request can be done.
       
   284     if ( error == KErrNone )
       
   285         {
       
   286         TPckg<TInt> successCount( aNumberOfSuccessfullUpdates );
       
   287 	    iPtr1.Set( successCount );
       
   288 
       
   289         TPckg<TInt> failCount( aNumberOfFailedUpdates );
       
   290 	    iPtr2.Set( failCount );
       
   291 
       
   292         TPckg<TInt> cancelCount( aNumberOfCancelledUpdates );
       
   293 	    iPtr3.Set( cancelCount );
       
   294 	    	            
       
   295         TIpcArgs args;
       
   296         args.Set( 0, iData );
       
   297         args.Set( 1, &iPtr1 );
       
   298         args.Set( 2, &iPtr2 );
       
   299         args.Set( 3, &iPtr3 );
       
   300       
       
   301         // Start the asynchronous operation in the server side.
       
   302         SendReceive( aUpdateFunction, args, aStatus );        
       
   303         }
       
   304 
       
   305     // Inform the caller about the success of the request initializing.
       
   306     IAUPDATE_TRACE("[IAUPDATE] RIAUpdateClient::SendUpdateRequest() end");
       
   307     return error;
       
   308     }    
       
   309 
       
   310 void RIAUpdateClient::ConnectNewAppToBackgroundL( TUid aAppUid )
       
   311     {
       
   312     TName notUsed;
       
   313     const TUint differentiator = GenerateServerDifferentiatorAndName(notUsed, aAppUid);
       
   314     TRequestStatus requestStatusForRendezvous;
       
   315           
       
   316     RApaLsSession apa;
       
   317     User::LeaveIfError(apa.Connect());
       
   318     CleanupClosePushL(apa);
       
   319             
       
   320     TApaAppInfo info;
       
   321     User::LeaveIfError(apa.GetAppInfo(info, aAppUid));
       
   322 
       
   323     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   324     cmdLine->SetExecutableNameL(info.iFullName);
       
   325     cmdLine->SetServerRequiredL( differentiator );
       
   326     cmdLine->SetCommandL(EApaCommandBackground);
       
   327             
       
   328     TThreadId notUsedId;
       
   329     User::LeaveIfError(apa.StartApp(*cmdLine, notUsedId, &requestStatusForRendezvous));
       
   330 
       
   331     CleanupStack::PopAndDestroy(2, &apa);   // cmdLine and apa
       
   332       
       
   333     User::WaitForRequest(requestStatusForRendezvous);
       
   334     User::LeaveIfError(requestStatusForRendezvous.Int());
       
   335     
       
   336     TName serverName;
       
   337     ServerName(serverName, aAppUid, differentiator);
       
   338     ConnectExistingByNameL(serverName);
       
   339     }
       
   340 
       
   341 
       
   342 
       
   343 
       
   344 void RIAUpdateClient::ServerName(TName& aServerName, TUid aAppServerUid, TUint aServerDifferentiator)
       
   345     {
       
   346     _LIT(KServerNameFormat, "%08x_%08x_AppServer");
       
   347     aServerName.Format(KServerNameFormat, aServerDifferentiator, aAppServerUid);
       
   348     }
       
   349     
       
   350 TUint RIAUpdateClient::GenerateServerDifferentiatorAndName(TName& aServerName, TUid aAppServerUid)
       
   351     {
       
   352     TUint r;
       
   353     FOREVER
       
   354         {
       
   355         r = Math::Random();
       
   356         if (r==0)
       
   357             continue;
       
   358         ServerName(aServerName, aAppServerUid, r);
       
   359         TFindServer find(aServerName);
       
   360         TFullName fullName;
       
   361         if (find.Next(fullName) == KErrNone)
       
   362             continue;
       
   363         break;
       
   364         }       
       
   365     return r;
       
   366     }