Exemple #1
0
		public FontSource (Stream stream)
		{
			if (stream != null)
				wrapper = new StreamWrapper (stream);
			else
				wrapper = null;
			
			type = FontSourceType.ManagedStream;
		}
Exemple #2
0
		public void SetSource (Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException ("stream");

			Source = null;
			wrapper = new StreamWrapper (stream);
			ManagedStreamCallbacks callbacks = wrapper.GetCallbacks ();
			NativeMethods.media_element_set_stream_source (this.native, ref callbacks);
		}
Exemple #3
0
		// note: throwing MoonException from here is NOT ok since this code is called async
		// and the exception won't be reported, directly, to the caller
		void AssemblyGetResponse (IAsyncResult result)
		{
			Assembly asm;
			object[] tuple = (object []) result.AsyncState;
			WebRequest wreq = (WebRequest) tuple [0];
			ManifestAssemblyKind kind = (ManifestAssemblyKind) tuple [1];
			int error_code = (kind == ManifestAssemblyKind.ExternalAssembly) ? 2152 : 2105;
			try {
				HttpWebResponse wresp = (HttpWebResponse) wreq.EndGetResponse (result);

				if (wresp.StatusCode != HttpStatusCode.OK) {
					wresp.Close ();
					EmitError (error_code, String.Format ("Error while downloading the '{0}'.", wreq.RequestUri));
					return;
				}

				if ((kind != ManifestAssemblyKind.ExternalAssembly) && (wresp.ResponseUri != wreq.RequestUri)) {
					wresp.Close ();
					EmitError (error_code, "Redirection not allowed to download assemblies.");
					return;
				}

				using (Stream responseStream = wresp.GetResponseStream ()) {
					byte [] buffer = AssemblyPart.StreamToBuffer (responseStream);

					if (IsZip (buffer)) {
						// unzip it.
						using (MemoryStream dest = new MemoryStream ()) {
							using (MemoryStream source = new MemoryStream (buffer)) {
								ManagedStreamCallbacks source_cb;
								ManagedStreamCallbacks dest_cb;
								StreamWrapper source_wrapper;
								StreamWrapper dest_wrapper;

								source_wrapper = new StreamWrapper (source);
								dest_wrapper = new StreamWrapper (dest);

								source_cb = source_wrapper.GetCallbacks ();
								dest_cb = dest_wrapper.GetCallbacks ();

								// Zip files may contain multiple assemblies, all of which need to be loaded. Keep
								// attempting to open subsequent files until it fails.
								for (int i = 0; ; i ++) {
									if (!NativeMethods.managed_unzip_stream_to_stream_nth_file (ref source_cb, ref dest_cb, i))
										break;
									if (Load (dest.ToArray (), kind) == null)
										EmitError (2153, String.Format ("Error while loading '{0}'.", wreq.RequestUri));
									source.Position = 0;
									dest.SetLength (0);
								}
							}
						}
					} else {
						if (Load (buffer, kind) == null)
							EmitError (2153, String.Format ("Error while loading '{0}'.", wreq.RequestUri));
					}
				}
				Dispatcher.BeginInvoke (AsyncDownloadComplete);
			}
			catch (Exception e) {
				// we need to report everything since any error means CreateApplication won't be called
				EmitError (error_code, e.ToString ());
			}
		}
Exemple #4
0
		// note: throwing MoonException from here is NOT ok since this code is called async
		// and the exception won't be reported, directly, to the caller
		void AssemblyGetResponse (IAsyncResult result)
		{
			object[] tuple = (object []) result.AsyncState;
			WebRequest wreq = (WebRequest) tuple [0];
			int error_code = (int) tuple [1];
			try {
				HttpWebResponse wresp = (HttpWebResponse) wreq.EndGetResponse (result);

				if (wresp.StatusCode != HttpStatusCode.OK) {
					wresp.Close ();
					EmitError (error_code, String.Format ("Error while downloading the '{0}'.", wreq.RequestUri));
					return;
				}

				if (wresp.ResponseUri != wreq.RequestUri) {
					wresp.Close ();
					EmitError (error_code, "Redirection not allowed to download assemblies.");
					return;
				}

				Stream responseStream = wresp.GetResponseStream ();

				AssemblyPart a = new AssemblyPart ();
				Assembly asm = a.Load (responseStream);

				if (asm == null) {
					// it's not a valid assembly, try to unzip it.
					using (MemoryStream ms = new MemoryStream ()) {
						ManagedStreamCallbacks source_cb;
						ManagedStreamCallbacks dest_cb;
						StreamWrapper source_wrapper;
						StreamWrapper dest_wrapper;

						responseStream.Seek (0, SeekOrigin.Begin);

						source_wrapper = new StreamWrapper (responseStream);
						dest_wrapper = new StreamWrapper (ms);

						source_cb = source_wrapper.GetCallbacks ();
						dest_cb = dest_wrapper.GetCallbacks ();

						// the zip files I've come across have a single file in them, the
						// dll.  so we assume that any/every zip file will contain a single
						// file, and just get the first one from the zip file directory.
						if (NativeMethods.managed_unzip_stream_to_stream_first_file (ref source_cb, ref dest_cb)) {
							ms.Seek (0, SeekOrigin.Begin);
							asm = a.Load (ms);
						}
					}
				}

				wresp.Close ();

				if (asm != null)
					Dispatcher.BeginInvoke (new AssemblyRegistration (AssemblyRegister), asm);
				else
					EmitError (2153, String.Format ("Error while loading '{0}'.", wreq.RequestUri));
			}
			catch (Exception e) {
				// we need to report everything since any error means CreateApplication won't be called
				EmitError (error_code, e.ToString ());
			}
		}
Exemple #5
0
		public static StreamResourceInfo GetResourceStream (StreamResourceInfo zipPackageStreamResourceInfo, Uri uriResource)
		{
			if (zipPackageStreamResourceInfo == null)
				throw new ArgumentNullException ("zipPackageStreamResourceInfo");
			if (uriResource == null)
				throw new ArgumentNullException ("resourceUri");
			
			MemoryStream ms = new MemoryStream ();
			ManagedStreamCallbacks source_cb;
			ManagedStreamCallbacks dest_cb;
			StreamWrapper source_wrapper;
			StreamWrapper dest_wrapper;
			Stream source;

			source = zipPackageStreamResourceInfo.Stream;

			source_wrapper = new StreamWrapper (source);
			dest_wrapper = new StreamWrapper (ms);

			source_cb = source_wrapper.GetCallbacks ();
			dest_cb = dest_wrapper.GetCallbacks ();

			if (NativeMethods.managed_unzip_stream_to_stream (ref source_cb, ref dest_cb, uriResource.ToString ())) {
				if (source.CanSeek)
					source.Seek (0, SeekOrigin.Begin);
				ms.Seek (0, SeekOrigin.Begin);
				return new StreamResourceInfo (ms, null);
			}

			return null;
		}