Bug 1589554 - Part 3: Screen Wake Lock permission integration. r=webidl,dom-core,smaug,pbz,edgar

Depends on D189509

Differential Revision: https://phabricator.services.mozilla.com/D189510
This commit is contained in:
Vincent Hilla 2023-12-05 23:58:06 +00:00
parent 248841bc9c
commit f0dc3526ed
9 changed files with 37 additions and 18 deletions

View file

@ -19,7 +19,8 @@ static const nsLiteralCString kPermissionTypes[] = {
// "midi" is the only public permission but internally we have both "midi" // "midi" is the only public permission but internally we have both "midi"
// and "midi-sysex" (and yes, this is confusing). // and "midi-sysex" (and yes, this is confusing).
"midi"_ns, "midi"_ns,
"storage-access"_ns "storage-access"_ns,
"screen-wake-lock"_ns
// clang-format on // clang-format on
}; };

View file

@ -68,6 +68,7 @@ CreatePermissionStatus(JSContext* aCx, JS::Handle<JSObject*> aPermission,
case PermissionName::Notifications: case PermissionName::Notifications:
case PermissionName::Push: case PermissionName::Push:
case PermissionName::Persistent_storage: case PermissionName::Persistent_storage:
case PermissionName::Screen_wake_lock:
return PermissionStatus::Create(aWindow, permission.mName); return PermissionStatus::Create(aWindow, permission.mName);
default: default:

View file

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ErrorList.h" #include "ErrorList.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h" #include "mozilla/Assertions.h"
#include "mozilla/dom/Document.h" #include "mozilla/dom/Document.h"
#include "mozilla/dom/Event.h" #include "mozilla/dom/Event.h"
@ -18,6 +19,9 @@
#include "nsIGlobalObject.h" #include "nsIGlobalObject.h"
#include "nsISupports.h" #include "nsISupports.h"
#include "nsPIDOMWindow.h" #include "nsPIDOMWindow.h"
#include "nsContentPermissionHelper.h"
#include "nscore.h"
#include "WakeLock.h"
#include "WakeLockJS.h" #include "WakeLockJS.h"
#include "WakeLockSentinel.h" #include "WakeLockSentinel.h"
@ -35,6 +39,8 @@ nsLiteralCString WakeLockJS::GetRequestErrorMessage(RequestError aRv) {
return "The pref dom.screenwakelock.enabled is disabled."_ns; return "The pref dom.screenwakelock.enabled is disabled."_ns;
case RequestError::InternalFailure: case RequestError::InternalFailure:
return "A browser-internal error occured."_ns; return "A browser-internal error occured."_ns;
case RequestError::PermissionDenied:
return "Permission to request screen-wake-lock was denied."_ns;
default: default:
MOZ_ASSERT_UNREACHABLE("Unknown error reason"); MOZ_ASSERT_UNREACHABLE("Unknown error reason");
return "Unknown error"_ns; return "Unknown error"_ns;
@ -126,7 +132,7 @@ WakeLockJS::Obtain(WakeLockType aType) {
lock->AcquireActualLock(); lock->AcquireActualLock();
} }
// Steps 7.3.4., 7.3.5. Append lock to locks and resolve promise with lock // Steps 7.3.4. append lock to locks
doc->ActiveWakeLocks(aType).Insert(lock); doc->ActiveWakeLocks(aType).Insert(lock);
return lock.forget(); return lock.forget();
@ -148,16 +154,19 @@ already_AddRefed<Promise> WakeLockJS::Request(WakeLockType aType,
return promise.forget(); return promise.forget();
} }
// Step 7.1. Requesting permission to use screen-wake-lock // For now, we don't check the permission as we always grant the lock
// Note: implemented in a future part, for now always try to obtain // Step 7.3. Queue a task
auto lockOrErr = Obtain(aType); NS_DispatchToMainThread(NS_NewRunnableFunction(
if (lockOrErr.isOk()) { "ObtainWakeLock", [aType, promise, self = RefPtr<WakeLockJS>(this)]() {
RefPtr<WakeLockSentinel> lock = lockOrErr.unwrap(); auto lockOrErr = self->Obtain(aType);
promise->MaybeResolve(lock); if (lockOrErr.isOk()) {
} else { RefPtr<WakeLockSentinel> lock = lockOrErr.unwrap();
promise->MaybeRejectWithNotAllowedError( promise->MaybeResolve(lock);
GetRequestErrorMessage(lockOrErr.unwrapErr())); } else {
} promise->MaybeRejectWithNotAllowedError(
GetRequestErrorMessage(lockOrErr.unwrapErr()));
}
}));
return promise.forget(); return promise.forget();
} }

