2005-05-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Threading / ExecutionContext.cs
1 // 
2 // System.Threading.ExecutionContext.cs
3 //
4 // Authors:
5 //      Lluis Sanchez (lluis@novell.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.InteropServices;
31 using System.Runtime.Serialization;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace System.Threading {
36
37         [Serializable]
38 #if NET_2_0
39         public sealed class ExecutionContext : ISerializable {
40 #else
41         internal sealed class ExecutionContext : ISerializable {
42 #endif
43                 private SecurityContext _sc;
44                 private bool _suppressFlow;
45                 private bool _capture;
46
47                 internal ExecutionContext ()
48                 {
49                 }
50
51                 internal ExecutionContext (ExecutionContext ec)
52                 {
53                         if (ec._sc != null)
54                                 _sc = new SecurityContext (ec._sc);
55                         _suppressFlow = ec._suppressFlow;
56                         _capture = true;
57                 }
58                 
59                 [MonoTODO]
60                 internal ExecutionContext (SerializationInfo info, StreamingContext context)
61                 {
62                         throw new NotImplementedException ();
63                 }
64                 
65                 public static ExecutionContext Capture ()
66                 {
67                         ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
68                         if (ec.FlowSuppressed)
69                                 return null;
70
71                         ExecutionContext capture = new ExecutionContext (ec);
72                         capture.SecurityContext = SecurityContext.Capture ();
73                         return capture;
74                 }
75                 
76                 public ExecutionContext CreateCopy ()
77                 {
78                         if (!_capture)
79                                 throw new InvalidOperationException ();
80
81                         return new ExecutionContext (this);
82                 }
83
84                 [MonoTODO]
85                 [ReflectionPermission (SecurityAction.Demand, MemberAccess = true)]
86                 public void GetObjectData (SerializationInfo info, StreamingContext context)
87                 {
88                         if (info == null)
89                                 throw new ArgumentNullException ("info");
90                         throw new NotImplementedException ();
91                 }
92                 
93                 // internal stuff
94
95                 internal SecurityContext SecurityContext {
96                         get {
97                                 if (_sc == null)
98                                         _sc = new SecurityContext ();
99                                 return _sc;
100                         }
101                         set { _sc = value; }
102                 }
103
104                 internal bool FlowSuppressed {
105                         get { return _suppressFlow; }
106                         set { _suppressFlow = value; }
107                 }
108
109                 // Note: Previous to version 2.0 only the CompressedStack and (sometimes!) the WindowsIdentity
110                 // were propagated to new threads. This is why ExecutionContext is internal in before NET_2_0.
111                 // It also means that all newer context classes should be here (i.e. inside the #if NET_2_0).
112
113                 public static bool IsFlowSuppressed ()
114                 {
115                         return Thread.CurrentThread.ExecutionContext.FlowSuppressed;
116                 }
117
118                 public static void RestoreFlow ()
119                 {
120                         ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
121                         if (!ec.FlowSuppressed)
122                                 throw new InvalidOperationException ();
123
124                         ec.FlowSuppressed = false;
125                 }
126 #if NET_2_0
127                 [MonoTODO ("only the SecurityContext is considered")]
128                 [SecurityPermission (SecurityAction.LinkDemand, Infrastructure = true)]
129                 public static void Run (ExecutionContext executionContext, ContextCallback callBack, object state)
130                 {
131                         if (executionContext == null) {
132                                 throw new InvalidOperationException (Locale.GetText (
133                                         "Null ExecutionContext"));
134                         }
135
136                         // FIXME: supporting more than one context (the SecurityContext)
137                         // will requires a rewrite of this method
138
139                         SecurityContext.Run (executionContext.SecurityContext, callBack, state);
140                 }
141 #endif
142                 public static AsyncFlowControl SuppressFlow ()
143                 {
144                         Thread t = Thread.CurrentThread;
145                         t.ExecutionContext.FlowSuppressed = true;
146                         return new AsyncFlowControl (t, AsyncFlowControlType.Execution);
147                 }
148         }
149 }