Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / permission / PermissionUtils.cpp
blobf0541d4927cc21b56399ee7889b496e46bf37f27
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 // clang-format on
22 const size_t kPermissionNameCount = PermissionNameValues::Count;
24 static_assert(MOZ_ARRAY_LENGTH(kPermissionTypes) == kPermissionNameCount,
25 "kPermissionTypes and PermissionName count should match");
27 const nsLiteralCString& PermissionNameToType(PermissionName aName) {
28 MOZ_ASSERT((size_t)aName < ArrayLength(kPermissionTypes));
29 return kPermissionTypes[static_cast<size_t>(aName)];
32 Maybe<PermissionName> TypeToPermissionName(const nsACString& aType) {
33 for (size_t i = 0; i < ArrayLength(kPermissionTypes); ++i) {
34 if (kPermissionTypes[i].Equals(aType)) {
35 return Some(static_cast<PermissionName>(i));
39 return Nothing();
42 PermissionState ActionToPermissionState(uint32_t aAction) {
43 switch (aAction) {
44 case nsIPermissionManager::ALLOW_ACTION:
45 return PermissionState::Granted;
47 case nsIPermissionManager::DENY_ACTION:
48 return PermissionState::Denied;
50 default:
51 case nsIPermissionManager::PROMPT_ACTION:
52 return PermissionState::Prompt;
56 } // namespace mozilla::dom