graphicstest/graphicstestharness/inc/singletontestexithelper.inl
changeset 152 9f1c3fea0f87
equal deleted inserted replaced
111:29ddb8a72f0e 152:9f1c3fea0f87
       
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Test shutdown cleanup functions that should be called to destroy singletons
       
    15 // before the heap is destroyed, in particular when using TEF steps.
       
    16 // These methods are implemented and called from Window Server - WServ, CWsTop
       
    17 // but this copy can be called by any low-level unit tests that bypass WServ   
       
    18 // but need to provide the same clean-up behaviour.
       
    19 // @test
       
    20 // @internalTechnology
       
    21 // 
       
    22 // Currently, three things are cleaned up:
       
    23 // 1) Singleton API mutex in OpenWF implementation
       
    24 // 2) Singleton Find/Acquire mutext in Native Stream implementation
       
    25 // 3) EGL thread status 
       
    26 //
       
    27 // Note that while these cleanups are only required by OWF-C implementations,
       
    28 // the methods called should be harmless if called 
       
    29 // when some other composition solution is in action.
       
    30 
       
    31 #include <e32property.h>
       
    32 #include <EGL/egl.h>
       
    33 
       
    34 // IDs of p&s properties that optionally contain onexit callbacks
       
    35 // that may be used to release singletons owned by libraries at shutdown in order to make 
       
    36 // the memory leak tests work.
       
    37 // By convention, the ID is the same as the UID3 of the libary.
       
    38 static TBool gReleaseSingletonsOnExit = EFalse;
       
    39 static const TUid KOpenWfcImplCleanupKey = {0x10286FC4};     
       
    40 static const TUid KOpenWfcInteropCleanupKey = {0x10286FC5};
       
    41 
       
    42 
       
    43 static void DefineOwfSingletonKey(const TUid& aSingletonKey)
       
    44 /**
       
    45  * Defines a new property for a singleton key. WServ must only process 
       
    46  * singleton keys that it created to prevent a malicious process with the 
       
    47  * WriteDeviceData capability causing arbitrary functions to be executed.
       
    48  * 
       
    49  * @param   aSingeltonKey   The UID of the singleton key to define.
       
    50  */
       
    51     {
       
    52     RThread t;      
       
    53     TUid category = { t.SecureId().iId };
       
    54     RProperty prop;
       
    55     
       
    56     // Write access is restricted to THIS process
       
    57     TInt err = prop.Define( category, aSingletonKey.iUid, 
       
    58             RProperty::EByteArray, TSecurityPolicy( t.SecureId() ), 
       
    59             TSecurityPolicy( t.SecureId() ), sizeof( TCallBack ) );
       
    60     
       
    61     if ( err == KErrNone || err == KErrAlreadyExists)
       
    62         {
       
    63         TCallBack cb( NULL, NULL );
       
    64         TPckgC<TCallBack> cbPckg( cb );
       
    65         
       
    66         // Any error should cause the properties to be ignored
       
    67         err = prop.Set( category, aSingletonKey.iUid, cbPckg );    
       
    68         }      
       
    69     //We presume that if property already exists it was previously set by this test code.
       
    70     if ( err != KErrNone )
       
    71         {
       
    72         // A problem occured / the property already existed so for safety
       
    73         // the release code should be skipped.
       
    74         gReleaseSingletonsOnExit = EFalse;
       
    75         }
       
    76     
       
    77     prop.Close();
       
    78     t.Close();
       
    79     }
       
    80 #define DefineOwfSingletonKeys DefineOwfSingletonKeys
       
    81 /** Call this method before starting the compositor.    
       
    82  * 
       
    83  */
       
    84 static void DefineOwfSingletonKeys()
       
    85     {
       
    86     // Define properties for singleton callbacks. This must only be done ONCE
       
    87     // to ensure the properties can't be hijacked.
       
    88     gReleaseSingletonsOnExit = ETrue;
       
    89     DefineOwfSingletonKey(KOpenWfcInteropCleanupKey);
       
    90     DefineOwfSingletonKey(KOpenWfcImplCleanupKey);
       
    91     }
       
    92 
       
    93 static void DeleteOwfSingleton( const TUid& aSingletonKey )
       
    94 /**
       
    95  * Deletes a singleton object that was created on WServ's main heap.
       
    96  * 
       
    97  * @pre     The ws plugins have not been unloaded.
       
    98  * @param   aSingletonKey   The UID of the singleton which correponds to an
       
    99  *                          RProperty within WServ's category.                       
       
   100  */
       
   101     {   
       
   102     if ( gReleaseSingletonsOnExit )
       
   103         {
       
   104         RThread t;
       
   105         TPckgBuf<TCallBack> cb;
       
   106         RProperty prop; 
       
   107         TInt err = prop.Get(TUid::Uid(t.SecureId().iId), aSingletonKey.iUid, cb);
       
   108         if (err == KErrNone && cb.Length() == sizeof(TCallBack) && 
       
   109                 cb().iFunction && cb().iPtr == &User::Heap())
       
   110             {
       
   111             // Callback is only invoked if the heap for the singleton was the 
       
   112             // WServ heap because the WServ memory leak tests only check this
       
   113             // heap.
       
   114             cb().CallBack();
       
   115             }
       
   116         // Errors are ignored because the purpose of this function is to free
       
   117         // singletons in order top make memory leak checks pass.
       
   118         prop.Close();
       
   119         t.Close();
       
   120         }
       
   121     }
       
   122 /** Call this method to destroy OWF-C singletons on shut down
       
   123  * 
       
   124  */
       
   125 #define DeleteOwfSingletons DeleteOwfSingletons
       
   126 static void DeleteOwfSingletons()
       
   127     {
       
   128     // Free singletons on WServ heap created by libraries. Must be called
       
   129     // BEFORE iPluginManager is deleted otherwise the library code could have
       
   130     // been unloaded.
       
   131     DeleteOwfSingleton(KOpenWfcImplCleanupKey);
       
   132     DeleteOwfSingleton(KOpenWfcInteropCleanupKey);
       
   133     /* Release any use of EGL by this thread. */
       
   134     eglReleaseThread();
       
   135     }
       
   136