/// <summary>
/// Adds a uri, package pair to the package store.
/// </summary>
/// <param name="uri">key uri</param>
/// <param name="package">package</param>
/// <permission cref="EnvironmentPermission"></permission>
/// <remarks>
/// If a package with the uri is already in the store,it throws an exception.
/// The package will not be automatically replaced within the store.
/// </remarks>
///<SecurityNote>
/// Demands EnvironmentPermission() if package is custom type of Package.
/// This prevents Partially Trusted callers from performing this operation. However, Partially Trusted callers can still
/// add well-known platform Package type (ZipPackage) to PackageStore.
/// the application's PackageStore.
///</SecurityNote>
public static void AddPackage(Uri uri, Package package)
{
// Allow well known platform Package to be added into PackageStore under Partial Trust.
// Otherwise, demand Environment Permission to make sure only Full Trust app can add a custom Package
DemandSecurityPermissionIfCustomPackage(package);
ValidatePackageUri(uri);
// There are well-known package types that are only for internal use (for resource loading)
// (i.e. ResourceContainer - "application://" and SiteOriginContainer - "siteoforigin://"
// Adding packages with such key uri will have no effect on PackWebRequest since
// they cannot be overriden. So, calling this method with such key Uris should be prevented
// However, uri.Equal cannot be used here since the key Uris are used as a pack Uri form and
// only PackUriHelper.ComparePackUri can do the proper comparison of pack Uris.
Uri packUri = PackUriHelper.Create(uri);
if (PackUriHelper.ComparePackUri(packUri, BaseUriHelper.PackAppBaseUri) == 0 ||
PackUriHelper.ComparePackUri(packUri, BaseUriHelper.SiteOfOriginBaseUri) == 0)
{
throw new ArgumentException(SR.Get(SRID.NotAllowedPackageUri), "uri");
}
if (package == null)
{
throw new ArgumentNullException("package");
}
lock (_globalLock)
{
if (_packages == null)
{
_packages = new HybridDictionary(2);
}
if (_packages.Contains(uri))
{
throw new InvalidOperationException(SR.Get(SRID.PackageAlreadyExists));
}
_packages.Add(uri, package);
}
}