1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
|
using System;
using System.Collections;
using System.Deployment.Internal;
using System.Deployment.Internal.Isolation;
using System.Deployment.Internal.Isolation.Manifest;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
using System.Security;
namespace System
{
// Public surface area for Whidbey:
// AppDomain.ActivationContext
// AppDomain.ActivationContext.Identity
// AppDomain.ActivationContext.Identity.FullName
// AppDomain.ActivationContext.Identity.CodeBase
// static ActivationContext.CreatePartialActivationContext(identity)
// static ActivationContext.CreatePartialActivationContext(identity, manifestPaths[])
// ActivationContext class
// ActivationContext.Identity
// ApplicationIdentity class
// ApplicationIdentity.FullName
// ApplicationIdentity.CodeBase
// + existing AppDomain.BaseDirectory: local app directory
[Serializable]
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class ActivationContext : IDisposable, ISerializable
{
private ApplicationIdentity _applicationIdentity;
// ISSUE - can use Generic lists.
private ArrayList _definitionIdentities;
private ArrayList _manifests;
private string[] _manifestPaths;
private ContextForm _form;
private ApplicationStateDisposition _appRunState;
private IActContext _actContext;
private const int DefaultComponentCount = 2;
private ActivationContext () {}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[SecurityCritical]
private ActivationContext (SerializationInfo info, StreamingContext context)
{
string fullName = (string) info.GetValue("FullName", typeof(string));
string[] manifestPaths = (string[]) info.GetValue("ManifestPaths", typeof(string[]));
if (manifestPaths == null)
CreateFromName(new ApplicationIdentity(fullName));
else
CreateFromNameAndManifests(new ApplicationIdentity(fullName), manifestPaths);
}
internal ActivationContext(ApplicationIdentity applicationIdentity)
{
CreateFromName(applicationIdentity);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
internal ActivationContext(ApplicationIdentity applicationIdentity, string[] manifestPaths)
{
CreateFromNameAndManifests(applicationIdentity, manifestPaths);
}
[SecuritySafeCritical]
private void CreateFromName (ApplicationIdentity applicationIdentity)
{
if (applicationIdentity == null)
throw new ArgumentNullException("applicationIdentity");
Contract.EndContractBlock();
_applicationIdentity = applicationIdentity;
IEnumDefinitionIdentity idenum = _applicationIdentity.Identity.EnumAppPath();
_definitionIdentities = new ArrayList(DefaultComponentCount);
IDefinitionIdentity[] asbId = new IDefinitionIdentity[1];
while (idenum.Next(1, asbId) == 1)
{
_definitionIdentities.Add(asbId[0]);
}
_definitionIdentities.TrimToSize();
if (_definitionIdentities.Count <= 1)
{
#if ISOLATION_IN_MSCORLIB
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidAppId"));
#else
throw new ArgumentException("Invalid identity: no deployment/app identity specified");
#endif
}
_manifestPaths = null;
_manifests = null;
// Construct real IActContext from store.
_actContext = IsolationInterop.CreateActContext(_applicationIdentity.Identity);
_form = ContextForm.StoreBounded;
_appRunState = ApplicationStateDisposition.Undefined;
#if ISOLATION_IN_MSCORLIB
Contract.Assert(_definitionIdentities.Count == 2, "An application must have exactly 1 deployment component and 1 application component in Whidbey");
#endif
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[SecuritySafeCritical]
private void CreateFromNameAndManifests (ApplicationIdentity applicationIdentity, string[] manifestPaths)
{
if (applicationIdentity == null)
throw new ArgumentNullException("applicationIdentity");
if (manifestPaths == null)
throw new ArgumentNullException("manifestPaths");
Contract.EndContractBlock();
_applicationIdentity = applicationIdentity;
// ISSUE - need validation on manifestPaths
IEnumDefinitionIdentity idenum = _applicationIdentity.Identity.EnumAppPath();
_manifests = new ArrayList(DefaultComponentCount);
_manifestPaths = new String[manifestPaths.Length];
IDefinitionIdentity[] asbId = new IDefinitionIdentity[1];
int i=0;
while (idenum.Next(1, asbId) == 1)
{
ICMS cms = (ICMS) IsolationInterop.ParseManifest(manifestPaths[i], null, ref IsolationInterop.IID_ICMS);
if (IsolationInterop.IdentityAuthority.AreDefinitionsEqual(0, cms.Identity, asbId[0]))
{
_manifests.Add(cms);
_manifestPaths[i]=manifestPaths[i];
}
else
{
#if ISOLATION_IN_MSCORLIB
throw new ArgumentException(Environment.GetResourceString("Argument_IllegalAppIdMismatch"));
#else
throw new ArgumentException("Application Identity does not match identity in manifests");
#endif
}
i++;
}
if (i!=manifestPaths.Length)
{
#if ISOLATION_IN_MSCORLIB
throw new ArgumentException(Environment.GetResourceString("Argument_IllegalAppId"));
#else
throw new ArgumentException("Application Identity does not have same number of components as manifest paths");
#endif
}
_manifests.TrimToSize();
if (_manifests.Count <= 1)
{
#if ISOLATION_IN_MSCORLIB
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidAppId"));
#else
throw new ArgumentException("Invalid identity: no deployment/app identity specified");
#endif
}
_definitionIdentities = null;
_actContext = null;
_form = ContextForm.Loose;
_appRunState = ApplicationStateDisposition.Undefined;
#if ISOLATION_IN_MSCORLIB
Contract.Assert(_manifests.Count == 2, "An application must have exactly 1 deployment component and 1 application component in Whidbey");
#endif
}
~ActivationContext()
{
Dispose(false);
}
public static ActivationContext CreatePartialActivationContext(ApplicationIdentity identity)
{
return new ActivationContext(identity);
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public static ActivationContext CreatePartialActivationContext(ApplicationIdentity identity, string[] manifestPaths)
{
return new ActivationContext(identity, manifestPaths);
}
public ApplicationIdentity Identity
{
get
{
return _applicationIdentity;
}
}
public ContextForm Form
{
get
{
return _form;
}
}
public byte[] ApplicationManifestBytes
{
get
{
return GetApplicationManifestBytes();
}
}
public byte[] DeploymentManifestBytes
{
get
{
return GetDeploymentManifestBytes();
}
}
internal string[] ManifestPaths
{
[ResourceExposure(ResourceScope.Machine)]
get
{
return _manifestPaths;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Nested Enum.
public enum ContextForm
{
Loose = 0,
StoreBounded = 1
}
// Internals.
internal string ApplicationDirectory
{
[ResourceExposure(ResourceScope.Machine)]
[SecurityCritical]
get
{
if (_form == ContextForm.Loose)
return Path.GetDirectoryName(_manifestPaths[_manifestPaths.Length-1]);
string s;
_actContext.ApplicationBasePath(0, out s);
return s;
}
}
internal string DataDirectory
{
[ResourceExposure(ResourceScope.Machine)]
[SecurityCritical]
get
{
// ISSUE - what is the data directory for 'loose'?
if (_form == ContextForm.Loose)
return null;
string s;
// Note: passing in flag == 1.
_actContext.GetApplicationStateFilesystemLocation(1, UIntPtr.Zero, IntPtr.Zero, out s);
return s;
}
}
// These internal methods to become public in Longhorn, when manifest APIs become public
// in LH M7 this will be come IACS instead of ICMS.
internal ICMS ActivationContextData
{
[SecurityCritical]
get
{
// this will be a true merge of deployment and application manifest contents (ACS, not CMS)
// for now return only the contents of the application manifest, as most consumers only care about app manifest anyway
return this.ApplicationComponentManifest;
}
}
// Return the manifest of the first deployment component.
internal ICMS DeploymentComponentManifest
{
[SecurityCritical]
get
{
if (_form == ContextForm.Loose)
return (ICMS) _manifests[0];
return GetComponentManifest((IDefinitionIdentity)_definitionIdentities[0]);
}
}
internal ICMS ApplicationComponentManifest
{
[SecurityCritical]
get
{
if (_form == ContextForm.Loose)
return (ICMS) _manifests[_manifests.Count-1];
return GetComponentManifest((IDefinitionIdentity)_definitionIdentities[_definitionIdentities.Count-1]);
}
}
internal ApplicationStateDisposition LastApplicationStateResult
{
get
{
// Return cached result.
return _appRunState;
}
}
[SecurityCritical]
internal ICMS GetComponentManifest(IDefinitionIdentity component)
{
object o;
_actContext.GetComponentManifest(0, component, ref IsolationInterop.IID_ICMS, out o);
return o as ICMS;
}
[SecuritySafeCritical]
internal byte[] GetDeploymentManifestBytes()
{
object o;
string manifestPath;
if (_form == ContextForm.Loose)
manifestPath = _manifestPaths[0];
else
{
_actContext.GetComponentManifest(0, (IDefinitionIdentity)_definitionIdentities[0], ref IsolationInterop.IID_IManifestInformation, out o);
((IManifestInformation)o).get_FullPath(out manifestPath);
Marshal.ReleaseComObject(o);
}
return ReadBytesFromFile(manifestPath);
}
[SecuritySafeCritical]
internal byte[] GetApplicationManifestBytes()
{
object o;
string manifestPath;
if (_form == ContextForm.Loose)
manifestPath = _manifestPaths[_manifests.Count-1];
else
{
_actContext.GetComponentManifest(0, (IDefinitionIdentity)_definitionIdentities[1], ref IsolationInterop.IID_IManifestInformation, out o);
((IManifestInformation)o).get_FullPath(out manifestPath);
Marshal.ReleaseComObject(o);
}
return ReadBytesFromFile(manifestPath);
}
// Internal methods used exclusively by application hosting code.
[SecuritySafeCritical]
internal void PrepareForExecution()
{
if (_form == ContextForm.Loose)
return; // Do nothing.
_actContext.PrepareForExecution(IntPtr.Zero, IntPtr.Zero);
}
[SecuritySafeCritical]
internal ApplicationStateDisposition SetApplicationState(ApplicationState s)
{
if (_form == ContextForm.Loose)
return ApplicationStateDisposition.Undefined; // Do nothing.
UInt32 disposition;
_actContext.SetApplicationRunningState(0, (UInt32)s, out disposition);
// Save the result.
_appRunState = (ApplicationStateDisposition)disposition;
return _appRunState;
}
// Nested Enum.
internal enum ApplicationState
{
Undefined = 0,
Starting = 1,
Running = 2
}
internal enum ApplicationStateDisposition
{
Undefined = 0,
Starting = 1,
StartingMigrated = (1 | (1 << 16)),
Running = 2,
RunningFirstTime = (2 | (1 << 17))
}
// Privates.
[System.Security.SecuritySafeCritical] // auto-generated
private void Dispose(bool fDisposing)
{
// ISSUE- should release unmanaged objects in array lists.
_applicationIdentity = null;
_definitionIdentities = null;
_manifests = null;
_manifestPaths = null;
if (_actContext != null)
Marshal.ReleaseComObject(_actContext);
}
private static byte[] ReadBytesFromFile(string manifestPath)
{
byte[] rawBytes = null;
using (FileStream fs = new FileStream(manifestPath, FileMode.Open, FileAccess.Read))
{
int bufferSize = (int)fs.Length;
// zero length file will except ultimately.
rawBytes = new byte[bufferSize];
if (fs.CanSeek)
{
fs.Seek(0, SeekOrigin.Begin);
}
// Read the file into buffer.
fs.Read(rawBytes, 0, bufferSize);
}
return rawBytes;
}
/// <internalonly/>
[System.Security.SecurityCritical] // auto-generated_required
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (_applicationIdentity != null)
info.AddValue("FullName", _applicationIdentity.FullName, typeof(String));
if (_manifestPaths != null)
info.AddValue("ManifestPaths", _manifestPaths, typeof(String[]));
}
}
[Serializable]
[System.Runtime.InteropServices.ComVisible(false)]
public sealed class ApplicationIdentity : ISerializable {
private IDefinitionAppId _appId;
private ApplicationIdentity () {}
[SecurityCritical]
private ApplicationIdentity (SerializationInfo info, StreamingContext context)
{
string fullName = (string) info.GetValue("FullName", typeof(string));
if (fullName == null)
throw new ArgumentNullException("fullName");
_appId = IsolationInterop.AppIdAuthority.TextToDefinition(0, fullName);
}
[SecuritySafeCritical]
public ApplicationIdentity(String applicationIdentityFullName)
{
if (applicationIdentityFullName == null)
throw new ArgumentNullException("applicationIdentityFullName");
Contract.EndContractBlock();
_appId = IsolationInterop.AppIdAuthority.TextToDefinition(0, applicationIdentityFullName);
}
[SecurityCritical]
internal ApplicationIdentity(IDefinitionAppId applicationIdentity)
{
// ISSUE- this should clone the IDefintionAppId.
_appId = applicationIdentity;
}
public String FullName
{
[SecuritySafeCritical]
get
{
return IsolationInterop.AppIdAuthority.DefinitionToText(0, _appId);
}
}
public String CodeBase
{
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
[SecuritySafeCritical]
get
{
return _appId.get_Codebase();
}
}
public override string ToString()
{
return this.FullName;
}
internal IDefinitionAppId Identity
{
[SecurityCritical]
get
{
return _appId;
}
}
/// <internalonly/>
[System.Security.SecurityCritical] // auto-generated_required
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("FullName", FullName, typeof(String));
}
}
}
namespace System.Deployment.Internal
{
// All classes in this namespace are only usable within CLR (enforced with Link demand/friend etc.)
// This class will go away in LH, when the ApplicationDefinitionIdentity class exposes the functionality directly
[System.Runtime.InteropServices.ComVisible(false)]
public static class InternalApplicationIdentityHelper
{
[SecurityCritical]
public static object /* really IDefinitionAppId */ GetInternalAppId(ApplicationIdentity id)
{
return id.Identity;
}
}
// This helper class will go away in LH, when the ActivationContext class exposes the functionality directly
[System.Runtime.InteropServices.ComVisible(false)]
public static class InternalActivationContextHelper
{
[SecuritySafeCritical]
public static object /* really ICMS */ GetActivationContextData(ActivationContext appInfo)
{
return appInfo.ActivationContextData;
}
[SecuritySafeCritical]
public static object GetApplicationComponentManifest(ActivationContext appInfo)
{
return appInfo.ApplicationComponentManifest;
}
[SecuritySafeCritical]
public static object GetDeploymentComponentManifest(ActivationContext appInfo)
{
return appInfo.DeploymentComponentManifest;
}
public static void PrepareForExecution(ActivationContext appInfo)
{
appInfo.PrepareForExecution();
}
public static bool IsFirstRun(ActivationContext appInfo)
{
return (appInfo.LastApplicationStateResult == ActivationContext.ApplicationStateDisposition.RunningFirstTime);
}
public static byte[] GetApplicationManifestBytes(ActivationContext appInfo)
{
if (appInfo == null)
throw new ArgumentNullException("appInfo");
return appInfo.GetApplicationManifestBytes();
}
public static byte[] GetDeploymentManifestBytes(ActivationContext appInfo)
{
if (appInfo == null)
throw new ArgumentNullException("appInfo");
return appInfo.GetDeploymentManifestBytes();
}
}
}
|