PageRenderTime 39ms CodeModel.GetById 19ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 16#if CLR2
 17using Microsoft.Scripting.Utils;
 18#endif
 19using System;
 20using System.Collections.Generic;
 21using System.IO;
 22using System.Reflection;
 23using System.Windows;
 24using System.Windows.Resources;
 25using Microsoft.Scripting.Hosting;
 26
 27namespace Microsoft.Scripting.Silverlight {
 28
 29    /// <summary>
 30    /// ScriptHost for use inside the browser.
 31    /// </summary>
 32    public sealed class BrowserScriptHost : ScriptHost {
 33
 34        public BrowserScriptHost() {
 35        }
 36
 37        public override PlatformAdaptationLayer/*!*/ PlatformAdaptationLayer {
 38            get {
 39                return BrowserPAL.PAL;
 40            }
 41        }
 42    }
 43
 44    /// <summary>
 45    /// Base class for all browser-based PlatformAdaptationLayers. 
 46    /// Delegates compatible operations to a BrowserVirtualFileSystem.
 47    /// </summary>
 48    // BUG: should be internal, but Ruby is refusing to call members if so
 49    public abstract class BrowserPAL : PlatformAdaptationLayer {
 50
 51        public BrowserVirtualFilesystem VirtualFilesystem { get; internal set; }
 52
 53        protected static BrowserPAL _PAL;
 54        internal static BrowserPAL PAL {
 55            get {
 56                if (_PAL == null) PAL = HttpPAL.PAL;
 57                return _PAL;
 58            }
 59            set { _PAL = value; }
 60        }
 61
 62        /// <summary>
 63        /// Get the virtual filesystem's current storage unit. It is "object"
 64        /// based since the CurrentStorageUnit can be different types.
 65        /// </summary>
 66        public object CurrentStorageUnit {
 67            get { return VirtualFilesystem.CurrentStorageUnit; }
 68            set { VirtualFilesystem.CurrentStorageUnit = value; }
 69        }
 70
 71        /// <summary>
 72        /// Executes "action" in the context of the "storageUnit". 
 73        /// </summary>
 74        /// <param name="xapFile"></param>
 75        /// <param name="action"></param>
 76        public void UsingStorageUnit(object storageUnit, Action action) {
 77            VirtualFilesystem.UsingStorageUnit(storageUnit, action);
 78        }
 79
 80        public override bool FileExists(string path) {
 81            return VirtualFilesystem.GetFile(path) != null;
 82        }
 83
 84        public override Assembly LoadAssemblyFromPath(string path) {
 85            Stream stream = VirtualFilesystem.GetFile(path);
 86            if (stream == null) {
 87                throw new FileNotFoundException(
 88                    string.Format("could not find assembly: {0} (check the {1})", 
 89                        path, VirtualFilesystem.Name()
 90                    )
 91                );
 92            }
 93            return new AssemblyPart().Load(stream);
 94        }
 95
 96        /// <exception cref="ArgumentException">Invalid path.</exception>
 97        public override string/*!*/ GetFullPath(string/*!*/ path) {
 98            return VirtualFilesystem.NormalizePath(path);
 99        }
100
101        /// <exception cref="ArgumentException">Invalid path.</exception>
102        public override bool IsAbsolutePath(string/*!*/ path) {
103            return PathToUri(path).IsAbsoluteUri;
104        }
105
106        public override Stream OpenInputFileStream(string path) {
107            Stream result = VirtualFilesystem.GetFile(GetFullPath(path));
108            if (result == null)
109                throw new IOException(
110                    String.Format("file {0} not found (check the {1})", 
111                        path, VirtualFilesystem.Name()
112                    )
113                );
114            return result;
115        }
116
117        public override Stream OpenInputFileStream
118        (string path, FileMode mode, FileAccess access, FileShare share) {
119            if (mode != FileMode.Open || access != FileAccess.Read) {
120                throw new IOException(
121                    string.Format("can only read files from the {0}",
122                        VirtualFilesystem.Name()
123                    )
124                );
125            }
126            return OpenInputFileStream(path);
127        }
128
129        public override Stream OpenInputFileStream
130        (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) {
131            return OpenInputFileStream(path, mode, access, share);
132        }
133
134        /// <summary>
135        /// Convert a string path to a Uri
136        /// </summary>
137        /// <param name="path">relative or absolute path</param>
138        /// <returns>normalized URI</returns>
139        private Uri/*!*/ PathToUri(string/*!*/ path) {
140            try {
141                return new Uri(VirtualFilesystem.NormalizePath(path), UriKind.RelativeOrAbsolute);
142            } catch (UriFormatException e) {
143                throw new ArgumentException("The specified path is invalid", e);
144            }
145        }
146    }
147
148    /// <summary>
149    /// PlatformAdaptationLayer for use with a read-only XAP file.
150    /// </summary>
151    internal sealed class XapPAL : BrowserPAL {
152        internal static new readonly BrowserPAL PAL = new XapPAL();
153
154        private XapPAL() {
155            VirtualFilesystem = new XapVirtualFilesystem();
156        }
157    }
158
159    /// <summary>
160    /// PlatformAdaptationLayer to download and cache files over HTTP.
161    /// </summary>
162    internal sealed class HttpPAL : BrowserPAL {
163        internal static new readonly BrowserPAL PAL = new HttpPAL();
164
165        private HttpPAL() {
166            VirtualFilesystem = new HttpVirtualFilesystem();
167        }
168    }
169}
170