dom/power/PowerManager.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/power/PowerManager.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,209 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; 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
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/dom/PowerManager.h"
    1.10 +
    1.11 +#include "mozilla/Hal.h"
    1.12 +#include "WakeLock.h"
    1.13 +#include "nsDOMClassInfoID.h"
    1.14 +#include "nsIDOMWakeLockListener.h"
    1.15 +#include "nsIDocument.h"
    1.16 +#include "nsIPermissionManager.h"
    1.17 +#include "nsIPowerManagerService.h"
    1.18 +#include "nsIPrincipal.h"
    1.19 +#include "nsPIDOMWindow.h"
    1.20 +#include "nsServiceManagerUtils.h"
    1.21 +#include "nsError.h"
    1.22 +#include "mozilla/dom/MozPowerManagerBinding.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace dom {
    1.26 +
    1.27 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager)
    1.28 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.29 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.30 +  NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
    1.31 +NS_INTERFACE_MAP_END
    1.32 +
    1.33 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(PowerManager, mListeners, mWindow)
    1.34 +
    1.35 +NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager)
    1.36 +NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager)
    1.37 +
    1.38 +/* virtual */ JSObject*
    1.39 +PowerManager::WrapObject(JSContext* aCx)
    1.40 +{
    1.41 +  return MozPowerManagerBinding::Wrap(aCx, this);
    1.42 +}
    1.43 +
    1.44 +nsresult
    1.45 +PowerManager::Init(nsIDOMWindow *aWindow)
    1.46 +{
    1.47 +  mWindow = aWindow;
    1.48 +
    1.49 +  nsCOMPtr<nsIPowerManagerService> pmService =
    1.50 +    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    1.51 +  NS_ENSURE_STATE(pmService);
    1.52 +
    1.53 +  // Add ourself to the global notification list.
    1.54 +  pmService->AddWakeLockListener(this);
    1.55 +  return NS_OK;
    1.56 +}
    1.57 +
    1.58 +nsresult
    1.59 +PowerManager::Shutdown()
    1.60 +{
    1.61 +  nsCOMPtr<nsIPowerManagerService> pmService =
    1.62 +    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    1.63 +  NS_ENSURE_STATE(pmService);
    1.64 +
    1.65 +  // Remove ourself from the global notification list.
    1.66 +  pmService->RemoveWakeLockListener(this);
    1.67 +  return NS_OK;
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +PowerManager::Reboot(ErrorResult& aRv)
    1.72 +{
    1.73 +  nsCOMPtr<nsIPowerManagerService> pmService =
    1.74 +    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    1.75 +  if (pmService) {
    1.76 +    pmService->Reboot();
    1.77 +  } else {
    1.78 +    aRv.Throw(NS_ERROR_UNEXPECTED);
    1.79 +  }
    1.80 +}
    1.81 +
    1.82 +void
    1.83 +PowerManager::FactoryReset()
    1.84 +{
    1.85 +  hal::FactoryReset();
    1.86 +}
    1.87 +
    1.88 +void
    1.89 +PowerManager::PowerOff(ErrorResult& aRv)
    1.90 +{
    1.91 +  nsCOMPtr<nsIPowerManagerService> pmService =
    1.92 +    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    1.93 +  if (pmService) {
    1.94 +    pmService->PowerOff();
    1.95 +  } else {
    1.96 +    aRv.Throw(NS_ERROR_UNEXPECTED);
    1.97 +  }
    1.98 +}
    1.99 +
   1.100 +void
   1.101 +PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
   1.102 +{
   1.103 +  if (!mListeners.Contains(aListener)) {
   1.104 +    mListeners.AppendElement(aListener);
   1.105 +  }
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
   1.110 +{
   1.111 +  mListeners.RemoveElement(aListener);
   1.112 +}
   1.113 +
   1.114 +void
   1.115 +PowerManager::GetWakeLockState(const nsAString& aTopic,
   1.116 +                               nsAString& aState,
   1.117 +                               ErrorResult& aRv)
   1.118 +{
   1.119 +  nsCOMPtr<nsIPowerManagerService> pmService =
   1.120 +    do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   1.121 +  if (pmService) {
   1.122 +    aRv = pmService->GetWakeLockState(aTopic, aState);
   1.123 +  } else {
   1.124 +    aRv.Throw(NS_ERROR_UNEXPECTED);
   1.125 +  }
   1.126 +}
   1.127 +
   1.128 +NS_IMETHODIMP
   1.129 +PowerManager::Callback(const nsAString &aTopic, const nsAString &aState)
   1.130 +{
   1.131 +  /**
   1.132 +   * We maintain a local listener list instead of using the global
   1.133 +   * list so that when the window is destroyed we don't have to
   1.134 +   * cleanup the mess.
   1.135 +   * Copy the listeners list before we walk through the callbacks
   1.136 +   * because the callbacks may install new listeners. We expect no
   1.137 +   * more than one listener per window, so it shouldn't be too long.
   1.138 +   */
   1.139 +  nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners);
   1.140 +  for (uint32_t i = 0; i < listeners.Length(); ++i) {
   1.141 +    listeners[i]->Callback(aTopic, aState);
   1.142 +  }
   1.143 +
   1.144 +  return NS_OK;
   1.145 +}
   1.146 +
   1.147 +bool
   1.148 +PowerManager::ScreenEnabled()
   1.149 +{
   1.150 +  return hal::GetScreenEnabled();
   1.151 +}
   1.152 +
   1.153 +void
   1.154 +PowerManager::SetScreenEnabled(bool aEnabled)
   1.155 +{
   1.156 +  hal::SetScreenEnabled(aEnabled);
   1.157 +}
   1.158 +
   1.159 +double
   1.160 +PowerManager::ScreenBrightness()
   1.161 +{
   1.162 +  return hal::GetScreenBrightness();
   1.163 +}
   1.164 +
   1.165 +void
   1.166 +PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
   1.167 +{
   1.168 +  if (0 <= aBrightness && aBrightness <= 1) {
   1.169 +    hal::SetScreenBrightness(aBrightness);
   1.170 +  } else {
   1.171 +    aRv.Throw(NS_ERROR_INVALID_ARG);
   1.172 +  }
   1.173 +}
   1.174 +
   1.175 +bool
   1.176 +PowerManager::CpuSleepAllowed()
   1.177 +{
   1.178 +  return hal::GetCpuSleepAllowed();
   1.179 +}
   1.180 +
   1.181 +void
   1.182 +PowerManager::SetCpuSleepAllowed(bool aAllowed)
   1.183 +{
   1.184 +  hal::SetCpuSleepAllowed(aAllowed);
   1.185 +}
   1.186 +
   1.187 +bool
   1.188 +PowerManager::CheckPermission(nsPIDOMWindow* aWindow)
   1.189 +{
   1.190 +  nsCOMPtr<nsIPermissionManager> permMgr =
   1.191 +    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
   1.192 +  NS_ENSURE_TRUE(permMgr, false);
   1.193 +
   1.194 +  uint32_t permission = nsIPermissionManager::DENY_ACTION;
   1.195 +  permMgr->TestPermissionFromWindow(aWindow, "power", &permission);
   1.196 +
   1.197 +  return permission == nsIPermissionManager::ALLOW_ACTION;
   1.198 +}
   1.199 +
   1.200 +already_AddRefed<PowerManager>
   1.201 +PowerManager::CreateInstance(nsPIDOMWindow* aWindow)
   1.202 +{
   1.203 +  nsRefPtr<PowerManager> powerManager = new PowerManager();
   1.204 +  if (NS_FAILED(powerManager->Init(aWindow))) {
   1.205 +    powerManager = nullptr;
   1.206 +  }
   1.207 +
   1.208 +  return powerManager.forget();
   1.209 +}
   1.210 +
   1.211 +} // dom
   1.212 +} // mozilla

mercurial