Merge pull request #3389 from lambdageek/bug-43099
[mono.git] / mcs / class / referencesource / mscorlib / system / AppContext / AppContextSwitches.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6
7 namespace System
8 {
9     using System;
10     using System.Runtime.CompilerServices;
11
12     internal static class AppContextSwitches
13     {
14         private static int _noAsyncCurrentCulture;
15         public static bool NoAsyncCurrentCulture
16         {
17             [MethodImpl(MethodImplOptions.AggressiveInlining)]
18             get
19             {
20                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchNoAsyncCurrentCulture, ref _noAsyncCurrentCulture);
21             }
22         }
23
24         private static int _throwExceptionIfDisposedCancellationTokenSource;
25         public static bool ThrowExceptionIfDisposedCancellationTokenSource
26         {
27             [MethodImpl(MethodImplOptions.AggressiveInlining)]
28             get
29             {
30                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchThrowExceptionIfDisposedCancellationTokenSource, ref _throwExceptionIfDisposedCancellationTokenSource);
31             }
32         }
33
34         private static int _preserveEventListnerObjectIdentity;
35         public static bool PreserveEventListnerObjectIdentity
36         {
37             [MethodImpl(MethodImplOptions.AggressiveInlining)]
38             get
39             {
40                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchPreserveEventListnerObjectIdentity, ref _preserveEventListnerObjectIdentity);
41             }
42         }
43
44         private static int _useLegacyPathHandling;
45
46         /// <summary>
47         /// Use legacy path normalization logic and blocking of extended syntax.
48         /// </summary>
49         public static bool UseLegacyPathHandling
50         {
51             [MethodImpl(MethodImplOptions.AggressiveInlining)]
52             get
53             {
54                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchUseLegacyPathHandling, ref _useLegacyPathHandling);
55             }
56         }
57
58         private static int _blockLongPaths;
59
60         /// <summary>
61         /// Throw PathTooLongException for paths greater than MAX_PATH or directories greater than 248 (as per CreateDirectory Win32 limitations)
62         /// </summary>
63         public static bool BlockLongPaths
64         {
65             [MethodImpl(MethodImplOptions.AggressiveInlining)]
66             get
67             {
68                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchBlockLongPaths, ref _blockLongPaths);
69             }
70         }
71
72         private static int _cloneActor;
73
74         /// <summary>
75         /// When copying a ClaimsIdentity.Actor this switch controls whether ClaimsIdentity.Actor should be set as a reference or the result of Actor.Clone()
76         /// </summary>
77         public static bool SetActorAsReferenceWhenCopyingClaimsIdentity
78         {
79             [MethodImpl(MethodImplOptions.AggressiveInlining)]
80             get
81             {
82                 return GetCachedSwitchValue(AppContextDefaultValues.SwitchSetActorAsReferenceWhenCopyingClaimsIdentity, ref _cloneActor);
83             }
84         }
85
86         //
87         // Implementation details
88         //
89
90         private static bool DisableCaching { get; set; }
91
92         static AppContextSwitches()
93         {
94             bool isEnabled;
95             if (AppContext.TryGetSwitch(@"TestSwitch.LocalAppContext.DisableCaching", out isEnabled))
96             {
97                 DisableCaching = isEnabled;
98             }
99         }
100
101         [MethodImpl(MethodImplOptions.AggressiveInlining)]
102         internal static bool GetCachedSwitchValue(string switchName, ref int switchValue)
103         {
104             if (switchValue < 0) return false;
105             if (switchValue > 0) return true;
106
107             return GetCachedSwitchValueInternal(switchName, ref switchValue);
108         }
109
110         private static bool GetCachedSwitchValueInternal(string switchName, ref int switchValue)
111         {
112             bool isSwitchEnabled;
113             AppContext.TryGetSwitch(switchName, out isSwitchEnabled);
114
115             if (DisableCaching)
116             {
117                 return isSwitchEnabled;
118             }
119
120             switchValue = isSwitchEnabled ? 1 /*true*/ : -1 /*false*/;
121             return isSwitchEnabled;
122         }
123     }
124 }