os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServDatabaseChangeTracker.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServDatabaseChangeTracker.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,222 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "LogServDatabaseChangeTracker.h"
1.20 +#include "LogChangeDefinition.h"
1.21 +#include "LogServDatabaseTransactionInterface.h"
1.22 +#include "logservpanic.h"
1.23 +#include "LogServBackupInterface.h"
1.24 +
1.25 +// Constants
1.26 +const TInt KLogArrayGranularityObservers = 3;
1.27 +const TInt KLogArrayGranularityGlobalChanges = 2;
1.28 +
1.29 +#define UNUSED_VAR(a) a = a
1.30 +
1.31 +/////////////////////////////////////////////////////////////////////////////////////////
1.32 +// -----> CLogServDatabaseChangeTracker (source)
1.33 +/////////////////////////////////////////////////////////////////////////////////////////
1.34 +
1.35 +CLogServDatabaseChangeTracker::CLogServDatabaseChangeTracker(MLogServDatabaseTransactionInterface& aDatabase, MLogServBackupInterface& aBackupInterface, TInt aPriority)
1.36 +: CActive(aPriority), iDatabase(aDatabase), iBackupInterface(aBackupInterface), iObservers(KLogArrayGranularityObservers), iGlobalChanges(KLogArrayGranularityGlobalChanges)
1.37 + {
1.38 + CActiveScheduler::Add(this);
1.39 + }
1.40 +
1.41 +CLogServDatabaseChangeTracker::~CLogServDatabaseChangeTracker()
1.42 + {
1.43 + Cancel();
1.44 + //
1.45 + iBackupInterface.BIObserverRemove(*this);
1.46 + iNotifier.Close();
1.47 + iObservers.Close();
1.48 + iGlobalChanges.Close();
1.49 + //
1.50 + delete iIdler;
1.51 + delete iChanges;
1.52 + }
1.53 +
1.54 +void CLogServDatabaseChangeTracker::ConstructL()
1.55 + {
1.56 + iBackupInterface.BIObserverAddL(*this, MLogServBackupInterface::EObjectChangeTracker);
1.57 + iChanges = CLogChangeDefinition::NewL();
1.58 + //
1.59 + const TInt error = iNotifier.Open(iDatabase.DTIDatabase());
1.60 + User::LeaveIfError(error);
1.61 + //
1.62 + Request();
1.63 + //
1.64 + iIdler = CIdle::NewL(CActive::EPriorityIdle);
1.65 + }
1.66 +
1.67 +CLogServDatabaseChangeTracker* CLogServDatabaseChangeTracker::NewL(MLogServDatabaseTransactionInterface& aDatabase, MLogServBackupInterface& aBackupInterface, TInt aPriority)
1.68 + {
1.69 + CLogServDatabaseChangeTracker* self = new(ELeave) CLogServDatabaseChangeTracker(aDatabase, aBackupInterface, aPriority);
1.70 + CleanupStack::PushL(self);
1.71 + self->ConstructL();
1.72 + CleanupStack::Pop(self);
1.73 + return self;
1.74 + }
1.75 +
1.76 +/////////////////////////////////////////////////////////////////////////////////////////
1.77 +/////////////////////////////////////////////////////////////////////////////////////////
1.78 +/////////////////////////////////////////////////////////////////////////////////////////
1.79 +
1.80 +void CLogServDatabaseChangeTracker::DCISubmitChangedEventContextL(TLogDatabaseChangeType aType, TLogId aEventId)
1.81 + {
1.82 + __ASSERT_DEBUG(aType != ELogChangeTypeUndefined, Panic(ELogUnrecognizedChangeType2));
1.83 + iChanges->AddL(aEventId, aType, KErrGeneral);
1.84 + }
1.85 +
1.86 +void CLogServDatabaseChangeTracker::DCISubmitGlobalChangeContextL(TUid aChangeType, TInt aContextParam1, TInt aContextParam2, TInt aContextParam3)
1.87 + {
1.88 + const TLogServDatabaseChangeDefinition item(aChangeType, aContextParam1, aContextParam2, aContextParam3);
1.89 + User::LeaveIfError(iGlobalChanges.Append(item));
1.90 + //
1.91 + iIdler->Cancel();
1.92 + iIdler->Start(TCallBack(IdleNotifyGlobalChangeEvents, this));
1.93 + }
1.94 +
1.95 +void CLogServDatabaseChangeTracker::DCIRequestChangeNotificationsL(MLogServDatabaseChangeObserver& aObserver)
1.96 + {
1.97 + const TInt error = iObservers.InsertInAddressOrder(&aObserver);
1.98 + User::LeaveIfError(error);
1.99 + }
1.100 +
1.101 +void CLogServDatabaseChangeTracker::DCIRequestChangeNotificationsCancel(MLogServDatabaseChangeObserver& aObserver)
1.102 + {
1.103 + const TInt error = iObservers.FindInAddressOrder(&aObserver);
1.104 + if (error >= KErrNone)
1.105 + iObservers.Remove(error);
1.106 + }
1.107 +
1.108 +/////////////////////////////////////////////////////////////////////////////////////////
1.109 +/////////////////////////////////////////////////////////////////////////////////////////
1.110 +/////////////////////////////////////////////////////////////////////////////////////////
1.111 +
1.112 +void CLogServDatabaseChangeTracker::BOHandleEventL(TLogServBackupEvent aEvent)
1.113 + {
1.114 + switch(aEvent)
1.115 + {
1.116 + case EBackupStarting:
1.117 + {
1.118 + Cancel();
1.119 + iNotifier.Close();
1.120 + break;
1.121 + }
1.122 + case EBackupEnded:
1.123 + {
1.124 + const TInt error = iNotifier.Open(iDatabase.DTIDatabase());
1.125 + User::LeaveIfError(error);
1.126 + Request();
1.127 + break;
1.128 + }
1.129 + default:
1.130 + __ASSERT_DEBUG(EFalse, User::Invariant());
1.131 + break;
1.132 + }
1.133 + }
1.134 +
1.135 +/////////////////////////////////////////////////////////////////////////////////////////
1.136 +/////////////////////////////////////////////////////////////////////////////////////////
1.137 +/////////////////////////////////////////////////////////////////////////////////////////
1.138 +
1.139 +void CLogServDatabaseChangeTracker::RunL()
1.140 + {
1.141 + // We are only interested if a commit has occurred
1.142 + const TInt completionCode = iStatus.Int();
1.143 + if (completionCode == RDbNotifier::ECommit)
1.144 + {
1.145 + LOGTEXT("CLogServDatabaseChangeTracker::RunL() - database commit");
1.146 +
1.147 + // Notify all observers
1.148 + NotifyObserversL();
1.149 +
1.150 + // Reset changes
1.151 + iChanges->Reset();
1.152 + }
1.153 + Request();
1.154 + }
1.155 +
1.156 +void CLogServDatabaseChangeTracker::DoCancel()
1.157 + {
1.158 + iNotifier.Cancel();
1.159 + }
1.160 +
1.161 +TInt CLogServDatabaseChangeTracker::RunError(TInt /*aError*/)
1.162 + {
1.163 + // Ignore errors such as leaves from notifying observers
1.164 + Request();
1.165 + return KErrNone;
1.166 + }
1.167 +
1.168 +/////////////////////////////////////////////////////////////////////////////////////////
1.169 +/////////////////////////////////////////////////////////////////////////////////////////
1.170 +/////////////////////////////////////////////////////////////////////////////////////////
1.171 +
1.172 +void CLogServDatabaseChangeTracker::Request()
1.173 + {
1.174 + iNotifier.NotifyChange(iStatus);
1.175 + SetActive();
1.176 + }
1.177 +
1.178 +void CLogServDatabaseChangeTracker::NotifyObserversL()
1.179 + {
1.180 + LOGTEXT("CLogServDatabaseChangeTracker::NotifyObserversL()");
1.181 + //
1.182 + const TInt count = iObservers.Count();
1.183 + for(TInt i=0; i<count; i++)
1.184 + {
1.185 + MLogServDatabaseChangeObserver* observer = iObservers[i];
1.186 + observer->DCOHandleChangeEventsL(*iChanges);
1.187 + }
1.188 +
1.189 + // Reduce memory usage
1.190 + iChanges->Reset();
1.191 + //
1.192 + LOGTEXT("CLogServDatabaseChangeTracker::NotifyObserversL() - end");
1.193 + }
1.194 +
1.195 +void CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL()
1.196 + {
1.197 + LOGTEXT("CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL()");
1.198 + //
1.199 + const TInt count = iGlobalChanges.Count();
1.200 + for(TInt i=0; i<count; i++)
1.201 + {
1.202 + const TLogServDatabaseChangeDefinition& change = iGlobalChanges[i];
1.203 + //
1.204 + const TInt observerCount = iObservers.Count();
1.205 + for(TInt j=0; j<observerCount; j++)
1.206 + {
1.207 + MLogServDatabaseChangeObserver* observer = iObservers[j];
1.208 + observer->DCOHandleGlobalChangeEventL(change);
1.209 + }
1.210 + }
1.211 +
1.212 + // Reduce memory usage
1.213 + iGlobalChanges.Reset();
1.214 + iGlobalChanges.GranularCompress();
1.215 + //
1.216 + LOGTEXT("CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL() - end");
1.217 + }
1.218 +
1.219 +TBool CLogServDatabaseChangeTracker::IdleNotifyGlobalChangeEvents(TAny* aSelf)
1.220 + {
1.221 + CLogServDatabaseChangeTracker* self = reinterpret_cast<CLogServDatabaseChangeTracker*>(aSelf);
1.222 + TRAPD(err, self->NotifyGlobalChangeEventsL());
1.223 + UNUSED_VAR(err);
1.224 + return EFalse;
1.225 + }