2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-03-07 15:03:25 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
#include "WakeLock.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2018-04-20 07:49:29 +03:00
|
|
|
#include "mozilla/dom/Event.h" // for Event
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "mozilla/Hal.h"
|
|
|
|
#include "mozilla/HalWakeLock.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "nsIPropertyBag2.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-11-21 04:14:41 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(WakeLock)
|
2014-01-09 18:20:25 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener)
|
2012-03-07 15:03:25 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
|
2013-02-15 00:41:30 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2018-09-28 08:05:07 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWakeLock)
|
2012-03-07 15:03:25 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2017-11-21 04:14:41 +03:00
|
|
|
NS_IMPL_ADDREF(WakeLock)
|
|
|
|
NS_IMPL_RELEASE(WakeLock)
|
2012-03-07 15:03:25 +04:00
|
|
|
|
|
|
|
WakeLock::WakeLock()
|
|
|
|
: mLocked(false),
|
|
|
|
mHidden(true),
|
2013-02-15 00:41:30 +04:00
|
|
|
mContentParentID(CONTENT_PROCESS_ID_UNKNOWN) {}
|
2012-03-07 15:03:25 +04:00
|
|
|
|
|
|
|
WakeLock::~WakeLock() {
|
|
|
|
DoUnlock();
|
|
|
|
DetachEventListener();
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsresult WakeLock::Init(const nsAString& aTopic, nsPIDOMWindowInner* aWindow) {
|
2013-02-15 00:41:30 +04:00
|
|
|
// Don't Init() a WakeLock twice.
|
|
|
|
MOZ_ASSERT(mTopic.IsEmpty());
|
|
|
|
|
|
|
|
if (aTopic.IsEmpty()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
mTopic.Assign(aTopic);
|
|
|
|
|
|
|
|
mWindow = do_GetWeakReference(aWindow);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Null windows are allowed. A wake lock without associated window
|
|
|
|
* is always considered invisible.
|
|
|
|
*/
|
2016-01-30 20:05:36 +03:00
|
|
|
if (aWindow) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = aWindow->GetExtantDoc();
|
2013-04-24 08:22:37 +04:00
|
|
|
NS_ENSURE_STATE(doc);
|
|
|
|
mHidden = doc->Hidden();
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
AttachEventListener();
|
|
|
|
DoLock();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-15 00:41:30 +04:00
|
|
|
nsresult WakeLock::Init(const nsAString& aTopic,
|
|
|
|
ContentParent* aContentParent) {
|
|
|
|
// Don't Init() a WakeLock twice.
|
|
|
|
MOZ_ASSERT(mTopic.IsEmpty());
|
|
|
|
MOZ_ASSERT(aContentParent);
|
|
|
|
|
|
|
|
if (aTopic.IsEmpty()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTopic.Assign(aTopic);
|
|
|
|
mContentParentID = aContentParent->ChildID();
|
|
|
|
mHidden = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoLock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
WakeLock::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* data) {
|
2013-02-15 00:41:30 +04:00
|
|
|
// If this wake lock was acquired on behalf of another process, unlock it
|
|
|
|
// when that process dies.
|
|
|
|
//
|
|
|
|
// Note that we do /not/ call DoUnlock() here! The wake lock back-end is
|
|
|
|
// already listening for ipc:content-shutdown messages and will clear out its
|
|
|
|
// tally for the process when it dies. All we need to do here is ensure that
|
|
|
|
// unlock() becomes a nop.
|
|
|
|
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
|
|
|
|
if (!props) {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without property bag as subject");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t childID = 0;
|
|
|
|
nsresult rv =
|
|
|
|
props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), &childID);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (childID == mContentParentID) {
|
|
|
|
mLocked = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("ipc:content-shutdown message without childID property");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
void WakeLock::DoLock() {
|
|
|
|
if (!mLocked) {
|
|
|
|
// Change the flag immediately to prevent recursive reentering
|
|
|
|
mLocked = true;
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
hal::ModifyWakeLock(
|
|
|
|
mTopic, hal::WAKE_LOCK_ADD_ONE,
|
2013-02-15 00:41:30 +04:00
|
|
|
mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_NO_CHANGE,
|
|
|
|
mContentParentID);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WakeLock::DoUnlock() {
|
|
|
|
if (mLocked) {
|
|
|
|
// Change the flag immediately to prevent recursive reentering
|
|
|
|
mLocked = false;
|
2013-02-15 00:41:30 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
hal::ModifyWakeLock(
|
|
|
|
mTopic, hal::WAKE_LOCK_REMOVE_ONE,
|
2013-02-15 00:41:30 +04:00
|
|
|
mHidden ? hal::WAKE_LOCK_REMOVE_ONE : hal::WAKE_LOCK_NO_CHANGE,
|
|
|
|
mContentParentID);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WakeLock::AttachEventListener() {
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow)) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = window->GetExtantDoc();
|
2013-04-24 08:22:37 +04:00
|
|
|
if (doc) {
|
|
|
|
doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"), this,
|
|
|
|
/* useCapture = */ true,
|
|
|
|
/* wantsUntrusted = */ false);
|
|
|
|
|
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(window);
|
2012-03-07 15:03:25 +04:00
|
|
|
target->AddSystemEventListener(NS_LITERAL_STRING("pagehide"), this,
|
|
|
|
/* useCapture = */ true,
|
|
|
|
/* wantsUntrusted = */ false);
|
|
|
|
target->AddSystemEventListener(NS_LITERAL_STRING("pageshow"), this,
|
|
|
|
/* useCapture = */ true,
|
|
|
|
/* wantsUntrusted = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WakeLock::DetachEventListener() {
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow)) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = window->GetExtantDoc();
|
2013-04-24 08:22:37 +04:00
|
|
|
if (doc) {
|
|
|
|
doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this,
|
|
|
|
/* useCapture = */ true);
|
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(window);
|
2012-03-07 15:03:25 +04:00
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING("pagehide"), this,
|
|
|
|
/* useCapture = */ true);
|
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING("pageshow"), this,
|
|
|
|
/* useCapture = */ true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:16:07 +04:00
|
|
|
void WakeLock::Unlock(ErrorResult& aRv) {
|
2012-03-07 15:03:25 +04:00
|
|
|
/*
|
|
|
|
* We throw NS_ERROR_DOM_INVALID_STATE_ERR on double unlock.
|
|
|
|
*/
|
|
|
|
if (!mLocked) {
|
2014-01-07 16:16:07 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DoUnlock();
|
|
|
|
DetachEventListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WakeLock::GetTopic(nsAString& aTopic) { aTopic.Assign(mTopic); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
WakeLock::HandleEvent(Event* aEvent) {
|
2012-03-07 15:03:25 +04:00
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
|
2012-11-17 02:22:56 +04:00
|
|
|
if (type.EqualsLiteral("visibilitychange")) {
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = do_QueryInterface(aEvent->GetTarget());
|
2013-04-20 02:18:32 +04:00
|
|
|
NS_ENSURE_STATE(doc);
|
|
|
|
|
2012-11-10 01:00:26 +04:00
|
|
|
bool oldHidden = mHidden;
|
2013-04-20 02:18:32 +04:00
|
|
|
mHidden = doc->Hidden();
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-11-10 01:00:26 +04:00
|
|
|
if (mLocked && oldHidden != mHidden) {
|
2012-03-07 15:03:25 +04:00
|
|
|
hal::ModifyWakeLock(
|
|
|
|
mTopic, hal::WAKE_LOCK_NO_CHANGE,
|
2013-02-15 00:41:30 +04:00
|
|
|
mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_REMOVE_ONE,
|
|
|
|
mContentParentID);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.EqualsLiteral("pagehide")) {
|
|
|
|
DoUnlock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type.EqualsLiteral("pageshow")) {
|
|
|
|
DoLock();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-28 08:05:07 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WakeLock::Unlock() {
|
|
|
|
ErrorResult error;
|
|
|
|
Unlock(error);
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2014-01-07 16:16:07 +04:00
|
|
|
nsPIDOMWindowInner* WakeLock::GetParentObject() const {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mWindow);
|
2014-01-07 16:16:07 +04:00
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|