2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2014-04-29 21:27:26 +04:00
|
|
|
#include "mozilla/Services.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
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(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*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
PowerManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-09-11 03:03:56 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return MozPowerManagerBinding::Wrap(aCx, this, aGivenProto);
|
2013-09-11 03:03:56 +04:00
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
nsresult
|
2016-01-30 20:05:36 +03:00
|
|
|
PowerManager::Init(nsPIDOMWindowInner* aWindow)
|
2012-03-07 15:03:25 +04:00
|
|
|
{
|
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
|
2014-07-18 05:30:47 +04:00
|
|
|
PowerManager::FactoryReset(mozilla::dom::FactoryResetReason& aReason)
|
2012-10-04 13:28:34 +04:00
|
|
|
{
|
2014-07-18 05:30:47 +04:00
|
|
|
hal::FactoryReset(aReason);
|
2012-10-04 13:28:34 +04:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<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);
|
|
|
|
}
|
|
|
|
|
2014-05-30 07:11:23 +04:00
|
|
|
bool
|
|
|
|
PowerManager::KeyLightEnabled()
|
|
|
|
{
|
|
|
|
return hal::GetKeyLightEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManager::SetKeyLightEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
hal::SetKeyLightEnabled(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
|
|
|
already_AddRefed<PowerManager>
|
2016-01-30 20:05:36 +03:00
|
|
|
PowerManager::CreateInstance(nsPIDOMWindowInner* aWindow)
|
2013-07-12 18:35:35 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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();
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|