michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/PowerManager.h" michael@0: michael@0: #include "mozilla/Hal.h" michael@0: #include "WakeLock.h" michael@0: #include "nsDOMClassInfoID.h" michael@0: #include "nsIDOMWakeLockListener.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIPermissionManager.h" michael@0: #include "nsIPowerManagerService.h" michael@0: #include "nsIPrincipal.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsError.h" michael@0: #include "mozilla/dom/MozPowerManagerBinding.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(PowerManager, mListeners, mWindow) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager) michael@0: michael@0: /* virtual */ JSObject* michael@0: PowerManager::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozPowerManagerBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: nsresult michael@0: PowerManager::Init(nsIDOMWindow *aWindow) michael@0: { michael@0: mWindow = aWindow; michael@0: michael@0: nsCOMPtr pmService = michael@0: do_GetService(POWERMANAGERSERVICE_CONTRACTID); michael@0: NS_ENSURE_STATE(pmService); michael@0: michael@0: // Add ourself to the global notification list. michael@0: pmService->AddWakeLockListener(this); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: PowerManager::Shutdown() michael@0: { michael@0: nsCOMPtr pmService = michael@0: do_GetService(POWERMANAGERSERVICE_CONTRACTID); michael@0: NS_ENSURE_STATE(pmService); michael@0: michael@0: // Remove ourself from the global notification list. michael@0: pmService->RemoveWakeLockListener(this); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: PowerManager::Reboot(ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr pmService = michael@0: do_GetService(POWERMANAGERSERVICE_CONTRACTID); michael@0: if (pmService) { michael@0: pmService->Reboot(); michael@0: } else { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: } michael@0: } michael@0: michael@0: void michael@0: PowerManager::FactoryReset() michael@0: { michael@0: hal::FactoryReset(); michael@0: } michael@0: michael@0: void michael@0: PowerManager::PowerOff(ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr pmService = michael@0: do_GetService(POWERMANAGERSERVICE_CONTRACTID); michael@0: if (pmService) { michael@0: pmService->PowerOff(); michael@0: } else { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: } michael@0: } michael@0: michael@0: void michael@0: PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener) michael@0: { michael@0: if (!mListeners.Contains(aListener)) { michael@0: mListeners.AppendElement(aListener); michael@0: } michael@0: } michael@0: michael@0: void michael@0: PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener) michael@0: { michael@0: mListeners.RemoveElement(aListener); michael@0: } michael@0: michael@0: void michael@0: PowerManager::GetWakeLockState(const nsAString& aTopic, michael@0: nsAString& aState, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr pmService = michael@0: do_GetService(POWERMANAGERSERVICE_CONTRACTID); michael@0: if (pmService) { michael@0: aRv = pmService->GetWakeLockState(aTopic, aState); michael@0: } else { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: PowerManager::Callback(const nsAString &aTopic, const nsAString &aState) michael@0: { michael@0: /** michael@0: * We maintain a local listener list instead of using the global michael@0: * list so that when the window is destroyed we don't have to michael@0: * cleanup the mess. michael@0: * Copy the listeners list before we walk through the callbacks michael@0: * because the callbacks may install new listeners. We expect no michael@0: * more than one listener per window, so it shouldn't be too long. michael@0: */ michael@0: nsAutoTArray, 2> listeners(mListeners); michael@0: for (uint32_t i = 0; i < listeners.Length(); ++i) { michael@0: listeners[i]->Callback(aTopic, aState); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: PowerManager::ScreenEnabled() michael@0: { michael@0: return hal::GetScreenEnabled(); michael@0: } michael@0: michael@0: void michael@0: PowerManager::SetScreenEnabled(bool aEnabled) michael@0: { michael@0: hal::SetScreenEnabled(aEnabled); michael@0: } michael@0: michael@0: double michael@0: PowerManager::ScreenBrightness() michael@0: { michael@0: return hal::GetScreenBrightness(); michael@0: } michael@0: michael@0: void michael@0: PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv) michael@0: { michael@0: if (0 <= aBrightness && aBrightness <= 1) { michael@0: hal::SetScreenBrightness(aBrightness); michael@0: } else { michael@0: aRv.Throw(NS_ERROR_INVALID_ARG); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: PowerManager::CpuSleepAllowed() michael@0: { michael@0: return hal::GetCpuSleepAllowed(); michael@0: } michael@0: michael@0: void michael@0: PowerManager::SetCpuSleepAllowed(bool aAllowed) michael@0: { michael@0: hal::SetCpuSleepAllowed(aAllowed); michael@0: } michael@0: michael@0: bool michael@0: PowerManager::CheckPermission(nsPIDOMWindow* aWindow) michael@0: { michael@0: nsCOMPtr permMgr = michael@0: do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); michael@0: NS_ENSURE_TRUE(permMgr, false); michael@0: michael@0: uint32_t permission = nsIPermissionManager::DENY_ACTION; michael@0: permMgr->TestPermissionFromWindow(aWindow, "power", &permission); michael@0: michael@0: return permission == nsIPermissionManager::ALLOW_ACTION; michael@0: } michael@0: michael@0: already_AddRefed michael@0: PowerManager::CreateInstance(nsPIDOMWindow* aWindow) michael@0: { michael@0: nsRefPtr powerManager = new PowerManager(); michael@0: if (NS_FAILED(powerManager->Init(aWindow))) { michael@0: powerManager = nullptr; michael@0: } michael@0: michael@0: return powerManager.forget(); michael@0: } michael@0: michael@0: } // dom michael@0: } // mozilla