dom/power/PowerManager.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "mozilla/dom/PowerManager.h"
     8 #include "mozilla/Hal.h"
     9 #include "WakeLock.h"
    10 #include "nsDOMClassInfoID.h"
    11 #include "nsIDOMWakeLockListener.h"
    12 #include "nsIDocument.h"
    13 #include "nsIPermissionManager.h"
    14 #include "nsIPowerManagerService.h"
    15 #include "nsIPrincipal.h"
    16 #include "nsPIDOMWindow.h"
    17 #include "nsServiceManagerUtils.h"
    18 #include "nsError.h"
    19 #include "mozilla/dom/MozPowerManagerBinding.h"
    21 namespace mozilla {
    22 namespace dom {
    24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager)
    25   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    26   NS_INTERFACE_MAP_ENTRY(nsISupports)
    27   NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
    28 NS_INTERFACE_MAP_END
    30 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(PowerManager, mListeners, mWindow)
    32 NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager)
    33 NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager)
    35 /* virtual */ JSObject*
    36 PowerManager::WrapObject(JSContext* aCx)
    37 {
    38   return MozPowerManagerBinding::Wrap(aCx, this);
    39 }
    41 nsresult
    42 PowerManager::Init(nsIDOMWindow *aWindow)
    43 {
    44   mWindow = aWindow;
    46   nsCOMPtr<nsIPowerManagerService> pmService =
    47     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    48   NS_ENSURE_STATE(pmService);
    50   // Add ourself to the global notification list.
    51   pmService->AddWakeLockListener(this);
    52   return NS_OK;
    53 }
    55 nsresult
    56 PowerManager::Shutdown()
    57 {
    58   nsCOMPtr<nsIPowerManagerService> pmService =
    59     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    60   NS_ENSURE_STATE(pmService);
    62   // Remove ourself from the global notification list.
    63   pmService->RemoveWakeLockListener(this);
    64   return NS_OK;
    65 }
    67 void
    68 PowerManager::Reboot(ErrorResult& aRv)
    69 {
    70   nsCOMPtr<nsIPowerManagerService> pmService =
    71     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    72   if (pmService) {
    73     pmService->Reboot();
    74   } else {
    75     aRv.Throw(NS_ERROR_UNEXPECTED);
    76   }
    77 }
    79 void
    80 PowerManager::FactoryReset()
    81 {
    82   hal::FactoryReset();
    83 }
    85 void
    86 PowerManager::PowerOff(ErrorResult& aRv)
    87 {
    88   nsCOMPtr<nsIPowerManagerService> pmService =
    89     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
    90   if (pmService) {
    91     pmService->PowerOff();
    92   } else {
    93     aRv.Throw(NS_ERROR_UNEXPECTED);
    94   }
    95 }
    97 void
    98 PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
    99 {
   100   if (!mListeners.Contains(aListener)) {
   101     mListeners.AppendElement(aListener);
   102   }
   103 }
   105 void
   106 PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
   107 {
   108   mListeners.RemoveElement(aListener);
   109 }
   111 void
   112 PowerManager::GetWakeLockState(const nsAString& aTopic,
   113                                nsAString& aState,
   114                                ErrorResult& aRv)
   115 {
   116   nsCOMPtr<nsIPowerManagerService> pmService =
   117     do_GetService(POWERMANAGERSERVICE_CONTRACTID);
   118   if (pmService) {
   119     aRv = pmService->GetWakeLockState(aTopic, aState);
   120   } else {
   121     aRv.Throw(NS_ERROR_UNEXPECTED);
   122   }
   123 }
   125 NS_IMETHODIMP
   126 PowerManager::Callback(const nsAString &aTopic, const nsAString &aState)
   127 {
   128   /**
   129    * We maintain a local listener list instead of using the global
   130    * list so that when the window is destroyed we don't have to
   131    * cleanup the mess.
   132    * Copy the listeners list before we walk through the callbacks
   133    * because the callbacks may install new listeners. We expect no
   134    * more than one listener per window, so it shouldn't be too long.
   135    */
   136   nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners);
   137   for (uint32_t i = 0; i < listeners.Length(); ++i) {
   138     listeners[i]->Callback(aTopic, aState);
   139   }
   141   return NS_OK;
   142 }
   144 bool
   145 PowerManager::ScreenEnabled()
   146 {
   147   return hal::GetScreenEnabled();
   148 }
   150 void
   151 PowerManager::SetScreenEnabled(bool aEnabled)
   152 {
   153   hal::SetScreenEnabled(aEnabled);
   154 }
   156 double
   157 PowerManager::ScreenBrightness()
   158 {
   159   return hal::GetScreenBrightness();
   160 }
   162 void
   163 PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
   164 {
   165   if (0 <= aBrightness && aBrightness <= 1) {
   166     hal::SetScreenBrightness(aBrightness);
   167   } else {
   168     aRv.Throw(NS_ERROR_INVALID_ARG);
   169   }
   170 }
   172 bool
   173 PowerManager::CpuSleepAllowed()
   174 {
   175   return hal::GetCpuSleepAllowed();
   176 }
   178 void
   179 PowerManager::SetCpuSleepAllowed(bool aAllowed)
   180 {
   181   hal::SetCpuSleepAllowed(aAllowed);
   182 }
   184 bool
   185 PowerManager::CheckPermission(nsPIDOMWindow* aWindow)
   186 {
   187   nsCOMPtr<nsIPermissionManager> permMgr =
   188     do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
   189   NS_ENSURE_TRUE(permMgr, false);
   191   uint32_t permission = nsIPermissionManager::DENY_ACTION;
   192   permMgr->TestPermissionFromWindow(aWindow, "power", &permission);
   194   return permission == nsIPermissionManager::ALLOW_ACTION;
   195 }
   197 already_AddRefed<PowerManager>
   198 PowerManager::CreateInstance(nsPIDOMWindow* aWindow)
   199 {
   200   nsRefPtr<PowerManager> powerManager = new PowerManager();
   201   if (NS_FAILED(powerManager->Init(aWindow))) {
   202     powerManager = nullptr;
   203   }
   205   return powerManager.forget();
   206 }
   208 } // dom
   209 } // mozilla

mercurial