PECengine/PluginServer2/SrvSrc/CPEngPlgSrv.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Implementation of class CPEngPlgSrv
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  Include Files
       
    20 #include "CPEngPlgSrv.h"
       
    21 #include "CPEngPlgSess.h"
       
    22 #include "PresenceDebugPrint.h"
       
    23 #include "TPEngServerParams.h"
       
    24 #include "CPEngPluginHolder.h"
       
    25 #include "CPEngPluginInterface2.h"
       
    26 #include <e32svr.h>
       
    27 #include <s32file.h>
       
    28 #include <sysutil.h>
       
    29 #include <ecom/ecom.h>
       
    30 
       
    31 // simple wrapper class with array deletion on close
       
    32 class RImplInfoPtrArray2 : public RImplInfoPtrArray
       
    33     {
       
    34     public:
       
    35         void Close()
       
    36             {
       
    37             ResetAndDestroy();
       
    38             }
       
    39     };
       
    40 
       
    41 // Static data for Capability check configuration
       
    42 // Two ranges: one for allowed, second for disallowed requests
       
    43 static const TInt KPEngPluginServerRangeCount = 2;
       
    44 
       
    45 /**
       
    46  * Ranges for the Request values
       
    47  * All requests will fall in one range
       
    48  */
       
    49 static const TInt PEngPluginServerRanges[KPEngPluginServerRangeCount] =
       
    50     {
       
    51     // Range from EPEngPlgShutdownServer to EPEngPlgServerLastRequest
       
    52     0,
       
    53     // range is from EPEngPlgServerLastRequest request to end
       
    54     EPEngPlgServerLastRequest
       
    55     };
       
    56 
       
    57 /**
       
    58  * Element indexes for the defined ranges
       
    59  * we have only one range and for it is defined only one Element
       
    60  */
       
    61 static const TUint8 KPEngPluginServerElementsIndex[KPEngPluginServerRangeCount] =
       
    62     {
       
    63     // First element in the element array will be applied for this range
       
    64     0,
       
    65     // the second element will be used for the second range
       
    66     CPolicyServer::ENotSupported
       
    67     };
       
    68 
       
    69 // Policy elements
       
    70 static const CPolicyServer::TPolicyElement KPEngPluginServerElements[] =
       
    71     {
       
    72         {
       
    73         _INIT_SECURITY_POLICY_C3( ECapabilityReadUserData,
       
    74         ECapabilityWriteUserData,
       
    75         ECapabilityNetworkServices ),
       
    76         CPolicyServer::EFailClient
       
    77         }
       
    78     };
       
    79 
       
    80 // and the Policy array is the following
       
    81 static const CPolicyServer::TPolicy KPEngPluginServerPolicy =
       
    82     {
       
    83     // The index into Elements,that is used to check a connection attempt
       
    84     0,
       
    85     // Number of ranges in the iRanges array
       
    86     KPEngPluginServerRangeCount,
       
    87     // A pointer to an array of ordered ranges of request numbers
       
    88     PEngPluginServerRanges,
       
    89     // A pointer to an array of TUint8 values specifying
       
    90     // the appropriate action to take for each range in iRanges
       
    91     KPEngPluginServerElementsIndex,
       
    92     // A pointer to an array of distinct policy elements
       
    93     KPEngPluginServerElements
       
    94     };
       
    95 
       
    96 // CONSTRUCTION
       
    97 // Static constructor, leaves pointer to cleanup-stack
       
    98 CPEngPlgSrv* CPEngPlgSrv::NewLC( const TDesC& aServerName, TInt aPriority )
       
    99     {
       
   100     CPEngPlgSrv* self = new( ELeave ) CPEngPlgSrv( aPriority );
       
   101     CleanupStack::PushL( self );
       
   102     self->ConstructL( aServerName );
       
   103     return self;
       
   104     }
       
   105 
       
   106 // Destructor (virtual by CBase)
       
   107 CPEngPlgSrv::~CPEngPlgSrv()
       
   108     {
       
   109     iPlugins.ResetAndDestroy();
       
   110     REComSession::FinalClose();
       
   111     }
       
   112 
       
   113 // Default constructor, protected
       
   114 CPEngPlgSrv::CPEngPlgSrv( TInt aPriority )
       
   115         : CPolicyServer( aPriority, KPEngPluginServerPolicy ),
       
   116         iOnlineState( EPEngPlgSvrStateUnknown )
       
   117     {
       
   118     }
       
   119 
       
   120 // Second phase construct
       
   121 void CPEngPlgSrv::ConstructL( const TDesC& aServerName )
       
   122     {
       
   123     iServerName.Copy( aServerName );
       
   124     StartL( aServerName );
       
   125     }
       
   126 
       
   127 
       
   128 TInt CPEngPlgSrv::ExecuteServerL( TPEngServerParams& aParams )
       
   129     {
       
   130     __UHEAP_MARK;
       
   131 
       
   132     //Renaming must be done as early as possible
       
   133     aParams.RenameMainThread( KPEngPluginServerExe );
       
   134 
       
   135     TInt res( KErrNone );
       
   136 
       
   137     // start scheduler and server
       
   138     CActiveScheduler* pA = new( ELeave )CActiveScheduler;
       
   139     CleanupStack::PushL( pA );
       
   140     CActiveScheduler::Install( pA );
       
   141 
       
   142     //If exe server, call RunServerL directly.
       
   143     TRAP( res, RunServerL( aParams ) );
       
   144 
       
   145     CActiveScheduler::Install( NULL );
       
   146     CleanupStack::PopAndDestroy();//pA
       
   147 
       
   148     __UHEAP_MARKEND;
       
   149     return res;
       
   150     }
       
   151 
       
   152 void CPEngPlgSrv::StopServer()
       
   153     {
       
   154     CActiveScheduler::Stop(); // CSI: 4 #
       
   155     }
       
   156 
       
   157 
       
   158 void CPEngPlgSrv::RunServerL( TPEngServerParams& aParams )
       
   159     {
       
   160     PENG_DP( D_PENG_LIT( "CPEngPlgSrv::RunServerL()" ) );
       
   161 
       
   162     //One instance of server must be allocated here.
       
   163     CPEngPlgSrv* server = CPEngPlgSrv::NewLC( aParams.ServerName(),
       
   164                                               CActive::EPriorityStandard );
       
   165 
       
   166     aParams.Signal();
       
   167     // start fielding requests from clients
       
   168     //Thread is ended when CActiveScheduler::Stop is called.
       
   169     server->StartServer();
       
   170 
       
   171     CleanupStack::PopAndDestroy();//server
       
   172     // finished
       
   173     }
       
   174 
       
   175 void CPEngPlgSrv::LoadByTypeL( const TDesC8& aPluginType )
       
   176     {
       
   177     PENG_DP( D_PENG_LIT( "CPEngPlgSrv::LoadByTypeL" ) );
       
   178 
       
   179     RImplInfoPtrArray2 plugins;
       
   180     CleanupClosePushL( plugins );
       
   181 
       
   182     TEComResolverParams params;
       
   183     params.SetDataType( aPluginType );
       
   184     params.SetWildcardMatch( EFalse );
       
   185     REComSession::ListImplementationsL( KPEngPluginInterfaceUid, params, plugins );
       
   186 
       
   187     const TInt count = plugins.Count();
       
   188 
       
   189     for ( TInt i( 0 ); i < count; i++ )
       
   190         {
       
   191         TUid uid = plugins[i]->ImplementationUid();
       
   192 
       
   193         CPEngPluginHolder* plug = CPEngPluginHolder::NewLC();
       
   194         plug->LoadPluginL( uid );
       
   195         iPlugins.AppendL( plug );
       
   196 
       
   197         CleanupStack::Pop();//plug
       
   198         }
       
   199 
       
   200     CleanupStack::PopAndDestroy();  // plugins
       
   201 
       
   202     PENG_DP( D_PENG_LIT( "CPEngPlgSrv::LoadByTypeL ends" ) );
       
   203     }
       
   204 
       
   205 void CPEngPlgSrv::UnloadOnlinePluginsL()
       
   206     {
       
   207     PENG_DP( D_PENG_LIT( "CPEngPlgSrv::UnloadOnlinePluginsL" ) );
       
   208     RImplInfoPtrArray2 plugins;
       
   209     CleanupClosePushL( plugins );
       
   210 
       
   211     TEComResolverParams params;
       
   212     params.SetDataType( KPEngOnlineType );
       
   213     params.SetWildcardMatch( EFalse );
       
   214     REComSession::ListImplementationsL( KPEngPluginInterfaceUid, params, plugins );
       
   215 
       
   216     const TInt implCount = plugins.Count();
       
   217     for ( TInt ii( implCount - 1 ); ii >= 0; ii-- )
       
   218         {
       
   219         const TInt loadedCount = iPlugins.Count();
       
   220         for ( TInt jj( loadedCount - 1 ); jj >= 0; jj-- )
       
   221             {
       
   222             if ( plugins[ii]->ImplementationUid() == iPlugins[jj]->Uid() )
       
   223                 {
       
   224                 delete iPlugins[ jj ];
       
   225                 iPlugins.Remove( jj );
       
   226                 }
       
   227             }
       
   228         }
       
   229 
       
   230     CleanupStack::PopAndDestroy();  // plugins
       
   231 
       
   232     PENG_DP( D_PENG_LIT( "CPEngPlgSrv::UnloadOnlinePluginsL ends" ) );
       
   233     }
       
   234 
       
   235 void CPEngPlgSrv::UnloadAllL()
       
   236     {
       
   237     iPlugins.ResetAndDestroy();
       
   238     }
       
   239 
       
   240 void CPEngPlgSrv::SetStateL( TPEngPlgOnlineState aNewState )
       
   241     {
       
   242     if ( iOnlineState < EPEngPlgSvrStateOnline &&
       
   243          aNewState == EPEngPlgSvrStateOnline )
       
   244         {
       
   245         // we went online, load online plugins
       
   246         LoadByTypeL( KPEngOnlineType );
       
   247         }
       
   248     if ( aNewState == EPEngPlgSvrStateOffline &&
       
   249          iOnlineState == EPEngPlgSvrStateOnline )
       
   250         {
       
   251         // we went offline, unload and load offline plugins
       
   252         UnloadOnlinePluginsL();
       
   253         }
       
   254     if ( aNewState == EPEngPlgSvrStateOffline &&
       
   255          iOnlineState == EPEngPlgSvrStateUnknown )
       
   256         {
       
   257         // the server was first started and we are offline
       
   258         LoadByTypeL( KPEngOfflineType );
       
   259         }
       
   260     if ( aNewState == EPEngPlgSvrStateUnknown )
       
   261         {
       
   262         // this shouldn't happen in normal situations
       
   263         UnloadAllL();
       
   264         }
       
   265 
       
   266     iOnlineState = aNewState;
       
   267     }
       
   268 
       
   269 TInt CPEngPlgSrv::PluginCount() const
       
   270     {
       
   271     return iPlugins.Count();
       
   272     }
       
   273 
       
   274 TInt CPEngPlgSrv::Plugin( TInt aIndex ) const
       
   275     {
       
   276     return iPlugins[aIndex]->Uid().iUid;
       
   277     }
       
   278 
       
   279 void CPEngPlgSrv::StartServer()
       
   280     {
       
   281     CActiveScheduler::Start(); // CSI: 3 #
       
   282     }
       
   283 
       
   284 CSession2* CPEngPlgSrv::NewSessionL( const TVersion &aVersion, const RMessage2& /*aMessage*/ ) const
       
   285     {
       
   286     if ( !User::QueryVersionSupported( aVersion, TVersion( KRequiredVersionMajor, KRequiredVersionMinor, KRequiredVersionBuild ) ) )
       
   287         {
       
   288         User::Leave( KErrNotSupported );
       
   289         }
       
   290 
       
   291     return CPEngPlgSess::NewL( const_cast<CPEngPlgSrv&>( *this ) );
       
   292     };
       
   293 
       
   294 //  End of File
       
   295