os/mm/mmplugins/cameraplugins/source/mmcameraclientplugin/mmcameraserver/src/mmcameraserverpolicymanager.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-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 // mmcamerapolicymanager.cpp
    15 // 
    16 //
    17 
    18 /**
    19  @file
    20  @internalComponent
    21 */
    22 
    23 #include "mmcameraserversession.h"
    24 #include "mmcameraserverpolicymanager.h"
    25 #include "mmcameraservercontroller.h"
    26 
    27 
    28 CMMCameraServerPolicyManager* CMMCameraServerPolicyManager::NewL()
    29 	{
    30 	CMMCameraServerPolicyManager* self = new (ELeave) CMMCameraServerPolicyManager();
    31 	CleanupStack::PushL(self);
    32 	self->ConstructL();
    33 	CleanupStack::Pop(self);
    34 	return self;	
    35 	}
    36 	
    37 CMMCameraServerPolicyManager::CMMCameraServerPolicyManager() : iReservedSessionQ(_FOFF(CMMCameraServerSession,iCamSessionLink)),
    38 												iIter(iReservedSessionQ)
    39 	{
    40 	}
    41 
    42 void CMMCameraServerPolicyManager::ConstructL()
    43 	{
    44 	}
    45 
    46 CMMCameraServerPolicyManager::~CMMCameraServerPolicyManager()
    47 	{	
    48 	}
    49 
    50 /**
    51  * Enforces client connection policies.
    52  * 
    53  * Called by the server as the client connects
    54  */
    55 void CMMCameraServerPolicyManager::OnConnectL(const RMessage2& aMessage)
    56 	{
    57 	// All users must have UserEnvironment capability
    58 	if (!KMMCameraServerPolicyUserEnvironment.CheckPolicy(aMessage, __PLATSEC_DIAGNOSTIC_STRING("CMMCameraServerPolicyManager::OnConnectL KMMCameraServerPolicyUserEnvironment")))
    59 		{
    60 		User::Leave(KErrPermissionDenied);
    61 		}
    62 	}
    63 
    64 /**
    65  * Performs further MultimediaDD policy check on clients connecting to the server.
    66  * 
    67  * Called at the start of the Sessions ServiceL routine
    68  */
    69 void CMMCameraServerPolicyManager::ServiceHandlerL(const RMessage2& aMessage)
    70 	{
    71 	// If client is connecting to the server, make sure correct capabilities are set
    72 	if (aMessage.Function() == ECamOpenCamera)
    73 		{
    74 		// Insert MMCapability into message buffer
    75 		TOpenCamera parameters;
    76 		TOpenCameraPckg openCamBuf(parameters);
    77 		aMessage.ReadL(TInt(0), openCamBuf);
    78 
    79 		parameters = openCamBuf();
    80 
    81 		if (KMMCameraServerPolicyMultimediaDD.CheckPolicy(aMessage, __PLATSEC_DIAGNOSTIC_STRING ("CMMCameraServerPolicyManager::ServiceHandlerL KMMCameraServerPolicyMultimediaDD")))
    82 			{
    83 			parameters.iMMCapability = ETrue;
    84 			}
    85 		else
    86 			{
    87 			parameters.iMMCapability = EFalse;
    88 			}
    89 
    90 	    TOpenCameraPckg buf(parameters);
    91 
    92 	    aMessage.WriteL(TInt(0), buf);	
    93 		}
    94 
    95 	// Add code here if necessary to check
    96 	// clients ability to access particular
    97 	// functions
    98 	}
    99 
   100 /**
   101  * Used by ReserveClient() to find first reserved client session.
   102  */
   103 CMMCameraServerSession* CMMCameraServerPolicyManager::FindFirstInQue(TInt aIndex)
   104 	{
   105 	CMMCameraServerSession* pS = NULL;
   106 	iIter.SetToFirst();
   107 
   108 	while ((pS = iIter++) != NULL)
   109 		{
   110 		if (pS->CameraIndex() == aIndex)
   111 			{
   112 			break;
   113 			}
   114 		}
   115 
   116 	return pS;
   117 	}
   118 
   119 /**
   120  * Used by CheckControlOvertaking() to find last client in queue that has reserved the camera.
   121  */
   122 CMMCameraServerSession* CMMCameraServerPolicyManager::FindLastInQue(TInt aIndex)
   123 	{
   124 	CMMCameraServerSession *pS, *ret = NULL;
   125 	iIter.SetToFirst();
   126 
   127 	while ((pS = iIter++) != NULL)
   128 		{
   129 		if (pS->CameraIndex() == aIndex)
   130 			{
   131 			ret = pS;
   132 			}
   133 		}
   134 
   135 	return ret;
   136 	}
   137 
   138 /**
   139  * Attempts to reserve the specific camera which the client requested.
   140  */
   141 TBool CMMCameraServerPolicyManager::ReserveClient(CMMCameraServerSession* aSession)
   142 	{
   143 	TInt camIndex = aSession->CameraIndex();
   144 
   145 	// Check if this is the first client reserving the camera device
   146 	if (FindFirstInQue(camIndex) == NULL)
   147 		{
   148 		ReserveUpdate(aSession);
   149 		return ETrue;
   150 		}
   151 	else 
   152 		{
   153 		// If not first client, check if it is a collaborative client with the one which has already reseved the camera
   154 		if(aSession->CollaborativeClient())
   155 			{
   156 			// Get the First session in the queue
   157 			// All collaborative clients require the same MMCapability
   158 			CMMCameraServerSession* firstSession = FindFirstInQue(camIndex);
   159 
   160 			if (aSession->MMCapability() == firstSession->MMCapability())
   161 				{
   162 				// Set the priority of the collaborative client to that of the first client
   163 				aSession->SetPriority(firstSession->Priority());
   164 				iReservedSessionQ.AddLast(*aSession);
   165 				return ETrue;
   166 				}
   167 			}
   168 
   169 		// See if new client has higher priority than current client
   170 		if(CheckControlOvertaking(aSession)) 
   171 			{
   172 			CMMCameraServerSession* pS = NULL;
   173 			iIter.SetToFirst();
   174 
   175 			aSession->CameraController()->Reset();
   176 
   177 			while ((pS = iIter++) != NULL)
   178 				{
   179 				// We Deque and overthrow those clients that 
   180 				// belong to the same camIndex as this client
   181 				if (pS->CameraIndex() == camIndex)
   182 					{
   183 					// set reserve status of previously reserved client to EFalse. 
   184 					pS->SetReserved(EFalse);
   185 					pS->CompleteOverthrow();
   186 
   187 					//removes from controller's 'reserved session' queue
   188 					pS->iCamSessionLink.Deque();
   189 					pS->iCamSessionLink.iNext = NULL;
   190 					}
   191 				}
   192 
   193 			// update details wrt new reserved client. 
   194 			ReserveUpdate(aSession);
   195 			return ETrue;
   196 			}
   197 		}
   198 
   199 	return EFalse;
   200 	}
   201 
   202 /**
   203  * Updates reserve status of clients. Used by ReserveClient().
   204  */
   205 void CMMCameraServerPolicyManager::ReserveUpdate(CMMCameraServerSession* aSession)
   206 	{
   207 	aSession->SetHandle(aSession->CameraController()->CameraHandle());
   208 
   209 	iReservedSessionQ.AddLast(*aSession);
   210 	}
   211 
   212 /**
   213  * Determines whether overthrowing client has higher priority than current client.
   214  * Used by ReserveClient().
   215  */
   216 TBool CMMCameraServerPolicyManager::CheckControlOvertaking(CMMCameraServerSession* aSession)
   217 	{
   218 	TInt camIndex = aSession->CameraIndex();
   219 	CMMCameraServerSession* lastSession = FindLastInQue(camIndex);
   220 
   221 	if(aSession->MMCapability() && !(lastSession->MMCapability()))  
   222 		{
   223 		return ETrue;
   224 		}
   225 	
   226 	if(aSession->MMCapability() == lastSession->MMCapability())
   227 		{
   228 		if(aSession->Priority() > lastSession->Priority())
   229 			{
   230 			return ETrue;
   231 			}
   232 		}
   233 
   234 	return EFalse;
   235 	}
   236 
   237 /**
   238  * Releases the camera device.
   239  */
   240 void CMMCameraServerPolicyManager::ReleaseClient(CMMCameraServerSession* aSession)
   241 	{
   242 	if(!iReservedSessionQ.IsEmpty())
   243 		{
   244 		aSession->iCamSessionLink.Deque();
   245 		aSession->iCamSessionLink.iNext = NULL;
   246 		}
   247 	}