browserutilities/favouritesengine/ClientServer/srvsrc/FavouritesSrvSession.cpp
changeset 0 dd21522fd290
child 68 92a765b5b3e7
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 CFavouritesSession
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "FavouritesSrvDb.h"
       
    24 #include "FavouritesSrvDbNotifier.h"
       
    25 #include "FavouritesSrvDbIncremental.h"
       
    26 #include "FavouritesSrvStream.h"
       
    27 #include "FavouritesSrvFile.h"
       
    28 #include "FavouritesSrvBuf.h"
       
    29 #include "FavouritesBuf.h"
       
    30 #include "FavouritesFilter.h"
       
    31 #include "FavouritesSrvSession.h"
       
    32 #include "FavouritesSrvSessionEntry.h"
       
    33 #include "FavouritesDef.h"
       
    34 #include "FavouritesMsg.h"
       
    35 #include "FavouritesItemImplList.h"
       
    36 #include "FavouritesUtil.h"
       
    37 #include "UidMap.h"
       
    38 #include "favouriteslogger.h"
       
    39 #include <utf.h>
       
    40 // CONSTANTS
       
    41 
       
    42 /// Object list granularity.
       
    43 LOCAL_D const TInt KFavouritesSrvSessionObjectGranularity = 4;
       
    44 /// Uid list granularity.
       
    45 LOCAL_D const TInt KFavouritesSrvSessionUidListGranularity = 4;
       
    46 /// Granularity of AP map in RFS.
       
    47 LOCAL_D const TInt KApMapGranularity = 4;
       
    48 
       
    49 // Secure policy ID of the Faveng database files.
       
    50 LOCAL_D const TUint KUidFavengDbPolicy = 0x101FD685;
       
    51 
       
    52 
       
    53 // ================= MEMBER FUNCTIONS =======================
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // CFavouritesSession::NewL
       
    57 // ---------------------------------------------------------
       
    58 //
       
    59 CFavouritesSession* CFavouritesSession::NewL()
       
    60     {
       
    61     CFavouritesSession* sess = new (ELeave) CFavouritesSession();
       
    62     CleanupStack::PushL( sess );
       
    63     sess->ConstructL();
       
    64     CleanupStack::Pop();
       
    65     return sess;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------
       
    69 // CFavouritesSession::~CFavouritesSession
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 CFavouritesSession::~CFavouritesSession()
       
    73     {
       
    74     // Free all entries in reverse order:
       
    75     // Files -> Streams -> Incrementals -> Notifiers -> Databases.
       
    76     for ( TInt type = TFavouritesSessionEntry::EFile;
       
    77           type > TFavouritesSessionEntry::ENone;
       
    78           type--
       
    79         )
       
    80         {
       
    81         for ( TInt i = 0; i < iNumEntries; i++ )
       
    82             {
       
    83             if ( STATIC_CAST( TInt, iEntries[i].Type() ) == type )
       
    84                 {
       
    85                 iEntries[i].Release();
       
    86                 }
       
    87             }
       
    88         }
       
    89     delete iEntries;
       
    90     iEntries = NULL;    // Safety code.
       
    91     iNumEntries = 0;    // Safety code.
       
    92     
       
    93     // In the case of -Secure Backup is in progress- 
       
    94     // the ActiveScheduler is not active the server does not exists at this point.
       
    95     if(CActiveScheduler::Current() != NULL)
       
    96     	{
       
    97     	FLOG(( _L("Server().SessionClosed()") ));
       
    98     	Server().SessionClosed();
       
    99     	}
       
   100     else
       
   101     	{
       
   102     	FLOG(( _L("CActiveScheduler::Current() == NULL") ));
       
   103     	}
       
   104     
       
   105     iDbs.Close();
       
   106     iFs.Close();
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CFavouritesSession::CFavouritesSession
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 CFavouritesSession::CFavouritesSession()
       
   114 : iEntries( NULL ), iNumEntries( 0 ), iChkNum( 1 )
       
   115     {
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------
       
   119 // CFavouritesSession::ConstructL
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 void CFavouritesSession::ConstructL()
       
   123     {
       
   124     User::LeaveIfError( iFs.Connect() );
       
   125     User::LeaveIfError( iFs.SetSessionToPrivate( EDriveC ) );
       
   126     User::LeaveIfError( iDbs.Connect() );
       
   127 
       
   128        }
       
   129 
       
   130 // ---------------------------------------------------------
       
   131 // CFavouritesSession::CountResources
       
   132 // ---------------------------------------------------------
       
   133 //
       
   134 TInt CFavouritesSession::CountResources()
       
   135     {
       
   136     TInt cnt( 0 );
       
   137     for ( TInt i = 0; i < iNumEntries; i++ )
       
   138         {
       
   139         if ( iEntries[i].Type() != TFavouritesSessionEntry::ENone )
       
   140             {
       
   141             cnt++;
       
   142             }
       
   143         }
       
   144     return cnt;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------
       
   148 // CFavouritesSession::ServiceL
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 void CFavouritesSession::ServiceL( const RMessage2& aMessage )
       
   152     {
       
   153 	// Set received message in server so it can be used in cleanup when
       
   154     // server panics.
       
   155 	Server().ReceivedMessage( aMessage );
       
   156     TInt ret( KErrNone );
       
   157     TFavouritesMsg msg( aMessage.Function() );
       
   158     if ( msg.IsNull() )
       
   159         {
       
   160         // Null handle -> session function.
       
   161         switch ( msg.Function() )
       
   162             {
       
   163             // Session functions.
       
   164             case EFavengResourceMarkStart:
       
   165                 {
       
   166                 ResourceCountMarkStart();
       
   167                 break;
       
   168                 }
       
   169 
       
   170             case EFavengResourceMarkEnd:
       
   171                 {
       
   172                 ResourceCountMarkEnd( aMessage );
       
   173                 break;
       
   174                 }
       
   175 
       
   176             case EFavengResourceCount:
       
   177                 {
       
   178                 ret = CountResources();
       
   179                 break;
       
   180                 }
       
   181 
       
   182             case EFavengDeleteDatabase:
       
   183                 {
       
   184                 TBuf<KFavouritesMaxName> name;
       
   185                 TInt len = aMessage.GetDesLength( 0 );
       
   186                 if ( len < 1 || len > KFavouritesMaxName )
       
   187                     {
       
   188                     User::Leave( KErrBadName );
       
   189                     }
       
   190                 name.SetLength( len );
       
   191                 aMessage.ReadL( 0, name );
       
   192                 DatabaseDirL( name, iParse );
       
   193                 TRAPD( err, DeleteDirL( iParse ) );
       
   194                 // Map KErrPathNotFound error to KErrNotFound.
       
   195                 if ( err == KErrPathNotFound )
       
   196                     {
       
   197                     err = KErrNotFound;
       
   198                     }
       
   199                 User::LeaveIfError( err );
       
   200                 break;
       
   201                 }
       
   202 
       
   203             case EFavengSetHeapFailure:
       
   204                 {
       
   205                 User::__DbgSetAllocFail
       
   206                     (
       
   207                     RAllocator::EUser,
       
   208                     STATIC_CAST( RAllocator::TAllocFail, aMessage.Int0() ),
       
   209                     aMessage.Int1()
       
   210                     );
       
   211                 break;
       
   212                 }
       
   213 
       
   214             case EFavengOpenDatabase:
       
   215                 {
       
   216                 
       
   217                 // Fixed for Bug id - JJUN-78RARE (FAVENGSRV crashes under IPC attack)
       
   218                 // When client sends 8 bit descriptor input and favourite server always 
       
   219                 // will attempt  on 16 bit RMessage2 read/write. So server will leave with 
       
   220                 // KErrBadDescriptor and hence crash occurs. To avoid this crash,favourite 
       
   221                 // server will attempt to  read/write on 8bit descriptor.
       
   222                 
       
   223                 HBufC *buf = HBufC::NewLC(KFavouritesMaxName);
       
   224                 TPtr name(buf->Des());
       
   225                 TInt len = aMessage.GetDesLength( 0 );
       
   226                 if ( len < 1 || len > KFavouritesMaxName )
       
   227                     {
       
   228                     User::Leave( KErrBadName );
       
   229                     }
       
   230                 name.SetLength( len );
       
   231                 TInt err =  aMessage.Read( 0, name );
       
   232                 
       
   233                 if ( KErrBadDescriptor == err)   // Mismatch between 8 bit and 16 bit descriptors
       
   234 	            {
       
   235             	    HBufC8 *buf8 = HBufC8::NewLC(KFavouritesMaxName);
       
   236             	    TPtr8 name8(buf8->Des());
       
   237                 	name8.SetLength( len );
       
   238                 	aMessage.ReadL( 0, name8 );
       
   239                 	CnvUtfConverter::ConvertToUnicodeFromUtf8(name,name8);
       
   240                 	CleanupStack::PopAndDestroy(/*buf8*/);
       
   241                 }
       
   242                 else
       
   243                 {
       
   244                 	User::LeaveIfError(err);
       
   245                 }
       
   246                 
       
   247                 DatabaseDirL( name, iParse );
       
   248                 TFavouritesHandle h = NewHandleL();
       
   249                 Entry( h ) = *(CFavouritesSrvDb::NewL
       
   250                     ( iFs, iDbs, iParse.FullName(), name ));
       
   251                 ret = h;
       
   252                 CleanupStack::PopAndDestroy(/*buf*/);
       
   253                 break;
       
   254                 }
       
   255 
       
   256             case EFavengOpenStream:
       
   257                 {
       
   258                 ret = NewStreamL( *(HFavouritesSrvBuf::NewL()), aMessage );
       
   259                 break;
       
   260                 }
       
   261 
       
   262             default:
       
   263                 {
       
   264                 ret = KErrNotSupported;
       
   265                 break;
       
   266                 }
       
   267             }
       
   268         }
       
   269     else
       
   270         {
       
   271         // Object based functions.
       
   272         TFavouritesSessionEntry& e = EntryL( msg );
       
   273 
       
   274         switch( msg.Function() )
       
   275             {
       
   276             // Favourites Engine functions.
       
   277             case EFavengClose:
       
   278                 {
       
   279                 e.Release();
       
   280                 break;
       
   281                 };
       
   282 
       
   283             case EFavengIsDamaged:
       
   284                 {
       
   285                 ret = e.Database().IsDamagedL();
       
   286                 break;
       
   287                 }
       
   288 
       
   289             case EFavengRecover:
       
   290                 {
       
   291                 e.Database().RecoverL();
       
   292                 break;
       
   293                 }
       
   294 
       
   295             case EFavengCompact:
       
   296                 {
       
   297                 e.Database().CompactL();
       
   298                 break;
       
   299                 }
       
   300 
       
   301             case EFavengSize:
       
   302                 {
       
   303                 TPckgBuf<RDbDatabase::TSize> size;
       
   304                 size() = e.Database().Size();
       
   305                 aMessage.WriteL( 0, size );
       
   306                 break;
       
   307                 }
       
   308 
       
   309             case EFavengUpdateStats:
       
   310                 {
       
   311                 e.Database().UpdateStatsL();
       
   312                 break;
       
   313                 }
       
   314 
       
   315             case EFavengBegin:
       
   316                 {
       
   317                 if ( e.Database().InTransaction() )
       
   318                     {
       
   319                     Server().PanicClient( EFavouritesNestedTransaction );
       
   320                     }
       
   321                 else
       
   322                     {
       
   323                     e.Database().BeginL( (TBool)aMessage.Int0() );
       
   324                     }
       
   325                 break;
       
   326                 }
       
   327 
       
   328             case EFavengCommit:
       
   329                 {
       
   330                 if ( e.Database().InTransaction() )
       
   331                     {
       
   332                     e.Database().CommitL();
       
   333                     }
       
   334                 else
       
   335                     {
       
   336                     Server().PanicClient( EFavouritesNoTransaction );
       
   337                     }
       
   338                 break;
       
   339                 }
       
   340 
       
   341             case EFavengRollback:
       
   342                 {
       
   343                 if ( e.Database().InTransaction() )
       
   344                     {
       
   345                     e.Database().Rollback();
       
   346                     }
       
   347                 else
       
   348                     {
       
   349                     Server().PanicClient( EFavouritesNoTransaction );
       
   350                     }
       
   351                 break;
       
   352                 }
       
   353 
       
   354             case EFavengGet:
       
   355                 {
       
   356                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   357                 CFavouritesItemImpl* item = CFavouritesItemImpl::NewLC();
       
   358                 e.Database().GetL( aMessage.Int0(), *item );
       
   359                 item->ExternalizeL( stream );
       
   360                 stream.CommitL();
       
   361                 CleanupStack::PopAndDestroy( item );
       
   362                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   363                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   364                 break;
       
   365                 }
       
   366 
       
   367             case EFavengGetAll:
       
   368                 {
       
   369                 TPckgBuf<TFavouritesFilter> filter;
       
   370                 aMessage.ReadL( 0, filter );
       
   371                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   372                 CFavouritesItemImplList* list =
       
   373                     new (ELeave) CFavouritesItemImplList();
       
   374                 CleanupStack::PushL( list );
       
   375                 e.Database().GetAllL( *list, filter() );
       
   376                 list->ExternalizeL( stream );
       
   377                 stream.CommitL();
       
   378                 CleanupStack::PopAndDestroy( list );
       
   379                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   380                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   381                 break;
       
   382                 }
       
   383 
       
   384             case EFavengGetUids:
       
   385                 {
       
   386                 TPckgBuf<TFavouritesFilter> filter;
       
   387                 aMessage.ReadL( 0, filter );
       
   388                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   389                 CArrayFixFlat<TInt>* list = new (ELeave) CArrayFixFlat<TInt>
       
   390                     ( KFavouritesSrvSessionUidListGranularity );
       
   391                 CleanupStack::PushL( list );
       
   392                 e.Database().GetUidsL( *list, filter() );
       
   393                 FavouritesUtil::ExternalizeL( *list, stream );
       
   394                 stream.CommitL();
       
   395                 CleanupStack::PopAndDestroy( list );
       
   396                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   397                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   398                 break;
       
   399                 }
       
   400 
       
   401             case EFavengPreferredUid:
       
   402                 {
       
   403                 TPckgBuf<TInt> uid;
       
   404                 uid() = e.Database().PreferredUidL( aMessage.Int0() );
       
   405                 aMessage.WriteL( 1, uid );
       
   406                 break;
       
   407                 }
       
   408 
       
   409             case EFavengDelete:
       
   410                 {
       
   411                 e.Database().DeleteL( aMessage.Int0() );
       
   412                 break;
       
   413                 }
       
   414 
       
   415             case EFavengUpdate:
       
   416                 {
       
   417                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   418                     ( aMessage.Int0() ) ).Stream().Host();
       
   419                 RReadStream rs( &buf );
       
   420                 CFavouritesItemImpl* item = CFavouritesItemImpl::NewLC();
       
   421                 item->InternalizeL( rs );
       
   422                 e.Database().UpdateL
       
   423                     ( *item, aMessage.Int1(), aMessage.Int2() );
       
   424                 RWriteStream ws( &buf );
       
   425                 item->ExternalizeUpdateDeltaL( ws );
       
   426                 ws.CommitL();
       
   427                 CleanupStack::PopAndDestroy( item );
       
   428                 break;
       
   429                 }
       
   430 
       
   431             case EFavengAdd:
       
   432                 {
       
   433                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   434                     ( aMessage.Int0() ) ).Stream().Host();
       
   435                 RReadStream rs( &buf );
       
   436                 CFavouritesItemImpl* item = CFavouritesItemImpl::NewLC();
       
   437                 item->InternalizeL( rs );
       
   438                 e.Database().AddL( *item, aMessage.Int1() );
       
   439                 RWriteStream ws( &buf );
       
   440                 item->ExternalizeUpdateDeltaL( ws );
       
   441                 ws.CommitL();
       
   442                 CleanupStack::PopAndDestroy( item );
       
   443                 break;
       
   444                 }
       
   445 
       
   446             case EFavengSetSpecialItem:
       
   447                 {
       
   448                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   449                     ( aMessage.Int0() ) ).Stream().Host();
       
   450                 RReadStream rs( &buf );
       
   451                 CFavouritesItemImpl* item = CFavouritesItemImpl::NewLC();
       
   452                 item->InternalizeL( rs );
       
   453                 e.Database().SetSpecialItemL( *item, aMessage.Int1() );
       
   454                 RWriteStream ws( &buf );
       
   455                 item->ExternalizeUpdateDeltaL( ws );
       
   456                 ws.CommitL();
       
   457                 CleanupStack::PopAndDestroy( item );
       
   458                 break;
       
   459                 }
       
   460 
       
   461             case EFavengSetFactoryItem:
       
   462                 {
       
   463                 e.Database().SetFactoryItemL
       
   464                                         ( aMessage.Int0(), aMessage.Int1() );
       
   465                 break;
       
   466                 }
       
   467 
       
   468             case EFavengSetReadOnly:
       
   469                 {
       
   470                 e.Database().SetReadOnlyL
       
   471                                         ( aMessage.Int0(), aMessage.Int1() );
       
   472                 break;
       
   473                 }
       
   474 
       
   475             case EFavengSetModified:
       
   476                 {
       
   477                 e.Database().SetModifiedL( aMessage.Int0(),
       
   478                     TTime( MAKE_TINT64( aMessage.Int1(), aMessage.Int2() ) ) );
       
   479                 break;
       
   480                 }
       
   481 
       
   482             case EFavengSetPreferredUid:
       
   483                 {
       
   484                 e.Database().SetPreferredUidL
       
   485                                         ( aMessage.Int0(), aMessage.Int1() );
       
   486                 break;
       
   487                 }
       
   488 
       
   489             case EFavengItemExists:
       
   490                 {
       
   491                 ret = e.Database().ItemExistsL( aMessage.Int0() );
       
   492                 break;
       
   493                 }
       
   494 
       
   495             case EFavengFolderExists:
       
   496                 {
       
   497                 ret = e.Database().FolderExistsL( aMessage.Int0() );
       
   498                 break;
       
   499                 }
       
   500 
       
   501             case EFavengCount:
       
   502                 {
       
   503                 TPckgBuf<TFavouritesFilter> filter;
       
   504                 aMessage.ReadL( 0, filter );
       
   505                 ret = e.Database().CountL( filter() );
       
   506                 break;
       
   507                 }
       
   508 
       
   509             case EFavengSetData:
       
   510                 {
       
   511                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   512                     ( aMessage.Int0() ) ).Stream().Host();
       
   513                 e.Database().SetDataL( aMessage.Int1(), buf );
       
   514                 break;
       
   515                 }
       
   516 
       
   517             case EFavengGetData:
       
   518                 {
       
   519                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   520                 e.Database().GetDataL
       
   521                     ( aMessage.Int0(), *(stream.Sink()) );
       
   522                 stream.CommitL();
       
   523                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   524                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   525                 break;
       
   526                 }
       
   527 
       
   528             case EFavengSetBrowserData:
       
   529                 {
       
   530                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   531                     ( aMessage.Int0() ) ).Stream().Host();
       
   532                 e.Database().SetBrowserDataL( aMessage.Int1(), buf );
       
   533                 break;
       
   534                 }
       
   535 
       
   536             case EFavengGetBrowserData:
       
   537                 {
       
   538                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   539                 e.Database().GetBrowserDataL
       
   540                     ( aMessage.Int0(), *(stream.Sink()) );
       
   541                 stream.CommitL();
       
   542                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   543                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   544                 break;
       
   545                 }
       
   546 
       
   547             case EFavengMakeUniqueName:
       
   548                 {
       
   549                 TBuf<KFavouritesMaxName> name;
       
   550                 name.SetLength( aMessage.Int1() );
       
   551                 aMessage.ReadL( 0, name );
       
   552                 e.Database().MakeUniqueNameL( name, aMessage.Int2() );
       
   553                 aMessage.WriteL( 0, name );
       
   554                 ret = name.Length();
       
   555                 break;
       
   556                 };
       
   557 
       
   558 
       
   559             case EFavengRestoreFactorySettings:
       
   560                 {
       
   561                 TFileName refDb;
       
   562                 TInt len = aMessage.Int1();
       
   563                 if ( len > refDb.MaxLength() )
       
   564                     {
       
   565                     User::Leave( KErrBadName );
       
   566                     }
       
   567                 refDb.SetLength( len );
       
   568                 aMessage.ReadL( 0, refDb );
       
   569                 RWriteStream stream( HFavouritesSrvBuf::NewLC() );
       
   570                 CUidMap* map = new (ELeave) CUidMap( KApMapGranularity );
       
   571                 CleanupStack::PushL( map );
       
   572                 e.Database().RestoreFactorySettingsL( refDb, *map );
       
   573                 map->ExternalizeL( stream );
       
   574                 stream.CommitL();
       
   575                 CleanupStack::PopAndDestroy( map );
       
   576                 CleanupStack::Pop();    // Stream buf; will be taken by stream.
       
   577                 ret = NewStreamL( *(stream.Sink()), aMessage );
       
   578 			    FLOG(( _L("restore: case EFavengRestoreFactorySettings") ));
       
   579                 break;
       
   580                 }
       
   581 
       
   582             case EFavengSetAccessPoints:
       
   583                 {
       
   584                 MStreamBuf& buf = EntryL( TFavouritesHandle
       
   585                     ( aMessage.Int0() ) ).Stream().Host();
       
   586                 RReadStream rs( &buf );
       
   587                 CUidMap* map = new (ELeave) CUidMap( KApMapGranularity );
       
   588                 CleanupStack::PushL( map );
       
   589                 map->InternalizeL( rs );
       
   590                 e.Database().SetAccessPointsL( *map );
       
   591                 CleanupStack::PopAndDestroy( map );
       
   592                 break;
       
   593                 }
       
   594 
       
   595 			case EFavengFileOpen:
       
   596                 {
       
   597                 TFavouritesHandle h = NewHandleL();
       
   598                 e.Database().FilenameL( aMessage.Int0(), iParse );
       
   599                 CFavouritesSrvFile* file = CFavouritesSrvFile::NewL();
       
   600                 Entry( h ) = *file;
       
   601                 file->SetName(iParse);
       
   602             
       
   603                 ret = h;
       
   604                 break;
       
   605                 }
       
   606                 
       
   607             case EFavengFileOpenRead:
       
   608                 {
       
   609                 CFavouritesSrvFile& file = e.File();
       
   610                 
       
   611                 file.OpenL();
       
   612                 file.TransferToClientL( aMessage );
       
   613                 
       
   614                 break;
       
   615                 }
       
   616 
       
   617             case EFavengFileOpenWrite:
       
   618                 {
       
   619                 CFavouritesSrvFile& file = e.File();
       
   620                 
       
   621                 file.ReplaceL();
       
   622                 file.TransferToClientL( aMessage );
       
   623                 
       
   624                 break;
       
   625                 }
       
   626 
       
   627             case EFavengFileDelete:
       
   628                 {
       
   629                 TFavouritesHandle h = NewHandleL();
       
   630                 e.Database().FilenameL( aMessage.Int0(), iParse );
       
   631                 TInt err = iFs.Delete( iParse.FullName() );
       
   632                 // Map KErrPathNotFound error to KErrNotFound.
       
   633                 if ( err == KErrPathNotFound )
       
   634                     {
       
   635                     err = KErrNotFound;
       
   636                     }
       
   637                 User::LeaveIfError( err );
       
   638                 break;
       
   639                 }
       
   640 
       
   641             case EFavengIncrementalRecover:
       
   642                 {
       
   643                 TFavouritesHandle h = NewHandleL();
       
   644                 CFavouritesSrvDbIncremental* inc =
       
   645                     new (ELeave) CFavouritesSrvDbIncremental();
       
   646                 Entry( h ) = *inc;
       
   647                 TPckgBuf<TInt> step;
       
   648                 inc->RecoverL( e.Database(), step() );
       
   649                 aMessage.WriteL( 0, step );
       
   650                 ret = h;
       
   651                 break;
       
   652                 }
       
   653 
       
   654             case EFavengIncrementalCompact:
       
   655                 {
       
   656                 TFavouritesHandle h = NewHandleL();
       
   657                 CFavouritesSrvDbIncremental* inc =
       
   658                     new (ELeave) CFavouritesSrvDbIncremental();
       
   659                 Entry( h ) = *inc;
       
   660                 TPckgBuf<TInt> step;
       
   661                 inc->CompactL( e.Database(), step() );
       
   662                 aMessage.WriteL( 0, step );
       
   663                 ret = h;
       
   664                 break;
       
   665                 }
       
   666 
       
   667             case EFavengIncrementalNext:
       
   668                 {
       
   669                 TInt step =  aMessage.Int0();
       
   670                 if ( step == e.Incremental().Step() && step > 0 )
       
   671                     {
       
   672                     e.Incremental().NextL( step );
       
   673                     ret = step;
       
   674                     }
       
   675                 else
       
   676                     {
       
   677                     Server().PanicClient( EFavouritesBadStep );
       
   678                     }
       
   679                 break;
       
   680                 }
       
   681 
       
   682             case EFavengOpenNotifier:
       
   683                 {
       
   684                 TFavouritesHandle h = NewHandleL();
       
   685                 Entry( h ) = *(CFavouritesSrvDbNotifier::NewL( e.Database() ));
       
   686                 ret = h;
       
   687                 break;
       
   688                 }
       
   689 
       
   690             case EFavengNotifyAllEvents:
       
   691                 {
       
   692                 if ( e.Notifier().IsActive() )
       
   693                     {
       
   694                     Server().PanicClient( EFavouritesAlreadyOpen );
       
   695                     }
       
   696                 else
       
   697                     {
       
   698                     e.Notifier().NotifyAllEvents( aMessage );
       
   699                     return; // Notifier will complete the message.
       
   700                     }
       
   701                 break;
       
   702                 }
       
   703 
       
   704             case EFavengNotifyChange:
       
   705                 {
       
   706                 if ( e.Notifier().IsActive() )
       
   707                     {
       
   708                     Server().PanicClient( EFavouritesAlreadyOpen );
       
   709                     }
       
   710                 else
       
   711                     {
       
   712                     e.Notifier().NotifyChange( aMessage );
       
   713                     return; // Notifier will complete the message.
       
   714                     }
       
   715                 break;
       
   716                 }
       
   717 
       
   718             case EFavengNotifyCancel:
       
   719                 {
       
   720                 e.Notifier().Cancel();
       
   721                 break;
       
   722                 }
       
   723 
       
   724             case EFavengStreamRead:
       
   725                 {
       
   726                 ret = e.Stream().ReadL( aMessage );
       
   727                 break;
       
   728                 }
       
   729 
       
   730             case EFavengStreamWrite:
       
   731                 {
       
   732                 e.Stream().WriteL( aMessage );
       
   733                 break;
       
   734                 }
       
   735 
       
   736             default:
       
   737                 {
       
   738                 ret = KErrNotSupported;
       
   739                 break;
       
   740                 }
       
   741             }
       
   742 
       
   743         }
       
   744 
       
   745 	// Fixed for Bug id - JJUN-78RARE (FAVENGSRV crashes under IPC attack)    
       
   746 	if ( (EFavengFileOpenRead == msg.Function()) && msg.IsNull() )
       
   747 		{
       
   748 		User::Leave(ret);	
       
   749 		}
       
   750 		
       
   751     if ( !Server().ReceivedMessage().IsNull() &&
       
   752     	// Special cases - file handler passed -
       
   753     	// IPC message is transfered automatically. 
       
   754     	msg.Function() != EFavengFileOpenRead &&
       
   755     	msg.Function() != EFavengFileOpenWrite)
       
   756         {
       
   757         aMessage.Complete( ret );
       
   758         }
       
   759     }
       
   760 
       
   761 // ---------------------------------------------------------
       
   762 // CFavouritesSession::ServiceError
       
   763 // ---------------------------------------------------------
       
   764 //
       
   765 void CFavouritesSession::ServiceError( const RMessage2& aMessage, TInt aError )
       
   766     {
       
   767     // A bad descriptor / bad handle error implies a badly programmed client,
       
   768     // so panic it; otherwise use the default handling (report the error to
       
   769     // the client).
       
   770     if ( aError == KErrBadDescriptor || aError == KErrBadHandle )
       
   771         {
       
   772         Server().PanicClient( aError );
       
   773         }
       
   774     CSession2::ServiceError( aMessage, aError );
       
   775     }
       
   776 
       
   777 // ---------------------------------------------------------
       
   778 // CFavouritesSession::NewHandleL
       
   779 // ---------------------------------------------------------
       
   780 //
       
   781 TFavouritesHandle CFavouritesSession::NewHandleL()
       
   782     {
       
   783     TInt i;
       
   784     for ( i = 0; i < iNumEntries; i++ )
       
   785         {
       
   786         if ( iEntries[i].Type() == TFavouritesSessionEntry::ENone )
       
   787             {
       
   788             // Found free slot; assign check number and return it.
       
   789             iEntries[i].ChkNum() = NextChkNum();
       
   790             return TFavouritesHandle( i, iEntries[i].ChkNum() );
       
   791             }
       
   792         }
       
   793     // No free slot; expand entry list.
       
   794     TInt newNum = iNumEntries + KFavouritesSrvSessionObjectGranularity;
       
   795     if ( newNum > (1 << KIndexBits) )
       
   796         {
       
   797         // Index range exhausted.
       
   798         User::Leave( KErrNoMemory );
       
   799         }
       
   800     iEntries = STATIC_CAST( TFavouritesSessionEntry*, User::ReAllocL\
       
   801         ( iEntries, newNum * sizeof( TFavouritesSessionEntry ) ) );
       
   802     // Initialize new entries.
       
   803     for ( i = iNumEntries; i < newNum; i++ )
       
   804         {
       
   805         iEntries[i].SetNull();
       
   806         }
       
   807     i = iNumEntries;    // Index of first newly allocated entry.
       
   808     iNumEntries = newNum;
       
   809     iEntries[i].ChkNum() = NextChkNum();
       
   810     return TFavouritesHandle( i, iEntries[i].ChkNum() );
       
   811     }
       
   812 
       
   813 // ---------------------------------------------------------
       
   814 // CFavouritesSession::EntryL
       
   815 // ---------------------------------------------------------
       
   816 //
       
   817 TFavouritesSessionEntry& CFavouritesSession::EntryL
       
   818 ( TFavouritesHandle aHandle )
       
   819     {
       
   820     TInt i = aHandle.Index();
       
   821     if ( i > iNumEntries || aHandle.ChkNum() != iEntries[i].ChkNum() )
       
   822         {
       
   823         User::Leave( KErrBadHandle );
       
   824         }
       
   825     return iEntries[i];
       
   826     }
       
   827 
       
   828 // ---------------------------------------------------------
       
   829 // CFavouritesSession::Entry
       
   830 // ---------------------------------------------------------
       
   831 //
       
   832 TFavouritesSessionEntry& CFavouritesSession::Entry
       
   833 ( TFavouritesHandle aHandle )
       
   834     {
       
   835     TInt i = aHandle.Index();
       
   836     if ( i > iNumEntries || aHandle.ChkNum() != iEntries[i].ChkNum() )
       
   837         {
       
   838         FavouritesPanic( EFavouritesInternal );
       
   839         }
       
   840     return iEntries[i];
       
   841     }
       
   842 
       
   843 // ---------------------------------------------------------
       
   844 // CFavouritesSession::NextChkNum
       
   845 // ---------------------------------------------------------
       
   846 //
       
   847 TInt CFavouritesSession::NextChkNum()
       
   848     {
       
   849     if ( iChkNum > (1 << KChkNumBits - 1) )
       
   850         {
       
   851         iChkNum = 1;    // Start from 1 - we never get 0 handle.
       
   852         }
       
   853     return iChkNum++;
       
   854     }
       
   855 
       
   856 // ---------------------------------------------------------
       
   857 // CFavouritesSession::NewStreamL
       
   858 // ---------------------------------------------------------
       
   859 //
       
   860 TFavouritesHandle CFavouritesSession::NewStreamL
       
   861 ( MStreamBuf& aHost, const RMessage2& aMessage )
       
   862     {
       
   863     aHost.PushL();
       
   864     TInt len = Min( aHost.SizeL(), KFavouritesStreamBufSize );
       
   865     if ( len )
       
   866         {
       
   867         // We already have some data; send it.
       
   868         TPckgBuf<TFavouritesBuf> buf;
       
   869         aHost.ReadL( buf().iData, len );
       
   870         buf().iLen = len;
       
   871         aMessage.WriteL( 3, buf );
       
   872         }
       
   873     // Make a handle.
       
   874     TFavouritesHandle h = NewHandleL();
       
   875     TFavouritesSessionEntry& e = Entry( h );
       
   876     // Create the stream, owner is the entry (== the session).
       
   877     // Pass ownership to session.
       
   878     e = *(new (ELeave) HFavouritesSrvStream( aHost ));
       
   879     CleanupStack::Pop();    // aHost
       
   880     return h;
       
   881     }
       
   882 
       
   883 
       
   884 // ---------------------------------------------------------
       
   885 // CFavouritesSession::DatabaseDirL
       
   886 // ---------------------------------------------------------
       
   887 //
       
   888 void CFavouritesSession::DatabaseDirL( const TDesC& aName, TParse& aParse )
       
   889     {
       
   890     TDriveName cDrive = TDriveUnit( EDriveC ).Name();
       
   891     
       
   892     _LIT(KDbExtension, ".db");
       
   893     
       
   894     User::LeaveIfError( aParse.SetNoWild( aName, &cDrive, &KDbExtension() ) );
       
   895     }
       
   896 
       
   897     
       
   898 // ---------------------------------------------------------
       
   899 // CFavouritesSession::DeleteDirL
       
   900 // ---------------------------------------------------------
       
   901 //
       
   902 void CFavouritesSession::DeleteDirL( const TParse& aParse )
       
   903     {
       
   904     const TUid KDbPolicyUid = TUid::Uid(KUidFavengDbPolicy);
       
   905 
       
   906     TPtrC path( aParse.FullName() );
       
   907     TInt err = iDbs.DeleteDatabase( path, KDbPolicyUid );
       
   908     
       
   909     if ( err != KErrNone )
       
   910         {
       
   911         User::Leave( err );
       
   912         }
       
   913     }
       
   914 
       
   915 //  End of File