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
|
|
|
|
2012-01-20 07:25:00 +04:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "mozilla/HalWakeLock.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-10-02 11:26:32 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-09-26 13:12:33 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2012-09-26 13:12:33 +04:00
|
|
|
#include "nsIObserverService.h"
|
2012-01-20 07:24:00 +04:00
|
|
|
#include "PowerManagerService.h"
|
2012-03-07 15:03:25 +04:00
|
|
|
#include "WakeLock.h"
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
// For _exit().
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-20 07:24:00 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace power {
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
using namespace hal;
|
|
|
|
|
2012-01-20 07:24:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(PowerManagerService, nsIPowerManagerService)
|
|
|
|
|
2012-07-26 23:33:45 +04:00
|
|
|
/* static */ StaticRefPtr<PowerManagerService> PowerManagerService::sSingleton;
|
2012-03-07 15:03:25 +04:00
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
/* static */ already_AddRefed<PowerManagerService>
|
2012-01-20 07:24:00 +04:00
|
|
|
PowerManagerService::GetInstance()
|
|
|
|
{
|
2012-03-07 15:03:25 +04:00
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new PowerManagerService();
|
|
|
|
sSingleton->Init();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
nsRefPtr<PowerManagerService> service = sSingleton.get();
|
2012-03-07 15:03:25 +04:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManagerService::Init()
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
RegisterWakeLockObserver(this);
|
|
|
|
|
|
|
|
// NB: default to *enabling* the watchdog even when the pref is
|
|
|
|
// absent, in case the profile might be damaged and we need to
|
|
|
|
// restart to repair it.
|
|
|
|
mWatchdogTimeoutSecs =
|
|
|
|
Preferences::GetInt("shutdown.watchdog.timeoutSecs", 5);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PowerManagerService::~PowerManagerService()
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
UnregisterWakeLockObserver(this);
|
2012-03-07 15:03:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-02 11:26:32 +04:00
|
|
|
PowerManagerService::ComputeWakeLockState(const WakeLockInformation& aWakeLockInfo,
|
2012-03-07 15:03:25 +04:00
|
|
|
nsAString &aState)
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
WakeLockState state = hal::ComputeWakeLockState(aWakeLockInfo.numLocks(),
|
|
|
|
aWakeLockInfo.numHidden());
|
2012-03-07 15:03:25 +04:00
|
|
|
switch (state) {
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_UNLOCKED:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("unlocked");
|
|
|
|
break;
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_HIDDEN:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("locked-background");
|
|
|
|
break;
|
2012-10-02 11:26:32 +04:00
|
|
|
case WAKE_LOCK_STATE_VISIBLE:
|
2012-03-07 15:03:25 +04:00
|
|
|
aState.AssignLiteral("locked-foreground");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
void
|
2012-10-02 11:26:32 +04:00
|
|
|
PowerManagerService::Notify(const WakeLockInformation& aWakeLockInfo)
|
2012-03-07 15:03:25 +04:00
|
|
|
{
|
|
|
|
nsAutoString state;
|
|
|
|
ComputeWakeLockState(aWakeLockInfo, state);
|
2012-01-20 07:24:00 +04:00
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
/**
|
|
|
|
* 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(mWakeLockListeners);
|
|
|
|
|
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(aWakeLockInfo.topic(), state);
|
|
|
|
}
|
2012-01-20 07:24:00 +04:00
|
|
|
}
|
|
|
|
|
2012-09-26 13:12:33 +04:00
|
|
|
void
|
|
|
|
PowerManagerService::SyncProfile()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obsServ = services::GetObserverService();
|
|
|
|
if (obsServ) {
|
|
|
|
NS_NAMED_LITERAL_STRING(context, "shutdown-persist");
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-change-net-teardown", context.get());
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-change-teardown", context.get());
|
|
|
|
obsServ->NotifyObservers(nullptr, "profile-before-change", context.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 07:24:00 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::Reboot()
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_Reboot, mWatchdogTimeoutSecs);
|
2012-09-26 13:12:33 +04:00
|
|
|
// To synchronize any unsaved user data before rebooting.
|
|
|
|
SyncProfile();
|
2012-01-20 07:25:00 +04:00
|
|
|
hal::Reboot();
|
2012-09-26 13:12:33 +04:00
|
|
|
MOZ_NOT_REACHED();
|
2012-01-20 07:25:00 +04:00
|
|
|
return NS_OK;
|
2012-01-20 07:24:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::PowerOff()
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_PowerOff, mWatchdogTimeoutSecs);
|
2012-09-26 13:12:33 +04:00
|
|
|
// To synchronize any unsaved user data before powering off.
|
|
|
|
SyncProfile();
|
2012-01-20 07:25:00 +04:00
|
|
|
hal::PowerOff();
|
2012-09-26 13:12:33 +04:00
|
|
|
MOZ_NOT_REACHED();
|
2012-01-20 07:25:00 +04:00
|
|
|
return NS_OK;
|
2012-01-20 07:24:00 +04:00
|
|
|
}
|
|
|
|
|
2012-10-02 11:26:32 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::Restart()
|
|
|
|
{
|
|
|
|
// FIXME/bug 796826 this implementation is currently gonk-specific,
|
|
|
|
// because it relies on the Gonk to initialize the Gecko processes to
|
|
|
|
// restart B2G. It's better to do it here to have a real "restart".
|
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_Restart, mWatchdogTimeoutSecs);
|
|
|
|
// To synchronize any unsaved user data before restarting.
|
|
|
|
SyncProfile();
|
2012-12-13 03:41:07 +04:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
sync();
|
|
|
|
#endif
|
2012-10-02 11:26:32 +04:00
|
|
|
_exit(0);
|
|
|
|
MOZ_NOT_REACHED();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-07 15:03:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
|
|
|
if (mWakeLockListeners.Contains(aListener))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
mWakeLockListeners.AppendElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener)
|
|
|
|
{
|
|
|
|
mWakeLockListeners.RemoveElement(aListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::GetWakeLockState(const nsAString &aTopic, nsAString &aState)
|
|
|
|
{
|
2012-10-02 11:26:32 +04:00
|
|
|
WakeLockInformation info;
|
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 15:03:25 +04:00
|
|
|
|
|
|
|
ComputeWakeLockState(info, aState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::NewWakeLock(const nsAString &aTopic,
|
|
|
|
nsIDOMWindow *aWindow,
|
|
|
|
nsIDOMMozWakeLock **aWakeLock)
|
|
|
|
{
|
|
|
|
nsRefPtr<WakeLock> wakelock = new WakeLock();
|
|
|
|
nsresult rv = wakelock->Init(aTopic, aWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-03-15 02:00:29 +04:00
|
|
|
nsCOMPtr<nsIDOMMozWakeLock> wl(wakelock);
|
2012-03-07 15:03:25 +04:00
|
|
|
wl.forget(aWakeLock);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-20 07:24:00 +04:00
|
|
|
} // power
|
|
|
|
} // dom
|
|
|
|
} // mozilla
|