graphicscomposition/openwfsupport/inc/owftestexithelper.inl
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2009 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 // A copy of shutdown cleanup functions that should be called  
       
    15 // to properly support openWF.
       
    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 #include <e32property.h>
       
    31 #include <EGL/egl.h>
       
    32 
       
    33 // IDs of p&s properties that optionally contain onexit callbacks
       
    34 // that may be used to release singletons owned by libraries at shutdown in order to make 
       
    35 // the memory leak tests work.
       
    36 // By convention, the ID is the same as the UID3 of the libary.
       
    37 static TBool gReleaseSingletonsOnExit = EFalse;
       
    38 static const TUid KOpenWfcImplCleanupKey = {0x10286FC4};     
       
    39 static const TUid KOpenWfcInteropCleanupKey = {0x10286FC5};
       
    40 
       
    41 
       
    42 static void DefineOwfSingletonKey(const TUid& aSingletonKey)
       
    43 /**
       
    44  * Defines a new property for a singleton key. WServ must only process 
       
    45  * singleton keys that it created to prevent a malicious process with the 
       
    46  * WriteDeviceData capability causing arbitrary functions to be executed.
       
    47  * 
       
    48  * @param   aSingeltonKey   The UID of the singleton key to define.
       
    49  */
       
    50     {
       
    51     RThread t;      
       
    52     TUid category = { t.SecureId().iId };
       
    53     RProperty prop;
       
    54     
       
    55     // Write access is restricted to THIS process
       
    56     TInt err = prop.Define( category, aSingletonKey.iUid, 
       
    57             RProperty::EByteArray, TSecurityPolicy( t.SecureId() ), 
       
    58             TSecurityPolicy( t.SecureId() ), sizeof( TCallBack ) );
       
    59     
       
    60     if ( err == KErrNone || err == KErrAlreadyExists)
       
    61         {
       
    62         TCallBack cb( NULL, NULL );
       
    63         TPckgC<TCallBack> cbPckg( cb );
       
    64         
       
    65         // Any error should cause the properties to be ignored
       
    66         err = prop.Set( category, aSingletonKey.iUid, cbPckg );    
       
    67         }      
       
    68     //We presume that if property already exists it was previously set by this test code.
       
    69     if ( err != KErrNone )
       
    70         {
       
    71         // A problem occured / the property already existed so for safety
       
    72         // the release code should be skipped.
       
    73         gReleaseSingletonsOnExit = EFalse;
       
    74         }
       
    75     
       
    76     prop.Close();
       
    77     t.Close();
       
    78     }
       
    79 #define DefineOwfSingletonKeys DefineOwfSingletonKeys
       
    80 /** Call this method before starting the compositor.    
       
    81  * 
       
    82  */
       
    83 static void DefineOwfSingletonKeys()
       
    84     {
       
    85     // Define properties for singleton callbacks. This must only be done ONCE
       
    86     // to ensure the properties can't be hijacked.
       
    87     gReleaseSingletonsOnExit = ETrue;
       
    88     DefineOwfSingletonKey(KOpenWfcInteropCleanupKey);
       
    89     DefineOwfSingletonKey(KOpenWfcImplCleanupKey);
       
    90     }
       
    91 
       
    92 static void DeleteOwfSingleton( const TUid& aSingletonKey )
       
    93 /**
       
    94  * Deletes a singleton object that was created on WServ's main heap.
       
    95  * 
       
    96  * @pre     The ws plugins have not been unloaded.
       
    97  * @param   aSingletonKey   The UID of the singleton which correponds to an
       
    98  *                          RProperty within WServ's category.                       
       
    99  */
       
   100     {   
       
   101     if ( gReleaseSingletonsOnExit )
       
   102         {
       
   103         RThread t;
       
   104         TPckgBuf<TCallBack> cb;
       
   105         RProperty prop; 
       
   106         TInt err = prop.Get(TUid::Uid(t.SecureId().iId), aSingletonKey.iUid, cb);
       
   107         if (err == KErrNone && cb.Length() == sizeof(TCallBack) && 
       
   108                 cb().iFunction && cb().iPtr == &User::Heap())
       
   109             {
       
   110             // Callback is only invoked if the heap for the singleton was the 
       
   111             // WServ heap because the WServ memory leak tests only check this
       
   112             // heap.
       
   113             cb().CallBack();
       
   114             }
       
   115         // Errors are ignored because the purpose of this function is to free
       
   116         // singletons in order top make memory leak checks pass.
       
   117         prop.Close();
       
   118         t.Close();
       
   119         }
       
   120     }
       
   121 /** Call this method to destroy OWF-C singletons on shut down
       
   122  * 
       
   123  */
       
   124 #define DeleteOwfSingletons DeleteOwfSingletons
       
   125 static void DeleteOwfSingletons()
       
   126     {
       
   127     // Free singletons on WServ heap created by libraries. Must be called
       
   128     // BEFORE iPluginManager is deleted otherwise the library code could have
       
   129     // been unloaded.
       
   130     DeleteOwfSingleton(KOpenWfcImplCleanupKey);
       
   131     DeleteOwfSingleton(KOpenWfcInteropCleanupKey);
       
   132     /* Release any use of EGL by this thread. */
       
   133     eglReleaseThread();
       
   134     }
       
   135