2
0

ScriptReferenceBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Author:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2009-2011 Novell, Inc. http://novell.com/
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Collections.Concurrent;
  28. using System.IO;
  29. using System.Text;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. using System.Security.Permissions;
  33. using System.Threading;
  34. using System.Web.UI.WebControls;
  35. namespace System.Web.UI
  36. {
  37. [AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public abstract class ScriptReferenceBase
  40. {
  41. static SplitOrderedList <ResourceCacheEntry, bool> resourceCache;
  42. string _path;
  43. public bool NotifyScriptLoaded {
  44. get; set;
  45. }
  46. public string Path {
  47. get { return _path != null ? _path : String.Empty; }
  48. set { _path = value; }
  49. }
  50. [TypeConverterAttribute(typeof(StringArrayConverter))]
  51. public string[] ResourceUICultures {
  52. get; set;
  53. }
  54. public ScriptMode ScriptMode {
  55. get; set;
  56. }
  57. internal static Assembly ThisAssembly {
  58. get; private set;
  59. }
  60. static ScriptReferenceBase ()
  61. {
  62. ThisAssembly = typeof (ScriptReferenceBase).Assembly;
  63. resourceCache = new SplitOrderedList <ResourceCacheEntry, bool> (EqualityComparer <ResourceCacheEntry>.Default);
  64. }
  65. protected ScriptReferenceBase ()
  66. {
  67. this.NotifyScriptLoaded = true;
  68. this.ScriptMode = ScriptMode.Auto;
  69. }
  70. #if NET_4_0
  71. protected internal virtual bool IsAjaxFrameworkScript (ScriptManager scriptManager)
  72. {
  73. return false;
  74. }
  75. [Obsolete ("Use IsAjaxFrameworkScript(ScriptManager)")]
  76. #endif
  77. protected internal abstract bool IsFromSystemWebExtensions ();
  78. protected internal abstract string GetUrl (ScriptManager scriptManager, bool zip);
  79. // This method is an example of particularily bad coding - .NET performs NO checks
  80. // on pathOrName!
  81. protected static string ReplaceExtension (string pathOrName)
  82. {
  83. // emulate .NET behavior
  84. if (pathOrName == null)
  85. throw new NullReferenceException ();
  86. // We should check the length, but since .NET doesn't do that, we won't
  87. // either. Ugh.
  88. return pathOrName.Substring (0, pathOrName.Length - 2) + "debug.js";
  89. }
  90. internal static string GetScriptName (string releaseName, bool isDebugMode, string [] supportedUICultures, Assembly assembly, out WebResourceAttribute wra)
  91. {
  92. if (assembly != null)
  93. VerifyAssemblyContainsResource (assembly, releaseName, out wra);
  94. else
  95. wra = null;
  96. if (!isDebugMode && (supportedUICultures == null || supportedUICultures.Length == 0))
  97. return releaseName;
  98. if (releaseName.Length < 3 || !releaseName.EndsWith (".js", StringComparison.OrdinalIgnoreCase))
  99. throw new InvalidOperationException (String.Format ("'{0}' is not a valid script path. The path must end in '.js'.", releaseName));
  100. StringBuilder sb = new StringBuilder (releaseName);
  101. sb.Length -= 3;
  102. if (isDebugMode)
  103. sb.Append (".debug");
  104. string culture = Thread.CurrentThread.CurrentUICulture.Name;
  105. if (supportedUICultures != null && Array.IndexOf<string> (supportedUICultures, culture) >= 0)
  106. sb.AppendFormat (".{0}", culture);
  107. sb.Append (".js");
  108. string ret = sb.ToString ();
  109. WebResourceAttribute debugWra;
  110. if (!CheckIfAssemblyContainsResource (assembly, ret, out debugWra))
  111. return releaseName;
  112. wra = debugWra;
  113. return ret;
  114. }
  115. static void VerifyAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra)
  116. {
  117. var rce = new ResourceCacheEntry {
  118. Assembly = assembly,
  119. ResourceName = resourceName
  120. };
  121. WebResourceAttribute attr = null;
  122. if (!resourceCache.InsertOrGet ((uint)rce.GetHashCode (), rce, false, () => CheckIfAssemblyContainsResource (assembly, resourceName, out attr)))
  123. throw new InvalidOperationException (String.Format ("Assembly '{0}' does not contain a Web resource with name '{1}'.",
  124. assembly.FullName, resourceName));
  125. wra = attr;
  126. }
  127. static bool CheckIfAssemblyContainsResource (Assembly assembly, string resourceName, out WebResourceAttribute wra)
  128. {
  129. foreach (WebResourceAttribute attr in assembly.GetCustomAttributes (typeof (WebResourceAttribute), false)) {
  130. if (String.Compare (resourceName, attr.WebResource, StringComparison.Ordinal) == 0) {
  131. using (Stream rs = assembly.GetManifestResourceStream (resourceName)) {
  132. if (rs == null)
  133. throw new InvalidOperationException (
  134. String.Format ("Assembly '{0}' contains a Web resource with name '{1}' but does not contain an embedded resource with name '{1}'.",
  135. assembly.FullName, resourceName)
  136. );
  137. }
  138. wra = attr;
  139. return true;
  140. }
  141. }
  142. wra = null;
  143. return false;
  144. }
  145. sealed class ResourceCacheEntry
  146. {
  147. public Assembly Assembly;
  148. public string ResourceName;
  149. public override int GetHashCode ()
  150. {
  151. int ret = 0;
  152. if (Assembly != null)
  153. ret ^= Assembly.GetHashCode ();
  154. if (ResourceName != null)
  155. ret ^= ResourceName.GetHashCode ();
  156. return ret;
  157. }
  158. }
  159. }
  160. }