dom/power/WakeLock.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/power/WakeLock.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,286 @@
     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 +#include "WakeLock.h"
    1.10 +#include "mozilla/dom/ContentParent.h"
    1.11 +#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
    1.12 +#include "mozilla/dom/MozWakeLockBinding.h"
    1.13 +#include "mozilla/Hal.h"
    1.14 +#include "mozilla/HalWakeLock.h"
    1.15 +#include "nsError.h"
    1.16 +#include "nsIDocument.h"
    1.17 +#include "nsIDOMWindow.h"
    1.18 +#include "nsIDOMEvent.h"
    1.19 +#include "nsPIDOMWindow.h"
    1.20 +#include "nsIPropertyBag2.h"
    1.21 +
    1.22 +using namespace mozilla::hal;
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace dom {
    1.26 +
    1.27 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WakeLock)
    1.28 +
    1.29 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WakeLock)
    1.30 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.31 +  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener)
    1.32 +  NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
    1.33 +  NS_INTERFACE_MAP_ENTRY(nsIObserver)
    1.34 +  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
    1.35 +NS_INTERFACE_MAP_END
    1.36 +
    1.37 +NS_IMPL_CYCLE_COLLECTING_ADDREF(WakeLock)
    1.38 +NS_IMPL_CYCLE_COLLECTING_RELEASE(WakeLock)
    1.39 +
    1.40 +WakeLock::WakeLock()
    1.41 +  : mLocked(false)
    1.42 +  , mHidden(true)
    1.43 +  , mContentParentID(CONTENT_PROCESS_ID_UNKNOWN)
    1.44 +{
    1.45 +  SetIsDOMBinding();
    1.46 +}
    1.47 +
    1.48 +WakeLock::~WakeLock()
    1.49 +{
    1.50 +  DoUnlock();
    1.51 +  DetachEventListener();
    1.52 +}
    1.53 +
    1.54 +JSObject*
    1.55 +WakeLock::WrapObject(JSContext* aCx)
    1.56 +{
    1.57 +  return MozWakeLockBinding::Wrap(aCx, this);
    1.58 +}
    1.59 +
    1.60 +nsresult
    1.61 +WakeLock::Init(const nsAString &aTopic, nsIDOMWindow *aWindow)
    1.62 +{
    1.63 +  // Don't Init() a WakeLock twice.
    1.64 +  MOZ_ASSERT(mTopic.IsEmpty());
    1.65 +
    1.66 +  if (aTopic.IsEmpty()) {
    1.67 +    return NS_ERROR_INVALID_ARG;
    1.68 +  }
    1.69 +
    1.70 +  mTopic.Assign(aTopic);
    1.71 +
    1.72 +  mWindow = do_GetWeakReference(aWindow);
    1.73 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow);
    1.74 +
    1.75 +  /**
    1.76 +   * Null windows are allowed. A wake lock without associated window
    1.77 +   * is always considered invisible.
    1.78 +   */
    1.79 +  if (window) {
    1.80 +    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
    1.81 +    NS_ENSURE_STATE(doc);
    1.82 +    mHidden = doc->Hidden();
    1.83 +  }
    1.84 +
    1.85 +  AttachEventListener();
    1.86 +  DoLock();
    1.87 +
    1.88 +  return NS_OK;
    1.89 +}
    1.90 +
    1.91 +nsresult
    1.92 +WakeLock::Init(const nsAString& aTopic, ContentParent* aContentParent)
    1.93 +{
    1.94 +  // Don't Init() a WakeLock twice.
    1.95 +  MOZ_ASSERT(mTopic.IsEmpty());
    1.96 +  MOZ_ASSERT(aContentParent);
    1.97 +
    1.98 +  if (aTopic.IsEmpty()) {
    1.99 +    return NS_ERROR_INVALID_ARG;
   1.100 +  }
   1.101 +
   1.102 +  mTopic.Assign(aTopic);
   1.103 +  mContentParentID = aContentParent->ChildID();
   1.104 +  mHidden = false;
   1.105 +
   1.106 +  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
   1.107 +  if (obs) {
   1.108 +    obs->AddObserver(this, "ipc:content-shutdown", /* ownsWeak */ true);
   1.109 +  }
   1.110 +
   1.111 +  DoLock();
   1.112 +  return NS_OK;
   1.113 +}
   1.114 +
   1.115 +NS_IMETHODIMP
   1.116 +WakeLock::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* data)
   1.117 +{
   1.118 +  // If this wake lock was acquired on behalf of another process, unlock it
   1.119 +  // when that process dies.
   1.120 +  //
   1.121 +  // Note that we do /not/ call DoUnlock() here!  The wake lock back-end is
   1.122 +  // already listening for ipc:content-shutdown messages and will clear out its
   1.123 +  // tally for the process when it dies.  All we need to do here is ensure that
   1.124 +  // unlock() becomes a nop.
   1.125 +
   1.126 +  MOZ_ASSERT(!strcmp(aTopic, "ipc:content-shutdown"));
   1.127 +
   1.128 +  nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject);
   1.129 +  if (!props) {
   1.130 +    NS_WARNING("ipc:content-shutdown message without property bag as subject");
   1.131 +    return NS_OK;
   1.132 +  }
   1.133 +
   1.134 +  uint64_t childID = 0;
   1.135 +  nsresult rv = props->GetPropertyAsUint64(NS_LITERAL_STRING("childID"),
   1.136 +                                           &childID);
   1.137 +  if (NS_SUCCEEDED(rv)) {
   1.138 +    if (childID == mContentParentID) {
   1.139 +      mLocked = false;
   1.140 +    }
   1.141 +  } else {
   1.142 +    NS_WARNING("ipc:content-shutdown message without childID property");
   1.143 +  }
   1.144 +  return NS_OK;
   1.145 +}
   1.146 +
   1.147 +void
   1.148 +WakeLock::DoLock()
   1.149 +{
   1.150 +  if (!mLocked) {
   1.151 +    // Change the flag immediately to prevent recursive reentering
   1.152 +    mLocked = true;
   1.153 +
   1.154 +    hal::ModifyWakeLock(mTopic,
   1.155 +                        hal::WAKE_LOCK_ADD_ONE,
   1.156 +                        mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_NO_CHANGE,
   1.157 +                        mContentParentID);
   1.158 +  }
   1.159 +}
   1.160 +
   1.161 +void
   1.162 +WakeLock::DoUnlock()
   1.163 +{
   1.164 +  if (mLocked) {
   1.165 +    // Change the flag immediately to prevent recursive reentering
   1.166 +    mLocked = false;
   1.167 +
   1.168 +    hal::ModifyWakeLock(mTopic,
   1.169 +                        hal::WAKE_LOCK_REMOVE_ONE,
   1.170 +                        mHidden ? hal::WAKE_LOCK_REMOVE_ONE : hal::WAKE_LOCK_NO_CHANGE,
   1.171 +                        mContentParentID);
   1.172 +  }
   1.173 +}
   1.174 +
   1.175 +void
   1.176 +WakeLock::AttachEventListener()
   1.177 +{
   1.178 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   1.179 +
   1.180 +  if (window) {
   1.181 +    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   1.182 +    if (doc) {
   1.183 +      doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
   1.184 +                                  this,
   1.185 +                                  /* useCapture = */ true,
   1.186 +                                  /* wantsUntrusted = */ false);
   1.187 +
   1.188 +      nsCOMPtr<EventTarget> target = do_QueryInterface(window);
   1.189 +      target->AddSystemEventListener(NS_LITERAL_STRING("pagehide"),
   1.190 +                                     this,
   1.191 +                                     /* useCapture = */ true,
   1.192 +                                     /* wantsUntrusted = */ false);
   1.193 +      target->AddSystemEventListener(NS_LITERAL_STRING("pageshow"),
   1.194 +                                     this,
   1.195 +                                     /* useCapture = */ true,
   1.196 +                                     /* wantsUntrusted = */ false);
   1.197 +    }
   1.198 +  }
   1.199 +}
   1.200 +
   1.201 +void
   1.202 +WakeLock::DetachEventListener()
   1.203 +{
   1.204 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   1.205 +
   1.206 +  if (window) {
   1.207 +    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   1.208 +    if (doc) {
   1.209 +      doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
   1.210 +                                     this,
   1.211 +                                     /* useCapture = */ true);
   1.212 +      nsCOMPtr<EventTarget> target = do_QueryInterface(window);
   1.213 +      target->RemoveSystemEventListener(NS_LITERAL_STRING("pagehide"),
   1.214 +                                        this,
   1.215 +                                        /* useCapture = */ true);
   1.216 +      target->RemoveSystemEventListener(NS_LITERAL_STRING("pageshow"),
   1.217 +                                        this,
   1.218 +                                        /* useCapture = */ true);
   1.219 +    }
   1.220 +  }
   1.221 +}
   1.222 +
   1.223 +void
   1.224 +WakeLock::Unlock(ErrorResult& aRv)
   1.225 +{
   1.226 +  /*
   1.227 +   * We throw NS_ERROR_DOM_INVALID_STATE_ERR on double unlock.
   1.228 +   */
   1.229 +  if (!mLocked) {
   1.230 +    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
   1.231 +    return;
   1.232 +  }
   1.233 +
   1.234 +  DoUnlock();
   1.235 +  DetachEventListener();
   1.236 +}
   1.237 +
   1.238 +void
   1.239 +WakeLock::GetTopic(nsAString &aTopic)
   1.240 +{
   1.241 +  aTopic.Assign(mTopic);
   1.242 +}
   1.243 +
   1.244 +NS_IMETHODIMP
   1.245 +WakeLock::HandleEvent(nsIDOMEvent *aEvent)
   1.246 +{
   1.247 +  nsAutoString type;
   1.248 +  aEvent->GetType(type);
   1.249 +
   1.250 +  if (type.EqualsLiteral("visibilitychange")) {
   1.251 +    nsCOMPtr<nsIDocument> doc =
   1.252 +      do_QueryInterface(aEvent->InternalDOMEvent()->GetTarget());
   1.253 +    NS_ENSURE_STATE(doc);
   1.254 +
   1.255 +    bool oldHidden = mHidden;
   1.256 +    mHidden = doc->Hidden();
   1.257 +
   1.258 +    if (mLocked && oldHidden != mHidden) {
   1.259 +      hal::ModifyWakeLock(mTopic,
   1.260 +                          hal::WAKE_LOCK_NO_CHANGE,
   1.261 +                          mHidden ? hal::WAKE_LOCK_ADD_ONE : hal::WAKE_LOCK_REMOVE_ONE,
   1.262 +                          mContentParentID);
   1.263 +    }
   1.264 +
   1.265 +    return NS_OK;
   1.266 +  }
   1.267 +
   1.268 +  if (type.EqualsLiteral("pagehide")) {
   1.269 +    DoUnlock();
   1.270 +    return NS_OK;
   1.271 +  }
   1.272 +
   1.273 +  if (type.EqualsLiteral("pageshow")) {
   1.274 +    DoLock();
   1.275 +    return NS_OK;
   1.276 +  }
   1.277 +
   1.278 +  return NS_OK;
   1.279 +}
   1.280 +
   1.281 +nsISupports*
   1.282 +WakeLock::GetParentObject() const
   1.283 +{
   1.284 +  nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow);
   1.285 +  return window;
   1.286 +}
   1.287 +
   1.288 +} // dom
   1.289 +} // mozilla

mercurial