PluginSecurityManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Security;
  3. using MediaBrowser.Model.Serialization;
  4. using Mediabrowser.Model.Entities;
  5. using Mediabrowser.PluginSecurity;
  6. using MediaBrowser.Common.Net;
  7. using System;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Common.Implementations.Security
  11. {
  12. /// <summary>
  13. /// Class PluginSecurityManager
  14. /// </summary>
  15. public class PluginSecurityManager : ISecurityManager
  16. {
  17. /// <summary>
  18. /// The _is MB supporter
  19. /// </summary>
  20. private bool? _isMBSupporter;
  21. /// <summary>
  22. /// The _is MB supporter initialized
  23. /// </summary>
  24. private bool _isMBSupporterInitialized;
  25. /// <summary>
  26. /// The _is MB supporter sync lock
  27. /// </summary>
  28. private object _isMBSupporterSyncLock = new object();
  29. /// <summary>
  30. /// Gets a value indicating whether this instance is MB supporter.
  31. /// </summary>
  32. /// <value><c>true</c> if this instance is MB supporter; otherwise, <c>false</c>.</value>
  33. public bool IsMBSupporter
  34. {
  35. get
  36. {
  37. LazyInitializer.EnsureInitialized(ref _isMBSupporter, ref _isMBSupporterInitialized, ref _isMBSupporterSyncLock, () => GetRegistrationStatus("MBSupporter").Result.IsRegistered);
  38. return _isMBSupporter.Value;
  39. }
  40. }
  41. private IHttpClient _httpClient;
  42. private IJsonSerializer _jsonSerializer;
  43. private IApplicationHost _appHost;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="PluginSecurityManager" /> class.
  46. /// </summary>
  47. public PluginSecurityManager(IApplicationHost appHost, IHttpClient httpClient, IJsonSerializer jsonSerializer, IApplicationPaths appPaths)
  48. {
  49. if (httpClient == null)
  50. {
  51. throw new ArgumentNullException("httpClient");
  52. }
  53. _appHost = appHost;
  54. _httpClient = httpClient;
  55. _jsonSerializer = jsonSerializer;
  56. MBRegistration.Init(appPaths);
  57. }
  58. /// <summary>
  59. /// Gets the registration status.
  60. /// </summary>
  61. /// <param name="feature">The feature.</param>
  62. /// <param name="mb2Equivalent">The MB2 equivalent.</param>
  63. /// <returns>Task{MBRegistrationRecord}.</returns>
  64. public async Task<MBRegistrationRecord> GetRegistrationStatus(string feature, string mb2Equivalent = null)
  65. {
  66. return await MBRegistration.GetRegistrationStatus(_httpClient, _jsonSerializer, feature, mb2Equivalent).ConfigureAwait(false);
  67. }
  68. /// <summary>
  69. /// Gets or sets the supporter key.
  70. /// </summary>
  71. /// <value>The supporter key.</value>
  72. public string SupporterKey
  73. {
  74. get { return MBRegistration.SupporterKey; }
  75. set
  76. {
  77. if (value != MBRegistration.SupporterKey)
  78. {
  79. MBRegistration.SupporterKey = value;
  80. // Clear this so it will re-evaluate
  81. ResetSupporterInfo();
  82. // And we'll need to restart to re-evaluate the status of plug-ins
  83. _appHost.NotifyPendingRestart();
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets the legacy key.
  89. /// </summary>
  90. /// <value>The legacy key.</value>
  91. public string LegacyKey
  92. {
  93. get { return MBRegistration.LegacyKey; }
  94. set
  95. {
  96. MBRegistration.LegacyKey = value;
  97. // And we'll need to restart to re-evaluate the status of plug-ins
  98. _appHost.NotifyPendingRestart();
  99. }
  100. }
  101. /// <summary>
  102. /// Resets the supporter info.
  103. /// </summary>
  104. private void ResetSupporterInfo()
  105. {
  106. _isMBSupporter = null;
  107. _isMBSupporterInitialized = false;
  108. }
  109. }
  110. }