Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "WakeLock.h" |
michael@0 | 7 | #include "mozilla/dom/ContentParent.h" |
michael@0 | 8 | #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent() |
michael@0 | 9 | #include "mozilla/dom/MozWakeLockBinding.h" |
michael@0 | 10 | #include "mozilla/Hal.h" |
michael@0 | 11 | #include "mozilla/HalWakeLock.h" |
michael@0 | 12 | #include "nsError.h" |
michael@0 | 13 | #include "nsIDocument.h" |
michael@0 | 14 | #include "nsIDOMWindow.h" |
michael@0 | 15 | #include "nsIDOMEvent.h" |
michael@0 | 16 | #include "nsPIDOMWindow.h" |
michael@0 | 17 | #include "nsIPropertyBag2.h" |
michael@0 | 18 | |
michael@0 | 19 | using namespace mozilla::hal; |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace dom { |
michael@0 | 23 | |
michael@0 | 24 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WakeLock) |
michael@0 | 25 | |
michael@0 | 26 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WakeLock) |
michael@0 | 27 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 28 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener) |
michael@0 | 29 | NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener) |
michael@0 | 30 | NS_INTERFACE_MAP_ENTRY(nsIObserver) |
michael@0 | 31 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 32 | NS_INTERFACE_MAP_END |
michael@0 | 33 | |
michael@0 | 34 | NS_IMPL_CYCLE_COLLECTING_ADDREF(WakeLock) |
michael@0 | 35 | NS_IMPL_CYCLE_COLLECTING_RELEASE(WakeLock) |
michael@0 | 36 | |
michael@0 | 37 | WakeLock::WakeLock() |
michael@0 | 38 | : mLocked(false) |
michael@0 | 39 | , mHidden(true) |
michael@0 | 40 | , mContentParentID(CONTENT_PROCESS_ID_UNKNOWN) |
michael@0 | 41 | { |
michael@0 | 42 | SetIsDOMBinding(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | WakeLock::~WakeLock() |
michael@0 | 46 | { |
michael@0 | 47 | DoUnlock(); |
michael@0 | 48 | DetachEventListener(); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | JSObject* |
michael@0 | 52 | WakeLock::WrapObject(JSContext* aCx) |
michael@0 | 53 | { |
michael@0 | 54 | return MozWakeLockBinding::Wrap(aCx, this); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | nsresult |
michael@0 | 58 | WakeLock::Init(const nsAString &aTopic, nsIDOMWindow *aWindow) |
michael@0 | 59 | { |
michael@0 | 60 | // Don't Init() a WakeLock twice. |
michael@0 | 61 | MOZ_ASSERT(mTopic.IsEmpty()); |
michael@0 | 62 | |
michael@0 | 63 | if (aTopic.IsEmpty()) { |
michael@0 | 64 | return NS_ERROR_INVALID_ARG; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | mTopic.Assign(aTopic); |
michael@0 | 68 | |
michael@0 | 69 | mWindow = do_GetWeakReference(aWindow); |
michael@0 | 70 | nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow); |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Null windows are allowed. A wake lock without associated window |
michael@0 | 74 | * is always considered invisible. |
michael@0 | 75 | */ |
michael@0 | 76 | if (window) { |
michael@0 | 77 | nsCOMPtr<nsIDocument> doc = window->GetExtantDoc(); |
michael@0 | 78 | NS_ENSURE_STATE(doc); |
michael@0 | 79 | mHidden = doc->Hidden(); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | AttachEventListener(); |
michael@0 | 83 | DoLock(); |
michael@0 | 84 | |
michael@0 | 85 | return NS_OK; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | nsresult |
michael@0 | 89 | WakeLock::Init(const nsAString& aTopic, ContentParent* aContentParent) |
michael@0 | 90 | { |
michael@0 | 91 | // Don't Init() a WakeLock twice. |
michael@0 | 92 | MOZ_ASSERT(mTopic.IsEmpty()); |
michael@0 | 93 | MOZ_ASSERT(aContentParent); |
michael@0 | 94 | |
michael@0 | 95 | if (aTopic.IsEmpty()) { |
michael@0 | 96 | return NS_ERROR_INVALID_ARG; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | mTopic.Assign(aTopic); |
michael@0 | 100 | mContentParentID = aContentParent->ChildID(); |
michael@0 | 101 | mHidden = false; |
michael@0 | 102 | |
michael@0 | 103 | nsCOMPtr<nsIObserverService> obs = services::GetObserverService(); |
michael@0 | 104 | if (obs) { |
michael@0 | 105 | obs->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | DoLock(); |
michael@0 | 109 | return NS_OK; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | NS_IMETHODIMP |
michael@0 | 113 | WakeLock::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* data) |
michael@0 | 114 | { |
michael@0 | 115 | // If this wake lock was acquired on behalf of another process, unlock it |
michael@0 | 116 | // when that process dies. |
michael@0 | 117 | // |
michael@0 | 118 | // Note that we do /not/ call DoUnlock() here! The wake lock back-end is |
michael@0 | 119 | // already listening for ipc:content-shutdown messages and will clear out its |
michael@0 | 120 | // tally for the process when it dies. All we need to do here is ensure that |
michael@0 | 121 | // unlock() becomes a nop. |
michael@0 | 122 | |
michael@0 | 123 | MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown")); |
michael@0 | 124 | |
michael@0 | 125 | nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject); |
michael@0 | 126 | if (!props) { |
michael@0 | 127 | NS_WARNING("ipc:content-shutdown message without property bag as subject"); |
michael@0 | 128 | return NS_OK; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | uint64_t childID = 0; |
michael@0 | 132 | nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"), |
michael@0 | 133 | &childID); |
michael@0 | 134 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 135 | if (childID == mContentParentID) { |
michael@0 | 136 | mLocked = false; |
michael@0 | 137 | } |
michael@0 | 138 | } else { |
michael@0 | 139 | NS_WARNING("ipc:content-shutdown message without childID property"); |
michael@0 | 140 | } |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | void |
michael@0 | 145 | WakeLock::DoLock() |
michael@0 | 146 | { |
michael@0 | 147 | if (!mLocked) { |
michael@0 | 148 | // Change the flag immediately to prevent recursive reentering |
michael@0 | 149 | mLocked = true; |
michael@0 | 150 | |
michael@0 | 151 | hal::ModifyWakeLock(mTopic, |
michael@0 | 152 | hal::WAKE_LOCK_ADD_ONE, |
michael@0 | 153 | mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_NO_CHANGE, |
michael@0 | 154 | mContentParentID); |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | void |
michael@0 | 159 | WakeLock::DoUnlock() |
michael@0 | 160 | { |
michael@0 | 161 | if (mLocked) { |
michael@0 | 162 | // Change the flag immediately to prevent recursive reentering |
michael@0 | 163 | mLocked = false; |
michael@0 | 164 | |
michael@0 | 165 | hal::ModifyWakeLock(mTopic, |
michael@0 | 166 | hal::WAKE_LOCK_REMOVE_ONE, |
michael@0 | 167 | mHidden ? hal::WAKE_LOCK_REMOVE_ONE : hal::WAKE_LOCK_NO_CHANGE, |
michael@0 | 168 | mContentParentID); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | void |
michael@0 | 173 | WakeLock::AttachEventListener() |
michael@0 | 174 | { |
michael@0 | 175 | nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow); |
michael@0 | 176 | |
michael@0 | 177 | if (window) { |
michael@0 | 178 | nsCOMPtr<nsIDocument> doc = window->GetExtantDoc(); |
michael@0 | 179 | if (doc) { |
michael@0 | 180 | doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"), |
michael@0 | 181 | this, |
michael@0 | 182 | /* useCapture = */ true, |
michael@0 | 183 | /* wantsUntrusted = */ false); |
michael@0 | 184 | |
michael@0 | 185 | nsCOMPtr<EventTarget> target = do_QueryInterface(window); |
michael@0 | 186 | target->AddSystemEventListener(NS_LITERAL_STRING("pagehide"), |
michael@0 | 187 | this, |
michael@0 | 188 | /* useCapture = */ true, |
michael@0 | 189 | /* wantsUntrusted = */ false); |
michael@0 | 190 | target->AddSystemEventListener(NS_LITERAL_STRING("pageshow"), |
michael@0 | 191 | this, |
michael@0 | 192 | /* useCapture = */ true, |
michael@0 | 193 | /* wantsUntrusted = */ false); |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | void |
michael@0 | 199 | WakeLock::DetachEventListener() |
michael@0 | 200 | { |
michael@0 | 201 | nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow); |
michael@0 | 202 | |
michael@0 | 203 | if (window) { |
michael@0 | 204 | nsCOMPtr<nsIDocument> doc = window->GetExtantDoc(); |
michael@0 | 205 | if (doc) { |
michael@0 | 206 | doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"), |
michael@0 | 207 | this, |
michael@0 | 208 | /* useCapture = */ true); |
michael@0 | 209 | nsCOMPtr<EventTarget> target = do_QueryInterface(window); |
michael@0 | 210 | target->RemoveSystemEventListener(NS_LITERAL_STRING("pagehide"), |
michael@0 | 211 | this, |
michael@0 | 212 | /* useCapture = */ true); |
michael@0 | 213 | target->RemoveSystemEventListener(NS_LITERAL_STRING("pageshow"), |
michael@0 | 214 | this, |
michael@0 | 215 | /* useCapture = */ true); |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | void |
michael@0 | 221 | WakeLock::Unlock(ErrorResult& aRv) |
michael@0 | 222 | { |
michael@0 | 223 | /* |
michael@0 | 224 | * We throw NS_ERROR_DOM_INVALID_STATE_ERR on double unlock. |
michael@0 | 225 | */ |
michael@0 | 226 | if (!mLocked) { |
michael@0 | 227 | aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); |
michael@0 | 228 | return; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | DoUnlock(); |
michael@0 | 232 | DetachEventListener(); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | void |
michael@0 | 236 | WakeLock::GetTopic(nsAString &aTopic) |
michael@0 | 237 | { |
michael@0 | 238 | aTopic.Assign(mTopic); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | NS_IMETHODIMP |
michael@0 | 242 | WakeLock::HandleEvent(nsIDOMEvent *aEvent) |
michael@0 | 243 | { |
michael@0 | 244 | nsAutoString type; |
michael@0 | 245 | aEvent->GetType(type); |
michael@0 | 246 | |
michael@0 | 247 | if (type.EqualsLiteral("visibilitychange")) { |
michael@0 | 248 | nsCOMPtr<nsIDocument> doc = |
michael@0 | 249 | do_QueryInterface(aEvent->InternalDOMEvent()->GetTarget()); |
michael@0 | 250 | NS_ENSURE_STATE(doc); |
michael@0 | 251 | |
michael@0 | 252 | bool oldHidden = mHidden; |
michael@0 | 253 | mHidden = doc->Hidden(); |
michael@0 | 254 | |
michael@0 | 255 | if (mLocked && oldHidden != mHidden) { |
michael@0 | 256 | hal::ModifyWakeLock(mTopic, |
michael@0 | 257 | hal::WAKE_LOCK_NO_CHANGE, |
michael@0 | 258 | mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_REMOVE_ONE, |
michael@0 | 259 | mContentParentID); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | return NS_OK; |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | if (type.EqualsLiteral("pagehide")) { |
michael@0 | 266 | DoUnlock(); |
michael@0 | 267 | return NS_OK; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | if (type.EqualsLiteral("pageshow")) { |
michael@0 | 271 | DoLock(); |
michael@0 | 272 | return NS_OK; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | return NS_OK; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | nsISupports* |
michael@0 | 279 | WakeLock::GetParentObject() const |
michael@0 | 280 | { |
michael@0 | 281 | nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow); |
michael@0 | 282 | return window; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | } // dom |
michael@0 | 286 | } // mozilla |