os/security/authorisation/userpromptservice/server/source/upsserver/upsdbmanager.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/authorisation/userpromptservice/server/source/upsserver/upsdbmanager.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,254 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Implements the UPS database handle manager.  See class and function
    1.19 +* definitions for more information.
    1.20 +*
    1.21 +*/
    1.22 +
    1.23 +
    1.24 +/**
    1.25 + @file
    1.26 +*/
    1.27 +#include <f32file.h>
    1.28 +#include "upscommon.h"
    1.29 +#include <ups/upsdbw.h>
    1.30 +#include "upsdbmanager.h"
    1.31 +
    1.32 +namespace UserPromptService
    1.33 +{
    1.34 +
    1.35 +RUpsDbHandleMaster::RUpsDbHandleMaster(RFs &aFs)
    1.36 +/**
    1.37 +	Construct the handle master
    1.38 +
    1.39 +	This class should be used for synchronous database operations.
    1.40 +
    1.41 +	If you need to do asynchronous operations then create/use an instance of the RUpsDbHandleSlave
    1.42 +	linked to the master.
    1.43 +
    1.44 +	If your database operation fails, particularly due to OOM, then you should call Close your handle. You probably
    1.45 +	do NOT need to explicitly re-open it - it will automatically re-open when the -> operator is used.
    1.46 +
    1.47 +	Note that the -> operator may leave.
    1.48 +*/
    1.49 +	: iFs(aFs), iDatabase(0), iClientListHead(0)
    1.50 +	{
    1.51 +	}
    1.52 +
    1.53 +RUpsDbHandleMaster::~RUpsDbHandleMaster()
    1.54 +/**
    1.55 +	Destroy the handle master
    1.56 +*/
    1.57 +	{
    1.58 +	ASSERT(iClientListHead == 0); // Should be no clients!!!!
    1.59 +	Close(); // Deletes iDatabase...
    1.60 +	iClientListHead = 0;
    1.61 +	// Do NOT close the iFs (it is a reference to the main server's handle).
    1.62 +	}
    1.63 +
    1.64 +_LIT(KDatabaseFile,"?:\\private\\10283558\\database\\ups.db");
    1.65 +void RUpsDbHandleMaster::OpenL()
    1.66 +/**
    1.67 +	Open the database handle
    1.68 +
    1.69 +       It is probably clearer to explictly call this function to open the handle before first use, but the -> operator will
    1.70 +       auto-open it if called on a closed handle. Note that the -> operator can leave.
    1.71 +
    1.72 +	If any database operation fails, particularly due to OOM, then you should Close your handle. Instead of 
    1.73 +	immediately re-opening it, it is generally safer to let it auto-reopen when the -> operator is next called.
    1.74 +
    1.75 +*/
    1.76 +	{
    1.77 +	BULLSEYE_OFF
    1.78 +	if(iDatabase != 0)
    1.79 +		{
    1.80 +		// Close/delete the existing database handle.
    1.81 +		Close();
    1.82 +		}
    1.83 +	BULLSEYE_RESTORE
    1.84 +
    1.85 +	// Open up a new handle.
    1.86 +	TFileName databaseFileBuf = KDatabaseFile();
    1.87 +
    1.88 +	databaseFileBuf[0] = iFs.GetSystemDriveChar();
    1.89 +	TParsePtrC databaseFile(databaseFileBuf);
    1.90 +
    1.91 +	// Make sure the dir exists
    1.92 +	(void)iFs.MkDirAll(databaseFile.DriveAndPath());
    1.93 +	// Create/Open the database
    1.94 +	iDatabase = CDecisionDbW::NewL(databaseFile.FullName(), iFs);
    1.95 +	}
    1.96 +
    1.97 +void RUpsDbHandleMaster::Close()
    1.98 +/**
    1.99 +	Notify all client handles that the master (real) database handle is about to be closed, and then close it.
   1.100 +*/
   1.101 +	{
   1.102 +	// Notify all registered clients that we are about to delete the database handle
   1.103 +	RUpsDbHandleSlave *p = iClientListHead;
   1.104 +	while(p)
   1.105 +		{
   1.106 +		ASSERT(p->iClient != 0);
   1.107 +		p->iClient->DbHandleAboutToBeDeleted();
   1.108 +		p = p->iClientListNext;
   1.109 +		}
   1.110 +
   1.111 +	// Delete database handle
   1.112 +	delete iDatabase;
   1.113 +	iDatabase = 0;
   1.114 +	}
   1.115 +
   1.116 +TBool RUpsDbHandleMaster::IsOpen() const
   1.117 +/**
   1.118 +	Returns true if the handle is already open.
   1.119 +*/
   1.120 +	{
   1.121 +	return iDatabase != 0;
   1.122 +	}
   1.123 +
   1.124 +CDecisionDbW *RUpsDbHandleMaster::operator->()
   1.125 +/**
   1.126 +	Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
   1.127 +
   1.128 +	If the database is not already open then it calls OpenL.
   1.129 +
   1.130 +	This operator CAN leave.
   1.131 +*/
   1.132 +	{
   1.133 +	if(iDatabase == 0)
   1.134 +		{
   1.135 +		OpenL();
   1.136 +		}
   1.137 +	return iDatabase;
   1.138 +	}
   1.139 +
   1.140 +void RUpsDbHandleMaster::Register(RUpsDbHandleSlave *aClient)
   1.141 +/**
   1.142 +	Register new client with us.
   1.143 +*/
   1.144 +	{
   1.145 +	BULLSEYE_OFF
   1.146 +	if(aClient == 0)
   1.147 +		{
   1.148 +		return;
   1.149 +		}
   1.150 +	BULLSEYE_RESTORE
   1.151 +		
   1.152 +	aClient->iClientListNext = iClientListHead;
   1.153 +	iClientListHead = aClient;
   1.154 +	}
   1.155 +
   1.156 +void RUpsDbHandleMaster::UnRegister(RUpsDbHandleSlave *aClient)
   1.157 +/**
   1.158 +	UnRegister client with us.
   1.159 +*/
   1.160 +	{
   1.161 +	RUpsDbHandleSlave **pp = &iClientListHead;
   1.162 +BULLSEYE_OFF
   1.163 +	while(*pp)
   1.164 +		{
   1.165 +BULLSEYE_RESTORE
   1.166 +		if(*pp == aClient)
   1.167 +			{
   1.168 +			// Found ptr to client, so remove it.
   1.169 +			*pp = (*pp)->iClientListNext;
   1.170 +			aClient->iClientListNext = 0;
   1.171 +			break;
   1.172 +			}
   1.173 +		pp = &(*pp)->iClientListNext;
   1.174 +		}
   1.175 +	}
   1.176 +
   1.177 +
   1.178 +
   1.179 +RUpsDbHandleSlave::RUpsDbHandleSlave(RUpsDbHandleMaster & aMaster, MDbHandleClient * aClient)
   1.180 +/**
   1.181 +	Initialise new client and register it with the RUpsDbHandleMaster
   1.182 +
   1.183 +	This class is intended for use when asynchronous database operations are being performed. These may then be failed
   1.184 +	due to another database operation failing, BUT the asycnhronous operation classes (eg. a db view) needs deleting
   1.185 +	before the actual database handle is deleted/re-created.
   1.186 +*/
   1.187 +	: iInUse(EFalse), iMaster(aMaster), iClient(aClient), iClientListNext(0)
   1.188 +	{
   1.189 +	ASSERT(aClient != 0);
   1.190 +	//RDebug::Printf("RUpsDbHandleSlave(%x, %x) - %x\n", &aMaster, aClient, this);
   1.191 +	}
   1.192 +
   1.193 +RUpsDbHandleSlave::~RUpsDbHandleSlave()
   1.194 +	{
   1.195 +	//RDebug::Printf("~RUpsDbHandleSlave() master %x client %x - %x\n", &iMaster, iClient, this);
   1.196 +	Close();
   1.197 +	}
   1.198 +
   1.199 +CDecisionDbW *RUpsDbHandleSlave::operator->()
   1.200 +/**
   1.201 +	Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
   1.202 +
   1.203 +	If the database is not already open, then the master will open it.
   1.204 +
   1.205 +	This operator CAN leave.
   1.206 +*/
   1.207 +	{
   1.208 +	if(!iInUse)
   1.209 +		{
   1.210 +		iMaster.Register(this);
   1.211 +		iInUse = ETrue;
   1.212 +		}
   1.213 +	return iMaster.operator->();
   1.214 +	}
   1.215 +
   1.216 +void RUpsDbHandleSlave::SetCallback(MDbHandleClient *aClient)
   1.217 +/**
   1.218 +	Change what callback should be run if the master handle is closed.
   1.219 +	Do NOT set it to 0.
   1.220 +*/
   1.221 +	{
   1.222 +	ASSERT(aClient != 0);
   1.223 +	//RDebug::Printf("RUpsDbHandleSlave::SetCallback client %x - %x\n", aClient, this);
   1.224 +	iClient = aClient;
   1.225 +	}
   1.226 +
   1.227 +void RUpsDbHandleSlave::Close()
   1.228 +/**
   1.229 +	Close this slave handle. This does NOT close the master, it just means we de-register with the
   1.230 +	master to we are no longer notified if the master is closed.
   1.231 +	It will automatically re-register if the -> operator is subsequently used.
   1.232 +*/
   1.233 +	{
   1.234 +	if(iInUse)
   1.235 +		{
   1.236 +		iMaster.UnRegister(this);
   1.237 +		iInUse = 0;
   1.238 +		}
   1.239 +	}
   1.240 +
   1.241 +void RUpsDbHandleSlave::CloseMaster()
   1.242 +/**
   1.243 +	Notify all client handles that the master (real) database handle is about to be closed, and then close it.
   1.244 +
   1.245 +	Typically used after a database error (particularly OOM) in an attempt to recover. Usually the database is not 
   1.246 +	explicitly re-opened, instead the re-open is defered until the next use of the -> operator on either tha master
   1.247 +	or a client handle.
   1.248 +*/
   1.249 +	{
   1.250 +		Close();
   1.251 +		iMaster.Close();
   1.252 +	}
   1.253 +
   1.254 +
   1.255 +} // End of UserPromptService namespace
   1.256 +
   1.257 +// End of file