summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/HostProtectionException.cs
blob: b08fccd1b3a39f2c59e1c7d9a17af9dc51c6cd39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// 

/*=============================================================================
**
**
** Purpose: Exception class for HostProtection
**
**
=============================================================================*/

namespace System.Security
{
    using System.Security;
    using System;
    using System.Runtime.Serialization;
    using System.Security.Permissions;
    using System.Reflection;
    using System.Text;
    using System.Diagnostics.Contracts;

    [System.Runtime.InteropServices.ComVisible(true)]
    [Serializable]
    public class HostProtectionException : SystemException
    {
        private HostProtectionResource m_protected;
        private HostProtectionResource m_demanded;

        private const String ProtectedResourcesName = "ProtectedResources";
        private const String DemandedResourcesName = "DemandedResources";

        public HostProtectionException() : base()
        {
            m_protected = HostProtectionResource.None;
            m_demanded = HostProtectionResource.None;
        }

        public HostProtectionException(string message) : base(message)
        {
            m_protected = HostProtectionResource.None;
            m_demanded = HostProtectionResource.None;
        }

        public HostProtectionException(string message, Exception e) : base(message, e)
        {
            m_protected = HostProtectionResource.None;
            m_demanded = HostProtectionResource.None;
        }

        protected HostProtectionException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            if (info==null)
                throw new ArgumentNullException(nameof(info));
            Contract.EndContractBlock();

            m_protected = (HostProtectionResource)info.GetValue(ProtectedResourcesName, typeof(HostProtectionResource));
            m_demanded = (HostProtectionResource)info.GetValue(DemandedResourcesName, typeof(HostProtectionResource));
        }

        public HostProtectionException(string message, HostProtectionResource protectedResources, HostProtectionResource demandedResources)
            : base(message)
        {
            SetErrorCode(__HResults.COR_E_HOSTPROTECTION);
            m_protected = protectedResources;
            m_demanded = demandedResources;
        }

        // Called from the VM to create a HP Exception
        private HostProtectionException(HostProtectionResource protectedResources, HostProtectionResource demandedResources)
            : base(SecurityException.GetResString("HostProtection_HostProtection"))
        {
            SetErrorCode(__HResults.COR_E_HOSTPROTECTION);
            m_protected = protectedResources;
            m_demanded = demandedResources;
        }


        public HostProtectionResource ProtectedResources
        {
            get
            {
                return m_protected;
            }
        }

        public HostProtectionResource DemandedResources
        {
            get
            {
                return m_demanded;
            }
        }

        private String ToStringHelper(String resourceString, Object attr)
        {
            if (attr == null)
                return String.Empty;
            StringBuilder sb = new StringBuilder();
            sb.Append(Environment.NewLine);
            sb.Append(Environment.NewLine);
            sb.Append(Environment.GetResourceString( resourceString ));
            sb.Append(Environment.NewLine);
            sb.Append(attr);
            return sb.ToString();
        }

        public override String ToString() 
        {
            String protectedResStrValue = ToStringHelper("HostProtection_ProtectedResources", ProtectedResources);
            StringBuilder sb = new StringBuilder();
            sb.Append(base.ToString());

            sb.Append(protectedResStrValue);
            sb.Append(ToStringHelper("HostProtection_DemandedResources", DemandedResources));

            return sb.ToString();

        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info==null)
                throw new ArgumentNullException(nameof(info));
            Contract.EndContractBlock();

            base.GetObjectData( info, context );

            info.AddValue(ProtectedResourcesName, ProtectedResources, typeof(HostProtectionResource));
            info.AddValue(DemandedResourcesName, DemandedResources, typeof(HostProtectionResource));
        }
    }
}