xmlsecurityengine/xmlsecwrapper/src/xmlsecwinit.cpp
changeset 0 e35f40988205
child 16 d10d750052f0
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     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: Methods for initializing and closing component.       
       
    15 *
       
    16 */
       
    17 // XML Engine
       
    18 #include <libxml2_globals.h>
       
    19 #include <xmlengxestd.h>
       
    20 
       
    21 // XML Sec
       
    22 #include "xmlsec_crypto.h"
       
    23 #include "xmlsec_error_flag.h"
       
    24 #include "xmlsecwerrors.h"
       
    25 #include "xmlsecwinit.h"
       
    26 #include "xmlsecwglobalstate.h"
       
    27 #include "xmlsecwkeymanager.h"
       
    28 
       
    29 #include "xmlsecwinternalutils.h"
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // Get global state for dll
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 XmlSecGlobalState* XmlSecGetTls()
       
    36     {
       
    37     return STATIC_CAST(XmlSecGlobalState*, Dll::Tls());
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // Set global state for dll
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 XmlSecGlobalState* XmlSecSetTlsL()
       
    45     {
       
    46     XmlSecGlobalState* gs = new(ELeave) XmlSecGlobalState;
       
    47     gs->iKeyManager = NULL;
       
    48     gs->iUserCount = 0;
       
    49     TInt error = Dll::SetTls(gs);
       
    50     if(error)
       
    51         {
       
    52         delete gs;
       
    53         User::Leave(error);
       
    54         }
       
    55     return gs;
       
    56     } 
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // UnSet global state for dll
       
    60 // ---------------------------------------------------------------------------
       
    61 //    
       
    62 void XmlSecUnsetTlsD( void* aGlobalState )
       
    63     {
       
    64     delete aGlobalState;
       
    65     Dll::SetTls( NULL );
       
    66     }
       
    67     
       
    68 // ---------------------------------------------------------------------------
       
    69 // Initialize wrapper
       
    70 // ---------------------------------------------------------------------------
       
    71 // 
       
    72 EXPORT_C void XmlSecInitializeL()
       
    73     {
       
    74     /* Init libxml and libxslt libraries */
       
    75     XmlSecGlobalState* gs = XmlSecGetTls();
       
    76     if(!gs)
       
    77         {
       
    78         gs = XmlSecSetTlsL();
       
    79         }
       
    80     if(gs->iUserCount == 0)
       
    81         {
       
    82         CleanupStack::PushL( TCleanupItem( XmlSecUnsetTlsD, gs ) );
       
    83         XmlEngineAttachL();
       
    84         
       
    85         RXmlEngDOMImplementation dom;
       
    86         dom.OpenL();
       
    87         gs->iDOMImpl = &dom;
       
    88         xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS;
       
    89         xmlSubstituteEntitiesDefault(1);
       
    90 
       
    91         // Init xmlsec library
       
    92         if(xmlSecInit() < 0)
       
    93             {
       
    94             User::Leave(KErrInit);
       
    95             }
       
    96         // Check loaded library version
       
    97         if(xmlSecCheckVersion() != 1)
       
    98             {
       
    99             User::Leave(KErrInit);
       
   100             }
       
   101         // Init crypto library
       
   102         if(xmlSecCryptoAppInit(NULL) < 0) 
       
   103             {
       
   104             User::Leave(KErrInit);
       
   105             }
       
   106         // Init xmlsec-crypto library
       
   107         if(xmlSecCryptoInit() < 0) 
       
   108             {
       
   109             User::Leave(KErrInit);
       
   110             }
       
   111         //initialization completed
       
   112         CleanupStack::Pop( gs );    //gs will be destroyed during cleanup of xmlsec
       
   113         }
       
   114     gs->iUserCount++;
       
   115     }
       
   116     
       
   117 // ---------------------------------------------------------------------------
       
   118 // Cleanup wrapper
       
   119 // ---------------------------------------------------------------------------
       
   120 // 
       
   121 EXPORT_C void XmlSecCleanup(TAny* globalState)
       
   122     {
       
   123     XmlSecGlobalState* gs = NULL;
       
   124     if(globalState)
       
   125         {
       
   126         gs = reinterpret_cast<XmlSecGlobalState*>(globalState);
       
   127         }
       
   128     else
       
   129         {
       
   130         gs = XmlSecGetTls();
       
   131         }
       
   132     if(!gs)
       
   133         {
       
   134         return;
       
   135         }
       
   136     gs->iUserCount--;
       
   137     if(gs->iUserCount > 0)
       
   138         {
       
   139         return;
       
   140         }
       
   141     // Shutdown xmlsec-crypto library
       
   142     xmlSecCryptoShutdown();
       
   143   
       
   144     // Shutdown crypto library
       
   145     xmlSecCryptoAppShutdown();
       
   146   
       
   147     // Shutdown xmlsec library
       
   148     xmlSecShutdown();
       
   149   
       
   150 #ifndef XMLSEC_NO_XSLT
       
   151     xsltCleanupGlobals();            
       
   152 #endif /* XMLSEC_NO_XSLT */
       
   153     gs->iDOMImpl->Close();
       
   154 	XmlEngineCleanup();
       
   155 	if(gs->iKeyManager)
       
   156 	    {
       
   157 	    delete gs->iKeyManager;
       
   158 	    }
       
   159 	delete gs;
       
   160 	Dll::SetTls(NULL);
       
   161 	xmlSecResetErrorFlag();
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // Initialize wrapper
       
   166 // ---------------------------------------------------------------------------
       
   167 //    
       
   168 EXPORT_C void XmlSecPushL()
       
   169     {
       
   170     XmlSecInitializeL();
       
   171     CleanupStack::PushL(TCleanupItem(XmlSecCleanup, NULL));
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // Cleanup wrapper
       
   176 // ---------------------------------------------------------------------------
       
   177 //     
       
   178 EXPORT_C void XmlSecPopAndDestroy()
       
   179     {
       
   180     CleanupStack::PopAndDestroy();
       
   181     }
       
   182