commands/backup/backup_child.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // backup_child.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <e32property.h>
       
    14 #include <connect/sbdefs.h>
       
    15 #include <baBackup.h>
       
    16 #include "backup.h"
       
    17 
       
    18 _LIT(KBackupOp,		"-b");
       
    19 _LIT(KRestoreOp,	"-r");
       
    20 
       
    21 const TInt KIdleTime = 5000000; // microseconds
       
    22 
       
    23 using namespace conn;
       
    24 
       
    25 class CBackupAO : public CActive
       
    26 	{
       
    27 public:
       
    28 	static CBackupAO* NewL();
       
    29 	~CBackupAO();
       
    30 	void Start();
       
    31 private:
       
    32 	CBackupAO();
       
    33 	void ConstructL();
       
    34 	void CheckForRestoreFlagL();
       
    35 
       
    36 	// from CActive
       
    37 	void DoCancel();
       
    38 	void RunL();
       
    39 private:
       
    40 	CBaBackupSessionWrapper* iBackupSession;
       
    41 	RSemaphore iSem;
       
    42 	TBool iRestoreOp;
       
    43 	};
       
    44 
       
    45 
       
    46 CBackupAO* CBackupAO::NewL()
       
    47 	{
       
    48 	CBackupAO* self = new (ELeave) CBackupAO();
       
    49 	CleanupStack::PushL(self);
       
    50 	self->ConstructL();
       
    51 	CleanupStack::Pop(self);
       
    52 	return self;
       
    53 	}
       
    54 
       
    55 CBackupAO::CBackupAO():
       
    56 CActive(CActive::EPriorityStandard)
       
    57 	{
       
    58 	CActiveScheduler::Add(this);
       
    59 	}
       
    60 
       
    61 CBackupAO::~CBackupAO()
       
    62 	{
       
    63 	Cancel();
       
    64 	delete iBackupSession; // will also issue the ETakeLock to system apps
       
    65 	if (iSem.Handle() > 0)
       
    66 		iSem.Close();
       
    67 	}
       
    68 
       
    69 void CBackupAO::ConstructL()
       
    70 	{
       
    71 	iBackupSession = CBaBackupSessionWrapper::NewL();
       
    72 	User::LeaveIfError(iSem.CreateGlobal(KBackupSemaphore, 0, EOwnerProcess));
       
    73 	CheckForRestoreFlagL();
       
    74 	}
       
    75 
       
    76 void CBackupAO::Start()
       
    77 	{
       
    78 	iBackupSession->CloseAll(MBackupObserver::EReleaseLockNoAccess, iStatus);
       
    79 	SetActive();
       
    80 	}
       
    81 
       
    82 void CBackupAO::RunL()
       
    83 	{
       
    84 	TInt error = iStatus.Int();
       
    85 
       
    86 	// check which instruction we must send
       
    87 	conn::TBURPartType instruction = conn::EBURBackupFull;
       
    88 	if (iRestoreOp)
       
    89 		instruction = conn::EBURRestoreFull;
       
    90 	
       
    91 	// inform subscribed parties of our intentions
       
    92 	RProperty bk;
       
    93 	if (error == KErrNone)
       
    94 		error = bk.Set(KUidSystemCategory, KUidBackupRestoreKey, instruction);
       
    95 	
       
    96 	// block for a little while to give apps a chance to process the backup instruction
       
    97 	User::After(KIdleTime);
       
    98 	
       
    99 	// inform fshell's backup cmd we've put the system into backup-mode (or not!)
       
   100 	RProcess().Rendezvous(error);
       
   101 	if (error == KErrNone)
       
   102 		{
       
   103 		iSem.Wait(); // wait for signal from fshell's backup cmd that we're ok to terminate
       
   104 		bk.Set(KUidSystemCategory, KUidBackupRestoreKey, conn::EBURNormal); // ignore the error in this case. what else can I do?
       
   105 		}
       
   106 
       
   107 	// the process has done it's thing
       
   108 	CActiveScheduler::Stop();
       
   109 	}
       
   110 
       
   111 void CBackupAO::DoCancel()
       
   112 	{
       
   113 	ASSERT(iBackupSession);
       
   114 	iBackupSession->RestartAll();
       
   115 	}
       
   116 
       
   117 //
       
   118 // CBackupAO::CheckForRestoreFlagL
       
   119 // determine whether we're issuing a backup or a restore
       
   120 //
       
   121 void CBackupAO::CheckForRestoreFlagL()
       
   122 	{
       
   123 	HBufC *buffer = HBufC::NewL(User::CommandLineLength());
       
   124 	CleanupStack::PushL(buffer);
       
   125 	TPtr cmdline = buffer->Des();
       
   126 	User::CommandLine(cmdline);
       
   127 	const TInt bPos = cmdline.Find(KBackupOp);
       
   128 	const TInt rPos = cmdline.Find(KRestoreOp);
       
   129 	CleanupStack::PopAndDestroy(1);
       
   130 	if (bPos != KErrNotFound)
       
   131 		{
       
   132 		if (rPos != KErrNotFound)
       
   133 			User::Leave(KErrArgument);
       
   134 		iRestoreOp = EFalse;
       
   135 		return;
       
   136 		}
       
   137 	if (rPos != KErrNotFound)
       
   138 		{
       
   139 		iRestoreOp = ETrue;
       
   140 		return;
       
   141 		}
       
   142 	User::Leave(KErrArgument);
       
   143 	}
       
   144 
       
   145 
       
   146 static void RunL()
       
   147 	{
       
   148 	// Active Scheduler
       
   149 	CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
   150 	CleanupStack::PushL(s);
       
   151 	CActiveScheduler::Install(s);
       
   152 
       
   153 	CBackupAO* woop = CBackupAO::NewL();
       
   154 	CleanupStack::PushL(woop);
       
   155 	woop->Start();
       
   156 
       
   157 	// start the active scheduler
       
   158 	CActiveScheduler::Start();
       
   159 	CleanupStack::PopAndDestroy(2, s);
       
   160 	}
       
   161 
       
   162 // program entry-point
       
   163 TInt E32Main()
       
   164 	{
       
   165 	__UHEAP_MARK;
       
   166 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   167 	TInt err = KErrNoMemory;
       
   168 	if (cleanup)
       
   169 		{
       
   170 		TRAP(err, RunL());
       
   171 		delete cleanup;
       
   172 		}
       
   173 	__UHEAP_MARKEND;
       
   174 	return err;
       
   175 	}