os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServBackupManager.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServBackupManager.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,350 @@
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 "LogServBackupManager.h"
1.20 +
1.21 +// User includes
1.22 +#include <logcli.h>
1.23 +#include "logservpanic.h"
1.24 +
1.25 +// Constants
1.26 +const TInt KTimerDelay = 10000000; // 10 seconds
1.27 +
1.28 +
1.29 +/////////////////////////////////////////////////////////////////////////////////////////
1.30 +// -----> CLogServBackupManager (source)
1.31 +/////////////////////////////////////////////////////////////////////////////////////////
1.32 +
1.33 +CLogServBackupManager::CLogServBackupManager(TInt aPriority)
1.34 +: CTimer(aPriority)
1.35 + {
1.36 + CActiveScheduler::Add(this);
1.37 + }
1.38 +
1.39 +CLogServBackupManager::~CLogServBackupManager()
1.40 + {
1.41 + __ASSERT_DEBUG(iObservers.Count() == 0, Panic(ELogBackupObserversStillRegistered));
1.42 + Cancel();
1.43 +
1.44 + // Unregister ourselves with backup server
1.45 + if (iBackup)
1.46 + {
1.47 + if (iDatabaseName)
1.48 + iBackup->DeregisterFile(*iDatabaseName);
1.49 + delete iBackup;
1.50 + }
1.51 + delete iDatabaseName;
1.52 + iObservers.Close();
1.53 + }
1.54 +
1.55 +void CLogServBackupManager::ConstructL()
1.56 + {
1.57 + CTimer::ConstructL();
1.58 + }
1.59 +
1.60 +CLogServBackupManager* CLogServBackupManager::NewL(TInt aPriority)
1.61 + {
1.62 + CLogServBackupManager* self = new(ELeave) CLogServBackupManager(aPriority);
1.63 + CleanupStack::PushL(self);
1.64 + self->ConstructL();
1.65 + CleanupStack::Pop(self);
1.66 + return self;
1.67 + }
1.68 +
1.69 +/////////////////////////////////////////////////////////////////////////////////////////
1.70 +/////////////////////////////////////////////////////////////////////////////////////////
1.71 +/////////////////////////////////////////////////////////////////////////////////////////
1.72 +
1.73 +void CLogServBackupManager::BIObserverAddL(MLogServBackupObserver& aObserver, TLogServBackupPriority aPriority)
1.74 + {
1.75 + LOGTEXT2("CLogServBackupManager::BIObserverAddL(aPriority=%d)", aPriority);
1.76 + TLogBackupNotificationEntry entry(aObserver, aPriority);
1.77 +
1.78 + // Create orderer which ensures that we place the objects in priority ascending order (i.e.
1.79 + // higher priority gets notified first).
1.80 + TLinearOrder<TLogBackupNotificationEntry> orderer(CompareEntries);
1.81 + const TInt error = iObservers.InsertInOrderAllowRepeats(entry, orderer);
1.82 + User::LeaveIfError(error);
1.83 +
1.84 + LOGTEXT("CLogServBackupManager::BIObserverAddL() - end");
1.85 + }
1.86 +
1.87 +void CLogServBackupManager::BIObserverRemove(MLogServBackupObserver& aObserver)
1.88 +//
1.89 +// Removes an observer from the notification queue
1.90 +//
1.91 + {
1.92 + const TInt count = iObservers.Count();
1.93 + for(TInt i=0; i<count; i++)
1.94 + {
1.95 + const TLogBackupNotificationEntry& entry = iObservers[i];
1.96 + if (&aObserver == &entry.iObserver)
1.97 + {
1.98 + iObservers.Remove(i);
1.99 + return;
1.100 + }
1.101 + }
1.102 + }
1.103 +
1.104 +MLogServBackupInterface::TLogServBackupState CLogServBackupManager::BIState() const
1.105 + {
1.106 + return iState;
1.107 + }
1.108 +
1.109 +TInt CLogServBackupManager::BIErrorValueForCurrentState() const
1.110 + {
1.111 + TInt error = KErrNone;
1.112 + switch(BIState())
1.113 + {
1.114 + case ELogServBackupStateIdle:
1.115 + error = KErrNone;
1.116 + break;
1.117 + case ELogServBackupStateBackupInProgress:
1.118 + error = KErrAccessDenied;
1.119 + break;
1.120 + }
1.121 + //
1.122 + LOGTEXT2("CLogServBackupManager::BIErrorValueForCurrentState() = %d", error);
1.123 + return error;
1.124 + }
1.125 +
1.126 +void CLogServBackupManager::BISetDatabaseNameL(const TDesC& aDatabaseName)
1.127 + {
1.128 +#ifdef LOGGING_ENABLED
1.129 + LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL(%S, isActive: %d)", &aDatabaseName, IsActive());
1.130 + if (iDatabaseName)
1.131 + {
1.132 + LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - currently registered database filename is: %S", iDatabaseName);
1.133 + }
1.134 + else
1.135 + {
1.136 + LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no file registered with backup interface yet");
1.137 + }
1.138 +#endif
1.139 +
1.140 + Cancel();
1.141 +
1.142 + HBufC* databaseName = aDatabaseName.AllocLC();
1.143 +
1.144 + // If we haven't already created a backup observer, then we need
1.145 + // to kick the object back into life again.
1.146 + if (!iBackup)
1.147 + {
1.148 + LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no backup session created");
1.149 +
1.150 + // Try and create backup interface synchronously first of all, if that fails
1.151 + // then construct as an idle operation
1.152 + TRAPD(err, iBackup = CreateBackupL(*databaseName));
1.153 +
1.154 + LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - backup session creation error: %d", err);
1.155 + if (err != KErrNone)
1.156 + After(0);
1.157 + }
1.158 + else if (iDatabaseName->Compare(aDatabaseName) != KErrNone)
1.159 + {
1.160 + LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL() - database filename changed from %S to %S", &iDatabaseName, &aDatabaseName);
1.161 +
1.162 + // De register the old, register the new
1.163 + iBackup->DeregisterFile(*iDatabaseName);
1.164 + iBackup->RegisterFileL(aDatabaseName, *this);
1.165 +
1.166 + LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - database re-registration complete");
1.167 + }
1.168 +
1.169 +
1.170 + delete iDatabaseName;
1.171 + iDatabaseName = databaseName;
1.172 + CleanupStack::Pop(databaseName);
1.173 +
1.174 + LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - end");
1.175 + }
1.176 +
1.177 +/////////////////////////////////////////////////////////////////////////////////////////
1.178 +/////////////////////////////////////////////////////////////////////////////////////////
1.179 +/////////////////////////////////////////////////////////////////////////////////////////
1.180 +
1.181 +void CLogServBackupManager::RunL()
1.182 +//
1.183 +// This method does two things
1.184 +//
1.185 +// 1) Keeps trying to create a backup object - which may fail on device
1.186 +// bootup until the ui framework starts the backup server.
1.187 +//
1.188 +// 2) Handles the case where the server fails to restart correctly after a backup - it keeps trying
1.189 +//
1.190 + {
1.191 + LOGTEXT2("CLogServBackupManager::RunL(%d)", iStatus.Int());
1.192 +
1.193 + if (!iBackup)
1.194 + {
1.195 + LOGTEXT("CLogServBackupManager::RunL() - trying to create backup object");
1.196 +
1.197 + // Keep trying to create backup object
1.198 + iBackup = CreateBackupL(*iDatabaseName);
1.199 +
1.200 + LOGTEXT("CLogServBackupManager::RunL() - backup object created okay");
1.201 + }
1.202 + else
1.203 + {
1.204 + // This branch is executed if we failed to create the backup object on our first
1.205 + // attempt in BISetDatabaseNameL
1.206 + LOGTEXT("CLogServBackupManager::RunL() - notifying observers about dummy backup ended event");
1.207 + NotifyObservers(MLogServBackupObserver::EBackupEnded);
1.208 + }
1.209 +
1.210 + LOGTEXT("CLogServBackupManager::RunL() - end");
1.211 + }
1.212 +
1.213 +TInt CLogServBackupManager::RunError(TInt aError)
1.214 + {
1.215 + (void) aError;
1.216 + LOGTEXT2("CLogServBackupManager::RunError(%d)", aError);
1.217 +
1.218 + // Make sure we don't leak anything, pretend the backup started again - won't fail
1.219 + if (iBackup)
1.220 + NotifyObservers(MLogServBackupObserver::EBackupStarting);
1.221 +
1.222 + After(KTimerDelay);
1.223 +
1.224 + LOGTEXT("CLogServBackupManager::RunError() - end");
1.225 + return KErrNone;
1.226 + }
1.227 +
1.228 +TInt CLogServBackupManager::NotifyObservers(MLogServBackupObserver::TLogServBackupEvent aEvent)
1.229 +//
1.230 +// Notify observers of the event. Assumes event queue correctly ordered.
1.231 +//
1.232 + {
1.233 + const TInt count = iObservers.Count();
1.234 + LOGTEXT3("CLogServBackupManager::NotifyObservers(aEvent = %d) - %d observers", aEvent, count);
1.235 +
1.236 + // Depending on the event type, we have to reverse the order of notification.
1.237 + // I wish it was possible to do this in a more elegant way... hmm... pointers
1.238 + // to member functions?...
1.239 +
1.240 + TInt error = KErrNone;
1.241 + TRAP(error,
1.242 + //
1.243 + switch(aEvent)
1.244 + {
1.245 + case MLogServBackupObserver::EBackupStarting:
1.246 + {
1.247 + // Update our state
1.248 + iState = ELogServBackupStateBackupInProgress;
1.249 +
1.250 + // Notify
1.251 + for(TInt i=0; i<count; i++)
1.252 + {
1.253 + TLogBackupNotificationEntry& entry = iObservers[i];
1.254 + entry.iObserver.BOHandleEventL(aEvent);
1.255 + }
1.256 + }
1.257 + break;
1.258 + case MLogServBackupObserver::EBackupEnded:
1.259 + {
1.260 + // Update our state
1.261 + iState = ELogServBackupStateIdle;
1.262 +
1.263 + // Notify
1.264 + for(TInt i=count-1; i>=0; i--)
1.265 + {
1.266 + TLogBackupNotificationEntry& entry = iObservers[i];
1.267 + entry.iObserver.BOHandleEventL(aEvent);
1.268 + }
1.269 + }
1.270 + break;
1.271 + }
1.272 + );
1.273 +
1.274 + LOGTEXT("CLogServBackupManager::NotifyObservers() - end");
1.275 + return error;
1.276 + }
1.277 +
1.278 +void CLogServBackupManager::ChangeFileLockL(const TDesC& aFileName, TFileLockFlags aFlags)
1.279 + {
1.280 + LOGTEXT3("CLogServBackupManager::ChangeFileLockL(%S, aFlags = %d)", &aFileName, aFlags);
1.281 +
1.282 +#ifdef LOGGING_ENABLED
1.283 + if (aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
1.284 + {
1.285 + LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is STARTING");
1.286 + }
1.287 + else
1.288 + {
1.289 + LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is ENDING");
1.290 + }
1.291 +#endif
1.292 +
1.293 + Cancel();
1.294 +
1.295 + // This probably won't ever happen
1.296 + if (iDatabaseName->Compare(aFileName) != 0)
1.297 + {
1.298 + LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Notification from backup server about the wrong file!");
1.299 + User::Leave(KErrNotFound);
1.300 + }
1.301 +
1.302 + // Work out type of backup event
1.303 + MLogServBackupObserver::TLogServBackupEvent event = MLogServBackupObserver::EBackupEnded;
1.304 + if (aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
1.305 + event = MLogServBackupObserver::EBackupStarting;
1.306 +
1.307 + // Notify observers
1.308 + const TInt error = NotifyObservers(event);
1.309 + LOGTEXT2("CLogServBackupManager::ChangeFileLockL() - notifying observers error: %d", error);
1.310 +
1.311 + // Shouldn't be any problems when notifying observers
1.312 + __ASSERT_DEBUG(!(event == MLogServBackupObserver::EBackupStarting && error != KErrNone), Panic(ELogStartBackupFailure));
1.313 +
1.314 + // Handle failure to restart the server after a backup
1.315 + if (error && (event == MLogServBackupObserver::EBackupEnded))
1.316 + {
1.317 + LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Kicking off server objects again after backup completed (and there was an error)");
1.318 +
1.319 + // Make sure we don't leak anything, pretend the backup started again - won't fail
1.320 + Cancel();
1.321 + NotifyObservers(MLogServBackupObserver::EBackupStarting);
1.322 + After(KTimerDelay);
1.323 + }
1.324 +
1.325 + LOGTEXT("CLogServBackupManager::ChangeFileLockL() - end");
1.326 + }
1.327 +
1.328 +CBaBackupSessionWrapper* CLogServBackupManager::CreateBackupL(const TDesC& aLogDatabaseFileName)
1.329 + {
1.330 + LOGTEXT("CLogServBackupManager::CreateBackupL()");
1.331 +
1.332 + // Create backup session
1.333 + CBaBackupSessionWrapper* backup = CBaBackupSessionWrapper::NewL();
1.334 + CleanupStack::PushL(backup);
1.335 +
1.336 + // Register with the backup server
1.337 + backup->RegisterFileL(aLogDatabaseFileName, *this);
1.338 +
1.339 + // All done
1.340 + CleanupStack::Pop(backup);
1.341 +
1.342 + LOGTEXT("CLogServBackupManager::CreateBackupL() - end");
1.343 + return backup;
1.344 + }
1.345 +
1.346 +TInt CLogServBackupManager::CompareEntries(const TLogBackupNotificationEntry& aLeft, const TLogBackupNotificationEntry& aRight)
1.347 + {
1.348 + if (aLeft.iPriority == aRight.iPriority)
1.349 + return 0;
1.350 + else if (aLeft.iPriority > aRight.iPriority) // Not what you might expect since higher priority must go first
1.351 + return -1;
1.352 + return 1;
1.353 + }