We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Threading
- {
- using System.Security;
- using System.Runtime.Remoting;
- using System.Runtime.CompilerServices;
- using System.Runtime.Serialization;
- using System.Security.Permissions;
- using System.Runtime.Remoting.Contexts;
- using System.Runtime.Remoting.Messaging;
- using System.Runtime.ConstrainedExecution;
- using System.Runtime.InteropServices;
-
-
-
- internal class HostExecutionContextSwitcher
- {
- internal ExecutionContext executionContext;
- internal HostExecutionContext previousHostContext;
- internal HostExecutionContext currentHostContext;
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- public static void Undo(object switcherObject)
- {
- if (switcherObject == null)
- return;
-
- HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
- if (hostMgr != null) {
- hostMgr.Revert(switcherObject);
- }
- }
- }
-
-
-
- public class HostExecutionContext
- {
- private object state;
-
- protected internal object State {
- get { return state; }
- set { state = value; }
- }
-
- public HostExecutionContext()
- {
- }
-
- public HostExecutionContext(object state)
- {
- this.state = state;
- }
-
- public virtual HostExecutionContext CreateCopy()
- {
- object newState = state;
- if (state is IUnknownSafeHandle) {
-
- newState = ((IUnknownSafeHandle)state).Clone();
- }
- return new HostExecutionContext(state);
- }
- }
-
- internal class IUnknownSafeHandle : SafeHandle
- {
- public IUnknownSafeHandle() : base(IntPtr.Zero, true)
- {
- }
-
- public override bool IsInvalid {
- get { return handle == IntPtr.Zero; }
- }
-
- protected override bool ReleaseHandle()
- {
- HostExecutionContextManager.ReleaseHostSecurityContext(this.handle);
- return true;
- }
-
- internal object Clone()
- {
- IUnknownSafeHandle unkSafeHandleCloned = new IUnknownSafeHandle();
-
-
- if (!IsInvalid) {
- HostExecutionContextManager.CloneHostSecurityContext(this, unkSafeHandleCloned);
- }
- return unkSafeHandleCloned;
- }
- }
-
-
-
- public class HostExecutionContextManager
- {
- private static bool _fIsHostedChecked;
- private static bool _fIsHosted;
- private static HostExecutionContextManager _hostExecutionContextManager;
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private static extern bool HostSecurityManagerPresent();
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern int ReleaseHostSecurityContext(IntPtr context);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern int CloneHostSecurityContext(SafeHandle context, SafeHandle clonedContext);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private static extern int CaptureHostSecurityContext(SafeHandle capturedContext);
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- private static extern int SetHostSecurityContext(SafeHandle context, bool fReturnPrevious, SafeHandle prevContext);
-
- static internal bool CheckIfHosted()
- {
- if (!_fIsHostedChecked) {
- _fIsHosted = HostSecurityManagerPresent();
- _fIsHostedChecked = true;
- }
- return _fIsHosted;
- }
-
-
- public virtual HostExecutionContext Capture()
- {
- HostExecutionContext context = null;
-
- if (CheckIfHosted()) {
- IUnknownSafeHandle unkSafeHandle = new IUnknownSafeHandle();
- context = new HostExecutionContext(unkSafeHandle);
-
-
-
- CaptureHostSecurityContext(unkSafeHandle);
- }
-
-
- return context;
-
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
- {
- if (hostExecutionContext == null) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
- }
-
- HostExecutionContextSwitcher switcher = new HostExecutionContextSwitcher();
- ExecutionContext currentExecutionContext = Thread.CurrentThread.ExecutionContext;
-
- switcher.executionContext = currentExecutionContext;
- switcher.currentHostContext = hostExecutionContext;
- switcher.previousHostContext = null;
-
- if (CheckIfHosted()) {
- if (hostExecutionContext.State is IUnknownSafeHandle) {
-
- IUnknownSafeHandle unkPrevSafeHandle = new IUnknownSafeHandle();
- switcher.previousHostContext = new HostExecutionContext(unkPrevSafeHandle);
-
-
- IUnknownSafeHandle unkSafeHandle = (IUnknownSafeHandle)hostExecutionContext.State;
-
-
-
- SetHostSecurityContext(unkSafeHandle, true, unkPrevSafeHandle);
- }
- }
-
-
- currentExecutionContext.HostExecutionContext = hostExecutionContext;
-
- return switcher;
- }
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure), ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- public virtual void Revert(object previousState)
- {
- HostExecutionContextSwitcher hostContextSwitcher = previousState as HostExecutionContextSwitcher;
- if (hostContextSwitcher == null) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
- }
-
-
- ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext;
-
- if (executionContext != hostContextSwitcher.executionContext) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
- }
- hostContextSwitcher.executionContext = null;
-
- HostExecutionContext revertFromHostContext = executionContext.HostExecutionContext;
-
- if (revertFromHostContext != hostContextSwitcher.currentHostContext) {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
- }
-
-
- HostExecutionContext revertToHostContext = hostContextSwitcher.previousHostContext;
-
-
- if (CheckIfHosted()) {
-
- if (revertToHostContext != null && revertToHostContext.State is IUnknownSafeHandle) {
- IUnknownSafeHandle unkprevSafeHandle = (IUnknownSafeHandle)revertToHostContext.State;
-
- SetHostSecurityContext(unkprevSafeHandle, false, null);
- }
- }
-
-
- executionContext.HostExecutionContext = revertToHostContext;
-
- }
-
- static internal HostExecutionContext CaptureHostExecutionContext()
- {
- HostExecutionContext hostContext = null;
-
- HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
- if (hostMgr != null) {
- hostContext = hostMgr.Capture();
- }
- return hostContext;
- }
-
- static internal object SetHostExecutionContextInternal(HostExecutionContext hostContext)
- {
- HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
- object switcher = null;
- if (hostMgr != null) {
- switcher = hostMgr.SetHostExecutionContext(hostContext);
- }
- return switcher;
- }
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- static internal HostExecutionContextManager GetCurrentHostExecutionContextManager()
- {
-
-
- if (AppDomainManager.CurrentAppDomainManager != null) {
-
- return AppDomainManager.CurrentAppDomainManager.HostExecutionContextManager;
- }
- return null;
- }
-
-
- static internal HostExecutionContextManager GetInternalHostExecutionContextManager()
- {
- if (_hostExecutionContextManager == null) {
-
- BCLDebug.Assert(_hostExecutionContextManager == null, "HostExecutionContextManager should be null");
- _hostExecutionContextManager = new HostExecutionContextManager();
- }
- return _hostExecutionContextManager;
- }
- }
- }