View file

@ -68,7 +68,8 @@ class WakeLockJS final : public nsIDOMEventListener,
DocHidden, DocHidden,
PolicyDisallowed, PolicyDisallowed,
PrefDisabled, PrefDisabled,
InternalFailure InternalFailure,
PermissionDenied
}; };
static nsLiteralCString GetRequestErrorMessage(RequestError aRv); static nsLiteralCString GetRequestErrorMessage(RequestError aRv);

View file

@ -13,7 +13,8 @@ enum PermissionName {
"push", "push",
"persistent-storage", "persistent-storage",
"midi", "midi",
"storage-access" // Defined in https://privacycg.github.io/storage-access/#permissions-integration "storage-access", // Defined in https://privacycg.github.io/storage-access/#permissions-integration
"screen-wake-lock" // Defined in https://w3c.github.io/screen-wake-lock/
}; };
[GenerateInit] [GenerateInit]

View file

@ -45,7 +45,8 @@ static const DelegateInfo sPermissionsMap[] = {
{"microphone", u"microphone", DelegatePolicy::eDelegateUseFeaturePolicy}, {"microphone", u"microphone", DelegatePolicy::eDelegateUseFeaturePolicy},
{"screen", u"display-capture", DelegatePolicy::eDelegateUseFeaturePolicy}, {"screen", u"display-capture", DelegatePolicy::eDelegateUseFeaturePolicy},
{"xr", u"xr-spatial-tracking", DelegatePolicy::eDelegateUseFeaturePolicy}, {"xr", u"xr-spatial-tracking", DelegatePolicy::eDelegateUseFeaturePolicy},
}; {"screen-wake-lock", u"screen-wake-lock",
DelegatePolicy::eDelegateUseFeaturePolicy}};
static_assert(PermissionDelegateHandler::DELEGATED_PERMISSION_COUNT == static_assert(PermissionDelegateHandler::DELEGATED_PERMISSION_COUNT ==
(sizeof(sPermissionsMap) / sizeof(DelegateInfo)), (sizeof(sPermissionsMap) / sizeof(DelegateInfo)),

View file

@ -52,7 +52,7 @@ class PermissionDelegateHandler final : public nsIPermissionDelegateHandler {
explicit PermissionDelegateHandler() = default; explicit PermissionDelegateHandler() = default;
explicit PermissionDelegateHandler(mozilla::dom::Document* aDocument); explicit PermissionDelegateHandler(mozilla::dom::Document* aDocument);
static constexpr size_t DELEGATED_PERMISSION_COUNT = 12; static constexpr size_t DELEGATED_PERMISSION_COUNT = 13;
typedef struct DelegatedPermissionList { typedef struct DelegatedPermissionList {
Array<uint32_t, DELEGATED_PERMISSION_COUNT> mPermissions; Array<uint32_t, DELEGATED_PERMISSION_COUNT> mPermissions;

View file

@ -107,8 +107,8 @@ bool HasDefaultPref(const nsACString& aType) {
// A list of permissions that can have a fallback default permission // A list of permissions that can have a fallback default permission
// set under the permissions.default.* pref. // set under the permissions.default.* pref.
static const nsLiteralCString kPermissionsWithDefaults[] = { static const nsLiteralCString kPermissionsWithDefaults[] = {
"camera"_ns, "microphone"_ns, "geo"_ns, "desktop-notification"_ns, "camera"_ns, "microphone"_ns, "geo"_ns, "desktop-notification"_ns,
"shortcuts"_ns}; "shortcuts"_ns, "screen-wake-lock"_ns};
if (!aType.IsEmpty()) { if (!aType.IsEmpty()) {
for (const auto& perm : kPermissionsWithDefaults) { for (const auto& perm : kPermissionsWithDefaults) {

View file

@ -12819,6 +12819,11 @@
value: 1 value: 1
mirror: always mirror: always
- name: permissions.default.screen-wake-lock
type: RelaxedAtomicUint32
value: 1
mirror: always
- name: permissions.isolateBy.userContext - name: permissions.isolateBy.userContext
type: RelaxedAtomicBool type: RelaxedAtomicBool
value: false value: false