os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServBackupManager.cpp
Update contrib.
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 "LogServBackupManager.h"
20 #include "logservpanic.h"
23 const TInt KTimerDelay = 10000000; // 10 seconds
26 /////////////////////////////////////////////////////////////////////////////////////////
27 // -----> CLogServBackupManager (source)
28 /////////////////////////////////////////////////////////////////////////////////////////
30 CLogServBackupManager::CLogServBackupManager(TInt aPriority)
33 CActiveScheduler::Add(this);
36 CLogServBackupManager::~CLogServBackupManager()
38 __ASSERT_DEBUG(iObservers.Count() == 0, Panic(ELogBackupObserversStillRegistered));
41 // Unregister ourselves with backup server
45 iBackup->DeregisterFile(*iDatabaseName);
52 void CLogServBackupManager::ConstructL()
57 CLogServBackupManager* CLogServBackupManager::NewL(TInt aPriority)
59 CLogServBackupManager* self = new(ELeave) CLogServBackupManager(aPriority);
60 CleanupStack::PushL(self);
62 CleanupStack::Pop(self);
66 /////////////////////////////////////////////////////////////////////////////////////////
67 /////////////////////////////////////////////////////////////////////////////////////////
68 /////////////////////////////////////////////////////////////////////////////////////////
70 void CLogServBackupManager::BIObserverAddL(MLogServBackupObserver& aObserver, TLogServBackupPriority aPriority)
72 LOGTEXT2("CLogServBackupManager::BIObserverAddL(aPriority=%d)", aPriority);
73 TLogBackupNotificationEntry entry(aObserver, aPriority);
75 // Create orderer which ensures that we place the objects in priority ascending order (i.e.
76 // higher priority gets notified first).
77 TLinearOrder<TLogBackupNotificationEntry> orderer(CompareEntries);
78 const TInt error = iObservers.InsertInOrderAllowRepeats(entry, orderer);
79 User::LeaveIfError(error);
81 LOGTEXT("CLogServBackupManager::BIObserverAddL() - end");
84 void CLogServBackupManager::BIObserverRemove(MLogServBackupObserver& aObserver)
86 // Removes an observer from the notification queue
89 const TInt count = iObservers.Count();
90 for(TInt i=0; i<count; i++)
92 const TLogBackupNotificationEntry& entry = iObservers[i];
93 if (&aObserver == &entry.iObserver)
101 MLogServBackupInterface::TLogServBackupState CLogServBackupManager::BIState() const
106 TInt CLogServBackupManager::BIErrorValueForCurrentState() const
108 TInt error = KErrNone;
111 case ELogServBackupStateIdle:
114 case ELogServBackupStateBackupInProgress:
115 error = KErrAccessDenied;
119 LOGTEXT2("CLogServBackupManager::BIErrorValueForCurrentState() = %d", error);
123 void CLogServBackupManager::BISetDatabaseNameL(const TDesC& aDatabaseName)
125 #ifdef LOGGING_ENABLED
126 LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL(%S, isActive: %d)", &aDatabaseName, IsActive());
129 LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - currently registered database filename is: %S", iDatabaseName);
133 LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no file registered with backup interface yet");
139 HBufC* databaseName = aDatabaseName.AllocLC();
141 // If we haven't already created a backup observer, then we need
142 // to kick the object back into life again.
145 LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no backup session created");
147 // Try and create backup interface synchronously first of all, if that fails
148 // then construct as an idle operation
149 TRAPD(err, iBackup = CreateBackupL(*databaseName));
151 LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - backup session creation error: %d", err);
155 else if (iDatabaseName->Compare(aDatabaseName) != KErrNone)
157 LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL() - database filename changed from %S to %S", &iDatabaseName, &aDatabaseName);
159 // De register the old, register the new
160 iBackup->DeregisterFile(*iDatabaseName);
161 iBackup->RegisterFileL(aDatabaseName, *this);
163 LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - database re-registration complete");
167 delete iDatabaseName;
168 iDatabaseName = databaseName;
169 CleanupStack::Pop(databaseName);
171 LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - end");
174 /////////////////////////////////////////////////////////////////////////////////////////
175 /////////////////////////////////////////////////////////////////////////////////////////
176 /////////////////////////////////////////////////////////////////////////////////////////
178 void CLogServBackupManager::RunL()
180 // This method does two things
182 // 1) Keeps trying to create a backup object - which may fail on device
183 // bootup until the ui framework starts the backup server.
185 // 2) Handles the case where the server fails to restart correctly after a backup - it keeps trying
188 LOGTEXT2("CLogServBackupManager::RunL(%d)", iStatus.Int());
192 LOGTEXT("CLogServBackupManager::RunL() - trying to create backup object");
194 // Keep trying to create backup object
195 iBackup = CreateBackupL(*iDatabaseName);
197 LOGTEXT("CLogServBackupManager::RunL() - backup object created okay");
201 // This branch is executed if we failed to create the backup object on our first
202 // attempt in BISetDatabaseNameL
203 LOGTEXT("CLogServBackupManager::RunL() - notifying observers about dummy backup ended event");
204 NotifyObservers(MLogServBackupObserver::EBackupEnded);
207 LOGTEXT("CLogServBackupManager::RunL() - end");
210 TInt CLogServBackupManager::RunError(TInt aError)
213 LOGTEXT2("CLogServBackupManager::RunError(%d)", aError);
215 // Make sure we don't leak anything, pretend the backup started again - won't fail
217 NotifyObservers(MLogServBackupObserver::EBackupStarting);
221 LOGTEXT("CLogServBackupManager::RunError() - end");
225 TInt CLogServBackupManager::NotifyObservers(MLogServBackupObserver::TLogServBackupEvent aEvent)
227 // Notify observers of the event. Assumes event queue correctly ordered.
230 const TInt count = iObservers.Count();
231 LOGTEXT3("CLogServBackupManager::NotifyObservers(aEvent = %d) - %d observers", aEvent, count);
233 // Depending on the event type, we have to reverse the order of notification.
234 // I wish it was possible to do this in a more elegant way... hmm... pointers
235 // to member functions?...
237 TInt error = KErrNone;
242 case MLogServBackupObserver::EBackupStarting:
245 iState = ELogServBackupStateBackupInProgress;
248 for(TInt i=0; i<count; i++)
250 TLogBackupNotificationEntry& entry = iObservers[i];
251 entry.iObserver.BOHandleEventL(aEvent);
255 case MLogServBackupObserver::EBackupEnded:
258 iState = ELogServBackupStateIdle;
261 for(TInt i=count-1; i>=0; i--)
263 TLogBackupNotificationEntry& entry = iObservers[i];
264 entry.iObserver.BOHandleEventL(aEvent);
271 LOGTEXT("CLogServBackupManager::NotifyObservers() - end");
275 void CLogServBackupManager::ChangeFileLockL(const TDesC& aFileName, TFileLockFlags aFlags)
277 LOGTEXT3("CLogServBackupManager::ChangeFileLockL(%S, aFlags = %d)", &aFileName, aFlags);
279 #ifdef LOGGING_ENABLED
280 if (aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
282 LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is STARTING");
286 LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is ENDING");
292 // This probably won't ever happen
293 if (iDatabaseName->Compare(aFileName) != 0)
295 LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Notification from backup server about the wrong file!");
296 User::Leave(KErrNotFound);
299 // Work out type of backup event
300 MLogServBackupObserver::TLogServBackupEvent event = MLogServBackupObserver::EBackupEnded;
301 if (aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
302 event = MLogServBackupObserver::EBackupStarting;
305 const TInt error = NotifyObservers(event);
306 LOGTEXT2("CLogServBackupManager::ChangeFileLockL() - notifying observers error: %d", error);
308 // Shouldn't be any problems when notifying observers
309 __ASSERT_DEBUG(!(event == MLogServBackupObserver::EBackupStarting && error != KErrNone), Panic(ELogStartBackupFailure));
311 // Handle failure to restart the server after a backup
312 if (error && (event == MLogServBackupObserver::EBackupEnded))
314 LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Kicking off server objects again after backup completed (and there was an error)");
316 // Make sure we don't leak anything, pretend the backup started again - won't fail
318 NotifyObservers(MLogServBackupObserver::EBackupStarting);
322 LOGTEXT("CLogServBackupManager::ChangeFileLockL() - end");
325 CBaBackupSessionWrapper* CLogServBackupManager::CreateBackupL(const TDesC& aLogDatabaseFileName)
327 LOGTEXT("CLogServBackupManager::CreateBackupL()");
329 // Create backup session
330 CBaBackupSessionWrapper* backup = CBaBackupSessionWrapper::NewL();
331 CleanupStack::PushL(backup);
333 // Register with the backup server
334 backup->RegisterFileL(aLogDatabaseFileName, *this);
337 CleanupStack::Pop(backup);
339 LOGTEXT("CLogServBackupManager::CreateBackupL() - end");
343 TInt CLogServBackupManager::CompareEntries(const TLogBackupNotificationEntry& aLeft, const TLogBackupNotificationEntry& aRight)
345 if (aLeft.iPriority == aRight.iPriority)
347 else if (aLeft.iPriority > aRight.iPriority) // Not what you might expect since higher priority must go first