localisation/apparchitecture/apparc/ApaAppClient.cpp
branchSymbian2
changeset 1 8758140453c0
child 6 c108117318cb
equal deleted inserted replaced
0:e8c1ea2c6496 1:8758140453c0
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ApaServerAppPriv.h"
       
    17 
       
    18 void AppServerPanic(TApaAppServerPanic aReason)
       
    19 	{
       
    20 	_LIT(KPanic, "ApaAppClient");
       
    21 	User::Panic(KPanic, aReason);
       
    22 	}
       
    23 	
       
    24 void CleanupServerName(TAny* aParam)
       
    25 	{
       
    26 	HBufC** pServerName = static_cast<HBufC**>(aParam);
       
    27 	delete *pServerName;
       
    28 	*pServerName = NULL;
       
    29 	}
       
    30 
       
    31 /** Protected constructor, instantiable services must derive from this class.*/
       
    32 EXPORT_C RApaAppServiceBase::RApaAppServiceBase()
       
    33 : iServerName(NULL)
       
    34 	{
       
    35 	}
       
    36 
       
    37 /** Closes the service and the connection to the server app.*/
       
    38 EXPORT_C void RApaAppServiceBase::Close()
       
    39 	{
       
    40 	delete iServerName;
       
    41 	iServerName = NULL;
       
    42 	RSessionBase::Close();
       
    43 	}
       
    44 	
       
    45 /** Connects to an already running server application, through an already connected service. 
       
    46 @param aClient A service which is already connected.*/
       
    47 EXPORT_C void RApaAppServiceBase::ConnectExistingAppL(const RApaAppServiceBase& aClient)
       
    48 	{
       
    49 	__ASSERT_DEBUG(!iServerName, AppServerPanic(EApaAppServerPanicClientAlreadyConnected));
       
    50 	iServerName = aClient.iServerName->AllocL();
       
    51 	ConnectL();
       
    52 	}
       
    53 
       
    54 /** Connects to an already running server application, through an already connected service. 
       
    55 @param aClient A service which is already connected.
       
    56 @param aSecurityPolicy The TSecurityPolicy which should be matched with the TSecurityPolicy of the app to be connected.
       
    57 
       
    58 @see TSecurityPolicy*/
       
    59 EXPORT_C void RApaAppServiceBase::ConnectExistingAppL(const RApaAppServiceBase& aClient, const TSecurityPolicy& aSecurityPolicy)
       
    60 	{
       
    61 	__ASSERT_DEBUG(!iServerName, AppServerPanic(EApaAppServerPanicClientAlreadyConnected));
       
    62 	iServerName = aClient.iServerName->AllocL();
       
    63 	ConnectL(aSecurityPolicy);
       
    64 	}
       
    65 
       
    66 /** Connects to an already running server application, by name.
       
    67 @param aName The name of the server hosted by the server app.*/
       
    68 EXPORT_C void RApaAppServiceBase::ConnectExistingByNameL(const TDesC& aServerName)
       
    69 	{
       
    70 	__ASSERT_DEBUG(!iServerName, AppServerPanic(EApaAppServerPanicClientAlreadyConnected));
       
    71 	iServerName = aServerName.AllocL();
       
    72 	ConnectL();
       
    73 	}
       
    74 
       
    75 /** Connects to an already running server application, by name.
       
    76 @param aName The name of the server hosted by the server app.
       
    77 @param aSecurityPolicy The TSecurityPolicy which should be matched with the TSecurityPolicy of the app to be connected.
       
    78 
       
    79 @see TSecurityPolicy*/
       
    80 EXPORT_C void RApaAppServiceBase::ConnectExistingByNameL(const TDesC& aServerName, const TSecurityPolicy& aSecurityPolicy)
       
    81 	{
       
    82 	__ASSERT_DEBUG(!iServerName, AppServerPanic(EApaAppServerPanicClientAlreadyConnected));
       
    83 	iServerName = aServerName.AllocL();
       
    84 	ConnectL(aSecurityPolicy);
       
    85 	}
       
    86 
       
    87 void RApaAppServiceBase::ConnectL()
       
    88 	{
       
    89 	CleanupStack::PushL(TCleanupItem(CleanupServerName, &iServerName));
       
    90 	TUid serviceUid = ServiceUid();
       
    91 	User::LeaveIfError(CreateSession(*iServerName,*reinterpret_cast<TVersion*>(&serviceUid)));
       
    92 	CleanupStack::Pop(&iServerName);
       
    93 	}
       
    94 
       
    95 void RApaAppServiceBase::ConnectL(const TSecurityPolicy& aSecurityPolicy)
       
    96 	{
       
    97 	CleanupStack::PushL(TCleanupItem(CleanupServerName, &iServerName));
       
    98 	TUid serviceUid = ServiceUid();
       
    99 	User::LeaveIfError(CreateSession(*iServerName, *reinterpret_cast<TVersion*>(&serviceUid), -1, EIpcSession_Unsharable, &aSecurityPolicy));
       
   100 	CleanupStack::Pop(&iServerName);
       
   101 	}
       
   102 
       
   103 /** Gets the name of the server hosted by the server application.
       
   104 @return the name of the server application, will be empty if
       
   105 the service is not connected*/
       
   106 EXPORT_C TPtrC RApaAppServiceBase::ServerName() const
       
   107 	{
       
   108 	if (iServerName)
       
   109 		{
       
   110 		return *iServerName;
       
   111 		}
       
   112 	else
       
   113 		{
       
   114 		return KNullDesC();
       
   115 		}
       
   116 	}
       
   117 
       
   118 /** Requests notification of server application exit.
       
   119 @param aStatus On completion, this contains the exit code of the server application.
       
   120 @see CApaServerAppExitMonitor*/
       
   121 EXPORT_C void RApaAppServiceBase::NotifyServerExit(TRequestStatus& aStatus) const
       
   122 	{
       
   123 	SendReceive(EApaAppServerNotifyServerExit,TIpcArgs(),aStatus);
       
   124 	}
       
   125 	
       
   126 /** Cancels the request for notification of server application exit.*/
       
   127 EXPORT_C void RApaAppServiceBase::CancelNotifyServerExit() const
       
   128 	{
       
   129 	SendReceive(EApaAppServerCancelNotifyServerExit);	
       
   130 	}
       
   131 
       
   132 /** Extension mechanism - for internal BC proofing. */
       
   133 EXPORT_C void RApaAppServiceBase::ExtensionInterface(TUid /*aInterfaceId*/, TAny*& /*aImplementaion*/)
       
   134 	{
       
   135 	}
       
   136 EXPORT_C void RApaAppServiceBase::RApaAppServiceBase_Reserved1()
       
   137 	{
       
   138 	}
       
   139 
       
   140 EXPORT_C void RApaAppServiceBase::RApaAppServiceBase_Reserved2()
       
   141 	{
       
   142 	}
       
   143 
       
   144 /** Transfers the ownership of the session passed to self.
       
   145 @param aClient A service which is already connected. On return, this service will be reset.
       
   146 @leave KErrArgument The session passed is not connected.
       
   147 @panic ApaAppClient 3 The client getting the ownership already has a connected session. Debug builds only.
       
   148 */	
       
   149 EXPORT_C void RApaAppServiceBase::TransferExistingSessionL(RApaAppServiceBase& aClient)
       
   150 	{
       
   151 	__ASSERT_DEBUG(!iServerName, AppServerPanic(EApaAppServerPanicClientAlreadyConnected));
       
   152 	if(aClient.iServerName == NULL)
       
   153 		{
       
   154 		User::Leave(KErrArgument);
       
   155 		}
       
   156 	if(this != &aClient)
       
   157 		{
       
   158 		iServerName = aClient.iServerName;
       
   159 		iApaReserved1 = aClient.iApaReserved1;
       
   160 		iApaReserved2 = aClient.iApaReserved2;
       
   161 		SetHandle(aClient.Handle());
       
   162 		aClient.iServerName = NULL;
       
   163 		aClient.SetHandle(0);
       
   164 		}
       
   165 	}
       
   166 	
       
   167 //
       
   168 // CApaServerAppExitMonitor
       
   169 //
       
   170 
       
   171 /** Creates a new monitor object and starts monitoring server app lifetime.
       
   172 @param aClient A connected server app session, which requires monitoring.
       
   173 @param aObserver An implementer of the MApaServerAppExitObserver that wants to be notified of server app exits.
       
   174 @param aPriority The active object priority that this monitor should run at.
       
   175 @return a new instance of a monitor. */
       
   176 EXPORT_C CApaServerAppExitMonitor* CApaServerAppExitMonitor::NewL(RApaAppServiceBase& aClient, MApaServerAppExitObserver& aObserver, TInt aPriority)
       
   177 	{
       
   178 	CApaServerAppExitMonitor* self = new(ELeave) CApaServerAppExitMonitor(aClient, aObserver, aPriority);
       
   179 	return self;
       
   180 	}
       
   181 	
       
   182 /** Creates a new monitor object, places it on the cleanup stack and starts monitoring server app lifetime.
       
   183 @param aClient A connected server app session, which requires monitoring.
       
   184 @param aObserver An implementer of the MApaServerAppExitObserver that wants to be notified of server app exits.
       
   185 @param aPriority The active object priority that this monitor should run at.
       
   186 @return a new instance of a monitor, which is placed on the cleanup stack. */
       
   187 EXPORT_C CApaServerAppExitMonitor* CApaServerAppExitMonitor::NewLC(RApaAppServiceBase& aClient, MApaServerAppExitObserver& aObserver, TInt aPriority)
       
   188 	{
       
   189 	CApaServerAppExitMonitor* self = new(ELeave) CApaServerAppExitMonitor(aClient, aObserver, aPriority);
       
   190 	CleanupStack::PushL(self);
       
   191 	return self;
       
   192 	}
       
   193 	
       
   194 /** The destructor stops monitoring of server app exit. */
       
   195 EXPORT_C CApaServerAppExitMonitor::~CApaServerAppExitMonitor()
       
   196 	{
       
   197 	Cancel();
       
   198 	}
       
   199 
       
   200 CApaServerAppExitMonitor::CApaServerAppExitMonitor(RApaAppServiceBase& aClient, MApaServerAppExitObserver& aObserver, TInt aPriority)
       
   201 : CActive(aPriority), iClient(aClient), iObserver(aObserver)
       
   202 	{
       
   203 	CActiveScheduler::Add(this);
       
   204 	iClient.NotifyServerExit(iStatus);
       
   205 	SetActive();
       
   206 	}
       
   207 
       
   208 void CApaServerAppExitMonitor::RunL()
       
   209 	{
       
   210 	TInt reason = iStatus.Int();
       
   211 	iObserver.HandleServerAppExit(reason);
       
   212 	}
       
   213 
       
   214 void CApaServerAppExitMonitor::DoCancel()
       
   215 	{
       
   216 	iClient.CancelNotifyServerExit();
       
   217 	}
       
   218 
       
   219 TInt CApaServerAppExitMonitor::RunError(TInt aError)
       
   220 	{
       
   221 	return aError;
       
   222 	}