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