InstallationManager.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Events;
  3. using MediaBrowser.Common.Implementations.Security;
  4. using MediaBrowser.Common.IO;
  5. using MediaBrowser.Common.Net;
  6. using MediaBrowser.Common.Plugins;
  7. using MediaBrowser.Common.Progress;
  8. using MediaBrowser.Common.Security;
  9. using MediaBrowser.Common.Updates;
  10. using MediaBrowser.Model.Events;
  11. using MediaBrowser.Model.Logging;
  12. using MediaBrowser.Model.Serialization;
  13. using MediaBrowser.Model.Updates;
  14. using System;
  15. using System.Collections.Concurrent;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Security.Cryptography;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. namespace MediaBrowser.Common.Implementations.Updates
  23. {
  24. /// <summary>
  25. /// Manages all install, uninstall and update operations (both plugins and system)
  26. /// </summary>
  27. public class InstallationManager : IInstallationManager
  28. {
  29. public event EventHandler<InstallationEventArgs> PackageInstalling;
  30. public event EventHandler<InstallationEventArgs> PackageInstallationCompleted;
  31. public event EventHandler<InstallationFailedEventArgs> PackageInstallationFailed;
  32. public event EventHandler<InstallationEventArgs> PackageInstallationCancelled;
  33. /// <summary>
  34. /// The current installations
  35. /// </summary>
  36. public List<Tuple<InstallationInfo, CancellationTokenSource>> CurrentInstallations { get; set; }
  37. /// <summary>
  38. /// The completed installations
  39. /// </summary>
  40. public ConcurrentBag<InstallationInfo> CompletedInstallations { get; set; }
  41. #region PluginUninstalled Event
  42. /// <summary>
  43. /// Occurs when [plugin uninstalled].
  44. /// </summary>
  45. public event EventHandler<GenericEventArgs<IPlugin>> PluginUninstalled;
  46. /// <summary>
  47. /// Called when [plugin uninstalled].
  48. /// </summary>
  49. /// <param name="plugin">The plugin.</param>
  50. private void OnPluginUninstalled(IPlugin plugin)
  51. {
  52. EventHelper.FireEventIfNotNull(PluginUninstalled, this, new GenericEventArgs<IPlugin> { Argument = plugin }, _logger);
  53. }
  54. #endregion
  55. #region PluginUpdated Event
  56. /// <summary>
  57. /// Occurs when [plugin updated].
  58. /// </summary>
  59. public event EventHandler<GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>>> PluginUpdated;
  60. /// <summary>
  61. /// Called when [plugin updated].
  62. /// </summary>
  63. /// <param name="plugin">The plugin.</param>
  64. /// <param name="newVersion">The new version.</param>
  65. private void OnPluginUpdated(IPlugin plugin, PackageVersionInfo newVersion)
  66. {
  67. _logger.Info("Plugin updated: {0} {1} {2}", newVersion.name, newVersion.versionStr ?? string.Empty, newVersion.classification);
  68. EventHelper.FireEventIfNotNull(PluginUpdated, this, new GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> { Argument = new Tuple<IPlugin, PackageVersionInfo>(plugin, newVersion) }, _logger);
  69. _applicationHost.NotifyPendingRestart();
  70. }
  71. #endregion
  72. #region PluginInstalled Event
  73. /// <summary>
  74. /// Occurs when [plugin updated].
  75. /// </summary>
  76. public event EventHandler<GenericEventArgs<PackageVersionInfo>> PluginInstalled;
  77. /// <summary>
  78. /// Called when [plugin installed].
  79. /// </summary>
  80. /// <param name="package">The package.</param>
  81. private void OnPluginInstalled(PackageVersionInfo package)
  82. {
  83. _logger.Info("New plugin installed: {0} {1} {2}", package.name, package.versionStr ?? string.Empty, package.classification);
  84. EventHelper.FireEventIfNotNull(PluginInstalled, this, new GenericEventArgs<PackageVersionInfo> { Argument = package }, _logger);
  85. _applicationHost.NotifyPendingRestart();
  86. }
  87. #endregion
  88. /// <summary>
  89. /// The _logger
  90. /// </summary>
  91. private readonly ILogger _logger;
  92. private readonly IApplicationPaths _appPaths;
  93. private readonly IHttpClient _httpClient;
  94. private readonly IJsonSerializer _jsonSerializer;
  95. private readonly ISecurityManager _securityManager;
  96. private readonly IConfigurationManager _config;
  97. private readonly IFileSystem _fileSystem;
  98. /// <summary>
  99. /// Gets the application host.
  100. /// </summary>
  101. /// <value>The application host.</value>
  102. private readonly IApplicationHost _applicationHost;
  103. public InstallationManager(ILogger logger, IApplicationHost appHost, IApplicationPaths appPaths, IHttpClient httpClient, IJsonSerializer jsonSerializer, ISecurityManager securityManager, IConfigurationManager config, IFileSystem fileSystem)
  104. {
  105. if (logger == null)
  106. {
  107. throw new ArgumentNullException("logger");
  108. }
  109. CurrentInstallations = new List<Tuple<InstallationInfo, CancellationTokenSource>>();
  110. CompletedInstallations = new ConcurrentBag<InstallationInfo>();
  111. _applicationHost = appHost;
  112. _appPaths = appPaths;
  113. _httpClient = httpClient;
  114. _jsonSerializer = jsonSerializer;
  115. _securityManager = securityManager;
  116. _config = config;
  117. _fileSystem = fileSystem;
  118. _logger = logger;
  119. }
  120. private Version GetPackageVersion(PackageVersionInfo version)
  121. {
  122. return new Version(ValueOrDefault(version.versionStr, "0.0.0.1"));
  123. }
  124. private static string ValueOrDefault(string str, string def)
  125. {
  126. return string.IsNullOrEmpty(str) ? def : str;
  127. }
  128. /// <summary>
  129. /// Gets all available packages.
  130. /// </summary>
  131. /// <param name="cancellationToken">The cancellation token.</param>
  132. /// <param name="packageType">Type of the package.</param>
  133. /// <param name="applicationVersion">The application version.</param>
  134. /// <returns>Task{List{PackageInfo}}.</returns>
  135. public async Task<IEnumerable<PackageInfo>> GetAvailablePackages(CancellationToken cancellationToken,
  136. PackageType? packageType = null,
  137. Version applicationVersion = null)
  138. {
  139. var data = new Dictionary<string, string>
  140. {
  141. { "key", _securityManager.SupporterKey },
  142. { "mac", _applicationHost.SystemId },
  143. { "systemid", _applicationHost.SystemId }
  144. };
  145. using (var json = await _httpClient.Post(MbAdmin.HttpsUrl + "service/package/retrieveall", data, cancellationToken).ConfigureAwait(false))
  146. {
  147. cancellationToken.ThrowIfCancellationRequested();
  148. var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
  149. return FilterPackages(packages, packageType, applicationVersion);
  150. }
  151. }
  152. private Tuple<List<PackageInfo>, DateTime> _lastPackageListResult;
  153. /// <summary>
  154. /// Gets all available packages.
  155. /// </summary>
  156. /// <param name="cancellationToken">The cancellation token.</param>
  157. /// <returns>Task{List{PackageInfo}}.</returns>
  158. public async Task<IEnumerable<PackageInfo>> GetAvailablePackagesWithoutRegistrationInfo(CancellationToken cancellationToken)
  159. {
  160. if (_lastPackageListResult != null)
  161. {
  162. TimeSpan cacheLength;
  163. switch (_config.CommonConfiguration.SystemUpdateLevel)
  164. {
  165. case PackageVersionClass.Beta:
  166. cacheLength = TimeSpan.FromMinutes(30);
  167. break;
  168. case PackageVersionClass.Dev:
  169. cacheLength = TimeSpan.FromMinutes(3);
  170. break;
  171. default:
  172. cacheLength = TimeSpan.FromHours(24);
  173. break;
  174. }
  175. if ((DateTime.UtcNow - _lastPackageListResult.Item2) < cacheLength)
  176. {
  177. return _lastPackageListResult.Item1;
  178. }
  179. }
  180. using (var json = await _httpClient.Get(MbAdmin.HttpUrl + "service/MB3Packages.json", cancellationToken).ConfigureAwait(false))
  181. {
  182. cancellationToken.ThrowIfCancellationRequested();
  183. var packages = _jsonSerializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
  184. packages = FilterPackages(packages).ToList();
  185. _lastPackageListResult = new Tuple<List<PackageInfo>, DateTime>(packages, DateTime.UtcNow);
  186. return _lastPackageListResult.Item1;
  187. }
  188. }
  189. protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages)
  190. {
  191. foreach (var package in packages)
  192. {
  193. package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
  194. .OrderByDescending(GetPackageVersion).ToList();
  195. }
  196. // Remove packages with no versions
  197. packages = packages.Where(p => p.versions.Any()).ToList();
  198. return packages;
  199. }
  200. protected IEnumerable<PackageInfo> FilterPackages(List<PackageInfo> packages, PackageType? packageType, Version applicationVersion)
  201. {
  202. foreach (var package in packages)
  203. {
  204. package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
  205. .OrderByDescending(GetPackageVersion).ToList();
  206. }
  207. if (packageType.HasValue)
  208. {
  209. packages = packages.Where(p => p.type == packageType.Value).ToList();
  210. }
  211. // If an app version was supplied, filter the versions for each package to only include supported versions
  212. if (applicationVersion != null)
  213. {
  214. foreach (var package in packages)
  215. {
  216. package.versions = package.versions.Where(v => IsPackageVersionUpToDate(v, applicationVersion)).ToList();
  217. }
  218. }
  219. // Remove packages with no versions
  220. packages = packages.Where(p => p.versions.Any()).ToList();
  221. return packages;
  222. }
  223. /// <summary>
  224. /// Determines whether [is package version up to date] [the specified package version info].
  225. /// </summary>
  226. /// <param name="packageVersionInfo">The package version info.</param>
  227. /// <param name="currentServerVersion">The current server version.</param>
  228. /// <returns><c>true</c> if [is package version up to date] [the specified package version info]; otherwise, <c>false</c>.</returns>
  229. private bool IsPackageVersionUpToDate(PackageVersionInfo packageVersionInfo, Version currentServerVersion)
  230. {
  231. if (string.IsNullOrEmpty(packageVersionInfo.requiredVersionStr))
  232. {
  233. return true;
  234. }
  235. Version requiredVersion;
  236. return Version.TryParse(packageVersionInfo.requiredVersionStr, out requiredVersion) && currentServerVersion >= requiredVersion;
  237. }
  238. /// <summary>
  239. /// Gets the package.
  240. /// </summary>
  241. /// <param name="name">The name.</param>
  242. /// <param name="guid">The assembly guid</param>
  243. /// <param name="classification">The classification.</param>
  244. /// <param name="version">The version.</param>
  245. /// <returns>Task{PackageVersionInfo}.</returns>
  246. public async Task<PackageVersionInfo> GetPackage(string name, string guid, PackageVersionClass classification, Version version)
  247. {
  248. var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
  249. var package = packages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
  250. ?? packages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
  251. if (package == null)
  252. {
  253. return null;
  254. }
  255. return package.versions.FirstOrDefault(v => GetPackageVersion(v).Equals(version) && v.classification == classification);
  256. }
  257. /// <summary>
  258. /// Gets the latest compatible version.
  259. /// </summary>
  260. /// <param name="name">The name.</param>
  261. /// <param name="guid">The assembly guid if this is a plug-in</param>
  262. /// <param name="currentServerVersion">The current server version.</param>
  263. /// <param name="classification">The classification.</param>
  264. /// <returns>Task{PackageVersionInfo}.</returns>
  265. public async Task<PackageVersionInfo> GetLatestCompatibleVersion(string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
  266. {
  267. var packages = await GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
  268. return GetLatestCompatibleVersion(packages, name, guid, currentServerVersion, classification);
  269. }
  270. /// <summary>
  271. /// Gets the latest compatible version.
  272. /// </summary>
  273. /// <param name="availablePackages">The available packages.</param>
  274. /// <param name="name">The name.</param>
  275. /// <param name="currentServerVersion">The current server version.</param>
  276. /// <param name="classification">The classification.</param>
  277. /// <returns>PackageVersionInfo.</returns>
  278. public PackageVersionInfo GetLatestCompatibleVersion(IEnumerable<PackageInfo> availablePackages, string name, string guid, Version currentServerVersion, PackageVersionClass classification = PackageVersionClass.Release)
  279. {
  280. var package = availablePackages.FirstOrDefault(p => string.Equals(p.guid, guid ?? "none", StringComparison.OrdinalIgnoreCase))
  281. ?? availablePackages.FirstOrDefault(p => p.name.Equals(name, StringComparison.OrdinalIgnoreCase));
  282. if (package == null)
  283. {
  284. return null;
  285. }
  286. return package.versions
  287. .OrderByDescending(GetPackageVersion)
  288. .FirstOrDefault(v => v.classification <= classification && IsPackageVersionUpToDate(v, currentServerVersion));
  289. }
  290. /// <summary>
  291. /// Gets the available plugin updates.
  292. /// </summary>
  293. /// <param name="applicationVersion">The current server version.</param>
  294. /// <param name="withAutoUpdateEnabled">if set to <c>true</c> [with auto update enabled].</param>
  295. /// <param name="cancellationToken">The cancellation token.</param>
  296. /// <returns>Task{IEnumerable{PackageVersionInfo}}.</returns>
  297. public async Task<IEnumerable<PackageVersionInfo>> GetAvailablePluginUpdates(Version applicationVersion, bool withAutoUpdateEnabled, CancellationToken cancellationToken)
  298. {
  299. var catalog = await GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
  300. var plugins = _applicationHost.Plugins.ToList();
  301. if (withAutoUpdateEnabled)
  302. {
  303. plugins = plugins
  304. .Where(p => _config.CommonConfiguration.EnableAutoUpdate)
  305. .ToList();
  306. }
  307. // Figure out what needs to be installed
  308. var packages = plugins.Select(p =>
  309. {
  310. var latestPluginInfo = GetLatestCompatibleVersion(catalog, p.Name, p.Id.ToString(), applicationVersion, _config.CommonConfiguration.SystemUpdateLevel);
  311. return latestPluginInfo != null && GetPackageVersion(latestPluginInfo) > p.Version ? latestPluginInfo : null;
  312. }).Where(i => i != null).ToList();
  313. return packages
  314. .Where(p => !string.IsNullOrWhiteSpace(p.sourceUrl) && !CompletedInstallations.Any(i => string.Equals(i.AssemblyGuid, p.guid, StringComparison.OrdinalIgnoreCase)));
  315. }
  316. /// <summary>
  317. /// Installs the package.
  318. /// </summary>
  319. /// <param name="package">The package.</param>
  320. /// <param name="progress">The progress.</param>
  321. /// <param name="cancellationToken">The cancellation token.</param>
  322. /// <returns>Task.</returns>
  323. /// <exception cref="System.ArgumentNullException">package</exception>
  324. public async Task InstallPackage(PackageVersionInfo package, IProgress<double> progress, CancellationToken cancellationToken)
  325. {
  326. if (package == null)
  327. {
  328. throw new ArgumentNullException("package");
  329. }
  330. if (progress == null)
  331. {
  332. throw new ArgumentNullException("progress");
  333. }
  334. var installationInfo = new InstallationInfo
  335. {
  336. Id = Guid.NewGuid().ToString("N"),
  337. Name = package.name,
  338. AssemblyGuid = package.guid,
  339. UpdateClass = package.classification,
  340. Version = package.versionStr
  341. };
  342. var innerCancellationTokenSource = new CancellationTokenSource();
  343. var tuple = new Tuple<InstallationInfo, CancellationTokenSource>(installationInfo, innerCancellationTokenSource);
  344. // Add it to the in-progress list
  345. lock (CurrentInstallations)
  346. {
  347. CurrentInstallations.Add(tuple);
  348. }
  349. var innerProgress = new ActionableProgress<double>();
  350. // Whenever the progress updates, update the outer progress object and InstallationInfo
  351. innerProgress.RegisterAction(percent =>
  352. {
  353. progress.Report(percent);
  354. installationInfo.PercentComplete = percent;
  355. });
  356. var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, innerCancellationTokenSource.Token).Token;
  357. var installationEventArgs = new InstallationEventArgs
  358. {
  359. InstallationInfo = installationInfo,
  360. PackageVersionInfo = package
  361. };
  362. EventHelper.FireEventIfNotNull(PackageInstalling, this, installationEventArgs, _logger);
  363. try
  364. {
  365. await InstallPackageInternal(package, innerProgress, linkedToken).ConfigureAwait(false);
  366. lock (CurrentInstallations)
  367. {
  368. CurrentInstallations.Remove(tuple);
  369. }
  370. progress.Report(100);
  371. CompletedInstallations.Add(installationInfo);
  372. EventHelper.FireEventIfNotNull(PackageInstallationCompleted, this, installationEventArgs, _logger);
  373. }
  374. catch (OperationCanceledException)
  375. {
  376. lock (CurrentInstallations)
  377. {
  378. CurrentInstallations.Remove(tuple);
  379. }
  380. _logger.Info("Package installation cancelled: {0} {1}", package.name, package.versionStr);
  381. EventHelper.FireEventIfNotNull(PackageInstallationCancelled, this, installationEventArgs, _logger);
  382. throw;
  383. }
  384. catch (Exception ex)
  385. {
  386. _logger.ErrorException("Package installation failed", ex);
  387. lock (CurrentInstallations)
  388. {
  389. CurrentInstallations.Remove(tuple);
  390. }
  391. EventHelper.FireEventIfNotNull(PackageInstallationFailed, this, new InstallationFailedEventArgs
  392. {
  393. InstallationInfo = installationInfo,
  394. Exception = ex
  395. }, _logger);
  396. throw;
  397. }
  398. finally
  399. {
  400. // Dispose the progress object and remove the installation from the in-progress list
  401. innerProgress.Dispose();
  402. tuple.Item2.Dispose();
  403. }
  404. }
  405. /// <summary>
  406. /// Installs the package internal.
  407. /// </summary>
  408. /// <param name="package">The package.</param>
  409. /// <param name="progress">The progress.</param>
  410. /// <param name="cancellationToken">The cancellation token.</param>
  411. /// <returns>Task.</returns>
  412. private async Task InstallPackageInternal(PackageVersionInfo package, IProgress<double> progress, CancellationToken cancellationToken)
  413. {
  414. // Do the install
  415. await PerformPackageInstallation(progress, package, cancellationToken).ConfigureAwait(false);
  416. var extension = Path.GetExtension(package.targetFilename) ?? "";
  417. // Do plugin-specific processing
  418. if (!string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) && !string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) && !string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase))
  419. {
  420. // Set last update time if we were installed before
  421. var plugin = _applicationHost.Plugins.FirstOrDefault(p => string.Equals(p.Id.ToString(), package.guid, StringComparison.OrdinalIgnoreCase))
  422. ?? _applicationHost.Plugins.FirstOrDefault(p => p.Name.Equals(package.name, StringComparison.OrdinalIgnoreCase));
  423. if (plugin != null)
  424. {
  425. OnPluginUpdated(plugin, package);
  426. }
  427. else
  428. {
  429. OnPluginInstalled(package);
  430. }
  431. }
  432. }
  433. private async Task PerformPackageInstallation(IProgress<double> progress, PackageVersionInfo package, CancellationToken cancellationToken)
  434. {
  435. // Target based on if it is an archive or single assembly
  436. // zip archives are assumed to contain directory structures relative to our ProgramDataPath
  437. var extension = Path.GetExtension(package.targetFilename);
  438. var isArchive = string.Equals(extension, ".zip", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".rar", StringComparison.OrdinalIgnoreCase) || string.Equals(extension, ".7z", StringComparison.OrdinalIgnoreCase);
  439. var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename);
  440. // Download to temporary file so that, if interrupted, it won't destroy the existing installation
  441. var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
  442. {
  443. Url = package.sourceUrl,
  444. CancellationToken = cancellationToken,
  445. Progress = progress
  446. }).ConfigureAwait(false);
  447. cancellationToken.ThrowIfCancellationRequested();
  448. // Validate with a checksum
  449. var packageChecksum = string.IsNullOrWhiteSpace(package.checksum) ? Guid.Empty : new Guid(package.checksum);
  450. if (packageChecksum != Guid.Empty) // support for legacy uploads for now
  451. {
  452. using (var crypto = new MD5CryptoServiceProvider())
  453. using (var stream = new BufferedStream(File.OpenRead(tempFile), 100000))
  454. {
  455. var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty));
  456. if (check != packageChecksum)
  457. {
  458. throw new ApplicationException(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name));
  459. }
  460. }
  461. }
  462. cancellationToken.ThrowIfCancellationRequested();
  463. // Success - move it to the real target
  464. try
  465. {
  466. Directory.CreateDirectory(Path.GetDirectoryName(target));
  467. File.Copy(tempFile, target, true);
  468. //If it is an archive - write out a version file so we know what it is
  469. if (isArchive)
  470. {
  471. File.WriteAllText(target + ".ver", package.versionStr);
  472. }
  473. }
  474. catch (IOException e)
  475. {
  476. _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
  477. throw;
  478. }
  479. try
  480. {
  481. _fileSystem.DeleteFile(tempFile);
  482. }
  483. catch (IOException e)
  484. {
  485. // Don't fail because of this
  486. _logger.ErrorException("Error deleting temp file {0]", e, tempFile);
  487. }
  488. }
  489. /// <summary>
  490. /// Uninstalls a plugin
  491. /// </summary>
  492. /// <param name="plugin">The plugin.</param>
  493. /// <exception cref="System.ArgumentException"></exception>
  494. public void UninstallPlugin(IPlugin plugin)
  495. {
  496. plugin.OnUninstalling();
  497. // Remove it the quick way for now
  498. _applicationHost.RemovePlugin(plugin);
  499. _fileSystem.DeleteFile(plugin.AssemblyFilePath);
  500. OnPluginUninstalled(plugin);
  501. _applicationHost.NotifyPendingRestart();
  502. }
  503. /// <summary>
  504. /// Releases unmanaged and - optionally - managed resources.
  505. /// </summary>
  506. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  507. protected virtual void Dispose(bool dispose)
  508. {
  509. if (dispose)
  510. {
  511. lock (CurrentInstallations)
  512. {
  513. foreach (var tuple in CurrentInstallations)
  514. {
  515. tuple.Item2.Dispose();
  516. }
  517. CurrentInstallations.Clear();
  518. }
  519. }
  520. }
  521. public void Dispose()
  522. {
  523. Dispose(true);
  524. }
  525. }
  526. }