internal DeferrableContent(Stream stream, Baml2006SchemaContext schemaContext, 
     IXamlObjectWriterFactory objectWriterFactory, IServiceProvider serviceProvider,
     object rootObject)
 {
     ObjectWriterParentSettings = objectWriterFactory.GetParentSettings();
     if (ObjectWriterParentSettings.AccessLevel != null)
     {
         XamlLoadPermission loadPermission = 
             new XamlLoadPermission(ObjectWriterParentSettings.AccessLevel);
         loadPermission.Demand();
         this.LoadPermission = loadPermission;
     }
     bool assemblyTargetsFramework2 = false;
     // The local assembly can be null if it is not specified in the XamlReaderSettings.
     if (schemaContext.LocalAssembly != null)
     {
         assemblyTargetsFramework2 = schemaContext.LocalAssembly.ImageRuntimeVersion.StartsWith("v2", StringComparison.Ordinal);
     }
     // There is an incompatibility between the framework versions 3 and 4 regarding MarkupExtension resources.
     // In version 3, MarkupExtension resources did not provide values when looked up.
     // In version 4, they do.
     if (assemblyTargetsFramework2)
     {
         ObjectWriterParentSettings.SkipProvideValueOnRoot = true;
     }
     this.Stream = stream;
     this.SchemaContext = schemaContext;
     this.ObjectWriterFactory = objectWriterFactory;
     this.ServiceProvider = serviceProvider;
     this.RootObject = rootObject;
 }
Example #2
0
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                bool isEmpty = !IsUnrestricted() && AllowedAccess.Count == 0;
                return(isEmpty);
            }
            XamlLoadPermission other = CastPermission(target, "target");

            if (other.IsUnrestricted())
            {
                return(true);
            }
            if (this.IsUnrestricted())
            {
                return(false);
            }

            foreach (XamlAccessLevel accessLevel in this.AllowedAccess)
            {
                if (!other.Includes(accessLevel))
                {
                    return(false);
                }
            }
            return(true);
        }
