os/persistentdata/loggingservices/eventlogger/LogServ/src/LogServBackupManager.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "LogServBackupManager.h"
    17 
    18 // User includes
    19 #include <logcli.h>
    20 #include "logservpanic.h"
    21 
    22 // Constants
    23 const TInt KTimerDelay = 10000000; // 10 seconds
    24 
    25 
    26 /////////////////////////////////////////////////////////////////////////////////////////
    27 // -----> CLogServBackupManager (source)
    28 /////////////////////////////////////////////////////////////////////////////////////////
    29 
    30 CLogServBackupManager::CLogServBackupManager(TInt aPriority)
    31 :	CTimer(aPriority)
    32 	{
    33 	CActiveScheduler::Add(this);
    34 	}
    35 
    36 CLogServBackupManager::~CLogServBackupManager()
    37 	{
    38 	__ASSERT_DEBUG(iObservers.Count() == 0, Panic(ELogBackupObserversStillRegistered));
    39 	Cancel();
    40 
    41 	// Unregister ourselves with backup server
    42 	if (iBackup)
    43 		{
    44 		if	(iDatabaseName)
    45 			iBackup->DeregisterFile(*iDatabaseName);
    46 		delete iBackup;
    47 		}
    48 	delete iDatabaseName;
    49 	iObservers.Close();
    50 	}
    51 
    52 void CLogServBackupManager::ConstructL()
    53 	{
    54 	CTimer::ConstructL();
    55 	}
    56 
    57 CLogServBackupManager* CLogServBackupManager::NewL(TInt aPriority)
    58 	{
    59 	CLogServBackupManager* self = new(ELeave) CLogServBackupManager(aPriority);
    60 	CleanupStack::PushL(self);
    61 	self->ConstructL();
    62 	CleanupStack::Pop(self);
    63 	return self;
    64 	}
    65 
    66 /////////////////////////////////////////////////////////////////////////////////////////
    67 /////////////////////////////////////////////////////////////////////////////////////////
    68 /////////////////////////////////////////////////////////////////////////////////////////
    69 
    70 void CLogServBackupManager::BIObserverAddL(MLogServBackupObserver& aObserver, TLogServBackupPriority aPriority)
    71 	{
    72 	LOGTEXT2("CLogServBackupManager::BIObserverAddL(aPriority=%d)", aPriority);
    73 	TLogBackupNotificationEntry entry(aObserver, aPriority);
    74 	
    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);
    80 
    81 	LOGTEXT("CLogServBackupManager::BIObserverAddL() - end");
    82 	}
    83 
    84 void CLogServBackupManager::BIObserverRemove(MLogServBackupObserver& aObserver)
    85 //
    86 //	Removes an observer from the notification queue
    87 //
    88 	{
    89 	const TInt count = iObservers.Count();
    90 	for(TInt i=0; i<count; i++)
    91 		{
    92 		const TLogBackupNotificationEntry& entry = iObservers[i];
    93 		if	(&aObserver == &entry.iObserver)
    94 			{
    95 			iObservers.Remove(i);
    96 			return;
    97 			}
    98 		}
    99 	}
   100 
   101 MLogServBackupInterface::TLogServBackupState CLogServBackupManager::BIState() const
   102 	{
   103 	return iState;
   104 	}
   105 
   106 TInt CLogServBackupManager::BIErrorValueForCurrentState() const
   107 	{
   108 	TInt error = KErrNone;
   109 	switch(BIState())
   110 		{
   111 	case ELogServBackupStateIdle:
   112 		error = KErrNone;
   113 		break;
   114 	case ELogServBackupStateBackupInProgress:
   115 		error = KErrAccessDenied;
   116 		break;
   117 		}
   118 	//
   119 	LOGTEXT2("CLogServBackupManager::BIErrorValueForCurrentState() = %d", error);
   120 	return error;
   121 	}
   122 
   123 void CLogServBackupManager::BISetDatabaseNameL(const TDesC& aDatabaseName)
   124 	{
   125 #ifdef LOGGING_ENABLED
   126 	LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL(%S, isActive: %d)", &aDatabaseName, IsActive());
   127 	if	(iDatabaseName)
   128 		{
   129 		LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - currently registered database filename is: %S", iDatabaseName);
   130 		}
   131 	else
   132 		{
   133 		LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no file registered with backup interface yet");
   134 		}
   135 #endif
   136 
   137 	Cancel();
   138 
   139 	HBufC* databaseName = aDatabaseName.AllocLC();
   140 
   141 	// If we haven't already created a backup observer, then we need
   142 	// to kick the object back into life again.
   143 	if	(!iBackup)
   144 		{
   145 		LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - no backup session created");
   146 
   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));
   150 
   151 		LOGTEXT2("CLogServBackupManager::BISetDatabaseNameL() - backup session creation error: %d", err);
   152 		if	(err != KErrNone)
   153 			After(0);
   154 		}
   155 	else if (iDatabaseName->Compare(aDatabaseName) != KErrNone)
   156 		{
   157 		LOGTEXT3("CLogServBackupManager::BISetDatabaseNameL() - database filename changed from %S to %S", &iDatabaseName, &aDatabaseName);
   158 
   159 		// De register the old, register the new
   160 		iBackup->DeregisterFile(*iDatabaseName);
   161 		iBackup->RegisterFileL(aDatabaseName, *this);
   162 
   163 		LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - database re-registration complete");
   164 		}
   165 
   166 
   167 	delete iDatabaseName;
   168 	iDatabaseName = databaseName;
   169 	CleanupStack::Pop(databaseName);
   170 
   171 	LOGTEXT("CLogServBackupManager::BISetDatabaseNameL() - end");
   172 	}
   173 
   174 /////////////////////////////////////////////////////////////////////////////////////////
   175 /////////////////////////////////////////////////////////////////////////////////////////
   176 /////////////////////////////////////////////////////////////////////////////////////////
   177 
   178 void CLogServBackupManager::RunL()
   179 //
   180 // This method does two things
   181 //
   182 // 1) Keeps trying to create a backup object - which may fail on device 
   183 //    bootup until the ui framework starts the backup server.
   184 //
   185 // 2) Handles the case where the server fails to restart correctly after a backup - it keeps trying
   186 //
   187 	{
   188 	LOGTEXT2("CLogServBackupManager::RunL(%d)", iStatus.Int());
   189 
   190 	if	(!iBackup)
   191 		{
   192 		LOGTEXT("CLogServBackupManager::RunL() - trying to create backup object");
   193 
   194 		// Keep trying to create backup object
   195 		iBackup = CreateBackupL(*iDatabaseName);
   196 
   197 		LOGTEXT("CLogServBackupManager::RunL() - backup object created okay");
   198 		}
   199 	else
   200 		{
   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);
   205 		}
   206 
   207 	LOGTEXT("CLogServBackupManager::RunL() - end");
   208 	}
   209 
   210 TInt CLogServBackupManager::RunError(TInt aError)
   211 	{
   212 	(void) aError;
   213 	LOGTEXT2("CLogServBackupManager::RunError(%d)", aError);
   214 
   215 	// Make sure we don't leak anything, pretend the backup started again - won't fail
   216 	if	(iBackup)
   217 		NotifyObservers(MLogServBackupObserver::EBackupStarting);
   218 
   219 	After(KTimerDelay);
   220 
   221 	LOGTEXT("CLogServBackupManager::RunError() - end");
   222 	return KErrNone;
   223 	}
   224 
   225 TInt CLogServBackupManager::NotifyObservers(MLogServBackupObserver::TLogServBackupEvent aEvent)
   226 //
   227 //	Notify observers of the event. Assumes event queue correctly ordered.
   228 //
   229 	{
   230 	const TInt count = iObservers.Count();
   231 	LOGTEXT3("CLogServBackupManager::NotifyObservers(aEvent = %d) - %d observers", aEvent, count);
   232 
   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?...
   236 
   237 	TInt error = KErrNone;
   238 	TRAP(error, 
   239 		//
   240 		switch(aEvent)
   241 			{
   242 		case MLogServBackupObserver::EBackupStarting:
   243 			{
   244 			// Update our state
   245 			iState = ELogServBackupStateBackupInProgress;
   246 
   247 			// Notify
   248 			for(TInt i=0; i<count; i++)
   249 				{
   250 				TLogBackupNotificationEntry& entry = iObservers[i];
   251 				entry.iObserver.BOHandleEventL(aEvent);
   252 				}
   253 			}
   254 			break;
   255 		case MLogServBackupObserver::EBackupEnded:
   256 			{
   257 			// Update our state
   258 			iState = ELogServBackupStateIdle;
   259 
   260 			// Notify
   261 			for(TInt i=count-1; i>=0; i--)
   262 				{
   263 				TLogBackupNotificationEntry& entry = iObservers[i];
   264 				entry.iObserver.BOHandleEventL(aEvent);
   265 				}
   266 			}
   267 			break;
   268 			}
   269 		);
   270 
   271 	LOGTEXT("CLogServBackupManager::NotifyObservers() - end");
   272 	return error;
   273 	}
   274 
   275 void CLogServBackupManager::ChangeFileLockL(const TDesC& aFileName, TFileLockFlags aFlags)
   276 	{
   277 	LOGTEXT3("CLogServBackupManager::ChangeFileLockL(%S, aFlags = %d)", &aFileName, aFlags);
   278 
   279 #ifdef LOGGING_ENABLED
   280 	if	(aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
   281 		{
   282 		LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is STARTING");
   283 		}
   284 	else
   285 		{
   286 		LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Backup is ENDING");
   287 		}
   288 #endif
   289 
   290 	Cancel();
   291 
   292 	// This probably won't ever happen
   293 	if	(iDatabaseName->Compare(aFileName) != 0)
   294 		{
   295 		LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Notification from backup server about the wrong file!");
   296 		User::Leave(KErrNotFound);
   297 		}
   298 
   299 	// Work out type of backup event
   300 	MLogServBackupObserver::TLogServBackupEvent event = MLogServBackupObserver::EBackupEnded;
   301 	if	(aFlags & (MBackupObserver::EReleaseLockReadOnly | MBackupObserver::EReleaseLockNoAccess))
   302 		event = MLogServBackupObserver::EBackupStarting;
   303 
   304 	// Notify observers
   305 	const TInt error = NotifyObservers(event);
   306 	LOGTEXT2("CLogServBackupManager::ChangeFileLockL() - notifying observers error: %d", error);
   307 
   308 	// Shouldn't be any problems when notifying observers
   309 	__ASSERT_DEBUG(!(event == MLogServBackupObserver::EBackupStarting && error != KErrNone), Panic(ELogStartBackupFailure));
   310 
   311 	// Handle failure to restart the server after a backup
   312 	if	(error && (event == MLogServBackupObserver::EBackupEnded))
   313 		{
   314 		LOGTEXT("CLogServBackupManager::ChangeFileLockL() - Kicking off server objects again after backup completed (and there was an error)");
   315 
   316 		// Make sure we don't leak anything, pretend the backup started again - won't fail
   317 		Cancel();
   318 		NotifyObservers(MLogServBackupObserver::EBackupStarting);
   319 		After(KTimerDelay);
   320 		}
   321 
   322 	LOGTEXT("CLogServBackupManager::ChangeFileLockL() - end");
   323 	}
   324 
   325 CBaBackupSessionWrapper* CLogServBackupManager::CreateBackupL(const TDesC& aLogDatabaseFileName)
   326 	{
   327 	LOGTEXT("CLogServBackupManager::CreateBackupL()");
   328 
   329 	// Create backup session
   330 	CBaBackupSessionWrapper* backup = CBaBackupSessionWrapper::NewL();
   331 	CleanupStack::PushL(backup);
   332 
   333 	// Register with the backup server
   334 	backup->RegisterFileL(aLogDatabaseFileName, *this);
   335 
   336 	// All done
   337 	CleanupStack::Pop(backup);
   338 
   339 	LOGTEXT("CLogServBackupManager::CreateBackupL() - end");
   340 	return backup;
   341 	}
   342 
   343 TInt CLogServBackupManager::CompareEntries(const TLogBackupNotificationEntry& aLeft, const TLogBackupNotificationEntry& aRight)
   344 	{
   345 	if	(aLeft.iPriority == aRight.iPriority)
   346 		return 0;
   347 	else if (aLeft.iPriority > aRight.iPriority) // Not what you might expect since higher priority must go first
   348 		return -1;
   349 	return 1;
   350 	}