browserutilities/favouritesengine/ClientServer/srvsrc/FavouritesSrv.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *      Implementation of class FavouritesSrv
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "FavouritesSession.h"
       
    24 #include "FavouritesSrv.h"
       
    25 #include "FavouritesSrvSession.h"
       
    26 #include "FavouritesDef.h"
       
    27 #include "FavouritesLogger.h"
       
    28 #include "Timeout.h"
       
    29 #include "FavouritesUtil.h"
       
    30 #include "BackupObserver.h"
       
    31 #include "FavouritesMsg.h"
       
    32 
       
    33 #ifdef __MYSERVER_NO_PROCESSES__
       
    34 
       
    35 // ==================== LOCAL FUNCTIONS ====================
       
    36 
       
    37 /**
       
    38 * WINS thread entry function.
       
    39 * @param aPtr Not used.
       
    40 * @return Error code.
       
    41 */
       
    42 LOCAL_C TInt ThreadEntryFunc( TAny* /*aPtr*/ )
       
    43     {
       
    44     return FavouritesSrv::Run();
       
    45     }
       
    46 
       
    47 #endif
       
    48 
       
    49 /**
       
    50 * Stop the Active Scheduler.
       
    51 * @param aPtr Not used.
       
    52 * @return KErrNone.
       
    53 */
       
    54 LOCAL_C TInt StopScheduler( TAny* /*aPtr*/ )
       
    55     {
       
    56     // Called by the exit timer, after all clients disconnected (plus a small
       
    57     // delay). Stop the scheduler, this will enable he thread exit.
       
    58     FLOG(( _L("StopScheduler") ));
       
    59     CActiveScheduler::Stop();
       
    60     return KErrNone;
       
    61     }
       
    62 
       
    63 /**
       
    64 * Create a server.
       
    65 * @param Pointer to created server (if created) returned here.
       
    66 * @return Error code.
       
    67 */
       
    68 LOCAL_C TInt CreateServer( CFavouritesSrv*& aServer )
       
    69     {
       
    70     // Server is not allowed to be created if Secure Backup is in progress
       
    71     
       
    72     TBool backupInProgress = EFalse; // This useless initialization is done 
       
    73         // because of RVCT compiler warning so do not delete it.
       
    74 
       
    75     TRAPD( err, backupInProgress = FavouritesUtil::IsBackupInProgressL());
       
    76 
       
    77     if(!err)
       
    78         {
       
    79         if(!backupInProgress)
       
    80             {
       
    81             // The TRAP is not working in the same stack frame where the
       
    82             // CTrapCleanup was created. This is why we need this function.
       
    83             TRAP( err, aServer = CFavouritesSrv::NewL() );
       
    84             }
       
    85         else
       
    86             {
       
    87             err = KErrLocked;
       
    88             }
       
    89         }
       
    90 
       
    91     return err;
       
    92     }
       
    93 
       
    94 // ================= MEMBER FUNCTIONS =======================
       
    95 
       
    96 // ---------------------------------------------------------
       
    97 // FavouritesSrv::Start
       
    98 // ---------------------------------------------------------
       
    99 //
       
   100 TInt FavouritesSrv::Start()
       
   101     {
       
   102     FLOG(( _L("FavouritesSrv::Start") ));
       
   103     TInt err( KErrNone );
       
   104 #ifdef __MYSERVER_NO_PROCESSES__
       
   105     RThread server;
       
   106     err = server.Create
       
   107         (
       
   108         KFavouritesSrvName,
       
   109         ThreadEntryFunc,
       
   110         KFavouritesSrvStackSize,
       
   111         KFavouritesSrvMinHeapSize,
       
   112         KFavouritesSrvMaxHeapSize,
       
   113         NULL,
       
   114         EOwnerProcess
       
   115         );
       
   116     FLOG(( _L("  thread created (%d)"), err ));
       
   117 #else
       
   118     RProcess server;
       
   119     err = server.Create
       
   120         (
       
   121         KFavouritesSrvExe,
       
   122         KNullDesC,
       
   123         TUidType( KNullUid, KNullUid, KFavouritesUid ),
       
   124         EOwnerThread
       
   125         );
       
   126     FLOG(( _L("  process created (%d)"), err ));
       
   127 #endif
       
   128     if ( !err )
       
   129         {
       
   130         TRequestStatus status;
       
   131         server.Rendezvous( status );
       
   132         if ( status != KRequestPending )
       
   133             {
       
   134             FLOG(( _L("FavouritesSrv::Start: abort startup") ));
       
   135             server.Kill( 0 );   // Abort startup.
       
   136             }
       
   137         else
       
   138             {
       
   139             FLOG(( _L("FavouritesSrv::Start: resume server") ));
       
   140             server.Resume();    // Logon OK - start the server.
       
   141             }
       
   142         User::WaitForRequest( status ); // Wait for start or death.
       
   143         // We can't use the 'exit reason' if the server panicked as this
       
   144         // is the panic 'reason' and may be '0' which cannot be distinguished
       
   145         // from KErrNone.
       
   146         err = (server.ExitType() == EExitPanic) ? KErrGeneral : status.Int();
       
   147         server.Close();
       
   148         }
       
   149     FLOG(( _L("FavouritesSrv::Start: returns (%d)"), err ));
       
   150     return err;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------
       
   154 // FavouritesSrv::Run
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 EXPORT_C TInt FavouritesSrv::Run()
       
   158     {
       
   159     FLOG(( _L("FavouritesSrv::Run") ));
       
   160     __UHEAP_MARK;
       
   161 
       
   162     CTrapCleanup* trapCleanup = NULL;
       
   163     CActiveScheduler* activeScheduler = NULL;
       
   164     CFavouritesSrv* server = NULL;
       
   165 
       
   166     TInt err = User::RenameThread( KFavouritesSrvName );
       
   167     if ( !err )
       
   168         {
       
   169         // Create a trap cleanup, make and install an active scheduler.
       
   170         err = KErrNoMemory;
       
   171         trapCleanup = CTrapCleanup::New();
       
   172         if ( trapCleanup )
       
   173             {
       
   174             activeScheduler = new CActiveScheduler();
       
   175             if ( activeScheduler )
       
   176                 {
       
   177                 CActiveScheduler::Install( activeScheduler );
       
   178                 err = CreateServer( server );   // Not pushed (no leaving).
       
   179                 if ( !err )
       
   180                     {
       
   181                     err = server->Start( KFavouritesSrvName );
       
   182                     FLOG(( _L("  server started: (%d)"), err ));
       
   183                     }
       
   184                 }
       
   185             }
       
   186         }
       
   187     // Let the caller know how it went.
       
   188 #ifdef __MYSERVER_NO_PROCESSES__
       
   189     RThread::Rendezvous( err );
       
   190 #else
       
   191     RProcess::Rendezvous( err );
       
   192 #endif
       
   193     if ( !err )
       
   194         {
       
   195         FLOG(( _L("FavouritesSrv::Run: starting active scheduler") ));
       
   196         CActiveScheduler::Start();
       
   197         FLOG(( _L("FavouritesSrv::Run: active scheduler stopped") ));
       
   198         }
       
   199         
       
   200     CActiveScheduler::Install( NULL );
       
   201     delete activeScheduler;
       
   202     
       
   203     FLOG(( _L("delete server") ));
       
   204     
       
   205     delete server;
       
   206     
       
   207     delete trapCleanup;
       
   208 
       
   209     __UHEAP_MARKEND;
       
   210     FLOG(( _L("FavouritesSrv::Run returns (%d)"), err ));
       
   211     return err;
       
   212     }
       
   213 
       
   214 // ================= MEMBER FUNCTIONS =======================
       
   215 
       
   216 // Platform security
       
   217 	
       
   218 // Custom check is applied to all IPCs. As IPC ids contain not only the ids
       
   219 // but other information is embadded into them.
       
   220 	
       
   221 static const int KRangeCount = 1;
       
   222 
       
   223 static const TInt SecurityRanges[KRangeCount] = 
       
   224 	{
       
   225 	EFavengNullFunction,
       
   226 	};
       
   227 	
       
   228 static const TUint8 SecurityRangesPolicy[KRangeCount] =
       
   229 	{
       
   230 	CPolicyServer::ECustomCheck
       
   231 	};
       
   232 
       
   233 static const CPolicyServer::TPolicy Policy =
       
   234 	{
       
   235 	CPolicyServer::EAlwaysPass,
       
   236 	KRangeCount,
       
   237 	SecurityRanges,
       
   238 	SecurityRangesPolicy,
       
   239 	NULL,
       
   240 	};
       
   241 
       
   242 // ---------------------------------------------------------
       
   243 // CFavouritesSrv::CustomSecurityCheckL
       
   244 // ---------------------------------------------------------
       
   245 //
       
   246 CPolicyServer::TCustomResult CFavouritesSrv::CustomSecurityCheckL(
       
   247 	const RMessage2& aMsg, TInt& /*aAction*/, TSecurityInfo& /*aMissing*/)
       
   248 	{
       
   249 		TFavouritesMsg msg( aMsg.Function() );
       
   250 		
       
   251 		TFavouritesFunction func = msg.Function();
       
   252 		
       
   253 		TCustomResult ret = EFail;
       
   254 		
       
   255 		if(func > EFavengTestCapabilityStart && 
       
   256 		   func < EFavengTestCapabilityEnd)
       
   257 			{
       
   258 			if(aMsg.HasCapability(ECapabilityAllFiles))
       
   259                	{
       
   260                	ret = EPass;
       
   261                	}
       
   262 			}
       
   263 		else if(func > EFavengReadCapabilityStart && 
       
   264 		   func < EFavengReadCapabilityEnd)
       
   265 			{
       
   266 			if(aMsg.HasCapability(ECapabilityReadUserData))
       
   267                	{
       
   268                	ret = EPass;
       
   269                	}
       
   270 			}
       
   271 		else if(func > EFavengWriteCapabilityStart && 
       
   272 		   func < EFavengWriteCapabilityEnd)
       
   273 			{
       
   274 			if(aMsg.HasCapability(ECapabilityWriteUserData))
       
   275                	{
       
   276                	ret = EPass;
       
   277                	}
       
   278 			}
       
   279         else
       
   280             {
       
   281             // Left empty by design
       
   282             }
       
   283 		
       
   284 		return ret;
       
   285 	}
       
   286 	
       
   287 // ---------------------------------------------------------
       
   288 // CFavouritesSrv::NewL
       
   289 // ---------------------------------------------------------
       
   290 //
       
   291 CFavouritesSrv* CFavouritesSrv::NewL()
       
   292     {
       
   293     CFavouritesSrv* srv = new (ELeave) CFavouritesSrv();
       
   294     CleanupStack::PushL( srv );
       
   295     srv->ConstructL();
       
   296     CleanupStack::Pop();
       
   297     return srv;
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------
       
   301 // CFavouritesSrv::~CFavouritesSrv
       
   302 // ---------------------------------------------------------
       
   303 //
       
   304 CFavouritesSrv::~CFavouritesSrv()
       
   305     {
       
   306     delete iExitTimer;
       
   307     delete iBackupObserver;
       
   308     }
       
   309 
       
   310 // ---------------------------------------------------------
       
   311 // CFavouritesSrv::SessionClosed
       
   312 // ---------------------------------------------------------
       
   313 //
       
   314 void CFavouritesSrv::SessionClosed()
       
   315     {
       
   316     FLOG(( _L("CFavouritesSrv::SessionClosed") ));
       
   317     iSessionIter.SetToFirst();
       
   318     iSessionIter++;
       
   319     if ( iSessionIter++ == 0 )
       
   320         {
       
   321         // Schedule exit when last client has closed.
       
   322         iExitTimer->Cancel();   // Safety code: cancel if running.
       
   323         FLOG(( _L("  start exit timer") ));
       
   324         iExitTimer->After
       
   325             ( TTimeIntervalMicroSeconds32( KFavouritesSrvExitDelay ) );
       
   326         }
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------
       
   330 // CFavouritesSrv::PanicClient()
       
   331 // ---------------------------------------------------------
       
   332 //
       
   333 void CFavouritesSrv::PanicClient( TInt aCode )
       
   334     {
       
   335     iReceivedMessage.Panic( KFavouritesSrvName, aCode );
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------
       
   339 // CFavouritesSrv::CFavouritesSrv
       
   340 // ---------------------------------------------------------
       
   341 //
       
   342 CFavouritesSrv::CFavouritesSrv() : 
       
   343 	CPolicyServer(CActive::EPriorityStandard, Policy, ESharableSessions)
       
   344     {
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------
       
   348 // CFavouritesSrv::ConstructL
       
   349 // ---------------------------------------------------------
       
   350 //
       
   351 void CFavouritesSrv::ConstructL()
       
   352     {
       
   353     FLOG(( _L("CFavouritesSrv::ConstructL") ));
       
   354     iExitTimer = CTimeout::NewL
       
   355         ( CActive::EPriorityStandard, TCallBack( StopScheduler, NULL ) );
       
   356     FLOG(( _L("  start exit timer") ));
       
   357     iExitTimer->After
       
   358         ( TTimeIntervalMicroSeconds32( KFavouritesSrvExitDelay ) );
       
   359 
       
   360     iBackupObserver = CBackupObserver::NewL();
       
   361     }
       
   362 
       
   363 // ---------------------------------------------------------
       
   364 // CFavouritesSrv::NewSessionL
       
   365 // ---------------------------------------------------------
       
   366 //
       
   367 CSession2* CFavouritesSrv::NewSessionL
       
   368 ( const TVersion& aVersion, const RMessage2& /*aMessage*/ ) const
       
   369     {
       
   370     FLOG(( _L("CFavouritesSrv::NewSessionL") ));
       
   371     if ( !User::QueryVersionSupported
       
   372         ( RFavouritesSession::Version(), aVersion ) )
       
   373         {
       
   374         User::Leave( KErrNotSupported );
       
   375         }
       
   376     CSession2* session = CFavouritesSession::NewL();
       
   377     iExitTimer->Cancel();   // We have a client, cancel exit (if pending).
       
   378     return session;
       
   379     }
       
   380 
       
   381 //  End of File