Example #3
0
        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            XamlLoadPermission permission = CastPermission(other, "other");

            if (this.IsUnrestricted() || permission.IsUnrestricted())
            {
                return(new XamlLoadPermission(PermissionState.Unrestricted));
            }
            List <XamlAccessLevel> allowedAccess = new List <XamlAccessLevel>(this.AllowedAccess);

            foreach (XamlAccessLevel level in permission.AllowedAccess)
            {
                if (!this.Includes(level))
                {
                    allowedAccess.Add(level);
                    if (level.PrivateAccessToTypeName != null)
                    {
                        for (int i = 0; i < allowedAccess.Count; i++)
                        {
                            if ((allowedAccess[i].PrivateAccessToTypeName == null) && (allowedAccess[i].AssemblyNameString == level.AssemblyNameString))
                            {
                                allowedAccess.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(new XamlLoadPermission(allowedAccess));
        }
Example #4
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            XamlLoadPermission permission = CastPermission(target, "target");

            if (permission.IsUnrestricted())
            {
                return(this.Copy());
            }
            if (this.IsUnrestricted())
            {
                return(permission.Copy());
            }
            List <XamlAccessLevel> allowedAccess = new List <XamlAccessLevel>();

            foreach (XamlAccessLevel level in this.AllowedAccess)
            {
                if (permission.Includes(level))
                {
                    allowedAccess.Add(level);
                }
                else if (level.PrivateAccessToTypeName != null)
                {
                    XamlAccessLevel requestedAccess = level.AssemblyOnly();
                    if (permission.Includes(requestedAccess))
                    {
                        allowedAccess.Add(requestedAccess);
                    }
                }
            }
            return(new XamlLoadPermission(allowedAccess));
        }
Example #5
0
        private static XamlLoadPermission CastPermission(IPermission other, string argName)
        {
            XamlLoadPermission result = other as XamlLoadPermission;

            if (result == null)
            {
                throw new ArgumentException(SR.Get(SRID.ExpectedLoadPermission), argName);
            }
            return(result);
        }
Example #6
0
        private static XamlLoadPermission CastPermission(IPermission other, string argName)
        {
            XamlLoadPermission permission = other as XamlLoadPermission;

            if (permission == null)
            {
                throw new ArgumentException(System.Xaml.SR.Get("ExpectedLoadPermission"), argName);
            }
            return(permission);
        }
Example #7
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            XamlLoadPermission other = CastPermission(target, "target");

            if (other.IsUnrestricted())
            {
                return(this.Copy());
            }
            if (this.IsUnrestricted())
            {
                return(other.Copy());
            }

            List <XamlAccessLevel> result = new List <XamlAccessLevel>();

            // We could optimize this with a hash, but we don't expect people to be creating
            // large unions of access levels.
            foreach (XamlAccessLevel accessLevel in this.AllowedAccess)
            {
                // First try the full access level
                if (other.Includes(accessLevel))
                {
                    result.Add(accessLevel);
                }
                // Then try the assembly subset
                else if (accessLevel.PrivateAccessToTypeName != null)
                {
                    XamlAccessLevel assemblyAccess = accessLevel.AssemblyOnly();
                    if (other.Includes(assemblyAccess))
                    {
                        result.Add(assemblyAccess);
                    }
                }
            }
            return(new XamlLoadPermission(result));
        }
Example #8
0
        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            XamlLoadPermission xamlOther = CastPermission(other, "other");

            if (IsUnrestricted() || xamlOther.IsUnrestricted())
            {
                return(new XamlLoadPermission(PermissionState.Unrestricted));
            }

            List <XamlAccessLevel> mergedAccess = new List <XamlAccessLevel>(this.AllowedAccess);

            foreach (XamlAccessLevel accessLevel in xamlOther.AllowedAccess)
            {
                if (!this.Includes(accessLevel))
                {
                    mergedAccess.Add(accessLevel);
                    if (accessLevel.PrivateAccessToTypeName != null)
                    {
                        // If we have an entry for access to just the assembly of this type, it is now redundant
                        for (int i = 0; i < mergedAccess.Count; i++)
                        {
                            if (mergedAccess[i].PrivateAccessToTypeName == null &&
                                mergedAccess[i].AssemblyNameString == accessLevel.AssemblyNameString)
                            {
                                mergedAccess.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(new XamlLoadPermission(mergedAccess));
        }
Example #9
0
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return(!this.IsUnrestricted() && (this.AllowedAccess.Count == 0));
            }
            XamlLoadPermission permission = CastPermission(target, "target");

            if (!permission.IsUnrestricted())
            {
                if (this.IsUnrestricted())
                {
                    return(false);
                }
                foreach (XamlAccessLevel level in this.AllowedAccess)
                {
                    if (!permission.Includes(level))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Example #10
0
            private ResourceDictionary LoadDictionary(Assembly assembly, string assemblyName, string resourceName, bool isTraceEnabled)
            { 
                ResourceDictionary dictionary = null;

                // Create the resource manager that will load the byte array
                ResourceManager rm = new ResourceManager(assemblyName + ".g", assembly); 

                resourceName = resourceName + ".baml"; 
                // Load the resource stream 
                Stream stream = null;
                try 
                {
                    stream = rm.GetStream(resourceName, CultureInfo.CurrentUICulture);
                }
                // There is no ResourceManager.HasManifest in order to detect this case before an exception is thrown. 
                // Likewise, there is no way to know if loading a resource will fail prior to loading it.
                // So, the exceptions must be caught. stream will continue to be null and handled accordingly later. 
#pragma warning disable 6502 

                catch (MissingManifestResourceException) 
                {
                    // No usable resources in the assembly
                }
#if !DEBUG 
                catch (InvalidOperationException)
                { 
                    // Object not stored correctly 
                }
#endif 

#pragma warning restore 6502

                if (stream != null) 
                {
                    Baml2006ReaderSettings settings = new Baml2006ReaderSettings(); 
                    settings.OwnsStream = true; 
                    settings.LocalAssembly = assembly;
 
                    Baml2006Reader bamlReader = new Baml2006Reader(stream, new Baml2006SchemaContext(settings.LocalAssembly), settings);

                    System.Xaml.XamlObjectWriterSettings owSettings = XamlReader.CreateObjectWriterSettingsForBaml();
                    if (assembly != null) 
                    {
                        owSettings.AccessLevel = XamlAccessLevel.AssemblyAccessTo(assembly); 
                    } 
                    System.Xaml.XamlObjectWriter writer = new System.Xaml.XamlObjectWriter(bamlReader.SchemaContext, owSettings);
 
                    if (owSettings.AccessLevel != null)
                    {
                        XamlLoadPermission loadPermission = new XamlLoadPermission(owSettings.AccessLevel);
                        loadPermission.Assert(); 
                        try
                        { 
                            System.Xaml.XamlServices.Transform(bamlReader, writer); 
                        }
                        finally 
                        {
                            CodeAccessPermission.RevertAssert();
                        }
                    } 
                    else
                    { 
                        System.Xaml.XamlServices.Transform(bamlReader, writer); 
                    }
                    dictionary = (ResourceDictionary)writer.Result; 

                    if (isTraceEnabled && (dictionary != null))
                    {
                        EventTrace.EventProvider.TraceEvent(EventTrace.Event.WClientResourceBamlAssembly, 
                                                            EventTrace.Keyword.KeywordXamlBaml, EventTrace.Level.Verbose,
                                                            assemblyName); 
                    } 
                }
 
                return dictionary;
            }
Example #11
0
 // copy ctor. We can reuse the list of the existing instance, because it is a
 // ReadOnlyCollection over a privately created array, hence is never mutated,
 // even if the other instance is mutated via FromXml().
 private XamlLoadPermission(XamlLoadPermission other)
 {
     _isUnrestricted    = other._isUnrestricted;
     this.AllowedAccess = other.AllowedAccess;
 }
Example #12
0
        //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647
        internal static object LoadBaml(
            Stream stream,
            ParserContext parserContext, 
            object parent,
            bool closeStream) 
        { 
            object root = null;
 
#if DEBUG_CLR_MEM
            bool clrTracingEnabled = false;
            // Use local pass variable to correctly log nested parses.
            int pass = 0; 

            if (CLRProfilerControl.ProcessIsUnderCLRProfiler && 
               (CLRProfilerControl.CLRLoggingLevel >= CLRProfilerControl.CLRLogState.Performance)) 
            {
                clrTracingEnabled = true; 
                pass = ++_CLRBamlPass;
                CLRProfilerControl.CLRLogWriteLine("Begin_BamlParse_{0}", pass);
            }
#endif // DEBUG_CLR_MEM 

            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXamlBaml | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientParseBamlBegin, parserContext.BaseUri); 
 
            if (TraceMarkup.IsEnabled)
            { 
                TraceMarkup.Trace(TraceEventType.Start, TraceMarkup.Load);
            }

            try 
            {
                // 
                // If the stream contains info about the Assembly that created it, 
                // set StreamCreatedAssembly from the stream instance.
                // 
                IStreamInfo streamInfo = stream as IStreamInfo;
                if (streamInfo != null)
                {
                    parserContext.StreamCreatedAssembly = streamInfo.Assembly; 
                }
 
                Baml2006ReaderSettings readerSettings = XamlReader.CreateBamlReaderSettings(); 
                readerSettings.BaseUri = parserContext.BaseUri;
                readerSettings.LocalAssembly = streamInfo.Assembly; 
 				// We do not set OwnsStream = true so the Baml2006Reader will not close the stream.
                // Calling code is responsible for disposing the stream
                if (readerSettings.BaseUri == null || String.IsNullOrEmpty(readerSettings.BaseUri.ToString()))
                { 
                    readerSettings.BaseUri = BaseUriHelper.PackAppBaseUri;
                } 
 
                var reader = new Baml2006Reader(stream, new Baml2006SchemaContext(readerSettings.LocalAssembly), readerSettings, parent);
 
                // We don't actually use the GeneratedInternalTypeHelper any more.
                // But for v3 compat, don't allow loading of internals in PT unless there is one.
                Type internalTypeHelper = null;
                if (streamInfo.Assembly != null) 
                {
                    try 
                    { 
                        internalTypeHelper = XamlTypeMapper.GetInternalTypeHelperTypeFromAssembly(parserContext);
                    } 
                    // This can perform attribute reflection which will fail if the assembly has unresolvable
                    // attributes. If that happens, just assume there is no helper.
                    catch (Exception e)
                    { 
                        if (MS.Internal.CriticalExceptions.IsCriticalException(e))
                        { 
                            throw; 
                        }
                    } 
                }

                if (internalTypeHelper != null)
                { 
                    XamlAccessLevel accessLevel = XamlAccessLevel.AssemblyAccessTo(streamInfo.Assembly);
                    XamlLoadPermission loadPermission = new XamlLoadPermission(accessLevel); 
                    loadPermission.Assert(); 
                    try
                    { 
                        root = WpfXamlLoader.LoadBaml(reader, parserContext.SkipJournaledProperties, parent, accessLevel, parserContext.BaseUri);
                    }
                    finally
                    { 
                        CodeAccessPermission.RevertAssert();
                    } 
                } 
                else
                { 
                    root = WpfXamlLoader.LoadBaml(reader, parserContext.SkipJournaledProperties, parent, null, parserContext.BaseUri);
                }

                DependencyObject dObject = root as DependencyObject; 
                if (dObject != null)
                { 
                    dObject.SetValue(BaseUriHelper.BaseUriProperty, readerSettings.BaseUri); 
                }
 
                Application app = root as Application;
                if (app != null)
                {
                    app.ApplicationMarkupBaseUri = GetBaseUri(readerSettings.BaseUri); 
                }
 
                Debug.Assert(parent == null || root == parent); 
            }
 
            finally
            {
                if (TraceMarkup.IsEnabled)
                { 
                    TraceMarkup.Trace(TraceEventType.Stop, TraceMarkup.Load, root);
                } 
 
                EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordXamlBaml | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientParseBamlEnd, parserContext.BaseUri);
 
				if (closeStream && stream != null)
                {
                    stream.Close();
                } 

#if DEBUG_CLR_MEM 
                if (clrTracingEnabled && (CLRProfilerControl.CLRLoggingLevel >= CLRProfilerControl.CLRLogState.Performance)) 
                {
                    CLRProfilerControl.CLRLogWriteLine("End_BamlParse_{0}", pass); 
                }
#endif // DEBUG_CLR_MEM
            }
 
            return (root);
        } 
 private XamlLoadPermission(XamlLoadPermission other)
 {
     this._isUnrestricted = other._isUnrestricted;
     this.AllowedAccess = other.AllowedAccess;
 }