PluginSecurityManager.cs 4.2 KB

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