installationservices/swcomponentregistry/scrhelper/source/scrhelperserver.cpp
branchRCL_3
changeset 26 8b7f4e561641
parent 25 7333d7932ef7
child 27 e8965914fac7
equal deleted inserted replaced
25:7333d7932ef7 26:8b7f4e561641
     1 /*
       
     2 * Copyright (c) 2008-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 "scrhelpercommon.h"
       
    20 #include "scrhelperserver.h"
       
    21 #include <e32cmn.h>
       
    22 #include <e32debug.h>
       
    23 #include <usif/scr/scrcommon.h>
       
    24 
       
    25 using namespace Usif;
       
    26 
       
    27 static const TUint scrHelperRangeCount = 2;
       
    28 
       
    29 static const TInt scrHelperRanges[scrHelperRangeCount] =
       
    30 	{
       
    31 	0,							 // Range-0 - 0 to EBaseSession-1. Not used.
       
    32 	CScsServer::EBaseSession,	 // Range-1 - EBaseSession to KMaxTInt inclusive.
       
    33 	};
       
    34 
       
    35 static const TUint8 scrHelperElementsIndex[scrHelperRangeCount] =
       
    36 	{
       
    37 	CPolicyServer::ENotSupported, // Range 0 is not supported.
       
    38 	0,							  // Range 1 must come from the SCR Server.
       
    39 	};							
       
    40 
       
    41 static const CPolicyServer::TPolicyElement scrHelperElements[] =
       
    42 	{
       
    43 	{_INIT_SECURITY_POLICY_S0(KUidScrServer.iUid), CPolicyServer::EFailClient}
       
    44 	};
       
    45 
       
    46 static const CPolicyServer::TPolicy scrHelperPolicy =
       
    47 	{
       
    48 	CPolicyServer::EAlwaysPass, // Allow all connections
       
    49 	scrHelperRangeCount,
       
    50 	scrHelperRanges,
       
    51 	scrHelperElementsIndex,
       
    52 	scrHelperElements,
       
    53 	};
       
    54 
       
    55 /////////////////////
       
    56 // CScrHelperServer
       
    57 /////////////////////
       
    58 
       
    59 CScrHelperServer::CScrHelperServer()
       
    60 	:	CScsServer(ScrHelperServerVersion(), scrHelperPolicy)
       
    61 	{
       
    62 	// empty
       
    63 	}
       
    64 
       
    65 CScrHelperServer::~CScrHelperServer()
       
    66 	{
       
    67 	iFs.Close();
       
    68 	}
       
    69 
       
    70 CScrHelperServer* CScrHelperServer::NewLC()
       
    71 	{
       
    72 	CScrHelperServer* self = new(ELeave) CScrHelperServer();
       
    73 	CleanupStack::PushL(self);
       
    74 	self->ConstructL();
       
    75 	return self;
       
    76 	}
       
    77 
       
    78 void CScrHelperServer::ConstructL() 
       
    79 	{
       
    80 	User::LeaveIfError(iFs.Connect());
       
    81 	User::LeaveIfError(iFs.ShareProtected());
       
    82 
       
    83 	StartL(KScrHelperServerName);
       
    84 	CScsServer::ConstructL(KScrHelperServerShutdownPeriod);	
       
    85 	}
       
    86 
       
    87 CScsSession* CScrHelperServer::DoNewSessionL(const RMessage2& aMessage)
       
    88 /**
       
    89 	Implement CScsServer by allocating a new instance of CScrHelperSession.
       
    90 
       
    91 	@param	aMessage	Standard server-side handle to message.
       
    92 	@return				New instance of the SCR Helper session class which is owned by the caller.
       
    93  */
       
    94 	{
       
    95 	DEBUG_PRINTF(_L8("SCR Helper session creation!"));
       
    96 	return CScrHelperSession::NewL(*this, aMessage);
       
    97 	}
       
    98 
       
    99 inline RFs& CScrHelperServer::FileServer()
       
   100 	{
       
   101 	return iFs;
       
   102 	}
       
   103 
       
   104 /////////////////////
       
   105 // CScrHelperSession
       
   106 /////////////////////
       
   107 
       
   108 CScrHelperSession::CScrHelperSession(CScrHelperServer& aServer)
       
   109 	:	CScsSession(aServer)
       
   110 	{
       
   111 	// empty
       
   112 	}
       
   113 
       
   114 CScrHelperSession::~CScrHelperSession()
       
   115 	{
       
   116 	// empty
       
   117 	}
       
   118 
       
   119 CScrHelperSession* CScrHelperSession::NewL(CScrHelperServer &aServer, const RMessage2& aMessage)
       
   120 /**
       
   121 	Factory function allocates new instance of CScrSession.
       
   122 	
       
   123 	@param aServer  SCR Helper Server object.
       
   124 	@param aMessage	Standard server-side handle to message. Not used.
       
   125 	@return			Newly created instance of CScrHelperSession which is owned by the caller.
       
   126  */
       
   127 	{
       
   128 	(void)aMessage; // Make the compiler happy in release mode
       
   129 	CScrHelperSession* self = new (ELeave) CScrHelperSession(aServer);
       
   130 	CleanupStack::PushL(self);
       
   131 	self->ConstructL();
       
   132 	CleanupStack::Pop(self);
       
   133 	return self;
       
   134 	}
       
   135 
       
   136 TBool CScrHelperSession::DoServiceL(TInt aFunction, const RMessage2& aMessage)
       
   137 	{
       
   138 	TScrHelperServerMessages f = static_cast<TScrHelperServerMessages>(aFunction);
       
   139 	RFs& fs =  static_cast<CScrHelperServer*>(&iServer)->FileServer();
       
   140 	
       
   141 	switch (f)
       
   142 		{
       
   143 		case EGetDatabaseFileHandle:
       
   144 			GetFileHandleL(fs, aMessage, KScrDatabaseFilePath);
       
   145 			break;
       
   146 		case EGetJournalFileHandle:
       
   147 			GetFileHandleL(fs, aMessage, KScrJournalFilePath);
       
   148 			break;
       
   149 		default:
       
   150 			User::Leave(KErrNotSupported);
       
   151 		}
       
   152 	//RMessage2 object is closed by both TransferToClient() and SCS framework.
       
   153 	//return EFalse to prevent SCS to close the message object.
       
   154 	return EFalse;
       
   155 	}
       
   156 
       
   157 void CopyDbFromROMToSystemL(RFs& aFs, const TDesC& aTargetPath)
       
   158 	{
       
   159 	CFileMan* fileManager = CFileMan::NewL(aFs);
       
   160 	CleanupStack::PushL(fileManager);
       
   161 	User::LeaveIfError(fileManager->Copy(KScrDbRomPath, aTargetPath, 0));
       
   162 
       
   163 	// Reset the read-only attribute on the copied file
       
   164 	User::LeaveIfError(aFs.SetAtt(aTargetPath, 0, KEntryAttReadOnly));
       
   165 	CleanupStack::PopAndDestroy(fileManager);
       
   166 	}
       
   167 
       
   168 void CScrHelperSession::GetFileHandleL(RFs& aFs, const RMessage2& aMessage, const TDesC& aFileName)
       
   169 	{
       
   170 	DEBUG_PRINTF2(_L("Returning file handle of %S."), &aFileName);
       
   171 	RBuf filePath;
       
   172 	filePath.CreateL(aFileName.Length());
       
   173 	filePath.CleanupClosePushL();
       
   174 	filePath.Copy(aFileName);
       
   175 	filePath[0] = aFs.GetSystemDriveChar();
       
   176 	TInt err = aFs.MkDirAll(filePath);
       
   177 	if(KErrNone != err && KErrAlreadyExists != err)
       
   178 		{
       
   179 		DEBUG_PRINTF3(_L("An error (%d) occured while making all directories of %S."), err, &filePath);
       
   180 		User::Leave(err);
       
   181 		}
       
   182 	
       
   183 	RFile file;
       
   184 	TEntry entry;
       
   185 
       
   186 	if(KErrNone == aFs.Entry(filePath, entry))
       
   187 		{ // The file exists, just open it.
       
   188 		User::LeaveIfError(file.Open(aFs, filePath, EFileShareAny|EFileWrite));
       
   189 		}    
       
   190 	else
       
   191 		{ // The file doesn't exist. First, check if the requested file is database or journal file.
       
   192 		if(KScrDatabaseFilePath() == aFileName)
       
   193 			{ // This is the database file. Copy the default one into the requested location.
       
   194 			DEBUG_PRINTF(_L8("SCR database file doesn't exist. It is being copied from ROM"));
       
   195 			CopyDbFromROMToSystemL(aFs, filePath);
       
   196 			 // Then, open the file.
       
   197 			User::LeaveIfError(file.Open(aFs, filePath, EFileShareAny|EFileWrite));
       
   198 			}
       
   199 		else
       
   200 			{ // This is the journal file, simply create an empty file.
       
   201 			User::LeaveIfError(file.Create(aFs, filePath, EFileShareAny|EFileWrite));
       
   202 			}
       
   203 		}
       
   204 	CleanupStack::PopAndDestroy(&filePath);
       
   205 	CleanupClosePushL(file);
       
   206 	
       
   207 	// Store the RFile handle into the package buffer in slot 0 and complete the message with the RFs handle
       
   208 	User::LeaveIfError(file.TransferToClient(aMessage, 0));
       
   209 	ASSERT(aMessage.IsNull());  // The message should have been completed
       
   210 
       
   211 	CleanupStack::PopAndDestroy(&file);
       
   212 	}