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