uigraphics/AknIcon/srvsrc/AknIconSrvSession.cpp
changeset 0 05e9090e2422
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2002 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 "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:   Session to AknIconServer.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <e32std.h>
       
    23 
       
    24 #include "AknIconSrvSession.h"
       
    25 #include "AknIconSrv.h"
       
    26 #include "AknIconPanic.h"
       
    27 #include "AknIconSrvIconItem.h"
       
    28 #include "AknIconTraces.h"
       
    29 
       
    30 
       
    31 // CONSTANTS
       
    32 
       
    33 const TInt KIconItemArrayGranularity = 4;
       
    34 const TInt KPreservedItemArrayGranularity = 1;
       
    35 
       
    36 // ================= MEMBER FUNCTIONS ==========================================
       
    37 
       
    38 CAknIconSrvSession::CAknIconSrvSession() :
       
    39     CSession2(),
       
    40     iIconItems( KIconItemArrayGranularity ),
       
    41     iPreservedItems( KPreservedItemArrayGranularity ),
       
    42     iClientPanicCode(EBadServerRequest)
       
    43     {
       
    44     }
       
    45 
       
    46 CAknIconSrvSession::~CAknIconSrvSession()
       
    47     {
       
    48     // Cleanup all icons and preserved icon data items referred by the session
       
    49     // that were not freed normally, for example due to a client thread panic.
       
    50 
       
    51     TInt count = iIconItems.Count();
       
    52     
       
    53     for ( TInt i = 0 ; i < count ; i++ )
       
    54         {
       
    55         Server()->CleanupSessionIconItem( iIconItems[i] );
       
    56         }
       
    57 
       
    58     count = iPreservedItems.Count();
       
    59     
       
    60     for ( TInt j = 0 ; j < count ; j++ )
       
    61         {
       
    62         Server()->CleanupSessionPreservedItem( iPreservedItems[j] );
       
    63         }
       
    64 
       
    65     iIconItems.Close();
       
    66     iPreservedItems.Close();
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CAknIconSrvSession::DispatchMessageL()
       
    71 // -----------------------------------------------------------------------------
       
    72 
       
    73 TInt CAknIconSrvSession::DispatchMessageL( const RMessage2& aMessage )
       
    74     {
       
    75 // Debug heap testing.
       
    76 #ifdef _DEBUG
       
    77     if ( iDbgHeapFailNextCount )
       
    78         {
       
    79         __UHEAP_FAILNEXT( iDbgHeapFailNextCount );
       
    80         iDbgHeapFailNextCount++;
       
    81         }
       
    82 #endif // _DEBUG
       
    83 
       
    84     TInt ret = KErrNone;
       
    85 
       
    86     switch ( aMessage.Function() )
       
    87         {
       
    88         case ERetrieveOrCreateSharedIcon:
       
    89             {
       
    90             // Append and remove a dummy element to prevent memory allocation
       
    91             // failure when the real item is appended.
       
    92             TAknIconSrvSessionIconItem dummy;
       
    93             User::LeaveIfError( iIconItems.Append( dummy ) );
       
    94             iIconItems.Remove( iIconItems.Count() - 1 );
       
    95 
       
    96             const CAknIconSrvIconItem* item = 
       
    97                 Server()->RetrieveOrCreateSharedIconL();
       
    98 
       
    99             TInt users = ( item->iMaskId < 0 ? 1 : 2 );
       
   100 
       
   101             // Append in here cannot fail now because of the operation done above.
       
   102             ModifyUserCount( item, users ); // bitmap and optionally mask
       
   103             break;
       
   104             }
       
   105 
       
   106         case EFreeBitmap:
       
   107             {
       
   108             const CAknIconSrvIconItem* item = Server()->FreeBitmapL();
       
   109             ModifyUserCount( item, -1 );
       
   110             break;
       
   111             }
       
   112 
       
   113         case EGetContentDimensions:
       
   114             {
       
   115             Server()->GetContentDimensionsL();
       
   116             break;
       
   117             }
       
   118 
       
   119         case EPreserveIconData:
       
   120             {
       
   121             // Append and remove a dummy element to prevent memory allocation
       
   122             // failure when the real item is appended.
       
   123             TAknIconSrvSessionPreservedItem dummy;
       
   124             User::LeaveIfError( iPreservedItems.Append( dummy ) );
       
   125             iPreservedItems.Remove( iPreservedItems.Count() - 1 );
       
   126 
       
   127             const CAknIconDataItem* item = Server()->PreserveIconDataL();
       
   128             ModifyUserCount( item, 1 );
       
   129             break;
       
   130             }
       
   131 
       
   132         case EDestroyIconData:
       
   133             {
       
   134             const CAknIconDataItem* item = Server()->UnpreserveIconDataL();
       
   135             ModifyUserCount( item, -1 );
       
   136             break;
       
   137             }
       
   138 
       
   139         case EGetInitData:
       
   140             {
       
   141             TAknIconInitData data;
       
   142             Server()->GetInitData( data );
       
   143             TPckgC<TAknIconInitData> retPack( data );
       
   144             aMessage.WriteL( 0, retPack );
       
   145             break;
       
   146             }
       
   147         
       
   148         case EAknIconServRequestEnableCache:
       
   149         	{
       
   150         	if (aMessage.HasCapability(ECapabilityWriteDeviceData))
       
   151 	        	Server()->EnableCache( aMessage.Int0() );
       
   152         	else
       
   153         		ret = KErrPermissionDenied;
       
   154         	break;
       
   155         	}
       
   156 
       
   157 // Debug heap testing.
       
   158 #ifdef _DEBUG
       
   159         case EServerHeapMark:
       
   160             {
       
   161             __UHEAP_MARK;
       
   162             break;
       
   163             }
       
   164 
       
   165         case EServerHeapMarkEnd:
       
   166             {
       
   167             __UHEAP_MARKEND;
       
   168             break;
       
   169             }
       
   170 
       
   171         case EServerHeapFailNext:
       
   172             {
       
   173             iDbgHeapFailNextCount = 0;
       
   174             __UHEAP_FAILNEXT( aMessage.Int0() );
       
   175             break;
       
   176             }
       
   177 
       
   178         case EServerHeapFailNextIncreasing:
       
   179             {
       
   180             iDbgHeapFailNextCount = aMessage.Int0();
       
   181             break;
       
   182             }
       
   183 
       
   184         case EServerHeapReset:
       
   185             {
       
   186             iDbgHeapFailNextCount = 0;
       
   187             __UHEAP_RESET;
       
   188             break;
       
   189             }
       
   190         
       
   191         case EServerResetDynamicallyChangingAllocations:
       
   192             {
       
   193             Server()->ResetDynamicallyChangingAllocations();
       
   194             break;
       
   195             }
       
   196 
       
   197         case EServerHeapUsed:
       
   198             {
       
   199             TInt heapUsed;
       
   200             User::AllocSize( heapUsed );
       
   201             ret = heapUsed;
       
   202             break;
       
   203             }
       
   204 
       
   205         case EServerSetPreferredIconDisplayMode:
       
   206             {
       
   207             Server()->SetPreferredIconDisplayMode( (TDisplayMode)aMessage.Int0() );
       
   208             break;
       
   209             }
       
   210 
       
   211 #endif // _DEBUG
       
   212 
       
   213         default:
       
   214             {
       
   215       //      PanicClient( aMessage, EBadServerRequest );
       
   216 			// If we dont call Leave here, Server crashes.. 
       
   217 			// Fixed it as a part of error fix JJUN-78QCL6
       
   218 			User::Leave(KAknIconSrvCodePanicClient);
       
   219             break;
       
   220             }
       
   221         }
       
   222 
       
   223     return ret;
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CAknIconSrvSession::ServiceL()
       
   228 // -----------------------------------------------------------------------------
       
   229 
       
   230 void CAknIconSrvSession::ServiceL(const RMessage2& aMessage)
       
   231     {
       
   232     TInt ret = KErrNone;
       
   233 
       
   234     TInt opCode = aMessage.Function();
       
   235     
       
   236     // File handles are transfered to server side with these opCodes.
       
   237     if ( opCode == ERetrieveOrCreateSharedIcon ||
       
   238          opCode == EGetContentDimensions ||
       
   239          opCode == EPreserveIconData )
       
   240         {
       
   241         if ( aMessage.Int2() )
       
   242             {
       
   243             User::LeaveIfError(
       
   244                 iAdoptedFile.AdoptFromClient( aMessage, 2, 3 ) );
       
   245             }
       
   246         }
       
   247 
       
   248     TRAPD( err,
       
   249         {
       
   250         ret = DispatchMessageL( aMessage );
       
   251         } );
       
   252 
       
   253     iAdoptedFile.Close();
       
   254 
       
   255 #ifdef __AKNICON_TRACES
       
   256     if (err != KErrNone)
       
   257         {                
       
   258         RDebug::Print( _L("CAknIconSrvSession: %x DispatchMessageL leaved: %d"), this, err );
       
   259         }
       
   260 #endif
       
   261 
       
   262     // Don't complete message if client panicked
       
   263     if (err != KAknIconSrvCodePanicClient)
       
   264         {
       
   265         // If the service routine leaved, return the leave reason.
       
   266         // Otherwise, return its return code.
       
   267 
       
   268         aMessage.Complete( err != KErrNone ? err : ret );
       
   269         }
       
   270     else   
       
   271 	    {
       
   272 	    if (aMessage.IsNull() == EFalse)
       
   273 	        {
       
   274 	        PanicClient( aMessage, iClientPanicCode );
       
   275 	        }
       
   276 		  User::Leave(KAknIconSrvCodePanicClient);	    	
       
   277 	    }
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CAknIconSrvSession::PanicClient()
       
   282 // -----------------------------------------------------------------------------
       
   283 
       
   284 void CAknIconSrvSession::PanicClient( const RMessage2& aMessage, TAknIconPanic aPanic ) const
       
   285     {
       
   286     aMessage.Panic( KAknIconPanicCategory, aPanic );
       
   287     }
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CAknIconSrvSession::ModifyUserCount()
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 void CAknIconSrvSession::ModifyUserCount( 
       
   294     const CAknIconSrvIconItem* aItem,
       
   295     TInt aCount )
       
   296     {    
       
   297     TAknIconSrvSessionIconItem newItem = { aItem, aCount };
       
   298     TInt index = iIconItems.FindInOrder( 
       
   299         newItem, TAknIconSrvSessionIconItem::LinearOrder );
       
   300         
       
   301     if ( index >= 0 )
       
   302         {
       
   303         TAknIconSrvSessionIconItem& sessionItem = iIconItems[index];
       
   304         // session's user count for this icon
       
   305         sessionItem.iUserCount += aCount;
       
   306 
       
   307         if ( sessionItem.iUserCount == 0 )
       
   308             {
       
   309             iIconItems.Remove( index );
       
   310             }
       
   311         return;
       
   312         }
       
   313 
       
   314     // If not found, it must be a new item,
       
   315     // and then the user count should be increasing.
       
   316     __ASSERT_ALWAYS( aCount > 0, User::Invariant() );
       
   317 
       
   318     // Cannot fail at this stage, see caller function.
       
   319     iIconItems.InsertInOrder(newItem, TAknIconSrvSessionIconItem::LinearOrder );    
       
   320     }
       
   321     
       
   322 // -----------------------------------------------------------------------------
       
   323 // CAknIconSrvSession::ModifyUserCount()
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 void CAknIconSrvSession::ModifyUserCount( 
       
   327     const CAknIconDataItem* aItem,
       
   328     TInt aCount )
       
   329     {
       
   330     TInt count = iPreservedItems.Count();
       
   331 
       
   332     for ( TInt i = 0 ; i < count ; i++ )
       
   333         {
       
   334         TAknIconSrvSessionPreservedItem& sessionItem = iPreservedItems[i];
       
   335         if ( sessionItem.iDataItem == aItem )
       
   336             {
       
   337             // session's user count for this icon
       
   338             sessionItem.iUserCount += aCount;
       
   339 
       
   340             if ( sessionItem.iUserCount == 0 )
       
   341                 {
       
   342                 iPreservedItems.Remove( i );
       
   343                 }
       
   344             return;
       
   345             }
       
   346         }
       
   347 
       
   348     // If not found, it must be a new item,
       
   349     // and then the user count should be increasing.
       
   350     __ASSERT_ALWAYS( aCount > 0, User::Invariant() );
       
   351 
       
   352     TAknIconSrvSessionPreservedItem newItem = { aItem, aCount };
       
   353     // Cannot fail at this stage, see caller function.
       
   354     iPreservedItems.Append( newItem );
       
   355     }
       
   356     
       
   357 // -----------------------------------------------------------------------------
       
   358 // CAknIconSrvSession::AdoptedFileHandle()
       
   359 // -----------------------------------------------------------------------------
       
   360 //    
       
   361 RFile& CAknIconSrvSession::AdoptedFileHandle()
       
   362     {
       
   363     return iAdoptedFile;
       
   364     }
       
   365 
       
   366 
       
   367 //  End of File