2012-01-20 03:24:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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 03:24:00 +00:00
|
|
|
|
2013-09-11 14:35:04 +00:00
|
|
|
#include "mozilla/dom/PowerManager.h"
|
|
|
|
|
2012-04-03 05:51:00 +00:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#include "WakeLock.h"
|
2012-01-20 03:25:00 +00:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
2012-08-11 11:40:43 +00:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
2012-01-20 03:25:00 +00:00
|
|
|
#include "nsIPowerManagerService.h"
|
2012-03-07 11:03:25 +00:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2012-01-20 03:25:00 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2013-09-10 23:03:56 +00:00
|
|
|
#include "mozilla/dom/MozPowerManagerBinding.h"
|
2014-04-29 17:27:26 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-01-20 03:24:00 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-07-18 16:07:28 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager)
|
2013-09-10 23:03:56 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2012-03-07 11:03:25 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
|
2012-01-20 03:24:00 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 08:57:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PowerManager, mListeners, mWindow)
|
2013-07-18 16:07:28 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager)
|
2012-01-20 03:24:00 +00:00
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
PowerManager::WrapObject(JSContext* aCx)
|
2013-09-10 23:03:56 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return MozPowerManagerBinding::Wrap(aCx, this);
|
2013-09-10 23:03:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 11:03:25 +00:00
|
|
|
nsresult
|
|
|
|
PowerManager::Init(nsIDOMWindow *aWindow)
|
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
mWindow = aWindow;
|
2012-03-07 11:03:25 +00: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-10 23:03:56 +00:00
|
|
|
void
|
|
|
|
PowerManager::Reboot(ErrorResult& aRv)
|
2012-01-20 03:24:00 +00:00
|
|
|
{
|
2012-01-20 03:25:00 +00:00
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 23:03:56 +00:00
|
|
|
if (pmService) {
|
|
|
|
pmService->Reboot();
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-01-20 03:24:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
2014-07-18 01:30:47 +00:00
|
|
|
PowerManager::FactoryReset(mozilla::dom::FactoryResetReason& aReason)
|
2012-10-04 09:28:34 +00:00
|
|
|
{
|
2014-07-18 01:30:47 +00:00
|
|
|
hal::FactoryReset(aReason);
|
2012-10-04 09:28:34 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
|
|
|
PowerManager::PowerOff(ErrorResult& aRv)
|
2012-01-20 03:24:00 +00:00
|
|
|
{
|
2012-01-20 03:25:00 +00:00
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 23:03:56 +00:00
|
|
|
if (pmService) {
|
|
|
|
pmService->PowerOff();
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-01-20 03:24:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
2012-03-07 11:03:25 +00:00
|
|
|
PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
if (!mListeners.Contains(aListener)) {
|
|
|
|
mListeners.AppendElement(aListener);
|
|
|
|
}
|
2012-03-07 11:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
2012-03-07 11:03:25 +00:00
|
|
|
PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
|
|
|
PowerManager::GetWakeLockState(const nsAString& aTopic,
|
|
|
|
nsAString& aState,
|
|
|
|
ErrorResult& aRv)
|
2012-03-07 11:03:25 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 23:03:56 +00:00
|
|
|
if (pmService) {
|
|
|
|
aRv = pmService->GetWakeLockState(aTopic, aState);
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-03-07 11:03:25 +00: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 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < listeners.Length(); ++i) {
|
2012-03-07 11:03:25 +00:00
|
|
|
listeners[i]->Callback(aTopic, aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
bool
|
|
|
|
PowerManager::ScreenEnabled()
|
2012-04-03 05:51:00 +00:00
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
return hal::GetScreenEnabled();
|
2012-04-03 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
2012-04-03 05:51:00 +00:00
|
|
|
PowerManager::SetScreenEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
hal::SetScreenEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2014-05-30 03:11:23 +00:00
|
|
|
bool
|
|
|
|
PowerManager::KeyLightEnabled()
|
|
|
|
{
|
|
|
|
return hal::GetKeyLightEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManager::SetKeyLightEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
hal::SetKeyLightEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
double
|
|
|
|
PowerManager::ScreenBrightness()
|
2012-04-03 05:51:00 +00:00
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
return hal::GetScreenBrightness();
|
2012-04-03 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
|
|
|
PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
|
2012-04-03 05:51:00 +00:00
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
if (0 <= aBrightness && aBrightness <= 1) {
|
|
|
|
hal::SetScreenBrightness(aBrightness);
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2012-04-03 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
bool
|
|
|
|
PowerManager::CpuSleepAllowed()
|
2012-04-16 22:35:33 +00:00
|
|
|
{
|
2013-09-10 23:03:56 +00:00
|
|
|
return hal::GetCpuSleepAllowed();
|
2012-04-16 22:35:33 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 23:03:56 +00:00
|
|
|
void
|
2012-04-16 22:35:33 +00:00
|
|
|
PowerManager::SetCpuSleepAllowed(bool aAllowed)
|
|
|
|
{
|
|
|
|
hal::SetCpuSleepAllowed(aAllowed);
|
|
|
|
}
|
|
|
|
|
2013-07-12 14:35:35 +00:00
|
|
|
already_AddRefed<PowerManager>
|
|
|
|
PowerManager::CreateInstance(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2012-08-11 11:40:43 +00:00
|
|
|
nsRefPtr<PowerManager> powerManager = new PowerManager();
|
2013-07-12 14:35:35 +00:00
|
|
|
if (NS_FAILED(powerManager->Init(aWindow))) {
|
|
|
|
powerManager = nullptr;
|
|
|
|
}
|
2012-08-11 11:40:43 +00:00
|
|
|
|
|
|
|
return powerManager.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-20 03:24:00 +00:00
|
|
|
} // dom
|
|
|
|
} // mozilla
|