michael@0: /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "WakeLock.h" michael@0: #include "mozilla/dom/ContentParent.h" michael@0: #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent() michael@0: #include "mozilla/dom/MozWakeLockBinding.h" michael@0: #include "mozilla/Hal.h" michael@0: #include "mozilla/HalWakeLock.h" michael@0: #include "nsError.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsIDOMEvent.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIPropertyBag2.h" michael@0: michael@0: using namespace mozilla::hal; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WakeLock) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WakeLock) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener) michael@0: NS_INTERFACE_MAP_ENTRY(nsIObserver) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(WakeLock) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(WakeLock) michael@0: michael@0: WakeLock::WakeLock() michael@0: : mLocked(false) michael@0: , mHidden(true) michael@0: , mContentParentID(CONTENT_PROCESS_ID_UNKNOWN) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: WakeLock::~WakeLock() michael@0: { michael@0: DoUnlock(); michael@0: DetachEventListener(); michael@0: } michael@0: michael@0: JSObject* michael@0: WakeLock::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozWakeLockBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: nsresult michael@0: WakeLock::Init(const nsAString &aTopic, nsIDOMWindow *aWindow) michael@0: { michael@0: // Don't Init() a WakeLock twice. michael@0: MOZ_ASSERT(mTopic.IsEmpty()); michael@0: michael@0: if (aTopic.IsEmpty()) { michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: mTopic.Assign(aTopic); michael@0: michael@0: mWindow = do_GetWeakReference(aWindow); michael@0: nsCOMPtr window = do_QueryInterface(aWindow); michael@0: michael@0: /** michael@0: * Null windows are allowed. A wake lock without associated window michael@0: * is always considered invisible. michael@0: */ michael@0: if (window) { michael@0: nsCOMPtr doc = window->GetExtantDoc(); michael@0: NS_ENSURE_STATE(doc); michael@0: mHidden = doc->Hidden(); michael@0: } michael@0: michael@0: AttachEventListener(); michael@0: DoLock(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: WakeLock::Init(const nsAString& aTopic, ContentParent* aContentParent) michael@0: { michael@0: // Don't Init() a WakeLock twice. michael@0: MOZ_ASSERT(mTopic.IsEmpty()); michael@0: MOZ_ASSERT(aContentParent); michael@0: michael@0: if (aTopic.IsEmpty()) { michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: michael@0: mTopic.Assign(aTopic); michael@0: mContentParentID = aContentParent->ChildID(); michael@0: mHidden = false; michael@0: michael@0: nsCOMPtr obs = services::GetObserverService(); michael@0: if (obs) { michael@0: obs->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true); michael@0: } michael@0: michael@0: DoLock(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WakeLock::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* data) michael@0: { michael@0: // If this wake lock was acquired on behalf of another process, unlock it michael@0: // when that process dies. michael@0: // michael@0: // Note that we do /not/ call DoUnlock() here! The wake lock back-end is michael@0: // already listening for ipc:content-shutdown messages and will clear out its michael@0: // tally for the process when it dies. All we need to do here is ensure that michael@0: // unlock() becomes a nop. michael@0: michael@0: MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown")); michael@0: michael@0: nsCOMPtr props = do_QueryInterface(aSubject); michael@0: if (!props) { michael@0: NS_WARNING("ipc:content-shutdown message without property bag as subject"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint64_t childID = 0; michael@0: nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), michael@0: &childID); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: if (childID == mContentParentID) { michael@0: mLocked = false; michael@0: } michael@0: } else { michael@0: NS_WARNING("ipc:content-shutdown message without childID property"); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: WakeLock::DoLock() michael@0: { michael@0: if (!mLocked) { michael@0: // Change the flag immediately to prevent recursive reentering michael@0: mLocked = true; michael@0: michael@0: hal::ModifyWakeLock(mTopic, michael@0: hal::WAKE_LOCK_ADD_ONE, michael@0: mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_NO_CHANGE, michael@0: mContentParentID); michael@0: } michael@0: } michael@0: michael@0: void michael@0: WakeLock::DoUnlock() michael@0: { michael@0: if (mLocked) { michael@0: // Change the flag immediately to prevent recursive reentering michael@0: mLocked = false; michael@0: michael@0: hal::ModifyWakeLock(mTopic, michael@0: hal::WAKE_LOCK_REMOVE_ONE, michael@0: mHidden ? hal::WAKE_LOCK_REMOVE_ONE : hal::WAKE_LOCK_NO_CHANGE, michael@0: mContentParentID); michael@0: } michael@0: } michael@0: michael@0: void michael@0: WakeLock::AttachEventListener() michael@0: { michael@0: nsCOMPtr window = do_QueryReferent(mWindow); michael@0: michael@0: if (window) { michael@0: nsCOMPtr doc = window->GetExtantDoc(); michael@0: if (doc) { michael@0: doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"), michael@0: this, michael@0: /* useCapture = */ true, michael@0: /* wantsUntrusted = */ false); michael@0: michael@0: nsCOMPtr target = do_QueryInterface(window); michael@0: target->AddSystemEventListener(NS_LITERAL_STRING("pagehide"), michael@0: this, michael@0: /* useCapture = */ true, michael@0: /* wantsUntrusted = */ false); michael@0: target->AddSystemEventListener(NS_LITERAL_STRING("pageshow"), michael@0: this, michael@0: /* useCapture = */ true, michael@0: /* wantsUntrusted = */ false); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: WakeLock::DetachEventListener() michael@0: { michael@0: nsCOMPtr window = do_QueryReferent(mWindow); michael@0: michael@0: if (window) { michael@0: nsCOMPtr doc = window->GetExtantDoc(); michael@0: if (doc) { michael@0: doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"), michael@0: this, michael@0: /* useCapture = */ true); michael@0: nsCOMPtr target = do_QueryInterface(window); michael@0: target->RemoveSystemEventListener(NS_LITERAL_STRING("pagehide"), michael@0: this, michael@0: /* useCapture = */ true); michael@0: target->RemoveSystemEventListener(NS_LITERAL_STRING("pageshow"), michael@0: this, michael@0: /* useCapture = */ true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: WakeLock::Unlock(ErrorResult& aRv) michael@0: { michael@0: /* michael@0: * We throw NS_ERROR_DOM_INVALID_STATE_ERR on double unlock. michael@0: */ michael@0: if (!mLocked) { michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); michael@0: return; michael@0: } michael@0: michael@0: DoUnlock(); michael@0: DetachEventListener(); michael@0: } michael@0: michael@0: void michael@0: WakeLock::GetTopic(nsAString &aTopic) michael@0: { michael@0: aTopic.Assign(mTopic); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: WakeLock::HandleEvent(nsIDOMEvent *aEvent) michael@0: { michael@0: nsAutoString type; michael@0: aEvent->GetType(type); michael@0: michael@0: if (type.EqualsLiteral("visibilitychange")) { michael@0: nsCOMPtr doc = michael@0: do_QueryInterface(aEvent->InternalDOMEvent()->GetTarget()); michael@0: NS_ENSURE_STATE(doc); michael@0: michael@0: bool oldHidden = mHidden; michael@0: mHidden = doc->Hidden(); michael@0: michael@0: if (mLocked && oldHidden != mHidden) { michael@0: hal::ModifyWakeLock(mTopic, michael@0: hal::WAKE_LOCK_NO_CHANGE, michael@0: mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_REMOVE_ONE, michael@0: mContentParentID); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (type.EqualsLiteral("pagehide")) { michael@0: DoUnlock(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (type.EqualsLiteral("pageshow")) { michael@0: DoLock(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsISupports* michael@0: WakeLock::GetParentObject() const michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(mWindow); michael@0: return window; michael@0: } michael@0: michael@0: } // dom michael@0: } // mozilla