os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServDatabaseChangeTracker.cpp
First public contribution.
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "LogServDatabaseChangeTracker.h"
17 #include "LogChangeDefinition.h"
18 #include "LogServDatabaseTransactionInterface.h"
19 #include "logservpanic.h"
20 #include "LogServBackupInterface.h"
23 const TInt KLogArrayGranularityObservers = 3;
24 const TInt KLogArrayGranularityGlobalChanges = 2;
26 #define UNUSED_VAR(a) a = a
28 /////////////////////////////////////////////////////////////////////////////////////////
29 // -----> CLogServDatabaseChangeTracker (source)
30 /////////////////////////////////////////////////////////////////////////////////////////
32 CLogServDatabaseChangeTracker::CLogServDatabaseChangeTracker(MLogServDatabaseTransactionInterface& aDatabase, MLogServBackupInterface& aBackupInterface, TInt aPriority)
33 : CActive(aPriority), iDatabase(aDatabase), iBackupInterface(aBackupInterface), iObservers(KLogArrayGranularityObservers), iGlobalChanges(KLogArrayGranularityGlobalChanges)
35 CActiveScheduler::Add(this);
38 CLogServDatabaseChangeTracker::~CLogServDatabaseChangeTracker()
42 iBackupInterface.BIObserverRemove(*this);
45 iGlobalChanges.Close();
51 void CLogServDatabaseChangeTracker::ConstructL()
53 iBackupInterface.BIObserverAddL(*this, MLogServBackupInterface::EObjectChangeTracker);
54 iChanges = CLogChangeDefinition::NewL();
56 const TInt error = iNotifier.Open(iDatabase.DTIDatabase());
57 User::LeaveIfError(error);
61 iIdler = CIdle::NewL(CActive::EPriorityIdle);
64 CLogServDatabaseChangeTracker* CLogServDatabaseChangeTracker::NewL(MLogServDatabaseTransactionInterface& aDatabase, MLogServBackupInterface& aBackupInterface, TInt aPriority)
66 CLogServDatabaseChangeTracker* self = new(ELeave) CLogServDatabaseChangeTracker(aDatabase, aBackupInterface, aPriority);
67 CleanupStack::PushL(self);
69 CleanupStack::Pop(self);
73 /////////////////////////////////////////////////////////////////////////////////////////
74 /////////////////////////////////////////////////////////////////////////////////////////
75 /////////////////////////////////////////////////////////////////////////////////////////
77 void CLogServDatabaseChangeTracker::DCISubmitChangedEventContextL(TLogDatabaseChangeType aType, TLogId aEventId)
79 __ASSERT_DEBUG(aType != ELogChangeTypeUndefined, Panic(ELogUnrecognizedChangeType2));
80 iChanges->AddL(aEventId, aType, KErrGeneral);
83 void CLogServDatabaseChangeTracker::DCISubmitGlobalChangeContextL(TUid aChangeType, TInt aContextParam1, TInt aContextParam2, TInt aContextParam3)
85 const TLogServDatabaseChangeDefinition item(aChangeType, aContextParam1, aContextParam2, aContextParam3);
86 User::LeaveIfError(iGlobalChanges.Append(item));
89 iIdler->Start(TCallBack(IdleNotifyGlobalChangeEvents, this));
92 void CLogServDatabaseChangeTracker::DCIRequestChangeNotificationsL(MLogServDatabaseChangeObserver& aObserver)
94 const TInt error = iObservers.InsertInAddressOrder(&aObserver);
95 User::LeaveIfError(error);
98 void CLogServDatabaseChangeTracker::DCIRequestChangeNotificationsCancel(MLogServDatabaseChangeObserver& aObserver)
100 const TInt error = iObservers.FindInAddressOrder(&aObserver);
101 if (error >= KErrNone)
102 iObservers.Remove(error);
105 /////////////////////////////////////////////////////////////////////////////////////////
106 /////////////////////////////////////////////////////////////////////////////////////////
107 /////////////////////////////////////////////////////////////////////////////////////////
109 void CLogServDatabaseChangeTracker::BOHandleEventL(TLogServBackupEvent aEvent)
113 case EBackupStarting:
121 const TInt error = iNotifier.Open(iDatabase.DTIDatabase());
122 User::LeaveIfError(error);
127 __ASSERT_DEBUG(EFalse, User::Invariant());
132 /////////////////////////////////////////////////////////////////////////////////////////
133 /////////////////////////////////////////////////////////////////////////////////////////
134 /////////////////////////////////////////////////////////////////////////////////////////
136 void CLogServDatabaseChangeTracker::RunL()
138 // We are only interested if a commit has occurred
139 const TInt completionCode = iStatus.Int();
140 if (completionCode == RDbNotifier::ECommit)
142 LOGTEXT("CLogServDatabaseChangeTracker::RunL() - database commit");
144 // Notify all observers
153 void CLogServDatabaseChangeTracker::DoCancel()
158 TInt CLogServDatabaseChangeTracker::RunError(TInt /*aError*/)
160 // Ignore errors such as leaves from notifying observers
165 /////////////////////////////////////////////////////////////////////////////////////////
166 /////////////////////////////////////////////////////////////////////////////////////////
167 /////////////////////////////////////////////////////////////////////////////////////////
169 void CLogServDatabaseChangeTracker::Request()
171 iNotifier.NotifyChange(iStatus);
175 void CLogServDatabaseChangeTracker::NotifyObserversL()
177 LOGTEXT("CLogServDatabaseChangeTracker::NotifyObserversL()");
179 const TInt count = iObservers.Count();
180 for(TInt i=0; i<count; i++)
182 MLogServDatabaseChangeObserver* observer = iObservers[i];
183 observer->DCOHandleChangeEventsL(*iChanges);
186 // Reduce memory usage
189 LOGTEXT("CLogServDatabaseChangeTracker::NotifyObserversL() - end");
192 void CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL()
194 LOGTEXT("CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL()");
196 const TInt count = iGlobalChanges.Count();
197 for(TInt i=0; i<count; i++)
199 const TLogServDatabaseChangeDefinition& change = iGlobalChanges[i];
201 const TInt observerCount = iObservers.Count();
202 for(TInt j=0; j<observerCount; j++)
204 MLogServDatabaseChangeObserver* observer = iObservers[j];
205 observer->DCOHandleGlobalChangeEventL(change);
209 // Reduce memory usage
210 iGlobalChanges.Reset();
211 iGlobalChanges.GranularCompress();
213 LOGTEXT("CLogServDatabaseChangeTracker::NotifyGlobalChangeEventsL() - end");
216 TBool CLogServDatabaseChangeTracker::IdleNotifyGlobalChangeEvents(TAny* aSelf)
218 CLogServDatabaseChangeTracker* self = reinterpret_cast<CLogServDatabaseChangeTracker*>(aSelf);
219 TRAPD(err, self->NotifyGlobalChangeEventsL());