Merge pull request #1349 from martinjt/MachineKeyProtect
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / CallContext.cs
1 // 
2 // System.Runtime.Remoting.Messaging.CallContext.cs
3 //
4 // Authors: Jaime Anguiano Olarra (jaime@gnome.org)
5 //         Lluis Sanchez Gual (lluis@ximian.com)
6 //          Marek Safar (marek.safar@gmail.com)
7 //
8 // (c) 2002, Jaime Anguiano Olarra
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Threading;
34 using System.Collections;
35 using System.Collections.Generic;
36
37 namespace System.Runtime.Remoting.Messaging 
38 {       
39         [Serializable]
40         [System.Runtime.InteropServices.ComVisible (true)]
41         public sealed class CallContext 
42         {
43                 [ThreadStatic] static Header [] Headers;
44                 [ThreadStatic] static object hostContext;
45                 
46                 private CallContext ()
47                 {
48                 }
49
50                 public static object HostContext {
51                         get { return hostContext; }
52                         set { hostContext = value; }
53                 }
54
55                 public static void FreeNamedDataSlot (string name)
56                 {
57                         ExecutionContext.FreeNamedDataSlot (name);
58                 }
59
60                 public static object GetData (string name) 
61                 {
62                         var value = LogicalGetData (name);
63                         if (value == null)
64                                 Datastore.TryGetValue (name, out value);
65
66                         return value;
67                 }
68                 
69                 public static void SetData (string name, object data) 
70                 {
71                         if (data is ILogicalThreadAffinative) {
72                                 LogicalSetData (name, data);
73                         } else {
74                                 var ec = ExecutionContext.GetCurrentWritable ();
75                                 ec.LogicalCallContext.FreeNamedDataSlot (name);
76                                 ec.DataStore [name] = data;
77                         }
78                 }
79                 
80                 public static object LogicalGetData (string name) 
81                 {
82                         return LogicalContext.GetData (name);
83                 }
84
85                 public static void LogicalSetData (string name, object data) 
86                 {
87                         var ec = ExecutionContext.GetCurrentWritable ();
88                         ec.DataStore.Remove (name);
89                         ec.LogicalCallContext.SetData (name, data);
90                 }
91
92                 public static Header[] GetHeaders () 
93                 {
94                         return Headers;
95                 }
96                 
97                 public static void SetHeaders (Header[] headers) 
98                 {
99                         Headers = headers;
100                 }
101
102                 internal static object SetCurrentCallContext (LogicalCallContext ctx)
103                 {
104                         object oldData = new object[] { Datastore, LogicalContext.Datastore };
105
106                         Hashtable logicalDatastore;
107                         if (ctx != null && ctx.HasInfo)
108                                 logicalDatastore = (Hashtable) ctx.Datastore.Clone ();
109                         else
110                                 logicalDatastore = null;
111                                 
112                         LogicalContext.Datastore = logicalDatastore;
113                         return oldData;
114                 }
115
116                 internal static void UpdateCurrentLogicalCallContext (LogicalCallContext ctx)
117                 {
118                         Hashtable data = ctx.Datastore;
119                         if (data == null)
120                                 return;
121
122                         foreach (DictionaryEntry entry in data)
123                                 LogicalSetData ((string)entry.Key, entry.Value);
124                 }
125                 
126                 internal static void RestoreCallContext (object oldContext)
127                 {
128                         object[] contextArray = (object[])oldContext;
129                         ExecutionContext.DataStore = (Dictionary<string, object>)contextArray [0];
130                         LogicalContext.Datastore = (Hashtable)contextArray [1];
131                 }
132
133                 static Dictionary<string, object> Datastore {
134                         get {
135                                 return ExecutionContext.DataStore;
136                         }
137                 }
138
139                 static LogicalCallContext LogicalContext {
140                         get {
141                                 return ExecutionContext.LogicalCallContext;
142                         }
143                 }
144
145                 static ExecutionContext ExecutionContext {
146                         get {
147                                 return ExecutionContext.Current;
148                         }
149                 }
150         }
151
152         [System.Runtime.InteropServices.ComVisible (true)]
153         public interface ILogicalThreadAffinative
154         {
155         }
156 }