Merge pull request #3622 from rolfbjarne/remove-stray-file
[mono.git] / mcs / class / corlib / System.Security.Permissions / IsolatedStoragePermission.cs
index 87c676873da78e9a9d3f1a6c01604ac2c9f03673..d4450eb3ec7318056df36babfdc68bf603dc043b 100644 (file)
@@ -1,13 +1,12 @@
 //
 // System.Security.Permissions.IsolatedStoragePermission.cs
 //
-// Piers Haken <piersh@friskit.com>
+// Authors:
+//     Piers Haken <piersh@friskit.com>
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2002 Ximian, Inc.                       http://www.ximian.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Globalization;
-using System.Security.Permissions;
+using System.Runtime.InteropServices;
+
+namespace System.Security.Permissions {
 
-namespace System.Security.Permissions
-{
        [Serializable]
-       public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission
-       {
+       [SecurityPermission (SecurityAction.InheritanceDemand, ControlEvidence = true, ControlPolicy = true)]
+       [ComVisible (true)]
+       public abstract class IsolatedStoragePermission : CodeAccessPermission, IUnrestrictedPermission {
+
+               private const int version = 1;
+
                internal long m_userQuota;
                internal long m_machineQuota;
                internal long m_expirationDays;
                internal bool m_permanentData;
                internal IsolatedStorageContainment m_allowed;
 
-               public IsolatedStoragePermission (PermissionState state)
+               protected IsolatedStoragePermission (PermissionState state)
                {
-                       if (state == PermissionState.None)
-                       {
-                               m_userQuota = 0;
-                               m_machineQuota = 0;
-                               m_expirationDays = 0;
-                               m_permanentData = false;
-                               m_allowed = IsolatedStorageContainment.None;
-                       }
-                       else if (state == PermissionState.Unrestricted)
-                       {
-                               m_userQuota = Int64.MaxValue;
-                               m_machineQuota = Int64.MaxValue;
-                               m_expirationDays = Int64.MaxValue ;
-                               m_permanentData = true;
-                               m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
-                       }
-                       else
-                       {
-                               throw new ArgumentException("Invalid Permission state");
+                       if (CheckPermissionState (state, true) == PermissionState.Unrestricted) {
+                               UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
                        }
                }
 
-               public long UserQuota
-               {
-                       set { m_userQuota = value; }
+               public long UserQuota {
                        get { return m_userQuota; }
+                       set { m_userQuota = value; }
                }
 
-
-               public IsolatedStorageContainment UsageAllowed
-               {
-                       set { m_allowed = value; }
+               public IsolatedStorageContainment UsageAllowed {
                        get { return m_allowed; }
+                       set {
+                               if (!Enum.IsDefined (typeof (IsolatedStorageContainment), value)) {
+                                       string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
+                                       throw new ArgumentException (msg, "IsolatedStorageContainment");
+                               }
+                               m_allowed = value;
+                               if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage) {
+                                       m_userQuota = Int64.MaxValue;
+                                       m_machineQuota = Int64.MaxValue;
+                                       m_expirationDays = Int64.MaxValue ;
+                                       m_permanentData = true;
+                               }
+                       }
                }
 
 
@@ -89,44 +83,53 @@ namespace System.Security.Permissions
 
                public override SecurityElement ToXml ()
                {
-                       SecurityElement e = new SecurityElement ("IPermission");
-                       e.AddAttribute ("class", GetType ().AssemblyQualifiedName);
-                       e.AddAttribute ("version", "1");
+                       SecurityElement se = Element (version);
 
                        if (m_allowed == IsolatedStorageContainment.UnrestrictedIsolatedStorage)
-                               e.AddAttribute ("Unrestricted", "true");
-
-                       else if (m_allowed == IsolatedStorageContainment.None)
-                               e.AddAttribute ("Allowed", "None");
+                               se.AddAttribute ("Unrestricted", "true");
+                       else {
+                               se.AddAttribute ("Allowed", m_allowed.ToString ());
+                               if (m_userQuota > 0)
+                                       se.AddAttribute ("UserQuota", m_userQuota.ToString ());
+                       }
                        
-                       return e;
+                       return se;
                }
 
-
                public override void FromXml (SecurityElement esd)
                {
-                       if (esd == null)
-                               throw new ArgumentNullException (
-                                       Locale.GetText ("The argument is null."));
-                       
-                       if (esd.Attribute ("class") != GetType ().AssemblyQualifiedName)
-                               throw new ArgumentException (
-                                       Locale.GetText ("The argument is not valid"));
+                       // General validation in CodeAccessPermission
+                       CheckSecurityElement (esd, "esd", version, version);
+                       // Note: we do not (yet) care about the return value 
+                       // as we only accept version 1 (min/max values)
+
+                       m_userQuota = 0;
+                       m_machineQuota = 0;
+                       m_expirationDays = 0;
+                       m_permanentData = false;
+                       m_allowed = IsolatedStorageContainment.None;
+
+                       if (IsUnrestricted (esd)) {
+                               UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
+                       } else {
+                               string a = esd.Attribute ("Allowed");
+                               if (a != null) {
+                                       UsageAllowed = (IsolatedStorageContainment) Enum.Parse (
+                                               typeof (IsolatedStorageContainment), a);
+                               }
+                               a = esd.Attribute ("UserQuota");
+                               if (a != null) {
+                                       m_userQuota = Int64.Parse (a, CultureInfo.InvariantCulture);
+                               }
+                       }
+               }
 
-                       if (esd.Attribute ("version") != "1")
-                               throw new ArgumentException (
-                                       Locale.GetText ("The argument is not valid"));
-                       
-                       if (esd.Attribute ("Unrestricted") == "true")
-                               m_allowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
+               // helpers
 
-                       else if (esd.Attribute ("Allowed") == "None")
-                               m_allowed = IsolatedStorageContainment.None;
+               internal bool IsEmpty ()
+               {
+                       // should we include internals ? or just publics ?
+                       return ((m_userQuota == 0) && (m_allowed == IsolatedStorageContainment.None));
                }
        }
 }
-
-
-
-
-