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: #ifndef mozilla_dom_power_WakeLock_h michael@0: #define mozilla_dom_power_WakeLock_h michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIDOMEventListener.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsString.h" michael@0: #include "nsWeakReference.h" michael@0: #include "nsWrapperCache.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: michael@0: class nsIDOMWindow; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class ContentParent; michael@0: michael@0: class WakeLock MOZ_FINAL michael@0: : public nsIDOMEventListener michael@0: , public nsWrapperCache michael@0: , public nsIObserver michael@0: , public nsSupportsWeakReference michael@0: { michael@0: public: michael@0: NS_DECL_NSIDOMEVENTLISTENER michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WakeLock, nsIDOMEventListener) michael@0: michael@0: // Note: WakeLock lives for the lifetime of the document in order to avoid michael@0: // exposing GC behavior to pages. This means that michael@0: // |var foo = navigator.requestWakeLock('cpu'); foo = null;| michael@0: // doesn't unlock the 'cpu' resource. michael@0: michael@0: WakeLock(); michael@0: virtual ~WakeLock(); michael@0: michael@0: // Initialize this wake lock on behalf of the given window. Null windows are michael@0: // allowed; a lock without an associated window is always considered michael@0: // invisible. michael@0: nsresult Init(const nsAString &aTopic, nsIDOMWindow* aWindow); michael@0: michael@0: // Initialize this wake lock on behalf of the given process. If the process michael@0: // dies, the lock is released. A wake lock initialized via this method is michael@0: // always considered visible. michael@0: nsresult Init(const nsAString &aTopic, ContentParent* aContentParent); michael@0: michael@0: // WebIDL methods michael@0: michael@0: nsISupports* GetParentObject() const; michael@0: michael@0: virtual JSObject* michael@0: WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: void GetTopic(nsAString& aTopic); michael@0: michael@0: void Unlock(ErrorResult& aRv); michael@0: michael@0: private: michael@0: void DoUnlock(); michael@0: void DoLock(); michael@0: void AttachEventListener(); michael@0: void DetachEventListener(); michael@0: michael@0: bool mLocked; michael@0: bool mHidden; michael@0: michael@0: // The ID of the ContentParent on behalf of whom we acquired this lock, or michael@0: // CONTENT_PROCESS_UNKNOWN_ID if this lock was acquired on behalf of the michael@0: // current process. michael@0: uint64_t mContentParentID; michael@0: nsString mTopic; michael@0: michael@0: // window that this was created for. Weak reference. michael@0: nsWeakPtr mWindow; michael@0: }; michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_dom_power_WakeLock_h