gssettingsuis/Gs/GSFramework/src/GSPluginAndViewIdCache.cpp
branchGCC_SURGE
changeset 45 f48d04161a92
parent 27 572294aa6075
parent 43 3341fe7c643a
equal deleted inserted replaced
27:572294aa6075 45:f48d04161a92
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  
       
    15 *           Plugin and view id caching support
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "GSPluginAndViewIdCache.h"
       
    22 
       
    23 // System includes
       
    24 #include <bautils.h>
       
    25 #include <s32file.h>
       
    26 #include <eikappui.h>
       
    27 
       
    28 // User includes
       
    29 #include "GSShimmedView.h"
       
    30 #include "GSPlaceholderView.h"
       
    31 
       
    32 // Constants
       
    33 _LIT( KGSCacheFileName, "PluginUidToViewIdCache.dat" );
       
    34 
       
    35 
       
    36 // ================= MEMBER FUNCTIONS =======================
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CGSPluginAndViewIdCache::CGSPluginAndViewIdCache
       
    40 // C++ constructor.
       
    41 //
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CGSPluginAndViewIdCache::CGSPluginAndViewIdCache( CEikAppUi& aAppUi )
       
    45 :   iAppUi( aAppUi ), iLookupTable( &HashFunction, &HashIdentityRelation )
       
    46     {
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CGSPluginAndViewIdCache::~CGSPluginAndViewIdCache
       
    51 // C++ destructor.
       
    52 //
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 CGSPluginAndViewIdCache::~CGSPluginAndViewIdCache()
       
    56     {
       
    57     TRAP_IGNORE( StoreL() );
       
    58     //
       
    59     delete iPlaceholderView;
       
    60     DestroyShims();
       
    61     iLookupTable.Close();
       
    62     //
       
    63     iFsSession.Close();
       
    64     }
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CGSPluginAndViewIdCache::ConstructL
       
    69 // 
       
    70 //
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 void CGSPluginAndViewIdCache::ConstructL()
       
    74     {
       
    75     User::LeaveIfError( iFsSession.Connect() );
       
    76     User::LeaveIfError( iFsSession.CreatePrivatePath( EDriveC ) );
       
    77     //
       
    78     iPlaceholderView = CGSPlaceholderView::NewL( iAppUi );
       
    79     //
       
    80     TRAP_IGNORE( RestoreL() );
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CGSPluginAndViewIdCache::NewL
       
    86 // 
       
    87 //
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CGSPluginAndViewIdCache* CGSPluginAndViewIdCache::NewL( CEikAppUi& aAppUi )
       
    91     {
       
    92     CGSPluginAndViewIdCache* self = new(ELeave) CGSPluginAndViewIdCache( aAppUi );
       
    93     CleanupStack::PushL( self );
       
    94     self->ConstructL();
       
    95     CleanupStack::Pop( self );
       
    96     return self;
       
    97     }
       
    98 
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // CGSPluginAndViewIdCache::ActivatePlaceholderViewL
       
   102 // 
       
   103 //
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 void CGSPluginAndViewIdCache::ActivatePlaceholderViewL()
       
   107     {
       
   108     iPlaceholderView->ActivateL();
       
   109     }
       
   110 
       
   111 
       
   112 // ----------------------------------------------------------------------------
       
   113 // CGSPluginAndViewIdCache::PriorToPlaceholderActiveViewId
       
   114 //
       
   115 // 
       
   116 // ----------------------------------------------------------------------------
       
   117 //
       
   118 const TVwsViewId& CGSPluginAndViewIdCache::PriorToPlaceholderActiveViewId() const
       
   119     {
       
   120     return iPlaceholderView->PreviouslyActiveViewId();
       
   121     }
       
   122 
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // CGSPluginAndViewIdCache::SetPriorToPlaceholderActiveViewId
       
   126 // 
       
   127 //
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CGSPluginAndViewIdCache::SetPriorToPlaceholderActiveViewId( const TVwsViewId& aViewId )
       
   131     {
       
   132     iPlaceholderView->SetPreviouslyActiveViewId( aViewId );
       
   133     }
       
   134 
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CGSPluginAndViewIdCache::RegisterViewAndImplementationAssociationL
       
   138 // 
       
   139 //
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void CGSPluginAndViewIdCache::RegisterViewAndImplementationAssociationL( TUid aViewId, TUid aImplementationUid )
       
   143     {
       
   144     RegisterViewAndImplementationAssociationL( aViewId, aImplementationUid, EFalse );
       
   145     }
       
   146 
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // CGSPluginAndViewIdCache::PrepareToRegisterPluginInstanceLC
       
   150 // 
       
   151 //
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 void CGSPluginAndViewIdCache::PrepareToRegisterPluginInstanceLC( TUid aViewId )
       
   155     {
       
   156     TCacheEntry* value = iLookupTable.Find( aViewId );
       
   157     if  ( value && value->iShim )
       
   158         {
       
   159         // Store cleanup view uid so that we know which shim to re-register
       
   160         // if the cleanup item is invoked.
       
   161         iTransientCleanupViewUid = aViewId;
       
   162 
       
   163         // Prepare item
       
   164         TCleanupItem item( ReRegisterShimWithUi, this );
       
   165         CleanupStack::PushL( item );
       
   166 
       
   167         // Unregister shim view
       
   168         value->iShim->DeregisterView();
       
   169 
       
   170         // If a leave now occurs, the cleanup stack will unwind and will
       
   171         // invoke our cleanup item. This item will then re-add
       
   172         // the specified shim to the view framework. See ReRegisterShimWithUiL
       
   173         // for further details.
       
   174         }
       
   175     else
       
   176         {
       
   177         // Record not found - just push NULL to ensure cleanup stack
       
   178         // remains balanced.
       
   179         CleanupStack::PushL( (CBase*) NULL );
       
   180         }
       
   181     }
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // CGSPluginAndViewIdCache::RegisterPluginInstanceL
       
   186 // 
       
   187 //
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CGSPluginAndViewIdCache::RegisterPluginInstanceL( TUid aViewId, CGSPluginInterface& aPluginInstance )
       
   191     {
       
   192     TCacheEntry* value = iLookupTable.Find( aViewId );
       
   193     ASSERT( value != NULL );
       
   194     
       
   195     // Can now associate the plugin instance with the cache entry.
       
   196     value->iLoadedPluginImplementation = &aPluginInstance;
       
   197     }
       
   198 
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CGSPluginAndViewIdCache::PluginImplementationUidForView
       
   202 // 
       
   203 //
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 TBool CGSPluginAndViewIdCache::IsPluginLoaded( TUid aViewId ) const
       
   207     {
       
   208     CGSPluginInterface* instance = PluginInstance( aViewId );
       
   209 
       
   210     TBool loaded = EFalse;
       
   211     if ( instance )
       
   212         {
       
   213         loaded = ETrue;
       
   214         }
       
   215     
       
   216     return loaded;
       
   217     }
       
   218 
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CGSPluginAndViewIdCache::PluginInstance
       
   222 // 
       
   223 //
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 CGSPluginInterface* CGSPluginAndViewIdCache::PluginInstance( TUid aViewId ) const
       
   227     {
       
   228     CGSPluginInterface* ret = NULL;
       
   229     //
       
   230     const TCacheEntry* value = iLookupTable.Find( aViewId );
       
   231     if  ( value )
       
   232         {
       
   233         ret = value->iLoadedPluginImplementation;
       
   234         }
       
   235     //
       
   236     return ret;
       
   237     }
       
   238 
       
   239 
       
   240 // ---------------------------------------------------------------------------
       
   241 // CGSPluginAndViewIdCache::PluginImplementationUidForView
       
   242 // 
       
   243 //
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TUid CGSPluginAndViewIdCache::PluginImplementationUidForView( TUid aViewId ) const
       
   247     {
       
   248     TUid pluginUid = KNullUid;
       
   249     //
       
   250     const TCacheEntry* value = iLookupTable.Find( aViewId );
       
   251     if  ( value )
       
   252         {
       
   253         pluginUid = value->iImplementationUid;
       
   254         }
       
   255     //
       
   256     return pluginUid;
       
   257     }
       
   258 
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // CGSPluginAndViewIdCache::DestroyShims
       
   262 // 
       
   263 //
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CGSPluginAndViewIdCache::DestroyShims()
       
   267     {
       
   268 	RHashMap< TUid, TCacheEntry >::TIter iterator( iLookupTable );
       
   269     FOREVER
       
   270 		{
       
   271 		const TUid* key = iterator.NextKey();
       
   272 		if  ( key )
       
   273             {
       
   274             TCacheEntry* value = iterator.CurrentValue();
       
   275 
       
   276             // Destroy shim instance
       
   277             delete value->iShim;
       
   278             value->iShim = NULL;
       
   279             }
       
   280         else
       
   281             {
       
   282 			break;
       
   283             }
       
   284 		}
       
   285     }
       
   286 
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // CGSPluginAndViewIdCache::RestoreL
       
   290 // 
       
   291 //
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void CGSPluginAndViewIdCache::RestoreL()
       
   295     {
       
   296     TFileName* cacheFile = new(ELeave) TFileName();
       
   297     CleanupStack::PushL( cacheFile );
       
   298     GetCacheFileNameL( *cacheFile );
       
   299     //
       
   300     if  ( BaflUtils::FileExists( iFsSession, *cacheFile ) )
       
   301         {
       
   302         RFileReadStream cacheStream;
       
   303         TInt error = cacheStream.Open( iFsSession, *cacheFile, EFileRead );
       
   304         User::LeaveIfError( error );
       
   305         CleanupClosePushL( cacheStream );
       
   306 
       
   307         // Now read in cache hash table
       
   308         TUid key = KNullUid;
       
   309         TUid value = KNullUid;
       
   310         
       
   311         const TInt entryCount = cacheStream.ReadInt32L();
       
   312         for( TInt i=0; i<entryCount; i++ )
       
   313             {
       
   314             key.iUid = cacheStream.ReadUint32L();
       
   315             value.iUid = cacheStream.ReadUint32L();
       
   316             //
       
   317             RegisterViewAndImplementationAssociationL( key, value, ETrue );
       
   318             }
       
   319 
       
   320         CleanupStack::PopAndDestroy( &cacheStream );
       
   321         }
       
   322     //
       
   323     CleanupStack::PopAndDestroy( cacheFile );
       
   324     }
       
   325 
       
   326 
       
   327 // ---------------------------------------------------------------------------
       
   328 // CGSPluginAndViewIdCache::StoreL
       
   329 // 
       
   330 //
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CGSPluginAndViewIdCache::StoreL()
       
   334     {
       
   335     TFileName* intermediateFileName = new(ELeave) TFileName();
       
   336     CleanupStack::PushL( intermediateFileName );
       
   337 
       
   338     TFileName* cachePath = new(ELeave) TFileName();
       
   339     CleanupStack::PushL( cachePath );
       
   340     GetCachePathL( *cachePath );
       
   341     
       
   342     // Create a temporary file in the GS private data cage to contain
       
   343     // the cache data. We'll rename this to the real cache file name
       
   344     // once the operation has been completed successfully.
       
   345     RFile intermediateCacheFile;
       
   346     TInt error = intermediateCacheFile.Temp( iFsSession, *cachePath, *intermediateFileName, EFileWrite );
       
   347     User::LeaveIfError( error );
       
   348   
       
   349     // Create a stream from the file. Stream takes ownership of file
       
   350     RFileWriteStream stream( intermediateCacheFile );
       
   351     CleanupClosePushL( stream );
       
   352 
       
   353     // Write each entry to the stream
       
   354     stream.WriteUint32L( iLookupTable.Count() );
       
   355 
       
   356 	RHashMap< TUid, TCacheEntry >::TIter iterator( iLookupTable );
       
   357     FOREVER
       
   358 		{
       
   359 		const TUid* key = iterator.NextKey();
       
   360 		if  ( key )
       
   361             {
       
   362             const TCacheEntry* value = iterator.CurrentValue();
       
   363             stream.WriteUint32L( key->iUid );
       
   364             stream.WriteUint32L( value->iImplementationUid.iUid );
       
   365             }
       
   366         else
       
   367             {
       
   368 			break;
       
   369             }
       
   370 		}
       
   371 	
       
   372     // Finalise the stream
       
   373     stream.CommitL();
       
   374     stream.Close(); // Also closes the file
       
   375     CleanupStack::PopAndDestroy( &stream ); 
       
   376 
       
   377     // If we got this far it means we created the cache file successfully.
       
   378     // Now its okay to replace any pre-existing cache file with the
       
   379     // file we just created. We're re-use the cachePath variable for the
       
   380     // ultimate filename since it saves allocating another 1/2kb on the heap.
       
   381     GetCacheFileNameL( *cachePath );
       
   382     if  ( BaflUtils::FileExists( iFsSession, *cachePath ) )
       
   383         {
       
   384         error = iFsSession.Delete( *cachePath );
       
   385         User::LeaveIfError( error );
       
   386         }
       
   387 
       
   388     // Now rename intermediate file name to ultimate cache file name.
       
   389     error = iFsSession.Rename( *intermediateFileName, *cachePath );
       
   390     User::LeaveIfError( error );
       
   391 
       
   392     CleanupStack::PopAndDestroy( 2, intermediateFileName );
       
   393     }
       
   394 
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CGSPluginAndViewIdCache::GetCacheFileNameL
       
   398 // 
       
   399 //
       
   400 // ---------------------------------------------------------------------------
       
   401 //
       
   402 void CGSPluginAndViewIdCache::GetCacheFileNameL( TDes& aFileName )
       
   403     {
       
   404     GetCachePathL( aFileName );
       
   405     aFileName.Append( KGSCacheFileName );
       
   406     }
       
   407 
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // CGSPluginAndViewIdCache::GetCachePathL
       
   411 // 
       
   412 //
       
   413 // ---------------------------------------------------------------------------
       
   414 //
       
   415 void CGSPluginAndViewIdCache::GetCachePathL( TDes& aPath )
       
   416     {
       
   417     aPath.Zero();
       
   418 
       
   419     // Get private data cage path
       
   420     TInt err = iFsSession.PrivatePath( aPath );
       
   421     User::LeaveIfError( err );
       
   422 
       
   423     // Combine with C: drive
       
   424     const TDriveUnit cDrive( EDriveC );
       
   425     const TDriveName cDriveName( cDrive.Name() );
       
   426     aPath.Insert( 0, cDriveName );
       
   427     }
       
   428 
       
   429 
       
   430 // ---------------------------------------------------------------------------
       
   431 // CGSPluginAndViewIdCache::RegisterViewAndImplementationAssociationL
       
   432 // 
       
   433 //
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CGSPluginAndViewIdCache::RegisterViewAndImplementationAssociationL( TUid aViewId, TUid aImplementationUid, TBool aCreateShim )
       
   437     {
       
   438     // Check if aViewId is a key within the look up table, so as to avoid
       
   439     // adding duplicate entries...
       
   440     const TCacheEntry* value = iLookupTable.Find( aViewId );
       
   441     if  ( !value )
       
   442         {
       
   443         // No registration for this view yet, so create a new entry
       
   444         TCacheEntry cacheEntry;
       
   445         cacheEntry.iImplementationUid = aImplementationUid;
       
   446         cacheEntry.iLoadedPluginImplementation = NULL;
       
   447 
       
   448         if  ( aCreateShim )
       
   449             {
       
   450             // Create a new shim
       
   451             cacheEntry.iShim = CGSShimmedView::NewLC( aViewId, *this, iAppUi );
       
   452             }
       
   453         else
       
   454             {
       
   455             cacheEntry.iShim = NULL;
       
   456             }
       
   457 
       
   458         // Add entry to table - the shim ownership is also transferred 
       
   459         // at this point, since the destructor of this class will
       
   460         // destroy all shims.
       
   461         iLookupTable.InsertL( aViewId, cacheEntry );
       
   462         
       
   463         if  ( aCreateShim )
       
   464             {
       
   465             CleanupStack::Pop( cacheEntry.iShim );
       
   466             }
       
   467         }
       
   468     }
       
   469 
       
   470 
       
   471 // ---------------------------------------------------------------------------
       
   472 // CGSPluginAndViewIdCache::PrepareForUIDestruction
       
   473 // 
       
   474 //
       
   475 // ---------------------------------------------------------------------------
       
   476 //
       
   477 void CGSPluginAndViewIdCache::PrepareForUIDestruction()
       
   478     {
       
   479     // Have to remove all UI furniture before the AppUi rug is pulled out
       
   480     // from underneath us...
       
   481     DestroyShims();
       
   482     //
       
   483     delete iPlaceholderView;
       
   484     iPlaceholderView = NULL;
       
   485     }
       
   486 
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // CGSPluginAndViewIdCache::HandleShimDestruction
       
   490 // 
       
   491 //
       
   492 // ---------------------------------------------------------------------------
       
   493 //
       
   494 void CGSPluginAndViewIdCache::HandleShimDestruction( TUid aViewId )
       
   495     {
       
   496     TCacheEntry* value = iLookupTable.Find( aViewId );
       
   497     if  ( value && value->iShim )
       
   498         {
       
   499         // The shim has been destroyed.
       
   500         value->iShim = NULL;
       
   501         }
       
   502     }
       
   503 
       
   504 
       
   505 // ---------------------------------------------------------------------------
       
   506 // CGSPluginAndViewIdCache::ReRegisterShimWithUi
       
   507 // 
       
   508 //
       
   509 // ---------------------------------------------------------------------------
       
   510 //
       
   511 void CGSPluginAndViewIdCache::ReRegisterShimWithUi( TAny* aSelf )
       
   512     {
       
   513     CGSPluginAndViewIdCache* self = reinterpret_cast< CGSPluginAndViewIdCache* >( aSelf );
       
   514     //
       
   515     if  ( self->iTransientCleanupViewUid != KNullUid )
       
   516         {
       
   517         TCacheEntry* value = self->iLookupTable.Find( self->iTransientCleanupViewUid );
       
   518         if  ( value && value->iShim )
       
   519             {
       
   520             TRAP_IGNORE( value->iShim->RegisterViewL() );
       
   521             }
       
   522         }
       
   523     //
       
   524     self->iTransientCleanupViewUid = KNullUid;
       
   525     }
       
   526 
       
   527 
       
   528 // ---------------------------------------------------------------------------
       
   529 // CGSPluginAndViewIdCache::HashFunction
       
   530 // 
       
   531 //
       
   532 // ---------------------------------------------------------------------------
       
   533 //
       
   534 TUint32 CGSPluginAndViewIdCache::HashFunction( const TUid& aViewId )
       
   535     {
       
   536     const TUint32 hash = DefaultHash::Integer( aViewId.iUid );
       
   537     return hash;
       
   538     }
       
   539 
       
   540 
       
   541 // ---------------------------------------------------------------------------
       
   542 // CGSPluginAndViewIdCache::HashIdentityRelation
       
   543 // 
       
   544 //
       
   545 // ---------------------------------------------------------------------------
       
   546 //
       
   547 TBool CGSPluginAndViewIdCache::HashIdentityRelation( const TUid& aLeft, const TUid& aRight )
       
   548     {
       
   549     const TBool equal = ( aLeft.iUid == aRight.iUid );
       
   550     return equal;
       
   551     }
       
   552