PermissionUtils.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "nsIPermissionManager.h"
  6. #include "PermissionUtils.h"
  7. namespace mozilla {
  8. namespace dom {
  9. const char* kPermissionTypes[] = {
  10. "geo",
  11. "desktop-notification",
  12. // Alias `push` to `desktop-notification`.
  13. "desktop-notification"
  14. };
  15. // `-1` for the last null entry.
  16. const size_t kPermissionNameCount =
  17. MOZ_ARRAY_LENGTH(PermissionNameValues::strings) - 1;
  18. static_assert(MOZ_ARRAY_LENGTH(kPermissionTypes) == kPermissionNameCount,
  19. "kPermissionTypes and PermissionName count should match");
  20. const char*
  21. PermissionNameToType(PermissionName aName)
  22. {
  23. MOZ_ASSERT((size_t)aName < ArrayLength(kPermissionTypes));
  24. return kPermissionTypes[static_cast<size_t>(aName)];
  25. }
  26. Maybe<PermissionName>
  27. TypeToPermissionName(const char* aType)
  28. {
  29. for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) {
  30. if (!strcmp(aType, kPermissionTypes[i])) {
  31. return Some(static_cast<PermissionName>(i));
  32. }
  33. }
  34. return Nothing();
  35. }
  36. PermissionState
  37. ActionToPermissionState(uint32_t aAction)
  38. {
  39. switch (aAction) {
  40. case nsIPermissionManager::ALLOW_ACTION:
  41. return PermissionState::Granted;
  42. case nsIPermissionManager::DENY_ACTION:
  43. return PermissionState::Denied;
  44. default:
  45. case nsIPermissionManager::PROMPT_ACTION:
  46. return PermissionState::Prompt;
  47. }
  48. }
  49. } // namespace dom
  50. } // namespace mozilla