sl@0: // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Test shutdown cleanup functions that should be called to destroy singletons sl@0: // before the heap is destroyed, in particular when using TEF steps. sl@0: // These methods are implemented and called from Window Server - WServ, CWsTop sl@0: // but this copy can be called by any low-level unit tests that bypass WServ sl@0: // but need to provide the same clean-up behaviour. sl@0: // @test sl@0: // @internalTechnology sl@0: // sl@0: // Currently, three things are cleaned up: sl@0: // 1) Singleton API mutex in OpenWF implementation sl@0: // 2) Singleton Find/Acquire mutext in Native Stream implementation sl@0: // 3) EGL thread status sl@0: // sl@0: // Note that while these cleanups are only required by OWF-C implementations, sl@0: // the methods called should be harmless if called sl@0: // when some other composition solution is in action. sl@0: sl@0: #include sl@0: #include sl@0: sl@0: // IDs of p&s properties that optionally contain onexit callbacks sl@0: // that may be used to release singletons owned by libraries at shutdown in order to make sl@0: // the memory leak tests work. sl@0: // By convention, the ID is the same as the UID3 of the libary. sl@0: static TBool gReleaseSingletonsOnExit = EFalse; sl@0: static const TUid KOpenWfcImplCleanupKey = {0x10286FC4}; sl@0: static const TUid KOpenWfcInteropCleanupKey = {0x10286FC5}; sl@0: sl@0: sl@0: static void DefineOwfSingletonKey(const TUid& aSingletonKey) sl@0: /** sl@0: * Defines a new property for a singleton key. WServ must only process sl@0: * singleton keys that it created to prevent a malicious process with the sl@0: * WriteDeviceData capability causing arbitrary functions to be executed. sl@0: * sl@0: * @param aSingeltonKey The UID of the singleton key to define. sl@0: */ sl@0: { sl@0: RThread t; sl@0: TUid category = { t.SecureId().iId }; sl@0: RProperty prop; sl@0: sl@0: // Write access is restricted to THIS process sl@0: TInt err = prop.Define( category, aSingletonKey.iUid, sl@0: RProperty::EByteArray, TSecurityPolicy( t.SecureId() ), sl@0: TSecurityPolicy( t.SecureId() ), sizeof( TCallBack ) ); sl@0: sl@0: if ( err == KErrNone || err == KErrAlreadyExists) sl@0: { sl@0: TCallBack cb( NULL, NULL ); sl@0: TPckgC cbPckg( cb ); sl@0: sl@0: // Any error should cause the properties to be ignored sl@0: err = prop.Set( category, aSingletonKey.iUid, cbPckg ); sl@0: } sl@0: //We presume that if property already exists it was previously set by this test code. sl@0: if ( err != KErrNone ) sl@0: { sl@0: // A problem occured / the property already existed so for safety sl@0: // the release code should be skipped. sl@0: gReleaseSingletonsOnExit = EFalse; sl@0: } sl@0: sl@0: prop.Close(); sl@0: t.Close(); sl@0: } sl@0: #define DefineOwfSingletonKeys DefineOwfSingletonKeys sl@0: /** Call this method before starting the compositor. sl@0: * sl@0: */ sl@0: static void DefineOwfSingletonKeys() sl@0: { sl@0: // Define properties for singleton callbacks. This must only be done ONCE sl@0: // to ensure the properties can't be hijacked. sl@0: gReleaseSingletonsOnExit = ETrue; sl@0: DefineOwfSingletonKey(KOpenWfcInteropCleanupKey); sl@0: DefineOwfSingletonKey(KOpenWfcImplCleanupKey); sl@0: } sl@0: sl@0: static void DeleteOwfSingleton( const TUid& aSingletonKey ) sl@0: /** sl@0: * Deletes a singleton object that was created on WServ's main heap. sl@0: * sl@0: * @pre The ws plugins have not been unloaded. sl@0: * @param aSingletonKey The UID of the singleton which correponds to an sl@0: * RProperty within WServ's category. sl@0: */ sl@0: { sl@0: if ( gReleaseSingletonsOnExit ) sl@0: { sl@0: RThread t; sl@0: TPckgBuf cb; sl@0: RProperty prop; sl@0: TInt err = prop.Get(TUid::Uid(t.SecureId().iId), aSingletonKey.iUid, cb); sl@0: if (err == KErrNone && cb.Length() == sizeof(TCallBack) && sl@0: cb().iFunction && cb().iPtr == &User::Heap()) sl@0: { sl@0: // Callback is only invoked if the heap for the singleton was the sl@0: // WServ heap because the WServ memory leak tests only check this sl@0: // heap. sl@0: cb().CallBack(); sl@0: } sl@0: // Errors are ignored because the purpose of this function is to free sl@0: // singletons in order top make memory leak checks pass. sl@0: prop.Close(); sl@0: t.Close(); sl@0: } sl@0: } sl@0: /** Call this method to destroy OWF-C singletons on shut down sl@0: * sl@0: */ sl@0: #define DeleteOwfSingletons DeleteOwfSingletons sl@0: static void DeleteOwfSingletons() sl@0: { sl@0: // Free singletons on WServ heap created by libraries. Must be called sl@0: // BEFORE iPluginManager is deleted otherwise the library code could have sl@0: // been unloaded. sl@0: DeleteOwfSingleton(KOpenWfcImplCleanupKey); sl@0: DeleteOwfSingleton(KOpenWfcInteropCleanupKey); sl@0: /* Release any use of EGL by this thread. */ sl@0: eglReleaseThread(); sl@0: } sl@0: