securitydialogs/lockclient/src/lockaccessextension.cpp
branchRCL_3
changeset 21 09b1ac925e3f
parent 0 164170e6151a
child 22 03674e5abf46
equal deleted inserted replaced
20:63339781d179 21:09b1ac925e3f
    13 *
    13 *
    14 * Description:  Extension to lockapp clients.
    14 * Description:  Extension to lockapp clients.
    15  *
    15  *
    16 */
    16 */
    17 
    17 
    18 
       
    19 #include "lockaccessextension.h"
    18 #include "lockaccessextension.h"
    20 #include <lockappclientserver.h>
    19 #include <lockappclientserver.h>
    21 #include <e32property.h> // P&S API
    20 #include <e32property.h> // P&S API
    22 #include <apgtask.h> // TApaTask, TApaTaskList
    21 #include <apgtask.h> // TApaTask, TApaTaskList
    23 #include <coemain.h> // CCoeEnv
    22 #include <coemain.h> // CCoeEnv
    24 
    23 #include "../../Autolock/PubSub/securityuisprivatepskeys.h"
    25 
    24 #include <apgcli.h>
    26 // Constants
    25 #include <apacmdln.h>
    27 const TInt KTimesToConnectServer( 2);
    26 
    28 const TInt KTimeoutBeforeRetrying( 50000);
    27 #include <xqservicerequest.h>
       
    28 #include <xqserviceutil.h>
       
    29 #include <xqrequestinfo.h>
       
    30 #include <xqaiwrequest.h>
       
    31 #include <xqappmgr.h>
    29 
    32 
    30 // ---------------------------------------------------------------------------
    33 // ---------------------------------------------------------------------------
    31 // Gets server version, needed for connection
    34 // Gets server version, needed for connection
    32 // ---------------------------------------------------------------------------
    35 // ---------------------------------------------------------------------------
    33 TVersion RLockAccessExtension::GetVersion( )
    36 TVersion RLockAccessExtension::GetVersion( )
    39 // Connects client to lockapp application server
    42 // Connects client to lockapp application server
    40 // ---------------------------------------------------------------------------
    43 // ---------------------------------------------------------------------------
    41 TInt RLockAccessExtension::TryConnect( RWsSession& aWsSession )
    44 TInt RLockAccessExtension::TryConnect( RWsSession& aWsSession )
    42 	{
    45 	{
    43 	TInt ret(KErrNone);
    46 	TInt ret(KErrNone);
       
    47 	/*
       
    48 	this is the old methd. Now we use QtHighway
    44 	TApaTaskList list(aWsSession);
    49 	TApaTaskList list(aWsSession);
    45 	// check that lockapp is running
    50 	// check that lockapp is running
    46 	TApaTask task = list.FindApp( KLockAppUid );
    51 	TApaTask task = list.FindApp( KLockAppUid );
    47 	if ( task.Exists( ) )
    52 	if ( task.Exists( ) )
    48 		{
    53 		{
    60 		{
    65 		{
    61 		// LockApp task not found
    66 		// LockApp task not found
    62 		RDebug::Printf( "%s %s (%u) ???? LockApp task not found=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, KLockAppUid );
    67 		RDebug::Printf( "%s %s (%u) ???? LockApp task not found=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, KLockAppUid );
    63 		ret = KErrNotReady;
    68 		ret = KErrNotReady;
    64 		}
    69 		}
       
    70 	*/
       
    71 	RDEBUG("ret", ret);
    65 	return ret;
    72 	return ret;
    66 	}
    73 	}
    67 
    74 
    68 // ---------------------------------------------------------------------------
    75 // ---------------------------------------------------------------------------
    69 // Ensures that the connection to the service is alive.
    76 // Ensures that the connection to the service is alive.
    70 // ---------------------------------------------------------------------------
    77 // ---------------------------------------------------------------------------
    71 TInt RLockAccessExtension::EnsureConnected( )
    78 TInt RLockAccessExtension::EnsureConnected( )
    72 	{
    79 	{
    73 	TInt ret(KErrNone);
    80 	TInt ret(KErrNone);
       
    81 
       
    82 	// Now we use QtHighway, but nevertheless need to be sure that only 1 process is running
       
    83 	// This is done because Autolock.exe should start at the beginning, but it might not be ready yet.
       
    84 	// As Qthighway will start it, it's better to give time for the first one to prepare itself.
       
    85 	TInt err = KErrNone;
       
    86 	TInt numAttempts = 0;
       
    87 	TInt numberOfInstances = 0;
       
    88 	do
       
    89 		{
       
    90 		numberOfInstances=0;
       
    91 		TFullName processName;
       
    92 		TFindThread find(_L("*utolock*"));	// first letter can can be uppercase or lowercase
       
    93 		while( find.Next( processName ) == KErrNone )
       
    94 		    {
       
    95 				// Autolock[100059b5]0002::Autolock		in device
       
    96 				// autolock.exe[100059b5]0002::Main		in emulator
       
    97 				RDEBUG("found process", 1);
       
    98         numberOfInstances++;
       
    99 		    }	// end while
       
   100 		RDEBUG("numberOfInstances", numberOfInstances);
       
   101 		if(numberOfInstances<=0)
       
   102 			{
       
   103 			RDEBUG("Autolock.exe not running already. Starting.", 0 );
       
   104 	    RApaLsSession ls;
       
   105 	    User::LeaveIfError(ls.Connect());
       
   106 	    CleanupClosePushL(ls);
       
   107 	    RDEBUG("commandLine", 0); 
       
   108 	    CApaCommandLine* commandLine = CApaCommandLine::NewLC();
       
   109 	    commandLine->SetExecutableNameL(_L("autolock.exe"));
       
   110 	    commandLine->SetCommandL(EApaCommandRun);
       
   111 	    // Try to launch the application.
       
   112 	    RDEBUG("StartApp", 0); 
       
   113 	    TInt err = ls.StartApp(*commandLine); // this migh fail
       
   114     	CleanupStack::PopAndDestroy(2); // commandLine, ls
       
   115 
       
   116 			RDEBUG("Autolock.exe launched. Waiting a bit. err", err );
       
   117 			User::After(1000*1000);
       
   118 			RDEBUG("re-verifying Autolock.exe process.", 1 );
       
   119 			}
       
   120 		} while (numAttempts++ <3 && numberOfInstances<=0);
       
   121 		
       
   122 
       
   123 
       
   124 	TInt value = -1;
       
   125 	err = KErrNone;
       
   126 	numAttempts = 0;
       
   127 	while( value<1 && numAttempts++ <10 )	// wait max 5 seconds
       
   128 		{
       
   129 		// process was started, but still not fully running. Give a bit more time
       
   130 		err = RProperty::Get(KPSUidSecurityUIs, KSecurityUIsLockInitiatorUID, value);
       
   131 		RDEBUG("err", err);
       
   132 		RDEBUG("value", value);
       
   133 		if(value<1)
       
   134 			{
       
   135 			RDEBUG("Autolock.exe has started but it's not fully running", value);
       
   136 			User::After(5*100*1000);	// half a second
       
   137 			}
       
   138 		}
       
   139 	RDEBUG("numAttempts", numAttempts);
       
   140 	/*
       
   141 	this is the old method. 
    74 	// we need CCoeEnv because of window group list
   142 	// we need CCoeEnv because of window group list
       
   143 	const TInt KTimesToConnectServer( 2);
       
   144 	const TInt KTimeoutBeforeRetrying( 50000);
    75 	CCoeEnv* coeEnv = CCoeEnv::Static( );
   145 	CCoeEnv* coeEnv = CCoeEnv::Static( );
    76 	if ( coeEnv )
   146 	if ( coeEnv )
    77 		{
   147 		{
    78 		// All server connections are tried to be made KTriesToConnectServer times because
   148 		// All server connections are tried to be made KTriesToConnectServer times because
    79 		// occasional fails on connections are possible at least on some servers
   149 		// occasional fails on connections are possible at least on some servers
    88 	else
   158 	else
    89 		{
   159 		{
    90 		// No CCoeEnv
   160 		// No CCoeEnv
    91 		ret = KErrNotSupported;
   161 		ret = KErrNotSupported;
    92 		}
   162 		}
       
   163 	*/
       
   164 	RDEBUG("ret", ret);
    93 	return ret;
   165 	return ret;
    94 	}
   166 	}
    95 
   167 
    96 // ---------------------------------------------------------------------------
   168 // ---------------------------------------------------------------------------
    97 // Sends blind message to lockapp if the session is connected.
   169 // Sends blind message to lockapp if the session is connected.
    98 // ---------------------------------------------------------------------------
   170 // ---------------------------------------------------------------------------
    99 TInt RLockAccessExtension::SendMessage( TInt aMessage )
   171 TInt RLockAccessExtension::SendMessage( TInt aMessage )
   100 	{
   172 	{
   101 	TInt ret = EnsureConnected( );
   173 	RDEBUG("0", 0);
       
   174 	TInt ret = KErrNone;
   102 	if ( ret == KErrNone )
   175 	if ( ret == KErrNone )
   103 		{
   176 		{
   104 		ret = SendReceive( aMessage );
   177 		// ret = SendReceive( aMessage );
       
   178 		ret = SendMessage( aMessage, -1, -1 );
   105 		}
   179 		}
   106 	return ret;
   180 	return ret;
   107 	}
   181 	}
   108 
   182 
   109 // ---------------------------------------------------------------------------
   183 // ---------------------------------------------------------------------------
   110 // Sends blind message to lockapp if the session is connected.
   184 // Sends blind message to lockapp if the session is connected.
   111 // ---------------------------------------------------------------------------
   185 // ---------------------------------------------------------------------------
   112 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1 )
   186 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1 )
   113 	{
   187 	{
   114 	TInt ret = EnsureConnected( );
   188 	RDEBUG("0", 0);
       
   189 	TInt ret = KErrNone;
   115 	if ( ret == KErrNone )
   190 	if ( ret == KErrNone )
   116 		{
   191 		{
   117 		// assign parameters to IPC argument
   192 		// assign parameters to IPC argument
   118 		TIpcArgs args(aParam1);
   193 		TIpcArgs args(aParam1);
   119 		ret = SendReceive( aMessage, args );
   194 		// ret = SendReceive( aMessage, args );
       
   195 		ret = SendMessage( aMessage, aParam1, -1 );
   120 		}
   196 		}
   121 	return ret;
   197 	return ret;
   122 	}
   198 	}
   123 
   199 
   124 // ---------------------------------------------------------------------------
   200 // ---------------------------------------------------------------------------
   125 // Sends blind message to lockapp if the session is connected.
   201 // Sends blind message to lockapp if the session is connected.
   126 // ---------------------------------------------------------------------------
   202 // ---------------------------------------------------------------------------
   127 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1, TInt aParam2 )
   203 TInt RLockAccessExtension::SendMessage( TInt aMessage, TInt aParam1, TInt aParam2 )
   128 	{
   204 	{
       
   205 	RDEBUG("0", 0);
   129 	TInt ret = EnsureConnected( );
   206 	TInt ret = EnsureConnected( );
       
   207 	RDEBUG("ret", ret);
   130 	if ( ret == KErrNone )
   208 	if ( ret == KErrNone )
   131 		{
   209 		{
   132 		// assign parameters to IPC argument
   210 		// assign parameters to IPC argument
   133 		TIpcArgs args( aParam1, aParam2);
   211 		// TIpcArgs args( aParam1, aParam2);
   134 		ret = SendReceive( aMessage, args );
   212 		// this is the old methd. Now we use QtHighway
   135 		}
   213 		// ret = SendReceive( aMessage, args );
       
   214     RDEBUG("aMessage", aMessage);
       
   215     RDEBUG("aParam1", aParam1);
       
   216     RDEBUG("aParam2", aParam2);
       
   217 
       
   218 		
       
   219 			{	// old method. Not used any more. Kept as reference
       
   220 			/*
       
   221 	    XQServiceRequest* mServiceRequest;
       
   222       RDEBUG("XQServiceRequest", 0);
       
   223 	    mServiceRequest = new XQServiceRequest("com.nokia.services.Autolock.Autolock","service(QString,QString,QString)");// use   , false    to make async
       
   224       RDEBUG("aMessage", 0);
       
   225 	    QString label;
       
   226 	    label = "" + QString("%1").arg(aMessage);
       
   227 	    *mServiceRequest << QString(label);
       
   228       RDEBUG("aParam1", 0);
       
   229 	    label = "" + QString("%1").arg(aParam1);
       
   230 	    *mServiceRequest << QString(label);
       
   231       RDEBUG("aParam2", 0);
       
   232 	    label = "" + QString("%1").arg(aParam2);
       
   233 	    *mServiceRequest << QString(label);
       
   234 	    int returnvalue;
       
   235       RDEBUG("send", 0);
       
   236 	    bool ret = mServiceRequest->send(returnvalue);
       
   237       RDEBUG("ret", ret);
       
   238       RDEBUG("returnvalue", returnvalue);
       
   239       */
       
   240 			}
       
   241 
       
   242 			RDEBUG("args", 0);
       
   243 			QList<QVariant> args;
       
   244 	    args << QVariant(QString(QString::number(aMessage)));
       
   245 	    args << QVariant(QString(QString::number(aParam1)));
       
   246 	    args << QVariant(QString(QString::number(aParam2)));
       
   247 
       
   248 	    XQApplicationManager mAppManager;
       
   249 	    XQAiwRequest *request;
       
   250 	    RDEBUG("create", 0);
       
   251 			request = mAppManager.create("com.nokia.services.Autolock", "Autolock", "service(QString,QString,QString)", false);
       
   252 			// also works with		create("Autolock", "service(QString,QString,QString)", false);
       
   253 			if(request)
       
   254 				{
       
   255 	    	RDEBUG("got request", 0);
       
   256 	    	}
       
   257 	    else
       
   258 	    	{
       
   259 	 	    RDEBUG("not got request", 0);
       
   260 	 	    RDebug::Printf( "%s %s (%u) not got request=%x", __FILE__, __PRETTY_FUNCTION__, __LINE__, 0 );
       
   261 	 	    return KErrAbort;
       
   262 	 	  	}
       
   263 				
       
   264 			RDEBUG("setArguments", 0);
       
   265 			request->setArguments(args);
       
   266 			RDEBUG("args", 0);
       
   267 	    int returnvalue=0;
       
   268 	    QVariant var = QVariant(returnvalue);
       
   269 			RDEBUG("send", 0);
       
   270 			bool retSend = request->send(var);
       
   271 			returnvalue = var.toInt();
       
   272 			RDEBUG("retSend", retSend);
       
   273 			RDEBUG("returnvalue", returnvalue);
       
   274 	    int error = request->lastError();
       
   275 			RDEBUG("error", error);
       
   276 	    ret = returnvalue;
       
   277 	
       
   278 	    delete request;
       
   279 		}
       
   280   RDEBUG("ret", ret);
   136 	return ret;
   281 	return ret;
   137 	}
   282 	}
   138 
   283 
   139 // End of File
   284 // End of File