os/security/contentmgmt/contentaccessfwfordrm/source/caf/agentinfo.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2003-2009 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 the License "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: 
    15 *
    16 */
    17 
    18 
    19 #include <ecom/ecom.h>
    20 #include "agentinfo.h"
    21 #include <caf/agent.h>
    22 #include <caf/agentfactory.h>
    23 #include <caf/agentinterface.h>
    24 
    25 using namespace ContentAccess;
    26 
    27 // Default buffer size of zero
    28 const TInt KCafApparcBufferSize = 0;
    29 
    30 CAgentInfo* CAgentInfo::NewLC(const CImplementationInformation& aImplInfo)
    31 	{
    32 	CAgentInfo* self = new (ELeave) CAgentInfo;
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL(aImplInfo);
    35 	return self;
    36 	}
    37 
    38 CAgentInfo::CAgentInfo()
    39 	{
    40 	}
    41 
    42 CAgentInfo::~CAgentInfo()
    43 	{
    44 	iSupplierMimeTypes.ResetAndDestroy();
    45 	iSupplierMimeTypes.Close();
    46 	iConsumerMimeTypes.ResetAndDestroy();
    47 	iConsumerMimeTypes.Close();
    48 	
    49 	// Delete the agent manager and agent factory, this may unload the agent DLL
    50 	delete iAgentManager;
    51 	delete iAgentFactory;
    52 	}
    53 
    54 
    55 void CAgentInfo::ConstructL(const CImplementationInformation& aImplInfo)
    56 	{
    57 	// Set up the agent member with the name and Uid of the agent
    58 	iAgent.SetValue(aImplInfo.DisplayName(), aImplInfo.ImplementationUid());
    59 
    60 	// get the name of the agents private directory
    61 	TInt length = aImplInfo.OpaqueData().Length();
    62 	if(length > KMaxSIDLength)
    63 		{
    64 		User::Leave(KErrCorrupt);
    65 		}
    66 	else
    67 		{
    68 		iPrivateDirectoryName.Copy(aImplInfo.OpaqueData().Left(KMaxSIDLength));
    69 		}
    70 			
    71 	// Extract from the data field from the info.
    72 	// The data field should be of the form:
    73 	// "<bufferlength>|<supplier1>,<supplier2>,....,<suppliern>:<consumer1>,<consumer2>"
    74 	// bufferlength is the desired length of the buffer passed from apparc to DoRecognize
    75 	TPtrC8 data(aImplInfo.DataType());
    76 	TPtrC8 supplier(KNullDesC8());
    77 	TPtrC8 consumer(KNullDesC8());
    78 
    79 	// Search for the "|" field to delimit buffersize and supplier mime types
    80 	TInt index = data.LocateF('|');
    81 	if(index < 0)
    82 		{
    83 		// No default buffersize, ie. corrupt
    84 		User::Leave(KErrCorrupt);
    85 		}
    86 	else
    87 		{
    88 		TPtrC8 buffersize(data.Left(index));
    89 		TLex8 bufferLex(buffersize);
    90 		bufferLex.Val(iPreferredBufferSize);
    91 
    92 		// Make sure a buffer size was actually specified before the | character
    93 		if(iPreferredBufferSize == 0)
    94 			{
    95 			iPreferredBufferSize = KCafApparcBufferSize;
    96 			}
    97 
    98 		// Make sure '|' is not the last character
    99 		if (index + 1 < data.Length())
   100 			{
   101 			data.Set(aImplInfo.DataType().Mid(index + 1));
   102 			}
   103 		else 
   104 			{
   105 			User::Leave(KErrCorrupt);
   106 			}
   107 		}
   108 
   109 	
   110 	// Search for the ":" field to delimit supplier and consumer
   111 	index = data.LocateF(':');
   112 	
   113 	// If the colon was present, then set the pointers appropriately
   114 	if (index >= 0)
   115 		{
   116 		// Set supplier pointer
   117 		supplier.Set(data.Left(index));
   118 
   119 		// Check that ':' is not the last character before setting consumer pointer
   120 		if (index + 1 < data.Length())
   121 			{
   122 			consumer.Set(data.Mid(index + 1));
   123 			}
   124 		}
   125 	else
   126 		{
   127 		User::Leave(KErrCorrupt);
   128 		}
   129 
   130 	// Now parse the supplier mime types
   131 	ParseMimeTypesL(supplier, iSupplierMimeTypes);
   132 	
   133 	// Do the same for the consumer mime types
   134 	ParseMimeTypesL(consumer, iConsumerMimeTypes);
   135 	}
   136 
   137 CAgentFactory& CAgentInfo::AgentFactoryL()
   138 	{
   139 	// Create the agent factory if it hasn't been done already
   140 	if (!iAgentFactory)
   141 		{
   142 		iAgentFactory = CAgentFactory::NewL(iAgent.ImplementationUid());
   143 		}
   144 	return *iAgentFactory;
   145 	}
   146 
   147 CAgentManager& CAgentInfo::AgentManagerL()
   148 	{
   149 	// Create the agent manager
   150 	if (!iAgentManager)
   151 		{
   152 		iAgentManager = AgentFactoryL().CreateManagerL();
   153 		}
   154 	return *iAgentManager;
   155 	}	
   156 
   157 TBool CAgentInfo::IsSupportedSupplier(const TDesC8& aSupplierMime) const
   158 	{
   159 	for (TInt i = 0; i < iSupplierMimeTypes.Count(); ++i)
   160 		{
   161 		if (aSupplierMime == *iSupplierMimeTypes[i])
   162 			{
   163 			return ETrue;
   164 			}
   165 		}
   166 	return EFalse;
   167 	}
   168 
   169 TBool CAgentInfo::IsSupportedConsumer(const TDesC8& aConsumerMime) const
   170 	{
   171 
   172 	for (TInt i = 0; i < iConsumerMimeTypes.Count(); ++i)
   173 		{
   174 		if (aConsumerMime == *iConsumerMimeTypes[i])
   175 			{
   176 			return ETrue;
   177 			}
   178 		}
   179 	return EFalse;
   180 	}
   181 		
   182 const RPointerArray<HBufC8>& CAgentInfo::SupplierMimeTypes() const
   183 	{
   184 	return iSupplierMimeTypes;
   185 	}
   186 
   187 const RPointerArray<HBufC8>& CAgentInfo::ConsumerMimeTypes() const
   188 	{
   189 	return iConsumerMimeTypes;
   190 	}
   191 
   192 void CAgentInfo::AddToArrayL(const TDesC8& aElement, 
   193 							 RPointerArray<HBufC8>& aArray)
   194 	{
   195 	// Don't bother adding empty elements
   196 	if (aElement.Length())
   197 		{
   198 		HBufC8* newElem = aElement.AllocLC();
   199 		TPtr8 lowerCasePtr = newElem->Des();
   200 		lowerCasePtr.LowerCase();
   201 		User::LeaveIfError(aArray.Append(newElem));
   202 		CleanupStack::Pop(newElem);
   203 		}
   204 	}
   205 
   206 void CAgentInfo::ParseMimeTypesL(const TDesC8& aBuf, 
   207 								 RPointerArray<HBufC8>& aMimeTypes)
   208 	{
   209 	TPtrC8 ptr(aBuf);
   210 	TInt pos = 0;
   211 	while ((pos = ptr.LocateF(',')) >= 0)
   212 		{
   213 		// Take into account possibility of ,,
   214 		if (pos > 0)
   215 			{
   216 			AddToArrayL(ptr.Left(pos), aMimeTypes);
   217 			}
   218 
   219 		// Now, move the pointer to the position after the ','. BUT if the
   220 		// ',' is the last position, then we are done, so return (a bit
   221 		// dirty, but makes things easier
   222 		if (pos + 1 < ptr.Length())
   223 			{
   224 			ptr.Set(ptr.Mid(pos + 1));
   225 			}
   226 		else
   227 			{
   228 			// The ',' is the last character, so quit
   229 			return;
   230 			}
   231 		}
   232 
   233 	// Now extract the last mime type.
   234 	AddToArrayL(ptr, aMimeTypes);
   235 	}
   236 
   237 TInt CAgentInfo::PreferredBufferSize() const
   238 	{
   239 	return iPreferredBufferSize;
   240 	}
   241 
   242 const TDesC& CAgentInfo::PrivateDirectoryName() const
   243 	{
   244 	return iPrivateDirectoryName;
   245 	}
   246 	
   247 TAgent& CAgentInfo::Agent()
   248 	{
   249 	return iAgent;
   250 	}