PermissionRequestBase.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "PermissionRequestBase.h"
  6. #include "MainThreadUtils.h"
  7. #include "mozilla/Assertions.h"
  8. #include "mozilla/Services.h"
  9. #include "mozilla/dom/Element.h"
  10. #include "nsIDOMWindow.h"
  11. #include "nsIObserverService.h"
  12. #include "nsIPrincipal.h"
  13. #include "nsPIDOMWindow.h"
  14. #include "nsXULAppAPI.h"
  15. namespace mozilla {
  16. namespace dom {
  17. namespace indexedDB {
  18. using namespace mozilla::services;
  19. namespace {
  20. #define IDB_PREFIX "indexedDB"
  21. #define TOPIC_PREFIX IDB_PREFIX "-permissions-"
  22. const char kPermissionString[] = IDB_PREFIX;
  23. const char kPermissionPromptTopic[] = TOPIC_PREFIX "prompt";
  24. #ifdef DEBUG
  25. const char kPermissionResponseTopic[] = TOPIC_PREFIX "response";
  26. #endif
  27. #undef TOPIC_PREFIX
  28. #undef IDB_PREFIX
  29. const uint32_t kPermissionDefault = nsIPermissionManager::UNKNOWN_ACTION;
  30. void
  31. AssertSanity()
  32. {
  33. MOZ_ASSERT(XRE_IsParentProcess());
  34. MOZ_ASSERT(NS_IsMainThread());
  35. }
  36. } // namespace
  37. PermissionRequestBase::PermissionRequestBase(Element* aOwnerElement,
  38. nsIPrincipal* aPrincipal)
  39. : mOwnerElement(aOwnerElement)
  40. , mPrincipal(aPrincipal)
  41. {
  42. AssertSanity();
  43. MOZ_ASSERT(aOwnerElement);
  44. MOZ_ASSERT(aPrincipal);
  45. }
  46. PermissionRequestBase::~PermissionRequestBase()
  47. {
  48. AssertSanity();
  49. }
  50. // static
  51. nsresult
  52. PermissionRequestBase::GetCurrentPermission(nsIPrincipal* aPrincipal,
  53. PermissionValue* aCurrentValue)
  54. {
  55. AssertSanity();
  56. MOZ_ASSERT(aPrincipal);
  57. MOZ_ASSERT(aCurrentValue);
  58. nsCOMPtr<nsIPermissionManager> permMan = GetPermissionManager();
  59. if (NS_WARN_IF(!permMan)) {
  60. return NS_ERROR_FAILURE;
  61. }
  62. uint32_t intPermission;
  63. nsresult rv = permMan->TestExactPermissionFromPrincipal(
  64. aPrincipal,
  65. kPermissionString,
  66. &intPermission);
  67. if (NS_WARN_IF(NS_FAILED(rv))) {
  68. return rv;
  69. }
  70. PermissionValue permission =
  71. PermissionValueForIntPermission(intPermission);
  72. MOZ_ASSERT(permission == kPermissionAllowed ||
  73. permission == kPermissionDenied ||
  74. permission == kPermissionPrompt);
  75. *aCurrentValue = permission;
  76. return NS_OK;
  77. }
  78. // static
  79. auto
  80. PermissionRequestBase::PermissionValueForIntPermission(uint32_t aIntPermission)
  81. -> PermissionValue
  82. {
  83. AssertSanity();
  84. switch (aIntPermission) {
  85. case kPermissionDefault:
  86. return kPermissionPrompt;
  87. case kPermissionAllowed:
  88. return kPermissionAllowed;
  89. case kPermissionDenied:
  90. return kPermissionDenied;
  91. default:
  92. MOZ_CRASH("Bad permission!");
  93. }
  94. MOZ_CRASH("Should never get here!");
  95. }
  96. nsresult
  97. PermissionRequestBase::PromptIfNeeded(PermissionValue* aCurrentValue)
  98. {
  99. AssertSanity();
  100. MOZ_ASSERT(aCurrentValue);
  101. MOZ_ASSERT(mPrincipal);
  102. // Tricky, we want to release the window and principal in all cases except
  103. // when we successfully prompt.
  104. nsCOMPtr<Element> element;
  105. mOwnerElement.swap(element);
  106. nsCOMPtr<nsIPrincipal> principal;
  107. mPrincipal.swap(principal);
  108. PermissionValue currentValue;
  109. nsresult rv = GetCurrentPermission(principal, &currentValue);
  110. if (NS_WARN_IF(NS_FAILED(rv))) {
  111. return rv;
  112. }
  113. MOZ_ASSERT(currentValue != kPermissionDefault);
  114. if (currentValue == kPermissionPrompt) {
  115. nsCOMPtr<nsIObserverService> obsSvc = GetObserverService();
  116. if (NS_WARN_IF(!obsSvc)) {
  117. return NS_ERROR_FAILURE;
  118. }
  119. // We're about to prompt so swap the members back.
  120. element.swap(mOwnerElement);
  121. principal.swap(mPrincipal);
  122. rv = obsSvc->NotifyObservers(static_cast<nsIObserver*>(this),
  123. kPermissionPromptTopic,
  124. nullptr);
  125. if (NS_WARN_IF(NS_FAILED(rv))) {
  126. // Finally release if we failed the prompt.
  127. mOwnerElement = nullptr;
  128. mPrincipal = nullptr;
  129. return rv;
  130. }
  131. }
  132. *aCurrentValue = currentValue;
  133. return NS_OK;
  134. }
  135. void
  136. PermissionRequestBase::SetExplicitPermission(nsIPrincipal* aPrincipal,
  137. uint32_t aIntPermission)
  138. {
  139. AssertSanity();
  140. MOZ_ASSERT(aPrincipal);
  141. MOZ_ASSERT(aIntPermission == kPermissionAllowed ||
  142. aIntPermission == kPermissionDenied);
  143. nsCOMPtr<nsIPermissionManager> permMan = GetPermissionManager();
  144. if (NS_WARN_IF(!permMan)) {
  145. return;
  146. }
  147. nsresult rv = permMan->AddFromPrincipal(aPrincipal,
  148. kPermissionString,
  149. aIntPermission,
  150. nsIPermissionManager::EXPIRE_NEVER,
  151. /* aExpireTime */ 0);
  152. if (NS_WARN_IF(NS_FAILED(rv))) {
  153. return;
  154. }
  155. }
  156. NS_IMPL_ISUPPORTS(PermissionRequestBase, nsIObserver, nsIInterfaceRequestor)
  157. NS_IMETHODIMP
  158. PermissionRequestBase::GetInterface(const nsIID& aIID,
  159. void** aResult)
  160. {
  161. AssertSanity();
  162. if (aIID.Equals(NS_GET_IID(nsIObserver))) {
  163. return QueryInterface(aIID, aResult);
  164. }
  165. if (aIID.Equals(NS_GET_IID(nsIDOMNode)) && mOwnerElement) {
  166. return mOwnerElement->QueryInterface(aIID, aResult);
  167. }
  168. *aResult = nullptr;
  169. return NS_ERROR_NOT_AVAILABLE;
  170. }
  171. NS_IMETHODIMP
  172. PermissionRequestBase::Observe(nsISupports* aSubject,
  173. const char* aTopic,
  174. const char16_t* aData)
  175. {
  176. AssertSanity();
  177. MOZ_ASSERT(!strcmp(aTopic, kPermissionResponseTopic));
  178. MOZ_ASSERT(mOwnerElement);
  179. MOZ_ASSERT(mPrincipal);
  180. nsCOMPtr<Element> element;
  181. element.swap(mOwnerElement);
  182. nsCOMPtr<nsIPrincipal> principal;
  183. mPrincipal.swap(principal);
  184. nsresult rv;
  185. uint32_t promptResult = nsDependentString(aData).ToInteger(&rv);
  186. MOZ_ALWAYS_SUCCEEDS(rv);
  187. // The UI prompt code will only return one of these three values. We have to
  188. // transform it to our values.
  189. MOZ_ASSERT(promptResult == kPermissionDefault ||
  190. promptResult == kPermissionAllowed ||
  191. promptResult == kPermissionDenied);
  192. if (promptResult != kPermissionDefault) {
  193. // Save explicitly allowed or denied permissions now.
  194. SetExplicitPermission(principal, promptResult);
  195. }
  196. PermissionValue permission;
  197. switch (promptResult) {
  198. case kPermissionDefault:
  199. permission = kPermissionPrompt;
  200. break;
  201. case kPermissionAllowed:
  202. permission = kPermissionAllowed;
  203. break;
  204. case kPermissionDenied:
  205. permission = kPermissionDenied;
  206. break;
  207. default:
  208. MOZ_CRASH("Bad prompt result!");
  209. }
  210. OnPromptComplete(permission);
  211. return NS_OK;
  212. }
  213. } // namespace indexedDB
  214. } // namespace dom
  215. } // namespace mozilla