This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / System.Security.Permissions / IsolatedStoragePermission.cs
1 //
2 // System.Security.Permissions.IsolatedStoragePermission.cs
3 //
4 // Piers Haken <piersh@friskit.com>
5 //
6 // (C) 2002 Ximian, Inc.                        http://www.ximian.com
7 //
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.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.Globalization;
34 using System.Security.Permissions;
35
36 namespace System.Security.Permissions
37 {
38         [Serializable]
39         public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission
40         {
41                 internal long m_userQuota;
42                 internal long m_machineQuota;
43                 internal long m_expirationDays;
44                 internal bool m_permanentData;
45                 internal IsolatedStorageContainment m_allowed;
46
47                 public IsolatedStoragePermission (PermissionState state)
48                 {
49                         if (state == PermissionState.None)
50                         {
51                                 m_userQuota = 0;
52                                 m_machineQuota = 0;
53                                 m_expirationDays = 0;
54                                 m_permanentData = false;
55                                 m_allowed = IsolatedStorageContainment.None;
56                         }
57                         else if (state == PermissionState.Unrestricted)
58                         {
59                                 m_userQuota = Int64.MaxValue;
60                                 m_machineQuota = Int64.MaxValue;
61                                 m_expirationDays = Int64.MaxValue ;
62                                 m_permanentData = true;
63                                 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
64                         }
65                         else
66                         {
67                                 throw new ArgumentException("Invalid Permission state");
68                         }
69                 }
70
71                 public long UserQuota
72                 {
73                         set { m_userQuota = value; }
74                         get { return m_userQuota; }
75                 }
76
77
78                 public IsolatedStorageContainment UsageAllowed
79                 {
80                         set { m_allowed = value; }
81                         get { return m_allowed; }
82                 }
83
84
85                 public bool IsUnrestricted ()
86                 {
87                         return IsolatedStorageContainment.UnrestrictedIsolatedStorage == m_allowed;
88                 }
89
90                 public override SecurityElement ToXml ()
91                 {
92                         SecurityElement e = new SecurityElement ("IPermission");
93                         e.AddAttribute ("class", GetType ().AssemblyQualifiedName);
94                         e.AddAttribute ("version", "1");
95
96                         if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
97                                 e.AddAttribute ("Unrestricted", "true");
98
99                         else if (m_allowed == IsolatedStorageContainment.None)
100                                 e.AddAttribute ("Allowed", "None");
101                         
102                         return e;
103                 }
104
105
106                 public override void FromXml (SecurityElement esd)
107                 {
108                         if (esd == null)
109                                 throw new ArgumentNullException (
110                                         Locale.GetText ("The argument is null."));
111                         
112                         if (esd.Attribute ("class") != GetType ().AssemblyQualifiedName)
113                                 throw new ArgumentException (
114                                         Locale.GetText ("The argument is not valid"));
115
116                         if (esd.Attribute ("version") != "1")
117                                 throw new ArgumentException (
118                                         Locale.GetText ("The argument is not valid"));
119                         
120                         if (esd.Attribute ("Unrestricted") == "true")
121                                 m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
122
123                         else if (esd.Attribute ("Allowed") == "None")
124                                 m_allowed = IsolatedStorageContainment.None;
125                 }
126         }
127 }
128
129
130
131
132