os/security/authorisation/userpromptservice/server/source/upsserver/upsdbmanager.cpp
First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implements the UPS database handle manager. See class and function
16 * definitions for more information.
25 #include "upscommon.h"
26 #include <ups/upsdbw.h>
27 #include "upsdbmanager.h"
29 namespace UserPromptService
32 RUpsDbHandleMaster::RUpsDbHandleMaster(RFs &aFs)
34 Construct the handle master
36 This class should be used for synchronous database operations.
38 If you need to do asynchronous operations then create/use an instance of the RUpsDbHandleSlave
41 If your database operation fails, particularly due to OOM, then you should call Close your handle. You probably
42 do NOT need to explicitly re-open it - it will automatically re-open when the -> operator is used.
44 Note that the -> operator may leave.
46 : iFs(aFs), iDatabase(0), iClientListHead(0)
50 RUpsDbHandleMaster::~RUpsDbHandleMaster()
52 Destroy the handle master
55 ASSERT(iClientListHead == 0); // Should be no clients!!!!
56 Close(); // Deletes iDatabase...
58 // Do NOT close the iFs (it is a reference to the main server's handle).
61 _LIT(KDatabaseFile,"?:\\private\\10283558\\database\\ups.db");
62 void RUpsDbHandleMaster::OpenL()
64 Open the database handle
66 It is probably clearer to explictly call this function to open the handle before first use, but the -> operator will
67 auto-open it if called on a closed handle. Note that the -> operator can leave.
69 If any database operation fails, particularly due to OOM, then you should Close your handle. Instead of
70 immediately re-opening it, it is generally safer to let it auto-reopen when the -> operator is next called.
77 // Close/delete the existing database handle.
82 // Open up a new handle.
83 TFileName databaseFileBuf = KDatabaseFile();
85 databaseFileBuf[0] = iFs.GetSystemDriveChar();
86 TParsePtrC databaseFile(databaseFileBuf);
88 // Make sure the dir exists
89 (void)iFs.MkDirAll(databaseFile.DriveAndPath());
90 // Create/Open the database
91 iDatabase = CDecisionDbW::NewL(databaseFile.FullName(), iFs);
94 void RUpsDbHandleMaster::Close()
96 Notify all client handles that the master (real) database handle is about to be closed, and then close it.
99 // Notify all registered clients that we are about to delete the database handle
100 RUpsDbHandleSlave *p = iClientListHead;
103 ASSERT(p->iClient != 0);
104 p->iClient->DbHandleAboutToBeDeleted();
105 p = p->iClientListNext;
108 // Delete database handle
113 TBool RUpsDbHandleMaster::IsOpen() const
115 Returns true if the handle is already open.
118 return iDatabase != 0;
121 CDecisionDbW *RUpsDbHandleMaster::operator->()
123 Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
125 If the database is not already open then it calls OpenL.
127 This operator CAN leave.
137 void RUpsDbHandleMaster::Register(RUpsDbHandleSlave *aClient)
139 Register new client with us.
149 aClient->iClientListNext = iClientListHead;
150 iClientListHead = aClient;
153 void RUpsDbHandleMaster::UnRegister(RUpsDbHandleSlave *aClient)
155 UnRegister client with us.
158 RUpsDbHandleSlave **pp = &iClientListHead;
165 // Found ptr to client, so remove it.
166 *pp = (*pp)->iClientListNext;
167 aClient->iClientListNext = 0;
170 pp = &(*pp)->iClientListNext;
176 RUpsDbHandleSlave::RUpsDbHandleSlave(RUpsDbHandleMaster & aMaster, MDbHandleClient * aClient)
178 Initialise new client and register it with the RUpsDbHandleMaster
180 This class is intended for use when asynchronous database operations are being performed. These may then be failed
181 due to another database operation failing, BUT the asycnhronous operation classes (eg. a db view) needs deleting
182 before the actual database handle is deleted/re-created.
184 : iInUse(EFalse), iMaster(aMaster), iClient(aClient), iClientListNext(0)
186 ASSERT(aClient != 0);
187 //RDebug::Printf("RUpsDbHandleSlave(%x, %x) - %x\n", &aMaster, aClient, this);
190 RUpsDbHandleSlave::~RUpsDbHandleSlave()
192 //RDebug::Printf("~RUpsDbHandleSlave() master %x client %x - %x\n", &iMaster, iClient, this);
196 CDecisionDbW *RUpsDbHandleSlave::operator->()
198 Returns the database handle so -> can be used on an RUpsDbHandleMaster instance to call database functions
200 If the database is not already open, then the master will open it.
202 This operator CAN leave.
207 iMaster.Register(this);
210 return iMaster.operator->();
213 void RUpsDbHandleSlave::SetCallback(MDbHandleClient *aClient)
215 Change what callback should be run if the master handle is closed.
219 ASSERT(aClient != 0);
220 //RDebug::Printf("RUpsDbHandleSlave::SetCallback client %x - %x\n", aClient, this);
224 void RUpsDbHandleSlave::Close()
226 Close this slave handle. This does NOT close the master, it just means we de-register with the
227 master to we are no longer notified if the master is closed.
228 It will automatically re-register if the -> operator is subsequently used.
233 iMaster.UnRegister(this);
238 void RUpsDbHandleSlave::CloseMaster()
240 Notify all client handles that the master (real) database handle is about to be closed, and then close it.
242 Typically used after a database error (particularly OOM) in an attempt to recover. Usually the database is not
243 explicitly re-opened, instead the re-open is defered until the next use of the -> operator on either tha master
252 } // End of UserPromptService namespace