diff options
Diffstat (limited to 'dom/power/WakeLockJS.cpp')
-rw-r--r-- | dom/power/WakeLockJS.cpp | 64 |
1 files changed, 12 insertions, 52 deletions
diff --git a/dom/power/WakeLockJS.cpp b/dom/power/WakeLockJS.cpp index 50e4a716c0..3aa748fbe6 100644 --- a/dom/power/WakeLockJS.cpp +++ b/dom/power/WakeLockJS.cpp @@ -127,8 +127,7 @@ NS_IMPL_CYCLE_COLLECTING_RELEASE(WakeLockJS) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WakeLockJS) NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY - NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener) - NS_INTERFACE_MAP_ENTRY(nsIDocumentActivity) + NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver) NS_INTERFACE_MAP_ENTRY(nsIObserver) NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) NS_INTERFACE_MAP_END @@ -148,14 +147,12 @@ JSObject* WakeLockJS::WrapObject(JSContext* aCx, // https://w3c.github.io/screen-wake-lock/#the-request-method Step 7.3 Result<already_AddRefed<WakeLockSentinel>, WakeLockJS::RequestError> -WakeLockJS::Obtain(WakeLockType aType) { +WakeLockJS::Obtain(WakeLockType aType, Document* aDoc) { // Step 7.3.1. check visibility again - nsCOMPtr<Document> doc = mWindow->GetExtantDoc(); - if (!doc) { - return Err(RequestError::InternalFailure); - } - if (doc->Hidden()) { - return Err(RequestError::DocHidden); + // 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 = @@ -166,7 +163,7 @@ WakeLockJS::Obtain(WakeLockType aType) { } // Steps 7.3.4. append lock to locks - doc->ActiveWakeLocks(aType).Insert(lock); + aDoc->ActiveWakeLocks(aType).Insert(lock); return lock.forget(); } @@ -191,8 +188,9 @@ already_AddRefed<Promise> WakeLockJS::Request(WakeLockType aType, // 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, self = RefPtr<WakeLockJS>(this)]() { - auto lockOrErr = self->Obtain(aType); + "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); @@ -208,30 +206,16 @@ already_AddRefed<Promise> WakeLockJS::Request(WakeLockType aType, } void WakeLockJS::AttachListeners() { - nsCOMPtr<Document> doc = mWindow->GetExtantDoc(); - MOZ_ASSERT(doc); - DebugOnly<nsresult> rv = - doc->AddSystemEventListener(u"visibilitychange"_ns, this, true, false); - MOZ_ASSERT(NS_SUCCEEDED(rv)); - doc->RegisterActivityObserver(ToSupports(this)); - hal::RegisterBatteryObserver(this); nsCOMPtr<nsIPrefBranch> prefBranch = do_GetService(NS_PREFSERVICE_CONTRACTID); MOZ_ASSERT(prefBranch); - rv = prefBranch->AddObserver("dom.screenwakelock.enabled", this, true); + DebugOnly<nsresult> rv = + prefBranch->AddObserver("dom.screenwakelock.enabled", this, true); MOZ_ASSERT(NS_SUCCEEDED(rv)); } void WakeLockJS::DetachListeners() { - if (mWindow) { - if (nsCOMPtr<Document> doc = mWindow->GetExtantDoc()) { - doc->RemoveSystemEventListener(u"visibilitychange"_ns, this, true); - - doc->UnregisterActivityObserver(ToSupports(this)); - } - } - hal::UnregisterBatteryObserver(this); if (nsCOMPtr<nsIPrefBranch> prefBranch = @@ -252,30 +236,6 @@ NS_IMETHODIMP WakeLockJS::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } -void WakeLockJS::NotifyOwnerDocumentActivityChanged() { - nsCOMPtr<Document> doc = mWindow->GetExtantDoc(); - MOZ_ASSERT(doc); - if (!doc->IsActive()) { - doc->UnlockAllWakeLocks(WakeLockType::Screen); - } -} - -NS_IMETHODIMP WakeLockJS::HandleEvent(Event* aEvent) { - nsAutoString type; - aEvent->GetType(type); - - if (type.EqualsLiteral("visibilitychange")) { - nsCOMPtr<Document> doc = do_QueryInterface(aEvent->GetTarget()); - NS_ENSURE_STATE(doc); - - if (doc->Hidden()) { - doc->UnlockAllWakeLocks(WakeLockType::Screen); - } - } - - return NS_OK; -} - void WakeLockJS::Notify(const hal::BatteryInformation& aBatteryInfo) { if (aBatteryInfo.level() > MIN_BATTERY_LEVEL || aBatteryInfo.charging()) { return; |