/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
nsLiteralCString WakeLockJS::GetRequestErrorMessage(RequestError aRv) { switch (aRv) { case RequestError::DocInactive: return"The requesting document is inactive."_ns; case RequestError::DocHidden: return"The requesting document is hidden."_ns; case RequestError::PolicyDisallowed: return"A permissions policy does not allow screen-wake-lock for the requesting document."_ns; case RequestError::PrefDisabled: return"The pref dom.screenwakelock.enabled is disabled."_ns; case RequestError::InternalFailure: return"A browser-internal error occured."_ns; case RequestError::PermissionDenied: return"Permission to request screen-wake-lock was denied."_ns; default:
MOZ_ASSERT_UNREACHABLE("Unknown error reason"); return"Unknown error"_ns;
}
}
// https://w3c.github.io/screen-wake-lock/#the-request-method Step 7.3
Result<already_AddRefed<WakeLockSentinel>, WakeLockJS::RequestError>
WakeLockJS::Obtain(WakeLockType aType, Document* aDoc) { // Step 7.3.1. check visibility again // Out of spec, but also check everything else
RequestError rv = WakeLockAllowedForDocument(aDoc); if (rv != RequestError::Success) { return Err(rv);
} // Step 7.3.3. let lock be a new WakeLockSentinel
RefPtr<WakeLockSentinel> lock =
MakeRefPtr<WakeLockSentinel>(mWindow->AsGlobal(), aType); // Step 7.3.2. acquire a wake lock if (IsWakeLockApplicable(aType)) {
lock->AcquireActualLock();
}
// Steps 7.3.4. append lock to locks
aDoc->ActiveWakeLocks(aType).Insert(lock);
return lock.forget();
}
// https://w3c.github.io/screen-wake-lock/#the-request-method
already_AddRefed<Promise> WakeLockJS::Request(WakeLockType aType,
ErrorResult& aRv) {
MOZ_LOG(sLogger, LogLevel::Debug, ("Received request for wake lock"));
nsCOMPtr<nsIGlobalObject> global = mWindow->AsGlobal();
// For now, we don't check the permission as we always grant the lock // Step 7.3. Queue a task
NS_DispatchToMainThread(NS_NewRunnableFunction( "ObtainWakeLock",
[aType, promise, doc, self = RefPtr<WakeLockJS>(this)]() { auto lockOrErr = self->Obtain(aType, doc); if (lockOrErr.isOk()) {
RefPtr<WakeLockSentinel> lock = lockOrErr.unwrap();
promise->MaybeResolve(lock);
MOZ_LOG(sLogger, LogLevel::Debug,
("Resolved promise with wake lock sentinel"));
} else {
promise->MaybeRejectWithNotAllowedError(
GetRequestErrorMessage(lockOrErr.unwrapErr()));
}
}));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.