OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Collections.Generic; | |
5 using System.Globalization; | |
6 using System.IO; | |
7 using System.Runtime.InteropServices; | |
8 using Microsoft.VisualStudio; | |
9 using Microsoft.VisualStudio.Shell.Interop; | |
10 | |
11 namespace Microsoft.VisualStudio.Project | |
12 { | |
13 /// <summary> | |
14 /// Defines abstract package. | |
15 /// </summary> | |
16 [ComVisible(true)] | |
17 [CLSCompliant(false)] | |
18 public abstract class ProjectPackage : Microsoft.VisualStudio.Shell.Pack
age | |
19 { | |
20 #region fields | |
21 /// <summary> | |
22 /// This is the place to register all the solution listeners. | |
23 /// </summary> | |
24 private List<SolutionListener> solutionListeners = new List<Solu
tionListener>(); | |
25 | |
26 /// <summary> | |
27 /// Knows about project trust levels for projects | |
28 /// </summary> | |
29 private Dictionary<Guid, ProjectTrustLevel> projectTrustTable =
new Dictionary<Guid, ProjectTrustLevel>(); | |
30 | |
31 /// <summary> | |
32 /// Key used for persistence in suo file | |
33 /// </summary> | |
34 private string projectTrustPersistenceKey; | |
35 #endregion | |
36 | |
37 #region properties | |
38 /// <summary> | |
39 /// Add your listener to this list. They should be added in the
overridden Initialize befaore calling the base. | |
40 /// </summary> | |
41 protected internal IList<SolutionListener> SolutionListeners | |
42 { | |
43 get | |
44 { | |
45 return this.solutionListeners; | |
46 } | |
47 } | |
48 | |
49 /// <summary> | |
50 /// Key used for persistence in suo file. | |
51 /// </summary> | |
52 private string ProjectTrustPersistenceKey | |
53 { | |
54 get | |
55 { | |
56 if(string.IsNullOrEmpty(this.projectTrustPersist
enceKey)) | |
57 { | |
58 string packageGuid = this.GetType().GUID
.ToString("B"); | |
59 this.projectTrustPersistenceKey = string
.Format(CultureInfo.InvariantCulture, "{0}_projecttrust", packageGuid.Substring(
1, 18)); | |
60 } | |
61 return this.projectTrustPersistenceKey; | |
62 } | |
63 } | |
64 #endregion | |
65 | |
66 #region ctor | |
67 protected ProjectPackage() | |
68 { | |
69 this.AddOptionKey(this.ProjectTrustPersistenceKey); | |
70 } | |
71 #endregion | |
72 | |
73 #region methods | |
74 /// <summary> | |
75 /// Get project trust level for a project instance | |
76 /// </summary> | |
77 /// <param name="projectInstance">the project instance guid</par
am> | |
78 /// <returns>project trust level</returns> | |
79 /// <exception cref="ArgumentException">project instance guid em
pty or not known </exception> | |
80 public ProjectTrustLevel GetProjectTrustLevel(Guid projectInstan
ce) | |
81 { | |
82 if(!this.projectTrustTable.ContainsKey(projectInstance)) | |
83 { | |
84 return ProjectTrustLevel.Unknown; | |
85 } | |
86 | |
87 return projectTrustTable[projectInstance]; ; | |
88 } | |
89 | |
90 /// <summary> | |
91 /// Sets the project trust level for a project. | |
92 /// </summary> | |
93 /// <param name="projectInstance">the project instance guid asso
ciated with project where project trus level is set</param> | |
94 /// <param name="projectTrustLevel">the trust level to be assign
ed to the project</param> | |
95 /// <exception cref="ArgumentException">project instance guid em
pty</exception> | |
96 public void SetProjectTrustLevel(Guid projectInstance, ProjectTr
ustLevel projectTrustLevel) | |
97 { | |
98 if(Guid.Empty == projectInstance) | |
99 { | |
100 throw new ArgumentException(SR.GetString(SR.Para
meterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "projectInstance"); | |
101 } | |
102 | |
103 if(this.projectTrustTable.ContainsKey(projectInstance)) | |
104 { | |
105 this.projectTrustTable[projectInstance] = projec
tTrustLevel; | |
106 } | |
107 else | |
108 { | |
109 this.projectTrustTable.Add(projectInstance, proj
ectTrustLevel); | |
110 } | |
111 } | |
112 | |
113 /// <summary> | |
114 /// Reads the project trust information. Basically it is invokin
g the OnLoadOptions method. | |
115 /// This is needed because OnLoadOptions method calls happen lat
er then the project is loaded, and checked for security. | |
116 /// </summary> | |
117 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usag
e", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.VisualStudio.Shell
.Interop.IVsSolutionPersistence.LoadPackageUserOpts(Microsoft.VisualStudio.Shell
.Interop.IVsPersistSolutionOpts,System.String)")] | |
118 internal void ReadProjectTrustInformation() | |
119 { | |
120 this.projectTrustTable.Clear(); | |
121 | |
122 IVsSolutionPersistence persistance = this.GetService(typ
eof(SVsSolutionPersistence)) as IVsSolutionPersistence; | |
123 | |
124 if(persistance != null) | |
125 { | |
126 // Read the stream out of the .SUO file. This w
ill call us back on our ReadUserOptions method. | |
127 persistance.LoadPackageUserOpts(this, this.Proje
ctTrustPersistenceKey); | |
128 } | |
129 } | |
130 | |
131 protected override void Initialize() | |
132 { | |
133 base.Initialize(); | |
134 | |
135 // Subscribe to the solution events | |
136 this.solutionListeners.Add(new SolutionListenerForProjec
tReferenceUpdate(this)); | |
137 this.solutionListeners.Add(new SolutionListenerForProjec
tOpen(this)); | |
138 this.solutionListeners.Add(new SolutionListenerForBuildD
ependencyUpdate(this)); | |
139 this.solutionListeners.Add(new SolutionListenerForProjec
tEvents(this)); | |
140 | |
141 foreach(SolutionListener solutionListener in this.soluti
onListeners) | |
142 { | |
143 solutionListener.Init(); | |
144 } | |
145 } | |
146 | |
147 protected override void Dispose(bool disposing) | |
148 { | |
149 // Unadvise solution listeners. | |
150 try | |
151 { | |
152 if(disposing) | |
153 { | |
154 foreach(SolutionListener solutionListene
r in this.solutionListeners) | |
155 { | |
156 solutionListener.Dispose(); | |
157 } | |
158 } | |
159 } | |
160 finally | |
161 { | |
162 | |
163 base.Dispose(disposing); | |
164 } | |
165 } | |
166 | |
167 /// <summary> | |
168 /// Called by the base package to load solution options. | |
169 /// </summary> | |
170 /// <param name="key">Name of the stream.</param> | |
171 /// <param name="stream">The stream from ehere the pachage shoul
d read user specific options.</param> | |
172 protected override void OnLoadOptions(string key, Stream stream) | |
173 { | |
174 // Check if the .suo file is safe, i.e. created on this
computer | |
175 // This should really go on the Package.cs | |
176 IVsSolution solution = this.GetService(typeof(SVsSolutio
n)) as IVsSolution; | |
177 | |
178 if(solution != null) | |
179 { | |
180 object valueAsBool; | |
181 int result = solution.GetProperty((int)__VSPROPI
D2.VSPROPID_SolutionUserFileCreatedOnThisComputer, out valueAsBool); | |
182 | |
183 if(ErrorHandler.Failed(result) || !(bool)valueAs
Bool) | |
184 { | |
185 return; | |
186 } | |
187 } | |
188 | |
189 if(string.Compare(key, this.ProjectTrustPersistenceKey,
StringComparison.OrdinalIgnoreCase) == 0 && stream != null) | |
190 { | |
191 using(BinaryReader reader = new BinaryReader(str
eam)) | |
192 { | |
193 if(reader.BaseStream.Length == 0) | |
194 { | |
195 //No project trust information f
ound | |
196 return; | |
197 } | |
198 | |
199 int projects = reader.ReadInt32(); | |
200 for(int i = 1; i <= projects; i++) | |
201 { | |
202 string projectGuid = reader.Read
String(); | |
203 string trustlevel = reader.ReadS
tring(); | |
204 this.projectTrustTable.Add(new G
uid(projectGuid), (ProjectTrustLevel)Enum.Parse(typeof(ProjectTrustLevel), trust
level, true)); | |
205 } | |
206 } | |
207 } | |
208 else | |
209 { | |
210 base.OnLoadOptions(key, stream); | |
211 } | |
212 } | |
213 | |
214 /// <summary> | |
215 /// Called by the base package when the solution save the option
s | |
216 /// </summary> | |
217 /// <param name="key">Name of the stream.</param> | |
218 /// <param name="stream">The stream from ehere the pachage shoul
d read user specific options.</param> | |
219 protected override void OnSaveOptions(string key, Stream stream) | |
220 { | |
221 if(string.Compare(key, this.ProjectTrustPersistenceKey,
StringComparison.OrdinalIgnoreCase) == 0 && stream != null) | |
222 { | |
223 using(BinaryWriter writer = new BinaryWriter(str
eam)) | |
224 { | |
225 // Write an Int32 for the number of proj
ects in the trust table. | |
226 writer.Write((int)this.projectTrustTable
.Count); | |
227 foreach(Guid projectGuid in this.project
TrustTable.Keys) | |
228 { | |
229 writer.Write((string)projectGuid
.ToString("B")); | |
230 writer.Write(this.projectTrustTa
ble[projectGuid].ToString()); | |
231 } | |
232 } | |
233 | |
234 this.projectTrustTable.Clear(); | |
235 } | |
236 else | |
237 { | |
238 base.OnSaveOptions(key, stream); | |
239 } | |
240 } | |
241 #endregion | |
242 } | |
243 } | |
OLD | NEW |