PluginSecurityManager.cs 4.0 KB

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