no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / permission / PermissionUtils.cpp
blob91d59bf900b70b60f94a42fba0c91924f9e3c2ed
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 "storage-access"_ns
23 // clang-format on
26 const size_t kPermissionNameCount = PermissionNameValues::Count;
28 static_assert(MOZ_ARRAY_LENGTH(kPermissionTypes) == kPermissionNameCount,
29 "kPermissionTypes and PermissionName count should match");
31 const nsLiteralCString& PermissionNameToType(PermissionName aName) {
32 MOZ_ASSERT((size_t)aName < ArrayLength(kPermissionTypes));
33 return kPermissionTypes[static_cast<size_t>(aName)];
36 Maybe<PermissionName> TypeToPermissionName(const nsACString& aType) {
37 // Annoyingly, "midi-sysex" is an internal permission. The public permission
38 // name is "midi" so we have to special-case it here...
39 if (aType.Equals("midi-sysex"_ns)) {
40 return Some(PermissionName::Midi);
43 // "storage-access" permissions are also annoying and require a special case.
44 if (StringBeginsWith(aType, "3rdPartyStorage^"_ns) ||
45 StringBeginsWith(aType, "3rdPartyFrameStorage^"_ns)) {
46 return Some(PermissionName::Storage_access);
49 for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) {
50 if (kPermissionTypes[i].Equals(aType)) {
51 return Some(static_cast<PermissionName>(i));
55 return Nothing();
58 PermissionState ActionToPermissionState(uint32_t aAction) {
59 switch (aAction) {
60 case nsIPermissionManager::ALLOW_ACTION:
61 return PermissionState::Granted;
63 case nsIPermissionManager::DENY_ACTION:
64 return PermissionState::Denied;
66 default:
67 case nsIPermissionManager::PROMPT_ACTION:
68 return PermissionState::Prompt;
72 } // namespace mozilla::dom