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