1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/power/WakeLock.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_dom_power_WakeLock_h 1.10 +#define mozilla_dom_power_WakeLock_h 1.11 + 1.12 +#include "nsCOMPtr.h" 1.13 +#include "nsIDOMEventListener.h" 1.14 +#include "nsIObserver.h" 1.15 +#include "nsString.h" 1.16 +#include "nsWeakReference.h" 1.17 +#include "nsWrapperCache.h" 1.18 +#include "mozilla/ErrorResult.h" 1.19 + 1.20 +class nsIDOMWindow; 1.21 + 1.22 +namespace mozilla { 1.23 +namespace dom { 1.24 + 1.25 +class ContentParent; 1.26 + 1.27 +class WakeLock MOZ_FINAL 1.28 + : public nsIDOMEventListener 1.29 + , public nsWrapperCache 1.30 + , public nsIObserver 1.31 + , public nsSupportsWeakReference 1.32 +{ 1.33 +public: 1.34 + NS_DECL_NSIDOMEVENTLISTENER 1.35 + NS_DECL_NSIOBSERVER 1.36 + 1.37 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.38 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WakeLock, nsIDOMEventListener) 1.39 + 1.40 + // Note: WakeLock lives for the lifetime of the document in order to avoid 1.41 + // exposing GC behavior to pages. This means that 1.42 + // |var foo = navigator.requestWakeLock('cpu'); foo = null;| 1.43 + // doesn't unlock the 'cpu' resource. 1.44 + 1.45 + WakeLock(); 1.46 + virtual ~WakeLock(); 1.47 + 1.48 + // Initialize this wake lock on behalf of the given window. Null windows are 1.49 + // allowed; a lock without an associated window is always considered 1.50 + // invisible. 1.51 + nsresult Init(const nsAString &aTopic, nsIDOMWindow* aWindow); 1.52 + 1.53 + // Initialize this wake lock on behalf of the given process. If the process 1.54 + // dies, the lock is released. A wake lock initialized via this method is 1.55 + // always considered visible. 1.56 + nsresult Init(const nsAString &aTopic, ContentParent* aContentParent); 1.57 + 1.58 + // WebIDL methods 1.59 + 1.60 + nsISupports* GetParentObject() const; 1.61 + 1.62 + virtual JSObject* 1.63 + WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.64 + 1.65 + void GetTopic(nsAString& aTopic); 1.66 + 1.67 + void Unlock(ErrorResult& aRv); 1.68 + 1.69 +private: 1.70 + void DoUnlock(); 1.71 + void DoLock(); 1.72 + void AttachEventListener(); 1.73 + void DetachEventListener(); 1.74 + 1.75 + bool mLocked; 1.76 + bool mHidden; 1.77 + 1.78 + // The ID of the ContentParent on behalf of whom we acquired this lock, or 1.79 + // CONTENT_PROCESS_UNKNOWN_ID if this lock was acquired on behalf of the 1.80 + // current process. 1.81 + uint64_t mContentParentID; 1.82 + nsString mTopic; 1.83 + 1.84 + // window that this was created for. Weak reference. 1.85 + nsWeakPtr mWindow; 1.86 +}; 1.87 + 1.88 +} // namespace dom 1.89 +} // namespace mozilla 1.90 + 1.91 +#endif // mozilla_dom_power_WakeLock_h