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 "mozilla/dom/Document.h"
9 #include "nsIPermissionManager.h"
11 namespace mozilla::dom
{
13 static const nsLiteralCString kPermissionTypes
[] = {
16 "desktop-notification"_ns
,
17 // Alias `push` to `desktop-notification`.
18 "desktop-notification"_ns
,
19 "persistent-storage"_ns
,
20 // "midi" is the only public permission but internally we have both "midi"
21 // and "midi-sysex" (and yes, this is confusing).
24 "screen-wake-lock"_ns
,
30 const size_t kPermissionNameCount
= ContiguousEnumSize
<PermissionName
>::value
;
32 static_assert(std::size(kPermissionTypes
) == kPermissionNameCount
,
33 "kPermissionTypes and PermissionName count should match");
35 const nsLiteralCString
& PermissionNameToType(PermissionName aName
) {
36 MOZ_ASSERT((size_t)aName
< std::size(kPermissionTypes
));
37 return kPermissionTypes
[static_cast<size_t>(aName
)];
40 Maybe
<PermissionName
> TypeToPermissionName(const nsACString
& aType
) {
41 // Annoyingly, "midi-sysex" is an internal permission. The public permission
42 // name is "midi" so we have to special-case it here...
43 if (aType
.Equals("midi-sysex"_ns
)) {
44 return Some(PermissionName::Midi
);
47 // "storage-access" permissions are also annoying and require a special case.
48 if (StringBeginsWith(aType
, "3rdPartyStorage^"_ns
) ||
49 StringBeginsWith(aType
, "3rdPartyFrameStorage^"_ns
)) {
50 return Some(PermissionName::Storage_access
);
53 for (size_t i
= 0; i
< std::size(kPermissionTypes
); ++i
) {
54 if (kPermissionTypes
[i
].Equals(aType
)) {
55 return Some(static_cast<PermissionName
>(i
));
62 PermissionState
ActionToPermissionState(uint32_t aAction
, PermissionName aName
,
63 nsIGlobalObject
* aGlobal
) {
67 case nsIPermissionManager::ALLOW_ACTION
:
68 return PermissionState::Granted
;
70 case nsIPermissionManager::DENY_ACTION
:
71 return PermissionState::Denied
;
73 case nsIPermissionManager::PROMPT_ACTION
:
74 if ((aName
== PermissionName::Camera
||
75 aName
== PermissionName::Microphone
) &&
76 !aGlobal
->ShouldResistFingerprinting(RFPTarget::MediaDevices
)) {
77 // A persisted PROMPT_ACTION means the user chose "Always Ask"
78 // which shows as "granted" to prevent websites from priming the
79 // user to escalate permission any further.
80 // Revisit if https://github.com/w3c/permissions/issues/414 reopens.
82 // This feature is not offered in resist-fingerprinting mode.
83 return PermissionState::Granted
;
85 return PermissionState::Prompt
;
88 return PermissionState::Prompt
;
92 } // namespace mozilla::dom