sysanadatacapture/piprofiler/piprofiler/engine/src/WriterPluginLoader.cpp
changeset 1 3ff3fecb12fe
equal deleted inserted replaced
0:f0f2b8682603 1:3ff3fecb12fe
       
     1 /*
       
     2 * Copyright (c) 2009 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 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "WriterPluginLoader.h"
       
    21 #include	<piprofiler/EngineUIDs.h>
       
    22 #include    <UTF.H> // CnvUtfConverter
       
    23 #include  	<basched.h>
       
    24 
       
    25 // constants
       
    26 const TInt KWriterCaptionSize = 256;
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CWriterPluginLoader::NewL
       
    30 //
       
    31 // EPOC two-phased constructor
       
    32 // ----------------------------------------------------------------------------
       
    33 //
       
    34 CWriterPluginLoader* CWriterPluginLoader::NewL()
       
    35     {
       
    36     CWriterPluginLoader* self = new( ELeave ) CWriterPluginLoader;
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL( );
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // CWriterPluginLoader::CWriterPluginLoader
       
    46 //
       
    47 // C++ default constructor can NOT contain any code, that
       
    48 // might leave.
       
    49 // ----------------------------------------------------------------------------
       
    50 //
       
    51 CWriterPluginLoader::CWriterPluginLoader() : CActive( EPriorityStandard )
       
    52     {
       
    53     LOGTEXT(_L("CWriterPluginLoader()::CWriterPluginLoader()" ));
       
    54     }
       
    55 
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // CWriterPluginLoader::ConstructL
       
    59 //
       
    60 // EPOC default constructor can leave.
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 void CWriterPluginLoader::ConstructL( )
       
    64     {
       
    65     LOGTEXT(_L("CWriterPluginLoader()::ConstructL()" ));
       
    66     
       
    67     // get list of implementations
       
    68     CWriterPluginInterface::ListAllImplementationsL( iImplInfoArray );
       
    69     
       
    70     CActiveScheduler::Add( this );
       
    71     }
       
    72 
       
    73 // ----------------------------------------------------------------------------
       
    74 // CWriterPluginLoader::~CWriterPluginLoader
       
    75 //
       
    76 // Destructor
       
    77 // ----------------------------------------------------------------------------
       
    78 //
       
    79 CWriterPluginLoader::~CWriterPluginLoader()
       
    80     {
       
    81     LOGTEXT(_L("CWriterPluginLoader()::~CWriterPluginLoader()") );
       
    82 
       
    83     AbortAsyncLoad();
       
    84 
       
    85     Cancel();
       
    86 
       
    87     // reset ECOM implementation info array
       
    88     iImplInfoArray.ResetAndDestroy(); // This is needed
       
    89     iImplInfoArray.Close();
       
    90     }
       
    91 
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // CWriterPluginLoader::LoadAsync
       
    95 //
       
    96 //
       
    97 // ----------------------------------------------------------------------------
       
    98 //
       
    99 void CWriterPluginLoader::LoadAsyncL( CArrayPtrFlat<CWriterPluginInterface>* aPluginArray )
       
   100     {
       
   101     iPluginArray = aPluginArray;
       
   102     
       
   103     // Reset iterator:
       
   104     iImplInfoArrayIterator = 0;
       
   105 
       
   106     LOGSTRING2( "CWriterPluginLoader()::Implementation info count: %d",
       
   107         iImplInfoArray.Count() );
       
   108 
       
   109     NotifyProgress();
       
   110 
       
   111     //Begin CActive asynchronous loop.
       
   112     CompleteOwnRequest();
       
   113     }
       
   114 
       
   115 
       
   116 // ----------------------------------------------------------------------------
       
   117 // CWriterPluginLoader::LoadSyncL
       
   118 //
       
   119 //
       
   120 // ----------------------------------------------------------------------------
       
   121 //
       
   122 CWriterPluginInterface& CWriterPluginLoader::LoadSyncL( TUid aImplementationUid )
       
   123     {
       
   124     Cancel();
       
   125     CWriterPluginInterface* plugin = NULL;
       
   126 
       
   127     // Get a list of all implementations, even though we only want one specific
       
   128     // one. There appears to be no way to otherwise extract a specific implementation
       
   129     // info object :(
       
   130     // Search for the implementation that matches aImplementationUid
       
   131     const TInt impCount = iImplInfoArray.Count();
       
   132     for( TInt i=0; i<impCount; i++ )
       
   133         {
       
   134         const CImplementationInformation* info = iImplInfoArray[ i ];
       
   135         if  ( info->ImplementationUid() == aImplementationUid )
       
   136             {
       
   137             TRAPD(ret, plugin = &CreatePluginInstanceL( *info ); );
       
   138             if( ret == KErrNone )
       
   139                 {
       
   140                 // Plugin ownership is transfered to iPluginArray
       
   141                 InsertPluginInOrderL( plugin, iPluginArray );
       
   142                 }
       
   143             else
       
   144                 {
       
   145                 // Error note is displayed even if plugin is not loaded
       
   146                 LOGSTRING3("CWriterPluginLoader::LoadSyncL() - plugin %S load failed, error %d", &info->iDisplayName(), ret);
       
   147                 }
       
   148             break;
       
   149             }
       
   150         }
       
   151 
       
   152     if  ( plugin == NULL )
       
   153         {
       
   154         User::Leave( KErrNotFound );
       
   155         }
       
   156     return *plugin;
       
   157     }
       
   158 
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CWriterPluginLoader::AbortAsyncLoad
       
   162 //
       
   163 //
       
   164 // ----------------------------------------------------------------------------
       
   165 //
       
   166 void CWriterPluginLoader::AbortAsyncLoad()
       
   167     {
       
   168     LOGTEXT(_L("CWriterPluginLoader()::AbortAsyncLoad()" ));
       
   169     Cancel();
       
   170     }
       
   171 
       
   172 
       
   173 // ----------------------------------------------------------------------------
       
   174 // CWriterPluginLoader::RunL
       
   175 //
       
   176 //
       
   177 // ----------------------------------------------------------------------------
       
   178 //
       
   179 void CWriterPluginLoader::RunL()
       
   180     {
       
   181     iRunLDebugCount++;
       
   182     LoadNextPluginL();
       
   183 
       
   184     // Check if there are still more plugins to be loaded:
       
   185     if ( iImplInfoArrayIterator < iImplInfoArray.Count() )
       
   186         {
       
   187         NotifyProgress();
       
   188         // Continue CActive asynchronous loop.
       
   189         CompleteOwnRequest();
       
   190         }
       
   191     else
       
   192         {
       
   193         // All plugins loaded:
       
   194         LOGTEXT(_L("CWriterPluginLoader()::Loading plugins finished." ));
       
   195         NotifyFinished();
       
   196         }
       
   197     }
       
   198 
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // CScGenreItemConstructionConductor::CompleteOwnRequest
       
   202 //
       
   203 // Issue request complete notification.
       
   204 // ---------------------------------------------------------------------------
       
   205 void CWriterPluginLoader::CompleteOwnRequest()
       
   206     {
       
   207     TRequestStatus* status = &iStatus;
       
   208     User::RequestComplete( status, KErrNone );
       
   209     SetActive();
       
   210     }
       
   211 
       
   212 
       
   213 // ----------------------------------------------------------------------------
       
   214 // CWriterPluginLoader::RunError
       
   215 //
       
   216 //
       
   217 // ----------------------------------------------------------------------------
       
   218 //
       
   219 TInt CWriterPluginLoader::RunError( TInt aError )
       
   220     {
       
   221     // This method is called when a plugin loading fails.
       
   222     // Always "fake" the return value so that ActiveSchedule
       
   223     // keeps running and later plugins are continued to be loaded.
       
   224     // Check if still plugins to be loaded:
       
   225     if( iImplInfoArrayIterator < iImplInfoArray.Count() )
       
   226         {
       
   227         NotifyProgress();
       
   228 
       
   229         //Continue CActive asynchronous loop.
       
   230         CompleteOwnRequest();
       
   231         }
       
   232     else // All plugins loaded:
       
   233         {
       
   234         NotifyFinished();
       
   235         }
       
   236 
       
   237     if ( aError == KLeaveExit )
       
   238         {
       
   239         return KLeaveExit;
       
   240         }
       
   241 
       
   242     return KErrNone;
       
   243     }
       
   244 
       
   245 
       
   246 // ----------------------------------------------------------------------------
       
   247 // CWriterPluginLoader::DoCancel
       
   248 //
       
   249 //
       
   250 // ----------------------------------------------------------------------------
       
   251 //
       
   252 void CWriterPluginLoader::DoCancel()
       
   253     {
       
   254 
       
   255     }
       
   256 
       
   257 
       
   258 // ----------------------------------------------------------------------------
       
   259 // CWriterPluginLoader::NotifyProgress
       
   260 //
       
   261 //
       
   262 // ----------------------------------------------------------------------------
       
   263 //
       
   264 void CWriterPluginLoader::NotifyProgress()
       
   265     {
       
   266     if( iObserver )
       
   267         {
       
   268         iObserver->HandlePluginLoaded( MWriterPluginLoadObserver::EWriterSuccess);
       
   269         }
       
   270     }
       
   271 
       
   272 
       
   273 // ----------------------------------------------------------------------------
       
   274 // CWriterPluginLoader::NotifyFinished
       
   275 //
       
   276 //
       
   277 // ----------------------------------------------------------------------------
       
   278 //
       
   279 void CWriterPluginLoader::NotifyFinished()
       
   280     {
       
   281     if( iObserver )
       
   282         {
       
   283         iObserver->HandlePluginLoaded( MWriterPluginLoadObserver::EWriterFinished );
       
   284         }
       
   285     }
       
   286 
       
   287 
       
   288 // ----------------------------------------------------------------------------
       
   289 // CWriterPluginLoader::SetObserver
       
   290 //
       
   291 //
       
   292 // ----------------------------------------------------------------------------
       
   293 //
       
   294 void CWriterPluginLoader::SetObserver(MWriterPluginLoadObserver* aObserver)
       
   295     {
       
   296     LOGSTRING2("CWriterPluginLoader()::Observer set:0x%X", aObserver);
       
   297     iObserver = aObserver;
       
   298     }
       
   299 
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 // CWriterPluginLoader::ParseToUid
       
   303 // Parses a UID from descriptor of form '0xNNNNNNNN' where N is hexadecimal.
       
   304 //
       
   305 // ----------------------------------------------------------------------------
       
   306 //
       
   307 TInt CWriterPluginLoader::ParseToUid( const TDesC8& aSource, TUid& aTarget )
       
   308     {
       
   309     // Remove "0x" from the descriptor if it exists
       
   310     _LIT8(KHexPrefix, "0x");
       
   311 
       
   312     TPtrC8 pSource( aSource );
       
   313     const TInt prefixPosition = pSource.Find( KHexPrefix );
       
   314     if  ( prefixPosition != KErrNotFound )
       
   315         {
       
   316         pSource.Set( aSource.Mid( prefixPosition + KHexPrefix().Length() ) );
       
   317         }
       
   318 
       
   319     // Parse to integer
       
   320     TLex8 lex( pSource );
       
   321     TUint integer = 0;
       
   322 
       
   323     // Parse using TRadix::EHex as radix:
       
   324     const TInt err = lex.Val( integer, EHex );
       
   325     aTarget.iUid = integer;
       
   326 
       
   327     if( err != KErrNone )
       
   328         {
       
   329         // If parsing parent UID failed, do not load plugin:
       
   330         LOGSTRING2(
       
   331             "CWriterPluginLoader()::Parsing parent UID failed. Error code:%d",
       
   332             err );
       
   333         }
       
   334     return err;
       
   335     }
       
   336 
       
   337 
       
   338 // ----------------------------------------------------------------------------
       
   339 // CWriterPluginLoader::ParseOrderNumber
       
   340 //
       
   341 //
       
   342 // ----------------------------------------------------------------------------
       
   343 //
       
   344 TInt CWriterPluginLoader::ParseOrderNumber( const TDesC8& aSource, TInt& aOrderNumber )
       
   345     {
       
   346     // Parse plugin's order number from opaque_data:
       
   347     TLex8 lex( aSource );
       
   348     const TInt orderErr = lex.Val( aOrderNumber );
       
   349     return orderErr;
       
   350     }
       
   351 
       
   352 // ----------------------------------------------------------------------------
       
   353 // CWriterPluginLoader::LoadNextPluginL
       
   354 // Iterate through iImplInfoArray. Load the plugin if it is eligible for
       
   355 // loading. Loaded plugin is added to iPluginArray. Each time a plugin is
       
   356 // loaded, iObserver is notified.
       
   357 //
       
   358 // ----------------------------------------------------------------------------
       
   359 //
       
   360 void CWriterPluginLoader::LoadNextPluginL()
       
   361     {
       
   362     // Iterate through iImplInfoArray. This loop continues between function
       
   363     // calls. Therefore member variable iImplInfoArrayIterator is used as a
       
   364     // counter. Loop will break when match is found and continues on next RunL.
       
   365     for( ; iImplInfoArrayIterator < iImplInfoArray.Count();  )
       
   366         {
       
   367         const CImplementationInformation* info =
       
   368             iImplInfoArray[ iImplInfoArrayIterator ];
       
   369 
       
   370         iImplInfoArrayIterator++;
       
   371 
       
   372         // If this plugin is OK -> load it:
       
   373         LOGSTRING2( "CWriterPluginLoader() %S eligible for parent",
       
   374                 &info->DisplayName() );
       
   375         CWriterPluginInterface* plugin = NULL;
       
   376         TInt error(KErrNone);
       
   377         // Create plugin. Trap leave for debugging purposes.
       
   378         TRAP( error, plugin = &CreatePluginInstanceL( *info ); );
       
   379         if( error == KErrNone )
       
   380             {
       
   381             // Plugin ownership is transfered to iPluginArray
       
   382             InsertPluginInOrderL( plugin, iPluginArray );
       
   383             }
       
   384         else
       
   385             {
       
   386             LOGSTRING3("CWriterPluginLoader::LoadNextPluginL() - plugin %S load failed, error %d", &info->iDisplayName(), error);
       
   387             }
       
   388         // Wait for next round
       
   389         break;
       
   390         }
       
   391     }
       
   392 
       
   393 // ----------------------------------------------------------------------------
       
   394 // CWriterPluginLoader::CreatePluginInstanceL
       
   395 //
       
   396 //
       
   397 // ----------------------------------------------------------------------------
       
   398 //
       
   399 
       
   400 CWriterPluginInterface& CWriterPluginLoader::CreatePluginInstanceL(
       
   401     const CImplementationInformation& aImpInfo )
       
   402     {
       
   403     // Now we can load the plugin
       
   404     const TUid implUid = aImpInfo.ImplementationUid();
       
   405 
       
   406     CWriterPluginInterface* plugin = CWriterPluginInterface::NewL( implUid , (TAny*)&aImpInfo.DisplayName() );
       
   407     CleanupStack::PushL ( plugin );
       
   408    
       
   409     TInt orderNumber(0);
       
   410     const TInt orderErr = ParseOrderNumber( aImpInfo.OpaqueData(), orderNumber );
       
   411     
       
   412     if  ( orderErr == KErrNone && orderNumber >= 0 )
       
   413         {
       
   414         plugin->iOrder = orderNumber;
       
   415         }
       
   416 
       
   417     CleanupStack::Pop( plugin ); // CWriterController is now responsible for this memory.
       
   418 
       
   419     return *plugin;
       
   420     }
       
   421 
       
   422 // ----------------------------------------------------------------------------
       
   423 // CWriterPluginLoader::SortPluginsL
       
   424 //
       
   425 // ----------------------------------------------------------------------------
       
   426 //
       
   427 void CWriterPluginLoader::SortPluginsL(
       
   428         CArrayPtrFlat<CWriterPluginInterface>* aPlugins )
       
   429     {
       
   430     RPointerArray<CWriterPluginInterface> plugins;
       
   431     TLinearOrder<CWriterPluginInterface> order( CWriterPluginLoader::Compare );
       
   432 
       
   433     // Insertion will also order
       
   434     for( TInt i = 0; i < aPlugins->Count(); i++ )
       
   435         {
       
   436         plugins.InsertInOrderL( (*aPlugins)[i], order );
       
   437         }
       
   438 
       
   439     // Replace original array content with sorted items
       
   440     aPlugins->Reset();
       
   441     for( TInt i = 0; i < plugins.Count(); i++ )
       
   442         {
       
   443         aPlugins->AppendL( plugins[i] );
       
   444         }
       
   445     }
       
   446 
       
   447 
       
   448 // ----------------------------------------------------------------------------
       
   449 // CWriterPluginLoader::Compare
       
   450 //
       
   451 // Compare two plugins.
       
   452 // Precedence:
       
   453 // [1. plugin provider category]
       
   454 // 2. plugin order number
       
   455 // 3. plugin caption
       
   456 // Plugin provider gategory is currently disabled (not supported yet).
       
   457 // ----------------------------------------------------------------------------
       
   458 //
       
   459 TInt CWriterPluginLoader::Compare( const CWriterPluginInterface& aFirst,
       
   460                                const CWriterPluginInterface& aSecond )
       
   461     {
       
   462     return CompareIndex( aFirst, aSecond );
       
   463     }
       
   464 
       
   465 
       
   466 // ----------------------------------------------------------------------------
       
   467 // CWriterPluginLoader::InsertPluginInOrderL
       
   468 //
       
   469 // ----------------------------------------------------------------------------
       
   470 //
       
   471 void CWriterPluginLoader::InsertPluginInOrderL(
       
   472     CWriterPluginInterface* aPlugin,
       
   473     CArrayPtrFlat<CWriterPluginInterface>* aPlugins )
       
   474     {
       
   475     CWriterPluginInterface* comparedPlugin;
       
   476     TInt comparison = 0;
       
   477     TBool inserted = EFalse;
       
   478 
       
   479     for( TInt i = 0; i < aPlugins->Count(); i++ )
       
   480         {
       
   481         comparedPlugin = (*aPlugins)[i];
       
   482         // Optimization: do not call time consuming Compare() multiple times!
       
   483         comparison = Compare( *aPlugin, *comparedPlugin );
       
   484         if( comparison < 0 )
       
   485             {
       
   486             aPlugins->InsertL( i, aPlugin );
       
   487             inserted = ETrue;
       
   488             break;
       
   489             }
       
   490         else if( comparison == 0 )
       
   491             {
       
   492             aPlugins->InsertL( i+1, aPlugin );
       
   493             inserted = ETrue;
       
   494             break;
       
   495             }
       
   496         }
       
   497     // Plugin was not before any other plugin - make sure it's appended
       
   498     if( !inserted )
       
   499         {
       
   500         aPlugins->AppendL( aPlugin );
       
   501         }
       
   502 
       
   503     #ifdef _GS_PLUGINLOADER_SORTING_TRACES
       
   504         PrintOrderTraces( aPlugins );
       
   505     #endif // _GS_PLUGINLOADER_SORTING_TRACES
       
   506 
       
   507     }
       
   508 
       
   509 // ----------------------------------------------------------------------------
       
   510 // CWriterPluginLoader::CompareCategory
       
   511 //
       
   512 // ----------------------------------------------------------------------------
       
   513 //
       
   514 void CWriterPluginLoader::PrintOrderTraces(
       
   515         CArrayPtrFlat<CWriterPluginInterface>* aPlugins )
       
   516     {
       
   517     LOGTEXT(_L("---[CWriterPluginLoader] Sorted list---") );
       
   518     HBufC* name = HBufC::New( KWriterCaptionSize );
       
   519     TRAPD(err, CleanupStack::PushL( name ));
       
   520     if(err != KErrNone)
       
   521         {
       
   522         LOGTEXT(_L("Writer plugin loader unable to sort plugins"));
       
   523         return;
       
   524         }
       
   525 
       
   526     TPtr ptr = name->Des();
       
   527     CWriterPluginInterface* plg;
       
   528 
       
   529     for( TInt i = 0; i < aPlugins->Count(); i++ )
       
   530         {
       
   531         plg = (*aPlugins)[i];
       
   532         plg->GetCaption( ptr );
       
   533 
       
   534         }
       
   535     CleanupStack::PopAndDestroy( name );
       
   536 
       
   537     }
       
   538 
       
   539 
       
   540 // ----------------------------------------------------------------------------
       
   541 // CWriterPluginLoader::CompareIndex
       
   542 //
       
   543 //
       
   544 // ----------------------------------------------------------------------------
       
   545 //
       
   546 
       
   547 TInt CWriterPluginLoader::CompareIndex( const CWriterPluginInterface& aFirst,
       
   548                                     const CWriterPluginInterface& aSecond )
       
   549     {
       
   550     TInt comparison = KWriterComparisonEqual;
       
   551     // The plugin having index is before the one not having one
       
   552 
       
   553     if( aFirst.iOrder  == KWriterPluginNotIndexed &&
       
   554         aSecond.iOrder == KWriterPluginNotIndexed )
       
   555         {
       
   556         // Neither have index -> equal
       
   557         comparison = KWriterComparisonEqual;
       
   558         }
       
   559     else if( aFirst.iOrder == KWriterPluginNotIndexed )
       
   560         {
       
   561         // The plugin having index is before the one not having one
       
   562         comparison = KWriterComparisonAfter;
       
   563         }
       
   564     else if( aSecond.iOrder == KWriterPluginNotIndexed )
       
   565         {
       
   566         // The plugin having index is before the one not having one
       
   567         comparison = KWriterComparisonBefore;
       
   568         }
       
   569     else if( aFirst.iOrder < aSecond.iOrder )
       
   570         {
       
   571         // Compare actual index values
       
   572         comparison = KWriterComparisonBefore;
       
   573         }
       
   574     else if( aFirst.iOrder > aSecond.iOrder )
       
   575         {
       
   576         // Compare actual index values
       
   577         comparison = KWriterComparisonAfter;
       
   578         }
       
   579     return comparison;
       
   580     }
       
   581 
       
   582 
       
   583 // ----------------------------------------------------------------------------
       
   584 // CWriterPluginLoader::GetDocument
       
   585 //
       
   586 //
       
   587 // ----------------------------------------------------------------------------
       
   588 //
       
   589 /*
       
   590 CWriterBaseDocument* CWriterPluginLoader::GetDocument()
       
   591     {
       
   592     CWriterBaseDocument* document = static_cast<CWriterBaseDocument*>( iAppUi->Document() );
       
   593     return document;
       
   594     }
       
   595 */
       
   596 
       
   597 //  End of File