private static Uri GetStructureUriFromRelationship(Uri contentUri, string relationshipName) { Uri result = null; if (contentUri != null && relationshipName != null) { Uri partUri = PackUriHelper.GetPartUri(contentUri); if (partUri != null) { Uri packageUri = PackUriHelper.GetPackageUri(contentUri); Package package = PreloadedPackages.GetPackage(packageUri); if (package == null && SecurityHelper.CheckEnvironmentPermission()) { package = PackageStore.GetPackage(packageUri); } if (package != null) { PackagePart part = package.GetPart(partUri); PackageRelationshipCollection relationshipsByType = part.GetRelationshipsByType(relationshipName); Uri uri = null; foreach (PackageRelationship packageRelationship in relationshipsByType) { uri = PackUriHelper.ResolvePartUri(partUri, packageRelationship.TargetUri); } if (uri != null) { result = PackUriHelper.Create(packageUri, uri); } } } } return(result); }
WebRequest IWebRequestCreate.Create(Uri uri) { if (uri == null) { throw new ArgumentNullException("uri"); } // Ensure uri is absolute - if we don't check now, the get_Scheme property will throw // InvalidOperationException which would be misleading to the caller. if (!uri.IsAbsoluteUri) { throw new ArgumentException(SR.Get(SRID.UriMustBeAbsolute), "uri"); } // Ensure uri is correct scheme because we can be called directly. Case sensitive // is fine because Uri.Scheme contract is to return in lower case only. if (String.Compare(uri.Scheme, PackUriHelper.UriSchemePack, StringComparison.Ordinal) != 0) { throw new ArgumentException(SR.Get(SRID.UriSchemeMismatch, PackUriHelper.UriSchemePack), "uri"); } #if DEBUG if (_traceSwitch.Enabled) { System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebRequestFactory - responding to uri: " + uri); } #endif // only inspect cache if part name is present because cache only contains an object, not // the stream it was derived from Uri packageUri; Uri partUri; PackUriHelper.ValidateAndGetPackUriComponents(uri, out packageUri, out partUri); if (partUri != null) { // Note: we look at PreloadedPackages first before we examine the PackageStore // This is to make sure that an app cannot override any predefine packages // match cached object by authority component only - ignore the local path (part name) // inspect local package cache and default to that if possible // All predefined packages such as a package activated by DocumentApplication, // ResourceContainer, and SiteOfOriginContainer are placed in PreloadedPackages bool cachedPackageIsThreadSafe; Package c = PreloadedPackages.GetPackage(packageUri, out cachedPackageIsThreadSafe); // If we don't find anything in the preloaded packages, look into the PackageStore bool cachedPackageIsFromPublicStore = false; if (c == null) { cachedPackageIsThreadSafe = false; // always assume PackageStore packages are not thread-safe cachedPackageIsFromPublicStore = true; // Try to get a package from the package store c = PackageStore.GetPackage(packageUri); } // do we have a package? if (c != null) { #if DEBUG if (_traceSwitch.Enabled) { System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebRequestFactory - cache hit - returning CachedPackWebRequest"); } #endif // use the cached object return(new PackWebRequest(uri, packageUri, partUri, c, cachedPackageIsFromPublicStore, cachedPackageIsThreadSafe)); } } #if DEBUG if (_traceSwitch.Enabled) { System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebRequestFactory - spawning regular PackWebRequest"); } #endif return(new PackWebRequest(uri, packageUri, partUri)); }
internal void Cleanup() { if (Application.Current != null) { IBrowserCallbackServices bcs = Application.Current.BrowserCallbackServices; if (bcs != null) { Debug.Assert(!Application.IsApplicationObjectShuttingDown); // Marshal.ReleaseComObject(bcs) has to be called so that the refcount of the // native objects goes to zero for clean shutdown. But it should not be called // right away, because there may still be DispatcherOperations in the queue // that will attempt to use IBCS, especially during downloading/activation. // Last, it can't be called with prioroty lower than Normal, because that's // the priority of Applicatoin.ShudownCallback(), which shuts down the // Dispatcher. Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new DispatcherOperationCallback(ReleaseBrowserCallback), bcs); } } ServiceProvider = null; ClearRootBrowserWindow(); if (_storageRoot != null && _storageRoot.Value != null) { _storageRoot.Value.Close(); } // Due to the dependecies the following objects have to be released // in the following order: _document, DocumentManager, // _packageStream, _unmanagedStream. if (_document.Value is PackageDocument) { // We are about to close the package ad remove it from the Preloaded Packages Store. // Let's make sure that the data structures are consistent. The package that we hold is // actually in the store under the URI that we think it should be using Debug.Assert(((PackageDocument)_document.Value).Package == PreloadedPackages.GetPackage(PackUriHelper.GetPackageUri(PackUriHelper.Create(Uri)))); // We need to remove the Package from the PreloadedPackage storage, // so that potential future requests would fail in a way of returning a null (resource not found) // rather then return a Package or stream that is already Closed PreloadedPackages.RemovePackage(PackUriHelper.GetPackageUri(PackUriHelper.Create(Uri))); ((PackageDocument)_document.Value).Dispose(); _document.Value = null; } if (_mimeType.Value == MimeType.Document) { DocumentManager.CleanUp(); } if (_packageStream.Value != null) { _packageStream.Value.Close(); } if (_unmanagedStream.Value != null) { Marshal.ReleaseComObject(_unmanagedStream.Value); _unmanagedStream = new SecurityCriticalData <object>(null); } }
/// <summary> /// rootElement == null: Load elements, validation of root element will occur in caller by checking object type or casting /// rootElement != null: Only perform validation, and expect rootElement at root of markup /// </summary> /// <param name="stream"></param> /// <param name="parentUri"></param> /// <param name="pc"></param> /// <param name="mimeType"></param> /// <param name="rootElement"></param> /// <returns></returns> private object Load(Stream stream, Uri parentUri, ParserContext pc, ContentType mimeType, string rootElement) { object obj = null; if (!DocumentMode) { // Loose XAML, just check against schema, don't check content type if (rootElement == null) { obj = XamlReader.Load(stream, pc); } } else { // inside an XPS Document. Perform maximum validation XpsSchema schema = XpsSchema.GetSchema(mimeType); Uri uri = pc.BaseUri; Uri packageUri = PackUriHelper.GetPackageUri(uri); Uri partUri = PackUriHelper.GetPartUri(uri); Package package = PreloadedPackages.GetPackage(packageUri); Uri parentPackageUri = null; if (parentUri != null) { parentPackageUri = PackUriHelper.GetPackageUri(parentUri); if (!parentPackageUri.Equals(packageUri)) { throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUriNotInSamePackage)); } } schema.ValidateRelationships(new SecurityCriticalData <Package>(package), packageUri, partUri, mimeType); if (schema.AllowsMultipleReferencesToSameUri(mimeType)) { _uniqueUriRef = null; } else { _uniqueUriRef = new Hashtable(11); } Hashtable validResources = (_validResources.Count > 0 ? _validResources.Peek() : null); if (schema.HasRequiredResources(mimeType)) { validResources = new Hashtable(11); PackagePart part = package.GetPart(partUri); PackageRelationshipCollection requiredResources = part.GetRelationshipsByType(_requiredResourceRel); foreach (PackageRelationship relationShip in requiredResources) { Uri targetUri = PackUriHelper.ResolvePartUri(partUri, relationShip.TargetUri); Uri absTargetUri = PackUriHelper.Create(packageUri, targetUri); PackagePart targetPart = package.GetPart(targetUri); if (schema.IsValidRequiredResourceMimeType(targetPart.ValidatedContentType())) { if (!validResources.ContainsKey(absTargetUri)) { validResources.Add(absTargetUri, true); } } else { if (!validResources.ContainsKey(absTargetUri)) { validResources.Add(absTargetUri, false); } } } } XpsSchemaValidator xpsSchemaValidator = new XpsSchemaValidator(this, schema, mimeType, stream, packageUri, partUri); _validResources.Push(validResources); if (rootElement != null) { xpsSchemaValidator.XmlReader.MoveToContent(); if (!rootElement.Equals(xpsSchemaValidator.XmlReader.Name)) { throw new FileFormatException(SR.Get(SRID.XpsValidatingLoaderUnsupportedMimeType)); } while (xpsSchemaValidator.XmlReader.Read()) { ; } } else { obj = XamlReader.Load(xpsSchemaValidator.XmlReader, pc, XamlParseMode.Synchronous); } _validResources.Pop(); } return(obj); }
private object Load(Stream stream, Uri parentUri, ParserContext pc, ContentType mimeType, string rootElement) { object result = null; List <Type> safeTypes = new List <Type> { typeof(ResourceDictionary) }; if (!XpsValidatingLoader.DocumentMode) { if (rootElement == null) { XmlReader reader = XmlReader.Create(stream, null, pc); result = XamlReader.Load(reader, pc, XamlParseMode.Synchronous, FrameworkCompatibilityPreferences.DisableLegacyDangerousXamlDeserializationMode, safeTypes); stream.Close(); } } else { XpsSchema schema = XpsSchema.GetSchema(mimeType); Uri baseUri = pc.BaseUri; Uri uri; Uri uri2; PackUriHelper.ValidateAndGetPackUriComponents(baseUri, out uri, out uri2); Package package = PreloadedPackages.GetPackage(uri); if (parentUri != null) { Uri packageUri = PackUriHelper.GetPackageUri(parentUri); if (!packageUri.Equals(uri)) { throw new FileFormatException(SR.Get("XpsValidatingLoaderUriNotInSamePackage")); } } schema.ValidateRelationships(new SecurityCriticalData <Package>(package), uri, uri2, mimeType); if (schema.AllowsMultipleReferencesToSameUri(mimeType)) { this._uniqueUriRef = null; } else { this._uniqueUriRef = new Hashtable(11); } Hashtable hashtable = (XpsValidatingLoader._validResources.Count > 0) ? XpsValidatingLoader._validResources.Peek() : null; if (schema.HasRequiredResources(mimeType)) { hashtable = new Hashtable(11); PackagePart part = package.GetPart(uri2); PackageRelationshipCollection relationshipsByType = part.GetRelationshipsByType(XpsValidatingLoader._requiredResourceRel); foreach (PackageRelationship packageRelationship in relationshipsByType) { Uri partUri = PackUriHelper.ResolvePartUri(uri2, packageRelationship.TargetUri); Uri key = PackUriHelper.Create(uri, partUri); PackagePart part2 = package.GetPart(partUri); if (schema.IsValidRequiredResourceMimeType(part2.ValidatedContentType)) { if (!hashtable.ContainsKey(key)) { hashtable.Add(key, true); } } else if (!hashtable.ContainsKey(key)) { hashtable.Add(key, false); } } } XpsSchemaValidator xpsSchemaValidator = new XpsSchemaValidator(this, schema, mimeType, stream, uri, uri2); XpsValidatingLoader._validResources.Push(hashtable); if (rootElement != null) { xpsSchemaValidator.XmlReader.MoveToContent(); if (!rootElement.Equals(xpsSchemaValidator.XmlReader.Name)) { throw new FileFormatException(SR.Get("XpsValidatingLoaderUnsupportedMimeType")); } while (xpsSchemaValidator.XmlReader.Read()) { } } else { result = XamlReader.Load(xpsSchemaValidator.XmlReader, pc, XamlParseMode.Synchronous, FrameworkCompatibilityPreferences.DisableLegacyDangerousXamlDeserializationMode, safeTypes); } XpsValidatingLoader._validResources.Pop(); } return(result); }