Bug 1437171 - Add Web MIDI support in `navigator.permissions.query()`. r=webidl,smaug,pbz
[gecko.git] / dom / permission / PermissionUtils.cpp
blob51deceee8b74f9626fe76d7de6779aa30d88e96b
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "PermissionUtils.h"
8 #include "nsIPermissionManager.h"
10 namespace mozilla::dom {
12 static const nsLiteralCString kPermissionTypes[] = {
13 // clang-format off
14 "geo"_ns,
15 "desktop-notification"_ns,
16 // Alias `push` to `desktop-notification`.
17 "desktop-notification"_ns,
18 "persistent-storage"_ns,
19 // "midi" is the only public permission but internally we have both "midi"
20 // and "midi-sysex" (and yes, this is confusing).
21 "midi"_ns
22 // clang-format on
25 const size_t kPermissionNameCount = PermissionNameValues::Count;
27 static_assert(MOZ_ARRAY_LENGTH(kPermissionTypes) == kPermissionNameCount,
28 "kPermissionTypes and PermissionName count should match");
30 const nsLiteralCString& PermissionNameToType(PermissionName aName) {
31 MOZ_ASSERT((size_t)aName < ArrayLength(kPermissionTypes));
32 return kPermissionTypes[static_cast<size_t>(aName)];
35 Maybe<PermissionName> TypeToPermissionName(const nsACString& aType) {
36 // Annoyingly, "midi-sysex" is an internal permission. The public permission
37 // name is "midi" so we have to special-case it here...
38 if (aType.Equals("midi-sysex"_ns)) {
39 return Some(PermissionName::Midi);
42 for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) {
43 if (kPermissionTypes[i].Equals(aType)) {
44 return Some(static_cast<PermissionName>(i));
48 return Nothing();
51 PermissionState ActionToPermissionState(uint32_t aAction) {
52 switch (aAction) {
53 case nsIPermissionManager::ALLOW_ACTION:
54 return PermissionState::Granted;
56 case nsIPermissionManager::DENY_ACTION:
57 return PermissionState::Denied;
59 default:
60 case nsIPermissionManager::PROMPT_ACTION:
61 return PermissionState::Prompt;
65 } // namespace mozilla::dom