2012-01-19 19:24:00 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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-19 19:24:00 -08:00
|
|
|
|
2013-09-11 07:35:04 -07:00
|
|
|
#include "mozilla/dom/PowerManager.h"
|
|
|
|
|
2012-04-02 22:51:00 -07:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "WakeLock.h"
|
2012-01-19 19:25:00 -08:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
2012-08-11 04:40:43 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
2012-01-19 19:25:00 -08:00
|
|
|
#include "nsIPowerManagerService.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2012-01-19 19:25:00 -08:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2013-09-10 16:03:56 -07:00
|
|
|
#include "mozilla/dom/MozPowerManagerBinding.h"
|
2014-04-29 10:27:26 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-01-19 19:24:00 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-07-18 09:07:28 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager)
|
2013-09-10 16:03:56 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2012-03-07 03:03:25 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener)
|
2012-01-19 19:24:00 -08:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-29 01:57:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(PowerManager, mListeners, mWindow)
|
2013-07-18 09:07:28 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager)
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
/* virtual */ JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
PowerManager::WrapObject(JSContext* aCx)
|
2013-09-10 16:03:56 -07: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 15:27:17 -07:00
|
|
|
return MozPowerManagerBinding::Wrap(aCx, this);
|
2013-09-10 16:03:56 -07:00
|
|
|
}
|
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
nsresult
|
|
|
|
PowerManager::Init(nsIDOMWindow *aWindow)
|
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
mWindow = aWindow;
|
2012-03-07 03:03:25 -08: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 16:03:56 -07:00
|
|
|
void
|
|
|
|
PowerManager::Reboot(ErrorResult& aRv)
|
2012-01-19 19:24:00 -08:00
|
|
|
{
|
2012-01-19 19:25:00 -08:00
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 16:03:56 -07:00
|
|
|
if (pmService) {
|
|
|
|
pmService->Reboot();
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
2014-07-17 18:30:47 -07:00
|
|
|
PowerManager::FactoryReset(mozilla::dom::FactoryResetReason& aReason)
|
2012-10-04 02:28:34 -07:00
|
|
|
{
|
2014-07-17 18:30:47 -07:00
|
|
|
hal::FactoryReset(aReason);
|
2012-10-04 02:28:34 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
|
|
|
PowerManager::PowerOff(ErrorResult& aRv)
|
2012-01-19 19:24:00 -08:00
|
|
|
{
|
2012-01-19 19:25:00 -08:00
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 16:03:56 -07:00
|
|
|
if (pmService) {
|
|
|
|
pmService->PowerOff();
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
2012-03-07 03:03:25 -08:00
|
|
|
PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
if (!mListeners.Contains(aListener)) {
|
|
|
|
mListeners.AppendElement(aListener);
|
|
|
|
}
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
2012-03-07 03:03:25 -08:00
|
|
|
PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
|
|
|
mListeners.RemoveElement(aListener);
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
|
|
|
PowerManager::GetWakeLockState(const nsAString& aTopic,
|
|
|
|
nsAString& aState,
|
|
|
|
ErrorResult& aRv)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPowerManagerService> pmService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2013-09-10 16:03:56 -07:00
|
|
|
if (pmService) {
|
|
|
|
aRv = pmService->GetWakeLockState(aTopic, aState);
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
2012-03-07 03:03:25 -08: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 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < listeners.Length(); ++i) {
|
2012-03-07 03:03:25 -08:00
|
|
|
listeners[i]->Callback(aTopic, aState);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
bool
|
|
|
|
PowerManager::ScreenEnabled()
|
2012-04-02 22:51:00 -07:00
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
return hal::GetScreenEnabled();
|
2012-04-02 22:51:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
2012-04-02 22:51:00 -07:00
|
|
|
PowerManager::SetScreenEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
hal::SetScreenEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2014-05-29 20:11:23 -07:00
|
|
|
bool
|
|
|
|
PowerManager::KeyLightEnabled()
|
|
|
|
{
|
|
|
|
return hal::GetKeyLightEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManager::SetKeyLightEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
hal::SetKeyLightEnabled(aEnabled);
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
double
|
|
|
|
PowerManager::ScreenBrightness()
|
2012-04-02 22:51:00 -07:00
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
return hal::GetScreenBrightness();
|
2012-04-02 22:51:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
|
|
|
PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv)
|
2012-04-02 22:51:00 -07:00
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
if (0 <= aBrightness && aBrightness <= 1) {
|
|
|
|
hal::SetScreenBrightness(aBrightness);
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
2012-04-02 22:51:00 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
bool
|
|
|
|
PowerManager::CpuSleepAllowed()
|
2012-04-16 15:35:33 -07:00
|
|
|
{
|
2013-09-10 16:03:56 -07:00
|
|
|
return hal::GetCpuSleepAllowed();
|
2012-04-16 15:35:33 -07:00
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:56 -07:00
|
|
|
void
|
2012-04-16 15:35:33 -07:00
|
|
|
PowerManager::SetCpuSleepAllowed(bool aAllowed)
|
|
|
|
{
|
|
|
|
hal::SetCpuSleepAllowed(aAllowed);
|
|
|
|
}
|
|
|
|
|
2013-07-12 07:35:35 -07:00
|
|
|
already_AddRefed<PowerManager>
|
|
|
|
PowerManager::CreateInstance(nsPIDOMWindow* aWindow)
|
|
|
|
{
|
2012-08-11 04:40:43 -07:00
|
|
|
nsRefPtr<PowerManager> powerManager = new PowerManager();
|
2013-07-12 07:35:35 -07:00
|
|
|
if (NS_FAILED(powerManager->Init(aWindow))) {
|
|
|
|
powerManager = nullptr;
|
|
|
|
}
|
2012-08-11 04:40:43 -07:00
|
|
|
|
|
|
|
return powerManager.forget();
|
|
|
|
}
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
} // dom
|
|
|
|
} // mozilla
|