os/security/authorisation/userpromptservice/server/source/upsserver/upsdbmanager.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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Implements the UPS database handle manager.  See class and function
sl@0
    16
* definitions for more information.
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
*/
sl@0
    24
#include <f32file.h>
sl@0
    25
#include "upscommon.h"
sl@0
    26
#include <ups/upsdbw.h>
sl@0
    27
#include "upsdbmanager.h"
sl@0
    28
sl@0
    29
namespace UserPromptService
sl@0
    30
{
sl@0
    31
sl@0
    32
RUpsDbHandleMaster::RUpsDbHandleMaster(RFs &aFs)
sl@0
    33
/**
sl@0
    34
	Construct the handle master
sl@0
    35
sl@0
    36
	This class should be used for synchronous database operations.
sl@0
    37
sl@0
    38
	If you need to do asynchronous operations then create/use an instance of the RUpsDbHandleSlave
sl@0
    39
	linked to the master.
sl@0
    40
sl@0
    41
	If your database operation fails, particularly due to OOM, then you should call Close your handle. You probably
sl@0
    42
	do NOT need to explicitly re-open it - it will automatically re-open when the -> operator is used.
sl@0
    43
sl@0
    44
	Note that the -> operator may leave.
sl@0
    45
*/
sl@0
    46
	: iFs(aFs), iDatabase(0), iClientListHead(0)
sl@0
    47
	{
sl@0
    48
	}
sl@0
    49
sl@0
    50
RUpsDbHandleMaster::~RUpsDbHandleMaster()
sl@0
    51
/**
sl@0
    52
	Destroy the handle master
sl@0
    53
*/
sl@0
    54
	{
sl@0
    55
	ASSERT(iClientListHead == 0); // Should be no clients!!!!
sl@0
    56
	Close(); // Deletes iDatabase...
sl@0
    57
	iClientListHead = 0;
sl@0
    58
	// Do NOT close the iFs (it is a reference to the main server's handle).
sl@0
    59
	}
sl@0
    60
sl@0
    61
_LIT(KDatabaseFile,"?:\\private\\10283558\\database\\ups.db");
sl@0
    62
void RUpsDbHandleMaster::OpenL()
sl@0
    63
/**
sl@0
    64
	Open the database handle
sl@0
    65
sl@0
    66
       It is probably clearer to explictly call this function to open the handle before first use, but the -> operator will
sl@0
    67
       auto-open it if called on a closed handle. Note that the -> operator can leave.
sl@0
    68
sl@0
    69
	If any database operation fails, particularly due to OOM, then you should Close your handle. Instead of 
sl@0
    70
	immediately re-opening it, it is generally safer to let it auto-reopen when the -> operator is next called.
sl@0
    71
sl@0
    72
*/
sl@0
    73
	{
sl@0
    74
	BULLSEYE_OFF
sl@0
    75
	if(iDatabase != 0)
sl@0
    76
		{
sl@0
    77
		// Close/delete the existing database handle.
sl@0
    78
		Close();
sl@0
    79
		}
sl@0
    80
	BULLSEYE_RESTORE
sl@0
    81
sl@0
    82
	// Open up a new handle.
sl@0
    83
	TFileName databaseFileBuf = KDatabaseFile();
sl@0
    84
sl@0
    85
	databaseFileBuf[0] = iFs.GetSystemDriveChar();
sl@0
    86
	TParsePtrC databaseFile(databaseFileBuf);
sl@0
    87
sl@0
    88
	// Make sure the dir exists
sl@0
    89
	(void)iFs.MkDirAll(databaseFile.DriveAndPath());
sl@0
    90
	// Create/Open the database
sl@0
    91
	iDatabase = CDecisionDbW::NewL(databaseFile.FullName(), iFs);
sl@0
    92
	}
sl@0
    93
sl@0
    94
void RUpsDbHandleMaster::Close()
sl@0
    95
/**
sl@0
    96
	Notify all client handles that the master (real) database handle is about to be closed, and then close it.
sl@0
    97
*/
sl@0
    98
	{
sl@0
    99
	// Notify all registered clients that we are about to delete the database handle
sl@0
   100
	RUpsDbHandleSlave *p = iClientListHead;
sl@0
   101
	while(p)
sl@0
   102
		{
sl@0
   103
		ASSERT(p->iClient != 0);
sl@0
   104
		p->iClient->DbHandleAboutToBeDeleted();
sl@0
   105
		p = p->iClientListNext;
sl@0
   106
		}
sl@0
   107
sl@0
   108
	// Delete database handle
sl@0
   109
	delete iDatabase;
sl@0
   110
	iDatabase = 0;
sl@0
   111
	}
sl@0
   112
sl@0
   113
TBool RUpsDbHandleMaster::IsOpen() const
sl@0
   114
/**
sl@0
   115
	Returns true if the handle is already open.
sl@0
   116
*/
sl@0
   117
	{
sl@0
   118
	return iDatabase != 0;
sl@0
   119
	}
sl@0
   120
sl@0
   121
CDecisionDbW *RUpsDbHandleMaster::operator->()
sl@0
   122
/**
sl@0
   123
	Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
sl@0
   124
sl@0
   125
	If the database is not already open then it calls OpenL.
sl@0
   126
sl@0
   127
	This operator CAN leave.
sl@0
   128
*/
sl@0
   129
	{
sl@0
   130
	if(iDatabase == 0)
sl@0
   131
		{
sl@0
   132
		OpenL();
sl@0
   133
		}
sl@0
   134
	return iDatabase;
sl@0
   135
	}
sl@0
   136
sl@0
   137
void RUpsDbHandleMaster::Register(RUpsDbHandleSlave *aClient)
sl@0
   138
/**
sl@0
   139
	Register new client with us.
sl@0
   140
*/
sl@0
   141
	{
sl@0
   142
	BULLSEYE_OFF
sl@0
   143
	if(aClient == 0)
sl@0
   144
		{
sl@0
   145
		return;
sl@0
   146
		}
sl@0
   147
	BULLSEYE_RESTORE
sl@0
   148
		
sl@0
   149
	aClient->iClientListNext = iClientListHead;
sl@0
   150
	iClientListHead = aClient;
sl@0
   151
	}
sl@0
   152
sl@0
   153
void RUpsDbHandleMaster::UnRegister(RUpsDbHandleSlave *aClient)
sl@0
   154
/**
sl@0
   155
	UnRegister client with us.
sl@0
   156
*/
sl@0
   157
	{
sl@0
   158
	RUpsDbHandleSlave **pp = &iClientListHead;
sl@0
   159
BULLSEYE_OFF
sl@0
   160
	while(*pp)
sl@0
   161
		{
sl@0
   162
BULLSEYE_RESTORE
sl@0
   163
		if(*pp == aClient)
sl@0
   164
			{
sl@0
   165
			// Found ptr to client, so remove it.
sl@0
   166
			*pp = (*pp)->iClientListNext;
sl@0
   167
			aClient->iClientListNext = 0;
sl@0
   168
			break;
sl@0
   169
			}
sl@0
   170
		pp = &(*pp)->iClientListNext;
sl@0
   171
		}
sl@0
   172
	}
sl@0
   173
sl@0
   174
sl@0
   175
sl@0
   176
RUpsDbHandleSlave::RUpsDbHandleSlave(RUpsDbHandleMaster & aMaster, MDbHandleClient * aClient)
sl@0
   177
/**
sl@0
   178
	Initialise new client and register it with the RUpsDbHandleMaster
sl@0
   179
sl@0
   180
	This class is intended for use when asynchronous database operations are being performed. These may then be failed
sl@0
   181
	due to another database operation failing, BUT the asycnhronous operation classes (eg. a db view) needs deleting
sl@0
   182
	before the actual database handle is deleted/re-created.
sl@0
   183
*/
sl@0
   184
	: iInUse(EFalse), iMaster(aMaster), iClient(aClient), iClientListNext(0)
sl@0
   185
	{
sl@0
   186
	ASSERT(aClient != 0);
sl@0
   187
	//RDebug::Printf("RUpsDbHandleSlave(%x, %x) - %x\n", &aMaster, aClient, this);
sl@0
   188
	}
sl@0
   189
sl@0
   190
RUpsDbHandleSlave::~RUpsDbHandleSlave()
sl@0
   191
	{
sl@0
   192
	//RDebug::Printf("~RUpsDbHandleSlave() master %x client %x - %x\n", &iMaster, iClient, this);
sl@0
   193
	Close();
sl@0
   194
	}
sl@0
   195
sl@0
   196
CDecisionDbW *RUpsDbHandleSlave::operator->()
sl@0
   197
/**
sl@0
   198
	Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
sl@0
   199
sl@0
   200
	If the database is not already open, then the master will open it.
sl@0
   201
sl@0
   202
	This operator CAN leave.
sl@0
   203
*/
sl@0
   204
	{
sl@0
   205
	if(!iInUse)
sl@0
   206
		{
sl@0
   207
		iMaster.Register(this);
sl@0
   208
		iInUse = ETrue;
sl@0
   209
		}
sl@0
   210
	return iMaster.operator->();
sl@0
   211
	}
sl@0
   212
sl@0
   213
void RUpsDbHandleSlave::SetCallback(MDbHandleClient *aClient)
sl@0
   214
/**
sl@0
   215
	Change what callback should be run if the master handle is closed.
sl@0
   216
	Do NOT set it to 0.
sl@0
   217
*/
sl@0
   218
	{
sl@0
   219
	ASSERT(aClient != 0);
sl@0
   220
	//RDebug::Printf("RUpsDbHandleSlave::SetCallback client %x - %x\n", aClient, this);
sl@0
   221
	iClient = aClient;
sl@0
   222
	}
sl@0
   223
sl@0
   224
void RUpsDbHandleSlave::Close()
sl@0
   225
/**
sl@0
   226
	Close this slave handle. This does NOT close the master, it just means we de-register with the
sl@0
   227
	master to we are no longer notified if the master is closed.
sl@0
   228
	It will automatically re-register if the -> operator is subsequently used.
sl@0
   229
*/
sl@0
   230
	{
sl@0
   231
	if(iInUse)
sl@0
   232
		{
sl@0
   233
		iMaster.UnRegister(this);
sl@0
   234
		iInUse = 0;
sl@0
   235
		}
sl@0
   236
	}
sl@0
   237
sl@0
   238
void RUpsDbHandleSlave::CloseMaster()
sl@0
   239
/**
sl@0
   240
	Notify all client handles that the master (real) database handle is about to be closed, and then close it.
sl@0
   241
sl@0
   242
	Typically used after a database error (particularly OOM) in an attempt to recover. Usually the database is not 
sl@0
   243
	explicitly re-opened, instead the re-open is defered until the next use of the -> operator on either tha master
sl@0
   244
	or a client handle.
sl@0
   245
*/
sl@0
   246
	{
sl@0
   247
		Close();
sl@0
   248
		iMaster.Close();
sl@0
   249
	}
sl@0
   250
sl@0
   251
sl@0
   252
} // End of UserPromptService namespace
sl@0
   253
sl@0
   254
// End of file