pkiutilities/DeviceToken/Src/Generic/Server/DevTokenServer.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 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:   Implementation of DevTokenServer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32debug.h>
       
    21 #include "DevTokenServer.h"
       
    22 #include "DevTokenUtil.h"
       
    23 #include "DevCertKeyStoreServer.h"
       
    24 #include "DevCertKeyStoreSession.h"
       
    25 #include "DevandTruSrvCertStoreServer.h"
       
    26 #include "DevandTruSrvCertStoreSession.h"
       
    27 #include "TrustedSitesServer.h"
       
    28 #include "TrustedSitesSession.h"
       
    29 #include "DevTokenServerDebug.h"
       
    30 #include "DevTokenServerName.h"
       
    31 #include "DevtokenLog.h"
       
    32 #include "DevTokenDialog.h"
       
    33 //  approx 2s
       
    34 const TInt KServerShutdownDelay = 0x200000; 
       
    35 
       
    36 
       
    37 // ======== MEMBER FUNCTIONS ========
       
    38 
       
    39 // CDevTokenServerSession 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CDevTokenServerSession::CDevTokenServerSession()
       
    43 // ---------------------------------------------------------------------------
       
    44 // 
       
    45 CDevTokenServerSession::CDevTokenServerSession()
       
    46     {
       
    47     }
       
    48 
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CDevTokenServerSession::Server()
       
    52 // ---------------------------------------------------------------------------
       
    53 // 
       
    54 inline CDevTokenServer& CDevTokenServerSession::Server()
       
    55     {
       
    56     return static_cast<CDevTokenServer&>(const_cast<CServer2&>(*CSession2::Server()));
       
    57     }
       
    58 
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CDevTokenServerSession::CreateL()
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 void CDevTokenServerSession::CreateL()
       
    65     {
       
    66     Server().AddSession();
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // CDevTokenServerSession::~CDevTokenServerSession()
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 CDevTokenServerSession::~CDevTokenServerSession()
       
    75     {
       
    76     Server().DropSession();
       
    77     }
       
    78 
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CDevTokenServerSession::ServiceL()
       
    83 // Handle a client request.  Leaving is handled by
       
    84 // CDevTokenServerSession::ServiceError() which reports the error code to the
       
    85 // client.
       
    86 // ---------------------------------------------------------------------------
       
    87 // 
       
    88 void CDevTokenServerSession::ServiceL(const RMessage2& aMessage)
       
    89     {
       
    90     #ifdef _DEBUG
       
    91     // OOM testing
       
    92     switch (aMessage.Function())
       
    93         {
       
    94         case EStartOOMTest:
       
    95             DevTokenServerDebug::StartOOMTest();
       
    96             aMessage.Complete(KErrNone);
       
    97             return;
       
    98 
       
    99         case EIncHeapFailPoint:
       
   100             DevTokenServerDebug::IncHeapFailPoint();
       
   101             aMessage.Complete(KErrNone);
       
   102             return;
       
   103 
       
   104         case EResetHeapFail:
       
   105             DevTokenServerDebug::ResetHeapFail();
       
   106             aMessage.Complete(KErrNone);
       
   107             return;
       
   108 
       
   109         case EAllocCount:
       
   110             aMessage.Complete(User::CountAllocCells());
       
   111             return;
       
   112         } 
       
   113     #endif
       
   114 
       
   115     DoServiceL(aMessage);
       
   116     }
       
   117 
       
   118 
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CDevTokenServerSession::ServiceError()
       
   122 // Handle an error from ServiceL() A bad descriptor error implies a badly
       
   123 // programmed client, so panic it - otherwise report the error to the client.
       
   124 // ---------------------------------------------------------------------------
       
   125 //  
       
   126 void CDevTokenServerSession::ServiceError(const RMessage2& aMessage, TInt aError)
       
   127     {
       
   128     if (aError==KErrBadDescriptor)
       
   129         {
       
   130         PanicClient(aMessage, EPanicBadDescriptor);
       
   131         }
       
   132 
       
   133     CSession2::ServiceError(aMessage, aError);
       
   134     }
       
   135 
       
   136 
       
   137 // CDevTokenServer 
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // CDevTokenServer::CDevTokenServer()
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 inline CDevTokenServer::CDevTokenServer() :
       
   144   CServer2(0, ESharableSessions)
       
   145     {
       
   146     }
       
   147 
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // CDevTokenServer::NewLC()
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 CServer2* CDevTokenServer::NewLC()
       
   154     {
       
   155     TRACE_PRINT(" CDevTokenServer::NewLC -->");
       
   156     CDevTokenServer* self=new(ELeave) CDevTokenServer;
       
   157     CleanupStack::PushL(self);
       
   158     self->ConstructL();
       
   159     TRACE_PRINT(" CDevTokenServer::NewLC <--");
       
   160     return self;
       
   161     }
       
   162 
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CDevTokenServer::~CDevTokenServer()
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 CDevTokenServer::~CDevTokenServer()
       
   169     {
       
   170     DevTokenDialog::Cleanup();
       
   171     delete iKeyStoreServer;
       
   172     delete iCertStoreServer;
       
   173     delete iTrustedSitesServer;
       
   174     }
       
   175 
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // CDevTokenServer::ConstructL()
       
   179 // 2nd phase construction - ensure the timer and server objects are running.
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CDevTokenServer::ConstructL()
       
   183     {
       
   184     TRACE_PRINT(" CDevTokenServer::ConstructL -->");
       
   185     TRACE_PRINT(" CDevTokenServer::ConstructL 1");
       
   186     DevTokenDialog::InitialiseL();
       
   187     StartL( KDevTokenServerName );
       
   188     TRACE_PRINT(" CDevTokenServer::ConstructL 2");
       
   189     // Ensure that the server still exits even if the 1st client fails to connect
       
   190     iShutdown.ConstructL();
       
   191     iShutdown.Start();
       
   192     }
       
   193 
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // CDevTokenServer::AddSession()
       
   197 // A new session is being created - cancel the shutdown timer if it was running.
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CDevTokenServer::AddSession()
       
   201     {
       
   202     ++iSessionCount;
       
   203     iShutdown.Cancel();
       
   204     }
       
   205 
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // CDevTokenServer::DropSession()
       
   209 // A session is being destroyed - start the shutdown timer if it is the last session. 
       
   210 // ---------------------------------------------------------------------------
       
   211 //
       
   212 void CDevTokenServer::DropSession()
       
   213     {
       
   214     if (--iSessionCount==0)
       
   215         {
       
   216         iShutdown.Start();
       
   217         }
       
   218     }
       
   219 
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // CDevTokenServer::KeyStoreServerL()
       
   223 // Lazily create key store server object.
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 CDevCertKeyStoreServer& CDevTokenServer::KeyStoreServerL() const
       
   227     {
       
   228     if (!iKeyStoreServer)
       
   229         {
       
   230         iKeyStoreServer = CDevCertKeyStoreServer::NewL();
       
   231         }
       
   232 
       
   233     return *iKeyStoreServer;
       
   234     }
       
   235 
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // CDevTokenServer::CertStoreServerL()
       
   239 // Lazily create cert store server object. 
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 CDevandTruSrvCertStoreServer& CDevTokenServer::CertStoreServerL() const
       
   243     {
       
   244     if (!iCertStoreServer)
       
   245         {
       
   246         iCertStoreServer = CDevandTruSrvCertStoreServer::NewL( &TrustedSitesL() );
       
   247         }
       
   248 
       
   249     return *iCertStoreServer;
       
   250     }
       
   251 
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // CDevTokenServer::TrustedSitesL()
       
   255 // Lazily create trusted site server object. 
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 CTrustedSitesServer& CDevTokenServer::TrustedSitesL() const
       
   259     {
       
   260     if( !iTrustedSitesServer )
       
   261         {
       
   262         iTrustedSitesServer = CTrustedSitesServer::NewL((CDevTokenServer*)this);
       
   263         }
       
   264     return *iTrustedSitesServer;    
       
   265     }
       
   266 
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // CDevTokenServer::NewSessionL()
       
   270 // Create a new client session.
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 CSession2* CDevTokenServer::NewSessionL(const TVersion& aVersion, 
       
   274                                         const RMessage2& /*aMessage*/) const
       
   275     {
       
   276     // The token the client wants to talk to is encoded in the major version number
       
   277     EDevTokenEnum token = static_cast<EDevTokenEnum>(aVersion.iMajor);  
       
   278 
       
   279     // The minor version number represents the version of the protocol
       
   280     if ( aVersion.iMinor != KDevTokenProtolVersion )
       
   281         {
       
   282         User::Leave(KErrNotSupported);
       
   283         }
       
   284 
       
   285     CDevTokenServerSession* result = NULL;
       
   286 
       
   287     switch (token)
       
   288         {
       
   289         case EDevCertKeyStore:
       
   290             result = KeyStoreServerL().CreateSessionL();
       
   291             break;
       
   292 
       
   293         case EDevCertStore:
       
   294         case ETruSrvCertStore:
       
   295             result = CertStoreServerL().CreateSessionL();
       
   296             break;
       
   297 
       
   298         case ETruSitesStore:
       
   299             result = TrustedSitesL().CreateSessionL();
       
   300             break;
       
   301         default:
       
   302             User::Leave(KErrNotSupported);
       
   303             break;
       
   304         }
       
   305 
       
   306     return result;
       
   307     }
       
   308 
       
   309 
       
   310 // CShutdown
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // CShutdown::CShutdown()
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 inline CShutdown::CShutdown() :
       
   317   CTimer(-1)
       
   318     {
       
   319     CActiveScheduler::Add(this);
       
   320     }
       
   321 
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // CShutdown::ConstructL()
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 inline void CShutdown::ConstructL()
       
   328     {
       
   329     CTimer::ConstructL();
       
   330     }
       
   331 
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CShutdown::Start()
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 inline void CShutdown::Start()
       
   338     {
       
   339     After(KServerShutdownDelay);
       
   340     }
       
   341 
       
   342 
       
   343 // ---------------------------------------------------------------------------
       
   344 // CShutdown::RunL()
       
   345 // Initiate server exit when the timer expires.
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 void CShutdown::RunL()
       
   349     {
       
   350     CActiveScheduler::Stop();
       
   351     }
       
   352 
       
   353 
       
   354 // ======== LOCAL FUNCTIONS ========
       
   355 
       
   356 // Server startup 
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // RunServerL()
       
   360 // Perform all server initialisation, in particular creation of the scheduler
       
   361 // and server and then run the scheduler.
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 static void RunServerL()
       
   365     {
       
   366     TRACE_PRINT("RunServerL 1");
       
   367     // Naming the server thread after the server helps to debug panics
       
   368     User::LeaveIfError(User::RenameThread( KDevTokenServerName ));
       
   369     TRACE_PRINT("RunServerL 2");
       
   370     // Create and install the active scheduler we need
       
   371     CActiveScheduler* s=new(ELeave) CActiveScheduler;
       
   372     CleanupStack::PushL(s);
       
   373     CActiveScheduler::Install(s);
       
   374     TRACE_PRINT("RunServerL 3");
       
   375     // Create the server and leave it on the cleanup stack
       
   376     CDevTokenServer::NewLC();
       
   377     TRACE_PRINT("RunServerL 4");
       
   378     // Before starting the server, notify client that initialisation is
       
   379     // complete.
       
   380     // (note that WINS on EKA1 uses threads since it lacks process emulation)
       
   381     RProcess::Rendezvous(KErrNone);
       
   382     TRACE_PRINT("RunServerL 5");
       
   383     // Ready to run
       
   384     CActiveScheduler::Start();
       
   385 
       
   386     // Cleanup the server and scheduler
       
   387     CleanupStack::PopAndDestroy(2);
       
   388     }
       
   389 
       
   390 
       
   391 // ---------------------------------------------------------------------------
       
   392 // E32Main()
       
   393 // Server process entry point.
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 TInt E32Main()
       
   397     {
       
   398     #ifdef _DEBUG
       
   399     DevTokenServerDebug::HeapCheckStart();
       
   400     #endif
       
   401 
       
   402     CTrapCleanup* cleanup=CTrapCleanup::New();
       
   403     TInt r=KErrNoMemory;
       
   404     if (cleanup)
       
   405         {
       
   406         TRAP(r,RunServerL());
       
   407         delete cleanup;
       
   408         }
       
   409 
       
   410     #ifdef _DEBUG
       
   411     DevTokenServerDebug::HeapCheckEnd();
       
   412     #endif
       
   413     return r;
       
   414     }
       
   415     
       
   416 //EOF
       
   417