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
|
|
|
|
2012-01-19 19:25:00 -08:00
|
|
|
#include "mozilla/Hal.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "mozilla/HalWakeLock.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2012-10-02 00:26:32 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-09-26 02:12:33 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2012-09-26 02:12:33 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-01-19 19:24:00 -08:00
|
|
|
#include "PowerManagerService.h"
|
2012-03-07 03:03:25 -08:00
|
|
|
#include "WakeLock.h"
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
// For _exit().
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace power {
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
NS_IMPL_ISUPPORTS1(PowerManagerService, nsIPowerManagerService)
|
|
|
|
|
2012-07-26 12:33:45 -07:00
|
|
|
/* static */ StaticRefPtr<PowerManagerService> PowerManagerService::sSingleton;
|
2012-03-07 03:03:25 -08:00
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
/* static */ already_AddRefed<PowerManagerService>
|
2012-01-19 19:24:00 -08:00
|
|
|
PowerManagerService::GetInstance()
|
|
|
|
{
|
2012-03-07 03:03:25 -08:00
|
|
|
if (!sSingleton) {
|
|
|
|
sSingleton = new PowerManagerService();
|
|
|
|
sSingleton->Init();
|
|
|
|
ClearOnShutdown(&sSingleton);
|
|
|
|
}
|
|
|
|
|
2012-10-02 00:26:32 -07:00
|
|
|
nsRefPtr<PowerManagerService> service = sSingleton.get();
|
2012-03-07 03:03:25 -08:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PowerManagerService::Init()
|
|
|
|
{
|
2012-10-02 00:26:32 -07: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 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PowerManagerService::~PowerManagerService()
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
UnregisterWakeLockObserver(this);
|
2012-03-07 03:03:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-10-02 00:26:32 -07:00
|
|
|
PowerManagerService::ComputeWakeLockState(const WakeLockInformation& aWakeLockInfo,
|
2012-03-07 03:03:25 -08:00
|
|
|
nsAString &aState)
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
WakeLockState state = hal::ComputeWakeLockState(aWakeLockInfo.numLocks(),
|
|
|
|
aWakeLockInfo.numHidden());
|
2012-03-07 03:03:25 -08:00
|
|
|
switch (state) {
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_UNLOCKED:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("unlocked");
|
|
|
|
break;
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_HIDDEN:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("locked-background");
|
|
|
|
break;
|
2012-10-02 00:26:32 -07:00
|
|
|
case WAKE_LOCK_STATE_VISIBLE:
|
2012-03-07 03:03:25 -08:00
|
|
|
aState.AssignLiteral("locked-foreground");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-03-07 03:03:25 -08:00
|
|
|
void
|
2012-10-02 00:26:32 -07:00
|
|
|
PowerManagerService::Notify(const WakeLockInformation& aWakeLockInfo)
|
2012-03-07 03:03:25 -08:00
|
|
|
{
|
|
|
|
nsAutoString state;
|
|
|
|
ComputeWakeLockState(aWakeLockInfo, state);
|
2012-01-19 19:24:00 -08:00
|
|
|
|
2012-03-07 03:03:25 -08: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 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(aWakeLockInfo.topic(), state);
|
|
|
|
}
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2012-09-26 02:12:33 -07: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-19 19:24:00 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::Reboot()
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_Reboot, mWatchdogTimeoutSecs);
|
2012-09-26 02:12:33 -07:00
|
|
|
// To synchronize any unsaved user data before rebooting.
|
|
|
|
SyncProfile();
|
2012-01-19 19:25:00 -08:00
|
|
|
hal::Reboot();
|
2012-09-26 02:12:33 -07:00
|
|
|
MOZ_NOT_REACHED();
|
2012-01-19 19:25:00 -08:00
|
|
|
return NS_OK;
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PowerManagerService::PowerOff()
|
|
|
|
{
|
2012-10-02 00:26:32 -07:00
|
|
|
StartForceQuitWatchdog(eHalShutdownMode_PowerOff, mWatchdogTimeoutSecs);
|
2012-09-26 02:12:33 -07:00
|
|
|
// To synchronize any unsaved user data before powering off.
|
|
|
|
SyncProfile();
|
2012-01-19 19:25:00 -08:00
|
|
|
hal::PowerOff();
|
2012-09-26 02:12:33 -07:00
|
|
|
MOZ_NOT_REACHED();
|
2012-01-19 19:25:00 -08:00
|
|
|
return NS_OK;
|
2012-01-19 19:24:00 -08:00
|
|
|
}
|
|
|
|
|
2012-10-02 00:26:32 -07: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-12 15:41:07 -08:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
sync();
|
|
|
|
#endif
|
2012-10-02 00:26:32 -07:00
|
|
|
_exit(0);
|
|
|
|
MOZ_NOT_REACHED();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-07 03:03:25 -08: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 00:26:32 -07:00
|
|
|
WakeLockInformation info;
|
|
|
|
GetWakeLockInfo(aTopic, &info);
|
2012-03-07 03:03:25 -08: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-14 15:00:29 -07:00
|
|
|
nsCOMPtr<nsIDOMMozWakeLock> wl(wakelock);
|
2012-03-07 03:03:25 -08:00
|
|
|
wl.forget(aWakeLock);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-19 19:24:00 -08:00
|
|
|
} // power
|
|
|
|
} // dom
|
|
|
|
} // mozilla
|