2
0

IsolatedStorageFilePermission.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // System.Security.Permissions.IsolatedStorageFilePermission.cs
  3. //
  4. // Author
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2003 Motus Technologies. http://www.motus.com
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Runtime.InteropServices;
  30. namespace System.Security.Permissions {
  31. [Serializable]
  32. #if NET_2_0
  33. [ComVisible (true)]
  34. #endif
  35. public sealed class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission {
  36. private const int version = 1;
  37. // Constructors
  38. public IsolatedStorageFilePermission (PermissionState state)
  39. : base (state)
  40. {
  41. }
  42. // Properties
  43. // Methods
  44. public override IPermission Copy ()
  45. {
  46. IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
  47. p.m_userQuota = m_userQuota;
  48. p.m_machineQuota = m_machineQuota;
  49. p.m_expirationDays = m_expirationDays;
  50. p.m_permanentData = m_permanentData;
  51. p.m_allowed = m_allowed;
  52. return p;
  53. }
  54. public override IPermission Intersect (IPermission target)
  55. {
  56. IsolatedStorageFilePermission isfp = Cast (target);
  57. if (isfp == null)
  58. return null;
  59. if (IsEmpty () && isfp.IsEmpty ())
  60. return null;
  61. IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
  62. p.m_userQuota = (m_userQuota < isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
  63. p.m_machineQuota = (m_machineQuota < isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
  64. p.m_expirationDays = (m_expirationDays < isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
  65. p.m_permanentData = (m_permanentData && isfp.m_permanentData);
  66. // UsageAllowed == Unrestricted is a special case handled by the property
  67. p.UsageAllowed = (m_allowed < isfp.m_allowed) ? m_allowed : isfp.m_allowed;
  68. return p;
  69. }
  70. public override bool IsSubsetOf (IPermission target)
  71. {
  72. IsolatedStorageFilePermission isfp = Cast (target);
  73. if (isfp == null)
  74. return IsEmpty ();
  75. if (isfp.IsUnrestricted ())
  76. return true;
  77. if (m_userQuota > isfp.m_userQuota)
  78. return false;
  79. if (m_machineQuota > isfp.m_machineQuota)
  80. return false;
  81. if (m_expirationDays > isfp.m_expirationDays)
  82. return false;
  83. if (m_permanentData != isfp.m_permanentData)
  84. return false;
  85. if (m_allowed > isfp.m_allowed)
  86. return false;
  87. return true;
  88. }
  89. public override IPermission Union (IPermission target)
  90. {
  91. IsolatedStorageFilePermission isfp = Cast (target);
  92. if (isfp == null)
  93. return Copy ();
  94. IsolatedStorageFilePermission p = new IsolatedStorageFilePermission (PermissionState.None);
  95. p.m_userQuota = (m_userQuota > isfp.m_userQuota) ? m_userQuota : isfp.m_userQuota;
  96. p.m_machineQuota = (m_machineQuota > isfp.m_machineQuota) ? m_machineQuota : isfp.m_machineQuota;
  97. p.m_expirationDays = (m_expirationDays > isfp.m_expirationDays) ? m_expirationDays : isfp.m_expirationDays;
  98. p.m_permanentData = (m_permanentData || isfp.m_permanentData);
  99. // UsageAllowed == Unrestricted is a special case handled by the property
  100. p.UsageAllowed = (m_allowed > isfp.m_allowed) ? m_allowed : isfp.m_allowed;
  101. return p;
  102. }
  103. #if NET_2_0
  104. [MonoTODO ("(2.0) new override - something must have been added ???")]
  105. [ComVisible (false)]
  106. public override SecurityElement ToXml ()
  107. {
  108. return base.ToXml ();
  109. }
  110. #endif
  111. // IBuiltInPermission
  112. int IBuiltInPermission.GetTokenIndex ()
  113. {
  114. return (int) BuiltInToken.IsolatedStorageFile;
  115. }
  116. // helpers
  117. private IsolatedStorageFilePermission Cast (IPermission target)
  118. {
  119. if (target == null)
  120. return null;
  121. IsolatedStorageFilePermission isfp = (target as IsolatedStorageFilePermission);
  122. if (isfp == null) {
  123. ThrowInvalidPermission (target, typeof (IsolatedStorageFilePermission));
  124. }
  125. return isfp;
  126. }
  127. }
  128. }