PermissionStatus.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/PermissionStatus.h"
  6. #include "mozilla/AsyncEventDispatcher.h"
  7. #include "mozilla/Services.h"
  8. #include "nsIPermissionManager.h"
  9. #include "PermissionObserver.h"
  10. #include "PermissionUtils.h"
  11. namespace mozilla {
  12. namespace dom {
  13. /* static */ already_AddRefed<PermissionStatus>
  14. PermissionStatus::Create(nsPIDOMWindowInner* aWindow,
  15. PermissionName aName,
  16. ErrorResult& aRv)
  17. {
  18. RefPtr<PermissionStatus> status = new PermissionStatus(aWindow, aName);
  19. aRv = status->Init();
  20. if (NS_WARN_IF(aRv.Failed())) {
  21. return nullptr;
  22. }
  23. return status.forget();
  24. }
  25. PermissionStatus::PermissionStatus(nsPIDOMWindowInner* aWindow,
  26. PermissionName aName)
  27. : DOMEventTargetHelper(aWindow)
  28. , mName(aName)
  29. , mState(PermissionState::Denied)
  30. {
  31. }
  32. nsresult
  33. PermissionStatus::Init()
  34. {
  35. mObserver = PermissionObserver::GetInstance();
  36. if (NS_WARN_IF(!mObserver)) {
  37. return NS_ERROR_FAILURE;
  38. }
  39. mObserver->AddSink(this);
  40. nsresult rv = UpdateState();
  41. if (NS_WARN_IF(NS_FAILED(rv))) {
  42. return rv;
  43. }
  44. return NS_OK;
  45. }
  46. PermissionStatus::~PermissionStatus()
  47. {
  48. if (mObserver) {
  49. mObserver->RemoveSink(this);
  50. }
  51. }
  52. JSObject*
  53. PermissionStatus::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  54. {
  55. return PermissionStatusBinding::Wrap(aCx, this, aGivenProto);
  56. }
  57. nsresult
  58. PermissionStatus::UpdateState()
  59. {
  60. nsCOMPtr<nsIPermissionManager> permMgr = services::GetPermissionManager();
  61. if (NS_WARN_IF(!permMgr)) {
  62. return NS_ERROR_FAILURE;
  63. }
  64. nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  65. if (NS_WARN_IF(!window)) {
  66. return NS_ERROR_FAILURE;
  67. }
  68. uint32_t action = nsIPermissionManager::DENY_ACTION;
  69. nsresult rv = permMgr->TestPermissionFromWindow(window,
  70. PermissionNameToType(mName),
  71. &action);
  72. if (NS_WARN_IF(NS_FAILED(rv))) {
  73. return rv;
  74. }
  75. mState = ActionToPermissionState(action);
  76. return NS_OK;
  77. }
  78. already_AddRefed<nsIPrincipal>
  79. PermissionStatus::GetPrincipal() const
  80. {
  81. nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  82. if (NS_WARN_IF(!window)) {
  83. return nullptr;
  84. }
  85. nsIDocument* doc = window->GetExtantDoc();
  86. if (NS_WARN_IF(!doc)) {
  87. return nullptr;
  88. }
  89. nsCOMPtr<nsIPrincipal> principal =
  90. mozilla::BasePrincipal::Cast(doc->NodePrincipal())->CloneStrippingUserContextIdAndFirstPartyDomain();
  91. NS_ENSURE_TRUE(principal, nullptr);
  92. return principal.forget();
  93. }
  94. void
  95. PermissionStatus::PermissionChanged()
  96. {
  97. auto oldState = mState;
  98. UpdateState();
  99. if (mState != oldState) {
  100. RefPtr<AsyncEventDispatcher> eventDispatcher =
  101. new AsyncEventDispatcher(this, NS_LITERAL_STRING("change"), false);
  102. eventDispatcher->PostDOMEvent();
  103. }
  104. }
  105. } // namespace dom
  106. } // namespace mozilla