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