/DLR_Main/Hosts/SilverLight/Microsoft.Scripting.Silverlight/BrowserScriptHost.cs

https://bitbucket.org/mdavid/dlr · C# · 170 lines · 103 code · 23 blank · 44 comment · 11 complexity · 60e008494a117d6181fe44dbad3dc24d MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if CLR2
  16. using Microsoft.Scripting.Utils;
  17. #endif
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Reflection;
  22. using System.Windows;
  23. using System.Windows.Resources;
  24. using Microsoft.Scripting.Hosting;
  25. namespace Microsoft.Scripting.Silverlight {
  26. /// <summary>
  27. /// ScriptHost for use inside the browser.
  28. /// </summary>
  29. public sealed class BrowserScriptHost : ScriptHost {
  30. public BrowserScriptHost() {
  31. }
  32. public override PlatformAdaptationLayer/*!*/ PlatformAdaptationLayer {
  33. get {
  34. return BrowserPAL.PAL;
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// Base class for all browser-based PlatformAdaptationLayers.
  40. /// Delegates compatible operations to a BrowserVirtualFileSystem.
  41. /// </summary>
  42. // BUG: should be internal, but Ruby is refusing to call members if so
  43. public abstract class BrowserPAL : PlatformAdaptationLayer {
  44. public BrowserVirtualFilesystem VirtualFilesystem { get; internal set; }
  45. protected static BrowserPAL _PAL;
  46. internal static BrowserPAL PAL {
  47. get {
  48. if (_PAL == null) PAL = HttpPAL.PAL;
  49. return _PAL;
  50. }
  51. set { _PAL = value; }
  52. }
  53. /// <summary>
  54. /// Get the virtual filesystem's current storage unit. It is "object"
  55. /// based since the CurrentStorageUnit can be different types.
  56. /// </summary>
  57. public object CurrentStorageUnit {
  58. get { return VirtualFilesystem.CurrentStorageUnit; }
  59. set { VirtualFilesystem.CurrentStorageUnit = value; }
  60. }
  61. /// <summary>
  62. /// Executes "action" in the context of the "storageUnit".
  63. /// </summary>
  64. /// <param name="xapFile"></param>
  65. /// <param name="action"></param>
  66. public void UsingStorageUnit(object storageUnit, Action action) {
  67. VirtualFilesystem.UsingStorageUnit(storageUnit, action);
  68. }
  69. public override bool FileExists(string path) {
  70. return VirtualFilesystem.GetFile(path) != null;
  71. }
  72. public override Assembly LoadAssemblyFromPath(string path) {
  73. Stream stream = VirtualFilesystem.GetFile(path);
  74. if (stream == null) {
  75. throw new FileNotFoundException(
  76. string.Format("could not find assembly: {0} (check the {1})",
  77. path, VirtualFilesystem.Name()
  78. )
  79. );
  80. }
  81. return new AssemblyPart().Load(stream);
  82. }
  83. /// <exception cref="ArgumentException">Invalid path.</exception>
  84. public override string/*!*/ GetFullPath(string/*!*/ path) {
  85. return VirtualFilesystem.NormalizePath(path);
  86. }
  87. /// <exception cref="ArgumentException">Invalid path.</exception>
  88. public override bool IsAbsolutePath(string/*!*/ path) {
  89. return PathToUri(path).IsAbsoluteUri;
  90. }
  91. public override Stream OpenInputFileStream(string path) {
  92. Stream result = VirtualFilesystem.GetFile(GetFullPath(path));
  93. if (result == null)
  94. throw new IOException(
  95. String.Format("file {0} not found (check the {1})",
  96. path, VirtualFilesystem.Name()
  97. )
  98. );
  99. return result;
  100. }
  101. public override Stream OpenInputFileStream
  102. (string path, FileMode mode, FileAccess access, FileShare share) {
  103. if (mode != FileMode.Open || access != FileAccess.Read) {
  104. throw new IOException(
  105. string.Format("can only read files from the {0}",
  106. VirtualFilesystem.Name()
  107. )
  108. );
  109. }
  110. return OpenInputFileStream(path);
  111. }
  112. public override Stream OpenInputFileStream
  113. (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) {
  114. return OpenInputFileStream(path, mode, access, share);
  115. }
  116. /// <summary>
  117. /// Convert a string path to a Uri
  118. /// </summary>
  119. /// <param name="path">relative or absolute path</param>
  120. /// <returns>normalized URI</returns>
  121. private Uri/*!*/ PathToUri(string/*!*/ path) {
  122. try {
  123. return new Uri(VirtualFilesystem.NormalizePath(path), UriKind.RelativeOrAbsolute);
  124. } catch (UriFormatException e) {
  125. throw new ArgumentException("The specified path is invalid", e);
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// PlatformAdaptationLayer for use with a read-only XAP file.
  131. /// </summary>
  132. internal sealed class XapPAL : BrowserPAL {
  133. internal static new readonly BrowserPAL PAL = new XapPAL();
  134. private XapPAL() {
  135. VirtualFilesystem = new XapVirtualFilesystem();
  136. }
  137. }
  138. /// <summary>
  139. /// PlatformAdaptationLayer to download and cache files over HTTP.
  140. /// </summary>
  141. internal sealed class HttpPAL : BrowserPAL {
  142. internal static new readonly BrowserPAL PAL = new HttpPAL();
  143. private HttpPAL() {
  144. VirtualFilesystem = new HttpVirtualFilesystem();
  145. }
  146. }
  147. }