Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / TrustUi / MS / Internal / documents / Application / PackageController.cs / 1 / PackageController.cs
//------------------------------------------------------------------------------ //// Copyright (C) Microsoft Corporation. All rights reserved. // //// Responsible for the lifecycle of the PackageDocument and the actions that can // be performed on it. // // // History: // 08/28/2005: [....]: Initial implementation. //----------------------------------------------------------------------------- using System; using System.IO.Packaging; using System.Security; using System.Windows.TrustUI; using System.Windows.Xps.Packaging; namespace MS.Internal.Documents.Application { ////// Responsible for the lifecycle of the PackageDocument and the actions that can /// be performed on it. /// internal class PackageController : IDocumentController { #region IDocumentController Members //------------------------------------------------------------------------- // IDocumentController Members //------------------------------------------------------------------------- ////// /// ////// /// Critical: /// - we are calling the ctor for TransactionalPackage /// TreatAsSafe: /// - safe by definition we are the one method responsibile for setting /// this value /// - the stream is the Workspace stream which is created based on the /// source stream /// [SecurityCritical, SecurityTreatAsSafe] bool IDocumentController.EnableEdit(Document document) { PackageDocument doc = (PackageDocument)document; if (doc.Workspace != null) { doc.Package.EnableEditMode(doc.Workspace); } return true; } ////// ////// /// Critical: /// - we are calling the ctor for TransactionalPackage /// - sets Package /// - creates DigitalSignatureProvider /// - creates DocumentSignatureManager /// TreatAsSafe: /// - safe by definition we are the one method responsible for setting /// this value /// - the stream is the source stream for this XpsDocument /// - source of package is our underlying stream which is protected /// - provider passed to DocumentSignatureManager is created in this /// method from a package created from the underlying stream /// [SecurityCritical, SecurityTreatAsSafe] bool IDocumentController.Open(Document document) { PackageDocument doc = (PackageDocument)document; TransactionalPackage package = new RestrictedTransactionalPackage( doc.Source); doc.Package = package; DocumentProperties.Current.SetXpsProperties(package.PackageProperties); DocumentSignatureManager.Initialize(new DigitalSignatureProvider(package)); // when signatures change (are added or removed) we can no longer simply copy bits on disk DocumentSignatureManager.Current.SignaturesChanged += new EventHandler( delegate ( object sender, EventArgs args) { if (doc.IsFileCopySafe) { Trace.SafeWrite( Trace.Signatures, "Disabling file copy for current document."); doc.IsFileCopySafe = false; } }); return true; } ////// ////// /// Critical: /// - we are calling the Rebind for TransactionalPackage /// TreatAsSafe: /// - safe by definition we are the one method responsibile for setting /// this value /// - the stream is the source stream for this XpsDocument /// - source of package is our underlying stream which is protected /// [SecurityCritical, SecurityTreatAsSafe] bool IDocumentController.Rebind(Document document) { PackageDocument doc = (PackageDocument)document; if (doc.IsRebindNeeded) { doc.Package.Rebind(doc.Source); // no rebinds are needed above us for documents as package doc.IsRebindNeeded = false; } return true; } ////// bool IDocumentController.SaveAsPreperation(Document document) { return ((IDocumentController)this).SavePreperation(document); } ////// /// ////// /// /// Critical: /// - we are calling the MergeChanges for TransactionalPackage /// TreatAsSafe: /// - safe by definition we are the one method responsibile for calling /// this member /// - the stream is the destination stream for this XpsDocument /// [SecurityCritical, SecurityTreatAsSafe] bool IDocumentController.SaveCommit(Document document) { PackageDocument doc = (PackageDocument)document; if (doc.Destination == null) { return false; } if (doc.Package.IsDirty || !doc.IsDestinationIdenticalToSource) { StreamProxy source = doc.Source as StreamProxy; StreamProxy destination = doc.Destination as StreamProxy; // this will catch the case where our source stream is our destination // stream, which would cause corruption of the package if (source.GetHashCode() == destination.GetHashCode()) { throw new InvalidOperationException( SR.Get(SRID.PackageControllerStreamCorruption)); } // this will catch the case where no one was able to copy the stream // thus the lengths will not match if (doc.Source.Length != doc.Destination.Length) { throw new InvalidOperationException( SR.Get(SRID.PackageControllerStreamCorruption)); } // Flush the package to ensure that the relationship parts are // written out before changes are merged doc.Package.Flush(); doc.Package.MergeChanges(doc.Destination); Trace.SafeWrite(Trace.File, "Destination Merged."); } else { Trace.SafeWrite(Trace.File, "Destination Merge Skipped (nothing to do)."); } return true; } ////// ////// /// This method severely breaks encapsulating the chain, however it does /// so because the requirement breaks encapsulation and the original design /// of the class only consider streams not properties. The requirement is /// that as we transition to/from a protected document we copy properties /// between the layers. It was least risk to compromise the design here /// then extend it. /// ////// Critical: /// - we construct UmcSuppressedProperties /// - we call DocumentProperties.Copy /// TreatAsSafe: /// - EncryptedPackage (which require RightsManagementPermission) is the /// only type for the UmcSuppressProperties ctor; this is safe because this /// is the correct source for the data /// - copy is safe; it's only critical because of the data we are passing /// [SecurityCritical, SecurityTreatAsSafe] bool IDocumentController.SavePreperation(Document document) { // we don't have to check packageDoc for because we are only responsible // for PackageDocuments PackageDocument packageDoc = (PackageDocument)document; RightsDocument rightsDoc = document.Dependency as RightsDocument; if (rightsDoc != null) { bool isDestinationProtected = rightsDoc.IsDestinationProtected(); bool isSourceProtected = rightsDoc.IsSourceProtected(); // the only time we don't need to copy properties is when // neither source nor destination is protected as OPC properties // are copied as parts // if the source was protected and the destination is not // then we need to copy properties to the package if (isSourceProtected && !isDestinationProtected) { DocumentProperties.Copy( new SuppressedProperties(rightsDoc.SourcePackage), packageDoc.Package.PackageProperties); } // if the source was not protected and the destination is // then we need to copy properties from the package to the envelope if (!isSourceProtected && isDestinationProtected) { DocumentProperties.Copy( packageDoc.Package.PackageProperties, new SuppressedProperties(rightsDoc.DestinationPackage)); } // if they were both protected we need to copy properties // from the old envelope to the new if (isDestinationProtected && isSourceProtected) { DocumentProperties.Copy( new SuppressedProperties(rightsDoc.SourcePackage), new SuppressedProperties(rightsDoc.DestinationPackage)); } } return true; } #endregion IDocumentController Members #region IChainOfResponsibiltyNodeMembers //-------------------------------------------------------------------------- // IChainOfResponsibiltyNode Members //------------------------------------------------------------------------- /// /// bool IChainOfResponsibiltyNode/// .IsResponsible(Document subject) { return subject is PackageDocument; } #endregion IChainOfResponsibiltyNode Members } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WinFormsSecurity.cs
- ArraySortHelper.cs
- TextBoxAutoCompleteSourceConverter.cs
- DataGridViewImageCell.cs
- DirectionalLight.cs
- NativeMethods.cs
- CompilerHelpers.cs
- latinshape.cs
- CharacterHit.cs
- LassoSelectionBehavior.cs
- DescendantOverDescendantQuery.cs
- NetworkCredential.cs
- WebPartManagerInternals.cs
- EntitySet.cs
- BinaryReader.cs
- DbDataRecord.cs
- CodeDirectiveCollection.cs
- GlobalizationSection.cs
- CompositeScriptReference.cs
- PenContexts.cs
- SqlNodeAnnotations.cs
- FormViewUpdateEventArgs.cs
- HMAC.cs
- UniqueEventHelper.cs
- SessionViewState.cs
- HelloOperationCD1AsyncResult.cs
- VsPropertyGrid.cs
- HMACSHA512.cs
- PermissionSetTriple.cs
- XmlSerializerFactory.cs
- WebPartTransformerAttribute.cs
- DesignUtil.cs
- TemplateNodeContextMenu.cs
- Resources.Designer.cs
- DataObjectEventArgs.cs
- sqlser.cs
- DbConnectionPoolCounters.cs
- EventMap.cs
- ComponentDispatcherThread.cs
- NativeCppClassAttribute.cs
- ToolStripStatusLabel.cs
- UpdateTracker.cs
- Activity.cs
- TranslateTransform.cs
- VisualBrush.cs
- SmiMetaDataProperty.cs
- MouseGestureValueSerializer.cs
- InteropTrackingRecord.cs
- UniqueIdentifierService.cs
- AuthenticationSection.cs
- DateTimeFormatInfoScanner.cs
- RuntimeIdentifierPropertyAttribute.cs
- AxisAngleRotation3D.cs
- LambdaCompiler.Statements.cs
- Marshal.cs
- StatusBarPanel.cs
- DataBindingExpressionBuilder.cs
- TaskFileService.cs
- PeerOutputChannel.cs
- ColumnTypeConverter.cs
- TransformPatternIdentifiers.cs
- AdobeCFFWrapper.cs
- odbcmetadatacolumnnames.cs
- BinHexEncoding.cs
- assertwrapper.cs
- XmlWriterTraceListener.cs
- CompiledQuery.cs
- SequentialUshortCollection.cs
- VerticalAlignConverter.cs
- MenuItemStyleCollectionEditor.cs
- AdornerLayer.cs
- CompensationHandlingFilter.cs
- basemetadatamappingvisitor.cs
- SHA256Cng.cs
- X509ThumbprintKeyIdentifierClause.cs
- DataContractJsonSerializerOperationFormatter.cs
- XmlDeclaration.cs
- InternalConfigEventArgs.cs
- SHA384Managed.cs
- LayoutTableCell.cs
- RemoteHelper.cs
- CleanUpVirtualizedItemEventArgs.cs
- InputLanguageCollection.cs
- SqlDataSourceCommandParser.cs
- MobileListItemCollection.cs
- BamlRecordHelper.cs
- ReadOnlyHierarchicalDataSource.cs
- MouseWheelEventArgs.cs
- TypeUtil.cs
- WorkflowRuntime.cs
- SharedUtils.cs
- PropertyGridView.cs
- RulePatternOps.cs
- securitycriticaldata.cs
- HwndSubclass.cs
- ParamArrayAttribute.cs
- WmlTextViewAdapter.cs
- MULTI_QI.cs
- GlobalProxySelection.cs
- SQLResource.cs